index.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. <template>
  2. <doc-alert title="公众号素材" url="https://doc.iocoder.cn/mp/material/" />
  3. <!-- 搜索工作栏 -->
  4. <ContentWrap>
  5. <el-form
  6. class="-mb-15px"
  7. :model="queryParams"
  8. ref="queryFormRef"
  9. :inline="true"
  10. label-width="68px"
  11. >
  12. <el-form-item label="公众号" prop="accountId">
  13. <el-select v-model="queryParams.accountId" placeholder="请选择公众号" class="!w-240px">
  14. <el-option
  15. v-for="item in accountList"
  16. :key="item.id"
  17. :label="item.name"
  18. :value="item.id"
  19. />
  20. </el-select>
  21. </el-form-item>
  22. <el-form-item>
  23. <el-button @click="handleQuery"><Icon icon="ep:search" />搜索</el-button>
  24. <el-button @click="resetQuery"><Icon icon="ep:refresh" />重置</el-button>
  25. </el-form-item>
  26. </el-form>
  27. </ContentWrap>
  28. <ContentWrap>
  29. <el-tabs v-model="type" @tab-change="handleTabChange">
  30. <!-- tab 1:图片 -->
  31. <el-tab-pane name="image">
  32. <template #label>
  33. <span><Icon icon="ep:picture" />图片</span>
  34. </template>
  35. <div class="add_but" v-hasPermi="['mp:material:upload-permanent']">
  36. <el-upload
  37. :action="actionUrl"
  38. :headers="headers"
  39. multiple
  40. :limit="1"
  41. :file-list="fileList"
  42. :data="uploadData"
  43. :before-upload="beforeImageUpload"
  44. :on-success="handleUploadSuccess"
  45. >
  46. <el-button type="primary" plain>点击上传</el-button>
  47. <template #tip>
  48. <span class="el-upload__tip" style="margin-left: 5px">
  49. 支持 bmp/png/jpeg/jpg/gif 格式,大小不超过 2M
  50. </span>
  51. </template>
  52. </el-upload>
  53. </div>
  54. <div class="waterfall" v-loading="loading">
  55. <div class="waterfall-item" v-for="item in list" :key="item.id">
  56. <a target="_blank" :href="item.url">
  57. <img class="material-img" :src="item.url" />
  58. <div class="item-name">{{ item.name }}</div>
  59. </a>
  60. <el-row class="ope-row" justify="center">
  61. <el-button
  62. type="danger"
  63. circle
  64. @click="handleDelete(item)"
  65. v-hasPermi="['mp:material:delete']"
  66. >
  67. <Icon icon="ep:delete" />
  68. </el-button>
  69. </el-row>
  70. </div>
  71. </div>
  72. <!-- 分页组件 -->
  73. <Pagination
  74. :total="total"
  75. v-model:page="queryParams.pageNo"
  76. v-model:limit="queryParams.pageSize"
  77. @pagination="getList"
  78. />
  79. </el-tab-pane>
  80. <!-- tab 2:语音 -->
  81. <el-tab-pane name="voice">
  82. <template #label>
  83. <span><Icon icon="ep:microphone" />语音</span>
  84. </template>
  85. <div class="add_but" v-hasPermi="['mp:material:upload-permanent']">
  86. <el-upload
  87. :action="actionUrl"
  88. :headers="headers"
  89. multiple
  90. :limit="1"
  91. :file-list="fileList"
  92. :data="uploadData"
  93. :on-success="handleUploadSuccess"
  94. :before-upload="beforeVoiceUpload"
  95. >
  96. <el-button type="primary" plain>点击上传</el-button>
  97. <template #tip>
  98. <span class="el-upload__tip" style="margin-left: 5px">
  99. 格式支持 mp3/wma/wav/amr,文件大小不超过 2M,播放长度不超过 60s
  100. </span>
  101. </template>
  102. </el-upload>
  103. </div>
  104. <el-table :data="list" stripe border v-loading="loading" style="margin-top: 10px">
  105. <el-table-column label="编号" align="center" prop="mediaId" />
  106. <el-table-column label="文件名" align="center" prop="name" />
  107. <el-table-column label="语音" align="center">
  108. <template #default="scope">
  109. <WxVoicePlayer :url="scope.row.url" />
  110. </template>
  111. </el-table-column>
  112. <el-table-column label="上传时间" align="center" prop="createTime" width="180">
  113. <template #default="scope">
  114. <span>{{ formatDate(scope.row.createTime) }}</span>
  115. </template>
  116. </el-table-column>
  117. <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
  118. <template #default="scope">
  119. <el-button type="primary" link plain @click="handleDownload(scope.row)">
  120. <Icon icon="ep:download" />下载
  121. </el-button>
  122. <el-button
  123. type="primary"
  124. link
  125. plain
  126. @click="handleDelete(scope.row)"
  127. v-hasPermi="['mp:material:delete']"
  128. >
  129. <Icon icon="ep:delete" />删除
  130. </el-button>
  131. </template>
  132. </el-table-column>
  133. </el-table>
  134. <!-- 分页组件 -->
  135. <Pagination
  136. :total="total"
  137. v-model:page="queryParams.pageNo"
  138. v-model:limit="queryParams.pageSize"
  139. @pagination="getList"
  140. />
  141. </el-tab-pane>
  142. <!-- tab 3:视频 -->
  143. <el-tab-pane name="video">
  144. <template #label>
  145. <span><Icon icon="ep:video-play" /> 视频</span>
  146. </template>
  147. <div class="add_but" v-hasPermi="['mp:material:upload-permanent']">
  148. <el-button type="primary" plain @click="handleAddVideo">新建视频</el-button>
  149. </div>
  150. <!-- 新建视频的弹窗 -->
  151. <el-dialog
  152. title="新建视频"
  153. v-model="dialogVideoVisible"
  154. width="600px"
  155. v-loading="addMaterialLoading"
  156. >
  157. <el-upload
  158. :action="actionUrl"
  159. :headers="headers"
  160. multiple
  161. :limit="1"
  162. :file-list="fileList"
  163. :data="uploadData"
  164. :before-upload="beforeVideoUpload"
  165. :on-success="handleUploadSuccess"
  166. ref="uploadVideoRef"
  167. :auto-upload="false"
  168. >
  169. <template #trigger>
  170. <el-button size="small" type="primary">选择视频</el-button>
  171. </template>
  172. <span class="el-upload__tip" style="margin-left: 10px"
  173. >格式支持 MP4,文件大小不超过 10MB</span
  174. >
  175. </el-upload>
  176. <el-form :model="uploadData" :rules="uploadRules" ref="uploadFormRef" label-width="80px">
  177. <el-row>
  178. <el-form-item label="标题" prop="title">
  179. <el-input
  180. v-model="uploadData.title"
  181. placeholder="标题将展示在相关播放页面,建议填写清晰、准确、生动的标题"
  182. />
  183. </el-form-item>
  184. </el-row>
  185. <el-row>
  186. <el-form-item label="描述" prop="introduction">
  187. <el-input
  188. :rows="3"
  189. type="textarea"
  190. v-model="uploadData.introduction"
  191. placeholder="介绍语将展示在相关播放页面,建议填写简洁明确、有信息量的内容"
  192. />
  193. </el-form-item>
  194. </el-row>
  195. </el-form>
  196. <template #footer>
  197. <el-button @click="cancelVideo">取 消</el-button>
  198. <el-button type="primary" @click="submitVideo">提 交</el-button>
  199. </template>
  200. </el-dialog>
  201. <el-table :data="list" stripe border v-loading="loading" style="margin-top: 10px">
  202. <el-table-column label="编号" align="center" prop="mediaId" />
  203. <el-table-column label="文件名" align="center" prop="name" />
  204. <el-table-column label="标题" align="center" prop="title" />
  205. <el-table-column label="介绍" align="center" prop="introduction" />
  206. <el-table-column label="视频" align="center">
  207. <template #default="scope">
  208. <WxVideoPlayer :url="scope.row.url" />
  209. </template>
  210. </el-table-column>
  211. <el-table-column label="上传时间" align="center" prop="createTime" width="180">
  212. <template #default="scope">
  213. <span>{{ formatDate(scope.row.createTime) }}</span>
  214. </template>
  215. </el-table-column>
  216. <el-table-column label="操作" align="center" fixed="right">
  217. <template #default="scope">
  218. <el-button type="primary" link plain @click="handleDownload(scope.row)"
  219. ><Icon icon="ep:download" />下载</el-button
  220. >
  221. <el-button
  222. type="primary"
  223. link
  224. size="small"
  225. plain
  226. @click="handleDelete(scope.row)"
  227. v-hasPermi="['mp:material:delete']"
  228. >
  229. <Icon icon="ep:delete" />删除
  230. </el-button>
  231. </template>
  232. </el-table-column>
  233. </el-table>
  234. <!-- 分页组件 -->
  235. <Pagination
  236. :total="total"
  237. v-model:page="queryParams.pageNo"
  238. v-model:limit="queryParams.pageSize"
  239. @pagination="getList"
  240. />
  241. </el-tab-pane>
  242. </el-tabs>
  243. </ContentWrap>
  244. </template>
  245. <script setup name="MpMaterial">
  246. import WxVoicePlayer from '@/views/mp/components/wx-voice-play/main.vue'
  247. import WxVideoPlayer from '@/views/mp/components/wx-video-play/main.vue'
  248. import { getSimpleAccountList } from '@/api/mp/account'
  249. import { getMaterialPage, deletePermanentMaterial } from '@/api/mp/material'
  250. import { getAccessToken } from '@/utils/auth'
  251. import { formatDate } from '@/utils/formatTime'
  252. const BASE_URL = import.meta.env.VITE_BASE_URL
  253. const message = useMessage()
  254. const queryFormRef = ref()
  255. const uploadFormRef = ref()
  256. const uploadVideoRef = ref()
  257. const type = ref('image')
  258. // 遮罩层
  259. const loading = ref(false)
  260. // 总条数
  261. const total = ref(0)
  262. // 数据列表
  263. const list = ref([])
  264. // 查询参数
  265. const queryParams = reactive({
  266. pageNo: 1,
  267. pageSize: 10,
  268. accountId: undefined,
  269. permanent: true
  270. })
  271. const actionUrl = BASE_URL + '/admin-api/mp/material/upload-permanent'
  272. const headers = { Authorization: 'Bearer ' + getAccessToken() }
  273. const fileList = ref([])
  274. const uploadData = reactive({
  275. type: 'image',
  276. title: '',
  277. introduction: ''
  278. })
  279. // === 视频上传,独有变量 ===
  280. const dialogVideoVisible = ref(false)
  281. const addMaterialLoading = ref(false)
  282. const uploadRules = reactive({
  283. // 视频上传的校验规则
  284. title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
  285. introduction: [{ required: true, message: '请输入描述', trigger: 'blur' }]
  286. })
  287. // 公众号账号列表
  288. const accountList = ref([])
  289. onMounted(() => {
  290. getSimpleAccountList().then((data) => {
  291. accountList.value = data
  292. // 默认选中第一个
  293. if (accountList.value.length > 0) {
  294. setAccountId(accountList.value[0].id)
  295. }
  296. // 加载数据
  297. getList()
  298. })
  299. })
  300. // ======================== 列表查询 ========================
  301. /** 设置账号编号 */
  302. const setAccountId = (accountId) => {
  303. queryParams.accountId = accountId
  304. uploadData.accountId = accountId
  305. }
  306. /** 查询列表 */
  307. const getList = () => {
  308. // 如果没有选中公众号账号,则进行提示。
  309. if (!queryParams.accountId) {
  310. message.error('未选中公众号,无法查询草稿箱')
  311. return false
  312. }
  313. loading.value = true
  314. getMaterialPage({
  315. ...queryParams,
  316. type: type.value
  317. })
  318. .then((data) => {
  319. list.value = data.list
  320. total.value = data.total
  321. })
  322. .finally(() => {
  323. loading.value = false
  324. })
  325. }
  326. /** 搜索按钮操作 */
  327. const handleQuery = () => {
  328. queryParams.pageNo = 1
  329. // 默认选中第一个
  330. if (queryParams.accountId) {
  331. setAccountId(queryParams.accountId)
  332. }
  333. getList()
  334. }
  335. /** 重置按钮操作 */
  336. const resetQuery = () => {
  337. queryFormRef.value?.resetFields()
  338. // 默认选中第一个
  339. if (accountList.value.length > 0) {
  340. setAccountId(accountList.value[0].id)
  341. }
  342. handleQuery()
  343. }
  344. const handleTabChange = (tabName) => {
  345. // 设置 type
  346. uploadData.type = tabName
  347. // 从第一页开始查询
  348. handleQuery()
  349. }
  350. // ======================== 文件上传 ========================
  351. const beforeImageUpload = (file) => {
  352. const isType =
  353. file.type === 'image/jpeg' ||
  354. file.type === 'image/png' ||
  355. file.type === 'image/gif' ||
  356. file.type === 'image/bmp' ||
  357. file.type === 'image/jpg'
  358. if (!isType) {
  359. message.error('上传图片格式不对!')
  360. return false
  361. }
  362. const isLt = file.size / 1024 / 1024 < 2
  363. if (!isLt) {
  364. message.error('上传图片大小不能超过 2M!')
  365. return false
  366. }
  367. loading.value = true
  368. return true
  369. }
  370. const beforeVoiceUpload = (file) => {
  371. const isType =
  372. file.type === 'audio/mp3' ||
  373. file.type === 'audio/wma' ||
  374. file.type === 'audio/wav' ||
  375. file.type === 'audio/amr'
  376. const isLt = file.size / 1024 / 1024 < 2
  377. if (!isType) {
  378. message.error('上传语音格式不对!')
  379. return false
  380. }
  381. if (!isLt) {
  382. message.error('上传语音大小不能超过 2M!')
  383. return false
  384. }
  385. loading.value = true
  386. return true
  387. }
  388. const beforeVideoUpload = (file) => {
  389. const isType = file.type === 'video/mp4'
  390. if (!isType) {
  391. message.error('上传视频格式不对!')
  392. return false
  393. }
  394. const isLt = file.size / 1024 / 1024 < 10
  395. if (!isLt) {
  396. message.error('上传视频大小不能超过 10M!')
  397. return false
  398. }
  399. addMaterialLoading.value = true
  400. return true
  401. }
  402. const handleUploadSuccess = (response, file, fileList) => {
  403. loading.value = false
  404. addMaterialLoading.value = false
  405. if (response.code !== 0) {
  406. message.error('上传出错:' + response.msg)
  407. return false
  408. }
  409. // 清空上传时的各种数据
  410. dialogVideoVisible.value = false
  411. fileList.value = []
  412. uploadData.title = ''
  413. uploadData.introduction = ''
  414. // 加载数据
  415. getList()
  416. }
  417. // 下载文件
  418. const handleDownload = (row) => {
  419. window.open(row.url, '_blank')
  420. }
  421. // 提交 video 新建的表单
  422. const submitVideo = () => {
  423. uploadFormRef.value.validate((valid) => {
  424. if (!valid) {
  425. return false
  426. }
  427. uploadVideoRef.value.submit()
  428. })
  429. }
  430. const handleAddVideo = () => {
  431. resetVideo()
  432. dialogVideoVisible.value = true
  433. }
  434. /** 取消按钮 */
  435. const cancelVideo = () => {
  436. dialogVideoVisible.value = false
  437. resetVideo()
  438. }
  439. /** 表单重置 */
  440. const resetVideo = () => {
  441. fileList.value = []
  442. uploadData.title = ''
  443. uploadData.introduction = ''
  444. uploadFormRef.value?.resetFields()
  445. }
  446. // ======================== 其它操作 ========================
  447. const handleDelete = async (item) => {
  448. await message.confirm('此操作将永久删除该文件, 是否继续?')
  449. await deletePermanentMaterial(item.id)
  450. message.alertSuccess('删除成功')
  451. }
  452. </script>
  453. <style lang="scss" scoped>
  454. /*瀑布流样式*/
  455. .waterfall {
  456. width: 100%;
  457. column-gap: 10px;
  458. column-count: 5;
  459. margin-top: 10px; /* 芋道源码:增加 10px,避免顶着上面 */
  460. }
  461. .waterfall-item {
  462. padding: 10px;
  463. margin-bottom: 10px;
  464. break-inside: avoid;
  465. border: 1px solid #eaeaea;
  466. }
  467. .material-img {
  468. width: 100%;
  469. }
  470. p {
  471. line-height: 30px;
  472. }
  473. @media (min-width: 992px) and (max-width: 1300px) {
  474. .waterfall {
  475. column-count: 3;
  476. }
  477. p {
  478. color: red;
  479. }
  480. }
  481. @media (min-width: 768px) and (max-width: 991px) {
  482. .waterfall {
  483. column-count: 2;
  484. }
  485. p {
  486. color: orange;
  487. }
  488. }
  489. @media (max-width: 767px) {
  490. .waterfall {
  491. column-count: 1;
  492. }
  493. }
  494. /*瀑布流样式*/
  495. </style>