feat 微信登录
This commit is contained in:
29
internal/wechat/mini.go
Normal file
29
internal/wechat/mini.go
Normal file
@@ -0,0 +1,29 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user