wechat.go 3.2 KB

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