tall小程序和时间轴结合在小程序中
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
2.8 KiB

// 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;
};
4 years ago
export default {
/**
* 上传单个文件
* @param {string} url 服务器地址
* @param {object} formData 上传的其他字段
* @param {array} extension 上传文件类型 扩展名数组
* @param {string} name
* @returns
*/
chooseAndUpload(url, formData = {}, extension = ['.xls', '.xlsx'], name = 'param') {
uni.hideLoading();
clearTimeout(timer);
let timer = null;
4 years ago
return new Promise((resolve, reject) => {
const token = uni.$t.storage.getStorageSync(uni.$t.app.tokenKey);
if (!token) {
return reject('用户未登录,请登录后重试');
}
chooseFile(extension)
.then(filePath => {
console.log('filePath: ', filePath);
if (!timer) {
timer = setTimeout(() => {
uni.$t.ui.showLoading('正在上传...');
timer = null;
}, 800);
}
// 开始上传
4 years ago
uni.uploadFile({
url,
filePath,
4 years ago
name,
formData,
header: { Authorization: `Bearer ${token}` },
success: ({ data, statusCode }) => {
clearTimeout(timer);
uni.hideLoading();
4 years ago
if (statusCode === 200 && data) {
const { code, msg } = JSON.parse(data);
if (code !== 200) {
reject(msg);
} else {
resolve(JSON.parse(data).data);
4 years ago
}
} else {
reject('上传失败');
}
},
fail: error => {
clearTimeout(timer);
uni.hideLoading();
4 years ago
reject(error);
},
});
})
.catch(error => {
clearTimeout(timer);
uni.hideLoading();
4 years ago
reject(error);
});
4 years ago
});
},
};