user.go 5.8 KB

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