♻️ Refactoring code. 重构表单校验规则

This commit is contained in:
lbw 2023-03-08 16:07:13 +08:00
parent 11f77005ff
commit 121d24b2e4
7 changed files with 115 additions and 44 deletions

View File

@ -26,11 +26,19 @@ export function addObj(obj?: Object) {
export function getObj(id?: string) {
return request({
url: '/admin/post/' + id,
url: '/admin/post/details/' + id,
method: 'get',
});
}
export function getObjDetails(obj?: object) {
return request({
url: '/admin/post/details',
method: 'get',
params: obj,
});
}
export function delObj(ids?: object) {
return request({
url: '/admin/post',
@ -46,3 +54,33 @@ export function putObj(obj?: Object) {
data: obj,
});
}
export function validatePostName(rule: any, value: any, callback: any, isEdit: boolean) {
if (isEdit) {
return callback();
}
getObjDetails({ postName: value }).then((response) => {
const result = response.data;
if (result !== null) {
callback(new Error('岗位名称已经存在'));
} else {
callback();
}
});
}
export function validatePostCode(rule: any, value: any, callback: any, isEdit: boolean) {
if (isEdit) {
return callback();
}
getObjDetails({ postCode: value }).then((response) => {
const result = response.data;
if (result !== null) {
callback(new Error('岗位编码已经存在'));
} else {
callback();
}
});
}

View File

@ -25,15 +25,16 @@ export const deptRoleList = () => {
export const getObj = (id: string) => {
return request({
url: '/admin/role/' + id,
url: '/admin/role/details/' + id,
method: 'get',
});
};
export const getObjByCode = (code: string) => {
export const getObjDetails = (obj: object) => {
return request({
url: '/admin/role/code/' + code,
url: '/admin/role/details',
method: 'get',
params: obj,
});
};
@ -78,3 +79,33 @@ export const fetchRoleTree = (roleId: string) => {
method: 'get',
});
};
export function validateRoleCode(rule: any, value: any, callback: any, isEdit: boolean) {
if (isEdit) {
return callback();
}
getObjDetails({ roleCode: value }).then((response) => {
const result = response.data;
if (result !== null) {
callback(new Error('角色标识已经存在'));
} else {
callback();
}
});
}
export function validateRoleName(rule: any, value: any, callback: any, isEdit: boolean) {
if (isEdit) {
return callback();
}
getObjDetails({ roleName: value }).then((response) => {
const result = response.data;
if (result !== null) {
callback(new Error('角色名称已经存在'));
} else {
callback();
}
});
}

View File

@ -18,7 +18,7 @@ export const addObj = (obj: Object) => {
export const getObj = (id: String) => {
return request({
url: '/admin/user/' + id,
url: '/admin/user/details/' + id,
method: 'get',
});
};
@ -41,15 +41,9 @@ export const putObj = (obj: Object) => {
export function getDetails(obj: Object) {
return request({
url: '/admin/user/details/' + obj,
method: 'get',
});
}
export function getDetailsByPhone(obj: Object) {
return request({
url: '/admin/user/detailsByPhone/' + obj,
url: '/admin/user/details',
method: 'get',
params: obj,
});
}
@ -101,7 +95,7 @@ export function validateUsername(rule: any, value: any, callback: any, isEdit: b
return callback();
}
getDetails(value).then((response) => {
getDetails({ username: value }).then((response) => {
const result = response.data;
if (result !== null) {
callback(new Error('用户名已经存在'));
@ -115,7 +109,7 @@ export function validatePhone(rule: any, value: any, callback: any, isEdit: bool
if (isEdit) {
return callback();
}
getDetailsByPhone(value).then((response) => {
getDetails({ phone: value }).then((response) => {
const result = response.data;
if (result !== null) {
callback(new Error('手机号已经存在'));

View File

@ -1,5 +1,4 @@
import request from '/@/utils/request';
import { getDetailsByPhone } from '/@/api/admin/user';
export const pageList = (params?: Object) => {
return request({

View File

@ -71,7 +71,7 @@ export function getDetails(obj: Object) {
});
}
export function getDetailsByPhone(obj: Object) {
export function getDetailsByCode(obj: Object) {
return request({
url: '/admin/approle/detailsByCode/' + obj,
method: 'get',
@ -102,7 +102,7 @@ export function validateAppRoleCode(rule: any, value: any, callback: any, isEdit
if (isEdit) {
return callback();
}
getDetailsByPhone(value).then((response) => {
getDetailsByCode(value).then((response) => {
const result = response.data;
if (result !== null) {
callback(new Error('角色标识已经存在'));

View File

@ -34,11 +34,12 @@
</template>
<script setup lang="ts" name="systemPostDialog">
import { useMessage } from '/@/hooks/message';
import { getObj, addObj, putObj, validatePostCode, validatePostName } from '/@/api/admin/post';
import { useI18n } from 'vue-i18n';
// /
const emit = defineEmits(['refresh']);
import { useMessage } from '/@/hooks/message';
import { getObj, addObj, putObj } from '/@/api/admin/post';
import { useI18n } from 'vue-i18n';
const { t } = useI18n();
@ -62,16 +63,26 @@ const form = reactive({
//
const dataRules = ref({
postCode: [{ required: true, message: '岗位编码不能为空', trigger: 'blur' }],
postName: [{ required: true, message: '岗位名称不能为空', trigger: 'blur' }],
postCode: [
{ required: true, message: '岗位编码不能为空', trigger: 'blur' },
{
validator: (rule: any, value: any, callback: any) => {
validatePostCode(rule, value, callback, form.postId !== '');
},
trigger: 'blur',
},
],
postName: [
{ required: true, message: '岗位名称不能为空', trigger: 'blur' },
{
validator: (rule: any, value: any, callback: any) => {
validatePostName(rule, value, callback, form.postId !== '');
},
trigger: 'blur',
},
],
postSort: [{ required: true, message: '岗位排序不能为空', trigger: 'blur' }],
remark: [{ required: true, message: '岗位描述不能为空', trigger: 'blur' }],
delFlag: [{ required: true, message: '是否删除 -1已删除 0正常不能为空', trigger: 'blur' }],
createTime: [{ required: true, message: '创建时间不能为空', trigger: 'blur' }],
createBy: [{ required: true, message: '创建人不能为空', trigger: 'blur' }],
updateTime: [{ required: true, message: '更新时间不能为空', trigger: 'blur' }],
updateBy: [{ required: true, message: '更新人不能为空', trigger: 'blur' }],
tenantId: [{ required: true, message: '租户ID不能为空', trigger: 'blur' }],
});
//

View File

@ -59,7 +59,7 @@
import { rule } from '/@/utils/validate';
import { depttree } from '/@/api/admin/dept';
import { useMessage } from '/@/hooks/message';
import { addObj, getObj, getObjByCode, putObj } from '/@/api/admin/role';
import { addObj, getObj, putObj, validateRoleCode, validateRoleName } from '/@/api/admin/role';
import { useI18n } from 'vue-i18n';
// /
@ -93,30 +93,28 @@ const dataForm = reactive({
},
});
// ROLE_CODE
const validateRoleCode = (rule: any, value: any, callback: any) => {
getObjByCode(value).then((response: any) => {
if (form.roleId !== '') callback();
const result = response.data;
if (result && result.length !== 0) {
callback(new Error('同名角色标识已存在'));
} else {
callback();
}
});
};
//
const dataRules = ref({
roleName: [
{ required: true, message: '角色名称不能为空', trigger: 'blur' },
{ min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' },
{
validator: (rule: any, value: any, callback: any) => {
validateRoleName(rule, value, callback, form.roleId !== '');
},
trigger: 'blur',
},
],
roleCode: [
{ required: true, message: '角色标识不能为空', trigger: 'blur' },
{ min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' },
{ validator: rule.validatorCapital, trigger: 'blur' },
{ validator: validateRoleCode, trigger: 'blur' },
{
validator: (rule: any, value: any, callback: any) => {
validateRoleCode(rule, value, callback, form.roleId !== '');
},
trigger: 'blur',
},
],
roleDesc: [{ max: 128, message: '长度在 128 个字符内', trigger: 'blur' }],
dsType: [{ required: true, message: '请选择数据权限类型', trigger: 'blur' }],