广告平台(总站长使用)
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

136 satır
4.6 KiB

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