Browse Source

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

tall
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, Vue: true,
VueRouter: true, VueRouter: true,
Vuex: true, Vuex: true,
axios: true,
_: true, _: true,
uni: true, uni: true,
wx: true,
}, },
}; };

1
CHANGELOG.md

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

2
src/pages/index/index.vue

@ -3,9 +3,9 @@
<view class="relative" @touchmove="onMove"> <view class="relative" @touchmove="onMove">
<!-- 日历 --> <!-- 日历 -->
<Calendar @selected-change="onDateChange" :show-back="true" ref="calendar" @handleFindPoint="handleFindPoint" /> <Calendar @selected-change="onDateChange" :show-back="true" ref="calendar" @handleFindPoint="handleFindPoint" />
<!-- #ifdef H5 -->
<!-- 上传 导入wbs --> <!-- 上传 导入wbs -->
<Upload @success="onUploadSuccess" @error="onUploadError" /> <Upload @success="onUploadSuccess" @error="onUploadError" />
<!-- #ifdef H5 -->
<!-- #endif --> <!-- #endif -->
</view> </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 { export default {
/** /**
* 上传单个文件 * 上传单个文件
@ -16,10 +62,9 @@ export default {
if (!token) { if (!token) {
return reject('用户未登录,请登录后重试'); return reject('用户未登录,请登录后重试');
} }
uni.chooseFile({ chooseFile(extension)
count: 1, //默认100 .then(filePath => {
extension, console.log('filePath: ', filePath);
success: res => {
if (!timer) { if (!timer) {
timer = setTimeout(() => { timer = setTimeout(() => {
uni.$t.ui.showLoading('正在上传...'); uni.$t.ui.showLoading('正在上传...');
@ -29,7 +74,7 @@ export default {
// 开始上传 // 开始上传
uni.uploadFile({ uni.uploadFile({
url, url,
filePath: res.tempFilePaths[0], filePath,
name, name,
formData, formData,
header: { Authorization: `Bearer ${token}` }, header: { Authorization: `Bearer ${token}` },
@ -54,13 +99,12 @@ export default {
reject(error); reject(error);
}, },
}); });
}, })
fail: error => { .catch(error => {
clearTimeout(timer); clearTimeout(timer);
uni.hideLoading(); uni.hideLoading();
reject(error); reject(error);
}, });
});
}); });
}, },
}; };

Loading…
Cancel
Save