f
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
package admin_agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"regexp"
|
||||
|
||||
"ycc-server/app/main/model"
|
||||
"ycc-server/common/xerr"
|
||||
"ycc-server/pkg/lzkit/crypto"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"ycc-server/app/main/api/internal/svc"
|
||||
"ycc-server/app/main/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
// 手机号正则:11 位大陆手机号
|
||||
var mobileRegexp = regexp.MustCompile(`^1[3-9]\d{9}$`)
|
||||
|
||||
type AdminUpdateAgentMobileLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewAdminUpdateAgentMobileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminUpdateAgentMobileLogic {
|
||||
return &AdminUpdateAgentMobileLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AdminUpdateAgentMobileLogic) AdminUpdateAgentMobile(req *types.AdminUpdateAgentMobileReq) (resp *types.AdminUpdateAgentMobileResp, err error) {
|
||||
// 1. 校验手机号格式
|
||||
if req.Mobile == "" {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("手机号不能为空"), "")
|
||||
}
|
||||
if !mobileRegexp.MatchString(req.Mobile) {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("手机号格式不正确"), "")
|
||||
}
|
||||
|
||||
// 2. 查询代理
|
||||
agent, err := l.svcCtx.AgentModel.FindOne(l.ctx, req.AgentId)
|
||||
if err != nil {
|
||||
if errors.Is(err, model.ErrNotFound) {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("代理不存在"), "")
|
||||
}
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询代理失败, %v", err)
|
||||
}
|
||||
|
||||
// 3. 加密新手机号
|
||||
encryptedMobile, err := crypto.EncryptMobile(req.Mobile, l.svcCtx.Config.Encrypt.SecretKey)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "加密手机号失败, %v", err)
|
||||
}
|
||||
|
||||
// 4. 更新代理手机号
|
||||
agent.Mobile = encryptedMobile
|
||||
if err := l.svcCtx.AgentModel.UpdateWithVersion(l.ctx, nil, agent); err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "更新手机号失败, %v", err)
|
||||
}
|
||||
|
||||
return &types.AdminUpdateAgentMobileResp{
|
||||
Success: true,
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user