user.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package models
  2. import (
  3. "hongze/hongze_cygx/utils"
  4. "rdluck_tools/orm"
  5. "rdluck_tools/paging"
  6. "time"
  7. )
  8. type UserDetail struct {
  9. Headimgurl string `description:"用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空"`
  10. Mobile string `description:"手机号码"`
  11. Email string `description:"邮箱"`
  12. NickName string `description:"用户昵称"`
  13. RealName string `description:"用户实际名称"`
  14. CompanyName string `description:"公司名称"`
  15. PermissionName string `description:"拥有权限分类,多个用英文逗号分隔"`
  16. HasPermission bool `description:"true:有权限,false:无权限"`
  17. }
  18. func GetUserDetailByUserId(userId int) (item *UserDetail, err error) {
  19. o := orm.NewOrm()
  20. sql := `SELECT * FROM wx_user WHERE user_id = ? `
  21. err = o.Raw(sql, userId).QueryRow(&item)
  22. return
  23. }
  24. type UserPermission struct {
  25. CompanyName string `description:"公司名称"`
  26. ChartPermissionName string `description:"权限"`
  27. }
  28. type LoginReq struct {
  29. LoginType int `description:"登录方式:1:手机,2:邮箱"`
  30. Mobile string `description:"手机号"`
  31. Email string `description:"邮箱"`
  32. }
  33. func PcBindMobile(unionId, mobile string, userId, loginType int) (wxUserId int, err error) {
  34. //loginType 登录方式:1:手机,2:邮箱
  35. utils.FileLog.Info("绑定参数:%s %s %d %d", unionId, mobile, userId, loginType)
  36. sql := ``
  37. if loginType == 1 {
  38. sql = `SELECT * FROM wx_user WHERE mobile = ? `
  39. } else {
  40. sql = "SELECT * FROM wx_user WHERE email = ? "
  41. }
  42. user := new(WxUser)
  43. o := orm.NewOrm()
  44. err = o.Raw(sql, mobile).QueryRow(&user)
  45. if err != nil && err.Error() != utils.ErrNoRow() {
  46. return
  47. }
  48. if user == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
  49. utils.FileLog.Info("用户不存在,根据union_id绑定")
  50. msql := ``
  51. if loginType == 1 {
  52. msql = "UPDATE wx_user SET mobile = ?,bind_account = ? WHERE union_id = ? "
  53. } else {
  54. msql = "UPDATE wx_user SET email = ?,bind_account = ? WHERE union_id = ? "
  55. }
  56. _, err = o.Raw(msql, mobile, mobile, unionId).Exec()
  57. wxUserId = userId
  58. } else {
  59. utils.FileLog.Info("用户存在,user.UnionId:%s", user.UnionId)
  60. if user.UnionId == "" {
  61. sql = `SELECT * FROM wx_user WHERE union_id = ? `
  62. userInfo := new(WxUser)
  63. o := orm.NewOrm()
  64. err = o.Raw(sql, unionId).QueryRow(&userInfo)
  65. if err != nil {
  66. return
  67. }
  68. utils.FileLog.Info("user.RegisterTime %s", user.RegisterTime.Format(utils.FormatDateTime))
  69. utils.FileLog.Info("userInfo.RegisterTime %s", userInfo.RegisterTime.Format(utils.FormatDateTime))
  70. var maxRegisterTime time.Time
  71. if user.RegisterTime.Before(userInfo.RegisterTime) {
  72. maxRegisterTime = user.RegisterTime
  73. utils.FileLog.Info("after")
  74. } else {
  75. maxRegisterTime = userInfo.RegisterTime
  76. utils.FileLog.Info("not after")
  77. }
  78. utils.FileLog.Info("maxRegisterTime %s", maxRegisterTime.Format(utils.FormatDateTime))
  79. wxUserId = user.UserId
  80. dsql := ` DELETE FROM wx_user WHERE union_id = ? `
  81. _, err = o.Raw(dsql, unionId).Exec()
  82. if err != nil {
  83. return wxUserId, err
  84. }
  85. msql := ` UPDATE wx_user SET union_id=?,register_time=?,province=?,city=?,country=?,headimgurl=?,unionid=?,sex=? WHERE user_id = ? `
  86. _, err = o.Raw(msql, unionId, maxRegisterTime, userInfo.Province, userInfo.City, userInfo.Country, userInfo.Headimgurl, unionId, userInfo.Sex, user.UserId).Exec()
  87. wxUserId = user.UserId
  88. } else {
  89. sql = `SELECT * FROM wx_user WHERE user_id = ? `
  90. userInfo := new(WxUser)
  91. o := orm.NewOrm()
  92. err = o.Raw(sql, userId).QueryRow(&userInfo)
  93. if err != nil && err.Error() != utils.ErrNoRow() {
  94. return
  95. }
  96. dsql := ` DELETE FROM wx_user WHERE user_id = ? `
  97. _, err = o.Raw(dsql, userId).Exec()
  98. if err != nil {
  99. return user.UserId, err
  100. }
  101. if user.Mobile == "" && loginType == 1 {
  102. msql := ` UPDATE wx_user SET mobile = ?,bind_account = ? WHERE user_id = ?`
  103. _, err = o.Raw(msql, mobile, mobile, user.UserId).Exec()
  104. wxUserId = user.UserId
  105. }
  106. if user.Email == "" && loginType == 2 {
  107. msql := ` UPDATE wx_user SET email = ?,bind_account = ? WHERE user_id = ?`
  108. _, err = o.Raw(msql, mobile, mobile, user.UserId).Exec()
  109. wxUserId = user.UserId
  110. }
  111. utils.FileLog.Info("用户存在,bind:%s,%d,%s", unionId, wxUserId)
  112. wxUserId = userId
  113. }
  114. }
  115. return
  116. }
  117. type LoginResp struct {
  118. UserId int `description:"用户id"`
  119. Authorization string `description:"Token"`
  120. Headimgurl string `description:"用户头像"`
  121. Mobile string `description:"手机号"`
  122. Email string `description:"邮箱"`
  123. CompanyName string `description:"客户名称"`
  124. Status string `description:"状态"`
  125. EndDate string `description:"到期日期"`
  126. ProductName string `description:"客户类型名称"`
  127. }
  128. type CheckStatusResp struct {
  129. IsBind bool `description:"true:需要绑定手机号或邮箱,false:不需要绑定手机号或邮箱"`
  130. IsAuth bool `description:"true:需要授权,false:不需要授权"`
  131. PermissionName string `description:"拥有权限分类,多个用英文逗号分隔"`
  132. }
  133. func GetArticleUserCollectCount(userId int) (count int, err error) {
  134. sql := `SELECT COUNT(1) AS count FROM cygx_article_collect AS a WHERE a.user_id=? `
  135. err = orm.NewOrm().Raw(sql,userId).QueryRow(&count)
  136. return
  137. }
  138. func GetArticleUserCollectList(startSize, pageSize, userId int) (items []*ArticleCollectList, err error) {
  139. sql := `SELECT a.* FROM cygx_article_collect AS a
  140. WHERE a.user_id=?
  141. ORDER BY a.create_time DESC LIMIT ?,? `
  142. _, err = orm.NewOrm().Raw(sql, userId, startSize, pageSize).QueryRows(&items)
  143. return
  144. }
  145. type ArticleCollectListResp struct {
  146. List []*ArticleCollectList
  147. Paging *paging.PagingItem
  148. }
  149. func GetArticleUserInterviewApplyCount(userId int) (count int, err error) {
  150. sql := `SELECT COUNT(1) AS count FROM cygx_interview_apply AS a WHERE a.user_id=? `
  151. err = orm.NewOrm().Raw(sql,userId).QueryRow(&count)
  152. return
  153. }
  154. func GetArticleUserInterviewApplyList(startSize, pageSize, userId int) (items []*ArticleInterviewApplyList, err error) {
  155. sql := `SELECT a.* FROM cygx_interview_apply AS a
  156. WHERE a.user_id=?
  157. ORDER BY a.sort ASC LIMIT ?,? `
  158. _, err = orm.NewOrm().Raw(sql, userId, startSize, pageSize).QueryRows(&items)
  159. return
  160. }
  161. type ArticleInterviewApplyListResp struct {
  162. List []*ArticleInterviewApplyList
  163. Paging *paging.PagingItem
  164. }