template-list.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <script setup lang="ts">
  2. import FormSearch, { type FormField } from "@/components/opts/form-search.vue";
  3. import TableButtons from "@/components/opts/btns2.vue";
  4. import Edit from "./template-edit.vue";
  5. import { PureTable } from "@pureadmin/table";
  6. import { computed, onBeforeMount, onMounted, reactive, ref, watch } from "vue";
  7. import * as $api from "@/api/sys/user";
  8. import { hasAuth } from "@/router/utils";
  9. import message from "@/utils/message";
  10. import { enableOptions } from "@/constants/constants";
  11. import { cloneDeep } from "@pureadmin/utils";
  12. const editRef = ref();
  13. defineOptions({
  14. name: "templateList"
  15. });
  16. const props: any = defineProps({
  17. mode: {
  18. required: false,
  19. type: String,
  20. default: "table"
  21. }
  22. });
  23. const searchFormFields = computed((): FormField[] => {
  24. const fields: FormField[] = [
  25. {
  26. type: "input",
  27. label: "用户名",
  28. prop: "username",
  29. placeholder: "请输入用户名模糊查询"
  30. },
  31. {
  32. type: "select",
  33. label: "状态",
  34. prop: "status",
  35. placeholder: "请选择",
  36. dataSourceKey: "enableOptions",
  37. options: {
  38. keys: {
  39. label: "label",
  40. value: "value",
  41. prop: "value"
  42. },
  43. filterable: true
  44. }
  45. }
  46. ];
  47. if (props.mode === "table") {
  48. return fields;
  49. } else {
  50. return fields;
  51. }
  52. });
  53. const pageData: any = reactive({
  54. mode: "table",
  55. permission: {
  56. query: ["user:query"],
  57. save: ["user:save"],
  58. update: ["user:update"],
  59. del: ["user:del"]
  60. },
  61. searchParam: {
  62. searchState: true,
  63. searchForm: {}
  64. },
  65. dataSource: {
  66. enableOptions: enableOptions
  67. },
  68. /*按钮 */
  69. btnOpts: {
  70. size: "small",
  71. leftBtns: [
  72. {
  73. key: "add",
  74. label: "新增",
  75. type: "primary",
  76. icon: "ep:plus",
  77. state: true,
  78. permission: ["user:save"]
  79. }
  80. ],
  81. rightBtns: [
  82. {
  83. key: "search",
  84. label: "查询",
  85. icon: "ep:search",
  86. state: true,
  87. options: {
  88. circle: true
  89. }
  90. },
  91. {
  92. key: "refresh",
  93. label: "刷新",
  94. icon: "ep:refresh",
  95. state: true,
  96. options: {
  97. circle: true
  98. }
  99. }
  100. ]
  101. },
  102. tableParam: {
  103. list: [],
  104. columns: [
  105. {
  106. label: "ID",
  107. prop: "id",
  108. slot: "id",
  109. width: 220
  110. },
  111. {
  112. label: "用户名",
  113. prop: "username",
  114. slot: "username"
  115. },
  116. {
  117. label: "状态",
  118. prop: "status",
  119. slot: "status"
  120. },
  121. {
  122. label: "更新时间",
  123. prop: "updateTime",
  124. slot: "updateTime"
  125. },
  126. {
  127. label: "操作",
  128. fixed: "right",
  129. slot: "operation"
  130. }
  131. ],
  132. loading: false,
  133. pagination: {
  134. small: true,
  135. pageSize: 10,
  136. defaultPageSize: 10,
  137. currentPage: 1,
  138. background: true,
  139. total: 0
  140. }
  141. }
  142. });
  143. const btnClickHandle = (val: string) => {
  144. switch (val) {
  145. case "add":
  146. editRef.value!.open({ enable: 1 }, pageData.dataSource, "add");
  147. break;
  148. case "update":
  149. break;
  150. case "search":
  151. _updateSearchState();
  152. break;
  153. case "refresh":
  154. _loadData();
  155. break;
  156. case "switch":
  157. break;
  158. default:
  159. break;
  160. }
  161. };
  162. /**
  163. * 更新搜索表单
  164. * @param data .
  165. */
  166. const _updateSearchFormData = (data: any) => {
  167. pageData.searchParam.searchForm = data;
  168. };
  169. /**
  170. * 点击搜索按钮
  171. */
  172. const _searchForm = (data: any) => {
  173. pageData.searchParam.searchForm = data;
  174. _loadData();
  175. };
  176. /**
  177. * 重置
  178. */
  179. const _resetSearchForm = (data?: any) => {
  180. pageData.searchParam.searchForm = data;
  181. };
  182. /**
  183. * 更新搜索表达的状态
  184. */
  185. const _updateSearchState = () => {
  186. pageData.searchParam.searchState = !pageData.searchParam.searchState;
  187. };
  188. /**
  189. * 分页 - 改变每页条数
  190. * @param val .
  191. */
  192. const handleChangePageSize = (val: any) => {
  193. pageData.tableParam.pagination.pageSize = val;
  194. _loadData();
  195. };
  196. /**
  197. * 分页 - 改变页码
  198. * @param val .
  199. */
  200. const handleChangeCurrentPage = (val: any) => {
  201. pageData.tableParam.pagination.currentPage = val;
  202. _loadData();
  203. };
  204. /**
  205. * 获取查询参数
  206. */
  207. const getQueryParams = () => {
  208. const sqp = {};
  209. const param = Object.assign(sqp, pageData.searchParam.searchForm);
  210. param.current = pageData.tableParam.pagination.currentPage;
  211. param.size = pageData.tableParam.pagination.pageSize;
  212. return param;
  213. };
  214. /**
  215. * 获取列表数据
  216. * @param page .
  217. */
  218. const _loadData = (page?: number) => {
  219. const query = getQueryParams();
  220. if (page) {
  221. query.current = page;
  222. }
  223. pageData.tableParam.loading = true;
  224. $api
  225. .queryPage(query)
  226. .then((res: any) => {
  227. if (res.success) {
  228. pageData.tableParam.list = res.result.records;
  229. pageData.tableParam.pagination.total = Number(res.result.total);
  230. }
  231. })
  232. .finally(() => {
  233. pageData.tableParam.loading = false;
  234. });
  235. };
  236. /**
  237. * 查看详情
  238. * @param row .
  239. */
  240. const handleDetail = (row: any) => {
  241. editRef.value!.open(cloneDeep(row), pageData.dataSource, "detail");
  242. };
  243. /**
  244. * 编辑
  245. * @param row .
  246. */
  247. const handleEdit = (row: any) => {
  248. editRef.value!.open(cloneDeep(row), pageData.dataSource, "edit");
  249. };
  250. /**
  251. * 删除
  252. * @param row .
  253. */
  254. const handleDel = (row: any) => {
  255. message.confirm(`确认删除 id 为 (${row.id}) 的数据?`).then(() => {
  256. batchDel([row.id]);
  257. });
  258. };
  259. /**
  260. * 批量删除
  261. * @param ids .
  262. */
  263. const batchDel = (ids: string[]) => {
  264. if (ids && ids.length > 0) {
  265. $api.del(ids).then((res: any) => {
  266. if (res.success) {
  267. message.success("删除成功");
  268. _loadData();
  269. } else {
  270. message.warning(res.message);
  271. }
  272. });
  273. }
  274. };
  275. watch(
  276. () => props.orgInfo,
  277. val => {
  278. if (val) {
  279. _loadData();
  280. }
  281. }
  282. );
  283. onBeforeMount(() => {});
  284. onMounted(() => {
  285. _loadData(1);
  286. });
  287. </script>
  288. <template>
  289. <div>
  290. <form-search
  291. size="small"
  292. :show="pageData.searchParam.searchState"
  293. :form-field="searchFormFields"
  294. :data-source="pageData.dataSource"
  295. :query-permission="pageData.permission.query"
  296. @search-form="_updateSearchFormData"
  297. @search="_searchForm"
  298. @reset="_resetSearchForm"
  299. />
  300. <!--operate-->
  301. <table-buttons
  302. :size="pageData.btnOpts.size"
  303. :left-btns="pageData.btnOpts.leftBtns"
  304. :right-btns="pageData.btnOpts.rightBtns"
  305. @click="btnClickHandle"
  306. />
  307. <!--table-->
  308. <pure-table
  309. :data="pageData.tableParam.list"
  310. :columns="pageData.tableParam.columns"
  311. row-key="id"
  312. size="small"
  313. border
  314. stripe
  315. :header-row-class-name="'table-header'"
  316. :loading="pageData.tableParam.loading"
  317. :pagination="pageData.tableParam.pagination"
  318. @page-current-change="handleChangeCurrentPage"
  319. @page-size-change="handleChangePageSize"
  320. >
  321. <template #ellipsis="{ row, column }">
  322. <el-tooltip placement="top-start" :content="row[column.property]"
  323. >{{ row[column.property] }}
  324. </el-tooltip>
  325. </template>
  326. <template #enableScope="scope">
  327. <el-tag v-if="scope.row.enable">启用</el-tag>
  328. <el-tag v-else type="info">禁用</el-tag>
  329. </template>
  330. <template #operation="{ row }">
  331. <div class="flex justify-center items-center">
  332. <el-link
  333. v-show="hasAuth(pageData.permission.update)"
  334. type="primary"
  335. @click="handleEdit(row)"
  336. >编辑
  337. </el-link>
  338. <el-divider
  339. v-show="hasAuth(pageData.permission.del)"
  340. direction="vertical"
  341. />
  342. <el-link
  343. v-show="hasAuth(pageData.permission.del)"
  344. type="primary"
  345. @click="handleDel(row)"
  346. >删除
  347. </el-link>
  348. </div>
  349. </template>
  350. </pure-table>
  351. <edit ref="editRef" @ok="_loadData" />
  352. </div>
  353. </template>