31
0
Fork 0
You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

359 lines
8.3 KiB
TypeScript

import dayjs from "dayjs";
import { message } from "@/utils/message";
import editForm from "../form.vue";
import { addDialog } from "@/components/ReDialog";
import { type FormItemProps } from "../utils/types";
// import { getUserList } from "@/api/system";
import { ElMessageBox } from "element-plus";
import { type PaginationProps } from "@pureadmin/table";
import { reactive, ref, computed, h, onMounted } from "vue";
// import { http } from "@/utils/http";
import { number } from "echarts";
export function useUser() {
const form = reactive({
/** 角色名称 */
username: "",
/** 手机号 */
mobile: "",
/** 状态 */
status: number
});
const formRef = ref();
const dataList = ref([]);
const loading = ref(true);
const switchLoadMap = ref({});
const pagination = reactive<PaginationProps>({
total: 0,
pageSize: 10,
currentPage: 1,
background: true
});
const columns: TableColumnList = [
{
label: "序号",
type: "index",
width: 70,
fixed: "left"
},
// {
// label: "用户编号",
// prop: "id",
// minWidth: 130
// },
{
label: "用户名称",
prop: "username",
minWidth: 130
},
// {
// label: "用户昵称",
// prop: "nickname",
// minWidth: 130
// },
{
label: "性别",
prop: "sex",
minWidth: 90,
cellRenderer: ({ row, props }) => (
<el-tag
size={props.size}
type={row.sex === 1 ? "danger" : ""}
effect="plain"
>
{row.sex === 1 ? "女" : "男"}
</el-tag>
)
},
{
label: "组织",
prop: "dept",
minWidth: 90,
formatter: ({ dept }) => dept.name
},
{
label: "手机号码",
prop: "mobile",
minWidth: 90
},
{
label: "状态",
prop: "status",
minWidth: 90,
cellRenderer: scope => (
<el-switch
size={scope.props.size === "small" ? "small" : "default"}
loading={switchLoadMap.value[scope.index]?.loading}
v-model={scope.row.status}
active-value={1}
inactive-value={0}
active-text="已开启"
inactive-text="已关闭"
inline-prompt
onChange={() => onChange(scope as any)}
/>
)
},
{
label: "创建时间",
minWidth: 90,
prop: "createTime",
formatter: ({ createTime }) =>
dayjs(createTime).format("YYYY-MM-DD HH:mm:ss")
},
{
label: "操作",
fixed: "right",
width: 180,
slot: "operation"
}
];
const buttonClass = computed(() => {
return [
"!h-[20px]",
"reset-margin",
"!text-gray-500",
"dark:!text-white",
"dark:hover:!text-primary"
];
});
function onChange({ row, index }) {
ElMessageBox.confirm(
`确认要<strong>${
row.status === 0 ? "停用" : "启用"
}</strong><strong style='color:var(--el-color-primary)'>${
row.username
}</strong>?`,
"系统提示",
{
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning",
dangerouslyUseHTMLString: true,
draggable: true
}
)
.then(() => {
switchLoadMap.value[index] = Object.assign(
{},
switchLoadMap.value[index],
{
loading: true
}
);
setTimeout(() => {
switchLoadMap.value[index] = Object.assign(
{},
switchLoadMap.value[index],
{
loading: false
}
);
message("已成功修改用户状态", {
type: "success"
});
}, 300);
})
.catch(() => {
row.status === 0 ? (row.status = 1) : (row.status = 0);
});
}
function handleUpdate(row) {
console.log(row);
}
function handleDelete(row) {
message(`您删除了角色名称为${row.username}的这条数据`, { type: "success" });
const deleteListData = userList.value.list.filter(
item => item.id !== row.id
);
userList.value.list = deleteListData;
onSearch();
}
function handleSizeChange(val: number) {
console.log(`${val} items per page`);
}
function handleCurrentChange(val: number) {
console.log(`current page: ${val}`);
}
function handleSelectionChange(val) {
console.log("handleSelectionChange", val);
}
const userList = ref({
list: [
{
username: "admin",
nickname: "admin",
remark: "管理员",
deptId: 103,
postIds: [1],
mobile: "15888888888",
sex: 0,
id: 1,
status: 0,
createTime: 1605456000000,
dept: {
id: 103,
name: "研发部门"
}
},
{
username: "pure",
nickname: "pure",
remark: "不要吓我",
deptId: 104,
postIds: [1],
mobile: "15888888888",
sex: 0,
id: 100,
status: 1,
createTime: 1605456000000,
dept: {
id: 104,
name: "市场部门"
}
},
{
username: "lucy",
nickname: "girl",
remark: null,
deptId: 106,
postIds: null,
mobile: "15888888888",
sex: 1,
id: 103,
status: 1,
createTime: 1605456000000,
dept: {
id: 106,
name: "财务部门"
}
},
{
username: "mike",
nickname: "boy",
remark: null,
deptId: 107,
postIds: [],
mobile: "15888888888",
sex: 0,
id: 104,
status: 0,
createTime: 1605456000000,
dept: {
id: 107,
name: "运维部门"
}
}
],
total: 4
});
function getUserList() {
// http.request("post", "/login")
return userList;
}
async function onSearch() {
loading.value = true;
const data = getUserList();
console.log(data);
dataList.value = data.value.list;
pagination.total = data.value.total;
setTimeout(() => {
loading.value = false;
}, 500);
}
const resetForm = formEl => {
if (!formEl) return;
formEl.resetFields();
onSearch();
};
function openDialog(title = "新增", row?: FormItemProps) {
addDialog({
title: `${title}用户`,
props: {
formInline: {
username: row?.username ?? "",
mobile: row?.mobile ?? "",
status: row?.status ?? ""
}
},
width: "40%",
draggable: true,
fullscreenIcon: true,
closeOnClickModal: false,
contentRenderer: () => h(editForm, { ref: formRef }),
beforeSure: (done, { options }) => {
const FormRef = formRef.value.getRef();
const curData = options.props.formInline as FormItemProps;
function chores() {
message(`${title}了角色名称为${curData.username}的这条数据`, {
type: "success"
});
done(); // 关闭弹框
onSearch(); // 刷新表格数据
}
FormRef.validate(valid => {
if (valid) {
console.log("curData", curData);
// 表单规则校验通过
if (title === "新增") {
// 实际开发先调用新增接口,再进行下面操作
const addItem = {
username: curData.username,
mobile: "15888888888",
status: curData.status,
nickname: "boy",
remark: null,
deptId: 107,
postIds: [],
sex: 0,
id: 105,
createTime: 1605456000000,
dept: {
id: 107,
name: "运维部门"
}
};
userList.value.list.push(addItem);
chores();
} else {
// 实际开发先调用编辑接口,再进行下面操作
chores();
}
}
});
}
});
}
onMounted(() => {
onSearch();
});
return {
form,
loading,
columns,
dataList,
pagination,
buttonClass,
onSearch,
openDialog,
resetForm,
handleUpdate,
handleDelete,
handleSizeChange,
handleCurrentChange,
handleSelectionChange
};
}