wechat.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. )
  10. type WechatController struct {
  11. BaseAuthController
  12. }
  13. type WechatCommonController struct {
  14. BaseCommonController
  15. }
  16. // @Title 微信登录接口
  17. // @Description 微信登录接口
  18. // @Param Code query string true "微信唯一编码code"
  19. // @Success 200 {object} models.WxLoginResp
  20. // @router /login [get]
  21. func (this *WechatCommonController) WechatLogin() {
  22. br := new(models.BaseResponse).Init()
  23. defer func() {
  24. this.Data["json"] = br
  25. this.ServeJSON()
  26. }()
  27. resp := new(models.WxLoginResp)
  28. code := this.GetString("Code")
  29. fmt.Println("code:", code)
  30. utils.FileLog.Info("WechatLogin code:%s", code)
  31. wxCodeInfo, err := models.GetWxUserCode(code)
  32. if err == nil && wxCodeInfo != nil && wxCodeInfo.Id > 0 {
  33. utils.FileLog.Info("WechatLogin code exist:%s", code)
  34. resp.UserId = wxCodeInfo.UserId
  35. resp.Code = 0
  36. resp.FirstLogin = wxCodeInfo.FirstLogin
  37. resp.Authorization = wxCodeInfo.Authorization
  38. resp.UserPermission = wxCodeInfo.UserPermission
  39. br.Ret = 200
  40. br.Success = true
  41. br.Msg = "登录成功"
  42. br.Data = resp
  43. return
  44. }
  45. item, err := services.WxGetUserOpenIdByCode(code)
  46. if err != nil {
  47. br.Msg = "获取用户信息失败"
  48. br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
  49. return
  50. }
  51. if item.Errcode != 0 {
  52. br.Msg = "获取用户信息失败"
  53. br.ErrMsg = "获取access_token 失败 errcode:" + strconv.Itoa(item.Errcode) + " ;errmsg:" + item.Errmsg
  54. return
  55. }
  56. openId := item.Openid
  57. if openId == "" {
  58. br.Msg = "获取用户信息失败"
  59. br.ErrMsg = "获取openid失败,openid:" + item.Openid
  60. return
  61. }
  62. accessToken, err := services.WxGetAccessToken()
  63. if err != nil {
  64. br.Msg = "获取用户信息失败"
  65. br.ErrMsg = "获取access_token失败,err:" + err.Error()
  66. return
  67. }
  68. //获取用户信息
  69. wxUserInfo, err := services.WxGetUserInfo(openId, accessToken)
  70. if err != nil {
  71. br.Msg = "获取用户信息失败"
  72. br.ErrMsg = "获取微信用户信息失败,Err:" + err.Error()
  73. return
  74. }
  75. if wxUserInfo.Errcode != 0 {
  76. userInfoJson, _ := json.Marshal(wxUserInfo)
  77. br.Msg = "登录失败"
  78. br.ErrMsg = "获取用户信息失败,err:" + string(userInfoJson)
  79. return
  80. }
  81. token, userId, firstLogin, permission, err := services.WxLogin(utils.WxPlatform, code, item, wxUserInfo)
  82. if err != nil {
  83. br.Msg = "微信登录失败"
  84. br.ErrMsg = "微信登录失败,err:" + err.Error()
  85. return
  86. }
  87. resp.UserId = userId
  88. resp.Code = 0
  89. resp.FirstLogin = firstLogin
  90. resp.Authorization = token
  91. resp.UserPermission = permission
  92. br.Ret = 200
  93. br.Success = true
  94. br.Msg = "登录成功"
  95. br.Data = resp
  96. //登录日志
  97. {
  98. returnResult, err := json.Marshal(br)
  99. if err != nil {
  100. utils.FileLog.Info(this.Ctx.Input.URI() + " Err:%s" + err.Error())
  101. }
  102. utils.FileLog.Info(this.Ctx.Input.URI()+" code: %s , return data: %s", code, string(returnResult))
  103. }
  104. }
  105. //作废于2021-03-29 10:14:54
  106. //func (this *WechatCommonController) WechatLoginV1() {
  107. // br := new(models.BaseResponse).Init()
  108. // defer func() {
  109. // this.Data["json"] = br
  110. // this.ServeJSON()
  111. // }()
  112. // resp := new(models.WxLoginResp)
  113. //
  114. // code := this.GetString("Code")
  115. // fmt.Println("code:", code)
  116. // utils.FileLog.Info("WechatLogin code:%s", code)
  117. // wxCodeInfo, err := models.GetWxUserCode(code)
  118. // if err == nil && wxCodeInfo != nil && wxCodeInfo.Id > 0 {
  119. // utils.FileLog.Info("WechatLogin code exist:%s", code)
  120. // resp.UserId = wxCodeInfo.UserId
  121. // resp.Code = 0
  122. // resp.FirstLogin = wxCodeInfo.FirstLogin
  123. // resp.Authorization = wxCodeInfo.Authorization
  124. // resp.UserPermission = wxCodeInfo.UserPermission
  125. // br.Ret = 200
  126. // br.Success = true
  127. // br.Msg = "登录成功"
  128. // br.Data = resp
  129. // return
  130. // }
  131. //
  132. // item, err := services.WxGetUserOpenIdByCode(code)
  133. // if err != nil {
  134. // br.Msg = "获取用户信息失败"
  135. // br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
  136. // return
  137. // }
  138. // if item.Errcode != 0 {
  139. // br.Msg = "获取用户信息失败"
  140. // br.ErrMsg = "获取access_token 失败 errcode:" + strconv.Itoa(item.Errcode) + " ;errmsg:" + item.Errmsg
  141. // return
  142. // }
  143. // openId := item.Openid
  144. // accessToken, err := services.WxGetAccessToken()
  145. // if err != nil {
  146. // br.Msg = "获取用户信息失败"
  147. // br.ErrMsg = "获取access_token失败,err:" + err.Error()
  148. // return
  149. // }
  150. // //获取用户信息
  151. // wxUserInfo, err := services.WxGetUserInfo(openId, accessToken)
  152. // if err != nil {
  153. // br.Msg = "获取用户信息失败"
  154. // br.ErrMsg = "获取微信用户信息失败,Err:" + err.Error()
  155. // return
  156. // }
  157. // if wxUserInfo.Errcode != 0 {
  158. // userInfoJson, _ := json.Marshal(wxUserInfo)
  159. // br.Msg = "登录失败"
  160. // br.ErrMsg = "获取用户信息失败,err:" + string(userInfoJson)
  161. // return
  162. // }
  163. //
  164. // unionid := item.Unionid
  165. // if unionid == "" {
  166. // unionid = wxUserInfo.Unionid
  167. // }
  168. // firstLogin := 1
  169. // userId := 0
  170. // utils.FileLog.Info("openId:%s", openId)
  171. // utils.FileLog.Info("unionid:%s", unionid)
  172. // //获取成功
  173. // if openId != "" {
  174. // wxUser, err := models.GetWxUserItemByOpenId(openId)
  175. // if err != nil && err.Error() != utils.ErrNoRow() {
  176. // br.Msg = "获取用户信息失败"
  177. // br.ErrMsg = "根据openid获取用户信息失败,Eerr:" + err.Error()
  178. // return
  179. // }
  180. // if wxUser == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
  181. // user := new(models.WxUser)
  182. // user.OpenId = openId
  183. // user.CompanyId = 1
  184. // user.CreatedTime = time.Now()
  185. // user.UnionId = unionid
  186. // user.Unionid = unionid
  187. // user.NickName = wxUserInfo.Nickname
  188. // user.Sex = wxUserInfo.Sex
  189. // user.City = wxUserInfo.City
  190. // user.Province = wxUserInfo.Province
  191. // user.Country = wxUserInfo.Country
  192. // user.Headimgurl = wxUserInfo.Headimgurl
  193. // user.FirstLogin = 1
  194. // user.Enabled = 1
  195. // user.RegisterPlatform = 1
  196. // user.RegisterTime = time.Now()
  197. // _, err = models.AddWxUser(user)
  198. // wxUser, err = models.GetWxUserItemByOpenId(openId)
  199. // if err != nil {
  200. // br.Msg = "获取用户信息失败"
  201. // br.ErrMsg = "unionid登录,获取微信用户信息失败,Err:" + err.Error()
  202. // return
  203. // }
  204. // userId = wxUser.UserId
  205. // } else {
  206. // firstLogin = wxUser.FirstLogin
  207. // userId = wxUser.UserId
  208. // }
  209. // } else {
  210. // br.Msg = "获取用户信息失败"
  211. // br.ErrMsg = "获取openid失败,openid:" + item.Openid
  212. // return
  213. // }
  214. // permission, err := services.CheckUserPermission(userId)
  215. // if err != nil {
  216. // utils.FileLog.Info("userId:%s,err:%s", strconv.Itoa(userId), err)
  217. // }
  218. // //if err != nil {
  219. // // br.Msg = "登录失败"
  220. // // br.ErrMsg = "登录失败,判断权限失败:" + err.Error()
  221. // // return
  222. // //}
  223. // var token string
  224. // tokenItem, err := models.GetTokenByUid(userId)
  225. // if err != nil && err.Error() != utils.ErrNoRow() {
  226. // br.Msg = "登录失败"
  227. // br.ErrMsg = "登录失败,获取token失败:" + err.Error()
  228. // return
  229. // }
  230. //
  231. // if tokenItem == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
  232. // timeUnix := time.Now().Unix()
  233. // timeUnixStr := strconv.FormatInt(timeUnix, 10)
  234. // token = utils.MD5(strconv.Itoa(userId)) + utils.MD5(timeUnixStr)
  235. // //新增session
  236. // {
  237. // session := new(models.Session)
  238. // session.OpenId = openId
  239. // session.UserId = userId
  240. // session.CreatedTime = time.Now()
  241. // session.LastUpdatedTime = time.Now()
  242. // session.ExpireTime = time.Now().AddDate(0, 3, 0)
  243. // session.AccessToken = token
  244. // err = models.AddSession(session)
  245. // if err != nil {
  246. // br.Msg = "登录失败"
  247. // br.ErrMsg = "登录失败,新增用户session信息失败:" + err.Error()
  248. // return
  249. // }
  250. // }
  251. // } else {
  252. // token = tokenItem.AccessToken
  253. // }
  254. //
  255. // if wxUserInfo != nil {
  256. // go models.ModifyWxUserInfo(wxUserInfo.Nickname, wxUserInfo.Headimgurl, wxUserInfo.City, wxUserInfo.Province, wxUserInfo.Country, wxUserInfo.Sex, userId)
  257. // }
  258. // //firstLogin==1,强制绑定手机号或者邮箱
  259. // {
  260. // newItem, _ := models.GetWxUserItemByUserId(userId)
  261. // if newItem.Mobile == "" && newItem.Email == "" {
  262. // firstLogin = 1
  263. // }
  264. // }
  265. // //新增登录日志
  266. // {
  267. // loginLog := new(models.WxUserLog)
  268. // loginLog.UserId = userId
  269. // loginLog.OpenId = openId
  270. // loginLog.UnionId = unionid
  271. // loginLog.CreateTime = time.Now()
  272. // loginLog.Handle = "wechat_login"
  273. // loginLog.Remark = token
  274. // go models.AddWxUserLog(loginLog)
  275. // }
  276. //
  277. // {
  278. // codeLog := new(models.WxUserCode)
  279. // codeLog.WxCode = code
  280. // codeLog.UserId = userId
  281. // codeLog.Code = 0
  282. // codeLog.FirstLogin = firstLogin
  283. // codeLog.Authorization = token
  284. // codeLog.UserPermission = permission
  285. // codeLog.CreateTime=time.Now()
  286. // models.AddWxUserCode(codeLog)
  287. // }
  288. //
  289. // resp.UserId = userId
  290. // resp.Code = 0
  291. // resp.FirstLogin = firstLogin
  292. // resp.Authorization = token
  293. // resp.UserPermission = permission
  294. // br.Ret = 200
  295. // br.Success = true
  296. // br.Msg = "登录成功"
  297. // br.Data = resp
  298. // //登录日志
  299. // {
  300. // returnResult, err := json.Marshal(br)
  301. // if err != nil {
  302. // utils.FileLog.Info(this.Ctx.Input.URI() + " Err:%s" + err.Error())
  303. // }
  304. // utils.FileLog.Info(this.Ctx.Input.URI()+" code: %s , return data: %s", code, string(returnResult))
  305. // }
  306. //}
  307. // @Title 微信获取签名接口
  308. // @Description 微信获取签名接口
  309. // @Param Url query string true "url地址"
  310. // @Success 200 {object} models.WechatSign
  311. // @router /getWxSign [get]
  312. func (this *WechatController) GetWxSign() {
  313. br := new(models.BaseResponse).Init()
  314. defer func() {
  315. this.Data["json"] = br
  316. this.ServeJSON()
  317. }()
  318. getUrl := this.GetString("Url")
  319. fmt.Println("getUrl:", getUrl)
  320. accessToken, err := services.WxGetAccessToken()
  321. if err != nil {
  322. br.Msg = "获取用户信息失败"
  323. br.ErrMsg = "获取access_token失败,err:" + err.Error()
  324. return
  325. }
  326. ticket, err := services.GetWxTicket(accessToken)
  327. if err != nil {
  328. br.Msg = "获取Ticket失败,请联系客服"
  329. br.ErrMsg = "获取Ticket失败,Err" + err.Error()
  330. return
  331. }
  332. if ticket == "" {
  333. br.Msg = "获取Ticket失败,请联系客服"
  334. br.ErrMsg = "ticket为空" + ticket
  335. return
  336. }
  337. nonceStr := utils.GetRandStringNoSpecialChar(16)
  338. signature, nonceString, timestamp := services.GetWxSignature(ticket, getUrl, nonceStr)
  339. resp := new(models.WechatSign)
  340. resp.AppId = utils.WxAppId
  341. resp.NonceStr = nonceString
  342. resp.Timestamp = timestamp
  343. resp.Url = getUrl
  344. resp.Signature = signature
  345. br.Ret = 200
  346. br.Success = true
  347. br.Msg = "获取签名成功"
  348. br.Data = resp
  349. }
  350. // @Title 微信获取签名接口
  351. // @Description 微信获取签名接口
  352. // @Param Url query string true "url地址"
  353. // @Success 200 {object} models.WechatSign
  354. // @router /notify [get]
  355. func (this *WechatCommonController) Notify() {
  356. signature := this.GetString("signature")
  357. timestamp := this.GetString("timestamp")
  358. nonce := this.GetString("nonce")
  359. echostr := this.GetString("echostr")
  360. fmt.Println("signature:", signature)
  361. fmt.Println("timestamp:", timestamp)
  362. fmt.Println("nonce:", nonce)
  363. body := this.Ctx.Input.RequestBody
  364. paramsMap := this.Ctx.Input.Params()
  365. fmt.Println("paramsMap")
  366. fmt.Println(paramsMap)
  367. paramsJson, err := json.Marshal(paramsMap)
  368. if err != nil {
  369. fmt.Println("json.Marshal Err:" + err.Error())
  370. }
  371. utils.FileLog.Info("notify result:" + string(body))
  372. utils.FileLog.Info("notify paramsJson result:" + string(paramsJson))
  373. this.Ctx.WriteString(echostr)
  374. }