app.js 6.42 KB
//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/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:3001',
        domain: 'https://jilegeji.xwenliang.cn',
        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 描述,删除时是唯一特征 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;
        }
    }
});