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
|
||||
}
|
||||
54
internal/wechat/response.go
Normal file
54
internal/wechat/response.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package wechat
|
||||
|
||||
type ResponseBase struct {
|
||||
ErrCode int32 `json:"errcode"`
|
||||
ErrMsg string `json:"errmsg"`
|
||||
}
|
||||
|
||||
type GetAccessTokenResp struct {
|
||||
ResponseBase
|
||||
AccessToken string `json:"access_token"`
|
||||
ExpiresIn int32 `json:"expires_in"`
|
||||
RefreshToken string `json:"refresh_token"`
|
||||
OpenID string `json:"openid"`
|
||||
Scope string `json:"scope"`
|
||||
UnionID string `json:"unionid"`
|
||||
}
|
||||
|
||||
type GetUserInfoResp struct {
|
||||
ResponseBase
|
||||
NickName string `json:"nickname"`
|
||||
HeadImgURL string `json:"headimgurl"`
|
||||
UnionID string `json:"unionid"`
|
||||
}
|
||||
|
||||
type Code2SessionResp struct {
|
||||
ResponseBase
|
||||
OpenID string `json:"openid"`
|
||||
UnionID string `json:"unionid"`
|
||||
}
|
||||
|
||||
type GetUserPhoneNumberResp struct {
|
||||
ResponseBase
|
||||
PhoneInfo *struct {
|
||||
CountryCode string `json:"countryCode"`
|
||||
PurePhoneNumber string `json:"purePhoneNumber"`
|
||||
} `json:"phone_info"`
|
||||
}
|
||||
|
||||
type GenerateSchemeResp struct {
|
||||
ResponseBase
|
||||
OpenLink string `json:"openlink"`
|
||||
}
|
||||
|
||||
type GenerateURLLinkReq struct {
|
||||
Path string `json:"path"` // 小程序页面路径
|
||||
Query string `json:"query"` // 参数
|
||||
ExpireTime int64 `json:"expire_time"` // 失效间隔时间
|
||||
EnvVersion string `json:"env_version"` // 版本
|
||||
}
|
||||
|
||||
type GenerateURLLinkResp struct {
|
||||
ResponseBase
|
||||
UrlLink string `json:"url_link"`
|
||||
}
|
||||
58
internal/wechat/service.go
Normal file
58
internal/wechat/service.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package wechat
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"git.hlsq.asia/mmorpg/service-common/log"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ErrorCode int32
|
||||
|
||||
const (
|
||||
ErrorCodeOK = 0
|
||||
)
|
||||
|
||||
func RequestWechatMini(path string, method string, params *url.Values, values []byte, response any) error {
|
||||
u := &url.URL{}
|
||||
u.Scheme = "https"
|
||||
u.Host = "api.weixin.qq.com"
|
||||
u.Path = path
|
||||
|
||||
client := &http.Client{Timeout: 10 * time.Second}
|
||||
if params != nil {
|
||||
u.RawQuery = params.Encode()
|
||||
}
|
||||
var resp *http.Response
|
||||
var err error
|
||||
if method == http.MethodGet {
|
||||
resp, err = client.Get(u.String())
|
||||
} else {
|
||||
resp, err = client.Post(u.String(), "application/json", bytes.NewBuffer(values))
|
||||
}
|
||||
if err != nil {
|
||||
log.Errorf("[WechatMini] %v err: %v", path, err)
|
||||
return err
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
log.Errorf("[WechatMini] %v err: resp.StatusCode = %v", path, resp.StatusCode)
|
||||
return errors.New(resp.Status)
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Errorf("[WechatMini] %v err: %v", path, err)
|
||||
return err
|
||||
}
|
||||
err = json.Unmarshal(body, response)
|
||||
if err != nil {
|
||||
log.Errorf("[WechatMini] %v err: %v", path, err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user