Files
yunzerwebsiteallinone/go/services/user_avatar.go
T
2026-09-29 18:25:09 +08:00

46 lines
1.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package services
import (
"errors"
"strings"
"time"
"server/models"
)
// 用户头像统一存在认证中心身份表 yz_auth_identity.avatar:
// /auth/userinfo 会返回该字段,前端登录回跳、顶部栏、个人中心都从这里取,
// 业务侧(yz_system_tenant_user)不再另存一份,避免两处不一致。
// UpdateIdentityAvatar 更新身份头像
func UpdateIdentityAvatar(identityID uint64, avatarURL string) error {
if identityID == 0 {
return errors.New("身份ID不能为空")
}
url := strings.TrimSpace(avatarURL)
if url == "" {
return errors.New("头像地址不能为空")
}
_, err := models.Orm.QueryTable(new(models.AuthIdentity)).
Filter("id", identityID).
Update(map[string]interface{}{
"avatar": url,
"update_time": time.Now(),
})
return err
}
// GetIdentityAvatar 读取身份头像;未设置或身份不存在时返回空字符串
func GetIdentityAvatar(identityID uint64) string {
if identityID == 0 {
return ""
}
var identity models.AuthIdentity
if err := models.Orm.QueryTable(new(models.AuthIdentity)).
Filter("id", identityID).
One(&identity); err != nil || identity.Avatar == nil {
return ""
}
return strings.TrimSpace(*identity.Avatar)
}