wx_user.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package models
  2. import (
  3. "hongze/hongze_api/utils"
  4. "rdluck_tools/orm"
  5. "time"
  6. )
  7. type WxUser struct {
  8. UserId int `orm:"column(user_id);pk"`
  9. OpenId string `description:"open_id"`
  10. UnionId string `description:"union_id"`
  11. Subscribe string `description:"是否关注"`
  12. CompanyId int `description:"客户id"`
  13. NickName string `description:"用户昵称"`
  14. RealName string `description:"用户实际名称"`
  15. UserCode string `description:"用户编码"`
  16. Mobile string `description:"手机号码"`
  17. BindAccount string `description:"绑定时的账号"`
  18. WxCode string `description:"微信号"`
  19. Profession string `description:"职业"`
  20. Email string `description:"邮箱"`
  21. Telephone string `description:"座机"`
  22. Sex int `description:"普通用户性别,1为男性,2为女性"`
  23. Province string `description:"普通用户个人资料填写的省份"`
  24. City string `description:"普通用户个人资料填写的城市"`
  25. Country string `description:"国家,如中国为CN"`
  26. SubscribeTime int `description:"关注时间"`
  27. Remark string `description:"备注"`
  28. Headimgurl string `description:"用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空"`
  29. Privilege string `description:"用户特权信息,json数组,如微信沃卡用户为(chinaunicom)"`
  30. Unionid string `description:"用户统一标识。针对一个微信开放平台帐号下的应用,同一用户的unionid是唯一的。"`
  31. FirstLogin int `description:"是否第一次登陆"`
  32. Enabled int `description:"是否可用"`
  33. CreatedTime time.Time `description:"创建时间"`
  34. LastUpdatedTime time.Time `description:"最新一次修改时间"`
  35. Seller string `description:"销售员"`
  36. Note string `description:"客户备份信息"`
  37. IsNote int `description:"是否备注过信息"`
  38. FromType string `description:"report' COMMENT 'report:研报,teleconference:电话会"`
  39. ApplyMethod int `description:"0:未申请,1:已付费客户申请试用,2:非客户申请试用"`
  40. }
  41. type WxUserItem struct {
  42. UserId int `description:"用户id"`
  43. OpenId string `description:"open_id"`
  44. UnionId string `description:"union_id"`
  45. CompanyId int `description:"客户id"`
  46. NickName string `description:"用户昵称"`
  47. RealName string `description:"用户实际名称"`
  48. Mobile string `description:"手机号码"`
  49. BindAccount string `description:"绑定时的账号"`
  50. Email string `description:"邮箱"`
  51. Headimgurl string `description:"用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空"`
  52. ApplyMethod int `description:"0:未申请,1:已付费客户申请试用,2:非客户申请试用"`
  53. FirstLogin int `description:"是否第一次登陆"`
  54. }
  55. func GetWxUserItemByUserId(userId int) (item *WxUserItem, err error) {
  56. sql := `SELECT * FROM wx_user WHERE user_id=? `
  57. err = orm.NewOrm().Raw(sql, userId).QueryRow(&item)
  58. return
  59. }
  60. func GetWxUserItemByOpenId(openId string) (item *WxUserItem, err error) {
  61. sql := `SELECT * FROM wx_user WHERE open_id=? `
  62. err = orm.NewOrm().Raw(sql, openId).QueryRow(&item)
  63. return
  64. }
  65. func GetWxUserItemByUnionid(unionid string) (item *WxUserItem, err error) {
  66. sql := `SELECT * FROM wx_user WHERE unionid=? `
  67. err = orm.NewOrm().Raw(sql, unionid).QueryRow(&item)
  68. return
  69. }
  70. type PermissionSearchKeyWord struct {
  71. KeyWord string
  72. }
  73. func GetPermissionSearchKeyWord(userId int) (items []*PermissionSearchKeyWord, err error) {
  74. sql := "SELECT a.key_word FROM chart_permission_search_key_word_mapping AS a INNER JOIN company_report_permission AS crp ON a.chart_permission_id=crp.chart_permission_id INNER JOIN wx_user AS wu ON wu.company_id=crp.company_id WHERE wu.user_id=? AND `from`='rddp' GROUP BY a.key_word "
  75. o := orm.NewOrm()
  76. _, err = o.Raw(sql, userId).QueryRows(&items)
  77. return
  78. }
  79. //判断客户权限总数
  80. func GetUserIsMaxPermission(companyId int) (count int, err error) {
  81. sql := ` SELECT COUNT(DISTINCT b.chart_permission_id) as count FROM company as a
  82. INNER JOIN company_report_permission as b on a.company_id=b.company_id
  83. WHERE b.company_id=? `
  84. o := orm.NewOrm()
  85. err = o.Raw(sql, companyId).QueryRow(&count)
  86. return
  87. }
  88. //添加用户信息
  89. func AddWxUser(item *WxUser) (err error) {
  90. o := orm.NewOrm()
  91. _, err = o.Insert(item)
  92. return
  93. }
  94. type WxLoginResp struct {
  95. Code int
  96. OpenId string
  97. Authorization string
  98. UserId int
  99. Expires time.Time
  100. FirstLogin int
  101. UserPermission int `description:"状态码"`
  102. }
  103. type UserDetail struct {
  104. FirstLogin int `description:"是否第一次登陆"`
  105. Headimgurl string `description:"用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空"`
  106. Mobile string `description:"手机号码"`
  107. Email string `description:"邮箱"`
  108. UserPermission int `description:"用户权限状态:0:付费用户,可正常查看报告,40001:获取用户信息失败,40002:非付费用户"`
  109. }
  110. func GetUserDetailByUserId(userId int) (item *UserDetail, err error) {
  111. o := orm.NewOrm()
  112. sql := `SELECT first_login,headimgurl,mobile,email FROM wx_user WHERE user_id = ? `
  113. err = o.Raw(sql, userId).QueryRow(&item)
  114. return
  115. }
  116. type CheckSmsCodeReq struct {
  117. Mobile string `description:"手机号"`
  118. SmsCode string `description:"验证码"`
  119. }
  120. type SmallLimitResp struct {
  121. IsMaxPermission int
  122. }
  123. type CheckEmailCodeReq struct {
  124. Email string `description:"邮箱"`
  125. SmsCode string `description:"验证码"`
  126. }
  127. type ApplyReq struct {
  128. ApplyMethod int `description:"申请方式:"`
  129. CompanyName string `description:"公司名称"`
  130. RealName string `description:"姓名"`
  131. }
  132. func Apply(userId, applyMethod int, mobile, email, companyName, realName, openId string) (err error) {
  133. sql := "INSERT INTO user_apply(user_id, mobile, email, company_name, real_name, apply_method) VALUES (?,?,?,?,?,?) "
  134. rddpOrm := orm.NewOrm()
  135. rddpOrm.Using("rddp")
  136. _, err = rddpOrm.Raw(sql, userId, mobile, email, companyName, realName, applyMethod).Exec()
  137. if err != nil {
  138. return
  139. }
  140. o := orm.NewOrm()
  141. if realName == "" {
  142. msql := " UPDATE wx_user SET apply_method = ?,note=? WHERE open_id = ? "
  143. _, err = o.Raw(msql, applyMethod, openId).Exec()
  144. } else {
  145. msql := " UPDATE wx_user SET apply_method = ?,real_name=?,note=? WHERE open_id = ? "
  146. _, err = o.Raw(msql, applyMethod, realName, companyName, openId).Exec()
  147. }
  148. return
  149. }
  150. type LoginReq struct {
  151. LoginType int `description:"登录方式:1:手机,2:邮箱"`
  152. Mobile string `description:"手机号"`
  153. Email string `description:"邮箱"`
  154. }
  155. type LoginResp struct {
  156. UserId int `description:"用户id"`
  157. UserPermission int `description:"手机号"`
  158. Authorization string
  159. }
  160. func BindMobile(openId, mobile string, userId, loginType int) (wxUserId int, err error) {
  161. //loginType 登录方式:1:手机,2:邮箱
  162. sql := ``
  163. if loginType == 1 {
  164. sql = `SELECT * FROM wx_user WHERE mobile = ? `
  165. } else {
  166. sql = "SELECT * FROM wx_user WHERE email = ? "
  167. }
  168. user := new(WxUser)
  169. o := orm.NewOrm()
  170. err = o.Raw(sql, mobile).QueryRow(&user)
  171. if err != nil && err.Error() != utils.ErrNoRow() {
  172. return
  173. }
  174. if user == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
  175. msql := ``
  176. if loginType == 1 {
  177. msql = "UPDATE wx_user SET mobile = ?,bind_account = ? where open_id = ? "
  178. } else {
  179. msql = "UPDATE wx_user SET email = ?,bind_account = ? where open_id = ? "
  180. }
  181. _, err = o.Raw(msql, mobile, mobile, openId).Exec()
  182. wxUserId = userId
  183. } else {
  184. if user.OpenId == "" {
  185. wxUserId = user.UserId
  186. dsql := ` DELETE FROM wx_user WHERE open_id = ? `
  187. _, err = o.Raw(dsql, openId).Exec()
  188. if err != nil {
  189. return wxUserId, err
  190. }
  191. msql := ``
  192. if loginType == 1 {
  193. msql = ` UPDATE wx_user SET open_id = ?,bind_account = ?,created_time=NOW() WHERE mobile = ? `
  194. } else {
  195. msql = ` UPDATE wx_user SET open_id = ?,bind_account = ?,created_time=NOW() WHERE email = ? `
  196. }
  197. _, err = o.Raw(msql, openId, mobile, mobile).Exec()
  198. } else {
  199. wxUserId = userId
  200. }
  201. }
  202. return
  203. }