wechat.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package controllers
  2. import (
  3. "eta_gn/eta_report/models"
  4. "eta_gn/eta_report/services"
  5. "eta_gn/eta_report/services/wechat"
  6. "eta_gn/eta_report/utils"
  7. "fmt"
  8. )
  9. type WechatCommonController struct {
  10. BaseCommonController
  11. }
  12. // @router /open/getWxSign [get]
  13. func (this *WechatCommonController) GetWxSign() {
  14. br := new(models.BaseResponse).Init()
  15. defer func() {
  16. this.Data["json"] = br
  17. this.ServeJSON()
  18. }()
  19. conf, e := models.GetBusinessConf()
  20. if e != nil {
  21. br.Msg = "获取失败"
  22. br.ErrMsg = "获取配置信息失败, Err: " + e.Error()
  23. return
  24. }
  25. appId := ""
  26. appSecret := ""
  27. if v, ok := conf[models.BusinessConfWxAppId]; ok {
  28. appId = v
  29. }
  30. if v, ok := conf[models.BusinessConfWxAppSecret]; ok {
  31. appSecret = v
  32. }
  33. if appId == "" || appSecret == "" {
  34. br.Ret = models.BaseRespIgnoreTipsRet
  35. br.Msg = "微信公众号信息未配置"
  36. return
  37. }
  38. getUrl := this.GetString("Url")
  39. fmt.Println("getUrl:", getUrl)
  40. tReq := wechat.WxTokenReq{
  41. WxAppId: appId,
  42. WxAppSecret: appSecret,
  43. }
  44. var accessToken string
  45. if utils.BusinessCode == utils.BusinessCodeRelease {
  46. token, e, msg := wechat.GetAccessToken(tReq)
  47. if e != nil {
  48. br.Msg = "获取微信配置失败"
  49. br.ErrMsg = "获取access_token失败,err:" + msg
  50. return
  51. }
  52. accessToken = token
  53. } else {
  54. token, e, msg := wechat.GetWxAccessToken(tReq.WxAppId, tReq.WxAppSecret)
  55. if e != nil {
  56. br.Msg = "获取微信信息失败"
  57. br.ErrMsg = "获取access_token失败,err:" + msg
  58. return
  59. }
  60. accessToken = token
  61. }
  62. errCode, ticket, err := services.GetWxTicket(accessToken)
  63. if err != nil {
  64. if errCode == 40001 {
  65. if utils.BusinessCode == utils.BusinessCodeRelease {
  66. token, e, msg := wechat.GetAccessToken(tReq)
  67. if e != nil {
  68. br.Msg = "获取微信配置失败"
  69. br.ErrMsg = "获取access_token失败,err:" + msg
  70. return
  71. }
  72. accessToken = token
  73. } else {
  74. token, e, msg := wechat.GetWxAccessToken(tReq.WxAppId, tReq.WxAppSecret)
  75. if e != nil {
  76. br.Msg = "获取微信信息失败"
  77. br.ErrMsg = "获取access_token失败,err:" + msg
  78. return
  79. }
  80. accessToken = token
  81. }
  82. errCode, ticket, err = services.GetWxTicket(accessToken)
  83. if err != nil {
  84. br.Msg = "获取Ticket失败,请联系客服"
  85. br.ErrMsg = "获取Ticket失败,Err" + err.Error()
  86. return
  87. }
  88. }
  89. br.Msg = "获取Ticket失败,请联系客服"
  90. br.ErrMsg = "获取Ticket失败,Err" + err.Error()
  91. return
  92. }
  93. if ticket == "" {
  94. br.Msg = "获取Ticket失败,请联系客服"
  95. br.ErrMsg = "ticket为空" + ticket
  96. return
  97. }
  98. nonceStr := utils.GetRandStringNoSpecialChar(16)
  99. signature, nonceString, timestamp := services.GetWxSignature(ticket, getUrl, nonceStr)
  100. resp := new(models.WechatSign)
  101. resp.AppId = appId
  102. resp.NonceStr = nonceString
  103. resp.Timestamp = timestamp
  104. resp.Url = getUrl
  105. resp.Signature = signature
  106. br.Ret = 200
  107. br.Success = true
  108. br.Msg = "获取签名成功"
  109. br.Data = resp
  110. }