wx_user.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. package models
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "hongze/hongze_api/utils"
  6. "strings"
  7. "time"
  8. )
  9. type WxUser struct {
  10. UserId int `orm:"column(user_id);pk"`
  11. OpenId string `description:"open_id"`
  12. UnionId string `description:"union_id"`
  13. Subscribe string `description:"是否关注"`
  14. CompanyId int `description:"客户id"`
  15. NickName string `description:"用户昵称"`
  16. RealName string `description:"用户实际名称"`
  17. UserCode string `description:"用户编码"`
  18. Mobile string `description:"手机号码"`
  19. BindAccount string `description:"绑定时的账号"`
  20. WxCode string `description:"微信号"`
  21. Profession string `description:"职业"`
  22. Email string `description:"邮箱"`
  23. Telephone string `description:"座机"`
  24. Sex int `description:"普通用户性别,1为男性,2为女性"`
  25. Province string `description:"普通用户个人资料填写的省份"`
  26. City string `description:"普通用户个人资料填写的城市"`
  27. Country string `description:"国家,如中国为CN"`
  28. SubscribeTime int `description:"关注时间"`
  29. Remark string `description:"备注"`
  30. Headimgurl string `description:"用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空"`
  31. Privilege string `description:"用户特权信息,json数组,如微信沃卡用户为(chinaunicom)"`
  32. Unionid string `description:"用户统一标识。针对一个微信开放平台帐号下的应用,同一用户的unionid是唯一的。"`
  33. FirstLogin int `description:"是否第一次登陆"`
  34. Enabled int `description:"是否可用"`
  35. CreatedTime time.Time `description:"创建时间"`
  36. LastUpdatedTime time.Time `description:"最新一次修改时间"`
  37. Seller string `description:"销售员"`
  38. Note string `description:"客户备份信息"`
  39. IsNote int `description:"是否备注过信息"`
  40. FromType string `description:"report' COMMENT 'report:研报,teleconference:电话会"`
  41. ApplyMethod int `description:"0:未申请,1:已付费客户申请试用,2:非客户申请试用"`
  42. RegisterTime time.Time `description:"注册时间"`
  43. RegisterPlatform int `description:"注册平台,1:微信端,2:PC网页端"`
  44. IsFreeLogin bool `description:"是否免登陆,true:免登陆,false:非免登陆"`
  45. LoginTime time.Time `description:"最近一次登录时间"`
  46. IsRegister int `description:"是否注册:1:已注册,0:未注册"`
  47. Source int `description:"绑定来源,1:微信端,2:pc网页端,3:查研观向小程序,4:每日咨询"`
  48. CountryCode string `description:"区号,86、852、886等"`
  49. OutboundMobile string `description:"外呼手机号"`
  50. OutboundCountryCode string `description:"外呼手机号区号,86、852、886等"`
  51. }
  52. type WxUserItem struct {
  53. UserId int `description:"用户id"`
  54. OpenId string `description:"open_id"`
  55. UnionId string `description:"union_id"`
  56. CompanyId int `description:"客户id"`
  57. NickName string `description:"用户昵称"`
  58. RealName string `description:"用户实际名称"`
  59. Mobile string `description:"手机号码"`
  60. BindAccount string `description:"绑定时的账号"`
  61. Email string `description:"邮箱"`
  62. Headimgurl string `description:"用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空"`
  63. ApplyMethod int `description:"0:未申请,1:已付费客户申请试用,2:非客户申请试用"`
  64. FirstLogin int `description:"是否第一次登陆"`
  65. IsFreeLogin int `description:"是否免登陆,true:免登陆,false:非免登陆"`
  66. LoginTime time.Time `description:"登录时间"`
  67. CreatedTime time.Time `description:"创建时间"`
  68. LastUpdatedTime time.Time `description:"最近一次修改时间"`
  69. IsRegister int `description:"是否注册:1:已注册,0:未注册"`
  70. Note string `description:"客户备份信息"`
  71. IsNote int8 `description:"是否备注过信息"`
  72. }
  73. func GetWxUserItemByUserId(userId int) (item *WxUserItem, err error) {
  74. sql := `SELECT * FROM wx_user WHERE user_id=? `
  75. err = orm.NewOrm().Raw(sql, userId).QueryRow(&item)
  76. return
  77. }
  78. func GetWxUserItemByOpenId(openId string) (item *WxUserItem, err error) {
  79. sql := `SELECT * FROM wx_user WHERE open_id=? `
  80. err = orm.NewOrm().Raw(sql, openId).QueryRow(&item)
  81. return
  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. type PermissionSearchKeyWord struct {
  89. KeyWord string
  90. }
  91. func GetPermissionSearchKeyWord(userId int) (items []*PermissionSearchKeyWord, err error) {
  92. 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 "
  93. o := orm.NewOrm()
  94. _, err = o.Raw(sql, userId).QueryRows(&items)
  95. return
  96. }
  97. // 判断客户权限总数
  98. func GetUserIsMaxPermission(companyId int) (count int, err error) {
  99. sql := ` SELECT COUNT(DISTINCT b.chart_permission_id) AS COUNT FROM company AS a
  100. INNER JOIN company_product AS c ON a.company_id=c.company_id AND c.product_id=1
  101. INNER JOIN company_report_permission AS b ON a.company_id=b.company_id
  102. WHERE b.company_id=? `
  103. o := orm.NewOrm()
  104. err = o.Raw(sql, companyId).QueryRow(&count)
  105. return
  106. }
  107. // 添加用户信息
  108. func AddWxUser(item *WxUser) (lastId int64, err error) {
  109. o := orm.NewOrm()
  110. lastId, err = o.Insert(item)
  111. return
  112. }
  113. type WxLoginResp struct {
  114. Code int
  115. Authorization string
  116. UserId int
  117. Expires time.Time
  118. FirstLogin int
  119. UserPermission int `description:"状态码"`
  120. Headimgurl string `description:"用户头像"`
  121. Mobile string `description:"手机号"`
  122. Email string `description:"邮箱"`
  123. CompanyName string `description:"客户名称"`
  124. Status string `description:"状态"`
  125. EndDate string `description:"到期日期"`
  126. ProductName string `description:"客户类型名称"`
  127. }
  128. type UserDetail struct {
  129. FirstLogin int `description:"是否第一次登陆"`
  130. Headimgurl string `description:"用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空"`
  131. Mobile string `description:"手机号码"`
  132. Email string `description:"邮箱"`
  133. UserPermission int `description:"用户权限状态:0:付费用户,可正常查看报告,40001:获取用户信息失败,40002:非付费用户"`
  134. }
  135. func GetUserDetailByUserId(userId int) (item *UserDetail, err error) {
  136. o := orm.NewOrm()
  137. sql := `SELECT first_login,headimgurl,mobile,email FROM wx_user WHERE user_id = ? `
  138. err = o.Raw(sql, userId).QueryRow(&item)
  139. return
  140. }
  141. type CheckSmsCodeReq struct {
  142. Mobile string `description:"手机号"`
  143. SmsCode string `description:"验证码"`
  144. }
  145. type SmallLimitResp struct {
  146. IsMaxPermission int
  147. }
  148. type CheckEmailCodeReq struct {
  149. Email string `description:"邮箱"`
  150. SmsCode string `description:"验证码"`
  151. }
  152. type ApplyReq struct {
  153. ApplyMethod int `description:"申请方式:"`
  154. CompanyName string `description:"公司名称"`
  155. RealName string `description:"姓名"`
  156. }
  157. func Apply(userId, applyMethod int, mobile, email, companyName, realName, openId string) (err error) {
  158. sql := "INSERT INTO user_apply(user_id, mobile, email, company_name, real_name, apply_method) VALUES (?,?,?,?,?,?) "
  159. rddpOrm := orm.NewOrmUsingDB("rddp")
  160. _, err = rddpOrm.Raw(sql, userId, mobile, email, companyName, realName, applyMethod).Exec()
  161. if err != nil {
  162. return
  163. }
  164. o := orm.NewOrm()
  165. if realName == "" {
  166. msql := " UPDATE wx_user SET apply_method = ?,note=? WHERE open_id = ? "
  167. _, err = o.Raw(msql, applyMethod, companyName, openId).Exec()
  168. } else {
  169. msql := " UPDATE wx_user SET apply_method = ?,real_name=?,note=? WHERE user_id = ? "
  170. _, err = o.Raw(msql, applyMethod, realName, companyName, userId).Exec()
  171. }
  172. return
  173. }
  174. type LoginReq struct {
  175. LoginType int `description:"登录方式:1:手机,2:邮箱"`
  176. Mobile string `description:"手机号"`
  177. Email string `description:"邮箱"`
  178. AreaNum int `description:"国际区号"`
  179. }
  180. type LoginResp struct {
  181. UserId int `description:"用户id"`
  182. UserPermission int `description:"权限"`
  183. Authorization string `description:"Token"`
  184. Headimgurl string `description:"用户头像"`
  185. Mobile string `description:"手机号"`
  186. Email string `description:"邮箱"`
  187. CompanyName string `description:"客户名称"`
  188. Status string `description:"状态"`
  189. EndDate string `description:"到期日期"`
  190. ProductName string `description:"客户类型名称"`
  191. }
  192. func BindMobile(openId, mobile string, userId, loginType int) (wxUserId int, err error) {
  193. utils.FileLog.Info("BindMobile: openId: %s,mobile: %s,userId: %d,loginType:%d ", openId, mobile, userId, loginType)
  194. mobile = strings.Trim(mobile, " ")
  195. //loginType 登录方式:1:手机,2:邮箱
  196. sql := ``
  197. if loginType == 1 {
  198. sql = `SELECT * FROM wx_user WHERE mobile = ? `
  199. } else {
  200. sql = "SELECT * FROM wx_user WHERE email = ? "
  201. }
  202. user := new(WxUser)
  203. o := orm.NewOrm()
  204. err = o.Raw(sql, mobile).QueryRow(&user)
  205. fmt.Println("err:", err)
  206. if err != nil && err.Error() != utils.ErrNoRow() {
  207. return
  208. }
  209. fmt.Println(user)
  210. if user == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
  211. utils.FileLog.Info("user is nil ")
  212. fmt.Println("line 210")
  213. msql := ``
  214. if loginType == 1 {
  215. msql = "UPDATE wx_user SET mobile = ?,bind_account = ? where open_id = ? "
  216. } else {
  217. msql = "UPDATE wx_user SET email = ?,bind_account = ? where open_id = ? "
  218. }
  219. fmt.Println("bind")
  220. fmt.Println(msql, mobile, mobile, openId)
  221. _, err = o.Raw(msql, mobile, mobile, openId).Exec()
  222. wxUserId = userId
  223. } else {
  224. utils.FileLog.Info("user is not nil ")
  225. fmt.Println("line 223")
  226. if openId != "" {
  227. sql = `SELECT * FROM wx_user WHERE open_id = ? `
  228. openIdUser := new(WxUser)
  229. o := orm.NewOrm()
  230. err = o.Raw(sql, openId).QueryRow(&openIdUser)
  231. if openIdUser != nil {
  232. if openIdUser.UserId != user.UserId {
  233. utils.FileLog.Info("user id is not eq %d , %d", openIdUser.UserId, user.UserId)
  234. wxUserId = user.UserId
  235. dsql := ` DELETE FROM wx_user WHERE open_id = ? `
  236. _, err = o.Raw(dsql, openId).Exec()
  237. if err != nil {
  238. return wxUserId, err
  239. }
  240. }
  241. }
  242. msql := ``
  243. ssql := ``
  244. if loginType == 1 {
  245. msql = ` UPDATE wx_user SET open_id = ?,bind_account = ?,created_time=NOW(),register_time=NOW() WHERE mobile = ? `
  246. ssql = `SELECT * FROM wx_user WHERE mobile = ? `
  247. } else {
  248. msql = ` UPDATE wx_user SET open_id = ?,bind_account = ?,created_time=NOW(),register_time=NOW() WHERE email = ? `
  249. ssql = `SELECT * FROM wx_user WHERE email = ? `
  250. }
  251. _, err = o.Raw(msql, openId, mobile, mobile).Exec()
  252. bindUser := new(WxUser)
  253. err = o.Raw(ssql, mobile).QueryRow(&bindUser)
  254. if bindUser != nil && bindUser.UserId > 0 {
  255. wxUserId = bindUser.UserId
  256. }
  257. } else {
  258. fmt.Println("line 239")
  259. wxUserId = userId
  260. }
  261. }
  262. if wxUserId <= 0 && (user != nil && err.Error() != utils.ErrNoRow()) {
  263. utils.FileLog.Info("wxUserId =0 %d , %d", user.UserId)
  264. wxUserId = user.UserId
  265. }
  266. return
  267. }
  268. func PcBindMobile(unionId, mobile string, userId, loginType int) (wxUserId int, err error) {
  269. //loginType 登录方式:1:手机,2:邮箱
  270. utils.FileLog.Info("绑定参数:%s %s %d %d", unionId, mobile, userId, loginType)
  271. sql := ``
  272. if loginType == 1 {
  273. sql = `SELECT * FROM wx_user WHERE mobile = ? `
  274. } else {
  275. sql = "SELECT * FROM wx_user WHERE email = ? "
  276. }
  277. user := new(WxUser)
  278. o := orm.NewOrm()
  279. err = o.Raw(sql, mobile).QueryRow(&user)
  280. if err != nil && err.Error() != utils.ErrNoRow() {
  281. return
  282. }
  283. if user == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
  284. utils.FileLog.Info("用户不存在,根据union_id绑定")
  285. msql := ``
  286. if loginType == 1 {
  287. msql = "UPDATE wx_user SET mobile = ?,bind_account = ? WHERE union_id = ? "
  288. } else {
  289. msql = "UPDATE wx_user SET email = ?,bind_account = ? WHERE union_id = ? "
  290. }
  291. _, err = o.Raw(msql, mobile, mobile, unionId).Exec()
  292. wxUserId = userId
  293. } else {
  294. utils.FileLog.Info("用户存在,user.UnionId:%s", user.UnionId)
  295. if user.UnionId == "" {
  296. sql = `SELECT * FROM wx_user WHERE union_id = ? `
  297. userInfo := new(WxUser)
  298. o := orm.NewOrm()
  299. err = o.Raw(sql, unionId).QueryRow(&userInfo)
  300. if err != nil {
  301. return
  302. }
  303. utils.FileLog.Info("user.RegisterTime %s", user.RegisterTime.Format(utils.FormatDateTime))
  304. utils.FileLog.Info("userInfo.RegisterTime %s", userInfo.RegisterTime.Format(utils.FormatDateTime))
  305. var maxRegisterTime time.Time
  306. if user.RegisterTime.Before(userInfo.RegisterTime) {
  307. maxRegisterTime = user.RegisterTime
  308. utils.FileLog.Info("after")
  309. } else {
  310. maxRegisterTime = userInfo.RegisterTime
  311. utils.FileLog.Info("not after")
  312. }
  313. utils.FileLog.Info("maxRegisterTime %s", maxRegisterTime.Format(utils.FormatDateTime))
  314. wxUserId = user.UserId
  315. dsql := ` DELETE FROM wx_user WHERE union_id = ? `
  316. _, err = o.Raw(dsql, unionId).Exec()
  317. if err != nil {
  318. return wxUserId, err
  319. }
  320. msql := ` UPDATE wx_user SET union_id=?,register_time=?,province=?,city=?,country=?,headimgurl=?,unionid=?,sex=? WHERE user_id = ? `
  321. _, err = o.Raw(msql, unionId, maxRegisterTime, userInfo.Province, userInfo.City, userInfo.Country, userInfo.Headimgurl, unionId, userInfo.Sex, user.UserId).Exec()
  322. wxUserId = user.UserId
  323. } else {
  324. sql = `SELECT * FROM wx_user WHERE user_id = ? `
  325. userInfo := new(WxUser)
  326. o := orm.NewOrm()
  327. err = o.Raw(sql, userId).QueryRow(&userInfo)
  328. if err != nil && err.Error() != utils.ErrNoRow() {
  329. return
  330. }
  331. dsql := ` DELETE FROM wx_user WHERE user_id = ? `
  332. _, err = o.Raw(dsql, userId).Exec()
  333. if err != nil {
  334. return user.UserId, err
  335. }
  336. if user.Mobile == "" && loginType == 1 {
  337. msql := ` UPDATE wx_user SET mobile = ?,bind_account = ? WHERE user_id = ?`
  338. _, err = o.Raw(msql, mobile, mobile, user.UserId).Exec()
  339. wxUserId = user.UserId
  340. }
  341. if user.Email == "" && loginType == 2 {
  342. msql := ` UPDATE wx_user SET email = ?,bind_account = ? WHERE user_id = ?`
  343. _, err = o.Raw(msql, mobile, mobile, user.UserId).Exec()
  344. wxUserId = user.UserId
  345. }
  346. utils.FileLog.Info("用户存在,bind:%s,%d,%s", unionId, wxUserId)
  347. wxUserId = userId
  348. }
  349. }
  350. return
  351. }
  352. func ModifyFirstLogin(userId int) (err error) {
  353. o := orm.NewOrm()
  354. sql := `UPDATE wx_user SET first_login=0 WHERE user_id = ? `
  355. _, err = o.Raw(sql, userId).Exec()
  356. return
  357. }
  358. func GetWxUserItemByEmail(email string) (item *WxUserItem, err error) {
  359. sql := `SELECT * FROM wx_user WHERE email=? `
  360. err = orm.NewOrm().Raw(sql, email).QueryRow(&item)
  361. return
  362. }
  363. func GetWxUserItemByMobile(mobile string) (item *WxUserItem, err error) {
  364. sql := `SELECT * FROM wx_user WHERE mobile=? OR mobile_two=? `
  365. err = orm.NewOrm().Raw(sql, mobile, mobile).QueryRow(&item)
  366. return
  367. }
  368. type PcLoginReq struct {
  369. LoginType int `description:"登录方式:1:手机,2:邮箱"`
  370. Mobile string `description:"手机号"`
  371. Email string `description:"邮箱"`
  372. SmsCode string `description:"短信/邮箱验证码"`
  373. IsFreeLogin bool `description:"是否免登陆,true:免登陆,false:非免登陆"`
  374. }
  375. type BindReq struct {
  376. BindType int `description:"绑定方式:1:手机,2:邮箱"`
  377. Mobile string `description:"手机号"`
  378. Email string `description:"邮箱"`
  379. AreaNum int `description:"国际区号"`
  380. VerifyCode string `description:"短信/邮箱 验证码"`
  381. }
  382. func ModifyLoginTime(userId int, isFreeLogin bool) (err error) {
  383. o := orm.NewOrm()
  384. sql := `UPDATE wx_user SET is_free_login=?,login_time=NOW() WHERE user_id = ? `
  385. _, err = o.Raw(sql, isFreeLogin, userId).Exec()
  386. return
  387. }
  388. func GetCustomPermission(companyId int) (count int, err error) {
  389. o := orm.NewOrm()
  390. sql := `SELECT COUNT(1) AS count FROM company AS a
  391. INNER JOIN company_product AS b ON a.company_id=b.company_id
  392. WHERE b.company_id=? AND b.status IN('试用','正式')
  393. GROUP BY b.company_id`
  394. err = o.Raw(sql, companyId).QueryRow(&count)
  395. return
  396. }
  397. type CheckLoginResp struct {
  398. IsTips bool `description:"true:需要提示,false:不需要提示"`
  399. IsBind bool `description:"true:需要绑定邮箱或验证码,false:不需要绑定邮箱或验证码"`
  400. }
  401. func GetOpenIdAll() (items []*WxUserItem, err error) {
  402. sql := ` SELECT * FROM wx_user WHERE open_id<>'' AND union_id IS NULL `
  403. o := orm.NewOrm()
  404. _, err = o.Raw(sql).QueryRows(&items)
  405. return
  406. }
  407. func ModifyWxUserUnionId(unionId string, userId int) (err error) {
  408. o := orm.NewOrm()
  409. sql := `UPDATE wx_user SET union_id=?,unionid=? WHERE user_id = ? `
  410. _, err = o.Raw(sql, unionId, unionId, userId).Exec()
  411. return
  412. }
  413. func GetWxUserAll() (items []*WxUserItem, err error) {
  414. sql := ` SELECT a.* FROM wx_user AS a
  415. INNER JOIN company_product AS b ON a.company_id=b.company_id
  416. WHERE b.status IN('正式','试用')
  417. AND b.product_id=1 `
  418. o := orm.NewOrm()
  419. _, err = o.Raw(sql).QueryRows(&items)
  420. return
  421. }
  422. // 变更联系人是否已注册状态
  423. func ModifyWxUserRegisterStatus(userId, status, source int, registerTime time.Time) (err error) {
  424. o := orm.NewOrm()
  425. sql := `UPDATE wx_user SET is_register=?,source=?,register_time=? WHERE user_id = ? `
  426. _, err = o.Raw(sql, status, source, registerTime, userId).Exec()
  427. return
  428. }
  429. // ModifyWxUserNameApplyMethod 更新用户名称等信息
  430. func ModifyWxUserNameApplyMethod(userId int, realName, note string, isNote int8, applyMethod int) (err error) {
  431. o := orm.NewOrm()
  432. sql := `UPDATE wx_user SET real_name=?,note=?,is_note=?,apply_method=? WHERE user_id = ? `
  433. _, err = o.Raw(sql, realName, note, isNote, applyMethod, userId).Exec()
  434. return
  435. }
  436. func UserSubscribe(subscribeType int, openId string) (err error) {
  437. o := orm.NewOrm()
  438. sql := `UPDATE user_record SET subscribe=?,subscribe_time=NOW() WHERE open_id = ? AND create_platform=1 `
  439. _, err = o.Raw(sql, subscribeType, openId).Exec()
  440. return
  441. }
  442. // UpdateWxUserViewData 更新客户产品的阅读记录(阅读时间、总阅读次数)
  443. func UpdateWxUserViewData(userId, productId int, lastViewTime string) (err error) {
  444. sql := ``
  445. if productId == 2 {
  446. sql = ` update wx_user set rai_view_total=rai_view_total+1,rai_last_view_time=?,report_last_view_time = ? WHERE user_id=? `
  447. } else {
  448. sql = ` update wx_user set ficc_view_total=ficc_view_total+1,ficc_last_view_time=?,report_last_view_time = ? WHERE user_id=? `
  449. }
  450. o := orm.NewOrm()
  451. _, err = o.Raw(sql, lastViewTime, lastViewTime, userId).Exec()
  452. return
  453. }