user.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. package services
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_api/models"
  6. "hongze/hongze_api/utils"
  7. "strconv"
  8. "time"
  9. )
  10. var ERR_NO_USER_RECORD = errors.New("用户关系没有入库")
  11. var ERR_USER_NOT_BIND = errors.New("用户没有绑定")
  12. //通过openid获取用户信息
  13. func GetWxUserItemByOpenId(openid string) (item *models.WxUserItem, err error) {
  14. //通过openid获取用户关联信息
  15. userRecord, userRecordErr := models.GetUserRecordByOpenId(openid)
  16. if userRecordErr != nil {
  17. if userRecordErr.Error() == utils.ErrNoRow() {
  18. err = ERR_NO_USER_RECORD
  19. return
  20. } else {
  21. err = userRecordErr
  22. return
  23. }
  24. }
  25. //该openid在系统中没有关联关系
  26. if userRecord == nil {
  27. err = ERR_NO_USER_RECORD
  28. return
  29. }
  30. //该openid没有绑定用户
  31. if userRecord.UserId <= 0 {
  32. err = ERR_USER_NOT_BIND
  33. item = new(models.WxUserItem)
  34. //格式化返回用户数据
  35. formatWxUserAndUserRecord(item, userRecord)
  36. return
  37. }
  38. //获取用户信息
  39. item, wxUserErr := models.GetWxUserItemByUserId(userRecord.UserId)
  40. if wxUserErr != nil {
  41. err = wxUserErr
  42. //如果是找不到数据,那么可能是该用户被删除了,但是user_record没有删除对应的关系
  43. if wxUserErr.Error() == utils.ErrNoRow() {
  44. //用户被删除了,但是user_record没有删除对应的关系,那么去解除绑定
  45. userUnbindErr := models.UnBindUserRecordByOpenid(openid)
  46. if userUnbindErr != nil {
  47. err = userUnbindErr
  48. return
  49. }
  50. //返回状态为 用户未绑定 逻辑代码
  51. err = ERR_USER_NOT_BIND
  52. item = new(models.WxUserItem)
  53. //格式化返回用户数据
  54. formatWxUserAndUserRecord(item, userRecord)
  55. return
  56. }
  57. return
  58. }
  59. //格式化返回用户数据
  60. formatWxUserAndUserRecord(item, userRecord)
  61. return
  62. }
  63. //根据用户id和平台id获取用户信息
  64. func GetWxUserItemByUserId(userId, platform int) (wxUserItem *models.WxUserItem, err error) {
  65. //获取用户信息
  66. wxUserItem, wxUserErr := models.GetWxUserItemByUserId(userId)
  67. if wxUserErr != nil {
  68. err = wxUserErr
  69. return
  70. }
  71. //格式化返回用户数据
  72. formatWxUser(wxUserItem, platform)
  73. return
  74. }
  75. //根据用户邮箱和平台id获取用户信息
  76. func GetWxUserItemByEmail(email string, platform int) (wxUserItem *models.WxUserItem, err error) {
  77. //获取用户信息
  78. wxUserItem, wxUserErr := models.GetWxUserItemByEmail(email)
  79. if wxUserErr != nil {
  80. err = wxUserErr
  81. return
  82. }
  83. //格式化返回用户数据
  84. formatWxUser(wxUserItem, platform)
  85. return
  86. }
  87. //根据用户手机号和平台id获取用户信息
  88. func GetWxUserItemByMobile(mobile string, platform int) (wxUserItem *models.WxUserItem, err error) {
  89. //获取用户信息
  90. wxUserItem, wxUserErr := models.GetWxUserItemByMobile(mobile)
  91. if wxUserErr != nil {
  92. err = wxUserErr
  93. return
  94. }
  95. //格式化返回用户数据
  96. formatWxUser(wxUserItem, platform)
  97. return
  98. }
  99. //根据用户unionid和平台id获取用户信息
  100. func GetWxUserItemByUnionId(unionId string, platform int) (wxUserItem *models.WxUserItem, err error) {
  101. //获取用户信息
  102. wxUserItem, wxUserErr := models.GetWxUserItemByUnionid(unionId)
  103. if wxUserErr != nil {
  104. err = wxUserErr
  105. return
  106. }
  107. //格式化返回用户数据
  108. formatWxUser(wxUserItem, platform)
  109. return
  110. }
  111. //通过用户 关系表记录 和 用户记录 格式化返回 用户数据
  112. func formatWxUserAndUserRecord(wxUser *models.WxUserItem, userRecord *models.UserRecord) {
  113. wxUser.OpenId = userRecord.OpenId
  114. wxUser.UnionId = userRecord.UnionId
  115. wxUser.NickName = userRecord.NickName
  116. //wxUser.RealName = userRecord.RealName
  117. //wxUser.BindAccount = userRecord.BindAccount
  118. wxUser.Headimgurl = userRecord.Headimgurl
  119. }
  120. //通过用户 用户记录 和 来源平台 格式化返回 用户数据
  121. func formatWxUser(wxUser *models.WxUserItem, platform int) {
  122. //根据用户id和平台id获取用户关系
  123. userRecord, userRecordErr := models.GetUserRecordByUserId(wxUser.UserId, platform)
  124. if userRecordErr != nil {
  125. if userRecordErr.Error() != utils.ErrNoRow() {
  126. return
  127. }
  128. if userRecordErr.Error() == utils.ErrNoRow() {
  129. return
  130. }
  131. }
  132. //该openid在系统中没有关联关系
  133. if userRecord == nil {
  134. return
  135. }
  136. wxUser.OpenId = userRecord.OpenId
  137. wxUser.UnionId = userRecord.UnionId
  138. wxUser.NickName = userRecord.NickName
  139. //wxUser.RealName = userRecord.RealName
  140. //wxUser.BindAccount = userRecord.BindAccount
  141. wxUser.Headimgurl = userRecord.Headimgurl
  142. return
  143. }
  144. // BindWxUser 用户绑定
  145. func BindWxUser(openid, mobile, email string, areaNum, registerPlatform int) (wxUser *models.WxUserItem, errMsg string, err error) {
  146. source := 1 //绑定来源,1:微信端,2:pc网页端,3:查研观向小程序,4:每日咨询
  147. if mobile == "" && email == "" {
  148. err = errors.New("手机号或邮箱必填一个")
  149. return
  150. }
  151. var bindAccount string
  152. //根据手机号获取用户信息
  153. if mobile != "" {
  154. tmpWxUser, wxUserErr := models.GetWxUserItemByMobile(mobile)
  155. if wxUserErr != nil && wxUserErr.Error() != utils.ErrNoRow() {
  156. err = wxUserErr
  157. return
  158. }
  159. wxUser = tmpWxUser
  160. bindAccount = mobile
  161. }
  162. //根据邮箱获取用户信息
  163. if wxUser == nil && email != "" {
  164. tmpWxUser, wxUserErr := models.GetWxUserItemByEmail(email)
  165. if wxUserErr != nil && wxUserErr.Error() != utils.ErrNoRow() {
  166. err = wxUserErr
  167. return
  168. }
  169. wxUser = tmpWxUser
  170. bindAccount = email
  171. }
  172. //查询openid的第三方(微信)信息
  173. userRecord, err := models.GetUserRecordByOpenId(openid)
  174. if err != nil {
  175. return
  176. }
  177. var userId int
  178. //如果查询出来的用户是nil,那么需要新增用户
  179. if wxUser == nil {
  180. key := "bind_wx_user:mobile:" + mobile + ":email:" + email
  181. isHas := utils.Rc.IsExist(key)
  182. if isHas {
  183. err = errors.New("多次提交,请关闭页面重新进入")
  184. return
  185. }
  186. utils.Rc.SetNX(key, "ok", time.Second*300)
  187. user := &models.WxUser{
  188. CompanyId: 1,
  189. CreatedTime: time.Now(),
  190. FirstLogin: 1,
  191. Enabled: 1,
  192. RegisterPlatform: registerPlatform, //账号注册来源,注册平台,1:微信端,2:PC网页端
  193. RegisterTime: time.Now(),
  194. Mobile: mobile,
  195. Email: email,
  196. IsRegister: 1,
  197. Source: source,
  198. CountryCode: strconv.Itoa(areaNum),
  199. OutboundMobile: mobile,
  200. OutboundCountryCode: strconv.Itoa(areaNum),
  201. }
  202. tmpUserId, addUserErr := models.AddWxUser(user)
  203. //添加完成,清除缓存
  204. _ = utils.Rc.Delete(key)
  205. if addUserErr != nil {
  206. err = addUserErr
  207. return
  208. }
  209. user.UserId = int(tmpUserId)
  210. userId = int(tmpUserId)
  211. wxUser, err = models.GetWxUserItemByUserId(userId)
  212. } else {
  213. userId = wxUser.UserId
  214. }
  215. //如果存在该手机号/邮箱,那么需要校验
  216. if userRecord.UserId > 0 && userRecord.UserId != userId {
  217. err = errors.New(fmt.Sprint("用户已绑定其他账户,已绑定的用户编号:", userRecord.UserId, ",不允许重复绑定"))
  218. currUser, _ := models.GetWxUserItemByUserId(userRecord.UserId)
  219. if currUser != nil {
  220. currBindAccount := currUser.Mobile
  221. if currBindAccount == `` {
  222. currBindAccount = currUser.Email
  223. }
  224. errMsg = "微信已绑定其它账户:" + currBindAccount
  225. }
  226. return
  227. }
  228. if userRecord.UserId == 0 {
  229. err = models.BindUserRecordByOpenid(userId, openid, bindAccount)
  230. if err != nil {
  231. return
  232. }
  233. userRecord.UserId = userId
  234. }
  235. //如果当前该第三方用户信息的昵称为空串的话,那么需要去查询该用户的第一个绑定信息的数据作为来源做数据修复
  236. if userRecord.NickName == "" {
  237. oldUserRecord, err := models.GetUserThirdRecordByUserId(userId)
  238. if err == nil && oldUserRecord != nil {
  239. //如果该用户绑定的第一条数据的头像信息不为空串,那么就去做新数据的修复
  240. if oldUserRecord.NickName != "" {
  241. _ = models.ModifyUserRecordInfo(userRecord.OpenId, oldUserRecord.NickName, oldUserRecord.Headimgurl, oldUserRecord.City, oldUserRecord.Province, oldUserRecord.Country, oldUserRecord.Sex, userId)
  242. }
  243. }
  244. }
  245. //如果该用户 绑定注册状态 字段处于 未注册 的情况下,那么去修改该数据
  246. if wxUser.IsRegister == 0 {
  247. err = models.ModifyWxUserRegisterStatus(userId, 1, source, time.Now())
  248. if err != nil {
  249. return
  250. }
  251. }
  252. //格式化用户数据
  253. formatWxUserAndUserRecord(wxUser, userRecord)
  254. return
  255. }
  256. //微信登录
  257. func WxLogin(wxPlatform int, code string, wxAccessToken *WxAccessToken, wxUserInfo *WxUserInfo) (token string, userId, firstLogin, permission int, err error) {
  258. openId := wxAccessToken.Openid
  259. unionId := wxAccessToken.Unionid
  260. if unionId == "" {
  261. unionId = wxUserInfo.Unionid
  262. }
  263. //firstLogin==1,强制绑定手机号或者邮箱
  264. firstLogin = 1
  265. QUERY_WX_USER:
  266. wxUser, wxUserErr := GetWxUserItemByOpenId(openId)
  267. if wxUserErr == ERR_NO_USER_RECORD { //没有用户openid记录
  268. _, recordErr := AddUserRecord(openId, unionId, wxUserInfo.Nickname, "", wxUserInfo.Province, wxUserInfo.City, wxUserInfo.Country, wxUserInfo.Headimgurl, "", wxPlatform, wxUserInfo.Sex, 0)
  269. //如果插入失败,那么直接将错误信息返回
  270. if recordErr != nil {
  271. err = recordErr
  272. return
  273. }
  274. //插入成功后,需要重新查询该用户,并进入下面的逻辑
  275. goto QUERY_WX_USER
  276. } else if wxUserErr == ERR_USER_NOT_BIND {
  277. //没有用户信息
  278. //wxUser.FirstLogin = 1
  279. } else if wxUserErr != nil {
  280. err = wxUserErr
  281. return
  282. }
  283. //如果已经登录注册绑定的情况下
  284. if wxUser != nil && wxUserErr == nil {
  285. //获取用户权限
  286. firstLogin = wxUser.FirstLogin
  287. userId = wxUser.UserId
  288. permission, permissionErr := CheckUserPermission(userId)
  289. if permissionErr != nil {
  290. //记录日志
  291. utils.FileLog.Info("userId:%s,err:%s", strconv.Itoa(userId), err)
  292. }
  293. if wxUserInfo != nil {
  294. go models.ModifyUserRecordInfo(openId, wxUserInfo.Nickname, wxUserInfo.Headimgurl, wxUserInfo.City, wxUserInfo.Province, wxUserInfo.Country, wxUserInfo.Sex, userId)
  295. }
  296. {
  297. codeLog := new(models.WxUserCode)
  298. codeLog.WxCode = code
  299. codeLog.UserId = userId
  300. codeLog.Code = 0
  301. codeLog.FirstLogin = firstLogin
  302. codeLog.Authorization = token
  303. codeLog.UserPermission = permission
  304. codeLog.CreateTime = time.Now()
  305. models.AddWxUserCode(codeLog)
  306. }
  307. if wxUser.Mobile == "" && wxUser.Email == "" {
  308. firstLogin = 1
  309. } else {
  310. firstLogin = 0
  311. }
  312. }
  313. //获取登录token
  314. tokenItem, tokenErr := models.GetTokenByOpenId(openId)
  315. if tokenErr != nil && tokenErr.Error() != utils.ErrNoRow() {
  316. err = errors.New("登录失败,获取token失败:" + tokenErr.Error())
  317. return
  318. }
  319. if tokenItem == nil || (tokenErr != nil && tokenErr.Error() == utils.ErrNoRow()) {
  320. timeUnix := time.Now().Unix()
  321. timeUnixStr := strconv.FormatInt(timeUnix, 10)
  322. token = utils.MD5(openId) + utils.MD5(timeUnixStr)
  323. //新增session
  324. {
  325. session := new(models.Session)
  326. session.OpenId = openId
  327. session.UserId = userId
  328. session.CreatedTime = time.Now()
  329. session.LastUpdatedTime = time.Now()
  330. session.ExpireTime = time.Now().AddDate(0, 3, 0)
  331. session.AccessToken = token
  332. sessionErr := models.AddSession(session)
  333. if err != nil {
  334. err = errors.New("登录失败,新增用户session信息失败:" + sessionErr.Error())
  335. return
  336. }
  337. }
  338. } else {
  339. token = tokenItem.AccessToken
  340. //如果联系人编号不为空,且联系人编号与session里面的联系人编号不一致的时候,需要做session变更
  341. if userId > 0 && tokenItem.UserId != userId {
  342. _ = models.UpdateSession(tokenItem.SessionId, userId, time.Now().AddDate(0, 1, 0))
  343. }
  344. }
  345. //新增登录日志
  346. {
  347. loginLog := new(models.WxUserLog)
  348. loginLog.UserId = userId
  349. loginLog.OpenId = openId
  350. loginLog.UnionId = unionId
  351. loginLog.CreateTime = time.Now()
  352. loginLog.Handle = "wechat_login"
  353. loginLog.Remark = token
  354. go models.AddWxUserLog(loginLog)
  355. }
  356. return
  357. }
  358. //添加第三方用户(微信)记录
  359. func AddUserRecord(openId, unionId, nickName, realName, province, city, country, headimgurl, sessionKey string, platform, sex, subscribe int) (userRecord *models.UserRecord, err error) {
  360. find, err := models.GetUserRecordByOpenId(openId)
  361. if err != nil && err.Error() != utils.ErrNoRow() {
  362. return
  363. }
  364. if find != nil {
  365. userRecord = find
  366. return
  367. }
  368. userRecord = &models.UserRecord{
  369. OpenId: openId, //用户open_id
  370. UnionId: unionId, //用户union_id
  371. Subscribe: subscribe,
  372. NickName: nickName, //用户昵称,最大长度:32
  373. RealName: realName, //用户实际名称,最大长度:32
  374. Sex: sex, //普通用户性别,1为男性,2为女性
  375. Province: province, //普通用户个人资料填写的省份,最大长度:30
  376. City: city, //普通用户个人资料填写的城市,最大长度:30
  377. Country: country, //国家,如中国为CN,最大长度:30
  378. Headimgurl: headimgurl, //用户第三方(微信)头像,最大长度:512
  379. CreateTime: time.Now(), //创建时间,关系添加时间、用户授权时间
  380. CreatePlatform: platform, //注册平台,1:日度点评公众号,2:管理后台,3:pc端网站,4:查研观向小程序;默认:1
  381. SessionKey: sessionKey, //微信小程序会话密钥,最大长度:255
  382. }
  383. recordId, err := models.AddUserRecord(userRecord)
  384. if err != nil {
  385. return
  386. }
  387. userRecord.UserRecordId = int(recordId)
  388. return
  389. }