pull
This commit is contained in:
@@ -1,26 +1,27 @@
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"ycc-server/app/main/api/internal/config"
|
"ycc-server/app/main/api/internal/config"
|
||||||
tianyuanapi "ycc-server/app/main/api/internal/service/tianyuanapi_sdk"
|
tianyuanapi "ycc-server/app/main/api/internal/service/tianyuanapi_sdk"
|
||||||
|
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
)
|
)
|
||||||
|
|
||||||
type VerificationService struct {
|
type VerificationService struct {
|
||||||
c config.Config
|
c config.Config
|
||||||
tianyuanapi *tianyuanapi.Client
|
tianyuanapi *tianyuanapi.Client
|
||||||
apiRequestService *ApiRequestService
|
apiRequestService *ApiRequestService
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewVerificationService(c config.Config, tianyuanapi *tianyuanapi.Client, apiRequestService *ApiRequestService) *VerificationService {
|
func NewVerificationService(c config.Config, tianyuanapi *tianyuanapi.Client, apiRequestService *ApiRequestService) *VerificationService {
|
||||||
return &VerificationService{
|
return &VerificationService{
|
||||||
c: c,
|
c: c,
|
||||||
tianyuanapi: tianyuanapi,
|
tianyuanapi: tianyuanapi,
|
||||||
apiRequestService: apiRequestService,
|
apiRequestService: apiRequestService,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -156,52 +157,52 @@ func (r *VerificationService) ThreeFactorVerification(request ThreeFactorVerific
|
|||||||
|
|
||||||
// GetWechatH5OpenID 通过code获取微信H5 OpenID
|
// GetWechatH5OpenID 通过code获取微信H5 OpenID
|
||||||
func (r *VerificationService) GetWechatH5OpenID(ctx context.Context, code string) (string, error) {
|
func (r *VerificationService) GetWechatH5OpenID(ctx context.Context, code string) (string, error) {
|
||||||
appID := r.c.WechatH5.AppID
|
appID := r.c.WechatH5.AppID
|
||||||
appSecret := r.c.WechatH5.AppSecret
|
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)
|
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)
|
resp, err := http.Get(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
body, err := io.ReadAll(resp.Body)
|
body, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
var data struct {
|
var data struct {
|
||||||
Openid string `json:"openid"`
|
Openid string `json:"openid"`
|
||||||
}
|
}
|
||||||
if err := json.Unmarshal(body, &data); err != nil {
|
if err := json.Unmarshal(body, &data); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
if data.Openid == "" {
|
if data.Openid == "" {
|
||||||
return "", fmt.Errorf("openid为空")
|
return "", fmt.Errorf("openid为空")
|
||||||
}
|
}
|
||||||
return data.Openid, nil
|
return data.Openid, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetWechatMiniOpenID 通过code获取微信小程序 OpenID
|
// GetWechatMiniOpenID 通过code获取微信小程序 OpenID
|
||||||
func (r *VerificationService) GetWechatMiniOpenID(ctx context.Context, code string) (string, error) {
|
func (r *VerificationService) GetWechatMiniOpenID(ctx context.Context, code string) (string, error) {
|
||||||
appID := r.c.WechatMini.AppID
|
appID := r.c.WechatMini.AppID
|
||||||
appSecret := r.c.WechatMini.AppSecret
|
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)
|
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)
|
resp, err := http.Get(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
body, err := io.ReadAll(resp.Body)
|
body, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
var data struct {
|
var data struct {
|
||||||
Openid string `json:"openid"`
|
Openid string `json:"openid"`
|
||||||
}
|
}
|
||||||
if err := json.Unmarshal(body, &data); err != nil {
|
if err := json.Unmarshal(body, &data); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
if data.Openid == "" {
|
if data.Openid == "" {
|
||||||
return "", fmt.Errorf("openid为空")
|
return "", fmt.Errorf("openid为空")
|
||||||
}
|
}
|
||||||
return data.Openid, nil
|
return data.Openid, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user