Files

74 lines
1.8 KiB
Go
Raw Normal View History

2021-09-30 12:09:45 +08:00
package platform
2021-11-01 11:19:49 +08:00
import (
"SciencesServer/utils"
"errors"
)
2021-09-30 12:09:45 +08:00
2021-11-01 11:19:49 +08:00
type Wechat struct {
AppID, AppSecret string
}
2021-09-30 12:09:45 +08:00
2021-11-01 11:19:49 +08:00
type WechatHandle func(appID, appSecret string) *Wechat
2021-09-30 12:09:45 +08:00
type (
WechatErrorResponse struct {
2021-11-01 11:19:49 +08:00
ErrCode int `json:"errcode"`
ErrMsg string `json:"errmsg"`
2021-09-30 12:09:45 +08:00
}
// WechatAccessTokenRequest 请求AccessToken参数
WechatAccessTokenRequest struct {
Code string
}
// WechatAccessTokenResponse 获取AccessToken响应参数
WechatAccessTokenResponse struct {
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
OpenID string `json:"openid"`
Scope string `json:"scope"`
UnionID string `json:"unionid"`
2021-11-01 11:19:49 +08:00
WechatErrorResponse
2021-09-30 12:09:45 +08:00
}
)
const (
// WechatAccessTokenUrl AccessToken访问地址
WechatAccessTokenUrl string = "https://api.weixin.qq.com/sns/oauth2/access_token"
)
2021-11-01 11:19:49 +08:00
func (this *Wechat) ScanLogin() {
2021-09-30 12:09:45 +08:00
}
2021-11-01 11:19:49 +08:00
func (this *Wechat) ScanPay() {
2021-09-30 12:09:45 +08:00
}
// AccessToken AccessToken操作
func (this *Wechat) AccessToken(req *WechatAccessTokenRequest) (*WechatAccessTokenResponse, error) {
params := map[string]interface{}{
2021-11-01 11:19:49 +08:00
"appid": this.AppID, "secret": this.AppSecret, "code": req.Code, "grant_type": "authorization_code",
2021-09-30 12:09:45 +08:00
}
resp, err := utils.NewClient(WechatAccessTokenUrl, utils.MethodForGet, params).Request(utils.RequestBodyFormatForXWWWFormUrlencoded)
if err != nil {
return nil, err
}
out := new(WechatAccessTokenResponse)
if err = utils.FromJSONBytes(resp, out); err != nil {
return nil, err
}
2021-11-01 11:19:49 +08:00
if out.WechatErrorResponse.ErrCode != 0 {
return nil, errors.New(out.WechatErrorResponse.ErrMsg)
}
2021-09-30 12:09:45 +08:00
return out, nil
}
2021-11-01 11:19:49 +08:00
func NewWechat() WechatHandle {
return func(appID, appSecret string) *Wechat {
return &Wechat{AppID: appID, AppSecret: appSecret}
2021-09-30 12:09:45 +08:00
}
}