|
|
|
@ -57,10 +57,10 @@ |
|
|
|
|
<NFormItem label="状态" path="status"> |
|
|
|
|
<NSwitch |
|
|
|
|
v-model:value="modalForm.status" |
|
|
|
|
:checked-value="'1'" |
|
|
|
|
:unchecked-value="'0'" |
|
|
|
|
:checked-value="'0'" |
|
|
|
|
:unchecked-value="'1'" |
|
|
|
|
/> |
|
|
|
|
<span class="ml-2">{{ modalForm.status === '1' ? '启用' : '停用' }}</span> |
|
|
|
|
<span class="ml-2">{{ modalForm.status === '0' ? '启用' : '停用' }}</span> |
|
|
|
|
</NFormItem> |
|
|
|
|
<NFormItem |
|
|
|
|
label="问题内容" |
|
|
|
@ -86,7 +86,8 @@ |
|
|
|
|
</template> |
|
|
|
|
|
|
|
|
|
<script setup> |
|
|
|
|
import { h, onMounted, ref, resolveDirective, withDirectives, onUnmounted } from 'vue' |
|
|
|
|
// 修复1:添加watch的导入 |
|
|
|
|
import { h, onMounted, ref, resolveDirective, withDirectives, onUnmounted, watch } from 'vue' |
|
|
|
|
import { |
|
|
|
|
NButton, NForm, NFormItem, NInput, NPopconfirm, NTag, NSwitch |
|
|
|
|
} from 'naive-ui' |
|
|
|
@ -112,7 +113,7 @@ const editorConfig = ref({ |
|
|
|
|
placeholder: '请输入问题内容...', |
|
|
|
|
MENU_CONF: { |
|
|
|
|
uploadImage: { |
|
|
|
|
server: '/api/upload/image', // 直接使用新的接口路径 |
|
|
|
|
server: '/api/upload/image', |
|
|
|
|
fieldName: 'file', |
|
|
|
|
maxFileSize: 2 * 1024 * 1024, // 2MB |
|
|
|
|
headers: { |
|
|
|
@ -120,7 +121,7 @@ const editorConfig = ref({ |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
uploadVideo: { |
|
|
|
|
server: '/api/upload/video', // 直接使用新的接口路径 |
|
|
|
|
server: '/api/upload/video', |
|
|
|
|
fieldName: 'file', |
|
|
|
|
maxFileSize: 100 * 1024 * 1024, // 100MB |
|
|
|
|
headers: { |
|
|
|
@ -130,18 +131,31 @@ const editorConfig = ref({ |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
// 添加一个变量来跟踪编辑器是否已创建 |
|
|
|
|
const editorCreated = ref(false) |
|
|
|
|
|
|
|
|
|
const handleEditorCreated = (editor) => { |
|
|
|
|
editorRef.value = editor // 记录editor实例,重要! |
|
|
|
|
editorRef.value = editor |
|
|
|
|
editorCreated.value = true // 标记编辑器已创建 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 组件销毁时,也及时销毁编辑器 |
|
|
|
|
// 修改组件销毁时的处理 |
|
|
|
|
onUnmounted(() => { |
|
|
|
|
const editor = editorRef.value |
|
|
|
|
if (editor) { |
|
|
|
|
editor.destroy() |
|
|
|
|
if (editorRef.value) { |
|
|
|
|
editorRef.value.destroy() |
|
|
|
|
editorRef.value = null |
|
|
|
|
editorCreated.value = false |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
// 添加一个方法来安全地获取编辑器实例 |
|
|
|
|
const getSafeEditor = () => { |
|
|
|
|
if (editorCreated.value && editorRef.value) { |
|
|
|
|
return editorRef.value |
|
|
|
|
} |
|
|
|
|
return null |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const $table = ref(null) |
|
|
|
|
const queryItems = ref({}) |
|
|
|
|
const vPermission = resolveDirective('permission') |
|
|
|
@ -162,7 +176,7 @@ const { |
|
|
|
|
initForm: { |
|
|
|
|
title: '', |
|
|
|
|
order_num: 0, |
|
|
|
|
status: '0', |
|
|
|
|
status: '0', // 默认启用 |
|
|
|
|
content: '' |
|
|
|
|
}, |
|
|
|
|
doCreate: api.createQuestion, |
|
|
|
@ -171,6 +185,50 @@ const { |
|
|
|
|
refresh: () => $table.value?.handleSearch(), |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
// 修复2:调整变量引用顺序,在使用前定义 |
|
|
|
|
// 在handleSave方法执行前添加 |
|
|
|
|
const originalHandleSave = handleSave |
|
|
|
|
|
|
|
|
|
// 修复3:删除重复定义,只保留一个customHandleSave函数 |
|
|
|
|
const customHandleSave = async () => { |
|
|
|
|
if (modalFormRef.value) { |
|
|
|
|
try { |
|
|
|
|
// 保存前手动获取编辑器内容 |
|
|
|
|
const editor = getSafeEditor() |
|
|
|
|
if (editor) { |
|
|
|
|
modalForm.value.content = editor.getHtml() |
|
|
|
|
} |
|
|
|
|
await modalFormRef.value.validate() |
|
|
|
|
originalHandleSave() |
|
|
|
|
} catch (error) { |
|
|
|
|
console.error('验证失败:', error) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 修复4:删除重复定义,只保留一个handleEdit函数 |
|
|
|
|
const handleEdit = async (row) => { |
|
|
|
|
originalHandleEdit(row); |
|
|
|
|
try { |
|
|
|
|
modalLoading.value = true; |
|
|
|
|
const res = await api.getQuestion({ question_id: row.id }); |
|
|
|
|
if (res && res.data) { |
|
|
|
|
modalForm.value = { ...res.data }; |
|
|
|
|
// 延迟设置编辑器内容,确保编辑器已初始化 |
|
|
|
|
setTimeout(() => { |
|
|
|
|
const editor = getSafeEditor() |
|
|
|
|
if (editor) { |
|
|
|
|
editor.setHtml(res.data.content || '') |
|
|
|
|
} |
|
|
|
|
}, 300) |
|
|
|
|
} |
|
|
|
|
} catch (error) { |
|
|
|
|
console.error('获取问题详情失败:', error); |
|
|
|
|
} finally { |
|
|
|
|
modalLoading.value = false; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
onMounted(() => { |
|
|
|
|
$table.value?.handleSearch() |
|
|
|
|
}) |
|
|
|
@ -270,26 +328,6 @@ const columns = [ |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
] |
|
|
|
|
// 在handleSave方法执行前添加 |
|
|
|
|
const originalHandleSave = handleSave |
|
|
|
|
|
|
|
|
|
// 覆盖handleSave方法 |
|
|
|
|
const customHandleSave = async () => { |
|
|
|
|
if (modalFormRef.value) { |
|
|
|
|
try { |
|
|
|
|
// 保存前手动获取编辑器内容 |
|
|
|
|
if (editorRef.value) { |
|
|
|
|
modalForm.value.content = editorRef.value.getHtml() |
|
|
|
|
} |
|
|
|
|
await modalFormRef.value.validate() |
|
|
|
|
originalHandleSave() |
|
|
|
|
} catch (error) { |
|
|
|
|
// 验证失败,不执行保存 |
|
|
|
|
console.error('验证失败:', error) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 修改watch监听,确保在富文本内容变化时触发表单验证 |
|
|
|
|
watch( |
|
|
|
|
() => modalForm.content, |
|
|
|
@ -305,28 +343,6 @@ watch( |
|
|
|
|
// 添加immediate选项,确保初始值也能触发验证 |
|
|
|
|
{ immediate: true } |
|
|
|
|
) |
|
|
|
|
// 覆盖handleEdit方法,添加API调用逻辑 |
|
|
|
|
const handleEdit = async (row) => { |
|
|
|
|
originalHandleEdit(row); |
|
|
|
|
try { |
|
|
|
|
modalLoading.value = true; |
|
|
|
|
// 调用getQuestion API获取完整详情 |
|
|
|
|
const res = await api.getQuestion({ question_id: row.id }); |
|
|
|
|
if (res && res.data) { |
|
|
|
|
// 更新modalForm的值 |
|
|
|
|
modalForm.value = { ...res.data }; |
|
|
|
|
// 手动设置富文本编辑器内容 |
|
|
|
|
if (editorRef.value) { |
|
|
|
|
// 确保编辑器已经初始化 |
|
|
|
|
editorRef.value.setHtml(res.data.content || ''); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} catch (error) { |
|
|
|
|
console.error('获取问题详情失败:', error); |
|
|
|
|
} finally { |
|
|
|
|
modalLoading.value = false; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
</script> |
|
|
|
|
|
|
|
|
|
<style scoped> |
|
|
|
|