12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- /**
- * Copyright (c) 2022 Yansen Zhang
- * wxcomponent is licensed under Mulan PSL v2.
- * You can use this software according to the terms and conditions of the Mulan PSL v2.
- * You may obtain a copy of Mulan PSL v2 at:
- * http://license.coscl.org.cn/MulanPSL2
- * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
- * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
- * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
- * See the Mulan PSL v2 for more details.
- **/
-
- package wxcomponent
-
- import (
- "gitee.com/yansen_zh/wxcomponent/api/authorization"
- "gitee.com/yansen_zh/wxcomponent/config"
- "gitee.com/yansen_zh/wxcomponent/utils"
- )
-
- // 授权与Token
-
- // RefreshToken 获取令牌
- // 主要是定时任务调用
- func RefreshToken() error {
- result, err := authorization.GetComponentToken()
- if err != nil {
- return err
- }
-
- token := result.ComponentAccessToken
- expire := utils.GetExpireTime(result.ExpiresIn)
-
- if e := config.RefreshToken(token, expire); e != nil {
- return e
- }
-
- return nil
- }
-
- // CreateAuthLink 自建授权链接, client 默认是 PC 端, 如果是移动端, 需要传入 H5。
- // url 为需要跳转的结果页, 必须是原始的值, 不能经过 encode 处理.
- // appID 为小程序或者公众号ID, 可以不传
- func CreateAuthLink(client, url, appID string) (string, error) {
- preAuth, err := authorization.CreatePreAuthCode()
- if err != nil {
- return "", err
- }
-
- return authorization.GetAuthLink(client, preAuth.PreAuthCode, url, appID, 3)
- }
-
- // RefreshAuthorizerToken 刷新接口调用令牌
- // 主要是定时任务调用
- func RefreshAuthorizerToken(appID string) error {
- res, err := authorization.RefreshAuthorizerToken(appID, config.GetAuthorizer().GetRefreshToken(appID))
- if err != nil {
- return err
- }
-
- expire := utils.GetExpireTime(res.ExpiresIn)
-
- if err := config.GetAuthorizer().RefreshToken(appID, res.AuthorizerAccessToken, res.AuthorizerRefreshToken, expire); err != nil {
- return err
- }
-
- return nil
- }
-
- // GetAuthorizerInfo 获取授权帐号信息
- func GetAuthorizerInfo(appID string) (*authorization.AuthorizerInfoResult, error) {
- return authorization.GetAuthorizerInfo(appID)
- }
|