48 lines
1.4 KiB
Go
48 lines
1.4 KiB
Go
package auth
|
||
|
||
import (
|
||
"context"
|
||
"strings"
|
||
|
||
"apps/backend/internal/middleware"
|
||
"apps/backend/internal/response"
|
||
"apps/backend/internal/svc"
|
||
"apps/backend/internal/types"
|
||
|
||
"github.com/zeromicro/go-zero/core/logx"
|
||
)
|
||
|
||
type UpdateProfileLogic struct {
|
||
logx.Logger
|
||
ctx context.Context
|
||
svcCtx *svc.ServiceContext
|
||
}
|
||
|
||
func NewUpdateProfileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateProfileLogic {
|
||
return &UpdateProfileLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||
}
|
||
|
||
func (l *UpdateProfileLogic) UpdateProfile(req *types.AuthProfilePatchReq) (resp *types.MemberPublic, err error) {
|
||
// 頭像:nil=不改;""=清除;非空須為 media upload 回傳的 http(s) URL(禁止 data URL)
|
||
if req.AvatarUrl != nil {
|
||
u := strings.TrimSpace(*req.AvatarUrl)
|
||
if u != "" {
|
||
if strings.HasPrefix(u, "data:") || len(u) > 2048 {
|
||
return nil, response.Biz(400, 400001, "avatar_url must be a short http(s) URL from media upload, not data URL")
|
||
}
|
||
if !strings.HasPrefix(u, "http://") && !strings.HasPrefix(u, "https://") {
|
||
return nil, response.Biz(400, 400001, "avatar_url must be http(s)")
|
||
}
|
||
}
|
||
// 正規化空白
|
||
*req.AvatarUrl = u
|
||
}
|
||
uid, _ := middleware.UIDFrom(l.ctx)
|
||
m, err := l.svcCtx.Auth.UpdateUserInfo(l.ctx, uid, types.PatchFromAuthProfile(req))
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
ids, _ := l.svcCtx.Auth.ListIdentities(l.ctx, m.UID)
|
||
return types.MemberFromModelWithIdentities(m, ids), nil
|
||
}
|