hook.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import dayjs from "dayjs";
  2. import { message } from "@/utils/message";
  3. import { getUserList } from "@/api/system";
  4. import { ElMessageBox } from "element-plus";
  5. import { type PaginationProps } from "@pureadmin/table";
  6. import { reactive, ref, computed, onMounted } from "vue";
  7. export function useUser() {
  8. const form = reactive({
  9. username: "",
  10. mobile: "",
  11. status: ""
  12. });
  13. const dataList = ref([]);
  14. const loading = ref(true);
  15. const switchLoadMap = ref({});
  16. const pagination = reactive<PaginationProps>({
  17. total: 0,
  18. pageSize: 10,
  19. currentPage: 1,
  20. background: true
  21. });
  22. const columns: TableColumnList = [
  23. {
  24. type: "selection",
  25. width: 55,
  26. align: "left",
  27. hide: ({ checkList }) => !checkList.includes("勾选列")
  28. },
  29. {
  30. label: "序号",
  31. type: "index",
  32. width: 70,
  33. hide: ({ checkList }) => !checkList.includes("序号列")
  34. },
  35. {
  36. label: "用户编号",
  37. prop: "id",
  38. minWidth: 130
  39. },
  40. {
  41. label: "用户名称",
  42. prop: "username",
  43. minWidth: 130
  44. },
  45. {
  46. label: "用户昵称",
  47. prop: "nickname",
  48. minWidth: 130
  49. },
  50. {
  51. label: "性别",
  52. prop: "sex",
  53. minWidth: 90,
  54. cellRenderer: ({ row, props }) => (
  55. <el-tag
  56. size={props.size}
  57. type={row.sex === 1 ? "danger" : ""}
  58. effect="plain"
  59. >
  60. {row.sex === 1 ? "女" : "男"}
  61. </el-tag>
  62. )
  63. },
  64. {
  65. label: "部门",
  66. prop: "dept",
  67. minWidth: 90,
  68. formatter: ({ dept }) => dept.name
  69. },
  70. {
  71. label: "手机号码",
  72. prop: "mobile",
  73. minWidth: 90
  74. },
  75. {
  76. label: "状态",
  77. prop: "status",
  78. minWidth: 90,
  79. cellRenderer: scope => (
  80. <el-switch
  81. size={scope.props.size === "small" ? "small" : "default"}
  82. loading={switchLoadMap.value[scope.index]?.loading}
  83. v-model={scope.row.status}
  84. active-value={1}
  85. inactive-value={0}
  86. active-text="已开启"
  87. inactive-text="已关闭"
  88. inline-prompt
  89. onChange={() => onChange(scope as any)}
  90. />
  91. )
  92. },
  93. {
  94. label: "创建时间",
  95. minWidth: 90,
  96. prop: "createTime",
  97. formatter: ({ createTime }) =>
  98. dayjs(createTime).format("YYYY-MM-DD HH:mm:ss")
  99. },
  100. {
  101. label: "操作",
  102. fixed: "right",
  103. width: 180,
  104. slot: "operation"
  105. }
  106. ];
  107. const buttonClass = computed(() => {
  108. return [
  109. "!h-[20px]",
  110. "reset-margin",
  111. "!text-gray-500",
  112. "dark:!text-white",
  113. "dark:hover:!text-primary"
  114. ];
  115. });
  116. function onChange({ row, index }) {
  117. ElMessageBox.confirm(
  118. `确认要<strong>${
  119. row.status === 0 ? "停用" : "启用"
  120. }</strong><strong style='color:var(--el-color-primary)'>${
  121. row.username
  122. }</strong>用户吗?`,
  123. "系统提示",
  124. {
  125. confirmButtonText: "确定",
  126. cancelButtonText: "取消",
  127. type: "warning",
  128. dangerouslyUseHTMLString: true,
  129. draggable: true
  130. }
  131. )
  132. .then(() => {
  133. switchLoadMap.value[index] = Object.assign(
  134. {},
  135. switchLoadMap.value[index],
  136. {
  137. loading: true
  138. }
  139. );
  140. setTimeout(() => {
  141. switchLoadMap.value[index] = Object.assign(
  142. {},
  143. switchLoadMap.value[index],
  144. {
  145. loading: false
  146. }
  147. );
  148. message("已成功修改用户状态", {
  149. type: "success"
  150. });
  151. }, 300);
  152. })
  153. .catch(() => {
  154. row.status === 0 ? (row.status = 1) : (row.status = 0);
  155. });
  156. }
  157. function handleUpdate(row) {
  158. console.log(row);
  159. }
  160. function handleDelete(row) {
  161. console.log(row);
  162. }
  163. function handleSizeChange(val: number) {
  164. console.log(`${val} items per page`);
  165. }
  166. function handleCurrentChange(val: number) {
  167. console.log(`current page: ${val}`);
  168. }
  169. function handleSelectionChange(val) {
  170. console.log("handleSelectionChange", val);
  171. }
  172. async function onSearch() {
  173. loading.value = true;
  174. const { data } = await getUserList();
  175. dataList.value = data.list;
  176. pagination.total = data.total;
  177. setTimeout(() => {
  178. loading.value = false;
  179. }, 500);
  180. }
  181. const resetForm = formEl => {
  182. if (!formEl) return;
  183. formEl.resetFields();
  184. onSearch();
  185. };
  186. onMounted(() => {
  187. onSearch();
  188. });
  189. return {
  190. form,
  191. loading,
  192. columns,
  193. dataList,
  194. pagination,
  195. buttonClass,
  196. onSearch,
  197. resetForm,
  198. handleUpdate,
  199. handleDelete,
  200. handleSizeChange,
  201. handleCurrentChange,
  202. handleSelectionChange
  203. };
  204. }