index.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <script lang="ts" setup>
  2. import type { OnActionClickParams, VxeTableGridOptions } from '#/adapter';
  3. import { onMounted, reactive, ref, unref } from 'vue';
  4. import { useRoute } from 'vue-router';
  5. import { AccessControl } from '@vben/access';
  6. import { alert, Fallback, Page, useVbenModal } from '@vben/common-ui';
  7. import { Button } from 'ant-design-vue';
  8. import { useTableGridOptions, useVbenVxeGrid } from '#/adapter';
  9. import { CmsArticleApi, CmsSiteChannelApi } from '#/api';
  10. import FormEdit from './components/edit.vue';
  11. import { useColumns, useSearchSchema } from './data.config';
  12. const route = useRoute();
  13. const state = reactive<{
  14. authCode: string;
  15. error: boolean;
  16. siteChannelId: string;
  17. }>({
  18. error: false,
  19. siteChannelId: (route.query?.id || '') as string,
  20. authCode: '',
  21. });
  22. const modelRef = ref<Record<string, any>>({});
  23. const conditionInfo = reactive<Record<string, any>>({});
  24. const [FormEditModal, formEditApi] = useVbenModal({
  25. connectedComponent: FormEdit,
  26. });
  27. const handelSuccess = () => {
  28. reload();
  29. };
  30. const handleDelete = async (id: number) => {
  31. await CmsArticleApi.deleteDetail(id);
  32. alert('数据删除成功');
  33. handelSuccess();
  34. };
  35. const handleUpdateStatus = async (record: any) => {
  36. await CmsArticleApi.updateStatus({
  37. id: record.id,
  38. status: record.status === 1 ? 2 : 1,
  39. });
  40. handelSuccess();
  41. };
  42. const handleEdit = (record: any, isUpdate: boolean) => {
  43. formEditApi
  44. .setData({
  45. isUpdate,
  46. baseData: { id: record.id },
  47. conditionInfo: { ...conditionInfo },
  48. })
  49. .open();
  50. };
  51. const handleActionClick = async ({
  52. code,
  53. row,
  54. }: OnActionClickParams<CmsArticleApi.RecordItem>) => {
  55. switch (code) {
  56. case 'delete': {
  57. await handleDelete(row.id);
  58. break;
  59. }
  60. case 'edit': {
  61. handleEdit(row, true);
  62. break;
  63. }
  64. case 'setStatus': {
  65. await handleUpdateStatus(row);
  66. break;
  67. }
  68. }
  69. };
  70. const [Grid, { setState, reload }] = useVbenVxeGrid(
  71. useTableGridOptions({
  72. formOptions: {
  73. fieldMappingTime: [['__date', ['startTime', 'endTime']]],
  74. schema: [],
  75. },
  76. gridOptions: {
  77. columns: [],
  78. proxyConfig: {
  79. autoLoad: false,
  80. ajax: {
  81. query: async ({ page }, formValues) => {
  82. return await CmsArticleApi.getPage({
  83. pageIndex: page.currentPage,
  84. pageSize: page.pageSize,
  85. ...formValues,
  86. ...conditionInfo,
  87. });
  88. },
  89. },
  90. },
  91. } as VxeTableGridOptions,
  92. }),
  93. );
  94. const loadPage = async () => {
  95. state.authCode = `${unref(modelRef).code}-content`;
  96. const schema = useSearchSchema(conditionInfo);
  97. const columns: VxeTableGridOptions<CmsArticleApi.RecordItem>['columns'] =
  98. useColumns(handleActionClick, state.authCode);
  99. setState({ formOptions: { schema }, gridOptions: { columns } });
  100. setTimeout(() => {
  101. reload();
  102. }, 500);
  103. };
  104. onMounted(async () => {
  105. try {
  106. modelRef.value = await CmsSiteChannelApi.getDetail(
  107. Number(state.siteChannelId),
  108. );
  109. conditionInfo.siteId = modelRef.value.siteId;
  110. conditionInfo.siteChannelId = modelRef.value.id;
  111. loadPage();
  112. } catch {
  113. state.error = true;
  114. }
  115. });
  116. </script>
  117. <template>
  118. <Page auto-content-height>
  119. <Fallback status="500" v-if="state.error">
  120. <template #action></template>
  121. </Fallback>
  122. <template v-else>
  123. <FormEditModal @success="handelSuccess" />
  124. <Grid>
  125. <template #toolbar-tools>
  126. <AccessControl :codes="[`${state.authCode}:add`]" type="code">
  127. <Button
  128. class="mr-2"
  129. type="primary"
  130. @click="() => handleEdit({}, false)"
  131. >
  132. 新增内容
  133. </Button>
  134. </AccessControl>
  135. </template>
  136. </Grid>
  137. </template>
  138. </Page>
  139. </template>