83 lines
2.4 KiB
JavaScript
83 lines
2.4 KiB
JavaScript
import App from './App'
|
|
|
|
// 全局拦截选择图片,在文件进入上传列表前进行预过滤,从根本上解决“不合规图片在列表里转圈卡死”的问题
|
|
uni.addInterceptor('chooseImage', {
|
|
success(res) {
|
|
const allowedExtensions = ['bmp', 'gif', 'jpg', 'jpeg', 'png'];
|
|
const validTempFilePaths = [];
|
|
const validTempFiles = [];
|
|
let hasInvalid = false;
|
|
let invalidExt = '';
|
|
|
|
res.tempFiles.forEach((file, index) => {
|
|
const path = file.path || res.tempFilePaths[index];
|
|
const cleanPath = path.split('?')[0];
|
|
const ext = cleanPath.split('.').pop().toLowerCase();
|
|
|
|
if (allowedExtensions.includes(ext)) {
|
|
validTempFiles.push(file);
|
|
validTempFilePaths.push(res.tempFilePaths[index]);
|
|
} else {
|
|
hasInvalid = true;
|
|
invalidExt = ext;
|
|
}
|
|
});
|
|
|
|
if (hasInvalid) {
|
|
uni.showToast({
|
|
title: `已过滤不支持的 .${invalidExt} 格式图片,请上传 png/jpg/jpeg/gif/bmp`,
|
|
icon: 'none',
|
|
duration: 3500
|
|
});
|
|
}
|
|
|
|
res.tempFilePaths = validTempFilePaths;
|
|
res.tempFiles = validTempFiles;
|
|
}
|
|
});
|
|
|
|
// 全局拦截文件上传,校验文件后缀是否在后端白名单中,预防非法格式报错
|
|
uni.addInterceptor('uploadFile', {
|
|
invoke(args) {
|
|
const filePath = args.filePath;
|
|
if (filePath) {
|
|
const cleanPath = filePath.split('?')[0];
|
|
const ext = cleanPath.split('.').pop().toLowerCase();
|
|
const allowedExtensions = ['bmp', 'gif', 'jpg', 'jpeg', 'png', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'html', 'htm', 'txt', 'rar', 'zip', 'gz', 'bz2', 'mp4', 'avi', 'rmvb', 'pdf'];
|
|
if (!allowedExtensions.includes(ext)) {
|
|
uni.showToast({
|
|
title: `不支持 .${ext} 格式,请上传合规的文件或图片`,
|
|
icon: 'none',
|
|
duration: 3000
|
|
});
|
|
return false; // 拦截请求
|
|
}
|
|
}
|
|
return args;
|
|
}
|
|
});
|
|
|
|
// #ifndef VUE3
|
|
import Vue from 'vue'
|
|
import './uni.promisify.adaptor'
|
|
import uviewPlus from '@/uni_modules/uview-plus'
|
|
Vue.config.productionTip = false
|
|
Vue.use(uviewPlus)
|
|
App.mpType = 'app'
|
|
const app = new Vue({
|
|
...App
|
|
})
|
|
app.$mount()
|
|
// #endif
|
|
|
|
// #ifdef VUE3
|
|
import { createSSRApp } from 'vue'
|
|
import uviewPlus from '@/uni_modules/uview-plus'
|
|
export function createApp() {
|
|
const app = createSSRApp(App)
|
|
app.use(uviewPlus)
|
|
return {
|
|
app
|
|
}
|
|
}
|
|
// #endif
|