app.js
4.79 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
//app.js
App({
async onLaunch () {
this.login();
// 移除 2022-12-05 之前的本地存储,因为可能有敏感词存在
const timestamp = new Date('2022-12-05').getTime();
const audioList = this.globalData.getAudioList().filter(v => v.time > timestamp);
const storageKey = 'audioList';
wx.setStorageSync(storageKey, audioList);
},
// 将登录态封装进去
// todo: 多个请求同时发起,且登录态都失效时,ssid 会相互覆盖
async request(config){
let ssid = wx.getStorageSync('ssid');
if(!ssid){
ssid = await this.login();
}
const initialSuccess = config.success;
config.data = {
...config.data,
ssid
};
config.success = async resp => {
if(resp.data.code === 5001000){
wx.setStorageSync('ssid', '');
delete config.ssid;
this.request(config);
}
else{
initialSuccess(resp);
}
};
config = {
header: {
'content-type': 'application/json'
},
dataType: 'json',
method: 'POST',
...config
};
return wx.request(config);
},
login(){
let ssid = wx.getStorageSync('ssid');
if(ssid){
return ssid;
}
wx.showLoading({
title: '登录中...',
mask: true
});
return new Promise((resolve, reject) => {
wx.login({
success: res => {
wx.request({
url: `${this.globalData.domain}/open-api/mp/login`,
data: {
code: res.code,
appid: 'wx330e54aa6000516d'
},
header: {
'content-type': 'application/json'
},
dataType: 'json',
method: 'POST',
success: resp => {
const {
ssid,
openid
} = resp.data?.data?.user;
wx.setStorageSync('ssid', ssid);
wx.setStorageSync('openid', openid);
resolve(resp.data?.data?.user);
},
fail: resp => {
wx.showModal({
title: '提示',
content: '登录失败,点击确定重试',
showCancel: false,
success: res => {
this.login();
}
});
reject(resp.errMsg);
},
complete: () => {
wx.hideLoading();
}
});
}
});
});
},
globalData: {
// domain: 'http://localhost:3003',
domain: 'https://api.xwenliang.cn',
getTypeList(){
return [
{
desc: '女声一',
createUrl(text){
return `https://api.oick.cn/txt/apiz.php?text=${text}&spd=10`;
}
}
];
},
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;
}
}
});