wechat_pc.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/hongze_api/models"
  6. "hongze/hongze_api/services"
  7. "hongze/hongze_api/utils"
  8. "strconv"
  9. "time"
  10. )
  11. // @Title pc端微信登录接口
  12. // @Description pc端微信登录接口
  13. // @Param Code query string true "微信唯一编码code"
  14. // @Success 200 {object} models.WxLoginResp
  15. // @router /pc/login [get]
  16. func (this *WechatCommonController) PcWechatLogin() {
  17. br := new(models.BaseResponse).Init()
  18. defer func() {
  19. this.Data["json"] = br
  20. this.ServeJSON()
  21. }()
  22. code := this.GetString("Code")
  23. fmt.Println("code:", code)
  24. item, err := services.PcWxGetUserOpenIdByCode(code)
  25. if err != nil {
  26. br.Msg = "获取用户信息失败"
  27. br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
  28. return
  29. }
  30. if item.Errcode != 0 {
  31. br.Msg = "获取用户信息失败"
  32. br.ErrMsg = "获取access_token 失败 errcode:" + strconv.Itoa(item.Errcode) + " ;errmsg:" + item.Errmsg
  33. return
  34. }
  35. openId := item.Openid
  36. //accessToken, err := services.PcWxGetAccessToken()
  37. //utils.FileLog.Info("accessToken:%s", accessToken)
  38. //if err != nil {
  39. // br.Msg = "获取用户信息失败"
  40. // br.ErrMsg = "获取access_token失败,err:" + err.Error()
  41. // return
  42. //}
  43. utils.FileLog.Info("item.AccessToken):%s", item.AccessToken)
  44. //获取用户信息
  45. wxPcUserInfo, err := services.PcWxGetUserInfo(openId, item.AccessToken)
  46. if err != nil {
  47. br.Msg = "获取用户信息失败"
  48. br.ErrMsg = "获取微信用户信息失败,Err:" + err.Error()
  49. return
  50. }
  51. if wxPcUserInfo.Errcode != 0 {
  52. userInfoJson, _ := json.Marshal(wxPcUserInfo)
  53. br.Msg = "登录失败"
  54. br.ErrMsg = "获取用户信息失败,err:" + string(userInfoJson)
  55. return
  56. }
  57. if err != nil{
  58. br.Msg = "微信登录失败"
  59. br.ErrMsg = "微信登录失败,err:" + err.Error()
  60. return
  61. }
  62. wxUserInfo := &services.WxUserInfo{
  63. Openid :wxPcUserInfo.Openid,
  64. Nickname :wxPcUserInfo.Nickname,
  65. Sex :wxPcUserInfo.Sex,
  66. Language :wxPcUserInfo.Language,
  67. City :wxPcUserInfo.City,
  68. Province :wxPcUserInfo.Province,
  69. Country :wxPcUserInfo.Country,
  70. Headimgurl :wxPcUserInfo.Headimgurl,
  71. //SubscribeTime :0,
  72. Unionid :wxPcUserInfo.Unionid,
  73. //Remark :wxPcUserInfo.Province,
  74. //Groupid :wxPcUserInfo.Groupid,
  75. //SubscribeScene string `json:"subscribe_scene"`
  76. //Errcode int `json:"errcode"`
  77. //Errmsg string `json:"errmsg"`
  78. }
  79. token,userId,firstLogin,permission,err := services.WxLogin(utils.WxPcPlatform,code,item,wxUserInfo)
  80. newUser, _ := services.GetWxUserItemByOpenId(wxUserInfo.Openid)
  81. utils.FileLog.Info("获取用户信息:%d", userId)
  82. var email,headimgurl,mobile,companyName, status, endDate, productName string
  83. //用户信息赋值
  84. if newUser != nil && userId > 0{
  85. email = newUser.Email
  86. headimgurl = newUser.Headimgurl
  87. mobile = newUser.Mobile
  88. cp, err := models.GetCompanyProductsByUserId(userId)
  89. if err != nil {
  90. br.Msg = "登录失败"
  91. br.ErrMsg = "登录失败,获取客户信息失败:" + err.Error()
  92. return
  93. }
  94. for _, v := range cp {
  95. companyName = v.CompanyName
  96. if v.EndDate == "0000-00-00" {
  97. v.EndDate = ""
  98. }
  99. if status != "" {
  100. status = status + "/" + v.Status
  101. if v.EndDate != "" {
  102. endDate = endDate + "/" + v.Status + ":" + v.EndDate
  103. }
  104. productName = productName + "/" + v.ProductName
  105. } else {
  106. status = v.Status
  107. if v.EndDate != "" {
  108. endDate = v.Status + ":" + v.EndDate
  109. }
  110. productName = v.ProductName
  111. }
  112. }
  113. }
  114. //新增登录日志
  115. {
  116. loginLog := new(models.WxUserLog)
  117. loginLog.UserId = userId
  118. loginLog.OpenId = openId
  119. loginLog.CreateTime = time.Now()
  120. loginLog.Handle="wechat_pc_login"
  121. loginLog.Remark=token
  122. go models.AddWxUserLog(loginLog)
  123. }
  124. resp := new(models.WxLoginResp)
  125. resp.UserId = userId
  126. resp.Code = 0
  127. resp.FirstLogin = firstLogin
  128. resp.Authorization = token
  129. resp.UserPermission = permission
  130. resp.Email = email
  131. resp.Headimgurl = headimgurl
  132. resp.Mobile = mobile
  133. resp.CompanyName = companyName
  134. resp.EndDate = endDate
  135. resp.Status = status
  136. resp.ProductName = productName
  137. br.Ret = 200
  138. br.Success = true
  139. br.Msg = "登录成功"
  140. br.Data = resp
  141. }
  142. //func init() {
  143. // accessToken:=`40_6_YtSyR4Kg9MYbODrNVc1mpztpbZDPWb8aiNzgq96vfvSLGb59dTZk6dprQuhCdsPrqYRKFvaAAij0_NzR-rnMCb0Bxsngov9eLBuk4WQoI`
  144. // openId:=`oNUl652NjgjPz_C4M1ezCIro_s98`
  145. // item,err:=services.WxGetUserInfo(openId, accessToken)
  146. // fmt.Println(err)
  147. // fmt.Println(item)
  148. //}
  149. //func init() {
  150. // fmt.Println("start accessToken")
  151. // accessToken, err := services.PcWxGetAccessToken()
  152. // fmt.Println(err)
  153. // utils.FileLog.Info("accessToken:%s",accessToken)
  154. // fmt.Println("end accessToken")
  155. //}