513 lines
15 KiB
Vue
Raw Normal View History

<template>
2023-04-09 09:56:42 +08:00
<doc-alert title="公众号素材" url="https://doc.iocoder.cn/mp/material/" />
<!-- 搜索工作栏 -->
<ContentWrap>
<el-form class="-mb-15px" :inline="true" label-width="68px">
<el-form-item label="公众号" prop="accountId">
2023-04-14 15:22:23 +08:00
<WxAccountSelect @change="onAccountChanged" />
</el-form-item>
</el-form>
2023-04-09 09:56:42 +08:00
</ContentWrap>
2023-04-08 23:40:21 +08:00
2023-04-09 09:56:42 +08:00
<ContentWrap>
2023-04-12 13:29:24 +08:00
<el-tabs v-model="type" @tab-change="onTabChange">
2023-04-08 23:40:21 +08:00
<!-- tab 1图片 -->
<el-tab-pane name="image">
<template #label>
<span> <Icon icon="ep:picture" />图片 </span>
2023-04-08 23:40:21 +08:00
</template>
<div class="add_but" v-hasPermi="['mp:material:upload-permanent']">
<el-upload
:action="uploadUrl"
2023-04-08 23:40:21 +08:00
:headers="headers"
multiple
:limit="1"
:file-list="fileList"
:data="uploadData"
:before-upload="beforeImageUpload"
:on-success="handleUploadSuccess"
>
2023-04-09 09:56:42 +08:00
<el-button type="primary" plain>点击上传</el-button>
2023-04-08 23:40:21 +08:00
<template #tip>
2023-04-09 09:56:42 +08:00
<span class="el-upload__tip" style="margin-left: 5px">
支持 bmp/png/jpeg/jpg/gif 格式大小不超过 2M
</span>
2023-04-08 23:40:21 +08:00
</template>
</el-upload>
</div>
<div class="waterfall" v-loading="loading">
<div class="waterfall-item" v-for="item in list" :key="item.id">
<a target="_blank" :href="item.url">
<img class="material-img" :src="item.url" />
<div class="item-name">{{ item.name }}</div>
</a>
<el-row justify="center">
2023-04-08 23:40:21 +08:00
<el-button
type="danger"
circle
@click="handleDelete(item)"
v-hasPermi="['mp:material:delete']"
2023-04-09 09:56:42 +08:00
>
<Icon icon="ep:delete" />
</el-button>
2023-04-08 23:40:21 +08:00
</el-row>
</div>
</div>
<!-- 分页组件 -->
2023-04-09 09:56:42 +08:00
<Pagination
2023-04-08 23:40:21 +08:00
:total="total"
v-model:page="queryParams.pageNo"
v-model:limit="queryParams.pageSize"
@pagination="getList"
/>
</el-tab-pane>
<!-- tab 2语音 -->
<el-tab-pane name="voice">
<template #label>
<span> <Icon icon="ep:microphone" />语音 </span>
2023-04-08 23:40:21 +08:00
</template>
<div class="add_but" v-hasPermi="['mp:material:upload-permanent']">
<el-upload
:action="uploadUrl"
2023-04-08 23:40:21 +08:00
:headers="headers"
multiple
:limit="1"
:file-list="fileList"
:data="uploadData"
:on-success="handleUploadSuccess"
:before-upload="beforeVoiceUpload"
>
2023-04-09 09:56:42 +08:00
<el-button type="primary" plain>点击上传</el-button>
2023-04-08 23:40:21 +08:00
<template #tip>
2023-04-09 09:56:42 +08:00
<span class="el-upload__tip" style="margin-left: 5px">
格式支持 mp3/wma/wav/amr文件大小不超过 2M播放长度不超过 60s
</span>
2023-04-08 23:40:21 +08:00
</template>
</el-upload>
</div>
<!-- 列表 -->
2023-04-08 23:40:21 +08:00
<el-table :data="list" stripe border v-loading="loading" style="margin-top: 10px">
<el-table-column label="编号" align="center" prop="mediaId" />
<el-table-column label="文件名" align="center" prop="name" />
<el-table-column label="语音" align="center">
<template #default="scope">
2023-04-12 13:29:24 +08:00
<WxVoicePlayer v-if="scope.row.url" :url="scope.row.url" />
2023-04-08 23:40:21 +08:00
</template>
</el-table-column>
<el-table-column
label="上传时间"
align="center"
prop="createTime"
:formatter="dateFormatter"
width="180"
>
2023-04-08 23:40:21 +08:00
<template #default="scope">
<span>{{ scope.row.createTime }}</span>
2023-04-08 23:40:21 +08:00
</template>
</el-table-column>
<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
<template #default="scope">
2023-04-09 09:56:42 +08:00
<el-button type="primary" link plain @click="handleDownload(scope.row)">
<Icon icon="ep:download" />下载
</el-button>
2023-04-08 23:40:21 +08:00
<el-button
type="primary"
link
plain
@click="handleDelete(scope.row)"
v-hasPermi="['mp:material:delete']"
>
2023-04-09 09:56:42 +08:00
<Icon icon="ep:delete" />删除
</el-button>
2023-04-08 23:40:21 +08:00
</template>
</el-table-column>
</el-table>
<!-- 分页组件 -->
2023-04-09 09:56:42 +08:00
<Pagination
2023-04-08 23:40:21 +08:00
:total="total"
v-model:page="queryParams.pageNo"
v-model:limit="queryParams.pageSize"
@pagination="getList"
/>
</el-tab-pane>
<!-- tab 3视频 -->
<el-tab-pane name="video">
<template #label>
<span> <Icon icon="ep:video-play" /> 视频 </span>
2023-04-08 23:40:21 +08:00
</template>
<div class="add_but" v-hasPermi="['mp:material:upload-permanent']">
2023-04-09 09:56:42 +08:00
<el-button type="primary" plain @click="handleAddVideo">新建视频</el-button>
2023-04-08 23:40:21 +08:00
</div>
<!-- 新建视频的弹窗 -->
<el-dialog
title="新建视频"
v-model="dialogVideoVisible"
width="600px"
v-loading="addMaterialLoading"
>
<el-upload
:action="uploadUrl"
2023-04-08 23:40:21 +08:00
:headers="headers"
multiple
:limit="1"
:file-list="fileList"
:data="uploadData"
:before-upload="beforeVideoUpload"
:on-success="handleUploadSuccess"
ref="uploadVideoRef"
:auto-upload="false"
>
<template #trigger>
<el-button size="small" type="primary">选择视频</el-button>
</template>
<span class="el-upload__tip" style="margin-left: 10px"
>格式支持 MP4文件大小不超过 10MB</span
>
</el-upload>
<el-form :model="uploadData" :rules="uploadRules" ref="uploadFormRef" label-width="80px">
<el-row>
<el-form-item label="标题" prop="title">
<el-input
v-model="uploadData.title"
placeholder="标题将展示在相关播放页面,建议填写清晰、准确、生动的标题"
/>
</el-form-item>
</el-row>
<el-row>
<el-form-item label="描述" prop="introduction">
<el-input
:rows="3"
type="textarea"
v-model="uploadData.introduction"
placeholder="介绍语将展示在相关播放页面,建议填写简洁明确、有信息量的内容"
/>
</el-form-item>
</el-row>
</el-form>
<template #footer>
2023-04-09 18:49:54 +08:00
<el-button @click="cancelVideo"> </el-button>
<el-button type="primary" @click="submitVideo"> </el-button>
2023-04-08 23:40:21 +08:00
</template>
</el-dialog>
<!-- 列表 -->
2023-04-08 23:40:21 +08:00
<el-table :data="list" stripe border v-loading="loading" style="margin-top: 10px">
<el-table-column label="编号" align="center" prop="mediaId" />
<el-table-column label="文件名" align="center" prop="name" />
<el-table-column label="标题" align="center" prop="title" />
<el-table-column label="介绍" align="center" prop="introduction" />
<el-table-column label="视频" align="center">
<template #default="scope">
2023-04-12 13:29:24 +08:00
<WxVideoPlayer v-if="scope.row.url" :url="scope.row.url" />
2023-04-08 23:40:21 +08:00
</template>
</el-table-column>
<el-table-column
label="上传时间"
align="center"
:formatter="dateFormatter"
prop="createTime"
width="180"
>
2023-04-08 23:40:21 +08:00
<template #default="scope">
<span>{{ scope.row.createTime }}</span>
2023-04-08 23:40:21 +08:00
</template>
</el-table-column>
2023-04-09 09:56:42 +08:00
<el-table-column label="操作" align="center" fixed="right">
2023-04-08 23:40:21 +08:00
<template #default="scope">
<el-button type="primary" link plain @click="handleDownload(scope.row)">
<Icon icon="ep:download" />下载
</el-button>
2023-04-08 23:40:21 +08:00
<el-button
type="primary"
link
size="small"
plain
@click="handleDelete(scope.row)"
v-hasPermi="['mp:material:delete']"
>
2023-04-09 09:56:42 +08:00
<Icon icon="ep:delete" />删除
</el-button>
2023-04-08 23:40:21 +08:00
</template>
</el-table-column>
</el-table>
<!-- 分页组件 -->
2023-04-09 09:56:42 +08:00
<Pagination
2023-04-08 23:40:21 +08:00
:total="total"
v-model:page="queryParams.pageNo"
v-model:limit="queryParams.pageSize"
@pagination="getList"
/>
</el-tab-pane>
</el-tabs>
2023-04-09 09:56:42 +08:00
</ContentWrap>
</template>
<script lang="ts" setup name="MpMaterial">
2023-04-08 23:40:21 +08:00
import WxVoicePlayer from '@/views/mp/components/wx-voice-play/main.vue'
import WxVideoPlayer from '@/views/mp/components/wx-video-play/main.vue'
2023-04-14 15:22:23 +08:00
import WxAccountSelect from '@/views/mp/components/wx-account-select/main.vue'
import * as MpMaterialApi from '@/api/mp/material'
import * as authUtil from '@/utils/auth'
import { dateFormatter } from '@/utils/formatTime'
import type {
FormInstance,
FormRules,
TabPaneName,
UploadInstance,
UploadProps,
UploadRawFile,
UploadUserFile
} from 'element-plus'
2023-04-08 23:40:21 +08:00
const BASE_URL = import.meta.env.VITE_BASE_URL
const uploadUrl = BASE_URL + '/admin-api/mp/material/upload-permanent'
const headers = { Authorization: 'Bearer ' + authUtil.getAccessToken() }
const message = useMessage()
const uploadFormRef = ref<FormInstance>()
const uploadVideoRef = ref<UploadInstance>()
2023-04-08 23:40:21 +08:00
const uploadRules: FormRules = {
title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
introduction: [{ required: true, message: '请输入描述', trigger: 'blur' }]
}
// 素材类型
2023-04-13 22:25:35 +08:00
type MaterialType = 'image' | 'voice' | 'video'
const type = ref<MaterialType>('image')
const loading = ref(false) // 遮罩层
const list = ref<any[]>([]) // 总条数
const total = ref(0) // 数据列表
2023-04-08 23:40:21 +08:00
// 查询参数
interface QueryParams {
pageNo: number
pageSize: number
accountId?: number
permanent: boolean
}
const queryParams: QueryParams = reactive({
2023-04-08 23:40:21 +08:00
pageNo: 1,
pageSize: 10,
accountId: undefined,
permanent: true
})
const fileList = ref<UploadUserFile[]>([])
interface UploadData {
2023-04-13 22:25:35 +08:00
type: MaterialType
title: string
introduction: string
}
const uploadData: UploadData = reactive({
2023-04-08 23:40:21 +08:00
type: 'image',
title: '',
introduction: ''
})
// === 视频上传,独有变量 ===
const dialogVideoVisible = ref(false)
const addMaterialLoading = ref(false)
/** 侦听公众号变化 **/
2023-04-12 13:29:24 +08:00
const onAccountChanged = (id?: number) => {
queryParams.accountId = id
getList()
}
2023-04-08 23:40:21 +08:00
// ======================== 列表查询 ========================
/** 查询列表 */
const getList = async () => {
2023-04-08 23:40:21 +08:00
loading.value = true
try {
const data = await MpMaterialApi.getMaterialPage({
...queryParams,
type: type.value
2023-04-08 23:40:21 +08:00
})
list.value = data.list
total.value = data.total
} finally {
loading.value = false
}
2023-04-08 23:40:21 +08:00
}
/** 搜索按钮操作 */
const handleQuery = () => {
queryParams.pageNo = 1
getList()
}
2023-04-12 13:29:24 +08:00
const onTabChange = (tabName: TabPaneName) => {
2023-04-08 23:40:21 +08:00
// 设置 type
2023-04-13 22:25:35 +08:00
uploadData.type = tabName as MaterialType
// 提前情况数据避免tab切换后显示垃圾数据
list.value = []
total.value = 0
2023-04-08 23:40:21 +08:00
// 从第一页开始查询
handleQuery()
}
// ======================== 文件上传 ========================
2023-04-12 13:29:24 +08:00
const beforeUpload = (rawFile: UploadRawFile, category: 'image' | 'audio' | 'video'): boolean => {
let allowTypes: string[] = []
let maxSizeMB = 0
let name = ''
switch (category) {
case 'image':
allowTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/bmp', 'image/jpg']
maxSizeMB = 2
name = '图片'
break
case 'audio':
allowTypes = ['audio/mp3', 'audio/mpeg', 'audio/wma', 'audio/wav', 'audio/amr']
maxSizeMB = 2
name = '图片'
break
case 'video':
allowTypes = ['video/mp4']
maxSizeMB = 10
name = '视频'
2023-04-08 23:40:21 +08:00
}
2023-04-12 13:29:24 +08:00
if (!allowTypes.includes(rawFile.type)) {
message.error(`上传${name}格式不对!`)
2023-04-08 23:40:21 +08:00
return false
}
2023-04-12 13:29:24 +08:00
// 校验大小
if (rawFile.size / 1024 / 1024 > maxSizeMB) {
message.error(`上传${name}大小不能超过${maxSizeMB}M!`)
2023-04-08 23:40:21 +08:00
return false
}
loading.value = true
return true
}
2023-04-12 13:29:24 +08:00
const beforeImageUpload: UploadProps['beforeUpload'] = (rawFile: UploadRawFile) =>
beforeUpload(rawFile, 'image')
2023-04-12 13:29:24 +08:00
const beforeVoiceUpload: UploadProps['beforeUpload'] = (rawFile: UploadRawFile) =>
beforeUpload(rawFile, 'audio')
2023-04-12 13:29:24 +08:00
const beforeVideoUpload: UploadProps['beforeUpload'] = (rawFile: UploadRawFile) =>
beforeUpload(rawFile, 'video')
2023-04-08 23:40:21 +08:00
const handleUploadSuccess: UploadProps['onSuccess'] = (response: any) => {
2023-04-08 23:40:21 +08:00
loading.value = false
addMaterialLoading.value = false
if (response.code !== 0) {
message.error('上传出错:' + response.msg)
return false
}
// 清空上传时的各种数据
dialogVideoVisible.value = false
fileList.value = []
uploadData.title = ''
uploadData.introduction = ''
// 加载数据
getList()
}
// 下载文件
const handleDownload = (row: any) => {
2023-04-08 23:40:21 +08:00
window.open(row.url, '_blank')
}
// 提交 video 新建的表单
const submitVideo = () => {
uploadFormRef.value?.validate((valid) => {
2023-04-08 23:40:21 +08:00
if (!valid) {
return false
}
uploadVideoRef.value?.submit()
2023-04-08 23:40:21 +08:00
})
}
2023-04-12 13:29:24 +08:00
// 弹出 video 新建的表单
2023-04-08 23:40:21 +08:00
const handleAddVideo = () => {
resetVideo()
dialogVideoVisible.value = true
}
/** 取消按钮 */
const cancelVideo = () => {
dialogVideoVisible.value = false
resetVideo()
}
/** 表单重置 */
const resetVideo = () => {
fileList.value = []
uploadData.title = ''
uploadData.introduction = ''
uploadFormRef.value?.resetFields()
}
// ======================== 其它操作 ========================
const handleDelete = async (item: any) => {
2023-04-08 23:40:21 +08:00
await message.confirm('此操作将永久删除该文件, 是否继续?')
await MpMaterialApi.deletePermanentMaterial(item.id)
2023-04-08 23:40:21 +08:00
message.alertSuccess('删除成功')
}
</script>
<style lang="scss" scoped>
/*瀑布流样式*/
.waterfall {
width: 100%;
column-gap: 10px;
column-count: 5;
margin-top: 10px;
/* 芋道源码:增加 10px避免顶着上面 */
2023-04-08 23:40:21 +08:00
}
2023-04-08 23:40:21 +08:00
.waterfall-item {
padding: 10px;
margin-bottom: 10px;
break-inside: avoid;
border: 1px solid #eaeaea;
}
2023-04-08 23:40:21 +08:00
.material-img {
width: 100%;
}
2023-04-08 23:40:21 +08:00
p {
line-height: 30px;
}
2023-04-08 23:40:21 +08:00
@media (min-width: 992px) and (max-width: 1300px) {
.waterfall {
column-count: 3;
}
2023-04-08 23:40:21 +08:00
p {
color: red;
}
}
2023-04-08 23:40:21 +08:00
@media (min-width: 768px) and (max-width: 991px) {
.waterfall {
column-count: 2;
}
2023-04-08 23:40:21 +08:00
p {
color: orange;
}
}
2023-04-08 23:40:21 +08:00
@media (max-width: 767px) {
.waterfall {
column-count: 1;
}
}
</style>