wx_user.go 16 KB

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