component_access_token.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package context
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "time"
  6. "github.com/silenceper/wechat/util"
  7. )
  8. const (
  9. componentAccessTokenURL = "https://api.weixin.qq.com/cgi-bin/component/api_component_token"
  10. getPreCodeURL = "https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=%s"
  11. queryAuthURL = "https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=%s"
  12. refreshTokenURL = "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=%s"
  13. getComponentInfoURL = "https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token=%s"
  14. getComponentConfigURL = "https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_option?component_access_token=%s"
  15. )
  16. // ComponentAccessToken 第三方平台
  17. type ComponentAccessToken struct {
  18. AccessToken string `json:"component_access_token"`
  19. ExpiresIn int64 `json:"expires_in"`
  20. }
  21. // GetComponentAccessToken 获取 ComponentAccessToken
  22. func (ctx *Context) GetComponentAccessToken() (string, error) {
  23. accessTokenCacheKey := fmt.Sprintf("component_access_token_%s", ctx.AppID)
  24. val := ctx.Cache.Get(accessTokenCacheKey)
  25. if val == nil {
  26. return "", fmt.Errorf("cann't get component access token")
  27. }
  28. return val.(string), nil
  29. }
  30. // SetComponentAccessToken 通过component_verify_ticket 获取 ComponentAccessToken
  31. func (ctx *Context) SetComponentAccessToken(verifyTicket string) (*ComponentAccessToken, error) {
  32. body := map[string]string{
  33. "component_appid": ctx.AppID,
  34. "component_appsecret": ctx.AppSecret,
  35. "component_verify_ticket": verifyTicket,
  36. }
  37. respBody, err := util.PostJSON(componentAccessTokenURL, body)
  38. if err != nil {
  39. return nil, err
  40. }
  41. at := &ComponentAccessToken{}
  42. if err := json.Unmarshal(respBody, at); err != nil {
  43. return nil, err
  44. }
  45. accessTokenCacheKey := fmt.Sprintf("component_access_token_%s", ctx.AppID)
  46. expires := at.ExpiresIn - 1500
  47. ctx.Cache.Set(accessTokenCacheKey, at.AccessToken, time.Duration(expires)*time.Second)
  48. return at, nil
  49. }
  50. // GetPreCode 获取预授权码
  51. func (ctx *Context) GetPreCode() (string, error) {
  52. cat, err := ctx.GetComponentAccessToken()
  53. if err != nil {
  54. return "", err
  55. }
  56. req := map[string]string{
  57. "component_appid": ctx.AppID,
  58. }
  59. uri := fmt.Sprintf(getPreCodeURL, cat)
  60. body, err := util.PostJSON(uri, req)
  61. if err != nil {
  62. return "", err
  63. }
  64. var ret struct {
  65. PreCode string `json:"pre_auth_code"`
  66. }
  67. if err := json.Unmarshal(body, &ret); err != nil {
  68. return "", err
  69. }
  70. return ret.PreCode, nil
  71. }
  72. // ID 微信返回接口中各种类型字段
  73. type ID struct {
  74. ID int `json:"id"`
  75. }
  76. // AuthBaseInfo 授权的基本信息
  77. type AuthBaseInfo struct {
  78. AuthrAccessToken
  79. FuncInfo []AuthFuncInfo `json:"func_info"`
  80. }
  81. // AuthFuncInfo 授权的接口内容
  82. type AuthFuncInfo struct {
  83. FuncscopeCategory ID `json:"funcscope_category"`
  84. }
  85. // AuthrAccessToken 授权方AccessToken
  86. type AuthrAccessToken struct {
  87. Appid string `json:"authorizer_appid"`
  88. AccessToken string `json:"authorizer_access_token"`
  89. ExpiresIn int64 `json:"expires_in"`
  90. RefreshToken string `json:"authorizer_refresh_token"`
  91. }
  92. // QueryAuthCode 使用授权码换取公众号或小程序的接口调用凭据和授权信息
  93. func (ctx *Context) QueryAuthCode(authCode string) (*AuthBaseInfo, error) {
  94. cat, err := ctx.GetComponentAccessToken()
  95. if err != nil {
  96. return nil, err
  97. }
  98. req := map[string]string{
  99. "component_appid": ctx.AppID,
  100. "authorization_code": authCode,
  101. }
  102. uri := fmt.Sprintf(queryAuthURL, cat)
  103. body, err := util.PostJSON(uri, req)
  104. if err != nil {
  105. return nil, err
  106. }
  107. var ret struct {
  108. Info *AuthBaseInfo `json:"authorization_info"`
  109. }
  110. if err := json.Unmarshal(body, &ret); err != nil {
  111. return nil, err
  112. }
  113. return ret.Info, nil
  114. }
  115. // RefreshAuthrToken 获取(刷新)授权公众号或小程序的接口调用凭据(令牌)
  116. func (ctx *Context) RefreshAuthrToken(appid, refreshToken string) (*AuthrAccessToken, error) {
  117. cat, err := ctx.GetComponentAccessToken()
  118. if err != nil {
  119. return nil, err
  120. }
  121. req := map[string]string{
  122. "component_appid": ctx.AppID,
  123. "authorizer_appid": appid,
  124. "authorizer_refresh_token": refreshToken,
  125. }
  126. uri := fmt.Sprintf(refreshTokenURL, cat)
  127. body, err := util.PostJSON(uri, req)
  128. if err != nil {
  129. return nil, err
  130. }
  131. ret := &AuthrAccessToken{}
  132. if err := json.Unmarshal(body, ret); err != nil {
  133. return nil, err
  134. }
  135. authrTokenKey := "authorizer_access_token_" + appid
  136. ctx.Cache.Set(authrTokenKey, ret.AccessToken, time.Minute*80)
  137. return ret, nil
  138. }
  139. // GetAuthrAccessToken 获取授权方AccessToken
  140. func (ctx *Context) GetAuthrAccessToken(appid string) (string, error) {
  141. authrTokenKey := "authorizer_access_token_" + appid
  142. val := ctx.Cache.Get(authrTokenKey)
  143. if val == nil {
  144. return "", fmt.Errorf("cannot get authorizer %s access token", appid)
  145. }
  146. return val.(string), nil
  147. }
  148. // AuthorizerInfo 授权方详细信息
  149. type AuthorizerInfo struct {
  150. NickName string `json:"nick_name"`
  151. HeadImg string `json:"head_img"`
  152. ServiceTypeInfo ID `json:"service_type_info"`
  153. VerifyTypeInfo ID `json:"verify_type_info"`
  154. UserName string `json:"user_name"`
  155. PrincipalName string `json:"principal_name"`
  156. BusinessInfo struct {
  157. OpenStore string `json:"open_store"`
  158. OpenScan string `json:"open_scan"`
  159. OpenPay string `json:"open_pay"`
  160. OpenCard string `json:"open_card"`
  161. OpenShake string `json:"open_shake"`
  162. }
  163. Alias string `json:"alias"`
  164. QrcodeURL string `json:"qrcode_url"`
  165. }
  166. // GetAuthrInfo 获取授权方的帐号基本信息
  167. func (ctx *Context) GetAuthrInfo(appid string) (*AuthorizerInfo, *AuthBaseInfo, error) {
  168. cat, err := ctx.GetComponentAccessToken()
  169. if err != nil {
  170. return nil, nil, err
  171. }
  172. req := map[string]string{
  173. "component_appid": ctx.AppID,
  174. "authorizer_appid": appid,
  175. }
  176. uri := fmt.Sprintf(getComponentInfoURL, cat)
  177. body, err := util.PostJSON(uri, req)
  178. if err != nil {
  179. return nil, nil, err
  180. }
  181. var ret struct {
  182. AuthorizerInfo *AuthorizerInfo `json:"authorizer_info"`
  183. AuthorizationInfo *AuthBaseInfo `json:"authorization_info"`
  184. }
  185. if err := json.Unmarshal(body, &ret); err != nil {
  186. return nil, nil, err
  187. }
  188. return ret.AuthorizerInfo, ret.AuthorizationInfo, nil
  189. }