From 906191f76e79e942ac6ebd27dcb32d247588d059 Mon Sep 17 00:00:00 2001 From: xwenliang Date: Sun, 6 Nov 2022 04:48:55 +0800 Subject: [PATCH] feat: add entry --- README.md | 9 +++++++-- app.js | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------- pages/index/index.js | 81 +++++++++++++++++++++++++++++++++------------------------------------------------ pages/index/index.wxml | 1 + pages/result/result.js | 7 ++++--- 5 files changed, 121 insertions(+), 72 deletions(-) diff --git a/README.md b/README.md index 527d6d5..5903924 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,10 @@ # 微信小程序 - 语音助手 -## todo +### todo -1. 历史记录 \ No newline at end of file +- [x] 内容安全问题处理 + +- [x] 下载音频需求 + - 无直接下载接口,可通过 webview 间接实现 + - 个人类小程序不支持 web-view + - wx.shareFileMessage 可以直接分享文件 diff --git a/app.js b/app.js index 99d5e94..2a3db26 100644 --- a/app.js +++ b/app.js @@ -1,27 +1,84 @@ //app.js App({ - onLaunch: function() { + onLaunch () { }, - getUserInfo: function(cb) { - var that = this - if (this.globalData.userInfo) { - typeof cb == "function" && cb(this.globalData.userInfo) - } else { - //调用登录接口 - wx.login({ - success: function() { - wx.getUserInfo({ - success: function(res) { - that.globalData.userInfo = res.userInfo - typeof cb == "function" && cb(that.globalData.userInfo) - } - }) + globalData: { + getTypeList(){ + return [ + { + desc: '男声一', + createUrl(text){ + return `https://fanyi.sogou.com/reventondc/synthesis?text=${text}&speed=1&lang=zh-CHS&from=translateweb&speaker=1` + } + }, + { + desc: '男声二', + createUrl(text){ + return `https://fanyi.sogou.com/reventondc/synthesis?text=${text}&speed=1&lang=zh-CHS&from=translateweb&speaker=4` + } + }, + { + desc: '女声一', + createUrl(text){ + return `https://fanyi.sogou.com/reventondc/synthesis?text=${text}&speed=1&lang=zh-CHS&from=translateweb&speaker=2` + } + }, + { + desc: '女声二', + createUrl(text){ + return `https://fanyi.sogou.com/reventondc/synthesis?text=${text}&speed=1&lang=zh-CHS&from=translateweb&speaker=3` + } + }, + { + desc: '女声三', + createUrl(text){ + return `https://fanyi.sogou.com/reventondc/synthesis?text=${text}&speed=1&lang=zh-CHS&from=translateweb&speaker=5` + } + }, + { + desc: '女声四', + createUrl(text){ + return `https://fanyi.sogou.com/reventondc/synthesis?text=${text}&speed=1&lang=zh-CHS&from=translateweb&speaker=6` + } + }, + { + desc: '女声五', + createUrl(text){ + return `https://tts.youdao.com/fanyivoice?word=${text}&le=zh&keyfrom=speaker-target` + } } - }) + ]; + }, + getSelectedTypeIndex(){ + const storageKey = 'audioTypeselectedIndex'; + return wx.getStorageSync(storageKey); + }, + setSelectedTypeIndex(index){ + const storageKey = 'audioTypeselectedIndex'; + wx.setStorageSync(storageKey, index); + return index; + }, + getAudioList(){ + const storageKey = 'audioList'; + return wx.getStorageSync(storageKey) || []; + }, + /** + * @param {(add|delete)} action - 增或删 + * @param {object|number} [param] - 增加时是 audio 描述,删除时是索引 + */ + setAudioList(action, param){ + const storageKey = 'audioList'; + const list = this.getAudioList() || []; + if(action === 'add' && param.constructor === Object){ + list.push(param); + wx.setStorageSync(storageKey, list); + } + if(action === 'delete' && String(param)){ + list.splice(param, 1); + wx.setStorageSync(storageKey, list); + } + return list; } - }, - globalData: { - userInfo: null } }); diff --git a/pages/index/index.js b/pages/index/index.js index fb439ae..354802d 100644 --- a/pages/index/index.js +++ b/pages/index/index.js @@ -1,52 +1,8 @@ //index.js //获取应用实例 const app = getApp(); -const storageKey = 'audioTypeselectedIndex'; -const selectedIndex = wx.getStorageSync(storageKey); -const type = [ - { - desc: '男声一', - createUrl(text){ - return `https://fanyi.sogou.com/reventondc/synthesis?text=${text}&speed=1&lang=zh-CHS&from=translateweb&speaker=1` - } - }, - { - desc: '男声二', - createUrl(text){ - return `https://fanyi.sogou.com/reventondc/synthesis?text=${text}&speed=1&lang=zh-CHS&from=translateweb&speaker=4` - } - }, - { - desc: '女声一', - createUrl(text){ - return `https://fanyi.sogou.com/reventondc/synthesis?text=${text}&speed=1&lang=zh-CHS&from=translateweb&speaker=2` - } - }, - { - desc: '女声二', - createUrl(text){ - return `https://fanyi.sogou.com/reventondc/synthesis?text=${text}&speed=1&lang=zh-CHS&from=translateweb&speaker=3` - } - }, - { - desc: '女声三', - createUrl(text){ - return `https://fanyi.sogou.com/reventondc/synthesis?text=${text}&speed=1&lang=zh-CHS&from=translateweb&speaker=5` - } - }, - { - desc: '女声四', - createUrl(text){ - return `https://fanyi.sogou.com/reventondc/synthesis?text=${text}&speed=1&lang=zh-CHS&from=translateweb&speaker=6` - } - }, - { - desc: '女声五', - createUrl(text){ - return `https://tts.youdao.com/fanyivoice?word=${text}&le=zh&keyfrom=speaker-target` - } - } -]; +const selectedIndex = app.globalData.getSelectedTypeIndex(); +const type = app.globalData.getTypeList(); let audio = null; @@ -65,10 +21,34 @@ Page({ }, play(){ const text = this.data.text || this.data.placeholder; + const url = this.data.type[this.data.selectedIndex].createUrl(text); this.resetAudio(); audio = wx.createInnerAudioContext({useWebAudioImplement: true}); audio.autoplay = true; - audio.src = this.data.type[this.data.selectedIndex].createUrl(text); + // 检查是否播放过 + const audioList = app.globalData.getAudioList(); + const playedIndex = audioList.findIndex(v => v.url === url); + // 播放过,使用本地文件减少请求 + if(playedIndex > -1){ + return audio.src = audioList[playedIndex].path; + } + // 未播放过,先下载再播放 + wx.downloadFile({ + url, + success (res) { + audio.src = res.tempFilePath; + app.globalData.setAudioList('add', { + // 音频文本 + text, + // 音频网络地址 + url, + // 音频本地地址 + path: res.tempFilePath, + // 音频创建时间 + time: Date.now() + }); + } + }); }, clear(){ this.resetAudio(); @@ -89,6 +69,11 @@ Page({ }, () => { this.play(); }); - wx.setStorageSync(storageKey, index); + app.globalData.setSelectedTypeIndex(index); + }, + gotoAudioList(){ + wx.navigateTo({ + url: '/pages/result/result' + }); } }); diff --git a/pages/index/index.wxml b/pages/index/index.wxml index cf348b2..8690367 100644 --- a/pages/index/index.wxml +++ b/pages/index/index.wxml @@ -13,6 +13,7 @@ + diff --git a/pages/result/result.js b/pages/result/result.js index b30df3e..620d748 100644 --- a/pages/result/result.js +++ b/pages/result/result.js @@ -2,7 +2,6 @@ import { convertDate } from '../../utils/util.js'; //获取应用实例 const app = getApp(); -const audioList = app.globalData.getAudioList(); const type = app.globalData.getTypeList(); let audio = null; @@ -17,10 +16,12 @@ let formateDate = function (audioList) { Page({ data: { - audioList: formateDate(audioList) + audioList: formateDate(app.globalData.getAudioList()) }, onLoad (options) { - + this.setData({ + audioList: formateDate(app.globalData.getAudioList()) + }); }, play(e){ const { index } = e.target.dataset; -- libgit2 0.22.2