user.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. if user.UserId != userId {
  97. dsql := ` DELETE FROM wx_user WHERE user_id = ? `
  98. _, err = o.Raw(dsql, userId).Exec()
  99. if err != nil {
  100. return user.UserId, err
  101. }
  102. }
  103. msql := ` UPDATE wx_user SET union_id=?,province=?,city=?,country=?,headimgurl=?,unionid=?,sex=? WHERE user_id = ? `
  104. _, err = o.Raw(msql, unionId, userInfo.Province, userInfo.City, userInfo.Country, userInfo.Headimgurl, unionId, userInfo.Sex, user.UserId).Exec()
  105. utils.FileLog.Info("用户存在,bind:%s,%d,%s", unionId, wxUserId)
  106. wxUserId = userId
  107. }
  108. }
  109. return
  110. }
  111. type LoginResp struct {
  112. UserId int `description:"用户id"`
  113. Authorization string `description:"Token"`
  114. Headimgurl string `description:"用户头像"`
  115. Mobile string `description:"手机号"`
  116. Email string `description:"邮箱"`
  117. CompanyName string `description:"客户名称"`
  118. Status string `description:"状态"`
  119. EndDate string `description:"到期日期"`
  120. ProductName string `description:"客户类型名称"`
  121. }
  122. type CheckStatusResp struct {
  123. IsBind bool `description:"true:需要绑定手机号或邮箱,false:不需要绑定手机号或邮箱"`
  124. IsAuth bool `description:"true:需要授权,false:不需要授权"`
  125. PermissionName string `description:"拥有权限分类,多个用英文逗号分隔"`
  126. }
  127. func GetArticleUserCollectCount(userId int) (count int, err error) {
  128. sql := `SELECT COUNT(1) AS count FROM cygx_article_collect AS a WHERE a.user_id=? `
  129. err = orm.NewOrm().Raw(sql, userId).QueryRow(&count)
  130. return
  131. }
  132. func GetArticleUserCollectList(startSize, pageSize, userId int) (items []*ArticleCollectList, err error) {
  133. sql := `SELECT a.* FROM cygx_article_collect AS a
  134. WHERE a.user_id=?
  135. ORDER BY a.create_time DESC LIMIT ?,? `
  136. _, err = orm.NewOrm().Raw(sql, userId, startSize, pageSize).QueryRows(&items)
  137. return
  138. }
  139. type ArticleCollectListResp struct {
  140. List []*ArticleCollectList
  141. Paging *paging.PagingItem
  142. }
  143. func GetArticleUserInterviewApplyCount(userId int) (count int, err error) {
  144. sql := `SELECT COUNT(1) AS count FROM cygx_interview_apply AS a WHERE a.user_id=? `
  145. err = orm.NewOrm().Raw(sql, userId).QueryRow(&count)
  146. return
  147. }
  148. func GetArticleUserInterviewApplyList(startSize, pageSize, userId int) (items []*ArticleInterviewApplyList, err error) {
  149. sql := `SELECT a.* FROM cygx_interview_apply AS a
  150. WHERE a.user_id=?
  151. ORDER BY a.status ASC LIMIT ?,? `
  152. _, err = orm.NewOrm().Raw(sql, userId, startSize, pageSize).QueryRows(&items)
  153. return
  154. }
  155. type ArticleInterviewApplyListResp struct {
  156. List []*ArticleInterviewApplyList
  157. Paging *paging.PagingItem
  158. }
  159. func GetArticleUserBrowseHistoryCount(userId int, endDate string) (count int, err error) {
  160. sql := `SELECT COUNT(1) AS count FROM cygx_article_view_record AS a WHERE a.user_id=? AND a.create_time>=? `
  161. err = orm.NewOrm().Raw(sql, userId, endDate).QueryRow(&count)
  162. return
  163. }
  164. func GetArticleUserBrowseHistoryList(startSize, pageSize, userId int, endDate string) (items []*ArticleInterviewApplyList, err error) {
  165. sql := `SELECT a.* FROM cygx_article_view_record AS a
  166. WHERE a.user_id=? AND a.create_time>=?
  167. ORDER BY a.create_time DESC LIMIT ?,? `
  168. _, err = orm.NewOrm().Raw(sql, userId, endDate, startSize, pageSize).QueryRows(&items)
  169. return
  170. }
  171. type ArticleBrowseHistoryListResp struct {
  172. List []*ArticleInterviewApplyList
  173. Paging *paging.PagingItem
  174. }
  175. type ApplyTryReq struct {
  176. BusinessCardUrl string `description:"名片地址"`
  177. RealName string `description:"姓名"`
  178. CompanyName string `description:"公司名称"`
  179. ApplyMethod int `description:"1:已付费客户申请试用,2:非客户申请试用"`
  180. }