|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+import axios from 'axios';
|
|
|
2
|
+import crypto from 'crypto';
|
|
|
3
|
+
|
|
|
4
|
+// 腾讯云翻译API配置
|
|
|
5
|
+const TENCENT_TRANSLATE_API = {
|
|
|
6
|
+ url: 'https://tmt.tencentcloudapi.com/',
|
|
|
7
|
+ secretId: 'AKID2GI7i7JH2av3yf9RVfkeMn6C5lMk0591',
|
|
|
8
|
+ secretKey: 'qUzMXYMxKUQ9eFnBNM9zgCZlmbHm6DDC',
|
|
|
9
|
+ region: 'ap-shanghai',
|
|
|
10
|
+ projectId: 0,
|
|
|
11
|
+};
|
|
|
12
|
+
|
|
|
13
|
+// 生成腾讯云API签名
|
|
|
14
|
+function generateTencentCloudSignature(secretKey, timestamp, date, payload) {
|
|
|
15
|
+ // 步骤1: 拼接规范请求串
|
|
|
16
|
+ const httpRequestMethod = 'POST';
|
|
|
17
|
+ const canonicalUri = '/';
|
|
|
18
|
+ const canonicalQueryString = '';
|
|
|
19
|
+ const contentType = 'application/json';
|
|
|
20
|
+ const canonicalHeaders = `content-type:${contentType}\nhost:tmt.tencentcloudapi.com\n`;
|
|
|
21
|
+ const signedHeaders = 'content-type;host';
|
|
|
22
|
+ const hashedRequestPayload = crypto.createHash('sha256').update(JSON.stringify(payload)).digest('hex');
|
|
|
23
|
+ const canonicalRequest = `${httpRequestMethod}\n${canonicalUri}\n${canonicalQueryString}\n${canonicalHeaders}\n${signedHeaders}\n${hashedRequestPayload}`;
|
|
|
24
|
+
|
|
|
25
|
+ // 步骤2: 拼接待签名字符串
|
|
|
26
|
+ const algorithm = 'TC3-HMAC-SHA256';
|
|
|
27
|
+ const credentialScope = `${date}/tmt/tc3_request`;
|
|
|
28
|
+ const hashedCanonicalRequest = crypto.createHash('sha256').update(canonicalRequest).digest('hex');
|
|
|
29
|
+ const stringToSign = `${algorithm}\n${timestamp}\n${credentialScope}\n${hashedCanonicalRequest}`;
|
|
|
30
|
+
|
|
|
31
|
+ // 步骤3: 计算签名
|
|
|
32
|
+ const secretDate = crypto.createHmac('sha256', `TC3${secretKey}`).update(date).digest();
|
|
|
33
|
+ const secretService = crypto.createHmac('sha256', secretDate).update('tmt').digest();
|
|
|
34
|
+ const secretSigning = crypto.createHmac('sha256', secretService).update('tc3_request').digest();
|
|
|
35
|
+ const signature = crypto.createHmac('sha256', secretSigning).update(stringToSign).digest('hex');
|
|
|
36
|
+
|
|
|
37
|
+ return signature;
|
|
|
38
|
+}
|
|
|
39
|
+
|
|
|
40
|
+// 腾讯云翻译API
|
|
|
41
|
+async function translateText(text, sourceLang = 'en', targetLang = 'zh') {
|
|
|
42
|
+ try {
|
|
|
43
|
+ const timestamp = Math.floor(Date.now() / 1000);
|
|
|
44
|
+ const date = new Date().toISOString().split('T')[0];
|
|
|
45
|
+
|
|
|
46
|
+ const payload = {
|
|
|
47
|
+ SourceText: text,
|
|
|
48
|
+ Source: sourceLang,
|
|
|
49
|
+ Target: targetLang,
|
|
|
50
|
+ ProjectId: TENCENT_TRANSLATE_API.projectId,
|
|
|
51
|
+ };
|
|
|
52
|
+
|
|
|
53
|
+ const signature = generateTencentCloudSignature(
|
|
|
54
|
+ TENCENT_TRANSLATE_API.secretKey,
|
|
|
55
|
+ timestamp,
|
|
|
56
|
+ date,
|
|
|
57
|
+ payload
|
|
|
58
|
+ );
|
|
|
59
|
+
|
|
|
60
|
+ const authorization = `TC3-HMAC-SHA256 Credential=${TENCENT_TRANSLATE_API.secretId}/${date}/tmt/tc3_request, SignedHeaders=content-type;host, Signature=${signature}`;
|
|
|
61
|
+
|
|
|
62
|
+ const response = await axios.post(
|
|
|
63
|
+ TENCENT_TRANSLATE_API.url,
|
|
|
64
|
+ payload,
|
|
|
65
|
+ {
|
|
|
66
|
+ headers: {
|
|
|
67
|
+ 'Content-Type': 'application/json',
|
|
|
68
|
+ 'Host': 'tmt.tencentcloudapi.com',
|
|
|
69
|
+ 'X-TC-Action': 'TextTranslate',
|
|
|
70
|
+ 'X-TC-Version': '2018-03-21',
|
|
|
71
|
+ 'X-TC-Region': TENCENT_TRANSLATE_API.region,
|
|
|
72
|
+ 'X-TC-Timestamp': timestamp,
|
|
|
73
|
+ 'Authorization': authorization,
|
|
|
74
|
+ },
|
|
|
75
|
+ }
|
|
|
76
|
+ );
|
|
|
77
|
+
|
|
|
78
|
+ return {
|
|
|
79
|
+ success: true,
|
|
|
80
|
+ data: response.data.Response.TargetText,
|
|
|
81
|
+ };
|
|
|
82
|
+ } catch (error) {
|
|
|
83
|
+ console.error('翻译失败:', error.message);
|
|
|
84
|
+ if (error.response) {
|
|
|
85
|
+ console.error('错误详情:', JSON.stringify(error.response.data));
|
|
|
86
|
+ }
|
|
|
87
|
+ return {
|
|
|
88
|
+ success: false,
|
|
|
89
|
+ error: error.message,
|
|
|
90
|
+ originalText: text,
|
|
|
91
|
+ };
|
|
|
92
|
+ }
|
|
|
93
|
+}
|
|
|
94
|
+
|
|
|
95
|
+// 导出API
|
|
|
96
|
+const machineTranslationAPI = {
|
|
|
97
|
+ translateText,
|
|
|
98
|
+};
|
|
|
99
|
+
|
|
|
100
|
+export default machineTranslationAPI;
|