index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <template>
  2. <div class="panorama con">
  3. <div class="top">
  4. <crumbs :list="tablist" />
  5. </div>
  6. <div class="second-line">
  7. <div class="btn">
  8. <button
  9. @mouseover.stop="showList = true"
  10. @click="$refs.uploadFile.click()"
  11. class="ui-button submit"
  12. >
  13. <span>上传素材</span>
  14. <i class="iconfont icon-material_prompt hover-tips hover-tips-upload-icon">
  15. <div>
  16. <div class="remark">请上传20MB以内、mp3格式的音频</div>
  17. </div>
  18. </i>
  19. <upload
  20. ref="uploadFile"
  21. :failString="'格式错误,请上传20MB以内、mp3格式的音频'"
  22. :limitFailStr="'过大,请上传20MB以内、mp3格式的音频'"
  23. accept-type="audio/mp3"
  24. media-type="audio"
  25. :limit="20"
  26. @file-change="onFileChange"
  27. ></upload>
  28. </button>
  29. </div>
  30. <div class="filter">
  31. <div :class="{active: isFilterFocus}" @focusin="onFilterFocus" @focusout="onFilterBlur">
  32. <i class="iconfont icon-works_search search" ></i>
  33. <input
  34. type="text"
  35. v-model="searchKey"
  36. placeholder="搜索素材"
  37. />
  38. <i v-if="searchKey" @click="searchKey=''" class="iconfont icontoast_red del"></i>
  39. </div>
  40. </div>
  41. </div>
  42. <div class="list">
  43. <tableList
  44. @selection-change="
  45. (data) => {
  46. selectedArr = data;
  47. }
  48. "
  49. @request-more-data="getMoreMaterialItem"
  50. :canRequestMoreData="hasMoreData && !isRequestingMoreData"
  51. :header="tabHeader"
  52. :showLine="true"
  53. :selection="false"
  54. :data="list"
  55. class="table-list"
  56. ref="table-list"
  57. >
  58. <div slot-scope="{ data }" slot="header">
  59. {{ data.name }}
  60. </div>
  61. <div slot-scope="{ data, item, sub }" slot="item" style="width: 100%">
  62. <div class="handle" v-if="sub.canclick">
  63. <i
  64. class="iconfont icon-material_operation_editor hover-tips"
  65. @click="(showRename = true), (popupItem = item)"
  66. >
  67. <div>
  68. <div class="remark">重命名</div>
  69. </div>
  70. </i>
  71. <i class="iconfont icon-material_operation_delete hover-tips-warn" @click="del(item)">
  72. <div>
  73. <div class="remark">删除</div>
  74. </div>
  75. </i>
  76. </div>
  77. <div class="audio" v-else-if="sub.type == 'audio'">
  78. <v-audio
  79. :vkey="item.id"
  80. :idleft="`_${$randomWord(true, 8, 8)}`"
  81. :idright="`_${$randomWord(true, 8, 8)}`"
  82. :myAudioUrl="data"
  83. ></v-audio>
  84. </div>
  85. <span
  86. v-else
  87. :style="{ fontWeight: sub.fontweight, color: '#202020' }"
  88. >{{ data || "-" }}</span
  89. >
  90. </div>
  91. </tableList>
  92. <UploadTaskList class="upload-task-list" fileType="AUDIO" :taskList="uploadListForUI" @cancel-task="onCancelTask"></UploadTaskList>
  93. <div class="total-number" v-if="list.length !== 0 || hasMoreData">已加载{{list.length}}条</div>
  94. <div class="nodata" v-if="list.length == 0 && !hasMoreData && lastestUsedSearchKey">
  95. <img :src="$noresult" alt="" />
  96. <span>未搜索到结果~</span>
  97. </div>
  98. <div class="nodata" v-if="list.length == 0 && !hasMoreData && !lastestUsedSearchKey">
  99. <img :src="config.empty" alt="" />
  100. <span>暂无素材~</span>
  101. <button @click="$refs.uploadFile.click()" class="upload-btn-in-table">上传素材</button>
  102. </div>
  103. </div>
  104. <rename
  105. v-if="showRename"
  106. :item="popupItem"
  107. @rename="handleRename"
  108. @close="showRename = false"
  109. />
  110. </div>
  111. </template>
  112. <script>
  113. import config from "@/config";
  114. import tableList from "@/components/table";
  115. import crumbs from "@/components/crumbs";
  116. import { data } from "./audio";
  117. import rename from "../popup/rename";
  118. import vAudio from "@/components/audio/index.vue";
  119. import Upload from "@/components/shared/uploads/UploadMultiple";
  120. import { changeByteUnit } from "@/utils/file";
  121. import UploadTaskList from "../components/uploadList1.1.0.vue";
  122. import { debounce } from "@/utils/other.js"
  123. import { mapState } from 'vuex';
  124. import {
  125. getMaterialList,
  126. uploadMaterial,
  127. editMaterial,
  128. delMaterial,
  129. } from "@/api";
  130. const TYPE = "audio";
  131. export default {
  132. components: {
  133. tableList,
  134. crumbs,
  135. rename,
  136. Upload,
  137. vAudio,
  138. UploadTaskList,
  139. },
  140. data() {
  141. return {
  142. config,
  143. showRename: false,
  144. showList: false,
  145. popupItem: null,
  146. tabHeader: data,
  147. selectedArr: [],
  148. searchKey: "",
  149. // 因为searchKey的变化经过debounce、异步请求的延时,才会反映到数据列表的变化上,所以是否显示、显示哪种无数据提示,也要等到数据列表变化后,根据数据列表是否为空,以及引发本次变化的那个searchKey瞬时值来决定。本变量就是用来保存那个瞬时值。
  150. lastestUsedSearchKey: '',
  151. isFilterFocus: false,
  152. tablist: [
  153. {
  154. name: "音频",
  155. id: TYPE,
  156. },
  157. ],
  158. list: [],
  159. hasMoreData: true,
  160. isRequestingMoreData: false,
  161. };
  162. },
  163. computed: {
  164. ...mapState({
  165. uploadListForUI: 'uploadStatusListAudio',
  166. })
  167. },
  168. mounted() {
  169. },
  170. watch: {
  171. searchKey: {
  172. handler: function () {
  173. this.refreshListDebounced()
  174. },
  175. immediate: false,
  176. },
  177. },
  178. methods: {
  179. onFilterFocus() {
  180. this.isFilterFocus = true
  181. },
  182. onFilterBlur() {
  183. this.isFilterFocus = false
  184. },
  185. refreshListDebounced: debounce(function() {
  186. this.list = []
  187. this.isRequestingMoreData = false
  188. this.hasMoreData = true
  189. this.$refs['table-list'].requestMoreData()
  190. }, 700, false),
  191. stopAllAudio() {
  192. Array.from($("audio")).forEach((item) => {
  193. if (!item.paused) {
  194. item.pause();
  195. }
  196. });
  197. },
  198. handleRename(newName) {
  199. editMaterial(
  200. {
  201. id: this.popupItem.id,
  202. name: newName,
  203. },
  204. () => {
  205. this.$msg.success("修改成功");
  206. const index = this.list.findIndex((eachItem) => {
  207. return eachItem.id === this.popupItem.id
  208. })
  209. if (index >= 0) {
  210. this.list[index].name = newName
  211. } else {
  212. console.error('在素材列表里没找到要重命名的那一项!');
  213. }
  214. this.showRename = false;
  215. this.popupItem = null;
  216. }
  217. );
  218. },
  219. del(item) {
  220. this.$confirm({
  221. title: '删除素材',
  222. content: "确定要删除素材吗?",
  223. okText: '删除',
  224. ok: () => {
  225. delMaterial(item.id, () => {
  226. this.$msg.success("删除成功");
  227. this.isRequestingMoreData = true
  228. const lastestUsedSearchKey = this.searchKey
  229. getMaterialList(
  230. {
  231. pageNum: this.list.length + 1,
  232. pageSize: 1,
  233. searchKey: this.searchKey,
  234. type: TYPE,
  235. },
  236. (data) => {
  237. const index = this.list.findIndex((eachItem) => {
  238. return eachItem.id === item.id
  239. })
  240. if (index >= 0) {
  241. this.list.splice(index, 1)
  242. const newData = data.data.list.map((i) => {
  243. i.fileSize = changeByteUnit(Number(i.fileSize));
  244. i.createTime = i.createTime.substring(0, i.createTime.length - 3)
  245. i.updateTime = i.updateTime.substring(0, i.updateTime.length - 3)
  246. return i;
  247. });
  248. this.list = this.list.concat(newData)
  249. if (this.list.length === data.data.total) {
  250. this.hasMoreData = false
  251. }
  252. this.$bus.emit('deletedAudio') // 各个Audio组件需要重新初始化
  253. } else {
  254. console.error('在素材列表里没找到要删除的那一项!');
  255. }
  256. this.isRequestingMoreData = false
  257. this.lastestUsedSearchKey = lastestUsedSearchKey
  258. },
  259. () => {
  260. this.isRequestingMoreData = false
  261. this.lastestUsedSearchKey = lastestUsedSearchKey
  262. }
  263. )
  264. });
  265. },
  266. });
  267. },
  268. onFileChange(e) {
  269. e.files.forEach((eachFile, i) => {
  270. if (eachFile.name.toLowerCase().indexOf("mp3") <= -1) {
  271. setTimeout(() => {
  272. this.$msg({
  273. message: `“${eachFile.name}”格式错误,请上传20MB以内、mp3格式的音频`,
  274. type: "warning",
  275. });
  276. }, i * 100);
  277. return;
  278. }
  279. if (eachFile.name.substring(0, eachFile.name.lastIndexOf(".")).length > 50) {
  280. setTimeout(() => {
  281. this.$msg({
  282. message: `“${eachFile.name}”名称过长,请上传标题在50字以内的音频`,
  283. type: "warning",
  284. });
  285. }, i * 100);
  286. return;
  287. }
  288. let itemInUploadList = {
  289. title: eachFile.name,
  290. ifKnowProgress: true,
  291. progress: 0,
  292. status: 'LOADING',
  293. statusText: "正在上传素材",
  294. uid: `u_${this.$randomWord(true, 8, 8)}`,
  295. abortHandler: null,
  296. };
  297. itemInUploadList.abortHandler = uploadMaterial(
  298. {
  299. file: eachFile
  300. },
  301. {
  302. type: TYPE,
  303. uid: itemInUploadList.uid,
  304. },
  305. () => { // 上传成功
  306. const index = this.uploadListForUI.findIndex((eachItem) => {
  307. return eachItem.uid === itemInUploadList.uid
  308. })
  309. this.uploadListForUI.splice(index, 1)
  310. this.refreshListDebounced()
  311. },
  312. (err) => {
  313. if (err.statusText === 'abort') { // 用户取消了上传任务。
  314. const index = this.uploadListForUI.findIndex((eachItem) => {
  315. return eachItem.uid === itemInUploadList.uid
  316. })
  317. this.uploadListForUI.splice(index, 1)
  318. } else {
  319. itemInUploadList.status = 'FAIL'
  320. itemInUploadList.statusText = '素材上传失败'
  321. }
  322. },
  323. (progress) => {
  324. itemInUploadList.progress = progress
  325. }
  326. );
  327. this.uploadListForUI.push(itemInUploadList);
  328. });
  329. },
  330. onCancelTask(uid) {
  331. const index = this.uploadListForUI.findIndex((eachItem) => {
  332. return eachItem.uid === uid
  333. })
  334. this.uploadListForUI[index].abortHandler.abort()
  335. },
  336. getMoreMaterialItem() {
  337. this.isRequestingMoreData = true
  338. const lastestUsedSearchKey = this.searchKey
  339. getMaterialList(
  340. {
  341. pageNum: Math.floor(this.list.length / config.PAGE_SIZE) + 1,
  342. pageSize: config.PAGE_SIZE,
  343. searchKey: this.searchKey,
  344. type: TYPE,
  345. },
  346. (data) => {
  347. const newData = data.data.list.map((i) => {
  348. i.fileSize = changeByteUnit(Number(i.fileSize));
  349. i.createTime = i.createTime.substring(0, i.createTime.length - 3)
  350. i.updateTime = i.updateTime.substring(0, i.updateTime.length - 3)
  351. return i;
  352. });
  353. this.list = this.list.concat(newData)
  354. if (this.list.length === data.data.total) {
  355. this.hasMoreData = false
  356. }
  357. this.isRequestingMoreData = false
  358. this.lastestUsedSearchKey = lastestUsedSearchKey
  359. },
  360. () => {
  361. this.isRequestingMoreData = false
  362. this.lastestUsedSearchKey = lastestUsedSearchKey
  363. }
  364. );
  365. },
  366. },
  367. };
  368. </script>
  369. <style lang="less" scoped>
  370. </style>
  371. <style lang="less" scoped>
  372. @import "../style.less";
  373. </style>