|
@@ -0,0 +1,89 @@
|
|
|
+package xiaoice
|
|
|
+
|
|
|
+import (
|
|
|
+ "context"
|
|
|
+ "encoding/json"
|
|
|
+ "fmt"
|
|
|
+ "github.com/zeromicro/go-zero/core/errorx"
|
|
|
+ "io"
|
|
|
+ "net/http"
|
|
|
+ "net/url"
|
|
|
+ "strconv"
|
|
|
+
|
|
|
+ "wechat-api/internal/svc"
|
|
|
+ "wechat-api/internal/types"
|
|
|
+
|
|
|
+ "github.com/zeromicro/go-zero/core/logx"
|
|
|
+)
|
|
|
+
|
|
|
+type SignatureGenLogic struct {
|
|
|
+ logx.Logger
|
|
|
+ ctx context.Context
|
|
|
+ svcCtx *svc.ServiceContext
|
|
|
+}
|
|
|
+
|
|
|
+func NewSignatureGenLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SignatureGenLogic {
|
|
|
+ return &SignatureGenLogic{
|
|
|
+ Logger: logx.WithContext(ctx),
|
|
|
+ ctx: ctx,
|
|
|
+ svcCtx: svcCtx}
|
|
|
+}
|
|
|
+
|
|
|
+func (l *SignatureGenLogic) SignatureGen() (resp *types.SignatureResp, err error) {
|
|
|
+
|
|
|
+ key := l.svcCtx.Config.Xiaoice.SubscriptionKey
|
|
|
+ baseURL, err := url.Parse("https://interactive-virtualhuman.xiaoice.com/openapi/signature/gen")
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ params := url.Values{}
|
|
|
+
|
|
|
+ params.Add("subscription-key", key)
|
|
|
+ params.Add("effectiveDurationMilliseconds", strconv.Itoa(24*60*60*1000))
|
|
|
+ baseURL.RawQuery = params.Encode()
|
|
|
+
|
|
|
+
|
|
|
+ req, err := http.NewRequest("GET", baseURL.String(), nil)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ client := &http.Client{}
|
|
|
+ response, err := client.Do(req)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ defer func(Body io.ReadCloser) {
|
|
|
+ err := Body.Close()
|
|
|
+ if err != nil {
|
|
|
+ l.Error("获取小冰 AuthToken 失败: %v", err)
|
|
|
+ }
|
|
|
+ }(response.Body)
|
|
|
+
|
|
|
+
|
|
|
+ body, err := io.ReadAll(response.Body)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if response.StatusCode != http.StatusOK {
|
|
|
+
|
|
|
+ return nil, errorx.NewDefaultError(fmt.Sprintf("获取小冰 AuthToken 失败:%d,响应: %s", response.StatusCode, string(body)))
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ var responseMap types.XiaoiceSignatureResp
|
|
|
+ if err := json.Unmarshal(body, &responseMap); err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ fmt.Printf("响应: %s\n", string(body))
|
|
|
+ return &types.SignatureResp{Data: &responseMap.Data}, nil
|
|
|
+}
|