diff --git a/.eslintrc.js b/.eslintrc.js index 067c1dd..d1d7b97 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -41,8 +41,8 @@ module.exports = { Vue: true, VueRouter: true, Vuex: true, - axios: true, _: true, uni: true, + wx: true, }, }; diff --git a/CHANGELOG.md b/CHANGELOG.md index 44ac6e0..c716016 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -195,6 +195,7 @@ - | 修改代码格式 | 14123d7 - | 修改定期任务骨架屏高度 | 909a734 - | 小红点api缓存修改 | e992343 + - | 提交本地代码 | e0cf2ed - | 插件查询及展示 | 4dba770 - | 整理代码 | 7a55315 - | 日历的更改 | 7353ac8 diff --git a/src/pages/index/index.vue b/src/pages/index/index.vue index 6facca9..fad3cf0 100644 --- a/src/pages/index/index.vue +++ b/src/pages/index/index.vue @@ -3,9 +3,9 @@ - + diff --git a/src/utils/upload.js b/src/utils/upload.js index 537a82a..bd3de1d 100644 --- a/src/utils/upload.js +++ b/src/utils/upload.js @@ -1,3 +1,49 @@ +// H5选择文件 +const chooseFileH5 = (extension = ['.xls', '.xlsx']) => { + return new Promise((resolve, reject) => { + uni.chooseFile({ + count: 1, //默认100 + extension, + success(res) { + resolve(res.tempFilePaths[0]); + }, + fail() { + reject('上传失败'); + }, + }); + }); +}; + +// 微信选择文件 从客户端会话选择文件。 +const chooseFileWeixin = (extension = ['.xls', '.xlsx']) => { + return new Promise((resolve, reject) => { + wx.chooseMessageFile({ + count: 1, + extension, + type: 'file', + success(res) { + resolve(res.tempFiles[0].path); + }, + fail() { + reject('上传失败'); + }, + }); + }); +}; + +// 选择文件 +const chooseFile = (extension = ['.xls', '.xlsx']) => { + let fn = null; + /* #ifdef H5 */ + fn = chooseFileH5(extension); + /* #endif */ + + /* #ifdef MP-WEIXIN */ + fn = chooseFileWeixin(extension); + /* #endif */ + return fn; +}; + export default { /** * 上传单个文件 @@ -16,10 +62,9 @@ export default { if (!token) { return reject('用户未登录,请登录后重试'); } - uni.chooseFile({ - count: 1, //默认100 - extension, - success: res => { + chooseFile(extension) + .then(filePath => { + console.log('filePath: ', filePath); if (!timer) { timer = setTimeout(() => { uni.$t.ui.showLoading('正在上传...'); @@ -29,7 +74,7 @@ export default { // 开始上传 uni.uploadFile({ url, - filePath: res.tempFilePaths[0], + filePath, name, formData, header: { Authorization: `Bearer ${token}` }, @@ -54,13 +99,12 @@ export default { reject(error); }, }); - }, - fail: error => { + }) + .catch(error => { clearTimeout(timer); uni.hideLoading(); reject(error); - }, - }); + }); }); }, };