98 lines
2.2 KiB
Go
98 lines
2.2 KiB
Go
package authser
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// SettingPageVM 账号设置页展示模型。
|
|
type SettingPageVM struct {
|
|
UserID int64
|
|
AccountNo string
|
|
HasAccount bool
|
|
HasPassword bool
|
|
NickName string
|
|
Introduction string
|
|
Email string
|
|
EmailMasked string
|
|
EmailBound bool
|
|
Avatar string // 可用头像 URL;空则模板用默认图
|
|
InviteCode string
|
|
PrivacyMode bool
|
|
VipType int
|
|
VipExpire string
|
|
Recharge int64
|
|
Score int64
|
|
DisplayName string
|
|
}
|
|
|
|
// BuildSettingVM 由 LoginResp 构建设置页 VM。
|
|
func BuildSettingVM(user *LoginResp) SettingPageVM {
|
|
if user == nil {
|
|
return SettingPageVM{}
|
|
}
|
|
nick := strings.TrimSpace(user.NickName)
|
|
account := strings.TrimSpace(user.AccountNo)
|
|
registered := user.IsRegister || user.Type == 1
|
|
hasAccount := registered && account != ""
|
|
hasPassword := hasAccount
|
|
if user.HasPassword != nil {
|
|
hasPassword = *user.HasPassword
|
|
}
|
|
email := strings.TrimSpace(user.Email)
|
|
intro := strings.TrimSpace(user.Introduction)
|
|
display := nick
|
|
if display == "" {
|
|
display = account
|
|
}
|
|
if display == "" {
|
|
display = "用户"
|
|
}
|
|
return SettingPageVM{
|
|
UserID: user.UserID,
|
|
AccountNo: account,
|
|
HasAccount: hasAccount,
|
|
HasPassword: hasPassword,
|
|
NickName: nick,
|
|
Introduction: intro,
|
|
Email: email,
|
|
EmailMasked: MaskEmail(email),
|
|
EmailBound: email != "",
|
|
Avatar: usableAvatarURL(user.Avatar),
|
|
InviteCode: strings.TrimSpace(user.InviteCode),
|
|
PrivacyMode: user.PrivacyMode,
|
|
VipType: user.VipType,
|
|
VipExpire: strings.TrimSpace(user.CurrentVipExpire),
|
|
Recharge: user.Recharge,
|
|
Score: user.Score,
|
|
DisplayName: display,
|
|
}
|
|
}
|
|
|
|
// MaskEmail 邮箱脱敏,如 ab***@mail.com。
|
|
func MaskEmail(email string) string {
|
|
email = strings.TrimSpace(email)
|
|
if email == "" {
|
|
return ""
|
|
}
|
|
at := strings.LastIndex(email, "@")
|
|
if at <= 0 || at == len(email)-1 {
|
|
return email
|
|
}
|
|
local := email[:at]
|
|
domain := email[at:]
|
|
runes := []rune(local)
|
|
if len(runes) <= 2 {
|
|
return string(runes[:1]) + "***" + domain
|
|
}
|
|
return string(runes[:2]) + "***" + domain
|
|
}
|
|
|
|
// FormatUserID 展示用 ID。
|
|
func FormatUserID(id int64) string {
|
|
if id <= 0 {
|
|
return ""
|
|
}
|
|
return fmt.Sprintf("%d", id)
|
|
}
|