Skip to content

Commit

Permalink
perf: 全部改用tauri fetch 发送请求
Browse files Browse the repository at this point in the history
  • Loading branch information
Pylogmon committed Mar 10, 2023
1 parent 3642369 commit 9200545
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 13 deletions.
4 changes: 1 addition & 3 deletions src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@
"http": {
"all": true,
"scope": [
"https://fanyi-api.baidu.com/*",
"https://fanyi.youdao.com/*",
"https://tmt.tencentcloudapi.com/*"
"https://**"
]
},
"notification": {
Expand Down
3 changes: 2 additions & 1 deletion src/interfaces/baidu.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ export async function translate(text, from, to) {
appid: appid,
salt: salt,
sign: sign
}
},
timeout: 5
})
let target = ""
const { trans_result } = res.data;
Expand Down
16 changes: 12 additions & 4 deletions src/interfaces/caiyun.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios from "axios"
import { fetch } from '@tauri-apps/api/http';
import { get } from "../global/config"

export const info = {
Expand All @@ -16,7 +16,7 @@ export const info = {

export async function translate(text, from, to) {
const { supportLanguage } = info;
const url = "http://api.interpreter.caiyunai.com/v1/translator"
const url = "https://api.interpreter.caiyunai.com/v1/translator"
const token = get('caiyun_token', '')
if (token == "") {
return '请先配置token'
Expand All @@ -36,10 +36,18 @@ export async function translate(text, from, to) {
"content-type": "application/json",
"x-authorization": "token " + token,
}
const res = await axios.post(url, body, {

const res = await fetch(url, {
method: 'POST',
headers: headers,
timeout: 30000,
body: {
type: 'Text',
payload: JSON.stringify(body)
},
timeout: 5
})

console.log(res)
const { target } = res.data;

return target
Expand Down
11 changes: 8 additions & 3 deletions src/interfaces/openai.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios from 'axios';
import { fetch } from '@tauri-apps/api/http';
import { get } from '../global/config';

export const info = {
Expand Down Expand Up @@ -51,9 +51,14 @@ export async function translate(text, from, to) {
]
};

const res = await axios.post(`https://${domain}/v1/chat/completions`, body, {
const res = await fetch(`https://${domain}/v1/chat/completions`, {
method: 'POST',
headers: headers,
timeout: 30000,
body: {
type: 'Json',
payload: body
},
timeout: 10
})

const { choices } = res.data;
Expand Down
3 changes: 2 additions & 1 deletion src/interfaces/tencent.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ export async function translate(text, from, to) {
body: {
type: "Text",
payload: payload
}
},
timeout: 5
})
let { Response } = res.data;
return Response['TargetText']
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/youdao_free.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export async function translate(text, from, to) {
}
let res = await fetch('https://fanyi.youdao.com/translate', {
method: 'GET',
timeout: 30,
timeout: 5,
query: {
"doctype": "json",
"type": "AUTO",
Expand Down

0 comments on commit 9200545

Please sign in to comment.