const FileSystemManager = wx.getFileSystemManager(); const formatTime = date => { const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() const hour = date.getHours() const minute = date.getMinutes() const second = date.getSeconds() return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}` } const formatNumber = n => { n = n.toString() return n[1] ? n : `0${n}` } const openFileFunc = (url, fileName = '') => { wx.showLoading({ title: '', }) wx.downloadFile({ url, success: function (res) { const filePath = res.tempFilePath; FileSystemManager.saveFile({ tempFilePath: filePath, filePath: wx.env.USER_DATA_PATH + '/' + decodeURI(fileName || '文件'), success() { wx.openDocument({ filePath: wx.env.USER_DATA_PATH + '/' + decodeURI(fileName || '文件'), success: function (res) { wx.hideLoading(); }, fail() { wx.hideLoading(); } }) }, fail(e) { wx.hideLoading() wx.showToast({ title: '请稍后再试!', }) } }) }, fail() { wx.hideLoading(); wx.showToast({ title: '请稍后再试!', }) } }) } const toNavigatePage = (url = '') => { wx.navigateTo({ url }) } module.exports = { formatTime, openFileFunc, toNavigatePage }