fix: issues
This commit is contained in:
@@ -23,8 +23,12 @@ import "github.com/pkg/errors"
|
||||
// 10013 state不能为空
|
||||
// 10015 公众号未授权第三方平台,请检查授权状态
|
||||
// 10016 不支持微信开放平台的Appid,请使用公众号Appid
|
||||
func translateError(errCode int) error {
|
||||
errors := map[int]error{
|
||||
func translateError(errCode int, msg string) error {
|
||||
if errCode == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
errs := map[int]error{
|
||||
0: nil,
|
||||
-1: errors.New("系统繁忙,此时请开发者稍候再试"),
|
||||
40001: errors.New("AppSecret错误或者AppSecret不属于这个公众号,请开发者确认AppSecret的正确性"),
|
||||
@@ -48,8 +52,8 @@ func translateError(errCode int) error {
|
||||
10016: errors.New("不支持微信开放平台的Appid,请使用公众号Appid"),
|
||||
}
|
||||
|
||||
if err, ok := errors[errCode]; ok {
|
||||
if err, ok := errs[errCode]; ok {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
return errors.New(msg)
|
||||
}
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
package wechat
|
||||
|
||||
type Response struct {
|
||||
ErrCode int `json:"errcode"`
|
||||
ErrMsg int `json:"errmsg"`
|
||||
ErrDescribe int `json:"-"`
|
||||
type ErrorResponse struct {
|
||||
ErrCode int `json:"errcode,omitempty"`
|
||||
ErrMsg string `json:"errmsg,omitempty"`
|
||||
}
|
||||
|
||||
func (r *Response) Error() error {
|
||||
return translateError(r.ErrCode)
|
||||
func (r *ErrorResponse) Error() error {
|
||||
return translateError(r.ErrCode, r.ErrMsg)
|
||||
}
|
||||
|
||||
type AccessTokenResponse struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
ExpiresIn int `json:"expires_in"` // seconds
|
||||
ErrorResponse
|
||||
AccessToken string `json:"access_token,omitempty"`
|
||||
ExpiresIn int `json:"expires_in,omitempty"` // seconds
|
||||
}
|
||||
|
||||
@@ -80,13 +80,22 @@ func (we *Client) GetAccessToken() (*AccessTokenResponse, error) {
|
||||
"grant_type": "client_credential",
|
||||
}
|
||||
|
||||
var data AccessTokenResponse
|
||||
_, err := we.client.R().SetSuccessResult(&data).SetQueryParams(params).Get("/cgi-bin/token")
|
||||
var data ErrorResponse
|
||||
resp, err := we.client.R().SetSuccessResult(&data).SetQueryParams(params).Get("/cgi-bin/token")
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "call /cgi-bin/token failed")
|
||||
}
|
||||
|
||||
return &data, nil
|
||||
if data.ErrCode != 0 {
|
||||
return nil, data.Error()
|
||||
}
|
||||
|
||||
var token AccessTokenResponse
|
||||
if err := resp.Unmarshal(&token); err != nil {
|
||||
return nil, errors.Wrap(err, "parse response failed")
|
||||
}
|
||||
|
||||
return &token, nil
|
||||
}
|
||||
|
||||
// ScopeAuthorizeURL
|
||||
@@ -115,13 +124,14 @@ func (we *Client) ScopeAuthorizeURL(opts ...ScopeAuthorizeURLOptions) (*url.URL,
|
||||
}
|
||||
|
||||
type AuthorizeAccessToken struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
ExpiresIn int64 `json:"expires_in"`
|
||||
IsSnapshotuser int64 `json:"is_snapshotuser"`
|
||||
Openid string `json:"openid"`
|
||||
RefreshToken string `json:"refresh_token"`
|
||||
Scope string `json:"scope"`
|
||||
Unionid string `json:"unionid"`
|
||||
ErrorResponse
|
||||
AccessToken string `json:"access_token,omitempty"`
|
||||
ExpiresIn int64 `json:"expires_in,omitempty"`
|
||||
IsSnapshotuser int64 `json:"is_snapshotuser,omitempty"`
|
||||
Openid string `json:"openid,omitempty"`
|
||||
RefreshToken string `json:"refresh_token,omitempty"`
|
||||
Scope string `json:"scope,omitempty"`
|
||||
Unionid string `json:"unionid,omitempty"`
|
||||
}
|
||||
|
||||
func (we *Client) AuthorizeCode2Token(code string) (*AuthorizeAccessToken, error) {
|
||||
@@ -136,6 +146,10 @@ func (we *Client) AuthorizeCode2Token(code string) (*AuthorizeAccessToken, error
|
||||
return nil, errors.Wrap(err, "call /sns/oauth2/access_token failed")
|
||||
}
|
||||
|
||||
if err := data.Error(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &data, nil
|
||||
}
|
||||
|
||||
@@ -151,19 +165,24 @@ func (we *Client) AuthorizeRefreshAccessToken(accessToken string) (*AuthorizeAcc
|
||||
return nil, errors.Wrap(err, "call /sns/oauth2/refresh_token failed")
|
||||
}
|
||||
|
||||
if err := data.Error(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &data, nil
|
||||
}
|
||||
|
||||
type AuthorizeUserInfo struct {
|
||||
City string `json:"city"`
|
||||
Country string `json:"country"`
|
||||
Headimgurl string `json:"headimgurl"`
|
||||
Nickname string `json:"nickname"`
|
||||
Openid string `json:"openid"`
|
||||
Privilege []string `json:"privilege"`
|
||||
Province string `json:"province"`
|
||||
Sex int64 `json:"sex"`
|
||||
Unionid string `json:"unionid"`
|
||||
ErrorResponse
|
||||
City string `json:"city,omitempty"`
|
||||
Country string `json:"country,omitempty"`
|
||||
Headimgurl string `json:"headimgurl,omitempty"`
|
||||
Nickname string `json:"nickname,omitempty"`
|
||||
Openid string `json:"openid,omitempty"`
|
||||
Privilege []string `json:"privilege,omitempty"`
|
||||
Province string `json:"province,omitempty"`
|
||||
Sex int64 `json:"sex,omitempty"`
|
||||
Unionid string `json:"unionid,omitempty"`
|
||||
}
|
||||
|
||||
func (we *Client) AuthorizeUserInfo(accessToken, openID string) (*AuthorizeUserInfo, error) {
|
||||
@@ -178,5 +197,9 @@ func (we *Client) AuthorizeUserInfo(accessToken, openID string) (*AuthorizeUserI
|
||||
return nil, errors.Wrap(err, "call /sns/userinfo failed")
|
||||
}
|
||||
|
||||
if err := data.Error(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &data, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user