<script lang="ts">
import { defineComponent, ref, reactive, toRefs, onMounted } from 'vue';
import { getUsers, createUser} from "@/api/users";
import { ElMessage, ElMessageBox, ElNotification } from "element-plus";
import { Search } from '@element-plus/icons-vue';
import FormInstance from "element-plus";
export default defineComponent({
name: "XX",
components: {},
setup() {
const emyptForm = {
alias_name: "",
}
const state = reactive({
serverUserForm: JSON.parse(JSON.stringify(emyptForm)),
tableData:[]
});
const columns = [
{ label: "用户", prop: "alias_name", width: "200" },
];
onMounted(() => {
getData();
});
const getData = () => {
getUsers({}).then(res => {
const { data } = res.data
state.tableData = data.results;
}).catch((error) => {
ElMessage.error('获取数据错误!');
})
}
const serverUserFormRef = ref<typeof FormInstance>();
const rules = {
alias_name: [{ required: true, message: "请输入用户名", trigger: "blur" }]
};
const clearAll = () => {
state.serverUserForm = JSON.parse(JSON.stringify(emyptForm));
}
const handleAdd = (formEl) => {
formEl.validate((valid: boolean) => {
if (valid) {
const params = state.serverUserForm;
createUser(params).then(res => {
const { code, data } = res.data
if(code == 0) {
ElNotification({
title: '操作成功',
message: h('i', { style: 'color: teal' }, '用户已新增!'),
});
getData();
} else {
ElMessage.error('新增用户错误,请重试!');
}
}).catch(error => {
ElMessage.error('服务器修复中,请稍后重试!');
});
clearAll();
} else {
return false;
}
})
}
return {
serverUserFormRef,
...toRefs(state),
rules,
handleAdd,
columns,
};
}
});
</script>