108 lines
3.0 KiB
Go
108 lines
3.0 KiB
Go
package analysisuserlogic
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
|
|
"lone-services/pkg/modelbase"
|
|
"lone-services/pkg/utils"
|
|
"lone-services/pkg/validate"
|
|
analysis "lone-services/rpc/analysis/pb"
|
|
userpb "lone-services/rpc/user/pb"
|
|
analysisconfig "lone-services/services/analysis/internal/config"
|
|
"lone-services/services/analysis/internal/logic/common"
|
|
"lone-services/services/analysis/internal/model"
|
|
"lone-services/services/analysis/internal/svc"
|
|
"lone-services/services/analysis/validator"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type UserRegisterLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewUserRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserRegisterLogic {
|
|
return &UserRegisterLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *UserRegisterLogic) UserRegister(in *analysis.UserRegisterReq) (*analysis.Response, error) {
|
|
var req validator.UserRegisterValidator
|
|
if msg := validate.ValidateFromProto(in, &req); msg != utils.StringEmpty {
|
|
return common.OutResponse(utils.ErrorParams, msg), nil
|
|
}
|
|
|
|
bizId, loginUser, fail := common.RequireBizUserId(l.ctx)
|
|
if fail != nil {
|
|
return fail, nil
|
|
}
|
|
|
|
row, err := common.LoadAnalysisUser(bizId)
|
|
if err != nil {
|
|
l.Errorf("register load user: %v", err)
|
|
return common.FailResponse(utils.Fail), nil
|
|
}
|
|
if row == nil {
|
|
return common.FailResponse(utils.ErrorNotFund), nil
|
|
}
|
|
|
|
wxCfg, cfgErr := analysisconfig.LoadWechatConfig()
|
|
if cfgErr != nil {
|
|
l.Errorf("register wechat config: %v", cfgErr)
|
|
return common.OutResponse(utils.Fail, "微信配置缺失"), nil
|
|
}
|
|
|
|
token, tokenErr := wxCfg.NewAuthClient().AccessToken(l.ctx)
|
|
if tokenErr != nil {
|
|
l.Errorf("register access token: %v", tokenErr)
|
|
return common.FailResponse(utils.Fail), nil
|
|
}
|
|
phone, phoneErr := wxCfg.NewAuthClient().GetPhoneNumber(token.AccessToken, req.Code)
|
|
if phoneErr != nil {
|
|
l.Errorf("register get phone: %v", phoneErr)
|
|
return common.FailResponse(utils.Fail), nil
|
|
}
|
|
|
|
mobile := phone.PhoneNumber
|
|
if mobile == utils.StringEmpty {
|
|
mobile = phone.PurePhoneNumber
|
|
}
|
|
if mobile == utils.StringEmpty {
|
|
return common.FailResponse(utils.Fail), nil
|
|
}
|
|
|
|
if l.svcCtx.UserSvcName == utils.StringEmpty {
|
|
return common.FailResponse(utils.Fail), nil
|
|
}
|
|
cli, err := svc.GetRpcClient(l.svcCtx.UserSvcName)
|
|
if err != nil {
|
|
l.Errorf("register get user rpc: %v", err)
|
|
return common.FailResponse(utils.Fail), nil
|
|
}
|
|
if _, err = userpb.NewUserClient(cli.Conn()).InternalUpdateMobile(l.ctx, &userpb.InternalUpdateMobileReq{
|
|
UserId: loginUser.ID,
|
|
Mobile: mobile,
|
|
}); err != nil {
|
|
l.Errorf("register update mobile: %v", err)
|
|
return common.FailResponse(utils.Fail), nil
|
|
}
|
|
|
|
_, err = model.UserModel{}.Init().Edit(modelbase.Params{
|
|
Eq: map[string]string{"id": strconv.FormatInt(bizId, utils.NumberTen)},
|
|
}, map[string]interface{}{
|
|
"is_registered": uint8(utils.StatusOk),
|
|
})
|
|
if err != nil {
|
|
l.Errorf("register update flag: %v", err)
|
|
return common.FailResponse(utils.Fail), nil
|
|
}
|
|
|
|
return common.OkResponse(nil), nil
|
|
}
|