gaoluyang
2026-06-24 712aa51536236d43e87273e4ce45ac5691dffad8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<script lang="ts" setup>
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { MpMaterialApi } from '#/api/mp/material';
 
import { provide, ref } from 'vue';
 
import { useAccess } from '..\..\..\packages\effects\access\src';
import { confirm, DocAlert, Page } from '..\..\..\packages\effects\common-ui\src';
import { IconifyIcon } from '..\..\..\packages\icons\src';
import { $t } from '..\..\..\packages\locales\src';
 
import { Button, message, Tabs } from 'ant-design-vue';
 
import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
import { deletePermanentMaterial, getMaterialPage } from '#/api/mp/material';
import { WxAccountSelect } from '#/views/mp/components';
 
import {
  useGridFormSchema,
  useImageGridColumns,
  useVideoGridColumns,
  useVoiceGridColumns,
} from './modules/data';
import { UploadType } from './modules/upload';
import UploadFile from './modules/UploadFile.vue';
import UploadVideo from './modules/UploadVideo.vue';
 
defineOptions({ name: 'MpMaterial' });
 
const { hasAccessByCodes } = useAccess();
 
const type = ref<UploadType>(UploadType.Image); // 素材类型
const showCreateVideo = ref(false); // 是否新建视频的弹窗
 
const accountId = ref(-1);
provide('accountId', accountId);
 
// 根据类型获取对应的列配置
const getColumnsByType = () => {
  switch (type.value) {
    case UploadType.Image: {
      return useImageGridColumns();
    }
    case UploadType.Video: {
      return useVideoGridColumns();
    }
    case UploadType.Voice: {
      return useVoiceGridColumns();
    }
    default: {
      return [];
    }
  }
};
 
const [Grid, gridApi] = useVbenVxeGrid({
  formOptions: {
    schema: useGridFormSchema(),
  },
  gridOptions: {
    columns: getColumnsByType(),
    height: 'auto',
    keepSource: true,
    pagerConfig: {},
    proxyConfig: {
      ajax: {
        query: async ({ page }, formValues) => {
          const finalAccountId = formValues?.accountId ?? accountId.value;
          if (!finalAccountId || finalAccountId === -1) {
            return { list: [], total: 0 };
          }
          return await getMaterialPage({
            pageNo: page.currentPage,
            pageSize: page.pageSize,
            type: type.value,
            permanent: true,
            accountId: finalAccountId,
            ...formValues,
          });
        },
      },
      autoLoad: false,
    },
    rowConfig: {
      keyField: 'id',
      isHover: true,
    },
    cellConfig: {
      height: type.value === UploadType.Image ? 220 : undefined,
    },
    toolbarConfig: {
      refresh: true,
      search: true,
    },
  } as VxeTableGridOptions<MpMaterialApi.Material>,
});
 
// 当 tab 切换时,更新 Grid 的 columns 和 rowConfig
async function onTabChange() {
  const columns = getColumnsByType();
  gridApi.setGridOptions({
    columns,
    rowConfig: {
      keyField: 'id',
      isHover: true,
    },
    cellConfig: {
      height: type.value === UploadType.Image ? 220 : undefined,
    },
  });
  await gridApi.reload();
}
 
async function handleAccountChange(id: number) {
  accountId.value = id;
  // 同步设置表单值
  await gridApi.formApi.setValues({ accountId: id });
  await gridApi.formApi.submitForm();
}
 
async function handleRefresh() {
  await gridApi.query();
}
 
/** 处理删除操作 */
async function handleDelete(id: number) {
  await confirm('此操作将永久删除该文件, 是否继续?');
  const hideLoading = message.loading({
    content: '正在删除...',
    duration: 0,
  });
  try {
    await deletePermanentMaterial(id);
    message.success('删除成功');
    await handleRefresh();
  } finally {
    hideLoading();
  }
}
</script>
 
<template>
  <Page auto-content-height>
    <template #doc>
      <DocAlert title="公众号素材" url="https://doc.iocoder.cn/mp/material/" />
    </template>
    <Grid class="material-grid">
      <template #form-accountId>
        <WxAccountSelect @change="handleAccountChange" />
      </template>
      <template #toolbar-actions>
        <Tabs v-model:active-key="type" class="w-full" @change="onTabChange">
          <!-- tab 1:图片  -->
          <Tabs.TabPane :key="UploadType.Image">
            <template #tab>
              <span class="flex items-center">
                <IconifyIcon icon="lucide:image" class="mr-1" />
                图片
              </span>
            </template>
          </Tabs.TabPane>
 
          <!-- tab 2:语音  -->
          <Tabs.TabPane :key="UploadType.Voice">
            <template #tab>
              <span class="flex items-center">
                <IconifyIcon icon="lucide:mic" class="mr-1" />
                语音
              </span>
            </template>
          </Tabs.TabPane>
 
          <!-- tab 3:视频 -->
          <Tabs.TabPane :key="UploadType.Video">
            <template #tab>
              <span class="flex items-center">
                <IconifyIcon icon="lucide:video" class="mr-1" />
                视频
              </span>
            </template>
          </Tabs.TabPane>
        </Tabs>
      </template>
      <template #toolbar-tools>
        <UploadFile
          v-if="
            hasAccessByCodes(['mp:material:upload-permanent']) &&
            type === UploadType.Image
          "
          :type="UploadType.Image"
          @uploaded="handleRefresh"
        />
        <UploadFile
          v-if="
            hasAccessByCodes(['mp:material:upload-permanent']) &&
            type === UploadType.Voice
          "
          :type="UploadType.Voice"
          @uploaded="handleRefresh"
        />
        <Button
          v-if="
            hasAccessByCodes(['mp:material:upload-permanent']) &&
            type === UploadType.Video
          "
          type="primary"
          @click="showCreateVideo = true"
        >
          新建视频
        </Button>
      </template>
 
      <!-- 图片列的 slot -->
      <template #image="{ row }">
        <div class="flex items-center justify-center" style="height: 192px">
          <img
            :src="row.url"
            class="object-contain"
            style="display: block; max-width: 100%; max-height: 192px"
          />
        </div>
      </template>
 
      <!-- 语音列的 slot -->
      <template #voice="{ row }">
        <audio :src="row.url" controls style="width: 160px"></audio>
      </template>
 
      <!-- 视频列的 slot -->
      <template #video="{ row }">
        <video
          :src="row.url"
          controls
          style="width: 200px; height: 150px"
        ></video>
      </template>
 
      <!-- 操作列的 slot -->
      <template #actions="{ row }">
        <TableAction
          :actions="[
            {
              label: $t('common.delete'),
              type: 'link',
              danger: true,
              icon: ACTION_ICON.DELETE,
              auth: ['mp:material:delete'],
              onClick: () => handleDelete(row.id!),
            },
          ]"
        />
      </template>
    </Grid>
 
    <!-- 新建视频的弹窗 -->
    <UploadVideo v-model:open="showCreateVideo" @uploaded="handleRefresh" />
  </Page>
</template>