30 lines
834 B
Go
30 lines
834 B
Go
package wechat
|
|
|
|
import (
|
|
"errors"
|
|
"git.hlsq.asia/mmorpg/service-common/log"
|
|
"git.hlsq.asia/mmorpg/service-user/config"
|
|
"net/http"
|
|
"net/url"
|
|
)
|
|
|
|
// MiniCode2Session 根据code获取openID
|
|
func MiniCode2Session(code string) (*Code2SessionResp, error) {
|
|
values := &url.Values{}
|
|
values.Set("appid", config.Get().WxMini.AppID)
|
|
values.Set("secret", config.Get().WxMini.Secret)
|
|
values.Set("js_code", code)
|
|
values.Set("grant_type", "authorization_code")
|
|
|
|
resp := &Code2SessionResp{}
|
|
err := RequestWechatMini("/sns/jscode2session", http.MethodGet, values, nil, resp)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if resp.ErrCode != ErrorCodeOK {
|
|
log.Errorf("[WechatMini] MiniCode2Session err: response.Code = %v, msg: %v, req: %v", resp.ErrCode, resp.ErrMsg, values.Encode())
|
|
return nil, errors.New(resp.ErrMsg)
|
|
}
|
|
return resp, nil
|
|
}
|