index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 ui-button-rect submit"
  12. >
  13. <span>上传素材</span>
  14. <i class="iconfont icon-material_prompt hover-tips hover-tips-upload-icon">
  15. <div>
  16. <div class="remark">请上传10MB以内、jpg/png格式的图片</div>
  17. </div>
  18. </i>
  19. <upload
  20. ref="uploadFile"
  21. :failString="'格式错误,请上传10MB以内、jpg/png格式的图片'"
  22. :limitFailStr="'过大,请上传10MB以内、jpg/png格式的图片'"
  23. accept-type="image/png,image/jpeg"
  24. media-type="image"
  25. :limit="10"
  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 v-if="sub.canclick" class="handle">
  63. <i
  64. class="iconfont iconbs_list_edit hover-tips"
  65. @click="(showRename = true), (popupItem = item)"
  66. >
  67. <div>
  68. <div class="remark">重命名</div>
  69. </div>
  70. </i>
  71. <i class="iconfont iconlist_delete hover-tips-warn" @click="del(item)">
  72. <div>
  73. <div class="remark">删除</div>
  74. </div>
  75. </i>
  76. </div>
  77. <div v-else-if="sub.type == 'image'" class="img">
  78. <img
  79. :id="'img' + item.id"
  80. :src="data + `?${Math.random()}`"
  81. alt=""
  82. />
  83. </div>
  84. <span
  85. v-else
  86. :style="{ fontWeight: sub.fontweight, color: '#202020' }"
  87. >{{ data || "-" }}</span
  88. >
  89. </div>
  90. </tableList>
  91. <UploadTaskList class="upload-list-new" fileType="AUDIO" :taskList="uploadListForUI" @cancel-task="onCancelTask"></UploadTaskList>
  92. <div class="total-number" v-if="list.length !== 0 || hasMoreData">已加载{{list.length}}条</div>
  93. <div class="nodata" v-if="list.length == 0 && !hasMoreData && lastestUsedSearchKey">
  94. <img :src="$noresult" alt="" />
  95. <span>未搜索到结果~</span>
  96. </div>
  97. <div class="nodata" v-if="list.length == 0 && !hasMoreData && !lastestUsedSearchKey">
  98. <img :src="config.empty" alt="" />
  99. <span>暂无素材~</span>
  100. <button @click="$refs.uploadFile.click()" class="upload-btn-in-table">上传素材</button>
  101. </div>
  102. </div>
  103. <rename
  104. :item="popupItem"
  105. :show="showRename"
  106. @rename="handleRename"
  107. @close="showRename = false"
  108. />
  109. <preview/>
  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 "./image";
  117. import rename from "../popup/rename";
  118. import UploadTaskList from "../components/uploadList1.1.0.vue";
  119. import Upload from "@/components/shared/uploads/UploadMultiple";
  120. import { changeByteUnit } from "@/utils/file";
  121. import preview from "../popup/preview1.1.0";
  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 = "image";
  131. export default {
  132. components: {
  133. tableList,
  134. crumbs,
  135. rename,
  136. Upload,
  137. preview,
  138. UploadTaskList,
  139. },
  140. data() {
  141. return {
  142. config, // TODO: 没必要这么弄
  143. showPreview: false,
  144. showRename: false,
  145. showList: false,
  146. popupItem: null,
  147. tabHeader: data,
  148. selectedArr: [],
  149. // 因为searchKey的变化经过debounce、异步请求的延时,才会反映到数据列表的变化上,所以是否显示、显示哪种无数据提示,也要等到数据列表变化后,根据数据列表是否为空,以及引发本次变化的那个searchKey瞬时值来决定。本变量就是用来保存那个瞬时值。
  150. lastestUsedSearchKey: '',
  151. isFilterFocus: false,
  152. searchKey: "",
  153. tablist: [
  154. {
  155. name: "图片",
  156. id: TYPE,
  157. },
  158. ],
  159. list: [],
  160. hasMoreData: true,
  161. isRequestingMoreData: false,
  162. };
  163. },
  164. computed: {
  165. ...mapState({
  166. uploadListForUI: 'uploadStatusListImage',
  167. })
  168. },
  169. mounted() {
  170. },
  171. watch: {
  172. searchKey: {
  173. handler: function () {
  174. this.refreshListDebounced()
  175. },
  176. immediate: false,
  177. },
  178. },
  179. methods: {
  180. onFilterFocus() {
  181. this.isFilterFocus = true
  182. },
  183. onFilterBlur() {
  184. this.isFilterFocus = false
  185. },
  186. refreshListDebounced: debounce(function() {
  187. this.list = []
  188. this.isRequestingMoreData = false
  189. this.hasMoreData = true
  190. this.$refs['table-list'].requestMoreData()
  191. }, 700, false),
  192. viewImg(id) {
  193. const viewer = this.$el.querySelector("#" + id).$viewer;
  194. viewer.show();
  195. },
  196. handleRename(newName) {
  197. editMaterial(
  198. {
  199. id: this.popupItem.id,
  200. name: newName,
  201. },
  202. () => {
  203. this.$msg.success("修改成功");
  204. const index = this.list.findIndex((eachItem) => {
  205. return eachItem.id === this.popupItem.id
  206. })
  207. if (index >= 0) {
  208. this.list[index].name = newName
  209. } else {
  210. console.error('在素材列表里没找到要重命名的那一项!');
  211. }
  212. this.showRename = false;
  213. this.popupItem = null;
  214. }
  215. );
  216. },
  217. del(item) {
  218. this.$confirm({
  219. content: "确定要删除素材吗?",
  220. ok: () => {
  221. delMaterial(item.id, () => {
  222. this.$msg.success("删除成功");
  223. this.isRequestingMoreData = true
  224. const lastestUsedSearchKey = this.searchKey
  225. getMaterialList(
  226. {
  227. pageNum: this.list.length + 1,
  228. pageSize: 1,
  229. searchKey: this.searchKey,
  230. type: TYPE,
  231. },
  232. (data) => {
  233. const index = this.list.findIndex((eachItem) => {
  234. return eachItem.id === item.id
  235. })
  236. if (index >= 0) {
  237. this.list.splice(index, 1)
  238. } else {
  239. console.error('在素材列表里没找到要删除的那一项!');
  240. }
  241. const newData = data.data.list.map((i) => {
  242. i.fileSize = changeByteUnit(Number(i.fileSize));
  243. return i;
  244. });
  245. this.list = this.list.concat(newData)
  246. if (this.list.length === data.data.total) {
  247. this.hasMoreData = false
  248. }
  249. this.isRequestingMoreData = false
  250. this.lastestUsedSearchKey = lastestUsedSearchKey
  251. },
  252. () => {
  253. this.isRequestingMoreData = false
  254. this.lastestUsedSearchKey = lastestUsedSearchKey
  255. }
  256. )
  257. });
  258. },
  259. });
  260. },
  261. onFileChange(e) {
  262. e.files.forEach((eachFile, i) => {
  263. if (
  264. eachFile.name.toLowerCase().indexOf("jpeg") <= -1 &&
  265. eachFile.name.toLowerCase().indexOf("jpg") <= -1 &&
  266. eachFile.name.toLowerCase().indexOf("png") <= -1
  267. ) {
  268. setTimeout(() => {
  269. this.$msg({
  270. message: `“${eachFile.name}”格式错误,请上传10MB以内、jpg/png格式的图片`,
  271. type: "error",
  272. });
  273. }, i * 100);
  274. return;
  275. }
  276. if (eachFile.name.substring(0, eachFile.name.lastIndexOf(".")).length > 50) {
  277. setTimeout(() => {
  278. this.$msg({
  279. message: `“${eachFile.name}”名称过长,请上传标题在50字以内的图片`,
  280. type: "error",
  281. });
  282. }, i * 100);
  283. return;
  284. }
  285. let itemInUploadList = {
  286. title: eachFile.name,
  287. ifKnowProgress: true,
  288. progress: 0,
  289. status: 'LOADING',
  290. statusStr: "正在上传素材",
  291. uid: `u_${this.$randomWord(true, 8, 8)}`,
  292. abortHandler: null,
  293. };
  294. itemInUploadList.abortHandler = uploadMaterial(
  295. {
  296. file: eachFile
  297. },
  298. {
  299. type: TYPE,
  300. uid: itemInUploadList.uid,
  301. },
  302. () => { // 上传成功
  303. const index = this.uploadListForUI.findIndex((eachItem) => {
  304. eachItem.uid === itemInUploadList.uid
  305. })
  306. this.uploadListForUI.splice(index, 1)
  307. this.refreshListDebounced()
  308. },
  309. (err) => {
  310. if (err.statusText === 'abort') { // 用户取消了上传任务。
  311. const index = this.uploadListForUI.findIndex((eachItem) => {
  312. eachItem.uid === itemInUploadList.uid
  313. })
  314. this.uploadListForUI.splice(index, 1)
  315. } else {
  316. itemInUploadList.status = 'FAIL'
  317. itemInUploadList.statusText = '素材上传失败'
  318. }
  319. },
  320. (progress) => {
  321. itemInUploadList.progress = progress
  322. }
  323. );
  324. this.uploadListForUI.push(itemInUploadList);
  325. });
  326. },
  327. onCancelTask(uid) {
  328. const index = this.uploadListForUI.findIndex((eachItem) => {
  329. return eachItem.uid === uid
  330. })
  331. this.uploadListForUI[index].abortHandler.abort()
  332. },
  333. getMoreMaterialItem() {
  334. this.isRequestingMoreData = true
  335. const lastestUsedSearchKey = this.searchKey
  336. getMaterialList(
  337. {
  338. pageNum: Math.floor(this.list.length / config.PAGE_SIZE) + 1,
  339. pageSize: config.PAGE_SIZE,
  340. searchKey: this.searchKey,
  341. type: TYPE,
  342. },
  343. (data) => {
  344. const newData = data.data.list.map((i) => {
  345. i.fileSize = changeByteUnit(Number(i.fileSize));
  346. return i;
  347. });
  348. this.list = this.list.concat(newData)
  349. if (this.list.length === data.data.total) {
  350. this.hasMoreData = false
  351. }
  352. this.isRequestingMoreData = false
  353. this.lastestUsedSearchKey = lastestUsedSearchKey
  354. },
  355. () => {
  356. this.isRequestingMoreData = false
  357. this.lastestUsedSearchKey = lastestUsedSearchKey
  358. }
  359. );
  360. },
  361. },
  362. };
  363. </script>
  364. <style lang="less" scoped>
  365. </style>
  366. <style lang="less" scoped>
  367. @import "../style.less";
  368. </style>