Browse Source

增加小冰签名接口

boweniac 3 months ago
parent
commit
c124dd3ff6

+ 2 - 1
desc/all.api

@@ -32,4 +32,5 @@ import "./wechat/avatar.api"
 import "./wechat/aliyun_avatar.api"
 import "./wechat/workphone.api"
 import "./wechat/usage_detail.api"
-import "./wechat/usage_total.api"
+import "./wechat/usage_total.api"
+import "./wechat/xiaoice.api"

+ 20 - 0
desc/wechat/xiaoice.api

@@ -0,0 +1,20 @@
+import "../base.api"
+
+type (
+    SignatureResp {
+        BaseDataInfo
+
+        // Sts information | Sts 数据
+        Data *string `json:"data"`
+    }
+)
+
+@server(
+	group: xiaoice
+)
+
+service Wechat {
+	// get xiaoice signature | 获取小冰签名
+    @handler signatureGen
+    get /api/xiaoice/signature () returns (SignatureResp)
+}

+ 1 - 0
internal/config/config.go

@@ -20,4 +20,5 @@ type Config struct {
 	Fastgpt            types.Fastgpt
 	Aliyun             types.Aliyun
 	CoreRpc            zrpc.RpcClientConf
+	Xiaoice            types.Xiaoice
 }

+ 11 - 0
internal/handler/routes.go

@@ -39,6 +39,7 @@ import (
 	wxcard "wechat-api/internal/handler/wxcard"
 	wxcarduser "wechat-api/internal/handler/wxcarduser"
 	wxcardvisit "wechat-api/internal/handler/wxcardvisit"
+	xiaoice "wechat-api/internal/handler/xiaoice"
 	"wechat-api/internal/svc"
 
 	"github.com/zeromicro/go-zero/rest"
@@ -1471,4 +1472,14 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
 		),
 		rest.WithJwt(serverCtx.Config.Auth.AccessSecret),
 	)
+
+	server.AddRoutes(
+		[]rest.Route{
+			{
+				Method:  http.MethodGet,
+				Path:    "/api/xiaoice/signature",
+				Handler: xiaoice.SignatureGenHandler(serverCtx),
+			},
+		},
+	)
 }

+ 31 - 0
internal/handler/xiaoice/signature_gen_handler.go

@@ -0,0 +1,31 @@
+package xiaoice
+
+import (
+	"net/http"
+
+	"github.com/zeromicro/go-zero/rest/httpx"
+
+	"wechat-api/internal/logic/xiaoice"
+	"wechat-api/internal/svc"
+)
+
+// swagger:route post /api/xiaoice/signature xiaoice SignatureGen
+//
+// get xiaoice signature | 获取小冰签名
+//
+// get xiaoice signature | 获取小冰签名
+//
+// Responses:
+//  200: SignatureResp
+
+func SignatureGenHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		l := xiaoice.NewSignatureGenLogic(r.Context(), svcCtx)
+		resp, err := l.SignatureGen()
+		if err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+		} else {
+			httpx.OkJsonCtx(r.Context(), w, resp)
+		}
+	}
+}

+ 89 - 0
internal/logic/xiaoice/signature_gen_logic.go

@@ -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) {
+	// 构建请求URL
+	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{}
+	// 如果需要设置 token 有效期,请取消下面注释并设置时间(以毫秒为单位)
+	params.Add("subscription-key", key)
+	params.Add("effectiveDurationMilliseconds", strconv.Itoa(24*60*60*1000))
+	baseURL.RawQuery = params.Encode()
+
+	// 创建 HTTP 请求
+	req, err := http.NewRequest("GET", baseURL.String(), nil)
+	if err != nil {
+		return nil, err
+	}
+
+	// 添加必要的Header信息
+	//req.Header.Add("subscription-key", subscriptionKey)
+
+	// 创建HTTP客户端并执行请求
+	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 {
+		//log.Fatalf("请求失败,状态码:%d,响应: %s", response.StatusCode, string(body))
+		return nil, errorx.NewDefaultError(fmt.Sprintf("获取小冰 AuthToken 失败:%d,响应: %s", response.StatusCode, string(body)))
+	}
+
+	// 解析 JSON 响应
+	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
+}

+ 7 - 0
internal/types/types.go

@@ -2890,3 +2890,10 @@ type UsageTotalListReq struct {
 	// 租户id
 	OrganizationId *uint64 `json:"organizationId,optional"`
 }
+
+// swagger:model SignatureResp
+type SignatureResp struct {
+	BaseDataInfo
+	// Sts information | Sts 数据
+	Data *string `json:"data"`
+}

+ 11 - 0
internal/types/xiaoice.go

@@ -0,0 +1,11 @@
+package types
+
+type Xiaoice struct {
+	SubscriptionKey string
+}
+
+type XiaoiceSignatureResp struct {
+	Code int64  `json:"code"`
+	Data string `json:"data"`
+	Msg  string `json:"msg"`
+}