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