index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <!--
  2. MIT License
  3. Copyright (c) 2020 www.joolun.com
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. 芋道源码:
  20. ① 清理冗余 css 内容,清理冗余 data 变量
  21. ② 美化样式,支持播放,提升使用体验
  22. ③ 优化代码,特别是方法名和变量,提升可读性
  23. -->
  24. <template>
  25. <div class="app-container">
  26. <doc-alert title="公众号素材" url="https://doc.iocoder.cn/mp/material/" />
  27. <!-- 搜索工作栏 -->
  28. <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
  29. <el-form-item label="公众号" prop="accountId">
  30. <el-select v-model="queryParams.accountId" placeholder="请选择公众号">
  31. <el-option v-for="item in accounts" :key="parseInt(item.id)" :label="item.name" :value="parseInt(item.id)" />
  32. </el-select>
  33. </el-form-item>
  34. <el-form-item>
  35. <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
  36. <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
  37. </el-form-item>
  38. </el-form>
  39. <el-tabs v-model="type" @tab-click="handleClick">
  40. <!-- tab 1:图片 -->
  41. <el-tab-pane name="image">
  42. <span slot="label"><i class="el-icon-picture"></i> 图片</span>
  43. <div class="add_but" v-hasPermi="['mp:material:upload-permanent']">
  44. <el-upload :action="actionUrl" :headers="headers" multiple :limit="1" :file-list="fileList" :data="uploadData"
  45. :before-upload="beforeImageUpload" :on-success="handleUploadSuccess">
  46. <el-button size="mini" type="primary">点击上传</el-button>
  47. <sapn slot="tip" class="el-upload__tip" style="margin-left: 5px">支持 bmp/png/jpeg/jpg/gif 格式,大小不超过 2M</sapn>
  48. </el-upload>
  49. </div>
  50. <div class="waterfall" v-loading="loading">
  51. <div class="waterfall-item" v-for="item in list" :key='item.id'>
  52. <a target="_blank" :href="item.url">
  53. <img class="material-img" :src="item.url">
  54. <div class="item-name">{{item.name}}</div>
  55. </a>
  56. <el-row class="ope-row">
  57. <el-button type="danger" icon="el-icon-delete" circle @click="handleDelete(item)"
  58. v-hasPermi="['mp:material:delete']"/>
  59. </el-row>
  60. </div>
  61. </div>
  62. <!-- 分页组件 -->
  63. <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
  64. @pagination="getList"/>
  65. </el-tab-pane>
  66. <!-- tab 2:语音 -->
  67. <el-tab-pane name="voice">
  68. <span slot="label"><i class="el-icon-microphone"></i> 语音</span>
  69. <div class="add_but" v-hasPermi="['mp:material:upload-permanent']">
  70. <el-upload :action="actionUrl" :headers="headers" multiple :limit="1" :file-list="fileList" :data="uploadData"
  71. :on-success="handleUploadSuccess" :before-upload="beforeVoiceUpload">
  72. <el-button size="mini" type="primary">点击上传</el-button>
  73. <span slot="tip" class="el-upload__tip" style="margin-left: 5px">格式支持 mp3/wma/wav/amr,文件大小不超过 2M,播放长度不超过 60s</span>
  74. </el-upload>
  75. </div>
  76. <el-table :data="list" stripe border v-loading="loading" style="margin-top: 10px;">
  77. <el-table-column label="编号" align="center" prop="mediaId" />
  78. <el-table-column label="文件名" align="center" prop="name" />
  79. <el-table-column label="语音" align="center">
  80. <template v-slot="scope">
  81. <wx-voice-player :url="scope.row.url" />
  82. </template>
  83. </el-table-column>
  84. <el-table-column label="上传时间" align="center" prop="createTime" width="180">
  85. <template v-slot="scope">
  86. <span>{{ parseTime(scope.row.createTime) }}</span>
  87. </template>
  88. </el-table-column>
  89. <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
  90. <template v-slot="scope">
  91. <el-button type="text" icon="el-icon-download" size="small" plain @click="handleDownload(scope.row)">下载</el-button>
  92. <el-button type="text" icon="el-icon-delete" size="small" plain @click="handleDelete(scope.row)"
  93. v-hasPermi="['mp:material:delete']">删除</el-button>
  94. </template>
  95. </el-table-column>
  96. </el-table>
  97. <!-- 分页组件 -->
  98. <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
  99. @pagination="getList"/>
  100. </el-tab-pane>
  101. <!-- tab 3:视频 -->
  102. <el-tab-pane name="video">
  103. <span slot="label"><i class="el-icon-video-play"></i> 视频</span>
  104. <div class="add_but" v-hasPermi="['mp:material:upload-permanent']">
  105. <el-button size="mini" type="primary" @click="handleAddVideo">新建视频</el-button>
  106. </div>
  107. <!-- 新建视频的弹窗 -->
  108. <el-dialog title="新建视频" :visible.sync="dialogVideoVisible" append-to-body width="600px"
  109. v-loading="addMaterialLoading">
  110. <el-upload :action="actionUrl" :headers="headers" multiple :limit="1" :file-list="fileList" :data="uploadData"
  111. :before-upload="beforeVideoUpload" :on-success="handleUploadSuccess"
  112. ref="uploadVideo" :auto-upload="false">
  113. <el-button slot="trigger" size="mini" type="primary">选择视频</el-button>
  114. <span class="el-upload__tip" style="margin-left: 10px;">格式支持 MP4,文件大小不超过 10MB</span>
  115. </el-upload>
  116. <el-form :model="uploadData" :rules="uploadRules" ref="uploadForm" label-width="80px">
  117. <el-row>
  118. <el-form-item label="标题" prop="title">
  119. <el-input v-model="uploadData.title" placeholder="标题将展示在相关播放页面,建议填写清晰、准确、生动的标题" />
  120. </el-form-item>
  121. </el-row>
  122. <el-row>
  123. <el-form-item label="描述" prop="introduction">
  124. <el-input :rows="3" type="textarea" v-model="uploadData.introduction"
  125. placeholder="介绍语将展示在相关播放页面,建议填写简洁明确、有信息量的内容" />
  126. </el-form-item>
  127. </el-row>
  128. </el-form>
  129. <div slot="footer" class="dialog-footer">
  130. <el-button @click="cancelVideo">取 消</el-button>
  131. <el-button type="primary" @click="submitVideo">提 交</el-button>
  132. </div>
  133. </el-dialog>
  134. <el-table :data="list" stripe border v-loading="loading" style="margin-top: 10px;">
  135. <el-table-column label="编号" align="center" prop="mediaId" />
  136. <el-table-column label="文件名" align="center" prop="name" />
  137. <el-table-column label="标题" align="center" prop="title" />
  138. <el-table-column label="介绍" align="center" prop="introduction" />
  139. <el-table-column label="视频" align="center">
  140. <template v-slot="scope">
  141. <wx-video-player :url="scope.row.url" />
  142. </template>
  143. </el-table-column>
  144. <el-table-column label="上传时间" align="center" prop="createTime" width="180">
  145. <template v-slot="scope">
  146. <span>{{ parseTime(scope.row.createTime) }}</span>
  147. </template>
  148. </el-table-column>
  149. <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
  150. <template v-slot="scope">
  151. <el-button type="text" icon="el-icon-download" size="small" plain @click="handleDownload(scope.row)">下载</el-button>
  152. <el-button type="text" icon="el-icon-delete" size="small" plain @click="handleDelete(scope.row)"
  153. v-hasPermi="['mp:material:delete']">删除</el-button>
  154. </template>
  155. </el-table-column>
  156. </el-table>
  157. <!-- 分页组件 -->
  158. <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
  159. @pagination="getList"/>
  160. </el-tab-pane>
  161. </el-tabs>
  162. </div>
  163. </template>
  164. <script>
  165. import WxVoicePlayer from '@/views/mp/components/wx-voice-play/main.vue';
  166. import WxVideoPlayer from '@/views/mp/components/wx-video-play/main.vue';
  167. import { getSimpleAccounts } from "@/api/mp/account";
  168. import { getMaterialPage, deletePermanentMaterial } from "@/api/mp/material";
  169. import { getAccessToken } from '@/utils/auth'
  170. export default {
  171. name: 'mpMaterial',
  172. components: {
  173. WxVoicePlayer,
  174. WxVideoPlayer
  175. },
  176. data() {
  177. return {
  178. type: 'image',
  179. // 遮罩层
  180. loading: false,
  181. // 显示搜索条件
  182. showSearch: true,
  183. // 总条数
  184. total: 0,
  185. // 数据列表
  186. list: [],
  187. // 查询参数
  188. queryParams: {
  189. pageNo: 1,
  190. pageSize: 10,
  191. accountId: undefined,
  192. permanent: true,
  193. },
  194. actionUrl: process.env.VUE_APP_BASE_API + '/admin-api/mp/material/upload-permanent',
  195. headers: { Authorization: "Bearer " + getAccessToken() }, // 设置上传的请求头部
  196. fileList:[],
  197. uploadData: {
  198. "type": 'image',
  199. "title":'',
  200. "introduction":''
  201. },
  202. // === 视频上传,独有变量 ===
  203. dialogVideoVisible: false,
  204. addMaterialLoading: false,
  205. uploadRules: { // 视频上传的校验规则
  206. title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
  207. introduction: [{ required: true, message: '请输入描述', trigger: 'blur' }],
  208. },
  209. // 公众号账号列表
  210. accounts: [],
  211. }
  212. },
  213. created() {
  214. getSimpleAccounts().then(response => {
  215. this.accounts = response.data;
  216. // 默认选中第一个
  217. if (this.accounts.length > 0) {
  218. this.setAccountId(this.accounts[0].id);
  219. }
  220. // 加载数据
  221. this.getList();
  222. })
  223. },
  224. methods: {
  225. // ======================== 列表查询 ========================
  226. /** 设置账号编号 */
  227. setAccountId(accountId) {
  228. this.queryParams.accountId = accountId;
  229. this.uploadData.accountId = accountId;
  230. },
  231. /** 查询列表 */
  232. getList() {
  233. // 如果没有选中公众号账号,则进行提示。
  234. if (!this.queryParams.accountId) {
  235. this.$message.error('未选中公众号,无法查询草稿箱')
  236. return false
  237. }
  238. this.loading = true
  239. getMaterialPage({
  240. ...this.queryParams,
  241. type: this.type
  242. }).then(response => {
  243. this.list = response.data.list
  244. this.total = response.data.total
  245. }).finally(() => {
  246. this.loading = false
  247. })
  248. },
  249. /** 搜索按钮操作 */
  250. handleQuery() {
  251. this.queryParams.pageNo = 1
  252. // 默认选中第一个
  253. if (this.queryParams.accountId) {
  254. this.setAccountId(this.queryParams.accountId)
  255. }
  256. this.getList()
  257. },
  258. /** 重置按钮操作 */
  259. resetQuery() {
  260. this.resetForm('queryForm')
  261. // 默认选中第一个
  262. if (this.accounts.length > 0) {
  263. this.setAccountId(this.accounts[0].id)
  264. }
  265. this.handleQuery()
  266. },
  267. handleClick(tab, event) {
  268. // 设置 type
  269. this.uploadData.type = tab.name
  270. // 从第一页开始查询
  271. this.handleQuery();
  272. },
  273. // ======================== 文件上传 ========================
  274. beforeImageUpload(file) {
  275. const isType = file.type === 'image/jpeg'
  276. || file.type === 'image/png'
  277. || file.type === 'image/gif'
  278. || file.type === 'image/bmp'
  279. || file.type === 'image/jpg';
  280. if (!isType) {
  281. this.$message.error('上传图片格式不对!')
  282. return false;
  283. }
  284. const isLt = file.size / 1024 / 1024 < 2
  285. if (!isLt) {
  286. this.$message.error('上传图片大小不能超过 2M!')
  287. return false;
  288. }
  289. this.loading = true
  290. return true;
  291. },
  292. beforeVoiceUpload(file){
  293. const isType = file.type === 'audio/mp3'
  294. || file.type === 'audio/wma'
  295. || file.type === 'audio/wav'
  296. || file.type === 'audio/amr';
  297. const isLt = file.size / 1024 / 1024 < 2
  298. if (!isType) {
  299. this.$message.error('上传语音格式不对!')
  300. return false;
  301. }
  302. if (!isLt) {
  303. this.$message.error('上传语音大小不能超过 2M!')
  304. return false;
  305. }
  306. this.loading = true
  307. return true;
  308. },
  309. beforeVideoUpload(file){
  310. const isType = file.type === 'video/mp4'
  311. if (!isType) {
  312. this.$message.error('上传视频格式不对!')
  313. return false;
  314. }
  315. const isLt = file.size / 1024 / 1024 < 10
  316. if (!isLt) {
  317. this.$message.error('上传视频大小不能超过 10M!')
  318. return false
  319. }
  320. this.addMaterialLoading = true
  321. return true
  322. },
  323. handleUploadSuccess(response, file, fileList) {
  324. this.loading = false
  325. this.addMaterialLoading = false
  326. if (response.code !== 0) {
  327. this.$message.error('上传出错:' + response.msg)
  328. return false;
  329. }
  330. // 清空上传时的各种数据
  331. this.dialogVideoVisible = false
  332. this.fileList = []
  333. this.uploadData.title = ''
  334. this.uploadData.introduction = ''
  335. // 加载数据
  336. this.getList()
  337. },
  338. // 下载文件
  339. handleDownload(row) {
  340. window.open(row.url,'_blank')
  341. },
  342. // 提交 video 新建的表单
  343. submitVideo() {
  344. this.$refs['uploadForm'].validate((valid) => {
  345. if (!valid) {
  346. return false;
  347. }
  348. this.$refs.uploadVideo.submit()
  349. })
  350. },
  351. handleAddVideo() {
  352. this.resetVideo();
  353. this.dialogVideoVisible = true
  354. },
  355. /** 取消按钮 */
  356. cancelVideo() {
  357. this.dialogVideoVisible = false;
  358. this.resetVideo();
  359. },
  360. /** 表单重置 */
  361. resetVideo() {
  362. this.fileList = []
  363. this.uploadData.title = ''
  364. this.uploadData.introduction = ''
  365. this.resetForm("uploadForm");
  366. },
  367. // ======================== 其它操作 ========================
  368. handleDelete(item) {
  369. const id = item.id
  370. this.$modal.confirm('此操作将永久删除该文件, 是否继续?').then(function() {
  371. return deletePermanentMaterial(id);
  372. }).then(() => {
  373. this.getList();
  374. this.$modal.msgSuccess("删除成功");
  375. }).catch(() => {});
  376. },
  377. }
  378. }
  379. </script>
  380. <style lang="scss" scoped>
  381. /*瀑布流样式*/
  382. .waterfall {
  383. width: 100%;
  384. column-gap:10px;
  385. column-count: 5;
  386. margin-top: 10px; /* 芋道源码:增加 10px,避免顶着上面 */
  387. }
  388. .waterfall-item {
  389. padding: 10px;
  390. margin-bottom: 10px;
  391. break-inside: avoid;
  392. border: 1px solid #eaeaea;
  393. }
  394. .material-img {
  395. width: 100%;
  396. }
  397. p {
  398. line-height: 30px;
  399. }
  400. @media (min-width: 992px) and (max-width: 1300px) {
  401. .waterfall {
  402. column-count: 3;
  403. }
  404. p {
  405. color:red;
  406. }
  407. }
  408. @media (min-width: 768px) and (max-width: 991px) {
  409. .waterfall {
  410. column-count: 2;
  411. }
  412. p {
  413. color: orange;
  414. }
  415. }
  416. @media (max-width: 767px) {
  417. .waterfall {
  418. column-count: 1;
  419. }
  420. }
  421. /*瀑布流样式*/
  422. </style>