Files
iot-device-management-frontend/apps/web-antd/src/views/mp/components/wx-material-select/main.vue

447 lines
9.8 KiB
Vue
Raw Normal View History

2025-11-04 14:31:32 +08:00
<script lang="ts" setup>
2025-11-07 13:21:42 +08:00
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
2025-11-04 14:31:32 +08:00
2025-11-07 13:21:42 +08:00
import { reactive, ref, watch } from 'vue';
import { Page } from '@vben/common-ui';
2025-11-04 14:31:32 +08:00
import { IconifyIcon } from '@vben/icons';
2025-11-07 13:21:42 +08:00
import { Button, Pagination, Row, Spin } from 'ant-design-vue';
2025-11-04 14:31:32 +08:00
2025-11-07 13:21:42 +08:00
import { useVbenVxeGrid } from '#/adapter/vxe-table';
2025-11-04 14:31:32 +08:00
import * as MpDraftApi from '#/api/mp/draft';
import * as MpFreePublishApi from '#/api/mp/freePublish';
import * as MpMaterialApi from '#/api/mp/material';
2025-11-06 15:25:11 +08:00
import { WxNews } from '#/views/mp/components/wx-news';
import { WxVideoPlayer } from '#/views/mp/components/wx-video-play';
import { WxVoicePlayer } from '#/views/mp/components/wx-voice-play';
2025-11-04 14:31:32 +08:00
import { NewsType } from './types';
defineOptions({ name: 'WxMaterialSelect' });
const props = withDefaults(
defineProps<{
accountId: number;
newsType?: NewsType;
type: string;
}>(),
{
newsType: NewsType.Published,
},
);
const emit = defineEmits<{
(e: 'selectMaterial', item: any): void;
}>();
// 遮罩层
const loading = ref(false);
// 总条数
const total = ref(0);
// 数据列表
const list = ref<any[]>([]);
// 查询参数
const queryParams = reactive({
accountId: props.accountId,
pageNo: 1,
pageSize: 10,
});
2025-11-07 13:21:42 +08:00
const voiceGridColumns: VxeTableGridOptions<any>['columns'] = [
2025-11-04 14:31:32 +08:00
{
2025-11-07 13:21:42 +08:00
field: 'mediaId',
2025-11-04 14:31:32 +08:00
title: '编号',
2025-11-07 13:21:42 +08:00
align: 'center',
minWidth: 160,
2025-11-04 14:31:32 +08:00
},
{
2025-11-07 13:21:42 +08:00
field: 'name',
2025-11-04 14:31:32 +08:00
title: '文件名',
2025-11-07 13:21:42 +08:00
minWidth: 200,
2025-11-04 14:31:32 +08:00
},
{
2025-11-07 13:21:42 +08:00
field: 'voice',
2025-11-04 14:31:32 +08:00
title: '语音',
2025-11-07 13:21:42 +08:00
minWidth: 200,
align: 'center',
slots: { default: 'voice' },
2025-11-04 14:31:32 +08:00
},
{
2025-11-07 13:21:42 +08:00
field: 'createTime',
2025-11-04 14:31:32 +08:00
title: '上传时间',
width: 180,
2025-11-07 13:21:42 +08:00
formatter: 'formatDateTime',
2025-11-04 14:31:32 +08:00
},
{
title: '操作',
2025-11-07 13:21:42 +08:00
width: 140,
fixed: 'right',
align: 'center',
slots: { default: 'actions' },
2025-11-04 14:31:32 +08:00
},
];
2025-11-07 13:21:42 +08:00
const videoGridColumns: VxeTableGridOptions<any>['columns'] = [
2025-11-04 14:31:32 +08:00
{
2025-11-07 13:21:42 +08:00
field: 'mediaId',
2025-11-04 14:31:32 +08:00
title: '编号',
2025-11-07 13:21:42 +08:00
minWidth: 160,
2025-11-04 14:31:32 +08:00
},
{
2025-11-07 13:21:42 +08:00
field: 'name',
2025-11-04 14:31:32 +08:00
title: '文件名',
2025-11-07 13:21:42 +08:00
minWidth: 200,
2025-11-04 14:31:32 +08:00
},
{
2025-11-07 13:21:42 +08:00
field: 'title',
2025-11-04 14:31:32 +08:00
title: '标题',
2025-11-07 13:21:42 +08:00
minWidth: 200,
2025-11-04 14:31:32 +08:00
},
{
2025-11-07 13:21:42 +08:00
field: 'introduction',
2025-11-04 14:31:32 +08:00
title: '介绍',
2025-11-07 13:21:42 +08:00
minWidth: 220,
2025-11-04 14:31:32 +08:00
},
{
2025-11-07 13:21:42 +08:00
field: 'video',
2025-11-04 14:31:32 +08:00
title: '视频',
2025-11-07 13:21:42 +08:00
minWidth: 220,
align: 'center',
slots: { default: 'video' },
2025-11-04 14:31:32 +08:00
},
{
2025-11-07 13:21:42 +08:00
field: 'createTime',
2025-11-04 14:31:32 +08:00
title: '上传时间',
width: 180,
2025-11-07 13:21:42 +08:00
formatter: 'formatDateTime',
2025-11-04 14:31:32 +08:00
},
{
title: '操作',
2025-11-07 13:21:42 +08:00
width: 140,
fixed: 'right',
align: 'center',
slots: { default: 'actions' },
2025-11-04 14:31:32 +08:00
},
];
2025-11-07 13:21:42 +08:00
const [VoiceGrid, voiceGridApi] = useVbenVxeGrid({
gridOptions: {
border: true,
columns: voiceGridColumns,
height: 'auto',
keepSource: true,
pagerConfig: {
enabled: true,
pageSize: 10,
},
proxyConfig: {
ajax: {
query: async ({ page }, { accountId }) => {
const finalAccountId = accountId ?? queryParams.accountId;
if (finalAccountId === undefined || finalAccountId === null) {
return { list: [], total: 0 };
}
return await MpMaterialApi.getMaterialPage({
pageNo: page.currentPage,
pageSize: page.pageSize,
accountId: finalAccountId,
type: 'voice',
});
},
},
},
rowConfig: {
keyField: 'mediaId',
isHover: true,
},
toolbarConfig: {
refresh: true,
},
} as VxeTableGridOptions<any>,
});
const [VideoGrid, videoGridApi] = useVbenVxeGrid({
gridOptions: {
border: true,
columns: videoGridColumns,
height: 'auto',
keepSource: true,
pagerConfig: {
enabled: true,
pageSize: 10,
},
proxyConfig: {
ajax: {
query: async ({ page }, { accountId }) => {
const finalAccountId = accountId ?? queryParams.accountId;
if (finalAccountId === undefined || finalAccountId === null) {
return { list: [], total: 0 };
}
return await MpMaterialApi.getMaterialPage({
pageNo: page.currentPage,
pageSize: page.pageSize,
accountId: finalAccountId,
type: 'video',
});
},
},
},
rowConfig: {
keyField: 'mediaId',
isHover: true,
},
toolbarConfig: {
refresh: true,
},
} as VxeTableGridOptions<any>,
2025-11-04 14:31:32 +08:00
});
2025-11-07 13:21:42 +08:00
function selectMaterialFun(item: any) {
emit('selectMaterial', item);
}
async function getMaterialPageFun() {
const data = await MpMaterialApi.getMaterialPage({
...queryParams,
type: props.type,
});
list.value = data.list;
total.value = data.total;
}
async function getFreePublishPageFun() {
const data = await MpFreePublishApi.getFreePublishPage(queryParams);
data.list.forEach((item: any) => {
const articles = item.content.newsItem;
articles.forEach((article: any) => {
article.picUrl = article.thumbUrl;
});
});
list.value = data.list;
total.value = data.total;
}
async function getDraftPageFun() {
const data = await MpDraftApi.getDraftPage(queryParams);
data.list.forEach((draft: any) => {
const articles = draft.content.newsItem;
articles.forEach((article: any) => {
article.picUrl = article.thumbUrl;
});
});
list.value = data.list;
total.value = data.total;
}
async function getPage() {
if (props.type === 'voice') {
await voiceGridApi.reload({ accountId: queryParams.accountId });
return;
}
if (props.type === 'video') {
await videoGridApi.reload({ accountId: queryParams.accountId });
return;
}
loading.value = true;
try {
if (props.type === 'news' && props.newsType === NewsType.Published) {
await getFreePublishPageFun();
} else if (props.type === 'news' && props.newsType === NewsType.Draft) {
await getDraftPageFun();
} else {
await getMaterialPageFun();
}
} finally {
loading.value = false;
}
}
watch(
() => props.accountId,
(accountId) => {
queryParams.accountId = accountId;
queryParams.pageNo = 1;
getPage();
},
{ immediate: true },
);
watch(
() => props.type,
() => {
queryParams.pageNo = 1;
getPage();
},
);
watch(
() => props.newsType,
() => {
if (props.type === 'news') {
queryParams.pageNo = 1;
getPage();
}
},
);
2025-11-04 14:31:32 +08:00
</script>
<template>
2025-11-07 13:21:42 +08:00
<Page :bordered="false" class="pb-8">
2025-11-04 14:31:32 +08:00
<!-- 类型image -->
2025-11-07 13:21:42 +08:00
<template v-if="props.type === 'image'">
2025-11-04 14:31:32 +08:00
<Spin :spinning="loading">
<div class="waterfall">
<div v-for="item in list" :key="item.mediaId" class="waterfall-item">
<img class="material-img" :src="item.url" alt="素材图片" />
<p class="item-name">{{ item.name }}</p>
<Row class="ope-row">
<Button type="primary" @click="selectMaterialFun(item)">
选择
<template #icon>
<IconifyIcon icon="mdi:check-circle" />
</template>
</Button>
</Row>
</div>
</div>
</Spin>
<Pagination
v-model:current="queryParams.pageNo"
v-model:page-size="queryParams.pageSize"
:total="total"
class="mt-4"
2025-11-07 13:21:42 +08:00
@change="getPage"
@show-size-change="getPage"
2025-11-04 14:31:32 +08:00
/>
2025-11-07 13:21:42 +08:00
</template>
2025-11-04 14:31:32 +08:00
<!-- 类型voice -->
2025-11-07 13:21:42 +08:00
<template v-else-if="props.type === 'voice'">
<VoiceGrid>
<template #voice="{ row }">
<WxVoicePlayer :url="row.url" />
2025-11-04 14:31:32 +08:00
</template>
2025-11-07 13:21:42 +08:00
<template #actions="{ row }">
<Button type="link" @click="selectMaterialFun(row)">
选择
<template #icon>
<IconifyIcon icon="mdi:plus" />
</template>
</Button>
</template>
</VoiceGrid>
</template>
2025-11-04 14:31:32 +08:00
<!-- 类型video -->
2025-11-07 13:21:42 +08:00
<template v-else-if="props.type === 'video'">
<VideoGrid>
<template #video="{ row }">
<WxVideoPlayer :url="row.url" />
2025-11-04 14:31:32 +08:00
</template>
2025-11-07 13:21:42 +08:00
<template #actions="{ row }">
<Button type="link" @click="selectMaterialFun(row)">
选择
<template #icon>
<IconifyIcon icon="mdi:plus-circle" />
</template>
</Button>
</template>
</VideoGrid>
</template>
2025-11-04 14:31:32 +08:00
<!-- 类型news -->
2025-11-07 13:21:42 +08:00
<template v-else-if="props.type === 'news'">
2025-11-04 14:31:32 +08:00
<Spin :spinning="loading">
<div class="waterfall">
<div v-for="item in list" :key="item.mediaId" class="waterfall-item">
<div v-if="item.content && item.content.newsItem">
<WxNews :articles="item.content.newsItem" />
<Row class="ope-row">
<Button type="primary" @click="selectMaterialFun(item)">
选择
<template #icon>
<IconifyIcon icon="mdi:check-circle" />
</template>
</Button>
</Row>
</div>
</div>
</div>
</Spin>
<Pagination
v-model:current="queryParams.pageNo"
v-model:page-size="queryParams.pageSize"
:total="total"
class="mt-4"
2025-11-07 13:21:42 +08:00
@change="getPage"
@show-size-change="getPage"
2025-11-04 14:31:32 +08:00
/>
2025-11-07 13:21:42 +08:00
</template>
</Page>
2025-11-04 14:31:32 +08:00
</template>
<style lang="scss" scoped>
@media (width >= 992px) and (width <= 1300px) {
.waterfall {
column-count: 3;
}
p {
color: red;
}
}
@media (width >= 768px) and (width <= 991px) {
.waterfall {
column-count: 2;
}
p {
color: orange;
}
}
@media (width <= 767px) {
.waterfall {
column-count: 1;
}
}
.waterfall {
column-count: 5;
column-gap: 10px;
width: 100%;
margin: 0 auto;
}
.waterfall-item {
padding: 10px;
margin-bottom: 10px;
border: 1px solid #eaeaea;
break-inside: avoid;
}
.material-img {
width: 100%;
}
p {
line-height: 30px;
}
.ope-row {
padding-top: 10px;
text-align: center;
}
.item-name {
overflow: hidden;
text-overflow: ellipsis;
font-size: 12px;
text-align: center;
white-space: nowrap;
}
</style>