Browse Source

refactor: 修改utils/upload 兼容小程序选择客户端文件上传WBS

pull/46/head
wally 4 years ago
parent
commit
8f491295be
  1. 2
      .eslintrc.js
  2. 1
      CHANGELOG.md
  3. 2
      src/pages/index/index.vue
  4. 62
      src/utils/upload.js

2
.eslintrc.js

@ -41,8 +41,8 @@ module.exports = {
Vue: true,
VueRouter: true,
Vuex: true,
axios: true,
_: true,
uni: true,
wx: true,
},
};

1
CHANGELOG.md

@ -195,6 +195,7 @@
- | 修改代码格式 | 14123d7
- | 修改定期任务骨架屏高度 | 909a734
- | 小红点api缓存修改 | e992343
- | 提交本地代码 | e0cf2ed
- | 插件查询及展示 | 4dba770
- | 整理代码 | 7a55315
- | 日历的更改 | 7353ac8

2
src/pages/index/index.vue

@ -3,9 +3,9 @@
<view class="relative" @touchmove="onMove">
<!-- 日历 -->
<Calendar @selected-change="onDateChange" :show-back="true" ref="calendar" @handleFindPoint="handleFindPoint" />
<!-- #ifdef H5 -->
<!-- 上传 导入wbs -->
<Upload @success="onUploadSuccess" @error="onUploadError" />
<!-- #ifdef H5 -->
<!-- #endif -->
</view>

62
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);
},
});
});
});
},
};

Loading…
Cancel
Save