wechat.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. type WechatController struct {
  12. BaseAuthController
  13. }
  14. type WechatCommonController struct {
  15. BaseCommonController
  16. }
  17. // @Title 微信登录接口
  18. // @Description 微信登录接口
  19. // @Param Code query string true "微信唯一编码code"
  20. // @Success 200 {object} models.WxLoginResp
  21. // @router /login [get]
  22. func (this *WechatCommonController) WechatLogin() {
  23. br := new(models.BaseResponse).Init()
  24. defer func() {
  25. this.Data["json"] = br
  26. this.ServeJSON()
  27. }()
  28. resp := new(models.WxLoginResp)
  29. code := this.GetString("Code")
  30. fmt.Println("code:", code)
  31. utils.FileLog.Info("WechatLogin code:%s", code)
  32. wxCodeInfo, err := models.GetWxUserCode(code)
  33. if err == nil && wxCodeInfo != nil && wxCodeInfo.Id > 0 {
  34. utils.FileLog.Info("WechatLogin code exist:%s", code)
  35. resp.UserId = wxCodeInfo.UserId
  36. resp.Code = 0
  37. resp.FirstLogin = wxCodeInfo.FirstLogin
  38. resp.Authorization = wxCodeInfo.Authorization
  39. resp.UserPermission = wxCodeInfo.UserPermission
  40. br.Ret = 200
  41. br.Success = true
  42. br.Msg = "登录成功"
  43. br.Data = resp
  44. return
  45. }
  46. item, err := services.WxGetUserOpenIdByCode(code)
  47. if err != nil {
  48. br.Msg = "获取用户信息失败"
  49. br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
  50. return
  51. }
  52. if item.Errcode != 0 {
  53. br.Msg = "获取用户信息失败"
  54. br.ErrMsg = "获取access_token 失败 errcode:" + strconv.Itoa(item.Errcode) + " ;errmsg:" + item.Errmsg
  55. return
  56. }
  57. openId := item.Openid
  58. accessToken, err := services.WxGetAccessToken()
  59. if err != nil {
  60. br.Msg = "获取用户信息失败"
  61. br.ErrMsg = "获取access_token失败,err:" + err.Error()
  62. return
  63. }
  64. //获取用户信息
  65. wxUserInfo, err := services.WxGetUserInfo(openId, accessToken)
  66. if err != nil {
  67. br.Msg = "获取用户信息失败"
  68. br.ErrMsg = "获取微信用户信息失败,Err:" + err.Error()
  69. return
  70. }
  71. if wxUserInfo.Errcode != 0 {
  72. userInfoJson, _ := json.Marshal(wxUserInfo)
  73. br.Msg = "登录失败"
  74. br.ErrMsg = "获取用户信息失败,err:" + string(userInfoJson)
  75. return
  76. }
  77. unionid := item.Unionid
  78. if unionid == "" {
  79. unionid = wxUserInfo.Unionid
  80. }
  81. firstLogin := 1
  82. userId := 0
  83. utils.FileLog.Info("openId:%s", openId)
  84. utils.FileLog.Info("unionid:%s", unionid)
  85. //获取成功
  86. if openId != "" {
  87. wxUser, err := models.GetWxUserItemByOpenId(openId)
  88. if err != nil && err.Error() != utils.ErrNoRow() {
  89. br.Msg = "获取用户信息失败"
  90. br.ErrMsg = "根据openid获取用户信息失败,Eerr:" + err.Error()
  91. return
  92. }
  93. if wxUser == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
  94. user := new(models.WxUser)
  95. user.OpenId = openId
  96. user.CompanyId = 1
  97. user.CreatedTime = time.Now()
  98. user.UnionId = unionid
  99. user.Unionid = unionid
  100. user.NickName = wxUserInfo.Nickname
  101. user.Sex = wxUserInfo.Sex
  102. user.City = wxUserInfo.City
  103. user.Province = wxUserInfo.Province
  104. user.Country = wxUserInfo.Country
  105. user.Headimgurl = wxUserInfo.Headimgurl
  106. user.FirstLogin = 1
  107. user.Enabled = 1
  108. user.RegisterPlatform = 1
  109. user.RegisterTime = time.Now()
  110. _, err = models.AddWxUser(user)
  111. wxUser, err = models.GetWxUserItemByOpenId(openId)
  112. if err != nil {
  113. br.Msg = "获取用户信息失败"
  114. br.ErrMsg = "unionid登录,获取微信用户信息失败,Err:" + err.Error()
  115. return
  116. }
  117. userId = wxUser.UserId
  118. } else {
  119. firstLogin = wxUser.FirstLogin
  120. userId = wxUser.UserId
  121. }
  122. } else {
  123. br.Msg = "获取用户信息失败"
  124. br.ErrMsg = "获取openid失败,openid:" + item.Openid
  125. return
  126. }
  127. permission, err := services.CheckUserPermission(userId)
  128. if err != nil {
  129. utils.FileLog.Info("userId:%s,err:%s", strconv.Itoa(userId), err)
  130. }
  131. //if err != nil {
  132. // br.Msg = "登录失败"
  133. // br.ErrMsg = "登录失败,判断权限失败:" + err.Error()
  134. // return
  135. //}
  136. var token string
  137. tokenItem, err := models.GetTokenByUid(userId)
  138. if err != nil && err.Error() != utils.ErrNoRow() {
  139. br.Msg = "登录失败"
  140. br.ErrMsg = "登录失败,获取token失败:" + err.Error()
  141. return
  142. }
  143. if tokenItem == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
  144. timeUnix := time.Now().Unix()
  145. timeUnixStr := strconv.FormatInt(timeUnix, 10)
  146. token = utils.MD5(strconv.Itoa(userId)) + utils.MD5(timeUnixStr)
  147. //新增session
  148. {
  149. session := new(models.Session)
  150. session.OpenId = openId
  151. session.UserId = userId
  152. session.CreatedTime = time.Now()
  153. session.LastUpdatedTime = time.Now()
  154. session.ExpireTime = time.Now().AddDate(0, 3, 0)
  155. session.AccessToken = token
  156. err = models.AddSession(session)
  157. if err != nil {
  158. br.Msg = "登录失败"
  159. br.ErrMsg = "登录失败,新增用户session信息失败:" + err.Error()
  160. return
  161. }
  162. }
  163. } else {
  164. token = tokenItem.AccessToken
  165. }
  166. if wxUserInfo != nil {
  167. go models.ModifyWxUserInfo(wxUserInfo.Nickname, wxUserInfo.Headimgurl, wxUserInfo.City, wxUserInfo.Province, wxUserInfo.Country, wxUserInfo.Sex, userId)
  168. }
  169. //firstLogin==1,强制绑定手机号或者邮箱
  170. {
  171. newItem, _ := models.GetWxUserItemByUserId(userId)
  172. if newItem.Mobile == "" && newItem.Email == "" {
  173. firstLogin = 1
  174. }
  175. }
  176. //新增登录日志
  177. {
  178. loginLog := new(models.WxUserLog)
  179. loginLog.UserId = userId
  180. loginLog.OpenId = openId
  181. loginLog.UnionId = unionid
  182. loginLog.CreateTime = time.Now()
  183. loginLog.Handle = "wechat_login"
  184. loginLog.Remark = token
  185. go models.AddWxUserLog(loginLog)
  186. }
  187. {
  188. codeLog := new(models.WxUserCode)
  189. codeLog.WxCode = code
  190. codeLog.UserId = userId
  191. codeLog.Code = 0
  192. codeLog.FirstLogin = firstLogin
  193. codeLog.Authorization = token
  194. codeLog.UserPermission = permission
  195. codeLog.CreateTime=time.Now()
  196. models.AddWxUserCode(codeLog)
  197. }
  198. resp.UserId = userId
  199. resp.Code = 0
  200. resp.FirstLogin = firstLogin
  201. resp.Authorization = token
  202. resp.UserPermission = permission
  203. br.Ret = 200
  204. br.Success = true
  205. br.Msg = "登录成功"
  206. br.Data = resp
  207. //登录日志
  208. {
  209. returnResult, err := json.Marshal(br)
  210. if err != nil {
  211. utils.FileLog.Info(this.Ctx.Input.URI() + " Err:%s" + err.Error())
  212. }
  213. utils.FileLog.Info(this.Ctx.Input.URI()+" code: %s , return data: %s", code, string(returnResult))
  214. }
  215. }
  216. // @Title 微信获取签名接口
  217. // @Description 微信获取签名接口
  218. // @Param Url query string true "url地址"
  219. // @Success 200 {object} models.WechatSign
  220. // @router /getWxSign [get]
  221. func (this *WechatController) GetWxSign() {
  222. br := new(models.BaseResponse).Init()
  223. defer func() {
  224. this.Data["json"] = br
  225. this.ServeJSON()
  226. }()
  227. getUrl := this.GetString("Url")
  228. fmt.Println("getUrl:", getUrl)
  229. accessToken, err := services.WxGetAccessToken()
  230. if err != nil {
  231. br.Msg = "获取用户信息失败"
  232. br.ErrMsg = "获取access_token失败,err:" + err.Error()
  233. return
  234. }
  235. ticket, err := services.GetWxTicket(accessToken)
  236. if err != nil {
  237. br.Msg = "获取Ticket失败,请联系客服"
  238. br.ErrMsg = "获取Ticket失败,Err" + err.Error()
  239. return
  240. }
  241. if ticket == "" {
  242. br.Msg = "获取Ticket失败,请联系客服"
  243. br.ErrMsg = "ticket为空" + ticket
  244. return
  245. }
  246. nonceStr := utils.GetRandStringNoSpecialChar(16)
  247. signature, nonceString, timestamp := services.GetWxSignature(ticket, getUrl, nonceStr)
  248. resp := new(models.WechatSign)
  249. resp.AppId = utils.WxAppId
  250. resp.NonceStr = nonceString
  251. resp.Timestamp = timestamp
  252. resp.Url = getUrl
  253. resp.Signature = signature
  254. br.Ret = 200
  255. br.Success = true
  256. br.Msg = "获取签名成功"
  257. br.Data = resp
  258. }