index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. </div>
  113. </template>
  114. <script>
  115. import ProjectMenu from '@/views/project_manage/common/ProjectMenu.vue';
  116. import PaginationPage from '@/components/PaginationPage.vue';
  117. import { PageQueryYSJBookList_OrgManager } from '@/api/list';
  118. import {
  119. AddBookOfflinePackAuth,
  120. DeleteBookOfflinePackAuth,
  121. DisableBookOfflinePackAuth,
  122. PageQueryBookOfflinePackAuthList,
  123. CreateBookOfflinePackChapterStructFileList,
  124. CreateOfflinePackCoursewareFileList,
  125. } from '@/api/offline';
  126. import { isTrue } from '@/utils/validate';
  127. export default {
  128. name: 'OrgProjectManageOfflinePackAuth',
  129. components: {
  130. ProjectMenu,
  131. PaginationPage,
  132. },
  133. data() {
  134. return {
  135. list: [],
  136. total: 0,
  137. cur_page_begin_index: 0,
  138. bookList: [],
  139. bookTotal: 0,
  140. bookPageIndex: 0,
  141. isTrue,
  142. dialogFormEdit: false,
  143. dialogSearchBook: false,
  144. queryForm: {
  145. book_name: '',
  146. auth_code: '',
  147. memo: '',
  148. page_capacity: 10,
  149. cur_page: 1,
  150. },
  151. editForm: {
  152. book_id: '',
  153. book_name: '',
  154. effective_count: null,
  155. effective_end_date: '',
  156. memo: '',
  157. },
  158. rules: {
  159. book_name: [{ required: true, message: '请选择教材', trigger: 'blur' }],
  160. effective_count: [
  161. { required: true, message: '请填写有效次数', trigger: 'blur' },
  162. { type: 'number', message: '有效次数必须为数字值' },
  163. ],
  164. effective_end_date: [{ required: true, message: '请选择有效截止日期', trigger: 'blur' }],
  165. },
  166. loadingInstance: null, // 加载中实例
  167. tempDir: '', // 临时目录,用于存放下载的文件
  168. savePath: '', // 保存离线包的路径
  169. packageName: 'EEP 离线包', // 离线包名称
  170. file_info_list: [], // 章节结构文件列表
  171. downloadTotal: 0, // 下载总数
  172. downloadCompleted: 0, // 已下载数量
  173. downloadWatcher: null, // 下载进度监听器
  174. };
  175. },
  176. computed: {
  177. downloadProgress() {
  178. if (this.downloadTotal === 0) return 0;
  179. return (this.downloadCompleted / this.downloadTotal) * 100;
  180. },
  181. },
  182. watch: {
  183. dialogFormEdit: {
  184. handler(val) {
  185. if (!val) {
  186. this.$refs.editForm.resetFields();
  187. }
  188. },
  189. deep: true,
  190. },
  191. },
  192. beforeDestroy() {
  193. // 删除临时目录及其内容
  194. if (this.tempDir && this.tempDir.length > 0) {
  195. window.fileAPI.deleteTempDir(this.tempDir);
  196. this.tempDir = '';
  197. }
  198. // 删除 watch 监听器
  199. if (this.downloadWatcher) {
  200. this.downloadWatcher();
  201. this.downloadWatcher = null;
  202. }
  203. // 关闭加载中实例
  204. if (this.loadingInstance) {
  205. this.loadingInstance.close();
  206. this.loadingInstance = null;
  207. }
  208. },
  209. methods: {
  210. queryList() {
  211. this.pageList(this.queryForm);
  212. },
  213. pageList(data) {
  214. Object.assign(this.queryForm, data);
  215. PageQueryBookOfflinePackAuthList(this.queryForm).then(
  216. ({ total_count, offline_pack_auth_list, cur_page_begin_index }) => {
  217. this.list = offline_pack_auth_list;
  218. this.total = total_count;
  219. this.cur_page_begin_index = cur_page_begin_index;
  220. },
  221. );
  222. },
  223. pageBookList(data) {
  224. this.handleSearchBook(data);
  225. },
  226. handleSearchBook(data) {
  227. let page = data || {
  228. cur_page: 1,
  229. page_capacity: 10,
  230. };
  231. PageQueryYSJBookList_OrgManager(page).then(({ total_count, book_list, cur_page_begin_index }) => {
  232. this.bookList = book_list;
  233. this.bookTotal = total_count;
  234. this.bookPageIndex = cur_page_begin_index;
  235. this.dialogSearchBook = true;
  236. });
  237. },
  238. selectBook(book) {
  239. this.editForm.book_name = book.name;
  240. this.editForm.book_id = book.id;
  241. this.dialogSearchBook = false;
  242. },
  243. handleSave(formName) {
  244. this.$refs[formName].validate((valid) => {
  245. if (valid) {
  246. let data = this.editForm;
  247. this.dialogSearchBook = false;
  248. AddBookOfflinePackAuth(data).then((res) => {
  249. if (res && res.status === 1) {
  250. this.dialogFormEdit = false;
  251. this.queryList();
  252. }
  253. });
  254. } else {
  255. return false;
  256. }
  257. });
  258. },
  259. async handleDisable(id) {
  260. this.$confirm('确定要废弃此条数据吗?', '提示', {
  261. confirmButtonText: '确定',
  262. cancelButtonText: '取消',
  263. type: 'warning',
  264. })
  265. .then(() => {
  266. DisableBookOfflinePackAuth({ id }).then(() => {
  267. this.queryList();
  268. });
  269. })
  270. .catch(() => {});
  271. },
  272. async handleDel(id) {
  273. this.$confirm('确定要删除此条数据吗?', '提示', {
  274. confirmButtonText: '确定',
  275. cancelButtonText: '取消',
  276. type: 'warning',
  277. })
  278. .then(() => {
  279. DeleteBookOfflinePackAuth({ id }).then(() => {
  280. this.queryList();
  281. });
  282. })
  283. .catch(() => {});
  284. },
  285. /**
  286. * 选择保存离线包的目录
  287. * @param {String} book_id 教材ID
  288. */
  289. async selectDirectory(book_id) {
  290. const result = await window.fileAPI.openFileDialog({
  291. title: '选择保存离线包的文件夹',
  292. properties: ['openDirectory', 'createDirectory'],
  293. });
  294. if (result.canceled) {
  295. this.$message.warning('未选择文件夹,操作已取消');
  296. return;
  297. }
  298. this.savePath = result.filePaths[0];
  299. this.downloadOfflinePackage(book_id);
  300. },
  301. /**
  302. * 下载离线包
  303. * @param {String} offline_pack_auth_id 离线包授权ID
  304. */
  305. async downloadOfflinePackage(offline_pack_auth_id) {
  306. const { file_info_list, courseware_id_list } = await CreateBookOfflinePackChapterStructFileList({
  307. offline_pack_auth_id,
  308. });
  309. this.file_info_list = file_info_list;
  310. if (file_info_list.length === 0) {
  311. this.$message.error('章节结构文件生成失败,无法下载离线包');
  312. return;
  313. }
  314. if (this.tempDir.length === 0) {
  315. this.tempDir = window.fileAPI.createTempDir(); // 创建临时保存目录
  316. }
  317. this.loadingInstance = this.$loading({
  318. lock: true,
  319. text: '正在下载离线包,请稍候...',
  320. spinner: 'el-icon-loading',
  321. background: 'rgba(255, 255, 255, 0.7)',
  322. });
  323. for (const { dir_name, file_name, file_url } of this.file_info_list) {
  324. const dirPath = dir_name.length > 0 ? `${this.tempDir}\\${dir_name}` : this.tempDir;
  325. await window.fileAPI.downloadFile(file_url, `${dirPath}\\${file_name}`);
  326. }
  327. const struct = await this.readFileContent('struct.json'); // 读取章节结构文件内容
  328. if (struct.node_list && struct.node_list.length > 0) {
  329. this.packageName = struct.node_list[0].name; // 设置离线包名称为课件名称
  330. }
  331. if (courseware_id_list && courseware_id_list.length > 0) {
  332. Promise.all(
  333. courseware_id_list.map((courseware_id) => this.CreateOfflinePackCoursewareFileList(courseware_id)),
  334. ).then(() => {
  335. this.downloadWatcher = this.$watch(
  336. 'downloadProgress',
  337. (newVal) => {
  338. if (newVal >= 100) {
  339. this.downloadWatcher();
  340. this.startCompress();
  341. } else {
  342. // 显示下载进度,预留 10% 的压缩进度
  343. this.loadingInstance.text = `正在下载离线包,进度:${((this.downloadCompleted / (this.downloadTotal + this.downloadTotal * 0.1)) * 100).toFixed(2)}%`;
  344. }
  345. },
  346. { immediate: true },
  347. );
  348. });
  349. }
  350. },
  351. /**
  352. * 下载课件文件列表
  353. * @param {Number} courseware_id 课件ID
  354. * @return {Promise} Promise对象
  355. */
  356. async CreateOfflinePackCoursewareFileList(courseware_id) {
  357. const { file_info_list } = await CreateOfflinePackCoursewareFileList({ courseware_id });
  358. this.downloadTotal += file_info_list.length;
  359. file_info_list.forEach((fileInfo) => {
  360. const { file_name, file_url, dir_name } = fileInfo;
  361. window.fileAPI.downloadFile(file_url, `${this.tempDir}\\${dir_name}\\${file_name}`).then(() => {
  362. this.downloadCompleted += 1;
  363. });
  364. });
  365. },
  366. /**
  367. * 开始压缩
  368. */
  369. async startCompress() {
  370. if (this.downloadWatcher) {
  371. this.downloadWatcher = null;
  372. }
  373. const sources = [`${this.tempDir}\\resource`, `${this.tempDir}\\courseware`]; // 要压缩的文件或文件夹路径数组
  374. this.file_info_list.forEach(({ dir_name, file_name }) => {
  375. const dirPath = dir_name.length > 0 ? `${this.tempDir}\\${dir_name}` : this.tempDir;
  376. sources.push(`${dirPath}\\${file_name}`);
  377. });
  378. const dest = `${this.savePath}\\${this.packageName}.eep`; // 压缩包保存路径
  379. try {
  380. const offErr = window.fileAPI.onStderr((text) => console.warn('7z stderr:', text));
  381. await window.fileAPI.compress({
  382. sources,
  383. dest,
  384. format: 'zip',
  385. level: 5,
  386. password: '1234567a',
  387. recurse: true,
  388. });
  389. offErr();
  390. } catch (e) {
  391. console.error('压缩失败:', e);
  392. this.$message.error('离线包下载失败,请重试!');
  393. this.loadingInstance.close();
  394. this.loadingInstance = null;
  395. this.tempDir = '';
  396. return;
  397. }
  398. this.loadingInstance.text = `正在下载离线包,进度:100%`;
  399. // 删除临时目录及其内容
  400. try {
  401. await window.fileAPI.deleteTempDir(this.tempDir);
  402. this.tempDir = '';
  403. } catch (e) {
  404. console.error('删除临时目录失败:', e);
  405. this.tempDir = '';
  406. }
  407. this.loadingInstance.close();
  408. this.loadingInstance = null;
  409. this.$message.success('离线包下载并保存成功!');
  410. },
  411. /**
  412. * 读取文件内容
  413. * @param entryName 文件条目名称
  414. * @param subdirectory 子目录
  415. */
  416. async readFileContent(entryName, subdirectory = '') {
  417. const content = await window.fileAPI.readZipFileSync(`${this.tempDir}/${subdirectory}`, entryName);
  418. const text = new TextDecoder().decode(content);
  419. const obj = JSON.parse(text);
  420. return obj;
  421. },
  422. },
  423. };
  424. </script>
  425. <style lang="scss" scoped>
  426. @use '@/styles/mixin.scss' as *;
  427. .book {
  428. @include page-base;
  429. @include table-list;
  430. }
  431. .el-divider--horizontal {
  432. margin: 10px 0 !important;
  433. }
  434. .el-form-item__content > * {
  435. width: 100%;
  436. }
  437. </style>