wx_user.go 17 KB

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