From 8814bbdfec71b9ca5d3ac47a3fcddd47149eae3d Mon Sep 17 00:00:00 2001 From: Afeyer Date: Tue, 27 Jul 2021 21:12:41 +0800 Subject: [PATCH] =?UTF-8?q?polish:=E4=BC=98=E5=8C=96=E5=85=A8=E5=B1=80?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- account.go | 11 +++++------ client.go | 21 ++------------------- customer.go | 3 +-- error.go | 22 ++++++++++++++++++++++ media.go | 3 +-- sendmsg.go | 3 +-- servicer.go | 7 +++---- servicestate.go | 5 ++--- token.go | 36 +++++++++++++++++++++++++++++++++--- upgrade.go | 7 +++---- 10 files changed, 73 insertions(+), 45 deletions(-) create mode 100644 error.go diff --git a/account.go b/account.go index ed7c2e0..56c5e0d 100644 --- a/account.go +++ b/account.go @@ -2,7 +2,6 @@ package WeChatCustomerServiceSDK import ( "encoding/json" - "errors" "fmt" "github.com/NICEXAI/WeChatCustomerServiceSDK/util" ) @@ -40,7 +39,7 @@ func (r *Client) AccountAdd(options AccountAddOptions) (info AccountAddSchema, e } _ = json.Unmarshal(data, &info) if info.ErrCode != 0 { - return info, errors.New(info.ErrMsg) + return info, NewSDKErr(info.ErrCode, info.ErrMsg) } return info, nil } @@ -58,7 +57,7 @@ func (r *Client) AccountDel(options AccountDelOptions) (info BaseModel, err erro } _ = json.Unmarshal(data, &info) if info.ErrCode != 0 { - return info, errors.New(info.ErrMsg) + return info, NewSDKErr(info.ErrCode, info.ErrMsg) } return info, nil } @@ -78,7 +77,7 @@ func (r *Client) AccountUpdate(options AccountUpdateOptions) (info BaseModel, er } _ = json.Unmarshal(data, &info) if info.ErrCode != 0 { - return info, errors.New(info.ErrMsg) + return info, NewSDKErr(info.ErrCode, info.ErrMsg) } return info, nil } @@ -104,7 +103,7 @@ func (r *Client) AccountList() (info AccountListSchema, err error) { } _ = json.Unmarshal(data, &info) if info.ErrCode != 0 { - return info, errors.New(info.ErrMsg) + return info, NewSDKErr(info.ErrCode, info.ErrMsg) } return info, nil } @@ -129,7 +128,7 @@ func (r *Client) AddContactWay(options AddContactWayOptions) (info AddContactWay } _ = json.Unmarshal(data, &info) if info.ErrCode != 0 { - return info, errors.New(info.ErrMsg) + return info, NewSDKErr(info.ErrCode, info.ErrMsg) } return info, nil } diff --git a/client.go b/client.go index ed6a213..e01e9ba 100644 --- a/client.go +++ b/client.go @@ -57,25 +57,8 @@ func New(options Options) (client *Client, err error) { mutex: sync.Mutex{}, } - //判断是否已初始化完成,如果己初始化则直接返回当前实例 - token, err := client.getAccessToken() - if err != nil { - return nil, errors.New("cache unavailable") - } - - if token == "" { - //初始化AccessToken - tokenInfo, err := client.GetAccessToken() - if err != nil { - return nil, err - } - - if err = client.setAccessToken(tokenInfo.AccessToken); err != nil { - return nil, err - } - client.accessToken = tokenInfo.AccessToken - } else { - client.accessToken = token + if err = client.initAccessToken(); err != nil { + return nil, err } return client, nil diff --git a/customer.go b/customer.go index 46aaedb..69d3a73 100644 --- a/customer.go +++ b/customer.go @@ -2,7 +2,6 @@ package WeChatCustomerServiceSDK import ( "encoding/json" - "errors" "fmt" "github.com/NICEXAI/WeChatCustomerServiceSDK/util" ) @@ -40,7 +39,7 @@ func (r *Client) CustomerBatchGet(options CustomerBatchGetOptions) (info Custome } _ = json.Unmarshal(data, &info) if info.ErrCode != 0 { - return info, errors.New(info.ErrMsg) + return info, NewSDKErr(info.ErrCode, info.ErrMsg) } return info, nil } \ No newline at end of file diff --git a/error.go b/error.go new file mode 100644 index 0000000..b6c3bab --- /dev/null +++ b/error.go @@ -0,0 +1,22 @@ +package WeChatCustomerServiceSDK + +import "fmt" + +// Error 错误 +type Error struct { + ErrCode int `json:"err_code,omitempty"` + ErrMsg string `json:"err_msg"` +} + +//输出错误信息 +func (r Error) Error() string { + return fmt.Sprintf("%d:%s", r.ErrCode, r.ErrMsg) +} + +// NewSDKErr 初始化SDK实例错误信息 +func NewSDKErr(code int, msg string) Error { + return Error{ + ErrCode: code, + ErrMsg: msg, + } +} \ No newline at end of file diff --git a/media.go b/media.go index 0151534..e73d81b 100644 --- a/media.go +++ b/media.go @@ -2,7 +2,6 @@ package WeChatCustomerServiceSDK import ( "encoding/json" - "errors" "fmt" "github.com/NICEXAI/WeChatCustomerServiceSDK/util" "mime/multipart" @@ -55,7 +54,7 @@ func (r *Client) MediaUpload(options MediaUploadOptions) (info MediaUploadSchema _ = json.Unmarshal(data, &info) fmt.Println(string(data)) if info.ErrCode != 0 { - return info, errors.New(info.ErrMsg) + return info, NewSDKErr(info.ErrCode, info.ErrMsg) } return info, nil } \ No newline at end of file diff --git a/sendmsg.go b/sendmsg.go index a50ebaf..006b4bd 100644 --- a/sendmsg.go +++ b/sendmsg.go @@ -2,7 +2,6 @@ package WeChatCustomerServiceSDK import ( "encoding/json" - "errors" "fmt" "github.com/NICEXAI/WeChatCustomerServiceSDK/util" ) @@ -26,7 +25,7 @@ func (r *Client) SendMsg(options interface{}) (info SendMsgSchema, err error) { } _ = json.Unmarshal(data, &info) if info.ErrCode != 0 { - return info, errors.New(info.ErrMsg) + return info, NewSDKErr(info.ErrCode, info.ErrMsg) } return info, nil } \ No newline at end of file diff --git a/servicer.go b/servicer.go index 5c4abfb..12d8416 100644 --- a/servicer.go +++ b/servicer.go @@ -2,7 +2,6 @@ package WeChatCustomerServiceSDK import ( "encoding/json" - "errors" "fmt" "github.com/NICEXAI/WeChatCustomerServiceSDK/util" ) @@ -39,7 +38,7 @@ func (r *Client) ReceptionistAdd(options ReceptionistOptions) (info Receptionist } _ = json.Unmarshal(data, &info) if info.ErrCode != 0 { - return info, errors.New(info.ErrMsg) + return info, NewSDKErr(info.ErrCode, info.ErrMsg) } return info, nil } @@ -52,7 +51,7 @@ func (r *Client) ReceptionistDel(options ReceptionistOptions) (info Receptionist } _ = json.Unmarshal(data, &info) if info.ErrCode != 0 { - return info, errors.New(info.ErrMsg) + return info, NewSDKErr(info.ErrCode, info.ErrMsg) } return info, nil } @@ -74,7 +73,7 @@ func (r *Client) ReceptionistList(kfID string) (info ReceptionistListSchema, err } _ = json.Unmarshal(data, &info) if info.ErrCode != 0 { - return info, errors.New(info.ErrMsg) + return info, NewSDKErr(info.ErrCode, info.ErrMsg) } return info, nil } \ No newline at end of file diff --git a/servicestate.go b/servicestate.go index 36eaeac..25adc8e 100644 --- a/servicestate.go +++ b/servicestate.go @@ -2,7 +2,6 @@ package WeChatCustomerServiceSDK import ( "encoding/json" - "errors" "fmt" "github.com/NICEXAI/WeChatCustomerServiceSDK/util" ) @@ -40,7 +39,7 @@ func (r *Client) ServiceStateGet(options ServiceStateGetOptions) (info ServiceSt } _ = json.Unmarshal(data, &info) if info.ErrCode != 0 { - return info, errors.New(info.ErrMsg) + return info, NewSDKErr(info.ErrCode, info.ErrMsg) } return info, nil } @@ -61,7 +60,7 @@ func (r *Client) ServiceStateTrans(options ServiceStateTransOptions) (info BaseM } _ = json.Unmarshal(data, &info) if info.ErrCode != 0 { - return info, errors.New(info.ErrMsg) + return info, NewSDKErr(info.ErrCode, info.ErrMsg) } return info, nil } \ No newline at end of file diff --git a/token.go b/token.go index edc50f7..5c92496 100644 --- a/token.go +++ b/token.go @@ -26,17 +26,47 @@ func (r *Client) GetAccessToken() (info AccessTokenSchema, err error) { return info, err } _ = json.Unmarshal(data, &info) - fmt.Println(string(data)) if info.ErrCode != 0 { - return info, errors.New(info.ErrMsg) + return info, NewSDKErr(info.ErrCode, info.ErrMsg) } return info, nil } +// RefreshAccessToken 刷新调用凭证access_token +func (r *Client) RefreshAccessToken() error { + //初始化AccessToken + tokenInfo, err := r.GetAccessToken() + if err != nil { + return err + } + if err = r.setAccessToken(tokenInfo.AccessToken); err != nil { + return err + } + r.accessToken = tokenInfo.AccessToken + return nil +} + +func (r *Client) initAccessToken() error { + //判断是否已初始化完成,如果己初始化则直接返回当前实例 + token, err := r.getAccessToken() + if err != nil { + return errors.New("cache unavailable") + } + if token == "" { + if err = r.RefreshAccessToken(); err != nil { + return err + } + } else { + r.accessToken = token + } + return nil +} + + func (r *Client) getAccessToken() (string, error) { return r.cache.Get("wechat:kf:" + r.corpID) } func (r *Client) setAccessToken(token string) error { return r.cache.Set("wechat:kf:" + r.corpID, token, r.expireTime) -} +} \ No newline at end of file diff --git a/upgrade.go b/upgrade.go index f0e76a8..c957e5c 100644 --- a/upgrade.go +++ b/upgrade.go @@ -2,7 +2,6 @@ package WeChatCustomerServiceSDK import ( "encoding/json" - "errors" "fmt" "github.com/NICEXAI/WeChatCustomerServiceSDK/util" ) @@ -36,7 +35,7 @@ func (r *Client) UpgradeServiceConfig() (info UpgradeServiceConfigSchema, err er } _ = json.Unmarshal(data, &info) if info.ErrCode != 0 { - return info, errors.New(info.ErrMsg) + return info, NewSDKErr(info.ErrCode, info.ErrMsg) } return info, nil } @@ -64,7 +63,7 @@ func (r *Client) UpgradeService(options UpgradeServiceOptions) (info BaseModel, } _ = json.Unmarshal(data, &info) if info.ErrCode != 0 { - return info, errors.New(info.ErrMsg) + return info, NewSDKErr(info.ErrCode, info.ErrMsg) } return info, nil } @@ -83,7 +82,7 @@ func (r *Client) UpgradeServiceCancel(options UpgradeServiceCancelOptions) (info } _ = json.Unmarshal(data, &info) if info.ErrCode != 0 { - return info, errors.New(info.ErrMsg) + return info, NewSDKErr(info.ErrCode, info.ErrMsg) } return info, nil } \ No newline at end of file