admin_wechat.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "hongze/hongze_mobile_admin/models/tables/admin"
  6. "hongze/hongze_mobile_admin/utils"
  7. "strconv"
  8. "time"
  9. )
  10. //AdminWxLogin 内部员工公众号(弘则部门)微信登录
  11. func AdminWxLogin(wxAccessToken *WxAccessToken, wxUserInfo *WxUserInfo) (ret admin.WxLoginResp, err error, errMsg string) {
  12. errMsg = `微信登录失败`
  13. openId := wxAccessToken.Openid
  14. unionId := wxAccessToken.Unionid
  15. if unionId == "" {
  16. unionId = wxUserInfo.Unionid
  17. }
  18. //firstLogin==1,强制绑定手机号或者邮箱
  19. _, err = admin.GetAdminWxByAdminOpenId(openId)
  20. if err != nil {
  21. if err.Error() == utils.ErrNoRow() {
  22. //wxUser.FirstLogin = 1
  23. /*err = ERR_ADMIN_NOT_BIND
  24. errMsg = "用户没有绑定"
  25. return*/
  26. err = nil
  27. timeUnix := time.Now().Unix()
  28. timeUnixStr := strconv.FormatInt(timeUnix, 10)
  29. ret.BindToken = utils.MD5(openId) + utils.MD5(timeUnixStr)
  30. //把openid 相关信息保存到redis
  31. content := make(map[string]string, 2)
  32. content["open_id"] = openId
  33. content["union_id"] = unionId
  34. contentStr, _ := json.Marshal(content)
  35. err = utils.Rc.Put(utils.HZ_MOBILE_ADMIN_WXLOGIN_TOKEN+ret.BindToken, contentStr, 24*time.Hour)
  36. if err != nil {
  37. err = errors.New("保存登录信息到redis中失败 Err "+err.Error())
  38. errMsg = "保存登录信息失败"
  39. return
  40. }
  41. ret.BindFlag = false
  42. return
  43. }
  44. errMsg = "查询账号失败"
  45. return
  46. }
  47. ret.BindFlag = true
  48. return
  49. }