74 lines
1.8 KiB
Go
74 lines
1.8 KiB
Go
package platform
|
|
|
|
import (
|
|
"SciencesServer/utils"
|
|
"errors"
|
|
)
|
|
|
|
type Wechat struct {
|
|
AppID, AppSecret string
|
|
}
|
|
|
|
type WechatHandle func(appID, appSecret string) *Wechat
|
|
|
|
type (
|
|
WechatErrorResponse struct {
|
|
ErrCode int `json:"errcode"`
|
|
ErrMsg string `json:"errmsg"`
|
|
}
|
|
// 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"`
|
|
WechatErrorResponse
|
|
}
|
|
)
|
|
|
|
const (
|
|
// WechatAccessTokenUrl AccessToken访问地址
|
|
WechatAccessTokenUrl string = "https://api.weixin.qq.com/sns/oauth2/access_token"
|
|
)
|
|
|
|
func (this *Wechat) ScanLogin() {
|
|
|
|
}
|
|
|
|
func (this *Wechat) ScanPay() {
|
|
|
|
}
|
|
|
|
// AccessToken AccessToken操作
|
|
func (this *Wechat) AccessToken(req *WechatAccessTokenRequest) (*WechatAccessTokenResponse, error) {
|
|
params := map[string]interface{}{
|
|
"appid": this.AppID, "secret": this.AppSecret, "code": req.Code, "grant_type": "authorization_code",
|
|
}
|
|
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
|
|
}
|
|
if out.WechatErrorResponse.ErrCode != 0 {
|
|
return nil, errors.New(out.WechatErrorResponse.ErrMsg)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func NewWechat() WechatHandle {
|
|
return func(appID, appSecret string) *Wechat {
|
|
return &Wechat{AppID: appID, AppSecret: appSecret}
|
|
}
|
|
}
|