user.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. Token string `description:"微信扫码登录之后返回的Token"`
  43. }
  44. type WxBindMobileReq struct {
  45. Mobile string `description:"手机号"`
  46. VCode string `description:"验证码"`
  47. //Token string `description:"Token"`
  48. }
  49. func PcBindMobile(unionId, mobile string, userId, loginType int) (wxUserId int, err error) {
  50. //loginType 登录方式:1:手机,2:邮箱
  51. sql := ``
  52. if loginType == 1 {
  53. sql = `SELECT * FROM wx_user WHERE mobile = ? `
  54. } else {
  55. sql = "SELECT * FROM wx_user WHERE email = ? "
  56. }
  57. user := new(WxUser)
  58. o := orm.NewOrm()
  59. err = o.Raw(sql, mobile).QueryRow(&user)
  60. if err != nil && err.Error() != utils.ErrNoRow() {
  61. return
  62. }
  63. if user == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
  64. msql := ``
  65. if loginType == 1 {
  66. msql = "UPDATE wx_user SET mobile = ?,bind_account = ? WHERE union_id = ? "
  67. } else {
  68. msql = "UPDATE wx_user SET email = ?,bind_account = ? WHERE union_id = ? "
  69. }
  70. _, err = o.Raw(msql, mobile, mobile, unionId).Exec()
  71. wxUserId = userId
  72. } else {
  73. if user.UnionId == "" {
  74. sql = `SELECT * FROM wx_user WHERE union_id = ? `
  75. userInfo := new(WxUser)
  76. o := orm.NewOrm()
  77. err = o.Raw(sql, unionId).QueryRow(&userInfo)
  78. if err != nil {
  79. return
  80. }
  81. var maxRegisterTime time.Time
  82. if user.RegisterTime.Before(userInfo.RegisterTime) {
  83. maxRegisterTime = user.RegisterTime
  84. } else {
  85. maxRegisterTime = userInfo.RegisterTime
  86. }
  87. wxUserId = user.UserId
  88. dsql := ` DELETE FROM wx_user WHERE union_id = ? `
  89. _, err = o.Raw(dsql, unionId).Exec()
  90. if err != nil {
  91. return wxUserId, err
  92. }
  93. msql := ` UPDATE wx_user SET union_id=?,register_time=?,province=?,city=?,country=?,headimgurl=?,unionid=?,sex=? WHERE user_id = ? `
  94. _, err = o.Raw(msql, unionId, maxRegisterTime, userInfo.Province, userInfo.City, userInfo.Country, userInfo.Headimgurl, unionId, userInfo.Sex, user.UserId).Exec()
  95. wxUserId = user.UserId
  96. } else {
  97. sql = `SELECT * FROM wx_user WHERE user_id = ? `
  98. userInfo := new(WxUser)
  99. o := orm.NewOrm()
  100. err = o.Raw(sql, userId).QueryRow(&userInfo)
  101. if err != nil && err.Error() != utils.ErrNoRow() {
  102. return
  103. }
  104. if user.UserId != userId {
  105. dsql := ` DELETE FROM wx_user WHERE user_id = ? `
  106. _, err = o.Raw(dsql, userId).Exec()
  107. if err != nil {
  108. return user.UserId, err
  109. }
  110. }
  111. msql := ` UPDATE wx_user SET union_id=?,province=?,city=?,country=?,headimgurl=?,unionid=?,sex=? WHERE user_id = ? `
  112. _, err = o.Raw(msql, unionId, userInfo.Province, userInfo.City, userInfo.Country, userInfo.Headimgurl, unionId, userInfo.Sex, user.UserId).Exec()
  113. wxUserId = userId
  114. }
  115. }
  116. return
  117. }
  118. type LoginResp struct {
  119. UserId int `description:"用户id"`
  120. Authorization string `description:"Token"`
  121. Headimgurl string `description:"用户头像"`
  122. Mobile string `description:"手机号"`
  123. Email string `description:"邮箱"`
  124. CompanyName string `description:"客户名称"`
  125. }
  126. type UserDetailResp struct {
  127. UserId int `description:"用户id"`
  128. Headimgurl string `description:"用户头像"`
  129. Mobile string `description:"手机号"`
  130. Email string `description:"邮箱"`
  131. CompanyName string `description:"客户名称"`
  132. }
  133. type CheckStatusResp struct {
  134. IsBind bool `description:"true:需要绑定手机号或邮箱,false:不需要绑定手机号或邮箱"`
  135. IsAuth bool `description:"true:需要授权,false:不需要授权"`
  136. PermissionName string `description:"拥有权限分类,多个用英文逗号分隔"`
  137. }
  138. type ApplyTryReq struct {
  139. BusinessCardUrl string `description:"名片地址"`
  140. RealName string `description:"姓名"`
  141. CompanyName string `description:"公司名称"`
  142. ApplyMethod int `description:"1:已付费客户申请试用,2:非客户申请试用,3:非客户申请试用(ficc下,不需要进行数据校验)"`
  143. TryType string `description:"提交类型,Article:文章、Activity:活动"`
  144. DetailId int `description:"详情ID"`
  145. }
  146. type CountryCode struct {
  147. IsNeedAddCountryCode bool `description:"是否需要填写区号:需要填写,false:不需要填写"`
  148. }
  149. type OutboundMobile struct {
  150. IsNeedAddOutboundMobile bool `description:"是否需要填写外呼手机号:需要填写,false:不需要填写"`
  151. }
  152. type CountryCodeItem struct {
  153. CountryCode string `description:"区号"`
  154. }
  155. //修改外呼手机号
  156. type OutboundMobileItem struct {
  157. OutboundMobile string `description:"外呼手机号"`
  158. OutboundCountryCode string `description:"外呼手机号区号"`
  159. ActivityId int `description:"活动ID"`
  160. }
  161. type UserWhiteList struct {
  162. Mobile string `description:"手机号码"`
  163. RealName string `description:"用户实际名称"`
  164. CompanyName string `description:"公司名称"`
  165. Permission string `description:"拥有权限分类,多个用英文逗号分隔"`
  166. CountryCode string `description:"区号"`
  167. SellerName string `description:"销售姓名"`
  168. CreatedTime time.Time
  169. Status string `description:"客户状态'试用','永续','冻结','流失','正式','潜在'"`
  170. }
  171. type UserWhiteListRep struct {
  172. List []*UserWhiteList
  173. }
  174. type UserDetailByUserLogin struct {
  175. Headimgurl string `description:"头像"`
  176. Mobile string `description:"手机号码"`
  177. RealName string `description:"用户实际名称"`
  178. CompanyName string `description:"公司名称"`
  179. Permission string `description:"拥有权限分类,多个用英文逗号分隔"`
  180. HasPermission int `description:"1:有该行业权限,正常展示,2:无权限,非潜在客户,3:潜在客户"`
  181. Token string `description:"Token"`
  182. }
  183. func GetCompanyPermission(companyId int) (permission string, err error) {
  184. sql := ` SELECT GROUP_CONCAT(DISTINCT b.chart_permission_name ORDER BY b.sort ASC SEPARATOR ',') AS permission
  185. FROM company_report_permission AS a
  186. INNER JOIN chart_permission AS b ON a.chart_permission_id=b.chart_permission_id
  187. INNER JOIN company_product AS c ON a.company_id=c.company_id AND a.product_id=c.product_id
  188. WHERE a.company_id=?
  189. AND c.is_suspend=0
  190. AND b.cygx_auth=1
  191. AND c.status IN('正式','试用','永续')
  192. AND a.status IN('正式','试用','永续') `
  193. o := orm.NewOrm()
  194. err = o.Raw(sql, companyId).QueryRow(&permission)
  195. return
  196. }
  197. func GetCompanyPermissionId(companyId int) (permissionId string, err error) {
  198. sql := ` SELECT GROUP_CONCAT(DISTINCT b.chart_permission_id ORDER BY b.sort ASC SEPARATOR ',') AS permissionId
  199. FROM company_report_permission AS a
  200. INNER JOIN chart_permission AS b ON a.chart_permission_id=b.chart_permission_id
  201. INNER JOIN company_product AS c ON a.company_id=c.company_id AND a.product_id=c.product_id
  202. WHERE a.company_id=?
  203. AND c.is_suspend=0
  204. AND b.cygx_auth=1
  205. AND c.status IN('正式','试用','永续')
  206. AND a.status IN('正式','试用','永续') `
  207. o := orm.NewOrm()
  208. err = o.Raw(sql, companyId).QueryRow(&permissionId)
  209. return
  210. }
  211. //用户绑定手机号时同时绑定外呼手机号
  212. func BindUserOutboundMobile(mobile, countryCode string, userId int) (err error) {
  213. o := orm.NewOrm()
  214. sql := `UPDATE wx_user SET outbound_mobile=? ,outbound_country_code = ?, country_code= ? WHERE user_id=? `
  215. _, err = o.Raw(sql, mobile, countryCode, countryCode, userId).Exec()
  216. return
  217. }
  218. type UserPermissionAuthInfo struct {
  219. HasPermission int `description:"是否有权限:1-有权限; 2-无权限; 3-潜在客户未提交申请; 4-潜在客户已提交申请 5-仅有FICC权限"`
  220. SellerMobile string `description:"销售手机号"`
  221. SellerName string `description:"销售名称"`
  222. OperationMode string `description:"操作方式:Apply-立即申请; Call-拨号"`
  223. PopupMsg string `description:"权限弹窗信息"`
  224. }