user.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 CheckStatusResp struct {
  121. IsBind bool `description:"true:需要绑定手机号或邮箱,false:不需要绑定手机号或邮箱"`
  122. IsAuth bool `description:"true:需要授权,false:不需要授权"`
  123. PermissionName string `description:"拥有权限分类,多个用英文逗号分隔"`
  124. }
  125. type ApplyTryReq struct {
  126. BusinessCardUrl string `description:"名片地址"`
  127. RealName string `description:"姓名"`
  128. CompanyName string `description:"公司名称"`
  129. ApplyMethod int `description:"1:已付费客户申请试用,2:非客户申请试用,3:非客户申请试用(ficc下,不需要进行数据校验)"`
  130. }
  131. type CountryCode struct {
  132. IsNeedAddCountryCode bool `description:"是否需要填写区号:需要填写,false:不需要填写"`
  133. }
  134. type OutboundMobile struct {
  135. IsNeedAddOutboundMobile bool `description:"是否需要填写外呼手机号:需要填写,false:不需要填写"`
  136. }
  137. type CountryCodeItem struct {
  138. CountryCode string `description:"区号"`
  139. }
  140. //修改外呼手机号
  141. type OutboundMobileItem struct {
  142. OutboundMobile string `description:"外呼手机号"`
  143. OutboundCountryCode string `description:"外呼手机号区号"`
  144. ActivityId int `description:"活动ID"`
  145. }
  146. type UserWhiteList struct {
  147. Mobile string `description:"手机号码"`
  148. RealName string `description:"用户实际名称"`
  149. CompanyName string `description:"公司名称"`
  150. Permission string `description:"拥有权限分类,多个用英文逗号分隔"`
  151. CountryCode string `description:"区号"`
  152. SellerName string `description:"销售姓名"`
  153. CreatedTime time.Time
  154. Status string `description:"客户状态'试用','永续','冻结','流失','正式','潜在'"`
  155. }
  156. type UserWhiteListRep struct {
  157. List []*UserWhiteList
  158. }
  159. type UserDetailByUserLogin struct {
  160. Headimgurl string `description:"头像"`
  161. Mobile string `description:"手机号码"`
  162. RealName string `description:"用户实际名称"`
  163. CompanyName string `description:"公司名称"`
  164. Permission string `description:"拥有权限分类,多个用英文逗号分隔"`
  165. HasPermission int `description:"1:有该行业权限,正常展示,2:无权限,非潜在客户,3:未在小程序授权用户信息 等"`
  166. Token string `description:"Token"`
  167. }
  168. func GetCompanyPermission(companyId int) (permission string, err error) {
  169. sql := ` SELECT GROUP_CONCAT(DISTINCT b.chart_permission_name ORDER BY b.sort ASC SEPARATOR ',') AS permission
  170. FROM company_report_permission AS a
  171. INNER JOIN chart_permission AS b ON a.chart_permission_id=b.chart_permission_id
  172. INNER JOIN company_product AS c ON a.company_id=c.company_id AND a.product_id=c.product_id
  173. WHERE a.company_id=?
  174. AND c.is_suspend=0
  175. AND b.cygx_auth=1
  176. AND c.status IN('正式','试用','永续')
  177. AND a.status IN('正式','试用','永续') `
  178. o := orm.NewOrm()
  179. err = o.Raw(sql, companyId).QueryRow(&permission)
  180. return
  181. }
  182. func GetCompanyPermissionId(companyId int) (permissionId string, err error) {
  183. sql := ` SELECT GROUP_CONCAT(DISTINCT b.chart_permission_id ORDER BY b.sort ASC SEPARATOR ',') AS permissionId
  184. FROM company_report_permission AS a
  185. INNER JOIN chart_permission AS b ON a.chart_permission_id=b.chart_permission_id
  186. INNER JOIN company_product AS c ON a.company_id=c.company_id AND a.product_id=c.product_id
  187. WHERE a.company_id=?
  188. AND c.is_suspend=0
  189. AND b.cygx_auth=1
  190. AND c.status IN('正式','试用','永续')
  191. AND a.status IN('正式','试用','永续') `
  192. o := orm.NewOrm()
  193. err = o.Raw(sql, companyId).QueryRow(&permissionId)
  194. return
  195. }