app.js
6.98 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
// zooble@2025-12-14
const { createRequest } = require('./utils/request.js');
App({
globalData: {
// domain: 'http://localhost:3003',
domain: 'https://api.xwenliang.cn',
appid: 'wx330e54aa6000516d',
cid: '',
safeAreaTop: 0,
safeAreaBottom: 0,
statusBarHeight: 0,
navBarHeight: 0,
totalNavHeight: 0, // 导航栏总高度(状态栏 + 导航栏)
tabBarHeight: 56,
totalTabBarHeight: 0, // tab总高度(tab + 底部安全区)
contentHeight: 0, // 内容区域高度(全屏高度 - 导航栏总高度 - 底部安全区)
contentWithoutTabbarHeight: 0, // 内容区域高度 - tab 高度
getTypeList(){
const globalData = this;
return [
{
desc: 'Chelsie(女)',
voice: 'Chelsie',
async createUrl(text){
return await globalData.requestTTS(text, 'Chelsie');
}
},
{
desc: 'Cherry(女)',
voice: 'Cherry',
async createUrl(text){
return await globalData.requestTTS(text, 'Cherry');
}
},
{
desc: 'Ethan(男)',
voice: 'Ethan',
async createUrl(text){
return await globalData.requestTTS(text, 'Ethan');
}
},
{
desc: 'Serena(女)',
voice: 'Serena',
async createUrl(text){
return await globalData.requestTTS(text, 'Serena');
}
},
{
desc: 'Dylan(北京话-男)',
voice: 'Dylan',
async createUrl(text){
return await globalData.requestTTS(text, 'Dylan');
}
},
{
desc: 'Jada(吴语-女)',
voice: 'Jada',
async createUrl(text){
return await globalData.requestTTS(text, 'Jada');
}
},
{
desc: 'Sunny(四川话-女)',
voice: 'Sunny',
async createUrl(text){
return await globalData.requestTTS(text, 'Sunny');
}
}
];
},
onLaunch(option){
const { query } = option;
// 获取渠道值
this.globalData.cid = query.cid || '';
// 挂载 request
const { domain, appid, cid } = this.globalData;
this.request = createRequest({ domain, appid, cid });
// 获取系统信息,计算安全区域
const windowInfo = wx.getWindowInfo ? wx.getWindowInfo() : wx.getSystemInfoSync();
const { statusBarHeight, safeArea, windowHeight, screenHeight } = windowInfo;
this.globalData.statusBarHeight = statusBarHeight;
this.globalData.safeAreaTop = safeArea.top;
this.globalData.safeAreaBottom = screenHeight - safeArea.bottom;
// 右上角胶囊位置信息
const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
// 导航栏高度,是胶囊以及胶囊的上下边距所包含的高度,通常是 32+4+4
this.globalData.navBarHeight = menuButtonInfo.height + (menuButtonInfo.top - statusBarHeight) * 2;
this.globalData.totalNavHeight = statusBarHeight + this.globalData.navBarHeight;
this.globalData.contentHeight = windowHeight - this.globalData.totalNavHeight;
this.globalData.totalTabBarHeight = this.globalData.tabBarHeight + this.globalData.safeAreaBottom;
this.globalData.contentWithoutTabbarHeight = this.globalData.contentHeight - this.globalData.totalTabBarHeight;
},
// TTS接口请求方法
async requestTTS(text, voice) {
return new Promise((resolve, reject) => {
const app = getApp();
app.request({
url: `${this.domain}/open-api/wx330e54aa6000516d/tts`,
data: {
text,
voice
},
success: (resp) => {
if (resp.data.code === 2000000) {
resolve(resp.data.data.url);
} else if (resp.data.code === 5000014) {
// 次数用完,显示广告弹窗
const pages = getCurrentPages();
const currentPage = pages[pages.length - 1];
if (currentPage && currentPage.showLimitDialog) {
currentPage.showLimitDialog();
}
reject(new Error(resp.data.msg || '次数已用完'));
} else {
wx.showToast({
title: resp.data.msg || '生成失败',
icon: 'none'
});
reject(new Error(resp.data.msg || '生成失败'));
}
},
fail: (error) => {
wx.showToast({
title: '网络错误',
icon: 'none'
});
reject(error);
}
});
});
},
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 描述,删除时是唯一特征 path 值
*/
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)){
const index = list.findIndex(v => v.path === param);
if(index > -1){
list.splice(index, 1);
wx.setStorageSync(storageKey, list);
}
}
return list;
}
}
});