result.js 1.56 KB

import { convertDate } from '../../utils/util.js';
//获取应用实例
const app = getApp();
const audioList = app.globalData.getAudioList();
const type = app.globalData.getTypeList();

let audio = null;
let formateDate = function (audioList) {
    return audioList.map(audio => {
        return {
            ...audio,
            date: convertDate(audio.time)
        };
    }).reverse();
};

Page({
    data: {
        audioList: formateDate(audioList)
    },
    onLoad (options) {
        
    },
    play(e){
        const { index } = e.target.dataset;
        const target = app.globalData.getAudioList()[index];
        audio && audio.destroy();
        audio = wx.createInnerAudioContext({useWebAudioImplement: true});
        audio.autoplay = true;
        audio.src = target.path;
    },
    download(e){
        const { index } = e.target.dataset;
        const target = app.globalData.getAudioList()[index];
        const ext = target.path.split('.').pop();
        wx.shareFileMessage({
            filePath: target.path,
            fileName: `${target.text.slice(0, 8)}.${ext}`
        });
    },
    delete(e){
        const { index } = e.target.dataset;
        wx.showModal({
            title: '提示',
            content: '确认删除吗?',
            success: res => {
                if(!res.confirm){
                    return;
                }
                const newList = app.globalData.setAudioList('delete', index);
                this.setData({
                    audioList: formateDate(newList)
                });
            }
        });
    }
});