user.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. SellerDepartmentPhone []string `description:"营业部电话"`
  49. Email string `description:"邮箱"`
  50. Componey string `description:"所属公司"`
  51. HasPermission string `description:"拥有权限"`
  52. ValidEndTime string `description:"服务截至时间"`
  53. IsRegistered bool `description:"是否注册:1:已注册,0:未注册"`
  54. Status int `description:"用户类型: 0表示禁用,1表示潜在客户,2表示正式客户"`
  55. ApplyStatus int `description:"申请状态: 0表示未申请,1表示已申请"`
  56. }
  57. func GetUserByToken(token string) (item *User, err error) {
  58. sql := `SELECT * FROM user WHERE access_token=? `
  59. o := orm.NewOrm()
  60. err = o.Raw(sql, token).QueryRow(&item)
  61. return
  62. }
  63. // 根据openid获取用户关系
  64. func GetUserByOpenId(openId string) (item *User, err error) {
  65. sql := `SELECT * FROM user WHERE open_id=? `
  66. o := orm.NewOrm()
  67. err = o.Raw(sql, openId).QueryRow(&item)
  68. return
  69. }
  70. // 根据unionid获取用户关系
  71. func GetUserByUnionId(unionId string) (item *User, err error) {
  72. sql := `SELECT * FROM user WHERE union_id=? `
  73. o := orm.NewOrm()
  74. err = o.Raw(sql, unionId).QueryRow(&item)
  75. return
  76. }
  77. // 变更联系人是否已注册状态
  78. func ModifyUserRegisterStatus(userId int, status bool, registerTime, modifyTime time.Time) (err error) {
  79. o := orm.NewOrm()
  80. sql := `UPDATE user SET is_registered=?, register_time=?, modify_time=? WHERE user_id = ? `
  81. _, err = o.Raw(sql, status, registerTime, modifyTime, userId).Exec()
  82. return
  83. }
  84. func GetUserById(userId int) (item *User, err error) {
  85. o := orm.NewOrm()
  86. sql := `SELECT * FROM user WHERE user_id=? `
  87. err = o.Raw(sql, userId).QueryRow(&item)
  88. return
  89. }
  90. func GetUserList(condition string, pars []interface{}) (items []*User, err error) {
  91. o := orm.NewOrm()
  92. sql := `SELECT * FROM user WHERE 1=1 `
  93. if condition != "" {
  94. sql += condition
  95. }
  96. _, err = o.Raw(sql, pars...).QueryRows(&items)
  97. return
  98. }
  99. func GetUserByPhone(phone string) (item *User, err error) {
  100. o := orm.NewOrm()
  101. sql := `SELECT * FROM user WHERE phone=? `
  102. err = o.Raw(sql, phone).QueryRow(&item)
  103. return
  104. }
  105. func GetUserByEmail(email string) (item *User, err error) {
  106. sql := `SELECT * FROM user WHERE email=? `
  107. err = orm.NewOrm().Raw(sql, email).QueryRow(&item)
  108. return
  109. }