소스 검색

feats: change error define

Your Name 3 년 전
부모
커밋
ad2817c009

+ 2
- 3
api/authorization/authorizer_info.go 파일 보기

14
 
14
 
15
 import (
15
 import (
16
 	"encoding/json"
16
 	"encoding/json"
17
-	"errors"
18
 	"net/url"
17
 	"net/url"
19
 
18
 
20
 	"gitee.com/yansen_zh/wxcomponent/config"
19
 	"gitee.com/yansen_zh/wxcomponent/config"
21
-	wxerr "gitee.com/yansen_zh/wxcomponent/errors"
20
+	"gitee.com/yansen_zh/wxcomponent/errors"
22
 	"gitee.com/yansen_zh/wxcomponent/utils/request"
21
 	"gitee.com/yansen_zh/wxcomponent/utils/request"
23
 )
22
 )
24
 
23
 
25
 // AuthorizerInfoResult 获取授权帐号信息结果
24
 // AuthorizerInfoResult 获取授权帐号信息结果
26
 type AuthorizerInfoResult struct {
25
 type AuthorizerInfoResult struct {
27
-	wxerr.Error
26
+	errors.Error
28
 	AuthorizationInfo AuthorizationInfo `json:"authorization_info"`
27
 	AuthorizationInfo AuthorizationInfo `json:"authorization_info"`
29
 	AuthorizerInfo    AuthorizerInfo    `json:"authorizer_info"`
28
 	AuthorizerInfo    AuthorizerInfo    `json:"authorizer_info"`
30
 }
29
 }

+ 2
- 3
api/authorization/authorizer_token.go 파일 보기

14
 
14
 
15
 import (
15
 import (
16
 	"encoding/json"
16
 	"encoding/json"
17
-	"errors"
18
 	"net/url"
17
 	"net/url"
19
 
18
 
20
 	"gitee.com/yansen_zh/wxcomponent/config"
19
 	"gitee.com/yansen_zh/wxcomponent/config"
21
-	wxerr "gitee.com/yansen_zh/wxcomponent/errors"
20
+	"gitee.com/yansen_zh/wxcomponent/errors"
22
 	"gitee.com/yansen_zh/wxcomponent/utils/request"
21
 	"gitee.com/yansen_zh/wxcomponent/utils/request"
23
 )
22
 )
24
 
23
 
25
 // AuthorizerTokenResult 获取/刷新接口调用令牌结果
24
 // AuthorizerTokenResult 获取/刷新接口调用令牌结果
26
 type AuthorizerTokenResult struct {
25
 type AuthorizerTokenResult struct {
27
-	wxerr.Error
26
+	errors.Error
28
 	AuthorizerAccessToken  string `json:"authorizer_access_token"`
27
 	AuthorizerAccessToken  string `json:"authorizer_access_token"`
29
 	ExpiresIn              int    `json:"expires_in"`
28
 	ExpiresIn              int    `json:"expires_in"`
30
 	AuthorizerRefreshToken string `json:"authorizer_refresh_token"`
29
 	AuthorizerRefreshToken string `json:"authorizer_refresh_token"`

+ 2
- 2
api/authorization/component_token.go 파일 보기

16
 	"encoding/json"
16
 	"encoding/json"
17
 
17
 
18
 	"gitee.com/yansen_zh/wxcomponent/config"
18
 	"gitee.com/yansen_zh/wxcomponent/config"
19
-	wxerr "gitee.com/yansen_zh/wxcomponent/errors"
19
+	"gitee.com/yansen_zh/wxcomponent/errors"
20
 	"gitee.com/yansen_zh/wxcomponent/utils/request"
20
 	"gitee.com/yansen_zh/wxcomponent/utils/request"
21
 )
21
 )
22
 
22
 
23
 // ComponentTokenResult 获取令牌结果
23
 // ComponentTokenResult 获取令牌结果
24
 type ComponentTokenResult struct {
24
 type ComponentTokenResult struct {
25
-	wxerr.Error
25
+	errors.Error
26
 	ComponentAccessToken string `json:"component_access_token"`
26
 	ComponentAccessToken string `json:"component_access_token"`
27
 	ExpiresIn            int    `json:"expires_in"`
27
 	ExpiresIn            int    `json:"expires_in"`
28
 }
28
 }

+ 2
- 2
api/authorization/pre_auth_code.go 파일 보기

17
 	"net/url"
17
 	"net/url"
18
 
18
 
19
 	"gitee.com/yansen_zh/wxcomponent/config"
19
 	"gitee.com/yansen_zh/wxcomponent/config"
