- 将 agreement.js、no-auth-plugin.js、processEntry.js、https.js、 menuConfig.js、voiceUtil.js 文件重命名为对应的 .ts 扩展名 - 在 agreement.ts 中引入配置并动态设置标题 - 在 no-auth-plugin.ts 中使用工具函数替代硬编码跳转 - 为 processEntry.ts 添加类型注解并重构代码结构 - 为 https.ts 添加类型定义并优化错误处理 - 创建 menuConfig.ts 并迁移导航配置 - 为 voiceUtil.ts 添加类型注解
76 lines
2.0 KiB
TypeScript
76 lines
2.0 KiB
TypeScript
import { post, urlAddBaseUrl } from "./https";
|
|
|
|
const playVoice = (src: string, text: string) => {
|
|
const innerAudioContext = wx.createInnerAudioContext({
|
|
useWebAudioImplement: true,
|
|
});
|
|
innerAudioContext.src = src;
|
|
innerAudioContext.play();
|
|
innerAudioContext.onError(() => {
|
|
innerAudioContext.destroy();
|
|
console.log("Audio Error");
|
|
// wx.removeStorageSync(text);
|
|
});
|
|
innerAudioContext.onEnded(() => {
|
|
innerAudioContext.destroy();
|
|
console.log("Audio End");
|
|
});
|
|
};
|
|
|
|
export const voiceRequest = (text: string, cacheVoice?: boolean) => {
|
|
// if (cacheVoice) {
|
|
// const data = wx.getStorageSync(text);
|
|
// console.log(data);
|
|
// if (data) {
|
|
// console.log("有 base64");
|
|
// const fs = wx.getFileSystemManager();
|
|
// //随机定义路径名称
|
|
// var times = new Date().getTime();
|
|
// var codeimg = wx.env.USER_DATA_PATH + "/" + times + ".mp3";
|
|
// //将base64图片写入
|
|
// fs.writeFile({
|
|
// filePath: codeimg,
|
|
// data: data,
|
|
// encoding: "base64",
|
|
// success: (res) => {
|
|
// console.log(res);
|
|
// playVoice(codeimg, text);
|
|
// },
|
|
// fail: (res) => {
|
|
// console.log(res);
|
|
// },
|
|
// });
|
|
// return;
|
|
// }
|
|
// }
|
|
post(
|
|
"/Tools/voice",
|
|
{
|
|
txt: text,
|
|
per: 3,
|
|
spd: 5,
|
|
pit: 5,
|
|
vol: 9,
|
|
},
|
|
{ showLoading: false }
|
|
).then((res: any) => {
|
|
if (res.err_code == 0) {
|
|
playVoice(urlAddBaseUrl(res.data), text);
|
|
// if (cacheVoice) {
|
|
// wx.request({
|
|
// url: urlAddBaseUrl(res.data),
|
|
// responseType: "arraybuffer",
|
|
// success(res) {
|
|
// const base64 = base64js.fromByteArray(new Uint8Array(res.data));
|
|
// try {
|
|
// wx.setStorageSync(text, base64);
|
|
// } catch (_) {
|
|
// console.log("缓存错误");
|
|
// }
|
|
// },
|
|
// });
|
|
// }
|
|
}
|
|
});
|
|
};
|