feat: minilogin改造
This commit is contained in:
@@ -1,15 +1,16 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package userProfile
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
filePb "sundynix-micro-go/app/file/rpc/file"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
plantModel "sundynix-micro-go/app/plant/model"
|
||||
"sundynix-micro-go/app/plant/rpc/plant"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type GetUserProfileLogic struct {
|
||||
@@ -33,5 +34,98 @@ func (l *GetUserProfileLogic) GetUserProfile() (interface{}, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
|
||||
// 1. 获取 Avatar 详情
|
||||
var avatarData map[string]interface{}
|
||||
if result.AvatarId != "" {
|
||||
avatarData = l.fetchAvatar(result.AvatarId)
|
||||
}
|
||||
|
||||
// 2. 获取 Level 详情
|
||||
var levelData map[string]interface{}
|
||||
var lvl plantModel.LevelConfig
|
||||
levelFound := false
|
||||
|
||||
if result.LevelId != "" {
|
||||
if errLvl := l.svcCtx.DB.Where("id = ?", result.LevelId).First(&lvl).Error; errLvl == nil {
|
||||
levelFound = true
|
||||
}
|
||||
}
|
||||
|
||||
// 如果没有在数据库中找到 level_id,或者 level_id 为空,根据用户 totalSunlight 动态查找最匹配的等级
|
||||
if !levelFound {
|
||||
var matchedLvl plantModel.LevelConfig
|
||||
// 查找最匹配的等级:total_sunlight >= min_sunlight 中 level 最大的那一个
|
||||
errLvl := l.svcCtx.DB.Order("level desc").Where("min_sunlight <= ?", result.TotalSunlight).First(&matchedLvl).Error
|
||||
if errLvl == nil {
|
||||
lvl = matchedLvl
|
||||
levelFound = true
|
||||
// 自动更新用户资料表的 level_id,保证后续一致性
|
||||
l.svcCtx.DB.Model(&plantModel.UserProfile{}).Where("id = ?", result.Id).Update("level_id", matchedLvl.ID)
|
||||
} else {
|
||||
// 如果没有等级配置,使用 level = 1 的配置作为默认
|
||||
var firstLvl plantModel.LevelConfig
|
||||
if l.svcCtx.DB.Order("level asc").First(&firstLvl).Error == nil {
|
||||
lvl = firstLvl
|
||||
levelFound = true
|
||||
l.svcCtx.DB.Model(&plantModel.UserProfile{}).Where("id = ?", result.Id).Update("level_id", firstLvl.ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if levelFound {
|
||||
levelData = map[string]interface{}{
|
||||
"id": lvl.ID,
|
||||
"level": lvl.Level,
|
||||
"title": lvl.Title,
|
||||
"minSunlight": lvl.MinSunlight,
|
||||
"perks": lvl.Perks,
|
||||
"createdAt": lvl.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 构造兼容新旧版本的复合 JSON 数据
|
||||
res := map[string]interface{}{
|
||||
"id": result.Id,
|
||||
"userId": result.UserId,
|
||||
"nickName": result.NickName,
|
||||
"nickname": result.NickName, // 兼容旧版 json tag
|
||||
"avatarId": result.AvatarId,
|
||||
"levelId": result.LevelId,
|
||||
"currentSunlight": result.CurrentSunlight,
|
||||
"totalSunlight": result.TotalSunlight,
|
||||
"plantCount": result.PlantCount,
|
||||
"careCount": result.CareCount,
|
||||
"postCount": result.PostCount,
|
||||
"waterCount": result.WaterCount,
|
||||
"fertilizeCount": result.FertilizeCount,
|
||||
"repotCount": result.RepotCount,
|
||||
"pruneCount": result.PruneCount,
|
||||
"photoCount": result.PhotoCount,
|
||||
"avatar": avatarData,
|
||||
"level": levelData,
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (l *GetUserProfileLogic) fetchAvatar(avatarId string) map[string]interface{} {
|
||||
if avatarId == "" {
|
||||
return nil
|
||||
}
|
||||
resp, err := l.svcCtx.FileRpc.GetFilesByIds(l.ctx, &filePb.GetFilesByIdsReq{Ids: []string{avatarId}})
|
||||
if err != nil || resp == nil || len(resp.Files) == 0 {
|
||||
return nil
|
||||
}
|
||||
f := resp.Files[0]
|
||||
return map[string]interface{}{
|
||||
"id": f.Id,
|
||||
"name": f.Name,
|
||||
"url": f.Url,
|
||||
"tag": f.Tag,
|
||||
"key": f.Key,
|
||||
"suffix": f.Suffix,
|
||||
"md5": f.Md5,
|
||||
"createdAt": time.Unix(f.CreatedAt, 0).Format(time.RFC3339),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user