Files
threeonecheck_web/main.js

95 lines
2.8 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;
}
});
// 全局拦截文件上传:校验本地文件后缀;七牛直传按 upload 域名放行
const UPLOAD_ALLOWED_EXTENSIONS = ['bmp', 'gif', 'jpg', 'jpeg', 'png', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'html', 'htm', 'txt', 'rar', 'zip', 'gz', 'bz2', 'mp4', 'avi', 'rmvb', 'pdf'];
const QINIU_UPLOAD_HOST_MARKERS = ['qiniup.com', 'qbox.me'];
function validateUploadLocalFileExt(filePath) {
if (!filePath) return true;
const cleanPath = filePath.split('?')[0];
const ext = cleanPath.split('.').pop().toLowerCase();
if (!UPLOAD_ALLOWED_EXTENSIONS.includes(ext)) {
uni.showToast({
title: `不支持 .${ext} 格式,请上传合规的文件或图片`,
icon: 'none',
duration: 3000
});
return false;
}
return true;
}
uni.addInterceptor('uploadFile', {
invoke(args) {
const uploadUrl = args.url || '';
const isQiniuUpload = QINIU_UPLOAD_HOST_MARKERS.some((m) => uploadUrl.includes(m));
if (isQiniuUpload) {
return validateUploadLocalFileExt(args.filePath) ? args : false;
}
if (uploadUrl.includes('/frontend/attachment/upload')) {
return validateUploadLocalFileExt(args.filePath) ? args : false;
}
return validateUploadLocalFileExt(args.filePath) ? args : false;
}
});
// #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