1、新接textverified

This commit is contained in:
2025-08-23 16:38:04 +08:00
parent 6c95e6fb04
commit 82d5acc873
45 changed files with 3154 additions and 461 deletions

View File

@ -2,9 +2,16 @@ package global
//ConfigKey
const (
TrafficProxyEffectiveDay = "traffic_proxy_effective_day" //流量代理有效天数
LongNumberRenewDeductionStandard = "long_number_renew_deduction_standard" //长效号码续费标准
IPRenewDeductionStandard = "ip_renew_deduction_standard" //长效IP续费kk扣除标准
//流量代理有效天数
TrafficProxyEffectiveDay = "traffic_proxy_effective_day"
//短期号码扣除标准
ShortNumberDeductionStandard = "number_fee_short_term"
//长效号码扣除标准
LongNumberDeductionStandard = "number_fee_long_term"
//长效号码续费标准
LongNumberRenewDeductionStandard = "long_number_renew_deduction_standard"
//长效IP续费kk扣除标准
IPRenewDeductionStandard = "ip_renew_deduction_standard"
ProxyExpiryTime = "proxy_expiry_time" //代理过期记录保留时长
SmsExpiryTime = "sms_expiry_time" //接码过期记录保留时长

View File

@ -0,0 +1,7 @@
package global
//短效平台code 字典【sms_platform】
const (
SmsPlatformDaisysms = "daisysms"
SmsPlatformTextVerified = "textverified"
)

View File

@ -0,0 +1,6 @@
package global
const (
// TextVerified Token
TextVerifiedToken = "TextVerifiedToken"
)

View File

@ -1,13 +1,28 @@
package middleware
import (
"errors"
"fmt"
"strconv"
"time"
"github.com/go-admin-team/go-admin-core/sdk/config"
jwt "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth"
"go-admin/app/admin/models"
"go-admin/common/middleware/handler"
"go-admin/common/rediskey"
"go-admin/common/statuscode"
"go-admin/utils/redishelper"
"github.com/bytedance/sonic"
"github.com/gin-gonic/gin"
"github.com/go-admin-team/go-admin-core/sdk/config"
"github.com/go-admin-team/go-admin-core/sdk/pkg"
jwt "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth"
)
var ErrQueryUserId = errors.New("查询用户失败")
var ErrNoAccount = errors.New("没有API用户")
var ErrApiUnActived = errors.New("API未激活")
// AuthInit jwt验证new
func AuthInit() (*jwt.GinJWTMiddleware, error) {
timeout := time.Hour
@ -33,4 +48,108 @@ func AuthInit() (*jwt.GinJWTMiddleware, error) {
TimeFunc: time.Now,
})
}
}
// apikey授权认证
func FrontedAuth(c *gin.Context) {
// 从请求头中获取 token 和 os
apikey := c.GetHeader("x-api-key")
// 如果 token 不存在,返回未登录的状态
if len(apikey) == 0 {
err := ResponseWithStatus(c, statuscode.Unauthorized)
if err != nil {
return
}
c.Abort() // 停止后续中间件的执行
return
}
// 验证 token 并获取结果
key := fmt.Sprintf(rediskey.MemberApiKey, apikey)
val, err := redishelper.DefaultRedis.GetString(key)
if err != nil || val == "" {
ResponseWithStatus(c, statuscode.Unauthorized)
c.Abort() // 停止后续中间件的执行
}
// 将解析后的 token 设置到请求头中
c.Set("apiKey", apikey)
// 继续处理请求
c.Next()
}
// ResponseWithStatus 带状态的响应
func ResponseWithStatus(ctx *gin.Context, code int, data ...interface{}) error {
resp := statuscode.Response{
Code: code,
Msg: "un authorized",
}
// resp.RequestID = pkg.GenerateMsgIDFromContext(ctx)
if len(data) > 0 {
resp.Data = data[0]
}
switch code {
case 401, 500, 405, 404:
ctx.JSON(code, resp)
default:
ctx.JSON(200, resp)
}
return nil
}
// 获取ApiKey用户id
func GetUserIdWithApiKey(ctx *gin.Context) (int, error) {
apikey, ok := ctx.Get("apiKey")
if !ok {
return 0, errors.New("apiKey not found")
}
return strconv.Atoi(apikey.(string))
}
// 获取用户id 根据ApiKey
func GetUserIdByApiKey(c *gin.Context) (int, error) {
apiKey, ok := c.Get("apiKey")
if !ok {
return 0, errors.New("apiKey not found")
}
db, err := pkg.GetOrm(c)
if err != nil {
return 0, err
}
var userId int
val, err := redishelper.DefaultRedis.GetString(fmt.Sprintf(rediskey.MemberApiKey, apiKey))
if err != nil {
return userId, err
}
if val == "" {
var user models.MemberApi
if dbErr := db.Model(&user).Where("api_key = ?", apiKey).First(&user).Error; dbErr != nil {
return userId, ErrNoAccount
}
if user.Status != 1 {
return userId, ErrApiUnActived
}
userId = user.UserId
} else {
data := models.MemberApi{}
err = sonic.UnmarshalString(val, &data)
if err != nil {
return userId, err
}
userId = data.UserId
}
return userId, nil
}

