fix: 自测bug修复。

This commit is contained in:
tianyongbao
2026-02-04 23:38:49 +08:00
parent 30b3475ed7
commit 32841eebc3

View File

@@ -69,8 +69,6 @@
</u--form>
<view class="form-btn">
<u-button type="primary" text="保存" @click="submitForm"></u-button>
<!-- 调试用的测试按钮 -->
<u-button type="warning" text="测试保存" @click="testSave" style="margin-top: 20rpx;"></u-button>
</view>
</view>
</view>
@@ -101,41 +99,27 @@ const form = reactive({
})
onLoad((options) => {
console.log('页面加载参数:', options)
if (options.id) {
noticeId.value = options.id
title.value = '修改公告'
console.log('加载公告详情ID:', noticeId.value)
getNotice(noticeId.value).then(response => {
console.log('获取到的公告数据:', response.data)
Object.assign(form, response.data)
console.log('表单数据初始化完成:', form)
// 如果编辑器已经初始化,设置内容
if (editorCtx.value && form.noticeContent) {
console.log('设置编辑器初始内容:', form.noticeContent)
editorCtx.value.setContents({
html: form.noticeContent
})
}
}).catch(error => {
console.error('获取公告详情失败:', error)
})
}
// 获取字典数据
console.log('开始获取字典数据')
getDicts('sys_notice_type').then(res => {
noticeTypeList.value = res.data
console.log('公告类型字典:', res.data)
}).catch(error => {
console.error('获取公告类型字典失败:', error)
})
getDicts('sys_notice_status').then(res => {
statusList.value = res.data
console.log('公告状态字典:', res.data)
}).catch(error => {
console.error('获取公告状态字典失败:', error)
})
})
@@ -145,24 +129,13 @@ onReady(() => {
// 编辑器初始化完成
function onEditorReady() {
console.log('编辑器初始化完成')
uni.createSelectorQuery().select('#editor').context((res) => {
editorCtx.value = res.context
console.log('编辑器上下文获取成功:', editorCtx.value)
// 如果是编辑模式,设置初始内容
if (form.noticeContent) {
console.log('设置初始内容到编辑器:', form.noticeContent)
editorCtx.value.setContents({
html: form.noticeContent,
success: () => {
console.log('编辑器内容设置成功')
},
fail: (error) => {
console.error('编辑器内容设置失败:', error)
}
html: form.noticeContent
})
} else {
console.log('没有初始内容需要设置')
}
}).exec()
}
@@ -177,7 +150,6 @@ function onEditorInput(e) {
// 计算内容长度去除HTML标签
const text = res.text || ''
contentLength.value = text.length
console.log('编辑器内容已更新:', res.html)
}
})
}
@@ -194,10 +166,6 @@ function editorBlur() {
editorCtx.value.getContents({
success: (res) => {
form.noticeContent = res.html
console.log('失去焦点时保存的内容:', res.html)
},
fail: (error) => {
console.error('失去焦点时获取内容失败:', error)
}
})
}
@@ -262,8 +230,7 @@ function insertImage() {
icon: 'success'
})
},
fail: (err) => {
console.error('插入图片失败', err)
fail: () => {
uni.showToast({
title: '插入失败',
icon: 'none'
@@ -278,16 +245,14 @@ function insertImage() {
})
}
} catch (e) {
console.error('解析上传结果失败', e)
uni.showToast({
title: '上传失败',
icon: 'none'
})
}
},
fail: (err) => {
fail: () => {
uni.hideLoading()
console.error('上传失败', err)
uni.showToast({
title: '上传失败',
icon: 'none'
@@ -295,9 +260,7 @@ function insertImage() {
}
})
},
fail: (err) => {
console.error('选择图片失败', err)
}
fail: () => {}
})
}
@@ -306,11 +269,10 @@ async function submitForm() {
try {
// 提交前确保获取最新的编辑器内容
if (editorCtx.value) {
const content = await new Promise((resolve, reject) => {
await new Promise((resolve, reject) => {
editorCtx.value.getContents({
success: (res) => {
form.noticeContent = res.html
console.log('提交前获取到的内容:', res.html)
resolve(res.html)
},
fail: reject
@@ -323,14 +285,11 @@ async function submitForm() {
doSubmit()
} catch (error) {
console.error('获取编辑器内容失败:', error)
modal.msgError('获取内容失败,请重试')
}
}
function doSubmit() {
console.log('准备提交的表单数据:', form)
if (!form.noticeTitle) {
modal.msgError('请输入公告标题')
return
@@ -345,98 +304,27 @@ function doSubmit() {
}
const submitForm = { ...form }
console.log('最终提交的数据:', submitForm)
// 检查必要字段
console.log('检查字段:')
console.log('- 标题:', submitForm.noticeTitle)
console.log('- 类型:', submitForm.noticeType)
console.log('- 内容长度:', submitForm.noticeContent.length)
console.log('- 状态:', submitForm.status)
console.log('- ID:', submitForm.noticeId)
if (noticeId.value) {
console.log('执行修改操作')
updateNotice(submitForm).then(response => {
console.log('修改成功响应:', response)
updateNotice(submitForm).then(() => {
modal.msgSuccess('修改成功')
setTimeout(() => {
uni.navigateBack()
}, 1000)
}).catch(error => {
console.error('修改失败:', error)
console.log('错误详情:', {
message: error.message,
response: error.response,
data: error.response?.data
})
modal.msgError('修改失败: ' + (error.message || '未知错误'))
})
} else {
console.log('执行新增操作')
addNotice(submitForm).then(response => {
console.log('新增成功响应:', response)
addNotice(submitForm).then(() => {
modal.msgSuccess('新增成功')
setTimeout(() => {
uni.navigateBack()
}, 1000)
}).catch(error => {
console.error('新增失败:', error)
console.log('错误详情:', {
message: error.message,
response: error.response,
data: error.response?.data
})
modal.msgError('新增失败: ' + (error.message || '未知错误'))
})
}
}
// 测试函数 - 用于验证基本功能
function testSave() {
console.log('=== 开始测试保存功能 ===')
console.log('当前表单状态:', form)
console.log('编辑器上下文:', editorCtx.value)
console.log('内容长度:', form.noticeContent?.length || 0)
// 强制获取编辑器内容进行测试
if (editorCtx.value) {
editorCtx.value.getContents({
success: (res) => {
console.log('测试获取的内容:', res)
console.log('HTML内容:', res.html)
console.log('纯文本内容:', res.text)
console.log('字符数:', res.text?.length || 0)
// 尝试直接保存测试数据
const testData = {
noticeTitle: form.noticeTitle || '测试标题',
noticeType: form.noticeType || '1',
noticeContent: res.html || '<p>测试内容</p>',
status: form.status || '0'
}
console.log('测试数据:', testData)
// 直接调用API测试
addNotice(testData).then(response => {
console.log('测试保存成功:', response)
modal.msgSuccess('测试保存成功!')
}).catch(error => {
console.error('测试保存失败:', error)
modal.msgError('测试保存失败: ' + error.message)
})
},
fail: (error) => {
console.error('测试获取内容失败:', error)
modal.msgError('无法获取编辑器内容')
}
})
} else {
console.log('编辑器未初始化')
modal.msgError('编辑器未初始化')
}
}
</script>
<style lang="scss" scoped>