This commit is contained in:
18278715334
2026-01-13 16:12:30 +08:00
parent ba7f219882
commit 2bcbede5cd
8 changed files with 131 additions and 37 deletions

Binary file not shown.

View File

@@ -4,6 +4,7 @@ import (
"context" "context"
"database/sql" "database/sql"
"fmt" "fmt"
"os"
"time" "time"
"tydata-server/app/main/api/internal/service" "tydata-server/app/main/api/internal/service"
@@ -43,17 +44,26 @@ func (l *AgentRealNameLogic) AgentRealName(req *types.AgentRealNameReq) (resp *t
if err != nil { if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "代理实名, 加密手机号失败: %v", err) return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "代理实名, 加密手机号失败: %v", err)
} }
// 检查手机号是否在一分钟内已发送过验证码 // 开发环境固定验证码为138888
redisKey := fmt.Sprintf("%s:%s", "realName", encryptedMobile) env := os.Getenv("ENV")
cacheCode, err := l.svcCtx.Redis.Get(redisKey) if env == "development" {
if err != nil { if req.Code != "138888" {
if errors.Is(err, redis.Nil) { return nil, errors.Wrapf(xerr.NewErrMsg("验证码不正确"), "开发环境验证码应为138888")
return nil, errors.Wrapf(xerr.NewErrMsg("验证码已过期"), "代理实名, 验证码过期: %s", encryptedMobile) }
logx.Infof("开发环境:验证码验证通过 %s", req.Mobile)
} else {
// 检查手机号是否在一分钟内已发送过验证码
redisKey := fmt.Sprintf("%s:%s", "realName", encryptedMobile)
cacheCode, err := l.svcCtx.Redis.Get(redisKey)
if err != nil {
if errors.Is(err, redis.Nil) {
return nil, errors.Wrapf(xerr.NewErrMsg("验证码已过期"), "代理实名, 验证码过期: %s", encryptedMobile)
}
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "代理实名, 读取验证码redis缓存失败, mobile: %s, err: %+v", encryptedMobile, err)
}
if cacheCode != req.Code {
return nil, errors.Wrapf(xerr.NewErrMsg("验证码不正确"), "代理实名, 验证码不正确: %s", encryptedMobile)
} }
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "代理实名, 读取验证码redis缓存失败, mobile: %s, err: %+v", encryptedMobile, err)
}
if cacheCode != req.Code {
return nil, errors.Wrapf(xerr.NewErrMsg("验证码不正确"), "代理实名, 验证码不正确: %s", encryptedMobile)
} }
agent, err := l.svcCtx.AgentModel.FindOneByUserId(l.ctx, userID) agent, err := l.svcCtx.AgentModel.FindOneByUserId(l.ctx, userID)
if err != nil { if err != nil {

View File

@@ -8,6 +8,7 @@ import (
"tydata-server/pkg/lzkit/crypto" "tydata-server/pkg/lzkit/crypto"
"database/sql" "database/sql"
"fmt" "fmt"
"os"
"time" "time"
"github.com/pkg/errors" "github.com/pkg/errors"
@@ -44,7 +45,14 @@ func (l *ApplyForAgentLogic) ApplyForAgent(req *types.AgentApplyReq) (resp *type
if err != nil { if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "加密手机号失败: %v", err) return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "加密手机号失败: %v", err)
} }
if req.Mobile != "18889793585" { // 开发环境固定验证码为138888
env := os.Getenv("ENV")
if env == "development" {
if req.Code != "138888" {
return nil, errors.Wrapf(xerr.NewErrMsg("验证码不正确"), "开发环境验证码应为138888")
}
logx.Infof("开发环境:验证码验证通过 %s", req.Mobile)
} else if req.Mobile != "18889793585" {
// 校验验证码 // 校验验证码
redisKey := fmt.Sprintf("%s:%s", "agentApply", encryptedMobile) redisKey := fmt.Sprintf("%s:%s", "agentApply", encryptedMobile)
cacheCode, err := l.svcCtx.Redis.Get(redisKey) cacheCode, err := l.svcCtx.Redis.Get(redisKey)

View File

@@ -2,11 +2,12 @@ package auth
import ( import (
"context" "context"
"tydata-server/common/xerr"
"tydata-server/pkg/lzkit/crypto"
"fmt" "fmt"
"math/rand" "math/rand"
"os"
"time" "time"
"tydata-server/common/xerr"
"tydata-server/pkg/lzkit/crypto"
"github.com/pkg/errors" "github.com/pkg/errors"
@@ -52,15 +53,22 @@ func (l *SendSmsLogic) SendSms(req *types.SendSmsReq) error {
return errors.Wrapf(xerr.NewErrMsg("一分钟内不能重复发送验证码"), "短信发送, 手机号1分钟内重复请求发送验证码: %s", encryptedMobile) return errors.Wrapf(xerr.NewErrMsg("一分钟内不能重复发送验证码"), "短信发送, 手机号1分钟内重复请求发送验证码: %s", encryptedMobile)
} }
code := fmt.Sprintf("%06d", rand.New(rand.NewSource(time.Now().UnixNano())).Intn(1000000)) // 开发环境固定验证码为138888
env := os.Getenv("ENV")
// 发送短信 var code string
smsResp, err := l.sendSmsRequest(req.Mobile, code) if env == "development" {
if err != nil { code = "138888"
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "短信发送, 调用阿里客户端失败: %v", err) logx.Infof("开发环境:固定验证码为 %s", code)
} } else {
if *smsResp.Body.Code != "OK" { code = fmt.Sprintf("%06d", rand.New(rand.NewSource(time.Now().UnixNano())).Intn(1000000))
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "短信发送, 阿里客户端响应失败: %s", *smsResp.Body.Message) // 发送短信
smsResp, err := l.sendSmsRequest(req.Mobile, code)
if err != nil {
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "短信发送, 调用阿里客户端失败: %v", err)
}
if *smsResp.Body.Code != "OK" {
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "短信发送, 阿里客户端响应失败: %s", *smsResp.Body.Message)
}
} }
codeKey := fmt.Sprintf("%s:%s", req.ActionType, encryptedMobile) codeKey := fmt.Sprintf("%s:%s", req.ActionType, encryptedMobile)
// 将验证码保存到 Redis设置过期时间 // 将验证码保存到 Redis设置过期时间

