user.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package models
  2. import (
  3. "time"
  4. "github.com/beego/beego/v2/client/orm"
  5. )
  6. type User struct {
  7. UserId int `orm:"pk" description:"用户id"`
  8. OpenId string `description:"openid"`
  9. UnionId string `description:"unionid"`
  10. NickName string `description:"用户昵称"`
  11. RealName string `description:"姓名"`
  12. Phone string `description:"手机号"`
  13. AreaCode string `description:"区号"`
  14. Email string `description:"邮箱"`
  15. SellerId int `description:"销售id(SysUserId)"`
  16. SellerDepartmentId int `description:"营业部门id"`
  17. SellerDepartmentName string `description:"营业部门名称"`
  18. Company string `description:"所属公司"`
  19. ValidStartTime time.Time `description:"有效期开始时间"`
  20. ValidEndTime time.Time `description:"有效期结束时间"`
  21. Status int `description:"用户类型: 0表示禁用,1表示潜在客户,2表示正式客户"`
  22. ApplyStatus int `description:"申请状态: 0表示未申请,1表示已申请"`
  23. CreateTime time.Time `description:"系统中首次新增用户的时间"`
  24. ModifyTime time.Time `description:"系统中用户信息变更的时间"`
  25. RegisterTime time.Time `description:"用户首次登录小程序的时间"`
  26. ApplyTime time.Time `description:"用户提交申请的时间"`
  27. IsSubscribed bool `description:"是否关注公众号: 0表示没有关注,1表示关注"`
  28. IsRegistered bool `description:"是否注册: 0表示没有注册,1表示注册"`
  29. AccessToken string `description:"用户token"`
  30. Headimgurl string `description:"用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空"`
  31. }
  32. func (u *User) Insert() (insertId int64, err error) {
  33. o := orm.NewOrm()
  34. insertId, err = o.Insert(u)
  35. return
  36. }
  37. func (u *User) Update(cols []string) (err error) {
  38. o := orm.NewOrm()
  39. _, err = o.Update(u, cols...)
  40. return
  41. }
  42. type UserView struct {
  43. Headimgurl string `description:"用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空"`
  44. RealName string `description:"用户实际名称"`
  45. Phone string `description:"手机号码"`
  46. AreaCode string `description:"区号"`
  47. SellerDepartmentName string `description:"营业部名称"`
  48. Email string `description:"邮箱"`
  49. Componey string `description:"所属公司"`
  50. HasPermission string `description:"拥有权限"`
  51. ValidEndTime string `description:"服务截至时间"`
  52. IsRegistered bool `description:"是否注册:1:已注册,0:未注册"`
  53. Status int `description:"用户类型: 0表示禁用,1表示潜在客户,2表示正式客户"`
  54. ApplyStatus int `description:"申请状态: 0表示未申请,1表示已申请"`
  55. }
  56. func GetUserByToken(token string) (item *User, err error) {
  57. sql := `SELECT * FROM user WHERE access_token=? `
  58. o := orm.NewOrm()
  59. err = o.Raw(sql, token).QueryRow(&item)
  60. return
  61. }
  62. // 根据openid获取用户关系
  63. func GetUserByOpenId(openId string) (item *User, err error) {
  64. sql := `SELECT * FROM user WHERE open_id=? `
  65. o := orm.NewOrm()
  66. err = o.Raw(sql, openId).QueryRow(&item)
  67. return
  68. }
  69. // 根据unionid获取用户关系
  70. func GetUserByUnionId(unionId string) (item *User, err error) {
  71. sql := `SELECT * FROM user WHERE union_id=? `
  72. o := orm.NewOrm()
  73. err = o.Raw(sql, unionId).QueryRow(&item)
  74. return
  75. }
  76. // 变更联系人是否已注册状态
  77. func ModifyUserRegisterStatus(userId int, status bool, registerTime, modifyTime time.Time) (err error) {
  78. o := orm.NewOrm()
  79. sql := `UPDATE user SET is_registered=?, register_time=?, modify_time=? WHERE user_id = ? `
  80. _, err = o.Raw(sql, status, registerTime, modifyTime, userId).Exec()
  81. return
  82. }
  83. func GetUserById(userId int) (item *User, err error) {
  84. o := orm.NewOrm()
  85. sql := `SELECT * FROM user WHERE user_id=? `
  86. err = o.Raw(sql, userId).QueryRow(&item)
  87. return
  88. }
  89. func GetUserList(condition string, pars []interface{}) (items []*User, err error) {
  90. o := orm.NewOrm()
  91. sql := `SELECT * FROM user WHERE 1=1 `
  92. if condition != "" {
  93. sql += condition
  94. }
  95. _, err = o.Raw(sql, pars...).QueryRows(&items)
  96. return
  97. }
  98. func GetUserByPhone(phone string) (item *User, err error) {
  99. o := orm.NewOrm()
  100. sql := `SELECT * FROM user WHERE phone=? `
  101. err = o.Raw(sql, phone).QueryRow(&item)
  102. return
  103. }
  104. func GetUserByEmail(email string) (item *User, err error) {
  105. sql := `SELECT * FROM user WHERE email=? `
  106. err = orm.NewOrm().Raw(sql, email).QueryRow(&item)
  107. return
  108. }