index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. <template>
  2. <div class="book org">
  3. <ProjectMenu cur-key="org/offlinepackageauth" />
  4. <el-row>
  5. <el-button type="primary" @click="dialogFormEdit = true">创建授权</el-button>
  6. </el-row>
  7. <el-divider />
  8. <div id="query-form">
  9. <el-form inline>
  10. <el-form-item prop="book_name" label="教材">
  11. <el-input v-model="queryForm.book_name" clearable />
  12. </el-form-item>
  13. <el-form-item prop="auth_code" label="授权码">
  14. <el-input v-model="queryForm.auth_code" clearable />
  15. </el-form-item>
  16. <el-form-item prop="memo" label="备注">
  17. <el-input v-model="queryForm.memo" clearable />
  18. </el-form-item>
  19. <el-form-item class="search-box">
  20. <el-button class="search-btn" type="primary" @click="queryList">查询</el-button>
  21. </el-form-item>
  22. </el-form>
  23. </div>
  24. <div class="book-list">
  25. <el-table :data="list">
  26. <el-table-column label="序号" width="60" align="center" header-align="center" class-name="index-column">
  27. <template slot-scope="{ $index }">
  28. {{ cur_page_begin_index + $index }}
  29. </template>
  30. </el-table-column>
  31. <el-table-column prop="book_name" min-width="200" label="教材" />
  32. <el-table-column prop="auth_code" label="授权码" width="180" align="center" />
  33. <el-table-column prop="effective_count" label="有效次数" width="100" align="right" />
  34. <el-table-column label="激活状态" width="100" align="center">
  35. <template slot-scope="{ row }">
  36. {{ isTrue(row.is_activated) ? '已激活' : '未激活' }}
  37. </template>
  38. </el-table-column>
  39. <el-table-column label="激活时间" width="160" align="center">
  40. <template slot-scope="{ row }">
  41. <span v-if="isTrue(row.is_activated)">{{ row.activate_time }}</span>
  42. <span v-else>--</span>
  43. </template>
  44. </el-table-column>
  45. <el-table-column label="有效截止日期" width="120" align="center">
  46. <template slot-scope="{ row }">
  47. <span v-if="isTrue(row.is_disabled)" style="color: red">已废弃</span>
  48. <span v-else>{{ row.effective_end_date }}</span>
  49. </template>
  50. </el-table-column>
  51. <el-table-column prop="creator_name" label="创建人" width="120" align="center" />
  52. <el-table-column prop="create_time" label="创建时间" width="160" align="center" />
  53. <el-table-column label="操作" fixed="right" width="180" align="center">
  54. <template slot-scope="{ row }">
  55. <span class="link" @click="selectDirectory(row.id)">导出离线包</span>
  56. <el-divider direction="vertical" />
  57. <span class="link" @click="handleDisable(row.id)">废弃</span>
  58. <el-divider direction="vertical" />
  59. <span class="link" @click="handleDel(row.id)">删除</span>
  60. </template>
  61. </el-table-column>
  62. <el-table-column prop="memo" label="备注" />
  63. </el-table>
  64. <PaginationPage :total="total" @getList="pageList" />
  65. </div>
  66. <el-dialog title="创建授权" width="500px" :visible.sync="dialogFormEdit" :close-on-click-modal="false">
  67. <el-form ref="editForm" :model="editForm" :rules="rules" label-width="120px">
  68. <el-form-item label="教材" prop="book_name">
  69. <el-input v-model="editForm.book_name" disabled class="input-with-select" placeholder="请选择教材">
  70. <el-button slot="append" icon="el-icon-search" @click="handleSearchBook('')" />
  71. </el-input>
  72. </el-form-item>
  73. <el-form-item label="有效次数" prop="effective_count">
  74. <el-input v-model.number="editForm.effective_count" placeholder="请输入有效次数" />
  75. </el-form-item>
  76. <el-form-item label="有效截止日期" prop="effective_end_date">
  77. <el-date-picker
  78. v-model="editForm.effective_end_date"
  79. type="date"
  80. placeholder="选择日期"
  81. value-format="yyyy-MM-dd"
  82. />
  83. </el-form-item>
  84. <el-form-item label="备注" prop="memo">
  85. <el-input v-model="editForm.memo" type="textarea" />
  86. </el-form-item>
  87. </el-form>
  88. <div slot="footer" class="dialog-footer">
  89. <el-button @click="dialogFormEdit = false">取 消</el-button>
  90. <el-button type="primary" @click="handleSave('editForm')">确 定</el-button>
  91. </div>
  92. </el-dialog>
  93. <el-dialog title="选择教材" width="950px" :visible.sync="dialogSearchBook" :close-on-click-modal="false">
  94. <el-table :data="bookList">
  95. <el-table-column label="序号" width="60" align="center" header-align="center" class-name="index-column">
  96. <template slot-scope="{ $index }">
  97. {{ cur_page_begin_index + $index }}
  98. </template>
  99. </el-table-column>
  100. <el-table-column prop="sn" label="编号" width="120" header-align="center" />
  101. <el-table-column prop="name" label="名称" min-width="240" header-align="center" />
  102. <el-table-column prop="project_sn" label="项目编号" width="120" header-align="center" />
  103. <el-table-column prop="project_name" label="项目名称" width="240" header-align="center" />
  104. <el-table-column label="操作" width="80">
  105. <template #default="{ row }">
  106. <el-button type="primary" @click="selectBook(row)">选择</el-button>
  107. </template>
  108. </el-table-column>
  109. </el-table>
  110. <PaginationPage :total="bookTotal" @getList="pageBookList" />
  111. </el-dialog>
  112. <CommonProgress
  113. :percentage="percentage"
  114. :visible.sync="visible"
  115. title="导出离线包"
  116. :text="`正在导出离线包,请稍候...`"
  117. />
  118. </div>
  119. </template>
  120. <script>
  121. import ProjectMenu from '@/views/project_manage/common/ProjectMenu.vue';
  122. import PaginationPage from '@/components/PaginationPage.vue';
  123. import CommonProgress from '@/components/CommonProgress.vue';
  124. import { PageQueryYSJBookList_OrgManager } from '@/api/list';
  125. import {
  126. AddBookOfflinePackAuth,
  127. DeleteBookOfflinePackAuth,
  128. DisableBookOfflinePackAuth,
  129. PageQueryBookOfflinePackAuthList,
  130. CreateBookOfflinePackChapterStructFileList,
  131. CreateOfflinePackCoursewareFileList,
  132. } from '@/api/offline';
  133. import { isTrue } from '@/utils/validate';
  134. export default {
  135. name: 'OrgProjectManageOfflinePackAuth',
  136. components: {
  137. ProjectMenu,
  138. PaginationPage,
  139. CommonProgress,
  140. },
  141. data() {
  142. return {
  143. list: [],
  144. total: 0,
  145. cur_page_begin_index: 0,
  146. bookList: [],
  147. bookTotal: 0,
  148. bookPageIndex: 0,
  149. isTrue,
  150. dialogFormEdit: false,
  151. dialogSearchBook: false,
  152. queryForm: {
  153. book_name: '',
  154. auth_code: '',
  155. memo: '',
  156. page_capacity: 10,
  157. cur_page: 1,
  158. },
  159. editForm: {
  160. book_id: '',
  161. book_name: '',
  162. effective_count: null,
  163. effective_end_date: '',
  164. memo: '',
  165. },
  166. rules: {
  167. book_name: [{ required: true, message: '请选择教材', trigger: 'blur' }],
  168. effective_count: [
  169. { required: true, message: '请填写有效次数', trigger: 'blur' },
  170. { type: 'number', message: '有效次数必须为数字值' },
  171. ],
  172. effective_end_date: [{ required: true, message: '请选择有效截止日期', trigger: 'blur' }],
  173. },
  174. tempDir: '', // 临时目录,用于存放下载的文件
  175. savePath: '', // 保存离线包的路径
  176. packageName: 'EEP 离线包', // 离线包名称
  177. file_info_list: [], // 章节结构文件列表
  178. downloadTotal: 0, // 下载总数
  179. downloadCompleted: 0, // 已下载数量
  180. downloadWatcher: null, // 下载进度监听器
  181. visible: false,
  182. percentage: 0,
  183. };
  184. },
  185. computed: {
  186. downloadProgress() {
  187. if (this.downloadTotal === 0) return 0;
  188. return (this.downloadCompleted / this.downloadTotal) * 100;
  189. },
  190. },
  191. watch: {
  192. dialogFormEdit: {
  193. handler(val) {
  194. if (!val) {
  195. this.$refs.editForm.resetFields();
  196. }
  197. },
  198. deep: true,
  199. },
  200. },
  201. beforeDestroy() {
  202. // 删除临时目录及其内容
  203. if (this.tempDir && this.tempDir.length > 0) {
  204. window.fileAPI.deleteTempDir(this.tempDir);
  205. this.tempDir = '';
  206. }
  207. // 删除 watch 监听器
  208. if (this.downloadWatcher) {
  209. this.downloadWatcher();
  210. this.downloadWatcher = null;
  211. }
  212. },
  213. methods: {
  214. queryList() {
  215. this.pageList(this.queryForm);
  216. },
  217. pageList(data) {
  218. Object.assign(this.queryForm, data);
  219. PageQueryBookOfflinePackAuthList(this.queryForm).then(
  220. ({ total_count, offline_pack_auth_list, cur_page_begin_index }) => {
  221. this.list = offline_pack_auth_list;
  222. this.total = total_count;
  223. this.cur_page_begin_index = cur_page_begin_index;
  224. },
  225. );
  226. },
  227. pageBookList(data) {
  228. this.handleSearchBook(data);
  229. },
  230. handleSearchBook(data) {
  231. let page = data || {
  232. cur_page: 1,
  233. page_capacity: 10,
  234. };
  235. PageQueryYSJBookList_OrgManager(page).then(({ total_count, book_list, cur_page_begin_index }) => {
  236. this.bookList = book_list;
  237. this.bookTotal = total_count;
  238. this.bookPageIndex = cur_page_begin_index;
  239. this.dialogSearchBook = true;
  240. });
  241. },
  242. selectBook(book) {
  243. this.editForm.book_name = book.name;
  244. this.editForm.book_id = book.id;
  245. this.dialogSearchBook = false;
  246. },
  247. handleSave(formName) {
  248. this.$refs[formName].validate((valid) => {
  249. if (valid) {
  250. let data = this.editForm;
  251. this.dialogSearchBook = false;
  252. AddBookOfflinePackAuth(data).then((res) => {
  253. if (res && res.status === 1) {
  254. this.dialogFormEdit = false;
  255. this.queryList();
  256. }
  257. });
  258. } else {
  259. return false;
  260. }
  261. });
  262. },
  263. async handleDisable(id) {
  264. this.$confirm('确定要废弃此条数据吗?', '提示', {
  265. confirmButtonText: '确定',
  266. cancelButtonText: '取消',
  267. type: 'warning',
  268. })
  269. .then(() => {
  270. DisableBookOfflinePackAuth({ id }).then(() => {
  271. this.queryList();
  272. });
  273. })
  274. .catch(() => {});
  275. },
  276. async handleDel(id) {
  277. this.$confirm('确定要删除此条数据吗?', '提示', {
  278. confirmButtonText: '确定',
  279. cancelButtonText: '取消',
  280. type: 'warning',
  281. })
  282. .then(() => {
  283. DeleteBookOfflinePackAuth({ id }).then(() => {
  284. this.queryList();
  285. });
  286. })
  287. .catch(() => {});
  288. },
  289. /**
  290. * 选择保存离线包的目录
  291. * @param {String} book_id 教材ID
  292. */
  293. async selectDirectory(book_id) {
  294. const result = await window.fileAPI.openFileDialog({
  295. title: '选择保存离线包的文件夹',
  296. properties: ['openDirectory', 'createDirectory'],
  297. });
  298. if (result.canceled) {
  299. this.$message.warning('未选择文件夹,操作已取消');
  300. return;
  301. }
  302. this.savePath = result.filePaths[0];
  303. this.downloadOfflinePackage(book_id);
  304. },
  305. /**
  306. * 下载离线包
  307. * @param {String} offline_pack_auth_id 离线包授权ID
  308. */
  309. async downloadOfflinePackage(offline_pack_auth_id) {
  310. const { file_info_list, courseware_id_list } = await CreateBookOfflinePackChapterStructFileList({
  311. offline_pack_auth_id,
  312. });
  313. this.file_info_list = file_info_list;
  314. if (file_info_list.length === 0) {
  315. this.$message.error('章节结构文件生成失败,无法下载离线包');
  316. return;
  317. }
  318. if (this.tempDir.length === 0) {
  319. this.tempDir = window.fileAPI.createTempDir(); // 创建临时保存目录
  320. }
  321. this.visible = true;
  322. for (const { dir_name, file_name, file_url } of this.file_info_list) {
  323. const dirPath = dir_name.length > 0 ? `${this.tempDir}\\${dir_name}` : this.tempDir;
  324. await window.fileAPI.downloadFile(file_url, `${dirPath}\\${file_name}`);
  325. }
  326. const struct = await this.readFileContent('struct.json'); // 读取章节结构文件内容
  327. if (struct.node_list && struct.node_list.length > 0) {
  328. this.packageName = struct.node_list[0].name; // 设置离线包名称为课件名称
  329. }
  330. if (courseware_id_list && courseware_id_list.length > 0) {
  331. Promise.all(
  332. courseware_id_list.map((courseware_id) => this.CreateOfflinePackCoursewareFileList(courseware_id)),
  333. ).then(() => {
  334. this.downloadWatcher = this.$watch(
  335. 'downloadProgress',
  336. (newVal) => {
  337. if (newVal >= 100) {
  338. this.downloadWatcher();
  339. this.startCompress();
  340. } else {
  341. this.percentage = (
  342. (this.downloadCompleted / (this.downloadTotal + this.downloadTotal * 0.1)) *
  343. 100
  344. ).toFixed(2);
  345. }
  346. },
  347. { immediate: true },
  348. );
  349. });
  350. }
  351. },
  352. /**
  353. * 下载课件文件列表
  354. * @param {Number} courseware_id 课件ID
  355. * @return {Promise} Promise对象
  356. */
  357. async CreateOfflinePackCoursewareFileList(courseware_id) {
  358. const { file_info_list } = await CreateOfflinePackCoursewareFileList({ courseware_id });
  359. this.downloadTotal += file_info_list.length;
  360. file_info_list.forEach((fileInfo) => {
  361. const { file_name, file_url, dir_name } = fileInfo;
  362. // 如果有子目录,则判断是否有,没有就创建子目录
  363. if (dir_name.split('/').length > 1) {
  364. if (!window.fileAPI.existsSync(`${this.tempDir}\\${dir_name}`)) {
  365. window.fileAPI.mkdirSync(`${this.tempDir}\\${dir_name}`);
  366. }
  367. }
  368. window.fileAPI.downloadFile(file_url, `${this.tempDir}\\${dir_name}\\${file_name}`).then(() => {
  369. this.downloadCompleted += 1;
  370. });
  371. });
  372. },
  373. /**
  374. * 开始压缩
  375. */
  376. async startCompress() {
  377. if (this.downloadWatcher) {
  378. this.downloadWatcher = null;
  379. }
  380. const sources = [`${this.tempDir}\\resource`, `${this.tempDir}\\courseware`]; // 要压缩的文件或文件夹路径数组
  381. this.file_info_list.forEach(({ dir_name, file_name }) => {
  382. const dirPath = dir_name.length > 0 ? `${this.tempDir}\\${dir_name}` : this.tempDir;
  383. sources.push(`${dirPath}\\${file_name}`);
  384. });
  385. const dest = `${this.savePath}\\${this.packageName}.eep`; // 压缩包保存路径
  386. try {
  387. const offErr = window.fileAPI.onStderr((text) => console.warn('7z stderr:', text));
  388. await window.fileAPI.compress({
  389. sources,
  390. dest,
  391. format: 'zip',
  392. level: 5,
  393. password: '1234567a',
  394. recurse: true,
  395. });
  396. offErr();
  397. } catch (e) {
  398. console.error('压缩失败:', e);
  399. this.$message.error('离线包下载失败,请重试!');
  400. this.tempDir = '';
  401. this.visible = false;
  402. return;
  403. } finally {
  404. this.percentage = 100;
  405. this.visible = false;
  406. }
  407. // 删除临时目录及其内容
  408. try {
  409. await window.fileAPI.deleteTempDir(this.tempDir);
  410. this.tempDir = '';
  411. } catch (e) {
  412. console.error('删除临时目录失败:', e);
  413. this.tempDir = '';
  414. } finally {
  415. this.percentage = 0;
  416. }
  417. this.$message.success('离线包下载并保存成功!');
  418. },
  419. /**
  420. * 读取文件内容
  421. * @param entryName 文件条目名称
  422. * @param subdirectory 子目录
  423. */
  424. async readFileContent(entryName, subdirectory = '') {
  425. const content = await window.fileAPI.readZipFileSync(`${this.tempDir}/${subdirectory}`, entryName);
  426. const text = new TextDecoder().decode(content);
  427. const obj = JSON.parse(text);
  428. return obj;
  429. },
  430. },
  431. };
  432. </script>
  433. <style lang="scss" scoped>
  434. @use '@/styles/mixin.scss' as *;
  435. .book {
  436. @include page-base;
  437. @include table-list;
  438. }
  439. .el-divider--horizontal {
  440. margin: 10px 0 !important;
  441. }
  442. .el-form-item__content > * {
  443. width: 100%;
  444. }
  445. </style>