SelectResource.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. append-to-body
  9. >
  10. <div class="search-box">
  11. <div class="search-left">
  12. <el-input v-model="search_content" />
  13. <el-button type="primary" @click="queryList()">查询</el-button>
  14. </div>
  15. <div class="search-right">
  16. <label>排序:</label>
  17. <el-select v-model="sort_value" placeholder="请选择">
  18. <el-option v-for="item in sort_list" :key="item.value" :label="item.label" :value="item.value" />
  19. </el-select>
  20. <SvgIcon :icon-class="isDesc ? 'sort-down' : 'sort-up'" size="16" style="cursor: pointer" @click="changeSort" />
  21. </div>
  22. </div>
  23. <div v-loading="loading" class="sources-box">
  24. <div
  25. v-for="(item, index) in list"
  26. :key="index"
  27. class="sources-item"
  28. :class="[select_sources_id === item.id ? 'active' : '']"
  29. @click="selectSourceNode(item)"
  30. >
  31. <template v-if="type_index === 0"> <el-image :src="item.file_url" fit="contain" /></template>
  32. <template v-else-if="type_index === 1">
  33. <div class="sources-item-border sources-item-audio">
  34. <SvgIcon icon-class="mp3" size="16" />
  35. <span>{{ realFormatSecond(item.media_duration) }}</span>
  36. <SvgIcon icon-class="play-large-fill" size="16" @click="viewDialog(item)" />
  37. </div>
  38. </template>
  39. <template v-else-if="type_index === 2">
  40. <video
  41. class="sources-item-border"
  42. :src="item.file_url"
  43. width="100%"
  44. height="140px"
  45. :poster="item.video_preview_image_file_url"
  46. preload="none"
  47. ></video>
  48. </template>
  49. <template v-else-if="type_index === 3">
  50. <el-image style="height: 90px" :src="require('@/assets/h5-games.png')" fit="contain" />
  51. </template>
  52. <template v-else-if="type_index === 4">
  53. <el-image style="height: 90px" :src="require('@/assets/3d-model.png')" fit="contain" />
  54. </template>
  55. <template v-else-if="type_index === 5">
  56. <el-image style="height: 90px" :src="require('@/assets/txt.png')" fit="contain" />
  57. </template>
  58. <el-popover placement="bottom" width="300" trigger="hover">
  59. <div class="sources-info">
  60. <p class="name">名称:{{ item.name }}</p>
  61. <p class="label">标签:{{ item.label }}</p>
  62. <p class="name">简介:{{ item.intro }}</p>
  63. <p class="label">修改时间:{{ item.last_modify_time }}</p>
  64. <p class="label">文件大小:{{ item.file_size_desc }}</p>
  65. </div>
  66. <template #reference>
  67. <div class="sources-info">
  68. <p class="name">
  69. {{ item.name
  70. }}<SvgIcon v-show="item.file_id" icon-class="uploadPreview" size="16" @click="viewDialog(item)" />
  71. </p>
  72. <b class="label">{{ item.label }}</b>
  73. </div>
  74. </template>
  75. </el-popover>
  76. </div>
  77. </div>
  78. <PaginationPage ref="pagination" :total="total" @getList="queryList" />
  79. <div slot="footer">
  80. <el-button @click="dialogClose">取消</el-button>
  81. <el-button type="primary" @click="confirmSelect">选择</el-button>
  82. </div>
  83. <el-dialog
  84. v-if="visible_flag"
  85. :visible.sync="visible_flag"
  86. :show-close="true"
  87. :close-on-click-modal="true"
  88. :modal-append-to-body="true"
  89. :append-to-body="true"
  90. :lock-scroll="true"
  91. width="80%"
  92. top="0"
  93. >
  94. <iframe v-if="visible_flag" :src="newpath" width="100%" :height="iframeHeight" frameborder="0"></iframe>
  95. </el-dialog>
  96. </el-dialog>
  97. </template>
  98. <script>
  99. import PaginationPage from '@/components/PaginationPage.vue';
  100. import { PageQueryProjectResourceList } from '@/api/list';
  101. import { H5StartupFile } 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. },
  109. props: {
  110. visible: {
  111. type: Boolean,
  112. default: false,
  113. },
  114. // 教材 id
  115. projectId: {
  116. type: String,
  117. default: '',
  118. },
  119. // 课件 id
  120. coursewareId: {
  121. type: String,
  122. default: '',
  123. },
  124. // 允许的文件类型
  125. accept: {
  126. type: String,
  127. default: '*',
  128. },
  129. },
  130. data() {
  131. const type_list = [
  132. {
  133. value: 0,
  134. key: 'image',
  135. label: '图片',
  136. },
  137. {
  138. value: 1,
  139. key: 'audio',
  140. label: '音频',
  141. },
  142. {
  143. value: 2,
  144. key: 'video',
  145. label: '视频',
  146. },
  147. {
  148. value: 3,
  149. key: 'h5_game',
  150. label: 'H5 游戏',
  151. },
  152. {
  153. value: 4,
  154. key: '3d_model',
  155. label: '3D 模型',
  156. },
  157. {
  158. value: 5,
  159. key: 'text',
  160. label: '文本',
  161. },
  162. ];
  163. return {
  164. loading: false,
  165. list: [],
  166. total: 0,
  167. page_capacity: 10,
  168. cur_page: 1,
  169. type_list,
  170. type_index: type_list.findIndex((item) => item.key === this.accept) || 0,
  171. sort_list: [
  172. {
  173. value: 'name',
  174. label: '名称',
  175. },
  176. {
  177. value: 'label',
  178. label: '标签',
  179. },
  180. {
  181. value: 'last_modify_time',
  182. label: '修改时间',
  183. },
  184. {
  185. value: 'file_size',
  186. label: '文件大小',
  187. },
  188. ], // 排序分类
  189. sort_value: '', // 排序值
  190. isDesc: false, // 排序是否为倒序
  191. search_content: '',
  192. select_sources_id: '',
  193. visible_flag: false,
  194. newpath: '',
  195. iframeHeight: `${window.innerHeight - 100}px`,
  196. file_preview_url: getConfig() ? getConfig().doc_preview_service_address : '',
  197. };
  198. },
  199. created() {},
  200. methods: {
  201. dialogClose() {
  202. this.$emit('update:visible', false);
  203. this.$emit('close');
  204. },
  205. // 查询列表
  206. queryList(data) {
  207. this.loading = true;
  208. this.list = [];
  209. if (data) {
  210. this.page_capacity = data.page_capacity;
  211. this.cur_page = data.cur_page;
  212. } else {
  213. this.page_capacity = 10;
  214. this.cur_page = 1;
  215. }
  216. let order_column_list = [];
  217. if (this.sort_value) {
  218. order_column_list = [this.isDesc ? `${this.sort_value}:desc` : this.sort_value];
  219. }
  220. PageQueryProjectResourceList({
  221. page_capacity: this.page_capacity,
  222. cur_page: this.cur_page,
  223. project_id: this.projectId,
  224. book_chapter_node_id: this.coursewareId,
  225. is_search_parent_node_resource: 'true',
  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. this.newpath = `${this.file_preview_url}onlinePreview?url=${Base64.encode(file.file_url)}`;
  327. this.visible_flag = true;
  328. }
  329. },
  330. },
  331. };
  332. </script>
  333. <style lang="scss" scoped>
  334. .select-resource-dialog {
  335. .search-box {
  336. display: flex;
  337. justify-content: space-between;
  338. padding: 5px;
  339. .search-left {
  340. display: flex;
  341. flex-flow: wrap;
  342. gap: 5px;
  343. align-items: center;
  344. }
  345. .label-btn {
  346. padding: 5px 10px;
  347. cursor: pointer;
  348. background: #ebebeb;
  349. border-radius: 4px;
  350. &.active {
  351. color: #f90;
  352. background: #ffefd6;
  353. }
  354. }
  355. .el-input,
  356. .el-select {
  357. width: 120px;
  358. }
  359. }
  360. .sources-box {
  361. display: flex;
  362. flex-flow: wrap;
  363. gap: 10px;
  364. place-content: start start;
  365. height: 400px;
  366. padding: 5px;
  367. overflow: auto;
  368. .sources-item {
  369. width: 200px;
  370. cursor: pointer;
  371. &-txt {
  372. width: 400px;
  373. }
  374. &-zip {
  375. width: 500px;
  376. }
  377. &-border {
  378. border: 1px solid #ccc;
  379. }
  380. &-audio {
  381. display: flex;
  382. align-items: center;
  383. justify-content: space-between;
  384. padding: 9px 5px;
  385. }
  386. .el-image {
  387. width: 100%;
  388. height: 140px;
  389. border: 1px solid #ccc;
  390. }
  391. .name,
  392. .label {
  393. margin: 5px 0;
  394. font-size: 14px;
  395. font-weight: normal;
  396. line-height: 1.3;
  397. }
  398. :deep .audioLine {
  399. background: #f2f3f5;
  400. }
  401. &.active {
  402. .el-image,
  403. .sources-item-border {
  404. border-color: #f90;
  405. }
  406. .name,
  407. .label {
  408. color: #f90;
  409. }
  410. }
  411. }
  412. }
  413. }
  414. </style>