wx_user.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. package models
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "time"
  5. )
  6. type WxUser struct {
  7. UserId int `orm:"column(user_id);pk"`
  8. OpenId string `description:"open_id"`
  9. UnionId string `description:"union_id"`
  10. Subscribe string `description:"是否关注"`
  11. CompanyId int `description:"客户id"`
  12. NickName string `description:"用户昵称"`
  13. RealName string `description:"用户实际名称"`
  14. UserCode string `description:"用户编码"`
  15. Mobile string `description:"手机号码"`
  16. BindAccount string `description:"绑定时的账号"`
  17. WxCode string `description:"微信号"`
  18. Profession string `description:"职业"`
  19. Email string `description:"邮箱"`
  20. Telephone string `description:"座机"`
  21. Sex int `description:"普通用户性别,1为男性,2为女性"`
  22. Province string `description:"普通用户个人资料填写的省份"`
  23. City string `description:"普通用户个人资料填写的城市"`
  24. Country string `description:"国家,如中国为CN"`
  25. SubscribeTime int `description:"关注时间"`
  26. Remark string `description:"备注"`
  27. Headimgurl string `description:"用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空"`
  28. Privilege string `description:"用户特权信息,json数组,如微信沃卡用户为(chinaunicom)"`
  29. Unionid string `description:"用户统一标识。针对一个微信开放平台帐号下的应用,同一用户的unionid是唯一的。"`
  30. FirstLogin int `description:"是否第一次登陆"`
  31. Enabled int `description:"是否可用"`
  32. CreatedTime time.Time `description:"创建时间"`
  33. LastUpdatedTime time.Time `description:"最新一次修改时间"`
  34. Seller string `description:"销售员"`
  35. Note string `description:"客户备份信息"`
  36. IsNote int `description:"是否备注过信息"`
  37. FromType string `description:"report' COMMENT 'report:研报,teleconference:电话会"`
  38. ApplyMethod int `description:"0:未申请,1:已付费客户申请试用,2:非客户申请试用"`
  39. RegisterTime time.Time `description:"注册时间"`
  40. RegisterPlatform int `description:"注册平台,1:微信端,2:PC网页端"`
  41. IsFreeLogin bool `description:"是否免登陆,true:免登陆,false:非免登陆"`
  42. LoginTime time.Time `description:"最近一次登录时间"`
  43. SessionKey string `description:"微信小程序会话密钥"`
  44. IsRegister int `description:"是否注册:1:已注册,0:未注册"`
  45. Source int `description:"绑定来源,1:微信端,2:pc网页端,3:查研观向小程序,4:每日咨询"`
  46. CountryCode string `description:"区号"`
  47. OutboundMobile string `description:"外呼手机号"`
  48. OutboundCountryCode string `description:"外呼手机号区号"`
  49. TripartiteCode string `description:"第三方给过来的用户编码,判断用户是否存在"`
  50. }
  51. //添加用户信息
  52. func AddWxUser(item *WxUser) (lastId int64, err error) {
  53. o := orm.NewOrm()
  54. lastId, err = o.Insert(item)
  55. return
  56. }
  57. type WxUserItem struct {
  58. UserId int `description:"用户id"`
  59. OpenId string `description:"open_id"`
  60. UnionId string `description:"union_id"`
  61. CompanyId int `description:"客户id"`
  62. NickName string `description:"用户昵称"`
  63. RealName string `description:"用户实际名称"`
  64. Mobile string `description:"手机号码"`
  65. BindAccount string `description:"绑定时的账号"`
  66. Email string `description:"邮箱"`
  67. Headimgurl string `description:"用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空"`
  68. ApplyMethod int `description:"0:未申请,1:已付费客户申请试用,2:非客户申请试用"`
  69. FirstLogin int `description:"是否第一次登陆"`
  70. IsFreeLogin int `description:"是否免登陆,true:免登陆,false:非免登陆"`
  71. LoginTime time.Time `description:"登录时间"`
  72. CreatedTime time.Time `description:"创建时间"`
  73. LastUpdatedTime time.Time `description:"最近一次修改时间"`
  74. SessionKey string `description:"微信小程序会话密钥"`
  75. CompanyName string `description:"公司名称"`
  76. IsRegister int `description:"是否注册:1:已注册,0:未注册"`
  77. CountryCode string `description:"手机国家区号"`
  78. OutboundMobile string `description:"外呼手机号"`
  79. OutboundCountryCode string `description:"外呼手机号区号"`
  80. IsMsgOutboundMobile int `description:"是否弹窗过绑定外呼手机号区号"`
  81. Source int
  82. }
  83. func GetWxUserItemByUnionid(unionid string) (item *WxUserItem, err error) {
  84. sql := `SELECT * FROM wx_user WHERE union_id=? `
  85. err = orm.NewOrm().Raw(sql, unionid).QueryRow(&item)
  86. return
  87. }
  88. func GetWxUserItemByUserId(userId int) (item *WxUserItem, err error) {
  89. sql := `SELECT a.*,b.company_name FROM wx_user AS a
  90. LEFT JOIN company AS b on a.company_id=b.company_id
  91. WHERE a.user_id=? `
  92. err = orm.NewOrm().Raw(sql, userId).QueryRow(&item)
  93. return
  94. }
  95. func GetWxUserItemByOpenId(openId string) (item *WxUserItem, err error) {
  96. sql := `SELECT * FROM wx_user WHERE open_id=? `
  97. err = orm.NewOrm().Raw(sql, openId).QueryRow(&item)
  98. return
  99. }
  100. type WxLoginResp struct {
  101. Authorization string
  102. UserId int
  103. FirstLogin int
  104. Headimgurl string `description:"用户头像"`
  105. Mobile string `description:"手机号"`
  106. Email string `description:"邮箱"`
  107. CompanyName string `description:"客户名称"`
  108. Status string `description:"状态"`
  109. EndDate string `description:"到期日期"`
  110. ProductName string `description:"客户类型名称"`
  111. }
  112. type WxGetUserInfoReq struct {
  113. RawData string `description:"rawData"`
  114. Signature string `description:"signature"`
  115. EncryptedData string `description:"encryptedData"`
  116. Iv string `description:"iv"`
  117. }
  118. //修改用户会话key
  119. func ModifyWxUserSessionKey(sessionKey string, userId int) (err error) {
  120. o := orm.NewOrm()
  121. sql := `UPDATE wx_user SET session_key=? WHERE user_id=? `
  122. _, err = o.Raw(sql, sessionKey, userId).Exec()
  123. return
  124. }
  125. //添加用户信息
  126. func ModifyWxUserInfo(unionId, nickName, province, city, country, avatar string, gender, userId int) (err error) {
  127. o := orm.NewOrm()
  128. sql := `UPDATE wx_user SET union_id=?,unionid=?,nick_name=?,sex=?,province=?,city=?,country=?,headimgurl=? WHERE user_id=? `
  129. _, err = o.Raw(sql, unionId, unionId, nickName, gender, province, city, country, avatar, userId).Exec()
  130. return
  131. }
  132. //修改用户会话key
  133. func DeleteWxUserByUserId(userId int) (err error) {
  134. o := orm.NewOrm()
  135. sql := `DELETE FROM wx_user WHERE user_id=? `
  136. _, err = o.Raw(sql, userId).Exec()
  137. return
  138. }
  139. type WxGetUserInfoResp struct {
  140. //UsersId int `orm:"column(id);pk"`
  141. //Mobile string `description:"手机号"`
  142. //NickName string `description:"昵称"`
  143. //Gender int `description:"用户性别 1:男性,2:女性,0:未知(默认)"`
  144. //CreateTime time.Time `description:"注册时间"`
  145. //ModifyTime time.Time `description:"修改时间"`
  146. //AvatarUrl string `description:"头像地址"`
  147. //City string `description:"城市"`
  148. //Province string `description:"省"`
  149. //Country string `description:"国家"`
  150. //Language string `description:"语言"`
  151. //Appid string `description:"Appid"`
  152. //Timestamp int64 `description:"时间戳"`
  153. Authorization string `description:"登陆凭证,后续接口调用时,带在请求头里面Key:Authorization"`
  154. }
  155. type WxGetPhoneNumberReq struct {
  156. EncryptedData string `description:"encryptedData"`
  157. Iv string `description:"iv"`
  158. }
  159. func ModifyUsersMobile(usersId int, phoneNumber string) (err error) {
  160. o := orm.NewOrm()
  161. sql := `UPDATE wx_user SET mobile=? WHERE user_id=? `
  162. _, err = o.Raw(sql, phoneNumber, usersId).Exec()
  163. return
  164. }
  165. type WxGetPhoneNumberResp struct {
  166. PhoneNumber string `description:"用户绑定的手机号(国外手机号会有区号)"`
  167. PurePhoneNumber string `description:"没有区号的手机号"`
  168. CountryCode string `description:"区号"`
  169. }
  170. func GetWxUserItemByMobile(mobile string) (item *WxUserItem, err error) {
  171. sql := `SELECT a.*,b.company_name FROM wx_user AS a
  172. LEFT JOIN company AS b on a.company_id=b.company_id
  173. WHERE a.mobile=? `
  174. err = orm.NewOrm().Raw(sql, mobile).QueryRow(&item)
  175. return
  176. }
  177. func GetWxUserItemByEmail(email string) (item *WxUserItem, err error) {
  178. sql := `SELECT * FROM wx_user WHERE email=? `
  179. err = orm.NewOrm().Raw(sql, email).QueryRow(&item)
  180. return
  181. }
  182. func ModifyReportLastViewTime(uid int) (err error) {
  183. sql := ` UPDATE wx_user SET report_last_view_time=NOW()
  184. WHERE user_id=? `
  185. _, err = orm.NewOrm().Raw(sql, uid).Exec()
  186. return
  187. }
  188. //变更联系人是否已注册状态
  189. func ModifyWxUserRegisterStatus(userId int) (err error) {
  190. o := orm.NewOrm()
  191. sql := `UPDATE wx_user SET is_register=?,source=3,register_time=NOW() WHERE user_id = ? `
  192. _, err = o.Raw(sql, 1, userId).Exec()
  193. return
  194. }
  195. //修改用户是否绑定外呼手机号弹窗
  196. func ModifyWxUserIsMsgOutboundMobile(userId int) (err error) {
  197. o := orm.NewOrm()
  198. sql := `UPDATE wx_user SET is_msg_outbound_mobile=1 WHERE user_id=? `
  199. _, err = o.Raw(sql, userId).Exec()
  200. return
  201. }
  202. //列表
  203. func GetUserListAll() (items []*WxUserItem, err error) {
  204. o := orm.NewOrm()
  205. sql := `SELECT * FROM wx_user WHERE mobile <>'' AND outbound_mobile = ''`
  206. _, err = o.Raw(sql).QueryRows(&items)
  207. return
  208. }
  209. //修改手机号区号 8位号码+852,9位号码+886,10位号码+1,11位及以上号码+86
  210. func UPdateUserCountryCode(item *WxUserItem) (err error) {
  211. o := orm.NewOrm()
  212. if item.CountryCode == "" && len(item.Mobile) >= 11 {
  213. sql := ` UPDATE wx_user SET outbound_mobile= ? , outbound_country_code=86 , country_code=86 WHERE user_id = ?`
  214. _, err = o.Raw(sql, item.Mobile, item.UserId).Exec()
  215. } else if item.CountryCode == "" && len(item.Mobile) == 8 {
  216. sql := ` UPDATE wx_user SET outbound_mobile= ? , outbound_country_code=852 , country_code=852 WHERE user_id = ?`
  217. _, err = o.Raw(sql, item.Mobile, item.UserId).Exec()
  218. } else if item.CountryCode == "" && len(item.Mobile) == 9 {
  219. sql := ` UPDATE wx_user SET outbound_mobile= ? , outbound_country_code=886 , country_code=886 WHERE user_id = ?`
  220. _, err = o.Raw(sql, item.Mobile, item.UserId).Exec()
  221. } else if item.CountryCode == "" && len(item.Mobile) == 10 {
  222. sql := ` UPDATE wx_user SET outbound_mobile= ? , outbound_country_code=1 , country_code=1 WHERE user_id = ?`
  223. _, err = o.Raw(sql, item.Mobile, item.UserId).Exec()
  224. } else {
  225. sql := ` UPDATE wx_user SET outbound_mobile= ? , outbound_country_code=? WHERE user_id = ?`
  226. _, err = o.Raw(sql, item.Mobile, item.CountryCode, item.UserId).Exec()
  227. }
  228. return
  229. }
  230. //判断公司下用户名称是否存在
  231. func GetUserCountByName(companyId int, name string) (count int, err error) {
  232. sql := `SELECT COUNT(1) AS count FROM wx_user WHERE company_id = ? AND real_name = ?`
  233. err = orm.NewOrm().Raw(sql, companyId, name).QueryRow(&count)
  234. return
  235. }
  236. //判断公司下用户名称是否存在
  237. func GetUserCountByThirdName(companyId int, name string) (count int, err error) {
  238. sql := `SELECT COUNT(1) AS count FROM wx_user WHERE company_id = ? AND tripartite_code = ?`
  239. err = orm.NewOrm().Raw(sql, companyId, name).QueryRow(&count)
  240. return
  241. }
  242. func UpdateUserMobile(uid int, mobile string) (err error) {
  243. sql := ` UPDATE wx_user SET mobile=?
  244. WHERE user_id=? `
  245. _, err = orm.NewOrm().Raw(sql, mobile, uid).Exec()
  246. return
  247. }
  248. //获取公司下用户详情详情详情
  249. func GetUserByName(companyId int, name string) (item *WxUser, err error) {
  250. o := orm.NewOrm()
  251. sql := `SELECT * FROM wx_user WHERE company_id = ? AND real_name = ? `
  252. err = o.Raw(sql, companyId, name).QueryRow(&item)
  253. return
  254. }
  255. //获取公司下用户详情详情详情
  256. func GetUserByThirdName(companyId int, name string) (item *WxUser, err error) {
  257. o := orm.NewOrm()
  258. sql := `SELECT * FROM wx_user WHERE company_id = ? AND tripartite_code = ? `
  259. err = o.Raw(sql, companyId, name).QueryRow(&item)
  260. return
  261. }