first commit
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
if (typeof Vue === 'undefined') {
|
||||
console.error('Vue is not defined. Please include Vue first.')
|
||||
}
|
||||
|
||||
const MARK = '__autoEnhance_validated__'
|
||||
|
||||
function autoEnhanceForm(formVm) {
|
||||
// 防止重复处理
|
||||
if (formVm[MARK]) return
|
||||
formVm[MARK] = true
|
||||
|
||||
const attrs = formVm.$attrs;
|
||||
if (attrs && attrs['no-auto-enhance'] !== undefined) {
|
||||
return; // 有 no-auto-enhance 属性,跳过增强
|
||||
}
|
||||
|
||||
// 如果 $attrs 没有,也可以尝试从 $vnode 回退(兼容旧版)
|
||||
if (!attrs) {
|
||||
const vnode = formVm.$vnode;
|
||||
if (vnode && vnode.data && vnode.data.attrs && vnode.data.attrs['no-auto-enhance'] !== undefined) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const originalValidate = formVm.validate
|
||||
|
||||
formVm.validate = function(callback) {
|
||||
let capturedFields;
|
||||
|
||||
const promise = new Promise((resolve) => {
|
||||
originalValidate.call(this, (valid, fields) => {
|
||||
capturedFields = fields;
|
||||
|
||||
if (valid) {
|
||||
resolve(true);
|
||||
} else {
|
||||
const firstField = Object.keys(fields)[0];
|
||||
const fieldErrors = fields[firstField];
|
||||
const errorMessage = fieldErrors?.[0]?.message || '校验失败';
|
||||
|
||||
let finalMessage = errorMessage;
|
||||
const formItem = this.fields?.find((item) => item.prop === firstField);
|
||||
if (errorMessage === '必填' && formItem && formItem.label) {
|
||||
finalMessage = `${formItem.label} 必填`;
|
||||
}
|
||||
|
||||
if (this.$message) {
|
||||
this.$message({ message: finalMessage, type: 'error' });
|
||||
} else {
|
||||
alert(finalMessage);
|
||||
}
|
||||
|
||||
this.$nextTick(() => {
|
||||
const inputEl =
|
||||
formItem?.$el?.querySelector('input, textarea, .el-input__inner, .el-textarea__inner');
|
||||
if (inputEl) inputEl.focus();
|
||||
});
|
||||
|
||||
resolve(false);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
if (typeof callback === 'function') {
|
||||
promise.then(valid => callback(valid, capturedFields));
|
||||
return;
|
||||
}
|
||||
|
||||
return promise;
|
||||
}
|
||||
}
|
||||
|
||||
// 拦截 Vue.component
|
||||
if (Vue.component) {
|
||||
const originalComponent = Vue.component
|
||||
Vue.component = function(name, def) {
|
||||
const res = originalComponent.apply(this, arguments)
|
||||
|
||||
if (name === 'el-form' && def) {
|
||||
const originalMounted = def.mounted
|
||||
def.mounted = function() {
|
||||
autoEnhanceForm(this)
|
||||
originalMounted?.call(this)
|
||||
}
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
}
|
||||
|
||||
if (Vue.extend) {
|
||||
const originalExtend = Vue.extend
|
||||
Vue.extend = function() {
|
||||
const Ctor = originalExtend.apply(this, arguments)
|
||||
|
||||
if (Ctor.options?.name === 'ElForm') {
|
||||
const originalMounted = Ctor.options.mounted
|
||||
Ctor.options.mounted = function() {
|
||||
autoEnhanceForm(this)
|
||||
originalMounted?.call(this)
|
||||
}
|
||||
}
|
||||
|
||||
return Ctor
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,350 @@
|
||||
const commonUtil = {
|
||||
//axios配置
|
||||
axiosService() {
|
||||
// 创建 axios 实例
|
||||
const axiosService = axios.create({
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
|
||||
"x-requested-with": "XMLHttpRequest"
|
||||
}
|
||||
})
|
||||
//axios拦截器
|
||||
axiosService.interceptors.response.use(
|
||||
(response) => {
|
||||
const userAgent = navigator.userAgent || navigator.vendor || window.opera
|
||||
const isMobile = /iPad|iPhone|iPod|Android/.test(userAgent)
|
||||
//判断是否为blob 不处理直接返回文件流
|
||||
if (response.config.responseType === "blob") {
|
||||
return response
|
||||
}
|
||||
if (response.data && response.data.code !== "200" && response.data.code !== 0 && response.data.code !== 99) {
|
||||
if (isMobile) {
|
||||
vant.Toast.fail(response.data.msg)
|
||||
} else {
|
||||
ELEMENT.Message.error(response.data.msg)
|
||||
}
|
||||
return Promise.reject(response.data)
|
||||
}
|
||||
return response.data
|
||||
},
|
||||
(error) => {
|
||||
const userAgent = navigator.userAgent || navigator.vendor || window.opera
|
||||
const isMobile = /iPad|iPhone|iPod|Android/.test(userAgent)
|
||||
if (isMobile) {
|
||||
vant.Toast(error.message)
|
||||
} else {
|
||||
ELEMENT.Message.error(error.message)
|
||||
}
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
return axiosService
|
||||
},
|
||||
|
||||
//下载文件
|
||||
downLoadService: function (url, data) {
|
||||
const userAgent = navigator.userAgent || navigator.vendor || window.opera
|
||||
const isMobile = /iPad|iPhone|iPod|Android/.test(userAgent)
|
||||
|
||||
let loading = null
|
||||
|
||||
if (isMobile) {
|
||||
loading = vant.Toast.loading({
|
||||
duration: 0,
|
||||
forbidClick: true,
|
||||
message: '导出中,请耐心等待'
|
||||
});
|
||||
} else {
|
||||
loading = ELEMENT.Loading.service({
|
||||
lock: true,
|
||||
text: "导出中,请耐心等待",
|
||||
spinner: "el-icon-loading",
|
||||
background: "rgba(0, 0, 0, 0.7)"
|
||||
})
|
||||
}
|
||||
|
||||
Vue.prototype.$axios
|
||||
.post(url, data, {responseType: "blob"})
|
||||
.then((response) => {
|
||||
if (response.data.type === "application/json") {
|
||||
try {
|
||||
const reader = new FileReader()
|
||||
reader.onload = function () {
|
||||
// reader.result 包含了 Blob 的内容,转换为字符串
|
||||
const content = reader.result
|
||||
// 将字符串解析为 JSON 对象
|
||||
const jsonObject = JSON.parse(content)
|
||||
ELEMENT.Message.error(jsonObject.msg)
|
||||
}
|
||||
reader.readAsText(response.data)
|
||||
} catch (err) {
|
||||
ELEMENT.Message.error("下载文件出错")
|
||||
}
|
||||
return
|
||||
}
|
||||
//获取服务器返回的文件描述信息
|
||||
const contentDisposition = response.headers["content-disposition"]
|
||||
const filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/
|
||||
const matches = filenameRegex.exec(contentDisposition)
|
||||
let filename = ""
|
||||
if (matches != null && matches[1]) {
|
||||
filename = matches[1].replace(/['"]/g, "")
|
||||
filename = decodeURIComponent(filename)
|
||||
}
|
||||
//执行下载文件
|
||||
const url = window.URL.createObjectURL(new Blob([response.data]))
|
||||
const link = document.createElement("a")
|
||||
link.href = url
|
||||
link.setAttribute("download", filename) // 例如 'document.pdf'
|
||||
document.body.appendChild(link)
|
||||
link.click()
|
||||
link.parentNode.removeChild(link)
|
||||
loading.close()
|
||||
})
|
||||
.finally(() => {
|
||||
if (isMobile) {
|
||||
loading.clear()
|
||||
} else {
|
||||
loading.close()
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
//权限认证
|
||||
authService() {
|
||||
function authPermission(permission) {
|
||||
if (typeof permission !== "string" || !permission.trim()) {
|
||||
return false
|
||||
}
|
||||
const permissions = store.state.user.permissions
|
||||
if (!Array.isArray(permissions) || permissions.length === 0) {
|
||||
return false
|
||||
}
|
||||
const permissionsSet = new Set(permissions)
|
||||
return permissionsSet.has(permission.trim())
|
||||
}
|
||||
|
||||
function authRole(role) {
|
||||
if (typeof role !== "string" || !role.trim()) {
|
||||
return false
|
||||
}
|
||||
const roles = store.state.user.roles.map((item) => item.code)
|
||||
if (!Array.isArray(roles) || roles.length === 0) {
|
||||
return false
|
||||
}
|
||||
const rolesSet = new Set(roles)
|
||||
return rolesSet.has(role.trim())
|
||||
}
|
||||
|
||||
return {
|
||||
// 验证用户是否具备某权限
|
||||
hasPermission(permission) {
|
||||
return authPermission(permission)
|
||||
},
|
||||
// 验证用户是否含有指定权限,只需包含其中一个
|
||||
hasPermissionOr(permissionsOrPermissionsStr) {
|
||||
const permissions = Array.isArray(permissionsOrPermissionsStr) ? permissionsOrPermissionsStr : permissionsOrPermissionsStr.split(",")
|
||||
return permissions.some((item) => {
|
||||
return authPermission(item)
|
||||
})
|
||||
},
|
||||
// 验证用户是否含有指定权限,必须全部拥有
|
||||
hasPermissionAnd(permissionsOrPermissionsStr) {
|
||||
const permissions = Array.isArray(permissionsOrPermissionsStr) ? permissionsOrPermissionsStr : permissionsOrPermissionsStr.split(",")
|
||||
return permissions.every((item) => {
|
||||
return authPermission(item)
|
||||
})
|
||||
},
|
||||
// 判断用户是否拥有某个角色
|
||||
hasRole(role) {
|
||||
return authRole(role)
|
||||
},
|
||||
// 判断用户是否拥有某些角色中的任意一个
|
||||
hasRoleOr(rolesOrRoleStr) {
|
||||
const roles = Array.isArray(rolesOrRoleStr) ? rolesOrRoleStr : rolesOrRoleStr.split(",")
|
||||
return roles.some((item) => {
|
||||
return authRole(item.trim())
|
||||
})
|
||||
},
|
||||
// 判断用户是否拥有某些角色中的所有
|
||||
hasRoleAnd(rolesOrRoleStr) {
|
||||
const roles = Array.isArray(rolesOrRoleStr) ? rolesOrRoleStr : rolesOrRoleStr.split(",")
|
||||
return roles.every((item) => {
|
||||
return authRole(item)
|
||||
})
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
//根据文件后缀名获取显示的svg路径
|
||||
getFileItemShowIcon(suffix) {
|
||||
let assets = "/assets/platform/img/svg/"
|
||||
if (["doc", "docx"].includes(suffix)) {
|
||||
assets += "docx.svg"
|
||||
} else if (["xls", "xlsx"].includes(suffix)) {
|
||||
assets += "xlsx.svg"
|
||||
} else if (["ppt", "pptx"].includes(suffix)) {
|
||||
assets += "pptx.svg"
|
||||
} else if (["pdf"].includes(suffix)) {
|
||||
assets += "pdf.svg"
|
||||
} else if (["txt"].includes(suffix)) {
|
||||
assets += "txt.svg"
|
||||
} else if (["zip", "rar"].includes(suffix)) {
|
||||
assets += "zip.svg"
|
||||
} else if (["mp4", "avi", "wmv", "rmvb", "flv", "mkv"].includes(suffix)) {
|
||||
assets += "mp4.svg"
|
||||
} else if (["mp3", "wav", "wma", "flac", "ape"].includes(suffix)) {
|
||||
assets += "mp3.svg"
|
||||
} else if (["jpg", "jpeg", "png", "gif", "bmp", "webp"].includes(suffix)) {
|
||||
assets += "image.svg"
|
||||
} else {
|
||||
assets += "unknow.svg"
|
||||
}
|
||||
return assets
|
||||
},
|
||||
|
||||
//文件预览
|
||||
previewFile(item) {
|
||||
if (["doc", "docx", "xls", "xlsx"].includes(item.suffix.toLocaleLowerCase())) {
|
||||
window.open(
|
||||
"/assets/platform/plugins/pdfJs/web/viewer.html?file=" + encodeURIComponent("/platform/sys/file/convertPDF?id=" + item.id),
|
||||
item.name
|
||||
)
|
||||
} else if (["pdf"].includes(item.suffix.toLocaleLowerCase())) {
|
||||
window.open("/assets/platform/plugins/pdfJs/web/viewer.html?file=" + encodeURIComponent(item.downloadPath), item.name)
|
||||
} else if (["jpg", "jpeg", "png", "gif", "bmp", "webp"].includes(item.suffix.toLocaleLowerCase())) {
|
||||
let image = new Image()
|
||||
image.src = item.downloadPath
|
||||
let viewer = new Viewer(image, {
|
||||
zIndex: 99999,
|
||||
title: false
|
||||
})
|
||||
viewer.show()
|
||||
} else {
|
||||
// this.$message.warning("暂不支持预览该文件,请下载后查看")
|
||||
alert("暂不支持预览该文件,请下载后查看")
|
||||
}
|
||||
},
|
||||
|
||||
smCrypto() {
|
||||
const cipherMode = 1 // 1 - C1C3C2,0 - C1C2C3,默认为1
|
||||
const publicKey =
|
||||
"04298364ec840088475eae92a591e01284d1abefcda348b47eb324bb521bb03b0b2a5bc393f6b71dabb8f15c99a0050818b56b23f31743b93df9cf8948f15ddb54"
|
||||
|
||||
// SM2加密
|
||||
function doSm2Encrypt(msgString) {
|
||||
return sm2.doEncrypt(msgString, publicKey, cipherMode)
|
||||
}
|
||||
|
||||
// SM2数组加密
|
||||
function doSm2ArrayEncrypt(msgString) {
|
||||
return sm2.doEncrypt(msgString, publicKey, cipherMode)
|
||||
}
|
||||
|
||||
return {
|
||||
doSm2Encrypt,
|
||||
doSm2ArrayEncrypt
|
||||
}
|
||||
},
|
||||
|
||||
// pjax跳转
|
||||
pjaxPush(url, data = {}) {
|
||||
const {pathname, search} = location
|
||||
if (url === pathname + search) {
|
||||
return
|
||||
}
|
||||
$.pjax({
|
||||
url: url,
|
||||
container: "#sub-app-container-main-content-body",
|
||||
maxCacheLength: 0,
|
||||
push: false,
|
||||
replace: true,
|
||||
fragment: "#sub-app-container-main-content-body",
|
||||
timeout: 8000,
|
||||
data
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function removeHTMLTag(str) {
|
||||
str = str.replace(/<\/?[^>]*>/g, "") //去除HTML tag
|
||||
str = str.replace(/[ | ]*\n/g, "\n") //去除行尾空白
|
||||
str = str.replace(/\n[\s| | ]*\r/g, "\n") //去除多余空行
|
||||
str = str.replace(/ /gi, "") //去掉
|
||||
return str
|
||||
}
|
||||
|
||||
function GetQueryString(name) {
|
||||
return new URLSearchParams(window.location.search).get(name)
|
||||
}
|
||||
|
||||
function clearAllTimers() {
|
||||
const maxTimeoutId = setTimeout(function () {
|
||||
}, 0)
|
||||
for (let i = 0; i <= maxTimeoutId; i++) {
|
||||
clearTimeout(i)
|
||||
clearInterval(i)
|
||||
}
|
||||
}
|
||||
|
||||
function getNestedValue(obj, path) {
|
||||
return path.split(".").reduce((acc, part) => acc && acc[part], obj)
|
||||
}
|
||||
|
||||
function loc() {
|
||||
return location.href.replace(location.search, "").replace(location.hash, "")
|
||||
}
|
||||
|
||||
function clone(obj) {
|
||||
return JSON.parse(JSON.stringify(obj))
|
||||
}
|
||||
|
||||
function base64ToFile(base64Data, filename) {
|
||||
// 将base64的数据部分提取出来
|
||||
const parts = base64Data.split(";base64,")
|
||||
const contentType = parts[0].split(":")[1]
|
||||
const raw = window.atob(parts[1])
|
||||
const rawLength = raw.length
|
||||
const uInt8Array = new Uint8Array(rawLength)
|
||||
|
||||
for (let i = 0; i < rawLength; ++i) {
|
||||
uInt8Array[i] = raw.charCodeAt(i)
|
||||
}
|
||||
|
||||
// 使用Blob对象创建File对象
|
||||
const blob = new Blob([uInt8Array], {type: contentType})
|
||||
blob.lastModifiedDate = new Date()
|
||||
blob.name = filename
|
||||
return new File([blob], filename, {type: contentType})
|
||||
}
|
||||
|
||||
function createLoading(text = '加载中...') {
|
||||
return ELEMENT.Loading.service({
|
||||
lock: true,
|
||||
text: text,
|
||||
spinner: 'el-icon-loading',
|
||||
background: 'rgba(0, 0, 0, 0.7)'
|
||||
})
|
||||
}
|
||||
|
||||
function getBaseSubAppPath(pathname = window.location.pathname) {
|
||||
const startIndex = pathname.indexOf('/platform');
|
||||
if (startIndex === -1) {
|
||||
return pathname
|
||||
}
|
||||
return pathname.substring(startIndex);
|
||||
}
|
||||
|
||||
function getFullSubAppPath() {
|
||||
const pathname = window.location.pathname;
|
||||
const search = window.location.search; // 包含 ?appId=...
|
||||
const startIndex = pathname.indexOf('/platform');
|
||||
if (startIndex === -1) {
|
||||
return pathname
|
||||
}
|
||||
return pathname.substring(startIndex) + search;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
// =============== 坐标系工具 ===============
|
||||
const PI = 3.1415926535897932384626
|
||||
const a = 6378245.0 //卫星椭球坐标投影到平面地图坐标系的投影因子。
|
||||
const ee = 0.00669342162296594323 //椭球的偏心率。
|
||||
const coordinateUtil = {
|
||||
// 判断是否在中国范围(仅中国加密)
|
||||
outOfChina(lon, lat) {
|
||||
if (lon < 72.004 || lon > 137.8347) {
|
||||
return true;
|
||||
}
|
||||
if (lat < 0.8293 || lat > 55.8271) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
|
||||
// 基础偏移计算
|
||||
transformLat(lng, lat) {
|
||||
let ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * Math.sqrt(Math.abs(lng))
|
||||
ret += ((20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0) / 3.0
|
||||
ret += ((20.0 * Math.sin(lat * PI) + 40.0 * Math.sin((lat / 3.0) * PI)) * 2.0) / 3.0
|
||||
ret += ((160.0 * Math.sin((lat / 12.0) * PI) + 320 * Math.sin((lat * PI) / 30.0)) * 2.0) / 3.0
|
||||
return ret
|
||||
},
|
||||
|
||||
transformLng(lng, lat) {
|
||||
let ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng))
|
||||
ret += ((20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0) / 3.0
|
||||
ret += ((20.0 * Math.sin(lng * PI) + 40.0 * Math.sin((lng / 3.0) * PI)) * 2.0) / 3.0
|
||||
ret += ((150.0 * Math.sin((lng / 12.0) * PI) + 300.0 * Math.sin((lng / 30.0) * PI)) * 2.0) / 3.0
|
||||
return ret
|
||||
},
|
||||
|
||||
// WGS-84 → GCJ-02(用于地图展示)
|
||||
wgs84ToGcj02(lng, lat) {
|
||||
let dlat = this.transformLat(lng - 105.0, lat - 35.0);
|
||||
let dlng = this.transformLng(lng - 105.0, lat - 35.0);
|
||||
let radlat = (lat / 180.0) * PI;
|
||||
let magic = Math.sin(radlat);
|
||||
magic = 1 - ee * magic * magic;
|
||||
let sqrtmagic = Math.sqrt(magic);
|
||||
dlat =
|
||||
(dlat * 180.0) /
|
||||
(((a * (1 - ee)) / (magic * sqrtmagic)) * PI);
|
||||
dlng =
|
||||
(dlng * 180.0) / ((a / sqrtmagic) * Math.cos(radlat) * PI);
|
||||
let mglat = lat + dlat;
|
||||
let mglng = lng + dlng;
|
||||
|
||||
return [mglat, mglng];
|
||||
},
|
||||
|
||||
// GCJ-02 → WGS-84(用于存储签到点)
|
||||
gcj02ToWgs84(lng, lat) {
|
||||
const originalLngSign = Math.sign(lng);
|
||||
const originalLatSign = Math.sign(lat);
|
||||
lat = Math.abs(lat);
|
||||
lng = Math.abs(lng);
|
||||
let dlat = this.transformLat(lng - 105.0, lat - 35.0)
|
||||
let dlng = this.transformLng(lng - 105.0, lat - 35.0)
|
||||
let radlat = lat / 180.0 * PI
|
||||
let magic = Math.sin(radlat)
|
||||
magic = 1 - ee * magic * magic
|
||||
let sqrtmagic = Math.sqrt(magic)
|
||||
dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI)
|
||||
dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI)
|
||||
let mglat = lat + dlat
|
||||
let mglng = lng + dlng
|
||||
let lngs = lng * 2 - mglng
|
||||
let lats = lat * 2 - mglat
|
||||
let finalLng = originalLngSign * lngs;
|
||||
let finalLat = originalLatSign * lats;
|
||||
|
||||
return [finalLat, finalLng];
|
||||
},
|
||||
|
||||
// 计算两点间距离(米),输入 WGS-84 坐标
|
||||
getDistance(lat1, lng1, lat2, lng2) {
|
||||
const R = 6371000; // 地球半径(米)
|
||||
const φ1 = lat1 * Math.PI / 180;
|
||||
const φ2 = lat2 * Math.PI / 180;
|
||||
const Δφ = (lat2 - lat1) * Math.PI / 180;
|
||||
const Δλ = (lng2 - lng1) * Math.PI / 180;
|
||||
|
||||
const a = Math.sin(Δφ / 2) * Math.sin(Δφ / 2) +
|
||||
Math.cos(φ1) * Math.cos(φ2) *
|
||||
Math.sin(Δλ / 2) * Math.sin(Δλ / 2);
|
||||
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
||||
return R * c;
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,56 @@
|
||||
// 劫持 validate:失败时先提示 + 滚动,再抛出异常(保持原有行为)
|
||||
if (typeof Vue !== 'undefined') {
|
||||
Vue.mixin({
|
||||
mounted() {
|
||||
this.$nextTick(() => {
|
||||
const formRefs = Object.keys(this.$refs).filter(ref => {
|
||||
const comp = this.$refs[ref];
|
||||
return comp &&
|
||||
comp.$options &&
|
||||
comp.$options._componentTag === 'van-form';
|
||||
});
|
||||
|
||||
formRefs.forEach(refName => {
|
||||
const form = this.$refs[refName];
|
||||
|
||||
const attrs = form.$attrs;
|
||||
if (attrs && attrs['no-auto-enhance'] !== undefined) {
|
||||
return; // 有 no-auto-enhance 属性,跳过增强
|
||||
}
|
||||
|
||||
if (!form || form._validatedEnhanced) return;
|
||||
|
||||
const originalValidate = form.validate;
|
||||
|
||||
form.validate = function () {
|
||||
// 保留原 validate 的返回值(Promise)
|
||||
const promise = originalValidate.call(this);
|
||||
|
||||
return promise.catch(errorInfo => {
|
||||
if (errorInfo?.length > 0) {
|
||||
const firstError = errorInfo[0];
|
||||
if(firstError.message) {
|
||||
Vue.prototype.$toast?.fail(firstError.message);
|
||||
} else {
|
||||
const fieldName = firstError.name;
|
||||
|
||||
// 滚动到字段
|
||||
// this.scrollToField(fieldName);
|
||||
|
||||
// 获取 label
|
||||
const field = this.fields?.find(f => f.name === fieldName);
|
||||
const label = field?.label;
|
||||
|
||||
// 提示用户
|
||||
Vue.prototype.$toast?.fail(`${label} 必填`);
|
||||
}
|
||||
}
|
||||
return new Promise(() => {});
|
||||
});
|
||||
};
|
||||
form._validatedEnhanced = true;
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user