Files
slogan/styleagent/service/avatar_service.go
T
adminandClaude Opus 4.7 ef22f7672a feat: slogan-agent MVP 服务端完整实现
- 用户域:注册/登录(JWT)/修改密码/个人资料
- 照片/衣橱/身形/化身:上传存储 + 3D 化身模板匹配
- 穿搭生成:天气(高德+和风+缓存) → 规则预筛 → LLM 规划(1次调用)
  → 规则评分(5维100分制) → 全低分触发 LLM 兜底创作 → 异步任务状态机
- 效果图:选主方案后异步生成 3 视角(mock/wanx 供应商 + 内容 hash 缓存 + 每日限额)
- 商业化:合作门店列表(seed 4 家)
- 冒烟:全链路端到端验证通过(mock LLM/天气),23 个 API 端点

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-07-31 12:15:11 +08:00

98 lines
2.8 KiB
Go

package service
import (
"context"
"encoding/json"
"errors"
"slogan-agent/styleagent/avatar"
"slogan-agent/styleagent/consts"
"slogan-agent/styleagent/dao"
"slogan-agent/styleagent/model/entity"
)
type avatarService struct{}
var AvatarService = new(avatarService)
// Build 构建化身(v1 同步简化:模板匹配 + 记录,异步管线后续接入)
func (s *avatarService) Build(ctx context.Context, userId int64) (*entity.AvatarModel, error) {
photos, err := dao.UserPhoto.ListByUser(ctx, userId, 0)
if err != nil {
return nil, err
}
var hasHead, hasFull bool
for _, p := range photos {
if p.Type == consts.PhotoTypeHeadshot {
hasHead = true
}
if p.Type >= consts.PhotoTypeFullFront {
hasFull = true
}
}
if !hasHead {
return nil, errors.New("请先上传大头照")
}
if !hasFull {
return nil, errors.New("请先上传全身照")
}
bm, err := dao.BodyMeasurement.GetByUser(ctx, userId)
if err != nil {
return nil, err
}
feature := &avatar.FaceFeature{SkinTone: 3, HeightCm: 170, WeightKg: 60}
if bm != nil {
feature = &avatar.FaceFeature{SkinTone: bm.SkinTone, HeightCm: bm.Height, WeightKg: bm.Weight}
}
faceId, bodyId, skinIdx := avatar.MatchTemplates(feature)
// 已有化身则重建(更新模板索引),否则新建
existing, _ := dao.AvatarModel.GetByUser(ctx, userId)
if existing != nil {
err := dao.AvatarModel.Update(ctx, existing.Id, map[string]any{
"face_template_id": faceId, "body_template_id": bodyId,
"skin_tone_index": skinIdx, "glb_url": avatar.PackGlbUrl(faceId, bodyId, skinIdx),
"build_status": consts.AvatarBuildDone, "error": "",
"params_snapshot": mustJSON(map[string]any{
"height_cm": feature.HeightCm, "weight_kg": feature.WeightKg, "skin_tone": skinIdx,
}),
"updated_at": "datetime('now','localtime')",
})
if err != nil {
return nil, err
}
existing.FaceTemplateId = faceId
existing.BodyTemplateId = bodyId
existing.SkinToneIndex = skinIdx
existing.GlbUrl = avatar.PackGlbUrl(faceId, bodyId, skinIdx)
existing.BuildStatus = consts.AvatarBuildDone
return existing, nil
}
_, err = dao.AvatarModel.Insert(ctx, &entity.AvatarModel{
UserId: userId,
FaceTemplateId: faceId,
BodyTemplateId: bodyId,
SkinToneIndex: skinIdx,
GlbUrl: avatar.PackGlbUrl(faceId, bodyId, skinIdx),
BuildStatus: consts.AvatarBuildDone,
ParamsSnapshot: mustJSON(map[string]any{
"height_cm": feature.HeightCm, "weight_kg": feature.WeightKg, "skin_tone": skinIdx,
}),
})
if err != nil {
return nil, err
}
return dao.AvatarModel.GetByUser(ctx, userId)
}
func (s *avatarService) Get(ctx context.Context, userId int64) (*entity.AvatarModel, error) {
return dao.AvatarModel.GetByUser(ctx, userId)
}
func mustJSON(v any) string {
b, _ := json.Marshal(v)
return string(b)
}