2024-11-21 12:18:28 +08:00
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2025-01-03 02:13:31 +08:00
|
|
|
"time"
|
2025-04-27 12:17:18 +08:00
|
|
|
"tyc-server/app/main/model"
|
2025-04-09 15:58:06 +08:00
|
|
|
jwtx "tyc-server/common/jwt"
|
|
|
|
"tyc-server/common/tool"
|
|
|
|
"tyc-server/common/xerr"
|
2025-04-09 17:27:40 +08:00
|
|
|
"tyc-server/pkg/lzkit/crypto"
|
2025-04-09 15:58:06 +08:00
|
|
|
"tyc-server/pkg/lzkit/lzUtils"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2024-11-21 12:18:28 +08:00
|
|
|
|
2025-04-27 12:17:18 +08:00
|
|
|
"tyc-server/app/main/api/internal/svc"
|
|
|
|
"tyc-server/app/main/api/internal/types"
|
2024-11-21 12:18:28 +08:00
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MobileLoginLogic struct {
|
|
|
|
logx.Logger
|
|
|
|
ctx context.Context
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMobileLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MobileLoginLogic {
|
|
|
|
return &MobileLoginLogic{
|
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
|
ctx: ctx,
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *MobileLoginLogic) MobileLogin(req *types.MobileLoginReq) (resp *types.MobileCodeLoginResp, err error) {
|
2025-04-09 17:27:40 +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)
|
|
|
|
}
|
|
|
|
user, findUserErr := l.svcCtx.UserModel.FindOneByMobile(l.ctx, encryptedMobile)
|
2024-11-21 12:18:28 +08:00
|
|
|
if findUserErr != nil && findUserErr != model.ErrNotFound {
|
2025-04-09 17:27:40 +08:00
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "手机登录, 读取数据库获取用户失败, mobile%s, err: %+v", encryptedMobile, err)
|
2024-11-21 12:18:28 +08:00
|
|
|
}
|
|
|
|
if user == nil {
|
2025-04-09 17:27:40 +08:00
|
|
|
return nil, errors.Wrapf(xerr.NewErrMsg("手机号码未注册"), "手机登录, 手机号未注册:%s", encryptedMobile)
|
2024-11-21 12:18:28 +08:00
|
|
|
}
|
|
|
|
if !(tool.Md5ByString(req.Password) == lzUtils.NullStringToString(user.Password)) {
|
2025-04-09 17:27:40 +08:00
|
|
|
return nil, errors.Wrapf(xerr.NewErrMsg("密码不正确"), "手机登录, 密码匹配不正确%s", encryptedMobile)
|
2024-11-21 12:18:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
token, generaErr := jwtx.GenerateJwtToken(user.Id, l.svcCtx.Config.JwtAuth.AccessSecret, l.svcCtx.Config.JwtAuth.AccessExpire)
|
|
|
|
if generaErr != nil {
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "手机登录, 生成token失败 : %d", user.Id)
|
|
|
|
}
|
|
|
|
|
2025-01-03 02:13:31 +08:00
|
|
|
// 获取当前时间戳
|
|
|
|
now := time.Now().Unix()
|
2024-11-21 12:18:28 +08:00
|
|
|
return &types.MobileCodeLoginResp{
|
|
|
|
AccessToken: token,
|
2025-01-03 02:13:31 +08:00
|
|
|
AccessExpire: now + l.svcCtx.Config.JwtAuth.AccessExpire,
|
|
|
|
RefreshAfter: now + l.svcCtx.Config.JwtAuth.RefreshAfter,
|
2024-11-21 12:18:28 +08:00
|
|
|
}, nil
|
|
|
|
}
|