index.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <template>
  2. <CollapseContainer
  3. class="sys-container"
  4. title="个人管理"
  5. :canExpan="false"
  6. helpMessage="个人管理"
  7. >
  8. <BasicTable
  9. ref="tableRef"
  10. @register="registerTable"
  11. rowKey="id"
  12. @selectionChange="selectionChange"
  13. @rowClick="rowClick"
  14. @rowDbClick="handleEdit"
  15. showTableSetting
  16. :canResize="true"
  17. :pagination="{
  18. pageSize: 10,
  19. defaultPageSize: 10,
  20. showSizeChanger: false,
  21. }"
  22. >
  23. <template #toolbar>
  24. <a-button type="primary" @click="addRole"> 添加 </a-button>
  25. <a-button color="error" :disabled="disable_btn" @click="deleteBatches"> 删除 </a-button>
  26. <a-button @click="openModal"> 导出 </a-button>
  27. </template>
  28. <template #form-custom> custom-slot </template>
  29. <template #action="{ record }">
  30. <TableAction :actions="createActions(record)" stopButtonPropagation />
  31. </template>
  32. </BasicTable>
  33. <ExpExcelModel @register="register" @success="defaultHeader" />
  34. <Popup @register="addRegister" :popupData="popupData" @saveData="saveData" />
  35. </CollapseContainer>
  36. </template>
  37. <script lang="ts">
  38. import { defineComponent, reactive, ref, toRefs, unref, createVNode } from 'vue';
  39. import { CollapseContainer } from '/@/components/Container/index';
  40. import Popup from './popup.vue';
  41. import { useMessage } from '/@/hooks/web/useMessage';
  42. import { useModal } from '/@/components/Modal';
  43. import { jsonToSheetXlsx, ExpExcelModel, ExportModalResult } from '/@/components/Excel';
  44. import { Modal } from 'ant-design-vue';
  45. import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
  46. import { getFormConfig, columns } from './data';
  47. import {
  48. getPersonList,
  49. addPerson,
  50. deleteBatchesPerson,
  51. // getPerson,
  52. editPerson,
  53. deletePerson,
  54. } from '/@/api/sys/member';
  55. import {
  56. BasicTable,
  57. useTable,
  58. TableAction,
  59. ActionItem,
  60. EditRecordRow,
  61. TableActionType,
  62. } from '/@/components/Table';
  63. interface PopupData {
  64. title: string;
  65. }
  66. interface Btn {
  67. disable_btn: boolean;
  68. }
  69. export default defineComponent({
  70. name: 'Person',
  71. components: { CollapseContainer, BasicTable, TableAction, Popup, ExpExcelModel },
  72. setup() {
  73. const { createMessage } = useMessage();
  74. const { success /*, error*/ } = createMessage;
  75. const tableRef = ref<Nullable<TableActionType>>(null);
  76. const currentEditKeyRef = ref('');
  77. const popupData = reactive<PopupData>({
  78. title: '添加',
  79. });
  80. const btn = reactive<Btn>({
  81. disable_btn: true,
  82. });
  83. const [registerTable] = useTable({
  84. rowSelection: { type: 'checkbox' },
  85. columns: columns,
  86. // clickToRowSelect: false, // 点击行不勾选
  87. api: getPersonList,
  88. useSearchForm: true,
  89. beforeFetch: beforeFetch,
  90. afterFetch: afterFetch,
  91. formConfig: getFormConfig(),
  92. actionColumn: {
  93. width: 160,
  94. title: '操作',
  95. dataIndex: 'action',
  96. slots: { customRender: 'action' },
  97. fixed: undefined,
  98. },
  99. showIndexColumn: false,
  100. bordered: true,
  101. });
  102. const [register, { openModal }] = useModal();
  103. const [addRegister, { openModal: openPopup }] = useModal();
  104. function getTableAction() {
  105. // 获取组件
  106. const tableAction = unref(tableRef);
  107. if (!tableAction) {
  108. throw new Error('tableAction is null');
  109. }
  110. return tableAction;
  111. }
  112. // 请求之前处理参数
  113. function beforeFetch(params) {
  114. console.log(params);
  115. console.log('==========before========');
  116. for (let k in params) {
  117. if (!params[k]) {
  118. delete params[k];
  119. }
  120. }
  121. if (params.logintime) {
  122. params.logintime[0] = params.logintime[0].replace(
  123. /([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])/g,
  124. '00:00:00'
  125. );
  126. params.logintime[1] = params.logintime[1].replace(
  127. /([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])/g,
  128. '23:59:59'
  129. );
  130. }
  131. params.offset = params.page;
  132. params.limit = params.pageSize;
  133. delete params.page;
  134. delete params.pageSize;
  135. }
  136. function afterFetch(result) {
  137. result.map((item) => {
  138. if (item.groups) {
  139. item.groups_value = [];
  140. item.groups.map((item_group) => {
  141. item.groups_value.push({ lable: item_group.name, value: item_group.id });
  142. });
  143. } else {
  144. item.groups = []; //null
  145. item.groups_text = [];
  146. }
  147. });
  148. console.log(`result`, result);
  149. }
  150. function addRole() {
  151. popupData.title = '添加';
  152. openPopup(true, {});
  153. }
  154. function handleEdit(record: EditRecordRow) {
  155. currentEditKeyRef.value = record.id; // record.key
  156. popupData.title = '编辑';
  157. const data = getTableAction().getDataSource();
  158. data.map((item) => {
  159. if (item.id === record.id) {
  160. record = item;
  161. }
  162. });
  163. openPopup(true, record);
  164. }
  165. async function handleDelete(record: Recordable) {
  166. console.log(record);
  167. await deletePerson({ id: record.id }).then((res) => {
  168. console.log(res);
  169. getTableAction().reload();
  170. success('删除成功!');
  171. });
  172. }
  173. function selectionChange() {
  174. const keys = getTableAction().getSelectRowKeys();
  175. if (keys.length) {
  176. btn.disable_btn = false;
  177. } else {
  178. btn.disable_btn = true;
  179. }
  180. }
  181. function rowClick() {
  182. const keys = getTableAction().getSelectRowKeys();
  183. if (keys.length) {
  184. btn.disable_btn = false;
  185. } else {
  186. btn.disable_btn = true;
  187. }
  188. }
  189. async function deleteBatches() {
  190. const keys = await getTableAction().getSelectRowKeys();
  191. const count = keys.length;
  192. const ids = keys.toString();
  193. if (!ids) {
  194. return;
  195. }
  196. Modal.confirm({
  197. title: '删除提示',
  198. icon: createVNode(ExclamationCircleOutlined),
  199. content: '确定删除选中的' + count + '项?',
  200. okText: '确定',
  201. okType: 'danger',
  202. cancelText: '取消',
  203. maskClosable: true,
  204. async onOk() {
  205. await deleteBatchesPerson({ ids }).then((res) => {
  206. console.log(res);
  207. getTableAction().reload();
  208. success('删除成功!');
  209. getTableAction().setSelectedRowKeys([]);
  210. });
  211. },
  212. onCancel() {
  213. console.log('Cancel');
  214. },
  215. });
  216. }
  217. async function saveData(params: any) {
  218. const data = params.data;
  219. const closeModel = params.closeModal;
  220. console.log(`data`, data);
  221. if (!data.id) {
  222. await addPerson(data).then((res) => {
  223. console.log(res);
  224. getTableAction().reload();
  225. closeModel();
  226. success('创建成功!');
  227. });
  228. console.log('----------add---');
  229. } else {
  230. await editPerson(data).then((res) => {
  231. console.log(res);
  232. getTableAction().reload();
  233. closeModel();
  234. success('修改成功!');
  235. });
  236. console.log('----------edit---');
  237. }
  238. }
  239. // 导出
  240. function defaultHeader({ filename, bookType }: ExportModalResult) {
  241. // 默认Object.keys(data[0])作为header
  242. jsonToSheetXlsx({
  243. data: getTableAction().getDataSource(),
  244. filename,
  245. write2excelOpts: {
  246. bookType,
  247. },
  248. });
  249. }
  250. function createActions(record: EditRecordRow): ActionItem[] {
  251. return [
  252. {
  253. label: '编辑',
  254. icon: 'ant-design:edit-outlined',
  255. color: 'warning',
  256. onClick: handleEdit.bind(null, record),
  257. },
  258. {
  259. label: '删除',
  260. color: 'error',
  261. icon: 'ic:outline-delete-outline',
  262. popConfirm: {
  263. title: '是否确认删除',
  264. confirm: handleDelete.bind(null, record),
  265. },
  266. },
  267. ];
  268. }
  269. return {
  270. popupData,
  271. tableRef,
  272. registerTable,
  273. addRole,
  274. handleEdit,
  275. deleteBatches,
  276. createActions,
  277. getTableAction,
  278. rowClick,
  279. selectionChange,
  280. addRegister,
  281. saveData,
  282. defaultHeader,
  283. openModal,
  284. register,
  285. ...toRefs(btn),
  286. };
  287. },
  288. });
  289. </script>
  290. <style>
  291. .ant-calendar-picker {
  292. width: 100%;
  293. }
  294. @media (max-width: 639px) {
  295. .sys-container .vben-basic-table-header__toolbar > * {
  296. margin-right: 3px;
  297. }
  298. .sys-container .vben-basic-table .ant-table-wrapper {
  299. padding: 3px;
  300. }
  301. }
  302. </style>