广告平台(总站长使用)
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

137 linhas
4.6 KiB

  1. package wechat
  2. import (
  3. "applet/app/cfg"
  4. md2 "applet/app/lib/wechat/md"
  5. "applet/app/md"
  6. "applet/app/utils"
  7. "applet/app/utils/cache"
  8. db "code.fnuoos.com/zhimeng/model.git/src"
  9. "code.fnuoos.com/zhimeng/model.git/src/super/implement"
  10. "encoding/json"
  11. "errors"
  12. "fmt"
  13. )
  14. type WxApiService struct {
  15. ComponentAppid string `json:"component_appid"`
  16. ComponentAppsecret string `json:"component_appsecret"`
  17. ComponentVerifyTicket string `json:"component_verify_ticket"`
  18. MasterId string `json:"master_id"`
  19. Host string `json:"host"`
  20. }
  21. func NewWxApiService(masterId, componentAppid, componentAppsecret string) (wxApiService WxApiService, err error) { // set方法
  22. wxApiService.MasterId = masterId
  23. wxApiService.ComponentAppid = componentAppid
  24. wxApiService.ComponentAppsecret = componentAppsecret
  25. wxApiService.Host = "http://super.advertisement.dengbiao.top"
  26. if cfg.Prd {
  27. wxApiService.Host = "http://www.baidu.com"
  28. }
  29. cacheKey := fmt.Sprintf(md.MasterComponentVerifyTicket, wxApiService.MasterId)
  30. cacheComponentVerifyTicket, _ := cache.GetString(cacheKey)
  31. if cacheComponentVerifyTicket == "" {
  32. return wxApiService, errors.New("微信验证票据ticket未获取到")
  33. }
  34. wxApiService.ComponentVerifyTicket = cacheComponentVerifyTicket
  35. return
  36. }
  37. // GetComponentAccessToken 获取接口调用令牌token
  38. func (wxApiService *WxApiService) GetComponentAccessToken() (cacheComponentAccessToken string, err error) { // set方法
  39. cacheKey := fmt.Sprintf(md.MasterComponentAccessToken, wxApiService.MasterId)
  40. cacheComponentAccessToken, _ = cache.GetString(cacheKey)
  41. if cacheComponentAccessToken == "" {
  42. url := "https://api.weixin.qq.com/cgi-bin/component/api_component_token"
  43. params := map[string]string{
  44. "component_appid": wxApiService.ComponentAppid,
  45. "component_appsecret": wxApiService.ComponentAppsecret,
  46. "component_verify_ticket": wxApiService.ComponentVerifyTicket,
  47. }
  48. postBody, err1 := utils.CurlPost(url, utils.SerializeStr(params), nil)
  49. if err1 != nil {
  50. return cacheComponentAccessToken, err1
  51. }
  52. var postData md2.GetComponentAccessToken
  53. err = json.Unmarshal(postBody, &postData)
  54. if err != nil {
  55. return
  56. }
  57. if postData.ComponentAccessToken == "" {
  58. err = errors.New("获取接口令牌token失败")
  59. return
  60. }
  61. cacheComponentAccessToken = postData.ComponentAccessToken
  62. cache.SetEx(cacheKey, cacheComponentAccessToken, postData.ExpiresIn-600)
  63. wxOpenThirdPartyAppListDb := implement.NewWxOpenThirdPartyAppListDb(db.Db)
  64. wxOpenThirdPartyAppList, err1 := wxOpenThirdPartyAppListDb.GetWxOpenThirdPartyAppListByAppId(wxApiService.ComponentAppid)
  65. if err1 != nil {
  66. return cacheComponentAccessToken, err1
  67. }
  68. if wxOpenThirdPartyAppList == nil {
  69. err = errors.New("未查询到对应App记录")
  70. return
  71. }
  72. wxOpenThirdPartyAppList.ComponentAccessToken = cacheComponentAccessToken
  73. _, err = wxOpenThirdPartyAppListDb.UpdateWxOpenThirdPartyAppList(wxOpenThirdPartyAppList, "component_access_token")
  74. if err != nil {
  75. return
  76. }
  77. }
  78. return
  79. }
  80. // GetPreAuthCode 获取预授权码
  81. func (wxApiService *WxApiService) GetPreAuthCode() (preAuthCode string, err error) { // set方法
  82. componentAccessToken, err := wxApiService.GetComponentAccessToken()
  83. if err != nil {
  84. return
  85. }
  86. url := "https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=" + componentAccessToken
  87. params := map[string]string{
  88. "component_appid": wxApiService.ComponentAppid,
  89. }
  90. postBody, err := utils.CurlPost(url, utils.SerializeStr(params), nil)
  91. if err != nil {
  92. return
  93. }
  94. var postData md2.GetPreAuthCode
  95. err = json.Unmarshal(postBody, &postData)
  96. if err != nil {
  97. return
  98. }
  99. if postData.PreAuthCode == "" {
  100. err = errors.New("获取预授权码失败")
  101. return
  102. }
  103. preAuthCode = postData.PreAuthCode
  104. return
  105. }
  106. // GetAuthorizerAccessTokenByAuthCode 使用授权码获取授权信息
  107. func (wxApiService *WxApiService) GetAuthorizerAccessTokenByAuthCode(authCode string) (resp md2.GetAuthorizerAccessTokenByAuthCode, err error) { // set方法
  108. componentAccessToken, err := wxApiService.GetComponentAccessToken()
  109. if err != nil {
  110. return
  111. }
  112. url := "https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=" + componentAccessToken
  113. params := map[string]string{
  114. "component_appid": wxApiService.ComponentAppid,
  115. "authorization_code": authCode,
  116. }
  117. postBody, err := utils.CurlPost(url, utils.SerializeStr(params), nil)
  118. if err != nil {
  119. return
  120. }
  121. err = json.Unmarshal(postBody, &resp)
  122. if err != nil {
  123. return
  124. }
  125. if resp.AuthorizationInfo.AuthorizerAppid == "" {
  126. err = errors.New("获取授权信息失败")
  127. return
  128. }
  129. return
  130. }