Browse Source

feats: change error define

Your Name 3 years ago
parent
commit
ad2817c009

+ 2
- 3
api/authorization/authorizer_info.go View File

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

+ 2
- 3
api/authorization/authorizer_token.go View File

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

+ 2
- 2
api/authorization/component_token.go View File

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

+ 2
- 2
api/authorization/pre_auth_code.go View File

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

+ 2
- 3
api/authorization/query_auth.go View File

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

+ 2
- 2
api/authorizer/authorizer_list.go View File

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

+ 2
- 3
api/authorizer/get_authorizer_option.go View File

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

+ 2
- 3
api/mp/webpage/jsapi.go View File

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

+ 2
- 3
api/mp/webpage/webpage.go View File

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

+ 6
- 6
authorization.go View File

@@ -57,7 +57,7 @@ func CreateAuthLink(client, url, appID string) (string, error) {
57 57
 }
58 58
 
59 59
 // RefreshAuthorizerInfo 刷新授权对象信息
60
-func RefreshAuthorizerInfo(authCode string, authorizer config.AuthorizerConfig) error {
60
+func RefreshAuthorizerInfo(authCode string) error {
61 61
 	res, err := authorization.GetQueryAuth(authCode)
62 62
 	if err != nil {
63 63
 		return err
@@ -74,8 +74,8 @@ func RefreshAuthorizerInfo(authCode string, authorizer config.AuthorizerConfig)
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 79
 		return err
80 80
 	}
81 81
 
@@ -83,15 +83,15 @@ func RefreshAuthorizerInfo(authCode string, authorizer config.AuthorizerConfig)
83 83
 }
84 84
 
85 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 88
 	if err != nil {
89 89
 		return err
90 90
 	}
91 91
 
92 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 95
 		return err
96 96
 	}
97 97
 

+ 14
- 0
config/authorizer.go View File

@@ -1,3 +1,15 @@
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 13
 package config
2 14
 
3 15
 import (
@@ -18,3 +30,5 @@ type AuthorizerConfig interface {
18 30
 	// RefreshFuncInfo 刷新权限集列表
19 31
 	RefreshFuncInfo(appID string, lst []int)
20 32
 }
33
+
34
+var authorizer AuthorizerConfig

+ 8
- 2
config/index.go View File

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

+ 8
- 1
errors/error.go View File

@@ -20,6 +20,13 @@ type Error struct {
20 20
 	Message string `json:"errmsg"`
21 21
 }
22 22
 
23
-func (e Error) Error() string {
23
+func (e *Error) Error() string {
24 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 View File

@@ -29,8 +29,8 @@ func InitLogger(logger log.Logger) {
29 29
 }
30 30
 
31 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 34
 	if err != nil {
35 35
 		var e error
36 36
 		aesKey, e = base64.StdEncoding.DecodeString(conf.GetAppSecret() + "=")