index.js
7.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
//index.js
//获取应用实例
const app = getApp();
const typeList = app.globalData.getTypeList();
const selectedIndex = typeList.length > 1 ? app.globalData.getSelectedTypeIndex() : 0;
let audio = null;
let videoAd = null;
Page({
data: {
placeholder: '点击此处输入文字',
text: '',
typeList,
selectedIndex: String(selectedIndex) ? selectedIndex : 0
},
onLoad(){
wx.showShareMenu();
this.initVideoAd();
},
// 初始化激励视频广告
initVideoAd() {
// 若在开发者工具中无法预览广告,请切换开发者工具中的基础库版本
if (wx.createRewardedVideoAd) {
videoAd = wx.createRewardedVideoAd({
adUnitId: 'adunit-aedf0933f69de926'
});
videoAd.onLoad(() => {
console.log('激励视频广告加载成功');
});
videoAd.onError((err) => {
console.error('激励视频广告加载失败', err);
});
videoAd.onClose((res) => {
if (res && res.isEnded) {
// 用户看完广告,重置使用次数
this.resetTtsCount();
} else {
// 用户中途退出
wx.showToast({
title: '请看完广告获取次数',
icon: 'none'
});
}
});
}
},
// 显示激励视频广告
showVideoAd() {
if (videoAd) {
videoAd.show().catch(() => {
// 失败重试
videoAd.load()
.then(() => videoAd.show())
.catch(err => {
console.error('激励视频广告显示失败', err);
wx.showToast({
title: '广告加载失败,请稍后重试',
icon: 'none'
});
});
});
} else {
wx.showToast({
title: '广告功能暂不可用',
icon: 'none'
});
}
},
// 重置TTS使用次数
async resetTtsCount() {
wx.showLoading({
title: '正在重置次数...',
mask: true
});
try {
const result = await new Promise((resolve, reject) => {
app.request({
url: `${app.globalData.domain}/open-api/wx330e54aa6000516d/tts-count-reset`,
success(res) {
resolve(res.data);
},
fail(err) {
reject(err);
}
});
});
wx.hideLoading();
if (result.code === 2000000) {
wx.showToast({
title: '获得10次使用机会!',
icon: 'success'
});
} else {
wx.showToast({
title: result.msg || '重置失败',
icon: 'none'
});
}
} catch (error) {
wx.hideLoading();
wx.showToast({
title: '网络错误',
icon: 'none'
});
}
},
// 显示次数用完弹窗
showLimitDialog() {
wx.showModal({
title: '使用次数已用完',
content: '今日免费次数已用完,观看广告可获得额外10次使用机会',
confirmText: '观看广告',
cancelText: '明日再试',
success: (res) => {
if (res.confirm) {
this.showVideoAd();
}
}
});
},
inputText(e){
this.data.text = e.detail.value;
},
async play(){
const text = this.data.text || this.data.placeholder;
const selectedType = this.data.typeList[this.data.selectedIndex];
const type = selectedType.desc;
this.resetAudio();
audio = wx.createInnerAudioContext({useWebAudioImplement: true});
audio.autoplay = true;
// 检查是否播放过
const audioList = app.globalData.getAudioList();
let played = audioList.find(v => v.text === text && v.type === type);
// 播放过,直接使用本地文件,无需检查合规性
if(played){
played.time = Date.now();
app.globalData.setAudioList('delete', played.path);
app.globalData.setAudioList('add', played);
// 播放失败要移除缓存内容
audio.onError(err => {
app.globalData.setAudioList('delete', played.path);
wx.showModal({
title: '提示',
content: '该语音播放过但缓存失效,点击确定重新播放',
showCancel: false,
success: res => {
this.play();
}
});
});
return audio.src = played.path;
}
// 未播放过,先检查文本是否合规
wx.showLoading({
title: '正在合成语音...',
mask: true
});
const result = await new Promise((resolve, reject) => {
app.request({
url: `${app.globalData.domain}/open-api/mp/msg-sec-check`,
data: {
appid: 'wx330e54aa6000516d',
content: text,
openid: wx.getStorageSync('openid')
},
success(res){
resolve(JSON.parse(res.data?.data)?.result);
},
fail(err){
reject(err);
}
});
});
// 不合规结束
if(result.suggest !== 'pass'){
wx.hideLoading();
return wx.showToast({
title: '内容不合规',
icon: 'error',
mask: true,
duration: 2000
});
}
// 未播放过,调用TTS接口生成语音
try {
const url = await selectedType.createUrl(text);
// 下载并播放
wx.downloadFile({
url,
success (res) {
wx.hideLoading();
audio.src = res.tempFilePath;
app.globalData.setAudioList('add', {
// 音频文本
text,
// 音频音效
type,
// 音频网络地址
url,
// 音频本地地址
path: res.tempFilePath,
// 音频创建时间
time: Date.now()
});
},
fail(err) {
console.log(err);
wx.hideLoading();
wx.showToast({
title: '下载失败',
icon: 'none'
});
}
});
} catch (error) {
wx.hideLoading();
// TTS接口调用失败的错误已经在app.js中处理了
}
},
clear(){
this.resetAudio();
this.setData({
text: ''
});
},
resetAudio(){
if(audio){
audio.destroy();
audio = null;
}
},
selectIndex(e){
const { index } = e.target.dataset;
this.setData({
selectedIndex: index
}, () => {
this.play();
});
app.globalData.setSelectedTypeIndex(index);
},
gotoAudioList(){
wx.navigateTo({
url: '/pages/result/result'
});
}
});