Files

89 lines
2.5 KiB
Go
Raw Normal View History

2021-09-30 12:09:45 +08:00
package user
import (
2021-10-15 15:06:02 +08:00
"SciencesServer/app/api/enterprise/model"
2021-10-12 14:47:50 +08:00
model2 "SciencesServer/app/common/model"
2021-11-24 10:50:09 +08:00
"SciencesServer/app/handle"
"SciencesServer/app/session"
2022-01-27 14:50:52 +08:00
"SciencesServer/config"
2021-11-24 10:50:09 +08:00
"SciencesServer/utils"
2021-10-12 14:47:50 +08:00
"errors"
"time"
2021-09-30 12:09:45 +08:00
)
2021-11-24 10:50:09 +08:00
type Instance struct{ *session.Enterprise }
2021-09-30 12:09:45 +08:00
2021-11-24 10:50:09 +08:00
type InstanceHandle func(session *session.Enterprise) *Instance
2021-09-30 12:09:45 +08:00
2021-10-08 09:39:40 +08:00
type (
2021-10-12 14:47:50 +08:00
// InstanceInfo 基本信息
InstanceInfo struct {
Avatar string `json:"avatar"` // 头像
Name string `json:"name"` // 名称
Currency float64 `json:"currency"` // 创新币
2021-12-22 14:53:45 +08:00
Vip int `json:"vip"` // Vip
VipDays int `json:"vip_days"` // Vip剩余天数
Identity int `json:"identity"` // 具体身份
SelectIdentity int `json:"select_identity"` // 最后一次选中的身份信息
2021-10-08 09:39:40 +08:00
}
2021-10-12 14:47:50 +08:00
// InstanceDetailInfo 详细信息
InstanceDetailInfo struct {
InstanceInfo
Email string `json:"email"` // 邮箱
Job string `json:"job"` // 职务
FixedPhone string `json:"fixed_phone"` // 固定电话
}
2021-10-08 09:39:40 +08:00
)
2021-09-30 12:09:45 +08:00
2021-10-08 09:39:40 +08:00
// Info 基本信息
2021-10-12 14:47:50 +08:00
func (c *Instance) Info() *InstanceInfo {
2022-01-27 14:50:52 +08:00
out := &InstanceInfo{
Name: c.Name,
Avatar: (&model2.UserInstance{Avatar: c.Avatar}).GetAvatarAttribute(config.SystemConfig[config.SysImageDomain]),
Currency: c.Currency,
Identity: c.Identity,
2022-01-27 14:50:52 +08:00
SelectIdentity: c.SelectIdentity,
}
2021-12-22 14:53:45 +08:00
if c.IsVip() {
out.Vip = c.Vip
out.VipDays = utils.DiffTimeDays(time.Now(), c.VipDeadline)
}
return out
2021-10-12 14:47:50 +08:00
}
2021-11-24 10:50:09 +08:00
// BindMobile 绑定手机号码
func (c *Instance) BindMobile(mobile, captcha string) error {
if !utils.ValidateMobile(mobile) {
return errors.New("操作错误,手机号码格式异常")
}
// 查询用户手机号码是否绑定
mUserInstance := model.NewUserInstance()
if _, err := model2.FirstField(mUserInstance.UserInstance, []string{"id", "uuid", "mobile"}, model2.NewWhere("uuid", c.UID)); err != nil {
return err
}
if mUserInstance.Mobile == mobile {
return nil
}
pass, err := handle.NewCaptcha().Validate(&handle.CaptchaSms{
Mobile: mobile, Captcha: captcha,
})
if err != nil {
return err
} else if !pass {
return errors.New("操作错误,验证码错误或已过期")
}
if err := model2.Updates(mUserInstance.UserInstance, map[string]interface{}{
"mobile": mobile, "updated_at": time.Now(),
}); err != nil {
return err
}
return nil
}
2021-09-30 12:09:45 +08:00
func NewInstance() InstanceHandle {
2021-11-24 10:50:09 +08:00
return func(session *session.Enterprise) *Instance {
return &Instance{Enterprise: session}
2021-09-30 12:09:45 +08:00
}
}