c4eede1114
CI / changes (push) Successful in 1m32s
CI / ad (push) Successful in 0s
CI / admin (push) Successful in 0s
CI / analysis (push) Successful in 3m45s
CI / bff (push) Successful in 31s
CI / chore (push) Successful in 0s
CI / equipment (push) Successful in 0s
CI / express (push) Successful in 1s
CI / factory (push) Successful in 0s
CI / message (push) Successful in 0s
CI / order (push) Successful in 0s
CI / payment (push) Successful in 0s
CI / percutaneous (push) Successful in 1s
CI / product (push) Successful in 0s
CI / sale (push) Successful in 0s
CI / record (push) Successful in 0s
CI / store (push) Successful in 0s
CI / support (push) Successful in 0s
CI / user (push) Successful in 1s
CI / task (push) Successful in 0s
CI / water (push) Successful in 0s
CI / wecom (push) Successful in 0s
CI / ws (push) Successful in 0s
182 lines
4.6 KiB
Go
182 lines
4.6 KiB
Go
package analysislogic
|
|
|
|
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"
|
|
"lone-services/services/analysis/internal/dao"
|
|
"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 UserLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLogic {
|
|
return &UserLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *UserLogic) User(in *analysis.UserReq) (*analysis.Response, error) {
|
|
var req validator.UserValidator
|
|
if msg := validate.ValidateFromProto(in, &req); msg != utils.StringEmpty {
|
|
return outParams(msg), nil
|
|
}
|
|
|
|
login, fail := common.RequireLogin(l.ctx)
|
|
if fail != nil {
|
|
return fail, nil
|
|
}
|
|
|
|
var bizId int64
|
|
var storeId int64
|
|
switch login.Type {
|
|
case dao.TypeUser:
|
|
bizId = login.BizId
|
|
case dao.TypeStore:
|
|
owner, err := common.AuthUserNoExpiry(login.BizId, req.UserId)
|
|
if err != nil || owner == utils.NumberZero {
|
|
if err != nil {
|
|
l.Errorf("common user auth: %v", err)
|
|
}
|
|
return common.FailResponse(utils.ErrorNotFund), nil
|
|
}
|
|
bizId = req.UserId
|
|
storeId = login.BizId
|
|
default:
|
|
return common.FailResponse(utils.ErrorAuthority), nil
|
|
}
|
|
if bizId < utils.NumberOne {
|
|
return common.FailResponse(utils.ErrorNotFund), nil
|
|
}
|
|
|
|
profile, err := l.loadProfile(bizId)
|
|
if err != nil {
|
|
l.Errorf("common user profile: %v", err)
|
|
return common.FailResponse(utils.Fail), nil
|
|
}
|
|
if profile == nil {
|
|
return common.FailResponse(utils.ErrorNotFund), nil
|
|
}
|
|
|
|
platformUserId := profile.GetUserId()
|
|
if platformUserId < utils.NumberOne {
|
|
platformUserId = bizId
|
|
}
|
|
birthday := profile.GetBirthday()
|
|
info := dao.ViewUserInfo{
|
|
Id: bizId,
|
|
UserId: platformUserId,
|
|
Username: profile.GetName(),
|
|
Nickname: profile.GetName(),
|
|
Avatar: profile.GetAvatar(),
|
|
Mobile: decryptMobile(profile.GetMobile()),
|
|
Gender: int(profile.GetGender()),
|
|
Birthday: birthday,
|
|
Age: utils.CalcAgeStr(dateHead(birthday)),
|
|
Tag: []dao.TagItem{},
|
|
}
|
|
|
|
if login.Type == dao.TypeStore {
|
|
info.Tag = l.loadTags(storeId, bizId)
|
|
l.applyRemark(storeId, bizId, &info)
|
|
}
|
|
return common.OkResponse(info), nil
|
|
}
|
|
|
|
func (l *UserLogic) loadProfile(bizId int64) (*userpb.BizUserItem, error) {
|
|
if l.svcCtx.UserSvcName == utils.StringEmpty {
|
|
return nil, errString("user service empty")
|
|
}
|
|
cli, err := svc.GetRpcClient(l.svcCtx.UserSvcName)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
res, err := userpb.NewUserClient(cli.Conn()).InternalUsersByBiz(l.ctx, &userpb.InternalUsersByBizReq{
|
|
ClientCode: dao.ClientCodeUserSkin,
|
|
BizIds: []int64{bizId},
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, item := range res.GetItems() {
|
|
if item.GetBizId() == bizId {
|
|
return item, nil
|
|
}
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func (l *UserLogic) loadTags(storeId, userId int64) []dao.TagItem {
|
|
var rows []dao.UserImageTag
|
|
err := model.UserImageTagModel{}.Init().Items(modelbase.Params{
|
|
Eq: map[string]string{
|
|
"user_id": strconv.FormatInt(userId, utils.NumberTen),
|
|
"store_id": strconv.FormatInt(storeId, utils.NumberTen),
|
|
"status": strconv.Itoa(utils.StatusOk),
|
|
},
|
|
}, &rows)
|
|
if err != nil {
|
|
l.Errorf("common user tags: %v", err)
|
|
return []dao.TagItem{}
|
|
}
|
|
tags := make([]dao.TagItem, 0, len(rows))
|
|
for _, row := range rows {
|
|
tags = append(tags, dao.TagItem{Id: row.Id, Tag: row.Tag})
|
|
}
|
|
return tags
|
|
}
|
|
|
|
func (l *UserLogic) applyRemark(storeId, userId int64, info *dao.ViewUserInfo) {
|
|
var remark dao.UserRemark
|
|
err := model.UserRemarkModel{}.Init().GetOne(modelbase.Params{
|
|
Eq: map[string]string{
|
|
"user_id": strconv.FormatInt(userId, utils.NumberTen),
|
|
"store_id": strconv.FormatInt(storeId, utils.NumberTen),
|
|
},
|
|
}, &remark)
|
|
if err != nil {
|
|
l.Errorf("common user remark: %v", err)
|
|
return
|
|
}
|
|
if remark.Name != utils.StringEmpty {
|
|
info.Nickname = remark.Name
|
|
}
|
|
if remark.Gender != utils.NumberZero {
|
|
info.Gender = int(remark.Gender)
|
|
}
|
|
if remark.Birthday != utils.StringEmpty {
|
|
info.Birthday = dateHead(remark.Birthday)
|
|
info.Age = utils.CalcAgeStr(info.Birthday)
|
|
}
|
|
}
|
|
|
|
func decryptMobile(mobile string) string {
|
|
plain, err := utils.DecryptPhone(mobile)
|
|
if err != nil {
|
|
return utils.StringEmpty
|
|
}
|
|
return plain
|
|
}
|
|
|
|
type strErr string
|
|
|
|
func (e strErr) Error() string { return string(e) }
|
|
|
|
func errString(msg string) error { return strErr(msg) }
|