View File

@ -0,0 +1,6 @@
package rediskey
const (
//用户api
MemberApiKey = "member_api:%s"
)

View File

@ -2,6 +2,14 @@ package statuscode
import "fmt"
type Response struct {
Status int `json:"status"`
Code int `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data"`
RequestID string `json:"RequestId"`
}
var StatusCodeZh = map[int]string{
Success: "成功",
AccountExisted: "账号已存在",
@ -17,15 +25,22 @@ var StatusCodeZh = map[int]string{
MaxPriceExceeded: "超过最大接受单价",
NoNumbers: "号码不足",
RentalsNotFinished: "需要先完成部分租赁才能继续租赁",
Unauthorized: "未授权",
ApiUnActived: "API未激活",
InvalidParams: "参数错误",
SmsCancel: "短信验证码_手机号过期",
SmsNoActivation: "短信验证码_手机号不存在",
SmsWaitCode: "短信验证码_等待验证码",
SmsLongNumWaitCode: "短信验证码_长效号码已唤醒",
SmsNotExisted: "号码不存在",
SmsNotExpired: "号码未过期无法删除",
SmsNotAutoRenew: "短效号码无法自动续期",
SmsServiceUnavailable: "%s服务暂不可用",
SmsCancel: "短信验证码_手机号过期",
SmsNoActivation: "短信验证码_手机号不存在",
SmsWaitCode: "短信验证码_等待验证码",
SmsLongNumWaitCode: "短信验证码_长效号码已唤醒",
SmsNotExisted: "号码不存在",
SmsNotExpired: "号码未过期无法删除",
SmsNotAutoRenew: "短效号码无法自动续期",
SmsServiceUnavailable: "%s服务暂不可用",
SmsPlatformUnavailable: "通道不可用",
SmsInvalidType: "短信验证码_无效类型",
SmsOutOfStockOrUnavailable: "短信验证码_缺货或服务不可用",
SmsRentalRefundNotPermitted: "短信验证码_租赁退款不允许",
}
var StatusCodeEn = map[int]string{
@ -43,15 +58,22 @@ var StatusCodeEn = map[int]string{
MaxPriceExceeded: "max price exceeded",
NoNumbers: "no numbers",
RentalsNotFinished: "need to finish some rentals before renting more",
Unauthorized: "un authorized",
ApiUnActived: "API un actived.",
InvalidParams: "invalid params",
SmsCancel: "sms code expired",
SmsNoActivation: "sms code not exist",
SmsWaitCode: "sms code wait for input",
SmsLongNumWaitCode: "sms code long num wake up",
SmsNotExisted: "number not exist",
SmsNotExpired: "number not expired, can not delete",
SmsNotAutoRenew: "num can not auto renew",
SmsServiceUnavailable: "%s service unavailable",
SmsCancel: "sms code expired",
SmsNoActivation: "sms code not exist",
SmsWaitCode: "sms code wait for input",
SmsLongNumWaitCode: "sms code long num wake up",
SmsNotExisted: "number not exist",
SmsNotExpired: "number not expired, can not delete",
SmsNotAutoRenew: "num can not auto renew",
SmsServiceUnavailable: "%s service unavailable",
SmsPlatformUnavailable: "channel unavailable",
SmsInvalidType: "sms type invalid",
SmsOutOfStockOrUnavailable: "sms out of stock or unavailable",
SmsRentalRefundNotPermitted: "sms rental refund not permitted",
}
// GetMsg 获取状态码对应的消息
@ -108,6 +130,12 @@ const (
NoNumbers = 10012
// Need to finish some rentals before renting more
RentalsNotFinished = 10013
// 未授权
Unauthorized = 10014
// Api未激活
ApiUnActived = 10015
//参数错误
InvalidParams = 10016
//短信验证码_手机号过期
SmsCancel = 20014
@ -125,4 +153,12 @@ const (
SmsNotAutoRenew = 20020
//短信验证码_服务暂不可用
SmsServiceUnavailable = 20021
//短信-通道不可用
SmsPlatformUnavailable = 20022
//短信-未知类型
SmsInvalidType = 20023
// 短信-缺货或服务不可用
SmsOutOfStockOrUnavailable = 20024
// 短信-租赁退款不允许
SmsRentalRefundNotPermitted = 20025
)