View File

@@ -2,9 +2,12 @@ package pay
import ( import (
"context" "context"
"database/sql"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"fmt" "fmt"
"os"
"time"
"tydata-server/app/main/api/internal/svc" "tydata-server/app/main/api/internal/svc"
"tydata-server/app/main/api/internal/types" "tydata-server/app/main/api/internal/types"
"tydata-server/app/main/model" "tydata-server/app/main/model"
@@ -27,6 +30,7 @@ type PaymentTypeResp struct {
amount float64 amount float64
outTradeNo string outTradeNo string
description string description string
orderID int64
} }
func NewPaymentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PaymentLogic { func NewPaymentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PaymentLogic {
@@ -71,6 +75,40 @@ func (l *PaymentLogic) Payment(req *types.PaymentReq) (resp *types.PaymentResp,
if err != nil { if err != nil {
return nil, err return nil, err
} }
// 开发环境测试支付模式:事务提交后处理订单状态更新和后续流程
isDevTestPayment := os.Getenv("ENV") == "development"
if isDevTestPayment && paymentTypeResp != nil && paymentTypeResp.orderID != 0 {
// 使用 goroutine 异步处理,确保事务已完全提交
go func() {
// 短暂延迟,确保事务已完全提交到数据库
time.Sleep(200 * time.Millisecond)
finalOrderID := paymentTypeResp.orderID
// 查找订单并更新状态为已支付
order, findOrderErr := l.svcCtx.OrderModel.FindOne(context.Background(), finalOrderID)
if findOrderErr != nil {
logx.Errorf("开发测试模式查找订单失败订单ID: %d, 错误: %v", finalOrderID, findOrderErr)
return
}
// 更新订单状态为已支付
order.Status = "paid"
now := time.Now()
order.PayTime = sql.NullTime{Time: now, Valid: true}
// 更新订单
updateErr := l.svcCtx.OrderModel.UpdateWithVersion(context.Background(), nil, order)
if updateErr != nil {
logx.Errorf("开发测试模式更新订单状态失败订单ID: %d, 错误: %v", finalOrderID, updateErr)
return
}
logx.Infof("开发测试模式订单状态已更新为已支付订单ID: %d", finalOrderID)
}()
}
switch v := prepayData.(type) { switch v := prepayData.(type) {
case string: case string:
// 如果 prepayData 是字符串类型,直接返回 // 如果 prepayData 是字符串类型,直接返回
@@ -157,7 +195,7 @@ func (l *PaymentLogic) QueryOrderPayment(req *types.PaymentReq, session sqlx.Ses
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "生成订单, 保存代理订单失败: %+v", agentOrderInsert) return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "生成订单, 保存代理订单失败: %+v", agentOrderInsert)
} }
} }
return &PaymentTypeResp{amount: amount, outTradeNo: outTradeNo, description: product.ProductName}, nil return &PaymentTypeResp{amount: amount, outTradeNo: outTradeNo, description: product.ProductName, orderID: orderID}, nil
} }
func (l *PaymentLogic) AgentVipOrderPayment(req *types.PaymentReq, session sqlx.Session) (resp *PaymentTypeResp, err error) { func (l *PaymentLogic) AgentVipOrderPayment(req *types.PaymentReq, session sqlx.Session) (resp *PaymentTypeResp, err error) {
userID, getUidErr := ctxdata.GetUidFromCtx(l.ctx) userID, getUidErr := ctxdata.GetUidFromCtx(l.ctx)

View File

@@ -5,6 +5,7 @@ import (
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"fmt" "fmt"
"os"
"time" "time"
"tydata-server/app/main/api/internal/service" "tydata-server/app/main/api/internal/service"
"tydata-server/app/main/model" "tydata-server/app/main/model"
@@ -558,6 +559,16 @@ func (l *QueryServiceLogic) DecryptData(data string) ([]byte, error) {
// 校验验证码 // 校验验证码
func (l *QueryServiceLogic) VerifyCode(mobile string, code string) error { func (l *QueryServiceLogic) VerifyCode(mobile string, code string) error {
// 开发环境固定验证码为138888
env := os.Getenv("ENV")
if env == "development" {
if code == "138888" {
logx.Infof("开发环境:验证码验证通过 %s", mobile)
return nil
}
return errors.Wrapf(xerr.NewErrMsg("验证码不正确"), "开发环境验证码应为138888: %s", mobile)
}
secretKey := l.svcCtx.Config.Encrypt.SecretKey secretKey := l.svcCtx.Config.Encrypt.SecretKey
encryptedMobile, err := crypto.EncryptMobile(mobile, secretKey) encryptedMobile, err := crypto.EncryptMobile(mobile, secretKey)
if err != nil { if err != nil {

View File

@@ -4,6 +4,7 @@ import (
"context" "context"
"database/sql" "database/sql"
"fmt" "fmt"
"os"
"time" "time"
"tydata-server/app/main/api/internal/svc" "tydata-server/app/main/api/internal/svc"
@@ -42,7 +43,14 @@ func (l *BindMobileLogic) BindMobile(req *types.BindMobileReq) (resp *types.Bind
if err != nil { if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "绑定手机号, 加密手机号失败: %v", err) return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "绑定手机号, 加密手机号失败: %v", err)
} }
if req.Mobile != "18889793585" { // 开发环境固定验证码为138888
env := os.Getenv("ENV")
if env == "development" {
if req.Code != "138888" {
return nil, errors.Wrapf(xerr.NewErrMsg("验证码不正确"), "开发环境验证码应为138888")
}
logx.Infof("开发环境:验证码验证通过 %s", req.Mobile)
} else if req.Mobile != "18889793585" {
// 检查手机号是否在一分钟内已发送过验证码 // 检查手机号是否在一分钟内已发送过验证码
redisKey := fmt.Sprintf("%s:%s", "bindMobile", encryptedMobile) redisKey := fmt.Sprintf("%s:%s", "bindMobile", encryptedMobile)
cacheCode, err := l.svcCtx.Redis.Get(redisKey) cacheCode, err := l.svcCtx.Redis.Get(redisKey)

View File

@@ -2,14 +2,15 @@ package user
import ( import (
"context" "context"
"database/sql"
"fmt"
"os"
"time"
"tydata-server/app/main/api/internal/svc" "tydata-server/app/main/api/internal/svc"
"tydata-server/app/main/api/internal/types" "tydata-server/app/main/api/internal/types"
"tydata-server/app/main/model" "tydata-server/app/main/model"
"tydata-server/common/xerr" "tydata-server/common/xerr"
"tydata-server/pkg/lzkit/crypto" "tydata-server/pkg/lzkit/crypto"
"database/sql"
"fmt"
"time"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/zeromicro/go-zero/core/stores/redis" "github.com/zeromicro/go-zero/core/stores/redis"
@@ -37,17 +38,27 @@ func (l *MobileCodeLoginLogic) MobileCodeLogin(req *types.MobileCodeLoginReq) (r
if err != nil { if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "手机登录, 加密手机号失败: %+v", err) return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "手机登录, 加密手机号失败: %+v", err)
} }
// 检查手机号是否在一分钟内已发送过验证码
redisKey := fmt.Sprintf("%s:%s", "login", encryptedMobile) // 开发环境固定验证码为138888
cacheCode, err := l.svcCtx.Redis.Get(redisKey) env := os.Getenv("ENV")
if err != nil { if env == "development" {
if errors.Is(err, redis.Nil) { if req.Code != "138888" {
return nil, errors.Wrapf(xerr.NewErrMsg("验证码已过期"), "手机登录, 验证码过期: %s", encryptedMobile) return nil, errors.Wrapf(xerr.NewErrMsg("验证码不正确"), "开发环境验证码应为138888")
}
logx.Infof("开发环境:验证码验证通过 %s", req.Mobile)
} else {
// 检查手机号是否在一分钟内已发送过验证码
redisKey := fmt.Sprintf("%s:%s", "login", encryptedMobile)
cacheCode, err := l.svcCtx.Redis.Get(redisKey)
if err != nil {
if errors.Is(err, redis.Nil) {
return nil, errors.Wrapf(xerr.NewErrMsg("验证码已过期"), "手机登录, 验证码过期: %s", encryptedMobile)
}
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "手机登录, 读取验证码redis缓存失败, mobile: %s, err: %+v", encryptedMobile, err)
}
if cacheCode != req.Code {
return nil, errors.Wrapf(xerr.NewErrMsg("验证码不正确"), "手机登录, 验证码不正确: %s", encryptedMobile)
} }
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "手机登录, 读取验证码redis缓存失败, mobile: %s, err: %+v", encryptedMobile, err)
}
if cacheCode != req.Code {
return nil, errors.Wrapf(xerr.NewErrMsg("验证码不正确"), "手机登录, 验证码不正确: %s", encryptedMobile)
} }
var userID int64 var userID int64
user, findUserErr := l.svcCtx.UserModel.FindOneByMobile(l.ctx, sql.NullString{String: encryptedMobile, Valid: true}) user, findUserErr := l.svcCtx.UserModel.FindOneByMobile(l.ctx, sql.NullString{String: encryptedMobile, Valid: true})