feat 微信登录

This commit is contained in:
2026-01-14 10:53:27 +08:00
parent 7aec1c2d4f
commit 3750ff1c34
12 changed files with 235 additions and 25 deletions

29
internal/wechat/mini.go Normal file
View 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
}