user.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. //用户绑定
  145. func BindWxUser(openid, mobile, email string, registerPlatform int) (wxUser *models.WxUserItem, 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. time.Sleep(time.Second * 5)
  188. user := &models.WxUser{
  189. CompanyId: 1,
  190. CreatedTime: time.Now(),
  191. FirstLogin: 1,
  192. Enabled: 1,
  193. RegisterPlatform: registerPlatform, //账号注册来源,注册平台,1:微信端,2:PC网页端
  194. RegisterTime: time.Now(),
  195. Mobile: mobile,
  196. Email: email,
  197. IsRegister: 1,
  198. Source: source,
  199. }
  200. tmpUserId, addUserErr := models.AddWxUser(user)
  201. //添加完成,清除缓存
  202. _ = utils.Rc.Delete(key)
  203. if addUserErr != nil {
  204. err = addUserErr
  205. return
  206. }
  207. user.UserId = int(tmpUserId)
  208. userId = int(tmpUserId)
  209. wxUser, err = models.GetWxUserItemByUserId(userId)
  210. } else {
  211. userId = wxUser.UserId
  212. }
  213. //如果存在该手机号/邮箱,那么需要校验
  214. if userRecord.UserId > 0 && userRecord.UserId != userId {
  215. err = errors.New(fmt.Sprint("用户已绑定其他账户,已绑定的用户编号:", userRecord.UserId, ",不允许重复绑定"))
  216. return
  217. }
  218. if userRecord.UserId == 0 {
  219. err = models.BindUserRecordByOpenid(userId, openid, bindAccount)
  220. if err != nil {
  221. return
  222. }
  223. userRecord.UserId = userId
  224. }
  225. //如果当前该第三方用户信息的昵称为空串的话,那么需要去查询该用户的第一个绑定信息的数据作为来源做数据修复
  226. if userRecord.NickName == "" {
  227. oldUserRecord, err := models.GetUserThirdRecordByUserId(userId)
  228. if err == nil && oldUserRecord != nil {
  229. //如果该用户绑定的第一条数据的头像信息不为空串,那么就去做新数据的修复
  230. if oldUserRecord.NickName != "" {
  231. _ = models.ModifyUserRecordInfo(userRecord.OpenId, oldUserRecord.NickName, oldUserRecord.Headimgurl, oldUserRecord.City, oldUserRecord.Province, oldUserRecord.Country, oldUserRecord.Sex, userId)
  232. }
  233. }
  234. }
  235. //如果该用户 绑定注册状态 字段处于 未注册 的情况下,那么去修改该数据
  236. if wxUser.IsRegister == 0 {
  237. err = models.ModifyWxUserRegisterStatus(userId, 1, source, time.Now())
  238. if err != nil {
  239. return
  240. }
  241. }
  242. //格式化用户数据
  243. formatWxUserAndUserRecord(wxUser, userRecord)
  244. return
  245. }
  246. //微信登录
  247. func WxLogin(wxPlatform int, code string, wxAccessToken *WxAccessToken, wxUserInfo *WxUserInfo) (token string, userId, firstLogin, permission int, err error) {
  248. openId := wxAccessToken.Openid
  249. unionId := wxAccessToken.Unionid
  250. if unionId == "" {
  251. unionId = wxUserInfo.Unionid
  252. }
  253. //firstLogin==1,强制绑定手机号或者邮箱
  254. firstLogin = 1
  255. QUERY_WX_USER:
  256. wxUser, wxUserErr := GetWxUserItemByOpenId(openId)
  257. if wxUserErr == ERR_NO_USER_RECORD { //没有用户openid记录
  258. _, recordErr := AddUserRecord(openId, unionId, wxUserInfo.Nickname, "", wxUserInfo.Province, wxUserInfo.City, wxUserInfo.Country, wxUserInfo.Headimgurl, "", wxPlatform, wxUserInfo.Sex, 0)
  259. //如果插入失败,那么直接将错误信息返回
  260. if recordErr != nil {
  261. err = recordErr
  262. return
  263. }
  264. //插入成功后,需要重新查询该用户,并进入下面的逻辑
  265. goto QUERY_WX_USER
  266. } else if wxUserErr == ERR_USER_NOT_BIND {
  267. //没有用户信息
  268. //wxUser.FirstLogin = 1
  269. } else if wxUserErr != nil {
  270. err = wxUserErr
  271. return
  272. }
  273. //如果已经登录注册绑定的情况下
  274. if wxUser != nil && wxUserErr == nil {
  275. //获取用户权限
  276. firstLogin = wxUser.FirstLogin
  277. userId = wxUser.UserId
  278. permission, permissionErr := CheckUserPermission(userId)
  279. if permissionErr != nil {
  280. //记录日志
  281. utils.FileLog.Info("userId:%s,err:%s", strconv.Itoa(userId), err)
  282. }
  283. if wxUserInfo != nil {
  284. go models.ModifyUserRecordInfo(openId, wxUserInfo.Nickname, wxUserInfo.Headimgurl, wxUserInfo.City, wxUserInfo.Province, wxUserInfo.Country, wxUserInfo.Sex, userId)
  285. }
  286. {
  287. codeLog := new(models.WxUserCode)
  288. codeLog.WxCode = code
  289. codeLog.UserId = userId
  290. codeLog.Code = 0
  291. codeLog.FirstLogin = firstLogin
  292. codeLog.Authorization = token
  293. codeLog.UserPermission = permission
  294. codeLog.CreateTime = time.Now()
  295. models.AddWxUserCode(codeLog)
  296. }
  297. if wxUser.Mobile == "" && wxUser.Email == "" {
  298. firstLogin = 1
  299. } else {
  300. firstLogin = 0
  301. }
  302. }
  303. //获取登录token
  304. tokenItem, tokenErr := models.GetTokenByOpenId(openId)
  305. if tokenErr != nil && tokenErr.Error() != utils.ErrNoRow() {
  306. err = errors.New("登录失败,获取token失败:" + tokenErr.Error())
  307. return
  308. }
  309. if tokenItem == nil || (tokenErr != nil && tokenErr.Error() == utils.ErrNoRow()) {
  310. timeUnix := time.Now().Unix()
  311. timeUnixStr := strconv.FormatInt(timeUnix, 10)
  312. token = utils.MD5(openId) + utils.MD5(timeUnixStr)
  313. //新增session
  314. {
  315. session := new(models.Session)
  316. session.OpenId = openId
  317. session.UserId = userId
  318. session.CreatedTime = time.Now()
  319. session.LastUpdatedTime = time.Now()
  320. session.ExpireTime = time.Now().AddDate(0, 3, 0)
  321. session.AccessToken = token
  322. sessionErr := models.AddSession(session)
  323. if err != nil {
  324. err = errors.New("登录失败,新增用户session信息失败:" + sessionErr.Error())
  325. return
  326. }
  327. }
  328. } else {
  329. token = tokenItem.AccessToken
  330. //如果联系人编号不为空,且联系人编号与session里面的联系人编号不一致的时候,需要做session变更
  331. if userId > 0 && tokenItem.UserId != userId {
  332. _ = models.UpdateSession(tokenItem.SessionId, userId, time.Now().AddDate(0, 1, 0))
  333. }
  334. }
  335. //新增登录日志
  336. {
  337. loginLog := new(models.WxUserLog)
  338. loginLog.UserId = userId
  339. loginLog.OpenId = openId
  340. loginLog.UnionId = unionId
  341. loginLog.CreateTime = time.Now()
  342. loginLog.Handle = "wechat_login"
  343. loginLog.Remark = token
  344. go models.AddWxUserLog(loginLog)
  345. }
  346. return
  347. }
  348. //添加第三方用户(微信)记录
  349. func AddUserRecord(openId, unionId, nickName, realName, province, city, country, headimgurl, sessionKey string, platform, sex, subscribe int) (userRecord *models.UserRecord, err error) {
  350. find, err := models.GetUserRecordByOpenId(openId)
  351. if err != nil && err.Error() != utils.ErrNoRow() {
  352. return
  353. }
  354. if find != nil {
  355. userRecord = find
  356. return
  357. }
  358. userRecord = &models.UserRecord{
  359. OpenId: openId, //用户open_id
  360. UnionId: unionId, //用户union_id
  361. Subscribe: subscribe,
  362. NickName: nickName, //用户昵称,最大长度:32
  363. RealName: realName, //用户实际名称,最大长度:32
  364. Sex: sex, //普通用户性别,1为男性,2为女性
  365. Province: province, //普通用户个人资料填写的省份,最大长度:30
  366. City: city, //普通用户个人资料填写的城市,最大长度:30
  367. Country: country, //国家,如中国为CN,最大长度:30
  368. Headimgurl: headimgurl, //用户第三方(微信)头像,最大长度:512
  369. CreateTime: time.Now(), //创建时间,关系添加时间、用户授权时间
  370. CreatePlatform: platform, //注册平台,1:日度点评公众号,2:管理后台,3:pc端网站,4:查研观向小程序;默认:1
  371. SessionKey: sessionKey, //微信小程序会话密钥,最大长度:255
  372. }
  373. recordId, err := models.AddUserRecord(userRecord)
  374. if err != nil {
  375. return
  376. }
  377. userRecord.UserRecordId = int(recordId)
  378. return
  379. }