fix(oa): 同意/拒绝/打回前自动发送评论草稿

审批动作前冲刷评论区未发送内容,减少先点发送再审批的一步操作。
关联 #118。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
zlwuandCursor committed 2026-11-19 11:55:53 +08:00
1 parent 0929164bf5
commit a69dcc5bca
8 files changed
+137 -13

No files matched your search

@@ -150,6 +150,7 @@ import {
uploadAttachment
} from '../../api/oa.js'
import OaCommentFileCards from './OaCommentFileCards.vue'
import { hasOaCommentDraft } from '../oa-comment-draft.js'
import { attachmentFullUrl } from '../../utils/attachment.js'
import { isHarmonyWechat } from '../../utils/harmony-file-picker.js'
import HarmonyFileSource from '../../components/HarmonyFileSource.vue'
@@ -516,12 +517,25 @@ function confirmMention() {
mentionVisible.value = false
}
async function onSend() {
const content = draft.value.trim()
if (!content && !mentionUsers.value.length && !picked.value.length) {
showToast('请填写评论、@人员或添加附件')
return
function draftPending() {
return hasOaCommentDraft({
content: draft.value,
mentionCount: mentionUsers.value.length,
attachmentCount: picked.value.length
})
}
/**
* 审批动作前冲刷未发送草稿:无草稿直接成功;有草稿则等同点击「发送」。
* @returns {Promise<boolean>} false 表示发送失败或忙碌,调用方应中止审批动作
*/
async function flushDraft(opts = {}) {
if (!draftPending()) return true
if (sending.value || uploading.value) {
showToast(uploading.value ? '附件上传中,请稍后再试' : '评论发送中,请稍后再试')
return false
}
const content = draft.value.trim()
sending.value = true
try {
await fetchCreateOaComment(props.instanceId, {
@@ -533,15 +547,27 @@ async function onSend() {
picked.value = []
mentionUsers.value = []
closeAtSuggest()
showToast('已发送')
if (!opts.quiet) showToast('已发送')
emit('refresh')
return true
} catch (e) {
showToast(e?.message || '发送失败')
return false
} finally {
sending.value = false
}
}
async function onSend() {
if (!draftPending()) {
showToast('请填写评论、@人员或添加附件')
return
}
await flushDraft()
}
defineExpose({ flushDraft, draftPending })
onMounted(() => {
void ensureUserCache()
})
+7
View File
@@ -0,0 +1,7 @@
/** 评论区是否有未发送草稿(文字 / @ / 附件) */
export function hasOaCommentDraft(input = {}) {
const content = String(input.content || '').trim()
const mentionCount = Number(input.mentionCount || 0)
const attachmentCount = Number(input.attachmentCount || 0)
return !!(content || mentionCount > 0 || attachmentCount > 0)
}
@@ -0,0 +1,14 @@
import assert from 'node:assert/strict'
import test from 'node:test'
import { hasOaCommentDraft } from './oa-comment-draft.js'
test('empty draft is false', () => {
assert.equal(hasOaCommentDraft({}), false)
assert.equal(hasOaCommentDraft({ content: ' ', mentionCount: 0, attachmentCount: 0 }), false)
})
test('text / mention / attachment counts as draft', () => {
assert.equal(hasOaCommentDraft({ content: '@test' }), true)
assert.equal(hasOaCommentDraft({ mentionCount: 1 }), true)
assert.equal(hasOaCommentDraft({ attachmentCount: 2 }), true)
})
+11 -1
View File
@@ -29,6 +29,7 @@
@refresh="reloadDetail"
/>
<OaCommentComposer
ref="commentComposerRef"
:instance-id="instanceId"
:instance-status="detail?.status || ''"
@refresh="reloadDetail"
@@ -238,6 +239,7 @@ const currentUserId = ref('')
const currentUserName = ref('')
const autoPrinted = ref(false)
const commentComposerRef = ref(null)
const commentVisible = ref(false)
const commentKind = ref('approve') // approve | reject | return
const commentText = ref('')
@@ -492,8 +494,16 @@ function goCopyRestart() {
})
}
function openComment(kind) {
/** 同意/拒绝/打回前先发送评论区未提交草稿 */
async function flushPendingComment() {
const flush = commentComposerRef.value?.flushDraft
if (!flush) return true
return await flush()
}
async function openComment(kind) {
if (!myPendingTask.value) return
if (!(await flushPendingComment())) return
commentKind.value = kind
commentText.value = defaultComment(kind)
commentVisible.value = true
@@ -192,6 +192,7 @@
} from '@/api/oaApproval'
import OaCommentFileCards from '@/components/oa-approval/OaCommentFileCards.vue'
import { attachmentFullUrl } from '@/composables/useAttachmentActions'
import { hasOaCommentDraft } from '@/utils/oa-comment-draft'
import { isOaUploadImageFile, isOaUploadVideoFile } from '@/utils/oa-upload-file-type'
defineOptions({ name: 'OaCommentPanel' })
@@ -546,12 +547,25 @@
}
}
async function onSend() {
const content = draft.value.trim()
if (!content && !mentionUsers.value.length && !picked.value.length) {
ElMessage.warning('请填写评论、@人员或添加附件')
return
function draftPending() {
return hasOaCommentDraft({
content: draft.value,
mentionCount: mentionUsers.value.length,
attachmentCount: picked.value.length
})
}
/**
* 审批动作前冲刷未发送草稿:无草稿直接成功;有草稿则等同点击「发送」。
* @returns false 表示发送失败或忙碌,调用方应中止审批动作
*/
async function flushDraft(opts?: { quiet?: boolean }): Promise<boolean> {
if (!draftPending()) return true
if (sending.value || uploading.value) {
ElMessage.warning(uploading.value ? '附件上传中,请稍后再试' : '评论发送中,请稍后再试')
return false
}
const content = draft.value.trim()
sending.value = true
try {
await fetchCreateOaComment(props.instanceId, {
@@ -565,15 +579,27 @@
picked.value = []
mentionUsers.value = []
closeAtSuggest()
ElMessage.success('已发送')
if (!opts?.quiet) ElMessage.success('已发送')
emit('refresh')
return true
} catch (e: any) {
ElMessage.error(e?.message || '发送失败')
return false
} finally {
sending.value = false
}
}
async function onSend() {
if (!draftPending()) {
ElMessage.warning('请填写评论、@人员或添加附件')
return
}
await flushDraft()
}
defineExpose({ flushDraft, draftPending })
onMounted(() => {
void ensureUserCache()
})
@@ -0,0 +1,16 @@
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { hasOaCommentDraft } from './oa-comment-draft.ts'
describe('hasOaCommentDraft', () => {
it('treats blank content without mentions/files as empty', () => {
assert.equal(hasOaCommentDraft({}), false)
assert.equal(hasOaCommentDraft({ content: ' ', mentionCount: 0, attachmentCount: 0 }), false)
})
it('detects text, mentions and attachments', () => {
assert.equal(hasOaCommentDraft({ content: '@test' }), true)
assert.equal(hasOaCommentDraft({ mentionCount: 1 }), true)
assert.equal(hasOaCommentDraft({ attachmentCount: 2 }), true)
})
})
+12
View File
@@ -0,0 +1,12 @@
/** 评论区是否有未发送草稿(文字 / @ / 附件) */
export function hasOaCommentDraft(input: {
content?: string | null
mentionCount?: number
attachmentCount?: number
}): boolean {
return !!(
String(input.content || '').trim() ||
(input.mentionCount || 0) > 0 ||
(input.attachmentCount || 0) > 0
)
}
@@ -99,6 +99,7 @@
/>
<OaCommentPanel
v-if="detail?.id"
ref="commentPanelRef"
:instance-id="detail.id"
:instance-status="detail.status || ''"
@refresh="load"
@@ -236,6 +237,9 @@
const userStore = useUserStore()
const loading = ref(false)
const acting = ref(false)
const commentPanelRef = ref<{ flushDraft?: (opts?: { quiet?: boolean }) => Promise<boolean> } | null>(
null
)
const detail = ref<any>(null)
const viewFormData = ref<Record<string, any>>({})
const viewRule = ref<any[]>([])
@@ -392,9 +396,17 @@
}
}
/** 同意/拒绝/打回前先发送评论区未提交草稿 */
async function flushPendingComment(): Promise<boolean> {
const flush = commentPanelRef.value?.flushDraft
if (!flush) return true
return await flush()
}
async function act(kind: 'approve' | 'reject') {
const task = myPendingTask.value
if (!task) return
if (!(await flushPendingComment())) return
let comment = ''
try {
const res = await ElMessageBox.prompt(
@@ -436,6 +448,7 @@
async function doReturn() {
const task = myPendingTask.value
if (!task) return
if (!(await flushPendingComment())) return
let comment = ''
try {
const res = await ElMessageBox.prompt('请填写打回意见(必填)', '打回', {