v1.1
This commit is contained in:
@@ -1,12 +1,15 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"ycc-server/app/main/api/internal/config"
|
||||
tianyuanapi "ycc-server/app/main/api/internal/service/tianyuanapi_sdk"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"ycc-server/app/main/api/internal/config"
|
||||
tianyuanapi "ycc-server/app/main/api/internal/service/tianyuanapi_sdk"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
type VerificationService struct {
|
||||
c config.Config
|
||||
@@ -150,3 +153,55 @@ func (r *VerificationService) ThreeFactorVerification(request ThreeFactorVerific
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
// GetWechatH5OpenID 通过code获取微信H5 OpenID
|
||||
func (r *VerificationService) GetWechatH5OpenID(ctx context.Context, code string) (string, error) {
|
||||
appID := r.c.WechatH5.AppID
|
||||
appSecret := r.c.WechatH5.AppSecret
|
||||
url := fmt.Sprintf("https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code", appID, appSecret, code)
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var data struct {
|
||||
Openid string `json:"openid"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &data); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if data.Openid == "" {
|
||||
return "", fmt.Errorf("openid为空")
|
||||
}
|
||||
return data.Openid, nil
|
||||
}
|
||||
|
||||
// GetWechatMiniOpenID 通过code获取微信小程序 OpenID
|
||||
func (r *VerificationService) GetWechatMiniOpenID(ctx context.Context, code string) (string, error) {
|
||||
appID := r.c.WechatMini.AppID
|
||||
appSecret := r.c.WechatMini.AppSecret
|
||||
url := fmt.Sprintf("https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code", appID, appSecret, code)
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var data struct {
|
||||
Openid string `json:"openid"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &data); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if data.Openid == "" {
|
||||
return "", fmt.Errorf("openid为空")
|
||||
}
|
||||
return data.Openid, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user