20
-	wxerr "gitee.com/yansen_zh/wxcomponent/errors"
20
+	"gitee.com/yansen_zh/wxcomponent/errors"
21
 	"gitee.com/yansen_zh/wxcomponent/utils/request"
21
 	"gitee.com/yansen_zh/wxcomponent/utils/request"
22
 )
22
 )
23
 
23
 
24
 // PreAuthCodeResult 获取预授权码结果
24
 // PreAuthCodeResult 获取预授权码结果
25
 type PreAuthCodeResult struct {
25
 type PreAuthCodeResult struct {
26
-	wxerr.Error
26
+	errors.Error
27
 	PreAuthCode string `json:"pre_auth_code"`
27
 	PreAuthCode string `json:"pre_auth_code"`
28
 	ExpiresIn   int    `json:"expires_in"`
28
 	ExpiresIn   int    `json:"expires_in"`
29
 }
29
 }

+ 2
- 3
api/authorization/query_auth.go 파일 보기

14
 
14
 
15
 import (
15
 import (
16
 	"encoding/json"
16
 	"encoding/json"
17
-	"errors"
18
 	"net/url"
17
 	"net/url"
19
 
18
 
20
 	"gitee.com/yansen_zh/wxcomponent/config"
19
 	"gitee.com/yansen_zh/wxcomponent/config"
21
-	wxerr "gitee.com/yansen_zh/wxcomponent/errors"
20
+	"gitee.com/yansen_zh/wxcomponent/errors"
22
 	"gitee.com/yansen_zh/wxcomponent/utils/request"
21
 	"gitee.com/yansen_zh/wxcomponent/utils/request"
23
 )
22
 )
24
 
23
 
25
 // QueryAuthResult 获取授权信息结果
24
 // QueryAuthResult 获取授权信息结果
26
 type QueryAuthResult struct {
25
 type QueryAuthResult struct {
27
-	wxerr.Error
26
+	errors.Error
28
 	AuthorizationInfo AuthorizationInfo `json:"authorization_info"`
27
 	AuthorizationInfo AuthorizationInfo `json:"authorization_info"`
29
 }
28
 }
30
 
29
 

+ 2
- 2
api/authorizer/authorizer_list.go 파일 보기

17
 	"net/url"
17
 	"net/url"
18
 
18
 
19
 	"gitee.com/yansen_zh/wxcomponent/config"
19
 	"gitee.com/yansen_zh/wxcomponent/config"
20
-	wxerr "gitee.com/yansen_zh/wxcomponent/errors"
20
+	"gitee.com/yansen_zh/wxcomponent/errors"
21
 	"gitee.com/yansen_zh/wxcomponent/utils/request"
21
 	"gitee.com/yansen_zh/wxcomponent/utils/request"
22
 )
22
 )
23
 
23
 
24
 // AuthorizerListResult 拉取所有已授权的帐号信息结果
24
 // AuthorizerListResult 拉取所有已授权的帐号信息结果
25
 type AuthorizerListResult struct {
25
 type AuthorizerListResult struct {
26
-	wxerr.Error
26
+	errors.Error
27
 	TotalCount int                   `json:"total_count"`
27
 	TotalCount int                   `json:"total_count"`
28
 	List       []AuthorizerBasicInfo `json:"list"`
28
 	List       []AuthorizerBasicInfo `json:"list"`
29
 }
29
 }

+ 2
- 3
api/authorizer/get_authorizer_option.go 파일 보기

14
 
14
 
15
 import (
15
 import (
16
 	"encoding/json"
16
 	"encoding/json"
17
-	"errors"
18
 	"net/url"
17
 	"net/url"
19
 
18
 
20
 	"gitee.com/yansen_zh/wxcomponent/config"
19
 	"gitee.com/yansen_zh/wxcomponent/config"
21
-	wxerr "gitee.com/yansen_zh/wxcomponent/errors"
20
+	"gitee.com/yansen_zh/wxcomponent/errors"
22
 	"gitee.com/yansen_zh/wxcomponent/utils/request"
21
 	"gitee.com/yansen_zh/wxcomponent/utils/request"
23
 )
22
 )
24
 
23
 
25
 // GetAuthorizerOptionResult 获取授权方选项信息结果
24
 // GetAuthorizerOptionResult 获取授权方选项信息结果
