user.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package models
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "hongze/hongze_clpt/utils"
  5. "time"
  6. )
  7. type UserDetail struct {
  8. Headimgurl string `description:"用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空"`
  9. Mobile string `description:"手机号码"`
  10. Email string `description:"邮箱"`
  11. NickName string `description:"用户昵称"`
  12. RealName string `description:"用户实际名称"`
  13. CompanyName string `description:"公司名称"`
  14. PermissionName string `description:"拥有权限分类,多个用英文逗号分隔"`
  15. HasPermission int `description:"1:无该行业权限,不存在权益客户下,2:潜在客户,未提交过申请,3:潜在客户,已提交过申请"`
  16. SellerMobile string `description:"销售手机号"`
  17. SellerName string `description:"销售名称"`
  18. Note string `json:"-" description:"申请提交时,公司名称"`
  19. CountryCode string `description:"区号"`
  20. OutboundMobile string `description:"外呼手机号"`
  21. OutboundCountryCode string `description:"外呼手机号区号"`
  22. }
  23. func GetUserDetailByUserId(userId int) (item *UserDetail, err error) {
  24. o := orm.NewOrm()
  25. sql := `SELECT * FROM wx_user WHERE user_id = ? `
  26. err = o.Raw(sql, userId).QueryRow(&item)
  27. return
  28. }
  29. func GetUserDetailByMobile(mobile string) (item *UserDetail, err error) {
  30. o := orm.NewOrm()
  31. sql := `SELECT * FROM wx_user WHERE mobile = ? `
  32. err = o.Raw(sql, mobile).QueryRow(&item)
  33. return
  34. }
  35. type UserPermission struct {
  36. CompanyName string `description:"公司名称"`
  37. ChartPermissionName string `description:"权限"`
  38. }
  39. type LoginReq struct {
  40. Mobile string `description:"手机号"`
  41. VCode string `description:"验证码"`
  42. }
  43. func PcBindMobile(unionId, mobile string, userId, loginType int) (wxUserId int, err error) {
  44. //loginType 登录方式:1:手机,2:邮箱
  45. sql := ``
  46. if loginType == 1 {
  47. sql = `SELECT * FROM wx_user WHERE mobile = ? `
  48. } else {
  49. sql = "SELECT * FROM wx_user WHERE email = ? "
  50. }
  51. user := new(WxUser)
  52. o := orm.NewOrm()
  53. err = o.Raw(sql, mobile).QueryRow(&user)
  54. if err != nil && err.Error() != utils.ErrNoRow() {
  55. return
  56. }
  57. if user == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
  58. msql := ``
  59. if loginType == 1 {
  60. msql = "UPDATE wx_user SET mobile = ?,bind_account = ? WHERE union_id = ? "
  61. } else {
  62. msql = "UPDATE wx_user SET email = ?,bind_account = ? WHERE union_id = ? "
  63. }
  64. _, err = o.Raw(msql, mobile, mobile, unionId).Exec()
  65. wxUserId = userId
  66. } else {
  67. if user.UnionId == "" {
  68. sql = `SELECT * FROM wx_user WHERE union_id = ? `
  69. userInfo := new(WxUser)
  70. o := orm.NewOrm()
  71. err = o.Raw(sql, unionId).QueryRow(&userInfo)
  72. if err != nil {
  73. return
  74. }
  75. var maxRegisterTime time.Time
  76. if user.RegisterTime.Before(userInfo.RegisterTime) {
  77. maxRegisterTime = user.RegisterTime
  78. } else {
  79. maxRegisterTime = userInfo.RegisterTime
  80. }
  81. wxUserId = user.UserId
  82. dsql := ` DELETE FROM wx_user WHERE union_id = ? `
  83. _, err = o.Raw(dsql, unionId).Exec()
  84. if err != nil {
  85. return wxUserId, err
  86. }
  87. msql := ` UPDATE wx_user SET union_id=?,register_time=?,province=?,city=?,country=?,headimgurl=?,unionid=?,sex=? WHERE user_id = ? `
  88. _, err = o.Raw(msql, unionId, maxRegisterTime, userInfo.Province, userInfo.City, userInfo.Country, userInfo.Headimgurl, unionId, userInfo.Sex, user.UserId).Exec()
  89. wxUserId = user.UserId
  90. } else {
  91. sql = `SELECT * FROM wx_user WHERE user_id = ? `
  92. userInfo := new(WxUser)
  93. o := orm.NewOrm()
  94. err = o.Raw(sql, userId).QueryRow(&userInfo)
  95. if err != nil && err.Error() != utils.ErrNoRow() {
  96. return
  97. }
  98. if user.UserId != userId {
  99. dsql := ` DELETE FROM wx_user WHERE user_id = ? `
  100. _, err = o.Raw(dsql, userId).Exec()
  101. if err != nil {
  102. return user.UserId, err
  103. }
  104. }
  105. msql := ` UPDATE wx_user SET union_id=?,province=?,city=?,country=?,headimgurl=?,unionid=?,sex=? WHERE user_id = ? `
  106. _, err = o.Raw(msql, unionId, userInfo.Province, userInfo.City, userInfo.Country, userInfo.Headimgurl, unionId, userInfo.Sex, user.UserId).Exec()
  107. wxUserId = userId
  108. }
  109. }
  110. return
  111. }
  112. type LoginResp struct {
  113. UserId int `description:"用户id"`
  114. Authorization string `description:"Token"`
  115. Headimgurl string `description:"用户头像"`
  116. Mobile string `description:"手机号"`
  117. Email string `description:"邮箱"`
  118. CompanyName string `description:"客户名称"`
  119. }
  120. type UserDetailResp struct {
  121. UserId int `description:"用户id"`
  122. Headimgurl string `description:"用户头像"`
  123. Mobile string `description:"手机号"`
  124. Email string `description:"邮箱"`
  125. CompanyName string `description:"客户名称"`
  126. }
  127. type CheckStatusResp struct {
  128. IsBind bool `description:"true:需要绑定手机号或邮箱,false:不需要绑定手机号或邮箱"`
  129. IsAuth bool `description:"true:需要授权,false:不需要授权"`
  130. PermissionName string `description:"拥有权限分类,多个用英文逗号分隔"`
  131. }
  132. type ApplyTryReq struct {
  133. BusinessCardUrl string `description:"名片地址"`
  134. RealName string `description:"姓名"`
  135. CompanyName string `description:"公司名称"`
  136. ApplyMethod int `description:"1:已付费客户申请试用,2:非客户申请试用,3:非客户申请试用(ficc下,不需要进行数据校验)"`
  137. TryType string `description:"提交类型,Article:文章、Activity:活动"`
  138. DetailId int `description:"详情ID"`
  139. }
  140. type CountryCode struct {
  141. IsNeedAddCountryCode bool `description:"是否需要填写区号:需要填写,false:不需要填写"`
  142. }
  143. type OutboundMobile struct {
  144. IsNeedAddOutboundMobile bool `description:"是否需要填写外呼手机号:需要填写,false:不需要填写"`
  145. }
  146. type CountryCodeItem struct {
  147. CountryCode string `description:"区号"`
  148. }
  149. //修改外呼手机号
  150. type OutboundMobileItem struct {
  151. OutboundMobile string `description:"外呼手机号"`
  152. OutboundCountryCode string `description:"外呼手机号区号"`
  153. ActivityId int `description:"活动ID"`
  154. }
  155. type UserWhiteList struct {
  156. Mobile string `description:"手机号码"`
  157. RealName string `description:"用户实际名称"`
  158. CompanyName string `description:"公司名称"`
  159. Permission string `description:"拥有权限分类,多个用英文逗号分隔"`
  160. CountryCode string `description:"区号"`
  161. SellerName string `description:"销售姓名"`
  162. CreatedTime time.Time
  163. Status string `description:"客户状态'试用','永续','冻结','流失','正式','潜在'"`
  164. }
  165. type UserWhiteListRep struct {
  166. List []*UserWhiteList
  167. }
  168. type UserDetailByUserLogin struct {
  169. Headimgurl string `description:"头像"`
  170. Mobile string `description:"手机号码"`
  171. RealName string `description:"用户实际名称"`
  172. CompanyName string `description:"公司名称"`
  173. Permission string `description:"拥有权限分类,多个用英文逗号分隔"`
  174. HasPermission int `description:"1:有该行业权限,正常展示,2:无权限,非潜在客户,3:未在小程序授权用户信息 等"`
  175. Token string `description:"Token"`
  176. }
  177. func GetCompanyPermission(companyId int) (permission string, err error) {
  178. sql := ` SELECT GROUP_CONCAT(DISTINCT b.chart_permission_name ORDER BY b.sort ASC SEPARATOR ',') AS permission
  179. FROM company_report_permission AS a
  180. INNER JOIN chart_permission AS b ON a.chart_permission_id=b.chart_permission_id
  181. INNER JOIN company_product AS c ON a.company_id=c.company_id AND a.product_id=c.product_id
  182. WHERE a.company_id=?
  183. AND c.is_suspend=0
  184. AND b.cygx_auth=1
  185. AND c.status IN('正式','试用','永续')
  186. AND a.status IN('正式','试用','永续') `
  187. o := orm.NewOrm()
  188. err = o.Raw(sql, companyId).QueryRow(&permission)
  189. return
  190. }
  191. func GetCompanyPermissionId(companyId int) (permissionId string, err error) {
  192. sql := ` SELECT GROUP_CONCAT(DISTINCT b.chart_permission_id ORDER BY b.sort ASC SEPARATOR ',') AS permissionId
  193. FROM company_report_permission AS a
  194. INNER JOIN chart_permission AS b ON a.chart_permission_id=b.chart_permission_id
  195. INNER JOIN company_product AS c ON a.company_id=c.company_id AND a.product_id=c.product_id
  196. WHERE a.company_id=?
  197. AND c.is_suspend=0
  198. AND b.cygx_auth=1
  199. AND c.status IN('正式','试用','永续')
  200. AND a.status IN('正式','试用','永续') `
  201. o := orm.NewOrm()
  202. err = o.Raw(sql, companyId).QueryRow(&permissionId)
  203. return
  204. }