feat: openid login
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"lone-services/pkg/utils"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
const accessTokenURL = "https://api.weixin.qq.com/cgi-bin/token"
|
||||
|
||||
type AccessTokenResult struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
ExpiresIn int `json:"expires_in"`
|
||||
ErrCode int `json:"errcode"`
|
||||
ErrMsg string `json:"errmsg"`
|
||||
}
|
||||
|
||||
func (c *Client) AccessToken() (*AccessTokenResult, error) {
|
||||
if c == nil || c.AppID == utils.StringEmpty || c.Secret == utils.StringEmpty {
|
||||
return nil, fmt.Errorf("wechat app config missing")
|
||||
}
|
||||
|
||||
body, err := utils.Curl(accessTokenURL).SetData(map[string]string{
|
||||
"grant_type": "client_credential",
|
||||
"appid": c.AppID,
|
||||
"secret": c.Secret,
|
||||
}).Get("")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result AccessTokenResult
|
||||
if err := jsoniter.UnmarshalFromString(body, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if result.ErrCode != utils.NumberZero {
|
||||
return nil, fmt.Errorf("wechat access_token: %d %s", result.ErrCode, result.ErrMsg)
|
||||
}
|
||||
if result.AccessToken == utils.StringEmpty {
|
||||
return nil, fmt.Errorf("wechat access_token empty")
|
||||
}
|
||||
return &result, nil
|
||||
}
|
||||
Reference in New Issue
Block a user