fix(admin-user): replace fragile avatar url regex with robust validator
This commit is contained in:
@@ -29,6 +29,39 @@ const editForm = reactive({
|
|||||||
gender: 0,
|
gender: 0,
|
||||||
introduction: ''
|
introduction: ''
|
||||||
})
|
})
|
||||||
|
const validateAvatar = (_rule, value, callback) => {
|
||||||
|
if (!value || !String(value).trim()) {
|
||||||
|
callback()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const avatar = String(value).trim()
|
||||||
|
|
||||||
|
// 兼容站内相对路径(如 /upload/xx、./xx、../xx、images/a.png)
|
||||||
|
const hasScheme = /^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(avatar)
|
||||||
|
if (!hasScheme) {
|
||||||
|
const isRelativePath = /^(\/?[^\s]+)$/.test(avatar) && !avatar.startsWith('//')
|
||||||
|
if (isRelativePath) {
|
||||||
|
callback()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
callback(new Error('请输入有效头像地址(http/https 或相对路径)'))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const url = new URL(avatar)
|
||||||
|
if (url.protocol === 'http:' || url.protocol === 'https:') {
|
||||||
|
callback()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} catch (_e) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
|
||||||
|
callback(new Error('请输入有效头像地址(http/https 或相对路径)'))
|
||||||
|
}
|
||||||
|
|
||||||
const editFormRules = {
|
const editFormRules = {
|
||||||
nickname: [
|
nickname: [
|
||||||
{ required: true, message: '请输入昵称', trigger: 'blur' },
|
{ required: true, message: '请输入昵称', trigger: 'blur' },
|
||||||
@@ -38,7 +71,7 @@ const editFormRules = {
|
|||||||
{ pattern: /^\S+@\S+\.\S+$/, message: '请输入正确的邮箱地址', trigger: 'blur' }
|
{ pattern: /^\S+@\S+\.\S+$/, message: '请输入正确的邮箱地址', trigger: 'blur' }
|
||||||
],
|
],
|
||||||
avatar: [
|
avatar: [
|
||||||
{ pattern: /^(https?:\/\/)?[\w\-]+(\.[\w\-]+)+([\w\-.,@?^=%&:/~+#]*[\w\-@?^=%&/~+#])?$/, message: '请输入正确的URL地址', trigger: 'blur' }
|
{ validator: validateAvatar, trigger: 'blur' }
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
const editFormRef = ref(null)
|
const editFormRef = ref(null)
|
||||||
|
|||||||
Reference in New Issue
Block a user