fix: update baidu translate api
This commit is contained in:
@@ -1,6 +1,10 @@
|
|||||||
import queryString from "query-string";
|
import queryString from "query-string";
|
||||||
import { getBdauth, setBdauth } from "../libs/storage";
|
import { getBdauth, setBdauth } from "../libs/storage";
|
||||||
import { URL_BAIDU_WEB, URL_BAIDU_TRAN } from "../config";
|
import {
|
||||||
|
URL_BAIDU_WEB,
|
||||||
|
URL_BAIDU_TRANSAPI_V2,
|
||||||
|
URL_BAIDU_TRANSAPI,
|
||||||
|
} from "../config";
|
||||||
import { fetchApi } from "../libs/fetch";
|
import { fetchApi } from "../libs/fetch";
|
||||||
|
|
||||||
/* eslint-disable */
|
/* eslint-disable */
|
||||||
@@ -203,7 +207,12 @@ const _bdAuth = () => {
|
|||||||
|
|
||||||
const bdAuth = _bdAuth();
|
const bdAuth = _bdAuth();
|
||||||
|
|
||||||
export const genBaidu = async ({ text, from, to }) => {
|
/**
|
||||||
|
* 失效作废
|
||||||
|
* @param {*} param0
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
export const genBaiduV2 = async ({ text, from, to }) => {
|
||||||
const { token, gtk } = await bdAuth();
|
const { token, gtk } = await bdAuth();
|
||||||
const sign = getSign(text, gtk);
|
const sign = getSign(text, gtk);
|
||||||
const data = {
|
const data = {
|
||||||
@@ -217,7 +226,7 @@ export const genBaidu = async ({ text, from, to }) => {
|
|||||||
ts: Date.now(),
|
ts: Date.now(),
|
||||||
};
|
};
|
||||||
|
|
||||||
const input = `${URL_BAIDU_TRAN}?from=${from}&to=${to}`;
|
const input = `${URL_BAIDU_TRANSAPI_V2}?from=${from}&to=${to}`;
|
||||||
const init = {
|
const init = {
|
||||||
headers: {
|
headers: {
|
||||||
"content-type": "application/x-www-form-urlencoded; charset=UTF-8",
|
"content-type": "application/x-www-form-urlencoded; charset=UTF-8",
|
||||||
@@ -228,3 +237,22 @@ export const genBaidu = async ({ text, from, to }) => {
|
|||||||
|
|
||||||
return [input, init];
|
return [input, init];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const genBaidu = async ({ text, from, to }) => {
|
||||||
|
const data = {
|
||||||
|
from,
|
||||||
|
to,
|
||||||
|
query: text,
|
||||||
|
source: "txt",
|
||||||
|
};
|
||||||
|
|
||||||
|
const init = {
|
||||||
|
headers: {
|
||||||
|
"content-type": "application/x-www-form-urlencoded; charset=UTF-8",
|
||||||
|
},
|
||||||
|
method: "POST",
|
||||||
|
body: queryString.stringify(data),
|
||||||
|
};
|
||||||
|
|
||||||
|
return [URL_BAIDU_TRANSAPI, init];
|
||||||
|
};
|
||||||
|
|||||||
@@ -125,11 +125,14 @@ export const apiTranslate = async ({
|
|||||||
return [trText, isSame];
|
return [trText, isSame];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 版本号一/二位升级,旧缓存失效
|
||||||
|
const [v1, v2] = process.env.REACT_APP_VERSION.split(".");
|
||||||
const cacheOpts = {
|
const cacheOpts = {
|
||||||
translator,
|
translator,
|
||||||
text,
|
text,
|
||||||
fromLang,
|
fromLang,
|
||||||
toLang,
|
toLang,
|
||||||
|
version: [v1, v2].join("."),
|
||||||
};
|
};
|
||||||
|
|
||||||
const transOpts = {
|
const transOpts = {
|
||||||
@@ -171,8 +174,15 @@ export const apiTranslate = async ({
|
|||||||
isSame = to === res.source_lang;
|
isSame = to === res.source_lang;
|
||||||
break;
|
break;
|
||||||
case OPT_TRANS_BAIDU:
|
case OPT_TRANS_BAIDU:
|
||||||
trText = res.trans_result?.data.map((item) => item.dst).join(" ");
|
// trText = res.trans_result?.data.map((item) => item.dst).join(" ");
|
||||||
isSame = res.trans_result?.to === res.trans_result?.from;
|
// isSame = res.trans_result?.to === res.trans_result?.from;
|
||||||
|
if (res.type === 1) {
|
||||||
|
trText = Object.keys(JSON.parse(res.result).content[0].mean[0].cont)[0];
|
||||||
|
isSame = to === res.from;
|
||||||
|
} else if (res.type === 2) {
|
||||||
|
trText = res.data[0].dst;
|
||||||
|
isSame = to === res.from;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case OPT_TRANS_TENCENT:
|
case OPT_TRANS_TENCENT:
|
||||||
trText = res.auto_translation;
|
trText = res.auto_translation;
|
||||||
|
|||||||
@@ -83,7 +83,8 @@ export const URL_MICROSOFT_TRAN =
|
|||||||
export const URL_MICROSOFT_AUTH = "https://edge.microsoft.com/translate/auth";
|
export const URL_MICROSOFT_AUTH = "https://edge.microsoft.com/translate/auth";
|
||||||
export const URL_BAIDU_LANGDETECT = "https://fanyi.baidu.com/langdetect";
|
export const URL_BAIDU_LANGDETECT = "https://fanyi.baidu.com/langdetect";
|
||||||
export const URL_BAIDU_WEB = "https://fanyi.baidu.com/";
|
export const URL_BAIDU_WEB = "https://fanyi.baidu.com/";
|
||||||
export const URL_BAIDU_TRAN = "https://fanyi.baidu.com/v2transapi";
|
export const URL_BAIDU_TRANSAPI = "https://fanyi.baidu.com/transapi";
|
||||||
|
export const URL_BAIDU_TRANSAPI_V2 = "https://fanyi.baidu.com/v2transapi";
|
||||||
export const URL_DEEPLFREE_TRAN = "https://www2.deepl.com/jsonrpc";
|
export const URL_DEEPLFREE_TRAN = "https://www2.deepl.com/jsonrpc";
|
||||||
export const URL_TENCENT_TRANSMART = "https://transmart.qq.com/api/imt";
|
export const URL_TENCENT_TRANSMART = "https://transmart.qq.com/api/imt";
|
||||||
|
|
||||||
@@ -101,11 +102,11 @@ export const OPT_TRANS_CUSTOMIZE = "Custom";
|
|||||||
export const OPT_TRANS_ALL = [
|
export const OPT_TRANS_ALL = [
|
||||||
OPT_TRANS_GOOGLE,
|
OPT_TRANS_GOOGLE,
|
||||||
OPT_TRANS_MICROSOFT,
|
OPT_TRANS_MICROSOFT,
|
||||||
|
OPT_TRANS_BAIDU,
|
||||||
|
OPT_TRANS_TENCENT,
|
||||||
OPT_TRANS_DEEPL,
|
OPT_TRANS_DEEPL,
|
||||||
OPT_TRANS_DEEPLFREE,
|
OPT_TRANS_DEEPLFREE,
|
||||||
OPT_TRANS_DEEPLX,
|
OPT_TRANS_DEEPLX,
|
||||||
OPT_TRANS_BAIDU,
|
|
||||||
OPT_TRANS_TENCENT,
|
|
||||||
OPT_TRANS_OPENAI,
|
OPT_TRANS_OPENAI,
|
||||||
OPT_TRANS_GEMINI,
|
OPT_TRANS_GEMINI,
|
||||||
OPT_TRANS_CLOUDFLAREAI,
|
OPT_TRANS_CLOUDFLAREAI,
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ function DictField({ word }) {
|
|||||||
fromLang: "en",
|
fromLang: "en",
|
||||||
toLang: "zh-CN",
|
toLang: "zh-CN",
|
||||||
});
|
});
|
||||||
setDictResult(dictRes[2].dict_result);
|
dictRes[2].type === 1 && setDictResult(JSON.parse(dictRes[2].result));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
setError(err.message);
|
setError(err.message);
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
@@ -1,16 +1,11 @@
|
|||||||
import Box from "@mui/material/Box";
|
import Box from "@mui/material/Box";
|
||||||
import Chip from "@mui/material/Chip";
|
|
||||||
import Stack from "@mui/material/Stack";
|
import Stack from "@mui/material/Stack";
|
||||||
import FavBtn from "./FavBtn";
|
import FavBtn from "./FavBtn";
|
||||||
import Typography from "@mui/material/Typography";
|
import Typography from "@mui/material/Typography";
|
||||||
|
|
||||||
const exchangeMap = {
|
const phonicMap = {
|
||||||
word_third: "第三人称单数",
|
en_phonic: "英",
|
||||||
word_ing: "现在分词",
|
us_phonic: "美",
|
||||||
word_done: "过去式",
|
|
||||||
word_past: "过去分词",
|
|
||||||
word_pl: "复数",
|
|
||||||
word_proto: "词源",
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function DictCont({ dictResult }) {
|
export default function DictCont({ dictResult }) {
|
||||||
@@ -26,40 +21,28 @@ export default function DictCont({ dictResult }) {
|
|||||||
alignItems="flex-start"
|
alignItems="flex-start"
|
||||||
>
|
>
|
||||||
<Typography variant="subtitle1" style={{ fontWeight: "bold" }}>
|
<Typography variant="subtitle1" style={{ fontWeight: "bold" }}>
|
||||||
{dictResult.simple_means?.word_name}
|
{dictResult.src}
|
||||||
</Typography>
|
</Typography>
|
||||||
<FavBtn word={dictResult.simple_means?.word_name} />
|
<FavBtn word={dictResult.src} />
|
||||||
</Stack>
|
</Stack>
|
||||||
|
|
||||||
{dictResult.simple_means?.symbols?.map(({ ph_en, ph_am, parts }, idx) => (
|
<Typography component="div">
|
||||||
<Typography key={idx} component="div">
|
<Typography>
|
||||||
{(ph_en || ph_am) && (
|
{dictResult.voice
|
||||||
<Typography>{`英 /${ph_en || ""}/ 美 /${ph_am || ""}/`}</Typography>
|
.map(Object.entries)
|
||||||
)}
|
.map((item) => item[0])
|
||||||
<ul style={{ margin: "0.5em 0" }}>
|
.map(([key, val]) => `${phonicMap[key] || key} ${val}`)
|
||||||
{parts.map(({ part, means }, idx) => (
|
.join(" ")}
|
||||||
<li key={idx}>
|
|
||||||
{part ? `[${part}] ${means.join("; ")}` : means.join("; ")}
|
|
||||||
</li>
|
|
||||||
))}
|
|
||||||
</ul>
|
|
||||||
</Typography>
|
</Typography>
|
||||||
))}
|
<ul style={{ margin: "0.5em 0" }}>
|
||||||
|
{dictResult.content[0].mean.map(({ pre, cont }, idx) => (
|
||||||
<Typography>
|
<li key={idx}>
|
||||||
{Object.entries(dictResult.simple_means?.exchange || {})
|
{pre && `[${pre}] `}
|
||||||
.map(([key, val]) => `${exchangeMap[key] || key}: ${val.join(", ")}`)
|
{Object.keys(cont).join("; ")}
|
||||||
.join("; ")}
|
</li>
|
||||||
</Typography>
|
|
||||||
|
|
||||||
<Stack direction="row" spacing={1} flexWrap="wrap" useFlexGap>
|
|
||||||
{Object.values(dictResult.simple_means?.tags || {})
|
|
||||||
.flat()
|
|
||||||
.filter((item) => item)
|
|
||||||
.map((item) => (
|
|
||||||
<Chip label={item} size="small" />
|
|
||||||
))}
|
))}
|
||||||
</Stack>
|
</ul>
|
||||||
|
</Typography>
|
||||||
</Box>
|
</Box>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
65
src/views/Selection/DictContV2.js
Normal file
65
src/views/Selection/DictContV2.js
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
import Box from "@mui/material/Box";
|
||||||
|
import Chip from "@mui/material/Chip";
|
||||||
|
import Stack from "@mui/material/Stack";
|
||||||
|
import FavBtn from "./FavBtn";
|
||||||
|
import Typography from "@mui/material/Typography";
|
||||||
|
|
||||||
|
const exchangeMap = {
|
||||||
|
word_third: "第三人称单数",
|
||||||
|
word_ing: "现在分词",
|
||||||
|
word_done: "过去式",
|
||||||
|
word_past: "过去分词",
|
||||||
|
word_pl: "复数",
|
||||||
|
word_proto: "词源",
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function DictCont({ dictResult }) {
|
||||||
|
if (!dictResult) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Box>
|
||||||
|
<Stack
|
||||||
|
direction="row"
|
||||||
|
justifyContent="space-between"
|
||||||
|
alignItems="flex-start"
|
||||||
|
>
|
||||||
|
<Typography variant="subtitle1" style={{ fontWeight: "bold" }}>
|
||||||
|
{dictResult.simple_means?.word_name}
|
||||||
|
</Typography>
|
||||||
|
<FavBtn word={dictResult.simple_means?.word_name} />
|
||||||
|
</Stack>
|
||||||
|
|
||||||
|
{dictResult.simple_means?.symbols?.map(({ ph_en, ph_am, parts }, idx) => (
|
||||||
|
<Typography key={idx} component="div">
|
||||||
|
{(ph_en || ph_am) && (
|
||||||
|
<Typography>{`英 /${ph_en || ""}/ 美 /${ph_am || ""}/`}</Typography>
|
||||||
|
)}
|
||||||
|
<ul style={{ margin: "0.5em 0" }}>
|
||||||
|
{parts.map(({ part, means }, idx) => (
|
||||||
|
<li key={idx}>
|
||||||
|
{part ? `[${part}] ${means.join("; ")}` : means.join("; ")}
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
</Typography>
|
||||||
|
))}
|
||||||
|
|
||||||
|
<Typography>
|
||||||
|
{Object.entries(dictResult.simple_means?.exchange || {})
|
||||||
|
.map(([key, val]) => `${exchangeMap[key] || key}: ${val.join(", ")}`)
|
||||||
|
.join("; ")}
|
||||||
|
</Typography>
|
||||||
|
|
||||||
|
<Stack direction="row" spacing={1} flexWrap="wrap" useFlexGap>
|
||||||
|
{Object.values(dictResult.simple_means?.tags || {})
|
||||||
|
.flat()
|
||||||
|
.filter((item) => item)
|
||||||
|
.map((item) => (
|
||||||
|
<Chip label={item} size="small" />
|
||||||
|
))}
|
||||||
|
</Stack>
|
||||||
|
</Box>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -59,7 +59,8 @@ export default function TranCont({
|
|||||||
// 词典
|
// 词典
|
||||||
if (isValidWord(text) && toLang.startsWith("zh")) {
|
if (isValidWord(text) && toLang.startsWith("zh")) {
|
||||||
if (fromLang === "en" && translator === OPT_TRANS_BAIDU) {
|
if (fromLang === "en" && translator === OPT_TRANS_BAIDU) {
|
||||||
setDictResult(tranRes[2].dict_result);
|
tranRes[2].type === 1 &&
|
||||||
|
setDictResult(JSON.parse(tranRes[2].result));
|
||||||
} else {
|
} else {
|
||||||
const dictRes = await apiTranslate({
|
const dictRes = await apiTranslate({
|
||||||
text,
|
text,
|
||||||
@@ -67,7 +68,8 @@ export default function TranCont({
|
|||||||
fromLang: "en",
|
fromLang: "en",
|
||||||
toLang: "zh-CN",
|
toLang: "zh-CN",
|
||||||
});
|
});
|
||||||
setDictResult(dictRes[2].dict_result);
|
dictRes[2].type === 1 &&
|
||||||
|
setDictResult(JSON.parse(dictRes[2].result));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
Reference in New Issue
Block a user