|
|
@@ -1,184 +1,188 @@
|
|
1
|
|
-<script lang="ts" setup>
|
|
2
|
|
-import { ref } from 'vue';
|
|
3
|
|
-
|
|
4
|
|
-import { useVbenDrawer, useVbenForm } from '@vben/common-ui';
|
|
5
|
|
-
|
|
6
|
|
-import { Plus } from '@element-plus/icons-vue';
|
|
7
|
|
-import { ElIcon, ElUpload } from 'element-plus';
|
|
8
|
|
-
|
|
9
|
|
-import TinymceEditor from '#/components/tinymce/src/editor.vue';
|
|
10
|
|
-
|
|
11
|
|
-import { drawerFormSchema } from './config-data';
|
|
12
|
|
-
|
|
13
|
|
-const emit = defineEmits<{
|
|
14
|
|
- reload: [];
|
|
15
|
|
-}>();
|
|
16
|
|
-
|
|
17
|
|
-// 附件列表
|
|
18
|
|
-const attachments = ref<any[]>([]);
|
|
19
|
|
-const uploadUrlRef = ref<string>('');
|
|
20
|
|
-
|
|
21
|
|
-// 表单配置
|
|
22
|
|
-const [Form, formApi] = useVbenForm({
|
|
23
|
|
- showDefaultActions: false,
|
|
24
|
|
- schema: drawerFormSchema(),
|
|
25
|
|
-});
|
|
26
|
|
-
|
|
27
|
|
-// 处理附件上传
|
|
28
|
|
-function handleAttachmentUpload(options: any) {
|
|
29
|
|
- // 模拟上传成功处理
|
|
30
|
|
- const fileData = {
|
|
31
|
|
- uid: options.file.uid,
|
|
32
|
|
- name: options.file.name,
|
|
33
|
|
- url: URL.createObjectURL(options.file),
|
|
34
|
|
- status: 'success',
|
|
35
|
|
- raw: options.file,
|
|
36
|
|
- };
|
|
37
|
|
-
|
|
38
|
|
- // 多个文件上传时,url以逗号隔开
|
|
39
|
|
- uploadUrlRef.value = uploadUrlRef.value
|
|
40
|
|
- ? `${uploadUrlRef.value},${fileData.url}`
|
|
41
|
|
- : fileData.url;
|
|
42
|
|
-
|
|
43
|
|
- options.onSuccess(fileData);
|
|
44
|
|
-}
|
|
45
|
|
-
|
|
46
|
|
-// 移除附件
|
|
47
|
|
-function handleAttachmentRemove(file: any, fileList: any[]) {
|
|
48
|
|
- attachments.value = fileList;
|
|
49
|
|
- // 更新上传url,移除已删除的文件
|
|
50
|
|
- uploadUrlRef.value = fileList
|
|
51
|
|
- .filter((item: any) => item.status === 'success')
|
|
52
|
|
- .map((item: any) => item.url)
|
|
53
|
|
- .join(',');
|
|
54
|
|
-}
|
|
55
|
|
-
|
|
56
|
|
-// 预览附件
|
|
57
|
|
-function handleAttachmentPreview(file: any) {
|
|
58
|
|
- console.log('预览附件', file);
|
|
59
|
|
- // TODO: 实现附件预览功能
|
|
60
|
|
-}
|
|
61
|
|
-
|
|
62
|
|
-// 确认编辑
|
|
63
|
|
-async function handleConfirm() {
|
|
64
|
|
- try {
|
|
65
|
|
- const { valid } = await formApi.validate();
|
|
66
|
|
- if (!valid) {
|
|
67
|
|
- return;
|
|
68
|
|
- }
|
|
69
|
|
- const data = await formApi.getValues();
|
|
70
|
|
-
|
|
71
|
|
- // 处理附件url,转换为逗号分隔的字符串
|
|
72
|
|
- data.attachments = uploadUrlRef.value;
|
|
73
|
|
-
|
|
74
|
|
- // 模拟提交数据
|
|
75
|
|
- console.log('提交数据:', data);
|
|
76
|
|
- // TODO: 实现真实的API调用
|
|
77
|
|
-
|
|
78
|
|
- // 提交成功后触发reload事件
|
|
79
|
|
- emit('reload');
|
|
80
|
|
- drawerApi.close();
|
|
81
|
|
- } catch (error) {
|
|
82
|
|
- console.error('保存失败:', error);
|
|
83
|
|
- }
|
|
84
|
|
-}
|
|
85
|
|
-
|
|
86
|
|
-const isUpdateRef = ref<boolean>(false);
|
|
87
|
|
-
|
|
88
|
|
-const [Drawer, drawerApi] = useVbenDrawer({
|
|
89
|
|
- async onOpenChange(isOpen) {
|
|
90
|
|
- if (!isOpen) {
|
|
91
|
|
- return;
|
|
92
|
|
- }
|
|
93
|
|
- try {
|
|
94
|
|
- drawerApi.drawerLoading(true);
|
|
95
|
|
- const { chapter, isUpdate } = drawerApi.getData();
|
|
96
|
|
- isUpdateRef.value = isUpdate;
|
|
97
|
|
- if (isUpdate && chapter) {
|
|
98
|
|
- // 处理附件url,转换为文件列表格式
|
|
99
|
|
- const attachmentsList = chapter.attachments
|
|
100
|
|
- ? chapter.attachments
|
|
101
|
|
- .split(',')
|
|
102
|
|
- .map((url: string, index: number) => ({
|
|
103
|
|
- uid: Date.now() + index,
|
|
104
|
|
- name: url.split('/').pop() || `文件${index + 1}`,
|
|
105
|
|
- url,
|
|
106
|
|
- status: 'success',
|
|
107
|
|
- }))
|
|
108
|
|
- : [];
|
|
109
|
|
-
|
|
110
|
|
- uploadUrlRef.value = chapter.attachments || '';
|
|
111
|
|
- attachments.value = attachmentsList;
|
|
112
|
|
-
|
|
113
|
|
- // 设置表单数据
|
|
114
|
|
- await formApi.setValues({
|
|
115
|
|
- ...chapter,
|
|
116
|
|
- content: chapter.content || '',
|
|
117
|
|
- attachments: attachmentsList,
|
|
118
|
|
- });
|
|
119
|
|
- } else {
|
|
120
|
|
- // 新增时重置表单
|
|
121
|
|
- await formApi.resetForm();
|
|
122
|
|
- attachments.value = [];
|
|
123
|
|
- uploadUrlRef.value = '';
|
|
124
|
|
- }
|
|
125
|
|
- } catch (error) {
|
|
126
|
|
- console.error('加载数据失败:', error);
|
|
127
|
|
- } finally {
|
|
128
|
|
- drawerApi.drawerLoading(false);
|
|
129
|
|
- }
|
|
130
|
|
- },
|
|
131
|
|
- async onConfirm() {
|
|
132
|
|
- await handleConfirm();
|
|
133
|
|
- },
|
|
134
|
|
- onClosed() {
|
|
135
|
|
- formApi.resetForm();
|
|
136
|
|
- attachments.value = [];
|
|
137
|
|
- uploadUrlRef.value = '';
|
|
138
|
|
- },
|
|
139
|
|
-});
|
|
140
|
|
-</script>
|
|
141
|
|
-
|
|
142
|
|
-<template>
|
|
143
|
|
- <Drawer :title="isUpdateRef ? '编辑章节' : '新增章节'">
|
|
144
|
|
- <Form>
|
|
145
|
|
- <!-- 自定义Tinymce编辑器插槽 -->
|
|
146
|
|
- <template #content="scope">
|
|
147
|
|
- <TinymceEditor v-model="scope.modelValue" :height="400" />
|
|
148
|
|
- </template>
|
|
149
|
|
-
|
|
150
|
|
- <!-- 自定义附件上传插槽 -->
|
|
151
|
|
- <template #attachments="scope">
|
|
152
|
|
- <ElUpload
|
|
153
|
|
- v-model:file-list="attachments"
|
|
154
|
|
- action="#"
|
|
155
|
|
- accept=".jpg,.jpeg,.png,.gif,.pdf,.doc,.docx,.xls,.xlsx,.ppt,.pptx"
|
|
156
|
|
- list-type="text"
|
|
157
|
|
- show-file-list
|
|
158
|
|
- :auto-upload="true"
|
|
159
|
|
- :drag="false"
|
|
160
|
|
- :limit="5"
|
|
161
|
|
- :multiple="true"
|
|
162
|
|
- :http-request="handleAttachmentUpload"
|
|
163
|
|
- :on-remove="handleAttachmentRemove"
|
|
164
|
|
- :on-preview="handleAttachmentPreview"
|
|
165
|
|
- class="mb-4"
|
|
166
|
|
- >
|
|
167
|
|
- <div class="flex items-center justify-center">
|
|
168
|
|
- <ElIcon class="el-upload__icon">
|
|
169
|
|
- <Plus size="18" />
|
|
170
|
|
- </ElIcon>
|
|
171
|
|
- <div class="el-upload__text">
|
|
172
|
|
- <div>上传附件</div>
|
|
173
|
|
- <div class="text-xs text-gray-500">最多上传5个附件</div>
|
|
174
|
|
- </div>
|
|
175
|
|
- </div>
|
|
176
|
|
- </ElUpload>
|
|
177
|
|
- </template>
|
|
178
|
|
- </Form>
|
|
179
|
|
- </Drawer>
|
|
180
|
|
-</template>
|
|
181
|
|
-
|
|
182
|
|
-<style scoped lang="scss">
|
|
183
|
|
-/* 自定义样式 */
|
|
184
|
|
-</style>
|
|
|
1
|
+<script lang="ts" setup>
|
|
|
2
|
+import { ref } from 'vue';
|
|
|
3
|
+
|
|
|
4
|
+import { useVbenDrawer, useVbenForm } from '@vben/common-ui';
|
|
|
5
|
+
|
|
|
6
|
+import { Plus } from '@element-plus/icons-vue';
|
|
|
7
|
+import { ElIcon, ElUpload } from 'element-plus';
|
|
|
8
|
+
|
|
|
9
|
+import TinymceEditor from '#/components/tinymce/src/editor.vue';
|
|
|
10
|
+
|
|
|
11
|
+import { drawerFormSchema } from './config-data';
|
|
|
12
|
+
|
|
|
13
|
+const emit = defineEmits<{
|
|
|
14
|
+ reload: [];
|
|
|
15
|
+}>();
|
|
|
16
|
+
|
|
|
17
|
+// 附件列表
|
|
|
18
|
+const attachments = ref<any[]>([]);
|
|
|
19
|
+const uploadUrlRef = ref<string>('');
|
|
|
20
|
+
|
|
|
21
|
+// 表单配置
|
|
|
22
|
+const [Form, formApi] = useVbenForm({
|
|
|
23
|
+ showDefaultActions: false,
|
|
|
24
|
+ schema: drawerFormSchema(),
|
|
|
25
|
+});
|
|
|
26
|
+
|
|
|
27
|
+// 处理附件上传
|
|
|
28
|
+function handleAttachmentUpload(options: any): Promise<unknown> {
|
|
|
29
|
+ return new Promise((resolve) => {
|
|
|
30
|
+ // 模拟上传成功处理
|
|
|
31
|
+ const fileData = {
|
|
|
32
|
+ uid: options.file.uid,
|
|
|
33
|
+ name: options.file.name,
|
|
|
34
|
+ url: URL.createObjectURL(options.file),
|
|
|
35
|
+ status: 'success',
|
|
|
36
|
+ raw: options.file,
|
|
|
37
|
+ };
|
|
|
38
|
+
|
|
|
39
|
+ // 多个文件上传时,url以逗号隔开
|
|
|
40
|
+ uploadUrlRef.value = uploadUrlRef.value
|
|
|
41
|
+ ? `${uploadUrlRef.value},${fileData.url}`
|
|
|
42
|
+ : fileData.url;
|
|
|
43
|
+
|
|
|
44
|
+ options.onSuccess(fileData);
|
|
|
45
|
+ resolve(fileData);
|
|
|
46
|
+ });
|
|
|
47
|
+}
|
|
|
48
|
+
|
|
|
49
|
+// 移除附件
|
|
|
50
|
+function handleAttachmentRemove(file: any, fileList: any[]) {
|
|
|
51
|
+ attachments.value = fileList;
|
|
|
52
|
+ // 更新上传url,移除已删除的文件
|
|
|
53
|
+ uploadUrlRef.value = fileList
|
|
|
54
|
+ .filter((item: any) => item.status === 'success')
|
|
|
55
|
+ .map((item: any) => item.url)
|
|
|
56
|
+ .join(',');
|
|
|
57
|
+}
|
|
|
58
|
+
|
|
|
59
|
+// 预览附件
|
|
|
60
|
+function handleAttachmentPreview(file: any) {
|
|
|
61
|
+ console.log('预览附件', file);
|
|
|
62
|
+ // TODO: 实现附件预览功能
|
|
|
63
|
+}
|
|
|
64
|
+
|
|
|
65
|
+// 确认编辑
|
|
|
66
|
+async function handleConfirm() {
|
|
|
67
|
+ try {
|
|
|
68
|
+ const { valid } = await formApi.validate();
|
|
|
69
|
+ if (!valid) {
|
|
|
70
|
+ return;
|
|
|
71
|
+ }
|
|
|
72
|
+ const data = await formApi.getValues();
|
|
|
73
|
+
|
|
|
74
|
+ // 处理附件url,转换为逗号分隔的字符串
|
|
|
75
|
+ data.attachments = uploadUrlRef.value;
|
|
|
76
|
+
|
|
|
77
|
+ // 模拟提交数据
|
|
|
78
|
+ console.log('提交数据:', data);
|
|
|
79
|
+ // TODO: 实现真实的API调用
|
|
|
80
|
+
|
|
|
81
|
+ // 提交成功后触发reload事件
|
|
|
82
|
+ emit('reload');
|
|
|
83
|
+ drawerApi.close();
|
|
|
84
|
+ } catch (error) {
|
|
|
85
|
+ console.error('保存失败:', error);
|
|
|
86
|
+ }
|
|
|
87
|
+}
|
|
|
88
|
+
|
|
|
89
|
+const isUpdateRef = ref<boolean>(false);
|
|
|
90
|
+
|
|
|
91
|
+const [Drawer, drawerApi] = useVbenDrawer({
|
|
|
92
|
+ async onOpenChange(isOpen) {
|
|
|
93
|
+ if (!isOpen) {
|
|
|
94
|
+ return;
|
|
|
95
|
+ }
|
|
|
96
|
+ try {
|
|
|
97
|
+ drawerApi.drawerLoading(true);
|
|
|
98
|
+ const { chapter, isUpdate } = drawerApi.getData();
|
|
|
99
|
+ isUpdateRef.value = isUpdate;
|
|
|
100
|
+ if (isUpdate && chapter) {
|
|
|
101
|
+ // 处理附件url,转换为文件列表格式
|
|
|
102
|
+ const attachmentsList = chapter.attachments
|
|
|
103
|
+ ? chapter.attachments
|
|
|
104
|
+ .split(',')
|
|
|
105
|
+ .map((url: string, index: number) => ({
|
|
|
106
|
+ uid: Date.now() + index,
|
|
|
107
|
+ name: url.split('/').pop() || `文件${index + 1}`,
|
|
|
108
|
+ url,
|
|
|
109
|
+ status: 'success',
|
|
|
110
|
+ }))
|
|
|
111
|
+ : [];
|
|
|
112
|
+
|
|
|
113
|
+ uploadUrlRef.value = chapter.attachments || '';
|
|
|
114
|
+ attachments.value = attachmentsList;
|
|
|
115
|
+
|
|
|
116
|
+ // 设置表单数据
|
|
|
117
|
+ await formApi.setValues({
|
|
|
118
|
+ ...chapter,
|
|
|
119
|
+ content: chapter.content || '',
|
|
|
120
|
+ attachments: attachmentsList,
|
|
|
121
|
+ });
|
|
|
122
|
+ } else {
|
|
|
123
|
+ // 新增时重置表单
|
|
|
124
|
+ await formApi.resetForm();
|
|
|
125
|
+ attachments.value = [];
|
|
|
126
|
+ uploadUrlRef.value = '';
|
|
|
127
|
+ }
|
|
|
128
|
+ } catch (error) {
|
|
|
129
|
+ console.error('加载数据失败:', error);
|
|
|
130
|
+ } finally {
|
|
|
131
|
+ drawerApi.drawerLoading(false);
|
|
|
132
|
+ }
|
|
|
133
|
+ },
|
|
|
134
|
+ async onConfirm() {
|
|
|
135
|
+ await handleConfirm();
|
|
|
136
|
+ },
|
|
|
137
|
+ onClosed() {
|
|
|
138
|
+ formApi.resetForm();
|
|
|
139
|
+ attachments.value = [];
|
|
|
140
|
+ uploadUrlRef.value = '';
|
|
|
141
|
+ },
|
|
|
142
|
+});
|
|
|
143
|
+</script>
|
|
|
144
|
+
|
|
|
145
|
+<template>
|
|
|
146
|
+ <Drawer :title="isUpdateRef ? '编辑章节' : '新增章节'">
|
|
|
147
|
+ <Form>
|
|
|
148
|
+ <!-- 自定义Tinymce编辑器插槽 -->
|
|
|
149
|
+ <template #content="scope">
|
|
|
150
|
+ <TinymceEditor v-model="scope.modelValue" :height="400" />
|
|
|
151
|
+ </template>
|
|
|
152
|
+
|
|
|
153
|
+ <!-- 自定义附件上传插槽 -->
|
|
|
154
|
+ <template #attachments="scope">
|
|
|
155
|
+ <ElUpload
|
|
|
156
|
+ v-model:file-list="attachments"
|
|
|
157
|
+ action="#"
|
|
|
158
|
+ accept=".jpg,.jpeg,.png,.gif,.pdf,.doc,.docx,.xls,.xlsx,.ppt,.pptx"
|
|
|
159
|
+ list-type="text"
|
|
|
160
|
+ show-file-list
|
|
|
161
|
+ :auto-upload="true"
|
|
|
162
|
+ :drag="false"
|
|
|
163
|
+ :limit="5"
|
|
|
164
|
+ :multiple="true"
|
|
|
165
|
+ :http-request="handleAttachmentUpload"
|
|
|
166
|
+ :on-remove="handleAttachmentRemove"
|
|
|
167
|
+ :on-preview="handleAttachmentPreview"
|
|
|
168
|
+ class="mb-4"
|
|
|
169
|
+ >
|
|
|
170
|
+ <div class="flex items-center justify-center">
|
|
|
171
|
+ <ElIcon class="el-upload__icon">
|
|
|
172
|
+ <Plus size="18" />
|
|
|
173
|
+ </ElIcon>
|
|
|
174
|
+ <div class="el-upload__text">
|
|
|
175
|
+ <div>上传附件</div>
|
|
|
176
|
+ <div class="text-xs text-gray-500">最多上传5个附件</div>
|
|
|
177
|
+ </div>
|
|
|
178
|
+ </div>
|
|
|
179
|
+ </ElUpload>
|
|
|
180
|
+ </template>
|
|
|
181
|
+ </Form>
|
|
|
182
|
+ </Drawer>
|
|
|
183
|
+</template>
|
|
|
184
|
+
|
|
|
185
|
+<style scoped lang="scss">
|
|
|
186
|
+/* 自定义样式 */
|
|
|
187
|
+</style>
|
|
|
188
|
+
|