26
 type GetAuthorizerOptionResult struct {
25
 type GetAuthorizerOptionResult struct {
27
-	wxerr.Error
26
+	errors.Error
28
 	AuthorizerAppID string `json:"authorizer_appid"`
27
 	AuthorizerAppID string `json:"authorizer_appid"`
29
 	OptionName      string `json:"option_name"`
28
 	OptionName      string `json:"option_name"`
30
 	OptionValue     string `json:"option_value"`
29
 	OptionValue     string `json:"option_value"`

+ 2
- 3
api/mp/webpage/jsapi.go 파일 보기

15
 import (
15
 import (
16
 	"crypto/sha1"
16
 	"crypto/sha1"
17
 	"encoding/json"
17
 	"encoding/json"
18
-	"errors"
19
 	"fmt"
18
 	"fmt"
20
 	"net/url"
19
 	"net/url"
21
 	"strconv"
20
 	"strconv"
22
 	"time"
21
 	"time"
23
 
22
 
24
-	wxerr "gitee.com/yansen_zh/wxcomponent/errors"
23
+	"gitee.com/yansen_zh/wxcomponent/errors"
25
 	"gitee.com/yansen_zh/wxcomponent/utils"
24
 	"gitee.com/yansen_zh/wxcomponent/utils"
26
 	"gitee.com/yansen_zh/wxcomponent/utils/request"
25
 	"gitee.com/yansen_zh/wxcomponent/utils/request"
27
 )
26
 )
28
 
27
 
29
 // JsapiTicketResult 通过 access_token 获取 jsapi_ticket 结果
28
 // JsapiTicketResult 通过 access_token 获取 jsapi_ticket 结果
30
 type JsapiTicketResult struct {
29
 type JsapiTicketResult struct {
31
-	wxerr.Error
30
+	errors.Error
32
 	Ticket    string `json:"ticket"`
31
 	Ticket    string `json:"ticket"`
33
 	ExpiresIn int    `json:"expires_in"`
32
 	ExpiresIn int    `json:"expires_in"`
34
 }
33
 }

+ 2
- 3
api/mp/webpage/webpage.go 파일 보기

16
 
16
 
17
 import (
17
 import (
18
 	"encoding/json"
18
 	"encoding/json"
19
-	"errors"
20
 	"net/url"
19
 	"net/url"
21
 	"strings"
20
 	"strings"
22
 
21
 
23
 	"gitee.com/yansen_zh/wxcomponent/config"
22
 	"gitee.com/yansen_zh/wxcomponent/config"
24
-	wxerr "gitee.com/yansen_zh/wxcomponent/errors"
23
+	"gitee.com/yansen_zh/wxcomponent/errors"
25
 	"gitee.com/yansen_zh/wxcomponent/utils/request"
24
 	"gitee.com/yansen_zh/wxcomponent/utils/request"
26
 )
25
 )
27
 
26
 
41
 
40
 
42
 // MpOAuthAccessTokenResult 网页授权, 通过 code 换取 access_token 返回值
41
 // MpOAuthAccessTokenResult 网页授权, 通过 code 换取 access_token 返回值
43
 type MpOAuthAccessTokenResult struct {
42
 type MpOAuthAccessTokenResult struct {
44
-	wxerr.Error
43
+	errors.Error
45
 	// AccessToken 接口调用凭证
44
 	// AccessToken 接口调用凭证
46
 	AccessToken string `json:"access_token"`
45
 	AccessToken string `json:"access_token"`
47
 	// ExpiresIn access_token 接口调用凭证超时时间,单位(秒)
46
 	// ExpiresIn access_token 接口调用凭证超时时间,单位(秒)

+ 6
- 6
authorization.go 파일 보기

57
 }
57
 }
58
 
58
 
59
 // RefreshAuthorizerInfo 刷新授权对象信息
59
 // RefreshAuthorizerInfo 刷新授权对象信息
60
-func RefreshAuthorizerInfo(authCode string, authorizer config.AuthorizerConfig) error {
60
+func RefreshAuthorizerInfo(authCode string) error {
61
 	res, err := authorization.GetQueryAuth(authCode)
61
 	res, err := authorization.GetQueryAuth(authCode)
62
 	if err != nil {
62
 	if err != nil {
63
 		return err
63
 		return err
74
 		}
74
 		}
75
 	}
75
 	}
76
 
76
 
77
-	authorizer.RefreshFuncInfo(appID, funcLst)
78
-	if err := authorizer.RefreshToken(appID, authInfo.AuthorizerAccessToken, authInfo.AuthorizerRefreshToken, expire); err != nil {
77
+	config.GetAuthorizer().RefreshFuncInfo(appID, funcLst)
78
+	if err := config.GetAuthorizer().RefreshToken(appID, authInfo.AuthorizerAccessToken, authInfo.AuthorizerRefreshToken, expire); err != nil {
79
 		return err
79
 		return err
80
 	}
80
 	}
81
 
81
 
83
 }
83
 }
84
 
84
 
85
 // RefreshAuthorizerToken 刷新接口调用令牌
