互动
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.7 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package models
  2. import (
  3. "errors"
  4. "github.com/ouxuanserver/osmanthuswine/src/core"
  5. )
  6. const UserTN = TableNamePrefix + "user"
  7. //用户
  8. type User struct {
  9. Model `xorm:"extends"`
  10. //Id int `json:"id" xorm:"not null pk autoincr INT(11)"`
  11. //IsDelete bool `json:"is_delete" xorm:"default(0)" description:"是否删除"`
  12. //CreatedAt time.Time `json:"created_at" xorm:"created" description:"创建时间"`
  13. //UpdatedAt time.Time `json:"updated_at" xorm:"updated" description:"更新时间"`
  14. Username string `json:"username" xorm:"not null default '' comment('用户名') VARCHAR(128)"`
  15. Phone string `json:"phone" xorm:"not null default '' comment('手机号码') VARCHAR(128)"`
  16. Password string `json:"password" xorm:"not null default '' comment('密码') VARCHAR(128)"`
  17. Code string `json:"code" xorm:"not null default '' comment('验证码') VARCHAR(128)"`
  18. City string `json:"city" xorm:"not null default '' comment('城市') VARCHAR(128)"`
  19. Province string `json:"province" xorm:"not null default '' comment('省份') VARCHAR(128)"`
  20. Country string `json:"country" xorm:"not null default '' comment('国家') VARCHAR(128)"`
  21. Unionid string `json:"unionid" xorm:"not null default '' comment('unionid') VARCHAR(128)"`
  22. Openid string `json:"openid" xorm:"not null default '' comment('openid') VARCHAR(128)"`
  23. Nickname string `json:"nickname" xorm:"not null default '' comment('昵称') VARCHAR(128)"`
  24. Avatar string `json:"avatar" xorm:"not null default '' comment('头像') VARCHAR(128)"`
  25. Gender string `json:"gender" xorm:"not null default '' comment('性别[男,女]') VARCHAR(128)"`
  26. Balance float64 `json:"balance" xorm:"not null default 0.00 comment('余额') DECIMAL(18,2)"`
  27. Token string `json:"token" xorm:"not null default '' comment('token') VARCHAR(255)"`
  28. // 无关变量
  29. Sig string `json:"sig" xorm:"-"`
  30. }
  31. func (t *User) TableName() string {
  32. return UserTN
  33. }
  34. func (t *User) Alias(name string) string {
  35. return Alias(t, name)
  36. }
  37. func (t *User) GetUserByOpenid(openId string) (bool, error) {
  38. return core.GetXormAuto().Where("openid=?", openId).Get(t)
  39. }
  40. func (t *User) SaveUserInfo(uid interface{}) (int64, error) {
  41. return core.GetXormAuto().Where("id=?", uid).Cols("nickname", "gender",
  42. "avatar", "unionid", "city", "province", "country").Update(t)
  43. }
  44. func GetUsersByIds(ids interface{}) ([]*User, error) {
  45. users := make([]*User, 0)
  46. err := core.GetXormAuto().In("id", ids).Find(&users)
  47. return users, err
  48. }
  49. func (t *User) GetByToken(token string) error {
  50. exist, err := core.GetXormAuto().Where("is_delete=0 and token=?", token).Get(t)
  51. if err != nil {
  52. return err
  53. }
  54. if !exist {
  55. return errors.New("token认证错误")
  56. }
  57. return nil
  58. }