|
|
@@ -136,7 +136,12 @@
|
|
|
<script>
|
|
|
import { getRandomNumber } from '@/utils/index';
|
|
|
import { componentList } from '../../data/bookType';
|
|
|
-import { ContentSaveCoursewareContent, ContentGetCoursewareContent, GetBookUnifiedAttrib } from '@/api/book';
|
|
|
+import {
|
|
|
+ ContentSaveCoursewareContent,
|
|
|
+ ContentGetCoursewareContent,
|
|
|
+ GetBookUnifiedAttrib,
|
|
|
+ BackupCoursewareContent,
|
|
|
+} from '@/api/book';
|
|
|
import _ from 'lodash';
|
|
|
import { unified_attrib } from '@/common/data';
|
|
|
import { waitLayoutStable } from '@/utils/common';
|
|
|
@@ -202,6 +207,7 @@ export default {
|
|
|
curComponentId: '', // 当前选中组件 id
|
|
|
projectResourcePopedom: [], // 当前编辑人员的项目资源权限
|
|
|
componentsDataLoaded: false, // 组件数据是否加载完成
|
|
|
+ savingCourseware: false,
|
|
|
visible_id: this.$route.query.visible_id, // 可见组件 id
|
|
|
canvasHeight: 0, // 画布高度,监听变化用于重绘分组线
|
|
|
};
|
|
|
@@ -645,35 +651,53 @@ export default {
|
|
|
* @param {string} type 类型
|
|
|
*/
|
|
|
async saveCoursewareContent(type) {
|
|
|
- if (!this.componentIsAllLoader()) {
|
|
|
- this.$message.warning('有组件内容未加载完成,请稍后再试');
|
|
|
- return false;
|
|
|
- }
|
|
|
+ if (this.savingCourseware) return false;
|
|
|
+ this.savingCourseware = true;
|
|
|
+ let loading = null;
|
|
|
+ try {
|
|
|
+ await this.$nextTick();
|
|
|
+ const component_id_list = this.data.row_list.flatMap((row) =>
|
|
|
+ row.col_list.flatMap((col) => col.grid_list.map((grid) => grid.id)),
|
|
|
+ );
|
|
|
+ const refs = this.isEdit ? this.$refs.component : this.$refs.previewEdit?.$refs?.preview;
|
|
|
+ const comps = Array.isArray(refs) ? refs : [refs].filter(Boolean);
|
|
|
+ const componentIds = new Set(comps.map((item) => item.id));
|
|
|
+ if (
|
|
|
+ !this.componentsDataLoaded ||
|
|
|
+ !this.componentIsAllLoader() ||
|
|
|
+ comps.length !== component_id_list.length ||
|
|
|
+ component_id_list.some((id) => !componentIds.has(id))
|
|
|
+ ) {
|
|
|
+ if (type !== 'quit' && type !== 'back') {
|
|
|
+ this.$message.warning('有组件内容未加载完成,请稍后再试');
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ throw new Error('有组件内容未加载完成');
|
|
|
+ }
|
|
|
|
|
|
- const loading = this.$loading({
|
|
|
- lock: true,
|
|
|
- text: '保存中...',
|
|
|
- spinner: 'el-icon-loading',
|
|
|
- background: 'rgba(0, 0, 0, 0.7)',
|
|
|
- });
|
|
|
+ loading = this.$loading({
|
|
|
+ lock: true,
|
|
|
+ text: '保存中...',
|
|
|
+ spinner: 'el-icon-loading',
|
|
|
+ background: 'rgba(0, 0, 0, 0.7)',
|
|
|
+ });
|
|
|
+
|
|
|
+ // 保存前先通知后端备份一下当前内容,备份好了前台再开始保存
|
|
|
+ await BackupCoursewareContent({ id: this.courseware_id });
|
|
|
|
|
|
- try {
|
|
|
// 先等待所有子组件内容保存完成
|
|
|
if (this.isEdit) {
|
|
|
- const comps = Array.isArray(this.$refs?.component)
|
|
|
- ? this.$refs.component
|
|
|
- : [this.$refs?.component].filter(Boolean);
|
|
|
- await Promise.all(comps.map((item) => item.saveCoursewareComponentContent()));
|
|
|
+ const results = await Promise.allSettled(
|
|
|
+ comps.map((item) => Promise.resolve().then(() => item.saveCoursewareComponentContent())),
|
|
|
+ );
|
|
|
+ const failed = results.find((result) => result.status === 'rejected' || result.value?.status !== 1);
|
|
|
+ if (failed) {
|
|
|
+ throw failed.reason || new Error('组件内容保存失败');
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- // 组件内容已按组件自身 id 保存到当前课件,清理仅用于从模板课件取内容的临时标记 oldId,
|
|
|
- // 否则该课件之后再次被当作模板使用时组件会按陈旧的 oldId 请求而取到空内容
|
|
|
- this.clearGridOldId();
|
|
|
-
|
|
|
- // 再收集并保存页面结构
|
|
|
- let component_id_list = this.data.row_list.flatMap((row) =>
|
|
|
- row.col_list.flatMap((col) => col.grid_list.map((grid) => grid.id)),
|
|
|
- );
|
|
|
+ const savedData = _.cloneDeep(this.data);
|
|
|
+ this.clearGridOldId(savedData.row_list);
|
|
|
|
|
|
let groupIdList = _.cloneDeep(this.content_group_row_list);
|
|
|
let groupList = [];
|
|
|
@@ -732,15 +756,19 @@ export default {
|
|
|
});
|
|
|
});
|
|
|
|
|
|
- await ContentSaveCoursewareContent({
|
|
|
+ const result = await ContentSaveCoursewareContent({
|
|
|
id: this.courseware_id,
|
|
|
category: 'NEW',
|
|
|
- content: JSON.stringify(this.data),
|
|
|
+ content: JSON.stringify(savedData),
|
|
|
component_id_list,
|
|
|
content_group_component_list: groupList,
|
|
|
content_group_row_list: this.content_group_row_list,
|
|
|
});
|
|
|
+ if (result?.status !== 1) {
|
|
|
+ throw new Error('课件框架保存失败');
|
|
|
+ }
|
|
|
|
|
|
+ this.clearGridOldId();
|
|
|
this.$message.success('保存成功');
|
|
|
if (type === 'quit') {
|
|
|
this.$emit('back', this.getFirstVisibleComponentId());
|
|
|
@@ -753,8 +781,16 @@ export default {
|
|
|
path: '/personal_workbench/edit_task',
|
|
|
});
|
|
|
}
|
|
|
+ return true;
|
|
|
+ } catch (error) {
|
|
|
+ console.error('保存课件内容失败', error);
|
|
|
+ this.$message.error(
|
|
|
+ type === 'quit' || type === 'back' ? '有数据未保存成功,暂不退出编辑页面' : '课件内容保存失败,请重试',
|
|
|
+ );
|
|
|
+ return false;
|
|
|
} finally {
|
|
|
- loading.close();
|
|
|
+ if (loading) loading.close();
|
|
|
+ this.savingCourseware = false;
|
|
|
}
|
|
|
},
|
|
|
/**
|