SelectResource.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. <template>
  2. <el-dialog
  3. custom-class="select-resource-dialog"
  4. title="选择资源"
  5. :visible="visible"
  6. :close-on-click-modal="false"
  7. @close="dialogClose"
  8. >
  9. <div class="search-box">
  10. <div class="search-left">
  11. <el-input v-model="search_content" />
  12. <el-button type="primary" @click="queryList()">查询</el-button>
  13. </div>
  14. <div class="search-right">
  15. <label>排序:</label>
  16. <el-select v-model="sort_value" placeholder="请选择">
  17. <el-option v-for="item in sort_list" :key="item.value" :label="item.label" :value="item.value" />
  18. </el-select>
  19. <SvgIcon :icon-class="isDesc ? 'sort-down' : 'sort-up'" size="16" style="cursor: pointer" @click="changeSort" />
  20. </div>
  21. </div>
  22. <div v-loading="loading" class="sources-box">
  23. <div
  24. v-for="(item, index) in list"
  25. :key="index"
  26. class="sources-item"
  27. :class="[select_sources_id === item.id ? 'active' : '']"
  28. @click="selectSourceNode(item)"
  29. >
  30. <template v-if="type_index === 0"> <el-image :src="item.file_url" fit="contain" /></template>
  31. <template v-else-if="type_index === 1">
  32. <div class="sources-item-border sources-item-audio">
  33. <SvgIcon icon-class="mp3" size="16" />
  34. <span>{{ realFormatSecond(item.media_duration) }}</span>
  35. <SvgIcon icon-class="play-large-fill" size="16" @click="viewDialog(item)" />
  36. </div>
  37. </template>
  38. <template v-else-if="type_index === 2">
  39. <video
  40. class="sources-item-border"
  41. :src="item.file_url"
  42. width="100%"
  43. height="140px"
  44. :poster="item.video_preview_image_file_url"
  45. preload="none"
  46. ></video>
  47. </template>
  48. <template v-else-if="type_index === 3">
  49. <el-image style="height: 90px" :src="require('@/assets/h5-games.png')" fit="contain" />
  50. </template>
  51. <template v-else-if="type_index === 4">
  52. <el-image style="height: 90px" :src="require('@/assets/3d-model.png')" fit="contain"
  53. /></template>
  54. <template v-else-if="type_index === 5">
  55. <el-image style="height: 90px" :src="require('@/assets/txt.png')" fit="contain" />
  56. </template>
  57. <el-popover placement="bottom" width="300" trigger="hover">
  58. <div class="sources-info">
  59. <p class="name">名称:{{ item.name }}</p>
  60. <p class="label">标签:{{ item.label }}</p>
  61. <p class="name">简介:{{ item.intro }}</p>
  62. <p class="label">修改时间:{{ item.last_modify_time }}</p>
  63. <p class="label">文件大小:{{ item.file_size_desc }}</p>
  64. </div>
  65. <template #reference>
  66. <div class="sources-info">
  67. <p class="name">
  68. {{ item.name
  69. }}<SvgIcon v-show="item.file_id" icon-class="uploadPreview" size="16" @click="viewDialog(item)" />
  70. </p>
  71. <b class="label">{{ item.label }}</b>
  72. </div>
  73. </template>
  74. </el-popover>
  75. </div>
  76. </div>
  77. <PaginationPage ref="pagination" :total="total" @getList="queryList" />
  78. <div slot="footer">
  79. <el-button @click="dialogClose">取消</el-button>
  80. <el-button type="primary" @click="confirmSelect">选择</el-button>
  81. </div>
  82. <el-dialog
  83. v-if="visible_flag"
  84. :visible.sync="visible_flag"
  85. :show-close="true"
  86. :close-on-click-modal="true"
  87. :modal-append-to-body="true"
  88. :append-to-body="true"
  89. :lock-scroll="true"
  90. width="80%"
  91. top="0"
  92. >
  93. <iframe v-if="visible_flag" :src="newpath" width="100%" :height="iframeHeight" frameborder="0"></iframe>
  94. </el-dialog>
  95. </el-dialog>
  96. </template>
  97. <script>
  98. import PaginationPage from '@/components/PaginationPage.vue';
  99. import AudioLine from '@/views/personal_workbench/project/components/AudioLine.vue';
  100. import { PageQueryProjectResourceList } from '@/api/list';
  101. import { H5StartupFile, GetFileURLMap } from '@/api/app';
  102. const Base64 = require('js-base64').Base64;
  103. import { getConfig } from '@/utils/auth';
  104. export default {
  105. name: 'SelectResource',
  106. components: {
  107. PaginationPage,
  108. AudioLine,
  109. },
  110. props: {
  111. visible: {
  112. type: Boolean,
  113. default: false,
  114. },
  115. // 教材 id
  116. projectId: {
  117. type: String,
  118. default: '',
  119. },
  120. // 课件 id
  121. coursewareId: {
  122. type: String,
  123. default: '',
  124. },
  125. // 允许的文件类型
  126. accept: {
  127. type: String,
  128. default: '*',
  129. },
  130. },
  131. data() {
  132. const type_list = [
  133. {
  134. value: 0,
  135. key: 'image',
  136. label: '图片',
  137. },
  138. {
  139. value: 1,
  140. key: 'audio',
  141. label: '音频',
  142. },
  143. {
  144. value: 2,
  145. key: 'video',
  146. label: '视频',
  147. },
  148. {
  149. value: 3,
  150. key: 'h5_game',
  151. label: 'H5 游戏',
  152. },
  153. {
  154. value: 4,
  155. key: '3d_model',
  156. label: '3D 模型',
  157. },
  158. {
  159. value: 5,
  160. key: 'text',
  161. label: '文本',
  162. },
  163. ];
  164. return {
  165. loading: false,
  166. list: [],
  167. total: 0,
  168. page_capacity: 10,
  169. cur_page: 1,
  170. type_list,
  171. type_index: type_list.findIndex((item) => item.key === this.accept) || 0,
  172. sort_list: [
  173. {
  174. value: 'name',
  175. label: '名称',
  176. },
  177. {
  178. value: 'label',
  179. label: '标签',
  180. },
  181. {
  182. value: 'last_modify_time',
  183. label: '修改时间',
  184. },
  185. {
  186. value: 'file_size',
  187. label: '文件大小',
  188. },
  189. ], // 排序分类
  190. sort_value: '', // 排序值
  191. isDesc: false, // 排序是否为倒序
  192. search_content: '',
  193. select_sources_id: '',
  194. visible_flag: false,
  195. newpath: '',
  196. iframeHeight: `${window.innerHeight - 100}px`,
  197. file_preview_url: getConfig() ? getConfig().doc_preview_service_address : '',
  198. };
  199. },
  200. created() {},
  201. methods: {
  202. dialogClose() {
  203. this.$emit('update:visible', false);
  204. this.$emit('close');
  205. },
  206. // 查询列表
  207. queryList(data) {
  208. this.loading = true;
  209. this.list = [];
  210. if (data) {
  211. this.page_capacity = data.page_capacity;
  212. this.cur_page = data.cur_page;
  213. } else {
  214. this.page_capacity = 10;
  215. this.cur_page = 1;
  216. }
  217. let order_column_list = [];
  218. if (this.sort_value) {
  219. order_column_list = [this.isDesc ? `${this.sort_value}:desc` : this.sort_value];
  220. }
  221. PageQueryProjectResourceList({
  222. page_capacity: this.page_capacity,
  223. cur_page: this.cur_page,
  224. project_id: this.projectId,
  225. book_chapter_node_id: this.coursewareId,
  226. type: this.type_list[this.type_index].value,
  227. name_or_label: this.search_content,
  228. order_column_list,
  229. })
  230. .then(({ total_count, resource_list }) => {
  231. this.total = total_count;
  232. // if (this.type_index === 5) {
  233. // resource_list.forEach((item) => {
  234. // item.new_path = `${this.file_preview_url}onlinePreview?url=${Base64.encode(item.file_url)}`;
  235. // });
  236. // this.loading = false;
  237. // }
  238. // else if (this.type_index === 3) {
  239. // resource_list.forEach((item) => {
  240. // H5StartupFile({ file_id: item.file_id, index_file_name: 'index.html' }).then((res) => {
  241. // item.new_path = res.file_url;
  242. // this.loading = false;
  243. // });
  244. // });
  245. // }
  246. // else {
  247. this.loading = false;
  248. // }
  249. this.list = resource_list;
  250. })
  251. .catch(() => {
  252. this.loading = false;
  253. });
  254. },
  255. selectSourceNode(item) {
  256. this.select_sources_id = item.id;
  257. },
  258. // 切换排序升序降序
  259. changeSort() {
  260. this.isDesc = !this.isDesc;
  261. },
  262. // 将整数转换成 时:分:秒的格式
  263. realFormatSecond(value) {
  264. let theTime = parseInt(value); // 秒
  265. let theTime1 = 0; // 分
  266. let theTime2 = 0; // 小时
  267. if (theTime > 60) {
  268. theTime1 = parseInt(theTime / 60);
  269. theTime = parseInt(theTime % 60);
  270. if (theTime1 > 60) {
  271. theTime2 = parseInt(theTime1 / 60);
  272. theTime1 = parseInt(theTime1 % 60);
  273. }
  274. }
  275. let result = String(parseInt(theTime));
  276. if (result < 10) {
  277. result = `0${result}`;
  278. }
  279. if (theTime1 > 0) {
  280. result = `${String(parseInt(theTime1))}:${result}`;
  281. if (theTime1 < 10) {
  282. result = `0${result}`;
  283. }
  284. } else {
  285. result = `00:${result}`;
  286. }
  287. if (theTime2 > 0) {
  288. result = `${String(parseInt(theTime2))}:${result}`;
  289. if (theTime2 < 10) {
  290. result = `0${result}`;
  291. }
  292. } else {
  293. // result = "00:" + result;
  294. }
  295. return result;
  296. },
  297. // 选择资源
  298. confirmSelect() {
  299. if (!this.select_sources_id) {
  300. this.$message.error('请选择资源');
  301. return;
  302. }
  303. const selectedResource = this.list.find((item) => item.id === this.select_sources_id);
  304. this.$emit('selectResource', {
  305. file_id: selectedResource.file_id,
  306. file_name: selectedResource.name,
  307. file_url: selectedResource.file_url,
  308. intro: selectedResource.intro,
  309. });
  310. },
  311. // 预览
  312. viewDialog(file) {
  313. this.newpath = '';
  314. if (this.type_index === 3) {
  315. const suffix = file.name.slice(file.name.lastIndexOf('.') + 1, file.name.length).toLowerCase();
  316. if (suffix === 'html') {
  317. this.newpath = file.file_url;
  318. this.visible_flag = true;
  319. } else {
  320. H5StartupFile({ file_id: file.file_id, index_file_name: 'index.html' }).then((res) => {
  321. this.newpath = res.file_url;
  322. this.visible_flag = true;
  323. });
  324. }
  325. } else {
  326. GetFileURLMap({ file_id_list: [file.file_id] }).then(({ url_map }) => {
  327. this.newpath = `${this.file_preview_url}onlinePreview?url=${Base64.encode(url_map[file.file_id])}`;
  328. this.visible_flag = true;
  329. });
  330. }
  331. },
  332. },
  333. };
  334. </script>
  335. <style lang="scss" scoped>
  336. .select-resource-dialog {
  337. .search-box {
  338. display: flex;
  339. justify-content: space-between;
  340. padding: 5px;
  341. .search-left {
  342. display: flex;
  343. flex-flow: wrap;
  344. gap: 5px;
  345. align-items: center;
  346. }
  347. .label-btn {
  348. padding: 5px 10px;
  349. cursor: pointer;
  350. background: #ebebeb;
  351. border-radius: 4px;
  352. &.active {
  353. color: #f90;
  354. background: #ffefd6;
  355. }
  356. }
  357. .el-input,
  358. .el-select {
  359. width: 120px;
  360. }
  361. }
  362. .sources-box {
  363. display: flex;
  364. flex-flow: wrap;
  365. gap: 10px;
  366. place-content: start start;
  367. height: 400px;
  368. padding: 5px;
  369. overflow: auto;
  370. .sources-item {
  371. width: 200px;
  372. cursor: pointer;
  373. &-txt {
  374. width: 400px;
  375. }
  376. &-zip {
  377. width: 500px;
  378. }
  379. &-border {
  380. border: 1px solid #ccc;
  381. }
  382. &-audio {
  383. display: flex;
  384. align-items: center;
  385. justify-content: space-between;
  386. padding: 9px 5px;
  387. }
  388. .el-image {
  389. width: 100%;
  390. height: 140px;
  391. border: 1px solid #ccc;
  392. }
  393. .name,
  394. .label {
  395. margin: 5px 0;
  396. font-size: 14px;
  397. font-weight: normal;
  398. line-height: 1.3;
  399. }
  400. :deep .audioLine {
  401. background: #f2f3f5;
  402. }
  403. &.active {
  404. .el-image,
  405. .sources-item-border {
  406. border-color: #f90;
  407. }
  408. .name,
  409. .label {
  410. color: #f90;
  411. }
  412. }
  413. }
  414. }
  415. }
  416. </style>