refactor: mp.auth
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package oauth2
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -7,16 +7,35 @@ import (
|
||||
"git.esin.io/lab/weixin/clientapi/request"
|
||||
)
|
||||
|
||||
type Oauth2Client struct {
|
||||
type Config struct {
|
||||
ClientID string
|
||||
ClientSecret string
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
appId string
|
||||
appSecret string
|
||||
}
|
||||
|
||||
type GetCodeURLRequest struct {
|
||||
RedirectURL, State, Scope string
|
||||
func NewClient(cfg *Config) *Client {
|
||||
return &Client{
|
||||
appId: cfg.ClientID,
|
||||
appSecret: cfg.ClientSecret,
|
||||
}
|
||||
}
|
||||
|
||||
func (c Oauth2Client) GetCodeURL(redirectURL, state, scope string) string {
|
||||
type Scope string
|
||||
|
||||
const (
|
||||
ScopeBase Scope = "snsapi_base"
|
||||
ScopeUserinfo Scope = "snsapi_userinfo"
|
||||
)
|
||||
|
||||
func (scope Scope) String() string {
|
||||
return string(scope)
|
||||
}
|
||||
|
||||
func (c Client) GetCodeURL(redirectURL, state string, scope Scope) string {
|
||||
endpoint := url.URL{
|
||||
Scheme: "https",
|
||||
Host: "open.weixin.qq.com",
|
||||
@@ -25,7 +44,7 @@ func (c Oauth2Client) GetCodeURL(redirectURL, state, scope string) string {
|
||||
"appid": {c.appId},
|
||||
"redirect_uri": {redirectURL},
|
||||
"response_type": {"code"},
|
||||
"scope": {scope},
|
||||
"scope": {scope.String()},
|
||||
"state": {state},
|
||||
}.Encode(),
|
||||
Fragment: "wechat_redirect",
|
||||
@@ -33,7 +52,7 @@ func (c Oauth2Client) GetCodeURL(redirectURL, state, scope string) string {
|
||||
return endpoint.String()
|
||||
}
|
||||
|
||||
func (c Oauth2Client) ExchangeToken(ctx context.Context, code string) (*Token, error) {
|
||||
func (c Client) ExchangeToken(ctx context.Context, code string) (*Token, error) {
|
||||
req := request.New(
|
||||
"/sns/oauth2/access_token",
|
||||
url.Values{
|
||||
@@ -54,7 +73,19 @@ func (c Oauth2Client) ExchangeToken(ctx context.Context, code string) (*Token, e
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
func (c Oauth2Client) GetUserinfo(ctx context.Context, accessToken, openid string) (*Userinfo, error) {
|
||||
type Lang string
|
||||
|
||||
const (
|
||||
LangZhCN Lang = "zh_CN"
|
||||
LangZhTW Lang = "zh_TW"
|
||||
LangEn Lang = "en"
|
||||
)
|
||||
|
||||
func (lang Lang) String() string {
|
||||
return string(lang)
|
||||
}
|
||||
|
||||
func (c Client) GetUserinfo(ctx context.Context, accessToken, openid string, lang Lang) (*Userinfo, error) {
|
||||
req := request.New(
|
||||
"/sns/userinfo",
|
||||
url.Values{
|
||||
@@ -74,7 +105,7 @@ func (c Oauth2Client) GetUserinfo(ctx context.Context, accessToken, openid strin
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
func (c Oauth2Client) RefreshToken(ctx context.Context, refreshToken, openid string) (*Token, error) {
|
||||
func (c Client) RefreshToken(ctx context.Context, refreshToken string) (*Token, error) {
|
||||
req := request.New(
|
||||
"/sns/oauth2/refresh_token",
|
||||
url.Values{
|
||||
@@ -95,7 +126,7 @@ func (c Oauth2Client) RefreshToken(ctx context.Context, refreshToken, openid str
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
func (c Oauth2Client) ValidateToken(ctx context.Context, accessToken, openid string) error {
|
||||
func (c Client) ValidateToken(ctx context.Context, accessToken, openid string) error {
|
||||
req := request.New(
|
||||
"/sns/auth",
|
||||
url.Values{
|
||||
@@ -115,7 +146,7 @@ func (c Oauth2Client) ValidateToken(ctx context.Context, accessToken, openid str
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c Oauth2Client) GetClientCredential(ctx context.Context) (*ClientCredential, error) {
|
||||
func (c Client) GetClientCredential(ctx context.Context) (*ClientCredential, error) {
|
||||
req := request.New(
|
||||
"/cgi-bin/token",
|
||||
url.Values{
|
@@ -1,4 +1,4 @@
|
||||
package oauth2
|
||||
package auth
|
||||
|
||||
import (
|
||||
"git.esin.io/lab/weixin/clientapi/request"
|
@@ -1,4 +1,4 @@
|
||||
package oauth2
|
||||
package auth
|
||||
|
||||
import (
|
||||
"git.esin.io/lab/weixin/clientapi/request"
|
||||
@@ -9,6 +9,6 @@ type Token struct {
|
||||
AccessToken string `json:"access_token"` //获取到的凭证
|
||||
ExpiresIn int32 `json:"expires_in"` //凭证有效时间,单位:秒
|
||||
RefreshToken string `json:"refresh_token"` //有效期为30天,当失效之后,需要用户重新授
|
||||
OpenID string `json:"openid"`
|
||||
OpenID string `json:"openid" gorm:"index"`
|
||||
Scope string `json:"scope"`
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
package oauth2
|
||||
package auth
|
||||
|
||||
import (
|
||||
"git.esin.io/lab/weixin/clientapi/request"
|
||||
@@ -6,13 +6,13 @@ import (
|
||||
|
||||
type Userinfo struct {
|
||||
request.Error
|
||||
OpenID string `json:"openid"`
|
||||
OpenID string `json:"openid" gorm:"index"`
|
||||
NickName string `json:"nickname"`
|
||||
Sex int8 `json:"sex"`
|
||||
Sex int32 `json:"sex"`
|
||||
Province string `json:"province"`
|
||||
City string `json:"city"`
|
||||
Country string `json:"country"`
|
||||
HeadImgURL string `json:"headimgurl"`
|
||||
Privilege []string `json:"privilege"`
|
||||
Privilege []string `json:"privilege" gorm:"type:text[]"`
|
||||
UnionID string `json:"unionid"`
|
||||
}
|
@@ -13,8 +13,11 @@ func (err Error) IsZero() bool {
|
||||
}
|
||||
|
||||
func (err Error) Error() string {
|
||||
text := ErrCodeText[err.Code]
|
||||
return fmt.Sprintf("%d:%s, %s", err.Code, err.Msg, text)
|
||||
text, ok := ErrCodeText[err.Code]
|
||||
if ok {
|
||||
return fmt.Sprintf("%d:%s, %s", err.Code, err.Msg, text)
|
||||
}
|
||||
return fmt.Sprintf("%d:%s", err.Code, err.Msg)
|
||||
}
|
||||
|
||||
func (err Error) Err() error {
|
||||
|
Reference in New Issue
Block a user