wx_user.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. package models
  2. import (
  3. "fmt"
  4. "hongze/hongze_api/utils"
  5. "rdluck_tools/orm"
  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. }
  71. func GetWxUserItemByUserId(userId int) (item *WxUserItem, err error) {
  72. sql := `SELECT * FROM wx_user WHERE user_id=? `
  73. err = orm.NewOrm().Raw(sql, userId).QueryRow(&item)
  74. return
  75. }
  76. func GetWxUserItemByOpenId(openId string) (item *WxUserItem, err error) {
  77. sql := `SELECT * FROM wx_user WHERE open_id=? `
  78. err = orm.NewOrm().Raw(sql, openId).QueryRow(&item)
  79. return
  80. }
  81. func GetWxUserItemByUnionid(unionid string) (item *WxUserItem, err error) {
  82. sql := `SELECT * FROM wx_user WHERE union_id=? `
  83. err = orm.NewOrm().Raw(sql, unionid).QueryRow(&item)
  84. return
  85. }
  86. type PermissionSearchKeyWord struct {
  87. KeyWord string
  88. }
  89. func GetPermissionSearchKeyWord(userId int) (items []*PermissionSearchKeyWord, err error) {
  90. 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 "
  91. o := orm.NewOrm()
  92. _, err = o.Raw(sql, userId).QueryRows(&items)
  93. return
  94. }
  95. //判断客户权限总数
  96. func GetUserIsMaxPermission(companyId int) (count int, err error) {
  97. sql := ` SELECT COUNT(DISTINCT b.chart_permission_id) AS COUNT FROM company AS a
  98. INNER JOIN company_product AS c ON a.company_id=c.company_id AND c.product_id=1
  99. INNER JOIN company_report_permission AS b ON a.company_id=b.company_id
  100. WHERE b.company_id=? `
  101. o := orm.NewOrm()
  102. err = o.Raw(sql, companyId).QueryRow(&count)
  103. return
  104. }
  105. //添加用户信息
  106. func AddWxUser(item *WxUser) (lastId int64, err error) {
  107. o := orm.NewOrm()
  108. lastId, err = o.Insert(item)
  109. return
  110. }
  111. type WxLoginResp struct {
  112. Code int
  113. Authorization string
  114. UserId int
  115. Expires time.Time
  116. FirstLogin int
  117. UserPermission int `description:"状态码"`
  118. Headimgurl string `description:"用户头像"`
  119. Mobile string `description:"手机号"`
  120. Email string `description:"邮箱"`
  121. CompanyName string `description:"客户名称"`
  122. Status string `description:"状态"`
  123. EndDate string `description:"到期日期"`
  124. ProductName string `description:"客户类型名称"`
  125. }
  126. type UserDetail struct {
  127. FirstLogin int `description:"是否第一次登陆"`
  128. Headimgurl string `description:"用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空"`
  129. Mobile string `description:"手机号码"`
  130. Email string `description:"邮箱"`
  131. UserPermission int `description:"用户权限状态:0:付费用户,可正常查看报告,40001:获取用户信息失败,40002:非付费用户"`
  132. }
  133. func GetUserDetailByUserId(userId int) (item *UserDetail, err error) {
  134. o := orm.NewOrm()
  135. sql := `SELECT first_login,headimgurl,mobile,email FROM wx_user WHERE user_id = ? `
  136. err = o.Raw(sql, userId).QueryRow(&item)
  137. return
  138. }
  139. type CheckSmsCodeReq struct {
  140. Mobile string `description:"手机号"`
  141. SmsCode string `description:"验证码"`
  142. }
  143. type SmallLimitResp struct {
  144. IsMaxPermission int
  145. }
  146. type CheckEmailCodeReq struct {
  147. Email string `description:"邮箱"`
  148. SmsCode string `description:"验证码"`
  149. }
  150. type ApplyReq struct {
  151. ApplyMethod int `description:"申请方式:"`
  152. CompanyName string `description:"公司名称"`
  153. RealName string `description:"姓名"`
  154. }
  155. func Apply(userId, applyMethod int, mobile, email, companyName, realName, openId string) (err error) {
  156. sql := "INSERT INTO user_apply(user_id, mobile, email, company_name, real_name, apply_method) VALUES (?,?,?,?,?,?) "
  157. rddpOrm := orm.NewOrm()
  158. rddpOrm.Using("rddp")
  159. _, err = rddpOrm.Raw(sql, userId, mobile, email, companyName, realName, applyMethod).Exec()
  160. if err != nil {
  161. return
  162. }
  163. o := orm.NewOrm()
  164. if realName == "" {
  165. msql := " UPDATE wx_user SET apply_method = ?,note=? WHERE open_id = ? "
  166. _, err = o.Raw(msql, applyMethod, companyName, openId).Exec()
  167. } else {
  168. msql := " UPDATE wx_user SET apply_method = ?,real_name=?,note=? WHERE user_id = ? "
  169. _, err = o.Raw(msql, applyMethod, realName, companyName, userId).Exec()
  170. }
  171. return
  172. }
  173. type LoginReq struct {
  174. LoginType int `description:"登录方式:1:手机,2:邮箱"`
  175. Mobile string `description:"手机号"`
  176. Email string `description:"邮箱"`
  177. AreaNum string `description:"国际区号"`
  178. }
  179. type LoginResp struct {
  180. UserId int `description:"用户id"`
  181. UserPermission int `description:"权限"`
  182. Authorization string `description:"Token"`
  183. Headimgurl string `description:"用户头像"`
  184. Mobile string `description:"手机号"`
  185. Email string `description:"邮箱"`
  186. CompanyName string `description:"客户名称"`
  187. Status string `description:"状态"`
  188. EndDate string `description:"到期日期"`
  189. ProductName string `description:"客户类型名称"`
  190. }
  191. func BindMobile(openId, mobile string, userId, loginType int) (wxUserId int, err error) {
  192. utils.FileLog.Info("BindMobile: openId: %s,mobile: %s,userId: %d,loginType:%d ", openId, mobile, userId, loginType)
  193. mobile = strings.Trim(mobile, " ")
  194. //loginType 登录方式:1:手机,2:邮箱
  195. sql := ``
  196. if loginType == 1 {
  197. sql = `SELECT * FROM wx_user WHERE mobile = ? `
  198. } else {
  199. sql = "SELECT * FROM wx_user WHERE email = ? "
  200. }
  201. user := new(WxUser)
  202. o := orm.NewOrm()
  203. err = o.Raw(sql, mobile).QueryRow(&user)
  204. fmt.Println("err:", err)
  205. if err != nil && err.Error() != utils.ErrNoRow() {
  206. return
  207. }
  208. fmt.Println(user)
  209. if user == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
  210. utils.FileLog.Info("user is nil ")
  211. fmt.Println("line 210")
  212. msql := ``
  213. if loginType == 1 {
  214. msql = "UPDATE wx_user SET mobile = ?,bind_account = ? where open_id = ? "
  215. } else {
  216. msql = "UPDATE wx_user SET email = ?,bind_account = ? where open_id = ? "
  217. }
  218. fmt.Println("bind")
  219. fmt.Println(msql, mobile, mobile, openId)
  220. _, err = o.Raw(msql, mobile, mobile, openId).Exec()
  221. wxUserId = userId
  222. } else {
  223. utils.FileLog.Info("user is not nil ")
  224. fmt.Println("line 223")
  225. if openId != "" {
  226. sql = `SELECT * FROM wx_user WHERE open_id = ? `
  227. openIdUser := new(WxUser)
  228. o := orm.NewOrm()
  229. err = o.Raw(sql, openId).QueryRow(&openIdUser)
  230. if openIdUser != nil {
  231. if openIdUser.UserId != user.UserId {
  232. utils.FileLog.Info("user id is not eq %d , %d", openIdUser.UserId, user.UserId)
  233. wxUserId = user.UserId
  234. dsql := ` DELETE FROM wx_user WHERE open_id = ? `
  235. _, err = o.Raw(dsql, openId).Exec()
  236. if err != nil {
  237. return wxUserId, err
  238. }
  239. }
  240. }
  241. msql := ``
  242. ssql := ``
  243. if loginType == 1 {
  244. msql = ` UPDATE wx_user SET open_id = ?,bind_account = ?,created_time=NOW(),register_time=NOW() WHERE mobile = ? `
  245. ssql = `SELECT * FROM wx_user WHERE mobile = ? `
  246. } else {
  247. msql = ` UPDATE wx_user SET open_id = ?,bind_account = ?,created_time=NOW(),register_time=NOW() WHERE email = ? `
  248. ssql = `SELECT * FROM wx_user WHERE email = ? `
  249. }
  250. _, err = o.Raw(msql, openId, mobile, mobile).Exec()
  251. bindUser := new(WxUser)
  252. err = o.Raw(ssql, mobile).QueryRow(&bindUser)
  253. if bindUser != nil && bindUser.UserId > 0 {
  254. wxUserId = bindUser.UserId
  255. }
  256. } else {
  257. fmt.Println("line 239")
  258. wxUserId = userId
  259. }
  260. }
  261. if wxUserId <= 0 && (user != nil && err.Error() != utils.ErrNoRow()) {
  262. utils.FileLog.Info("wxUserId =0 %d , %d", user.UserId)
  263. wxUserId = user.UserId
  264. }
  265. return
  266. }
  267. func PcBindMobile(unionId, mobile string, userId, loginType int) (wxUserId int, err error) {
  268. //loginType 登录方式:1:手机,2:邮箱
  269. utils.FileLog.Info("绑定参数:%s %s %d %d", unionId, mobile, userId, loginType)
  270. sql := ``
  271. if loginType == 1 {
  272. sql = `SELECT * FROM wx_user WHERE mobile = ? `
  273. } else {
  274. sql = "SELECT * FROM wx_user WHERE email = ? "
  275. }
  276. user := new(WxUser)
  277. o := orm.NewOrm()
  278. err = o.Raw(sql, mobile).QueryRow(&user)
  279. if err != nil && err.Error() != utils.ErrNoRow() {
  280. return
  281. }
  282. if user == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
  283. utils.FileLog.Info("用户不存在,根据union_id绑定")
  284. msql := ``
  285. if loginType == 1 {
  286. msql = "UPDATE wx_user SET mobile = ?,bind_account = ? WHERE union_id = ? "
  287. } else {
  288. msql = "UPDATE wx_user SET email = ?,bind_account = ? WHERE union_id = ? "
  289. }
  290. _, err = o.Raw(msql, mobile, mobile, unionId).Exec()
  291. wxUserId = userId
  292. } else {
  293. utils.FileLog.Info("用户存在,user.UnionId:%s", user.UnionId)
  294. if user.UnionId == "" {
  295. sql = `SELECT * FROM wx_user WHERE union_id = ? `
  296. userInfo := new(WxUser)
  297. o := orm.NewOrm()
  298. err = o.Raw(sql, unionId).QueryRow(&userInfo)
  299. if err != nil {
  300. return
  301. }
  302. utils.FileLog.Info("user.RegisterTime %s", user.RegisterTime.Format(utils.FormatDateTime))
  303. utils.FileLog.Info("userInfo.RegisterTime %s", userInfo.RegisterTime.Format(utils.FormatDateTime))
  304. var maxRegisterTime time.Time
  305. if user.RegisterTime.Before(userInfo.RegisterTime) {
  306. maxRegisterTime = user.RegisterTime
  307. utils.FileLog.Info("after")
  308. } else {
  309. maxRegisterTime = userInfo.RegisterTime
  310. utils.FileLog.Info("not after")
  311. }
  312. utils.FileLog.Info("maxRegisterTime %s", maxRegisterTime.Format(utils.FormatDateTime))
  313. wxUserId = user.UserId
  314. dsql := ` DELETE FROM wx_user WHERE union_id = ? `
  315. _, err = o.Raw(dsql, unionId).Exec()
  316. if err != nil {
  317. return wxUserId, err
  318. }
  319. msql := ` UPDATE wx_user SET union_id=?,register_time=?,province=?,city=?,country=?,headimgurl=?,unionid=?,sex=? WHERE user_id = ? `
  320. _, err = o.Raw(msql, unionId, maxRegisterTime, userInfo.Province, userInfo.City, userInfo.Country, userInfo.Headimgurl, unionId, userInfo.Sex, user.UserId).Exec()
  321. wxUserId = user.UserId
  322. } else {
  323. sql = `SELECT * FROM wx_user WHERE user_id = ? `
  324. userInfo := new(WxUser)
  325. o := orm.NewOrm()
  326. err = o.Raw(sql, userId).QueryRow(&userInfo)
  327. if err != nil && err.Error() != utils.ErrNoRow() {
  328. return
  329. }
  330. dsql := ` DELETE FROM wx_user WHERE user_id = ? `
  331. _, err = o.Raw(dsql, userId).Exec()
  332. if err != nil {
  333. return user.UserId, err
  334. }
  335. if user.Mobile == "" && loginType == 1 {
  336. msql := ` UPDATE wx_user SET mobile = ?,bind_account = ? WHERE user_id = ?`
  337. _, err = o.Raw(msql, mobile, mobile, user.UserId).Exec()
  338. wxUserId = user.UserId
  339. }
  340. if user.Email == "" && loginType == 2 {
  341. msql := ` UPDATE wx_user SET email = ?,bind_account = ? WHERE user_id = ?`
  342. _, err = o.Raw(msql, mobile, mobile, user.UserId).Exec()
  343. wxUserId = user.UserId
  344. }
  345. utils.FileLog.Info("用户存在,bind:%s,%d,%s", unionId, wxUserId)
  346. wxUserId = userId
  347. }
  348. }
  349. return
  350. }
  351. func ModifyFirstLogin(userId int) (err error) {
  352. o := orm.NewOrm()
  353. sql := `UPDATE wx_user SET first_login=0 WHERE user_id = ? `
  354. _, err = o.Raw(sql, userId).Exec()
  355. return
  356. }
  357. func GetWxUserItemByEmail(email string) (item *WxUserItem, err error) {
  358. sql := `SELECT * FROM wx_user WHERE email=? `
  359. err = orm.NewOrm().Raw(sql, email).QueryRow(&item)
  360. return
  361. }
  362. func GetWxUserItemByMobile(mobile string) (item *WxUserItem, err error) {
  363. sql := `SELECT * FROM wx_user WHERE mobile=? OR mobile_two=? `
  364. err = orm.NewOrm().Raw(sql, mobile, mobile).QueryRow(&item)
  365. return
  366. }
  367. type PcLoginReq struct {
  368. LoginType int `description:"登录方式:1:手机,2:邮箱"`
  369. Mobile string `description:"手机号"`
  370. Email string `description:"邮箱"`
  371. SmsCode string `description:"短信/邮箱验证码"`
  372. IsFreeLogin bool `description:"是否免登陆,true:免登陆,false:非免登陆"`
  373. }
  374. type BindReq struct {
  375. BindType int `description:"绑定方式:1:手机,2:邮箱"`
  376. Mobile string `description:"手机号"`
  377. Email string `description:"邮箱"`
  378. AreaNum string `description:"国际区号"`
  379. VerifyCode string `description:"短信/邮箱 验证码"`
  380. }
  381. func ModifyLoginTime(userId int, isFreeLogin bool) (err error) {
  382. o := orm.NewOrm()
  383. sql := `UPDATE wx_user SET is_free_login=?,login_time=NOW() WHERE user_id = ? `
  384. _, err = o.Raw(sql, isFreeLogin, userId).Exec()
  385. return
  386. }
  387. func GetCustomPermission(companyId int) (count int, err error) {
  388. o := orm.NewOrm()
  389. sql := `SELECT COUNT(1) AS count FROM company AS a
  390. INNER JOIN company_product AS b ON a.company_id=b.company_id
  391. WHERE b.company_id=? AND b.status IN('试用','正式')
  392. GROUP BY b.company_id`
  393. err = o.Raw(sql, companyId).QueryRow(&count)
  394. return
  395. }
  396. type CheckLoginResp struct {
  397. IsTips bool `description:"true:需要提示,false:不需要提示"`
  398. IsBind bool `description:"true:需要绑定邮箱或验证码,false:不需要绑定邮箱或验证码"`
  399. }
  400. func GetOpenIdAll() (items []*WxUserItem, err error) {
  401. sql := ` SELECT * FROM wx_user WHERE open_id<>'' AND union_id IS NULL `
  402. o := orm.NewOrm()
  403. _, err = o.Raw(sql).QueryRows(&items)
  404. return
  405. }
  406. func ModifyWxUserUnionId(unionId string, userId int) (err error) {
  407. o := orm.NewOrm()
  408. sql := `UPDATE wx_user SET union_id=?,unionid=? WHERE user_id = ? `
  409. _, err = o.Raw(sql, unionId, unionId, userId).Exec()
  410. return
  411. }
  412. func GetWxUserAll() (items []*WxUserItem, err error) {
  413. sql := ` SELECT a.* FROM wx_user AS a
  414. INNER JOIN company_product AS b ON a.company_id=b.company_id
  415. WHERE b.status IN('正式','试用')
  416. AND b.product_id=1 `
  417. o := orm.NewOrm()
  418. _, err = o.Raw(sql).QueryRows(&items)
  419. return
  420. }
  421. //变更联系人是否已注册状态
  422. func ModifyWxUserRegisterStatus(userId, status, source int, registerTime time.Time) (err error) {
  423. o := orm.NewOrm()
  424. sql := `UPDATE wx_user SET is_register=?,source=?,register_time=? WHERE user_id = ? `
  425. _, err = o.Raw(sql, status, source, registerTime, userId).Exec()
  426. return
  427. }