fix: perfect constants

This commit is contained in:
chansee97 2024-04-16 10:46:51 +08:00
parent 5d1eb3cb3c
commit 2353bd7a8a
6 changed files with 58 additions and 15 deletions

12
src/constants/Regex.ts Normal file
View File

@ -0,0 +1,12 @@
/**
* @description Some common rules
* @link https://any-rule.vercel.app/
*/
export enum Regex {
Url = '^(((ht|f)tps?):\\\/\\\/)?([^!@#$%^&*?.\\s-]([^!@#$%^&*?.\\s]{0,63}[^!@#$%^&*?.\\s])?\\.)+[a-z]{2,6}\\\/?',
Email = '^(([^<>()[\\]\\\\.,;:\\s@"]+(\\.[^<>()[\\]\\\\.,;:\\s@"]+)*)|(".+"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$',
RouteName = '^[\\w_!@#$%^&*~-]+$',
}

9
src/constants/User.ts Normal file
View File

@ -0,0 +1,9 @@
// export const genderLabels: Record<NonNullable<CommonList.GenderType>, string> = {
// 0: '女',
// 1: '男',
// }
/** Gender */
export enum Gender {
male,
female,
}

View File

@ -1,5 +0,0 @@
/** 用户性别 */
export const genderLabels: Record<NonNullable<CommonList.GenderType>, string> = {
0: '女',
1: '男',
}

View File

@ -1 +1,2 @@
export * from './business'
export * from './Regex'
export * from './User'

View File

@ -4,7 +4,7 @@ import { NButton, NPopconfirm, NSpace, NSwitch, NTag } from 'naive-ui'
import TableModal from './components/TableModal.vue'
import { fetchUserList } from '@/service'
import { useBoolean, useLoading } from '@/hooks'
import { genderLabels } from '@/constants'
import { Gender } from '@/constants'
const { loading, startLoading, endLoading } = useLoading(false)
const { bool: visible, setTrue: openModal } = useBoolean(false)
@ -45,7 +45,7 @@ const columns: DataTableColumns = [
if (rowData.gender) {
return (
<NTag type={tagType[rowData.gender]}>
{genderLabels[rowData.gender]}
{Gender[rowData.gender]}
</NTag>
)
}

View File

@ -1,6 +1,10 @@
<script setup lang="ts">
import type {
FormItemRule,
} from 'naive-ui'
import HelpInfo from '@/components/common/HelpInfo.vue'
import { useLoading } from '@/hooks'
import { Regex } from '@/constants'
interface Props {
modalName?: string
@ -129,7 +133,16 @@ function filterDirectory(node: any[]) {
const rules = {
'name': {
required: true,
message: '请输入菜单名称',
// message: '',
validator(rule: FormItemRule, value: string) {
if (!value)
return new Error('请输入菜单名称')
if (!new RegExp(Regex.RouteName).test(value))
return new Error('菜单只能包含英文数字_!@#$%^&*~-')
return true
},
trigger: 'blur',
},
'path': {
@ -147,6 +160,15 @@ const rules = {
message: '请输入菜单标题',
trigger: 'blur',
},
'meta.herf': {
validator(rule: FormItemRule, value: string) {
if (!new RegExp(Regex.Url).test(value))
return new Error('请输入正确的URL地址')
return true
},
trigger: 'blur',
},
}
const options = [
@ -189,13 +211,13 @@ const options = [
/>
</n-form-item-grid-item>
<n-form-item-grid-item :span="1" label="菜单名称" path="name">
<n-input v-model:value="formModel.name" />
<n-input v-model:value="formModel.name" placeholder="Eg: system" />
</n-form-item-grid-item>
<n-form-item-grid-item :span="1" label="标题" path="meta.title">
<n-input v-model:value="formModel['meta.title']" />
<n-input v-model:value="formModel['meta.title']" placeholder="Eg: My-System" />
</n-form-item-grid-item>
<n-form-item-grid-item :span="2" label="路由路径" path="path">
<n-input v-model:value="formModel.path" />
<n-input v-model:value="formModel.path" placeholder="Eg: /system/user" />
</n-form-item-grid-item>
<n-form-item-grid-item :span="1" label="菜单类型" path="meta.menuType">
<n-radio-group v-model:value="formModel['meta.menuType']" name="radiogroup">
@ -213,9 +235,13 @@ const options = [
<icon-select v-model:value="formModel['meta.icon']" :disabled="modalType === 'view'" />
</n-form-item-grid-item>
<n-form-item-grid-item v-if="formModel['meta.menuType'] === 'page'" :span="2" label="组件路径" path="componentPath">
<n-input v-model:value="formModel.componentPath" />
<n-input v-model:value="formModel.componentPath" placeholder="Eg: /system/user/index.vue" />
</n-form-item-grid-item>
<n-form-item-grid-item :span="1" label="菜单排序" path="meta.order">
<n-form-item-grid-item :span="1" path="meta.order">
<template #label>
菜单排序
<HelpInfo message="数字越小,同级中越靠前" />
</template>
<n-input-number v-model:value="formModel['meta.order']" />
</n-form-item-grid-item>
<n-form-item-grid-item v-if="formModel['meta.menuType'] === 'page'" :span="1" path="meta.herf">
@ -223,7 +249,7 @@ const options = [
外链页面
<HelpInfo message="填写后,点击菜单将跳转到该地址,组件路径任意填写" />
</template>
<n-input v-model:value="formModel['meta.herf']" />
<n-input v-model:value="formModel['meta.herf']" placeholder="Eg: https://example.com" />
</n-form-item-grid-item>
<n-form-item-grid-item :span="1" label="登录访问" path="meta.requiresAuth">
<n-switch v-model:value="formModel['meta.requiresAuth']" />