85
 // RefreshAuthorizerToken 刷新接口调用令牌
86
-func RefreshAuthorizerToken(appID string, authorizer config.AuthorizerConfig) error {
87
-	res, err := authorization.RefreshAuthorizerToken(appID, authorizer.GetRefreshToken(appID))
86
+func RefreshAuthorizerToken(appID string) error {
87
+	res, err := authorization.RefreshAuthorizerToken(appID, config.GetAuthorizer().GetRefreshToken(appID))
88
 	if err != nil {
88
 	if err != nil {
89
 		return err
89
 		return err
90
 	}
90
 	}
91
 
91
 
92
 	expire := utils.GetExpireTime(res.ExpiresIn)
92
 	expire := utils.GetExpireTime(res.ExpiresIn)
93
 
93
 
94
-	if err := authorizer.RefreshToken(appID, res.AuthorizerAccessToken, res.AuthorizerRefreshToken, expire); err != nil {
94
+	if err := config.GetAuthorizer().RefreshToken(appID, res.AuthorizerAccessToken, res.AuthorizerRefreshToken, expire); err != nil {
95
 		return err
95
 		return err
96
 	}
96
 	}
97
 
97
 

+ 14
- 0
config/authorizer.go 파일 보기

1
+/**
2
+ * Copyright (c) 2022 Yansen Zhang
3
+ * wxcomponent is licensed under Mulan PSL v2.
4
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
5
+ * You may obtain a copy of Mulan PSL v2 at:
6
+ *          http://license.coscl.org.cn/MulanPSL2
7
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
8
+ * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
9
+ * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
10
+ * See the Mulan PSL v2 for more details.
11
+**/
12
+
1
 package config
13
 package config
2
 
14
 
3
 import (
15
 import (
18
 	// RefreshFuncInfo 刷新权限集列表
30
 	// RefreshFuncInfo 刷新权限集列表
19
 	RefreshFuncInfo(appID string, lst []int)
31
 	RefreshFuncInfo(appID string, lst []int)
20
 }
32
 }
33
+
34
+var authorizer AuthorizerConfig

+ 8
- 2
config/index.go 파일 보기

18
 )
18
 )
19
 
19
 
20
 // Init 初始化平台实例
20
 // Init 初始化平台实例
21
-func Init(config Config) error {
22
-	if config == nil {
21
+func Init(config Config, authorizerConfig AuthorizerConfig) error {
22
+	if config == nil || authorizerConfig == nil {
23
 		return errors.New("平台初始化参数不能为空")
23
 		return errors.New("平台初始化参数不能为空")
24
 	}
24
 	}
25
 
25
 
32
 	}
32
 	}
33
 
33
 
34
 	conf = config
34
 	conf = config
35
+	authorizer = authorizerConfig
35
 	return nil
36
 	return nil
36
 }
37
 }
37
 
38
 
40
 	return conf
41
 	return conf
41
 }
42
 }
42
 
43
 
44
+// GetAuthorizer 获取授权对象配置
45
+func GetAuthorizer() AuthorizerConfig {
46
+	return authorizer
47
+}
48
+
43
 // GetAppID 获取平台 APPID
49
 // GetAppID 获取平台 APPID
44
 func GetAppID() string {
50
 func GetAppID() string {
45
 	return conf.GetAppID()
51
 	return conf.GetAppID()

+ 8
- 1
errors/error.go 파일 보기

20
 	Message string `json:"errmsg"`
20
 	Message string `json:"errmsg"`
21
 }
21
 }
22
 
22
 
23
-func (e Error) Error() string {
23
+func (e *Error) Error() string {
24
 	return fmt.Sprintf("[%d] - %s", e.Code, e.Message)
24
 	return fmt.Sprintf("[%d] - %s", e.Code, e.Message)
25
 }
25
 }
26
+
27
+func New(msg string) error {
28
+	return &Error{
29
+		Code:    -100,
30
+		Message: msg,
31
+	}
32
+}

+ 2
- 2
init.go 파일 보기

29
 }
29
 }
30
 
30
 
31
 // InitConfig 初始化配置
31
 // InitConfig 初始化配置
32
-func InitConfig(conf config.Config) error {
33
-	err := config.Init(conf)
32
+func InitConfig(conf config.Config, authConfig config.AuthorizerConfig) error {
33
+	err := config.Init(conf, authConfig)
34
 	if err != nil {
34
 	if err != nil {
35
 		var e error
35
 		var e error
36
 		aesKey, e = base64.StdEncoding.DecodeString(conf.GetAppSecret() + "=")
36
 		aesKey, e = base64.StdEncoding.DecodeString(conf.GetAppSecret() + "=")