wx_user.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. RegisterTime time.Time `description:"注册时间"`
  41. }
  42. type WxUserItem struct {
  43. UserId int `description:"用户id"`
  44. OpenId string `description:"open_id"`
  45. UnionId string `description:"union_id"`
  46. CompanyId int `description:"客户id"`
  47. NickName string `description:"用户昵称"`
  48. RealName string `description:"用户实际名称"`
  49. Mobile string `description:"手机号码"`
  50. BindAccount string `description:"绑定时的账号"`
  51. Email string `description:"邮箱"`
  52. Headimgurl string `description:"用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空"`
  53. ApplyMethod int `description:"0:未申请,1:已付费客户申请试用,2:非客户申请试用"`
  54. FirstLogin int `description:"是否第一次登陆"`
  55. }
  56. func GetWxUserItemByUserId(userId int) (item *WxUserItem, err error) {
  57. sql := `SELECT * FROM wx_user WHERE user_id=? `
  58. err = orm.NewOrm().Raw(sql, userId).QueryRow(&item)
  59. return
  60. }
  61. func GetWxUserItemByOpenId(openId string) (item *WxUserItem, err error) {
  62. sql := `SELECT * FROM wx_user WHERE open_id=? `
  63. err = orm.NewOrm().Raw(sql, openId).QueryRow(&item)
  64. return
  65. }
  66. func GetWxUserItemByUnionid(unionid string) (item *WxUserItem, err error) {
  67. sql := `SELECT * FROM wx_user WHERE union_id=? `
  68. err = orm.NewOrm().Raw(sql, unionid).QueryRow(&item)
  69. return
  70. }
  71. type PermissionSearchKeyWord struct {
  72. KeyWord string
  73. }
  74. func GetPermissionSearchKeyWord(userId int) (items []*PermissionSearchKeyWord, err error) {
  75. 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 "
  76. o := orm.NewOrm()
  77. _, err = o.Raw(sql, userId).QueryRows(&items)
  78. return
  79. }
  80. //判断客户权限总数
  81. func GetUserIsMaxPermission(companyId int) (count int, err error) {
  82. sql := ` SELECT COUNT(DISTINCT b.chart_permission_id) AS COUNT FROM company AS a
  83. INNER JOIN company_product AS c ON a.company_id=c.company_id AND c.product_id=1
  84. INNER JOIN company_report_permission AS b ON a.company_id=b.company_id
  85. WHERE b.company_id=? `
  86. o := orm.NewOrm()
  87. err = o.Raw(sql, companyId).QueryRow(&count)
  88. return
  89. }
  90. //添加用户信息
  91. func AddWxUser(item *WxUser) (err error) {
  92. o := orm.NewOrm()
  93. _, err = o.Insert(item)
  94. return
  95. }
  96. type WxLoginResp struct {
  97. Code int
  98. OpenId string
  99. Authorization string
  100. UserId int
  101. Expires time.Time
  102. FirstLogin int
  103. UserPermission int `description:"状态码"`
  104. }
  105. type UserDetail struct {
  106. FirstLogin int `description:"是否第一次登陆"`
  107. Headimgurl string `description:"用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空"`
  108. Mobile string `description:"手机号码"`
  109. Email string `description:"邮箱"`
  110. UserPermission int `description:"用户权限状态:0:付费用户,可正常查看报告,40001:获取用户信息失败,40002:非付费用户"`
  111. }
  112. func GetUserDetailByUserId(userId int) (item *UserDetail, err error) {
  113. o := orm.NewOrm()
  114. sql := `SELECT first_login,headimgurl,mobile,email FROM wx_user WHERE user_id = ? `
  115. err = o.Raw(sql, userId).QueryRow(&item)
  116. return
  117. }
  118. type CheckSmsCodeReq struct {
  119. Mobile string `description:"手机号"`
  120. SmsCode string `description:"验证码"`
  121. }
  122. type SmallLimitResp struct {
  123. IsMaxPermission int
  124. }
  125. type CheckEmailCodeReq struct {
  126. Email string `description:"邮箱"`
  127. SmsCode string `description:"验证码"`
  128. }
  129. type ApplyReq struct {
  130. ApplyMethod int `description:"申请方式:"`
  131. CompanyName string `description:"公司名称"`
  132. RealName string `description:"姓名"`
  133. }
  134. func Apply(userId, applyMethod int, mobile, email, companyName, realName, openId string) (err error) {
  135. sql := "INSERT INTO user_apply(user_id, mobile, email, company_name, real_name, apply_method) VALUES (?,?,?,?,?,?) "
  136. rddpOrm := orm.NewOrm()
  137. rddpOrm.Using("rddp")
  138. _, err = rddpOrm.Raw(sql, userId, mobile, email, companyName, realName, applyMethod).Exec()
  139. if err != nil {
  140. return
  141. }
  142. o := orm.NewOrm()
  143. if realName == "" {
  144. msql := " UPDATE wx_user SET apply_method = ?,note=? WHERE open_id = ? "
  145. _, err = o.Raw(msql, applyMethod, companyName, openId).Exec()
  146. } else {
  147. msql := " UPDATE wx_user SET apply_method = ?,real_name=?,note=? WHERE open_id = ? "
  148. _, err = o.Raw(msql, applyMethod, realName, companyName, openId).Exec()
  149. }
  150. return
  151. }
  152. type LoginReq struct {
  153. LoginType int `description:"登录方式:1:手机,2:邮箱"`
  154. Mobile string `description:"手机号"`
  155. Email string `description:"邮箱"`
  156. }
  157. type LoginResp struct {
  158. UserId int `description:"用户id"`
  159. UserPermission int `description:"手机号"`
  160. Authorization string
  161. }
  162. func BindMobile(openId, mobile string, userId, loginType int) (wxUserId int, err error) {
  163. //loginType 登录方式:1:手机,2:邮箱
  164. sql := ``
  165. if loginType == 1 {
  166. sql = `SELECT * FROM wx_user WHERE mobile = ? `
  167. } else {
  168. sql = "SELECT * FROM wx_user WHERE email = ? "
  169. }
  170. user := new(WxUser)
  171. o := orm.NewOrm()
  172. err = o.Raw(sql, mobile).QueryRow(&user)
  173. if err != nil && err.Error() != utils.ErrNoRow() {
  174. return
  175. }
  176. if user == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
  177. msql := ``
  178. if loginType == 1 {
  179. msql = "UPDATE wx_user SET mobile = ?,bind_account = ? where open_id = ? "
  180. } else {
  181. msql = "UPDATE wx_user SET email = ?,bind_account = ? where open_id = ? "
  182. }
  183. _, err = o.Raw(msql, mobile, mobile, openId).Exec()
  184. wxUserId = userId
  185. } else {
  186. if user.OpenId == "" {
  187. wxUserId = user.UserId
  188. dsql := ` DELETE FROM wx_user WHERE open_id = ? `
  189. _, err = o.Raw(dsql, openId).Exec()
  190. if err != nil {
  191. return wxUserId, err
  192. }
  193. msql := ``
  194. if loginType == 1 {
  195. msql = ` UPDATE wx_user SET open_id = ?,bind_account = ?,created_time=NOW(),register_time=NOW() WHERE mobile = ? `
  196. } else {
  197. msql = ` UPDATE wx_user SET open_id = ?,bind_account = ?,created_time=NOW(),register_time=NOW() WHERE email = ? `
  198. }
  199. _, err = o.Raw(msql, openId, mobile, mobile).Exec()
  200. } else {
  201. wxUserId = userId
  202. }
  203. }
  204. return
  205. }
  206. func ModifyFirstLogin(userId int) (err error) {
  207. o := orm.NewOrm()
  208. sql := `UPDATE wx_user SET first_login=0 WHERE user_id = ? `
  209. _, err = o.Raw(sql, userId).Exec()
  210. return
  211. }