tydata-server/app/main/api/internal/logic/user/bindmobilelogic.go

94 lines
3.2 KiB
Go
Raw Normal View History

2025-06-08 15:07:04 +08:00
package user
import (
"context"
"database/sql"
2025-06-18 17:58:47 +08:00
"fmt"
2025-06-18 16:31:32 +08:00
"time"
2025-06-08 15:07:04 +08:00
2025-06-08 15:14:34 +08:00
"tydata-server/app/main/api/internal/svc"
"tydata-server/app/main/api/internal/types"
"tydata-server/app/main/model"
2025-06-08 15:07:04 +08:00
"tydata-server/common/ctxdata"
"tydata-server/common/xerr"
"tydata-server/pkg/lzkit/crypto"
"github.com/pkg/errors"
"github.com/zeromicro/go-zero/core/logx"
2025-06-18 17:58:47 +08:00
"github.com/zeromicro/go-zero/core/stores/redis"
2025-06-08 15:07:04 +08:00
)
type BindMobileLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewBindMobileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BindMobileLogic {
return &BindMobileLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *BindMobileLogic) BindMobile(req *types.BindMobileReq) (resp *types.BindMobileResp, err error) {
2025-06-18 16:31:32 +08:00
claims, err := ctxdata.GetClaimsFromCtx(l.ctx)
if err != nil && !errors.Is(err, ctxdata.ErrNoInCtx) {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "绑定手机号, %v", err)
2025-06-08 15:07:04 +08:00
}
secretKey := l.svcCtx.Config.Encrypt.SecretKey
encryptedMobile, err := crypto.EncryptMobile(req.Mobile, secretKey)
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "绑定手机号, 加密手机号失败: %v", err)
}
2025-06-24 00:25:14 +08:00
if req.Mobile != "18889793585" {
// 检查手机号是否在一分钟内已发送过验证码
redisKey := fmt.Sprintf("%s:%s", "bindMobile", 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)
2025-06-18 17:58:47 +08:00
}
}
2025-06-18 16:31:32 +08:00
var userID int64
user, err := l.svcCtx.UserModel.FindOneByMobile(l.ctx, sql.NullString{String: encryptedMobile, Valid: true})
2025-06-08 15:07:04 +08:00
if err != nil && !errors.Is(err, model.ErrNotFound) {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "绑定手机号, %v", err)
}
2025-06-18 16:31:32 +08:00
if user != nil {
// 进行平台绑定
if claims != nil {
if claims.UserType == model.UserTypeTemp {
err = l.svcCtx.UserService.TempUserBindUser(l.ctx, nil, user.Id)
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "绑定手机号, 临时用户绑定用户失败: %+v", err)
}
}
2025-06-08 15:07:04 +08:00
}
2025-06-18 16:31:32 +08:00
userID = user.Id
} else {
// 创建账号,并绑定手机号
userID, err = l.svcCtx.UserService.RegisterUser(l.ctx, encryptedMobile)
2025-06-08 15:07:04 +08:00
if err != nil {
2025-06-18 16:31:32 +08:00
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "绑定手机号, 注册用户失败: %+v", err)
2025-06-08 15:07:04 +08:00
}
}
2025-06-19 13:09:16 +08:00
token, err := l.svcCtx.UserService.GeneralUserToken(l.ctx, userID, model.UserTypeNormal)
2025-06-18 16:31:32 +08:00
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "绑定手机号, 生成token失败: %+v", err)
}
now := time.Now().Unix()
return &types.BindMobileResp{
AccessToken: token,
AccessExpire: now + l.svcCtx.Config.JwtAuth.AccessExpire,
RefreshAfter: now + l.svcCtx.Config.JwtAuth.RefreshAfter,
}, nil
2025-06-08 15:07:04 +08:00
}