feat: 用户管理

This commit is contained in:
dap
2024-10-05 12:51:28 +08:00
parent 51c63c1b69
commit 63fe4c5b2e
4 changed files with 298 additions and 44 deletions

View File

@@ -0,0 +1 @@
export { default as TableSwitch } from './src/table-switch.vue';

View File

@@ -0,0 +1,82 @@
<script lang="ts">
import { defineComponent } from 'vue';
import { Switch } from 'ant-design-vue';
import { isFunction } from 'lodash-es';
export default defineComponent({
name: 'TableSwitch',
components: {
Switch,
},
inheritAttrs: false,
props: {
modelValue: {
type: [Boolean, String, Number],
default: false,
},
checkedText: {
type: String,
default: '启用',
},
unCheckedText: {
type: String,
default: '禁用',
},
// 使用严格相等判断 类型要正确
checkedValue: {
type: [Boolean, String, Number],
default: '0',
},
unCheckedValue: {
type: [Boolean, String, Number],
default: '1',
},
api: {
type: Function,
required: false,
default: null,
},
reload: {
type: Function,
required: false,
default: null,
},
},
emits: ['update:modelValue'],
setup(props, { emit }) {
type CheckedType = boolean | number | string;
async function onChange(checked: CheckedType) {
const { checkedValue, unCheckedValue } = props;
// 原本的状态
const lastStatus =
checked === checkedValue ? unCheckedValue : checkedValue;
// 切换状态
emit('update:modelValue', checked);
const { api, reload } = props;
try {
isFunction(api) && (await api());
isFunction(reload) && (await reload());
} catch {
emit('update:modelValue', lastStatus);
}
}
return {
onChange,
};
},
});
</script>
<template>
<Switch
v-bind="$attrs"
:checked="modelValue"
:checked-children="checkedText"
:checked-value="checkedValue"
:un-checked-children="unCheckedText"
:un-checked-value="unCheckedValue"
@change="onChange"
/>
</template>