Files
2021-12-01 14:12:23 +08:00

73 lines
2.0 KiB
Go

package user
import (
"SciencesServer/app/api/enterprise/model"
model2 "SciencesServer/app/common/model"
"SciencesServer/app/handle"
"SciencesServer/app/session"
"SciencesServer/utils"
"errors"
"time"
)
type Instance struct{ *session.Enterprise }
type InstanceHandle func(session *session.Enterprise) *Instance
type (
// InstanceInfo 基本信息
InstanceInfo struct {
Name string `json:"name"` // 名称
Identity int `json:"identity"` // 具体身份
SelectIdentity int `json:"select_identity"` // 最后一次选中的身份信息
}
// InstanceDetailInfo 详细信息
InstanceDetailInfo struct {
InstanceInfo
Email string `json:"email"` // 邮箱
Job string `json:"job"` // 职务
FixedPhone string `json:"fixed_phone"` // 固定电话
}
)
// Info 基本信息
func (c *Instance) Info() *InstanceInfo {
return &InstanceInfo{Name: c.Name, Identity: c.Identity, SelectIdentity: c.SelectIdentity}
}
// 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
}
func NewInstance() InstanceHandle {
return func(session *session.Enterprise) *Instance {
return &Instance{Enterprise: session}
}
}