diff --git a/components/admin/NotificationCenterPanel.test.tsx b/components/admin/NotificationCenterPanel.test.tsx
index c94dc192..3c73b515 100644
--- a/components/admin/NotificationCenterPanel.test.tsx
+++ b/components/admin/NotificationCenterPanel.test.tsx
@@ -52,6 +52,16 @@ describe('NotificationCenterPanel', () => {
readText: jest.fn().mockImplementation(async () => clipboardText),
},
})
+ Object.defineProperty(document, 'execCommand', {
+ configurable: true,
+ value: jest.fn().mockImplementation((command: string) => {
+ if (command !== 'copy') return false
+ clipboardText = document.querySelector('textarea[aria-hidden="true"]')?.textContent
+ || (document.querySelector('textarea[aria-hidden="true"]') as HTMLTextAreaElement | null)?.value
+ || ''
+ return true
+ }),
+ })
global.fetch = jest.fn((input: RequestInfo | URL, init?: RequestInit) => {
const url = String(input)
if (url.endsWith('/api/admin/notification-center/projects') && (!init?.method || init.method === 'GET')) {
@@ -252,6 +262,27 @@ describe('NotificationCenterPanel', () => {
expect(screen.queryByText('current-project-secret')).not.toBeInTheDocument()
})
+ test('浏览器拒绝 Clipboard API 时回退到兼容复制', async () => {
+ const user = userEvent.setup()
+ Object.defineProperty(navigator, 'clipboard', {
+ configurable: true,
+ value: {
+ writeText: jest.fn().mockRejectedValue(new Error('NotAllowedError')),
+ },
+ })
+ render()
+ await screen.findByText('素材平台')
+
+ await user.click(screen.getByRole('button', { name: '查看配置并编辑资料 素材平台' }))
+ await user.click(screen.getByRole('button', { name: '查看当前密钥' }))
+ await screen.findByText('current-project-secret')
+ await user.click(screen.getByRole('button', { name: '复制 素材平台 的当前项目密钥' }))
+
+ expect(document.execCommand).toHaveBeenCalledWith('copy')
+ expect(clipboardText).toBe('current-project-secret')
+ expect(showToast).toHaveBeenCalledWith('项目密钥已复制', 'success')
+ })
+
test('测试通知支持直接输入准确花名并在确认后透传 recipientAlias', async () => {
const user = userEvent.setup()
render()
diff --git a/components/admin/NotificationCenterPanel.tsx b/components/admin/NotificationCenterPanel.tsx
index 60a99cff..1508dde3 100644
--- a/components/admin/NotificationCenterPanel.tsx
+++ b/components/admin/NotificationCenterPanel.tsx
@@ -638,11 +638,41 @@ export default function NotificationCenterPanel({ showToast }: NotificationCente
}
const copyText = async (value: string, label: string) => {
+ const copyWithSelection = () => {
+ const previousFocus = document.activeElement instanceof HTMLElement ? document.activeElement : null
+ const textarea = document.createElement('textarea')
+ textarea.value = value
+ textarea.readOnly = true
+ textarea.setAttribute('aria-hidden', 'true')
+ textarea.style.position = 'fixed'
+ textarea.style.inset = '0 auto auto -9999px'
+ textarea.style.fontSize = '16px'
+ textarea.style.opacity = '0'
+ document.body.appendChild(textarea)
+ try {
+ textarea.focus()
+ textarea.select()
+ textarea.setSelectionRange(0, textarea.value.length)
+ if (!document.execCommand('copy')) throw new Error('浏览器拒绝复制')
+ } finally {
+ textarea.remove()
+ previousFocus?.focus({ preventScroll: true })
+ }
+ }
+
try {
- await navigator.clipboard.writeText(value)
+ if (navigator.clipboard?.writeText) {
+ try {
+ await navigator.clipboard.writeText(value)
+ } catch {
+ copyWithSelection()
+ }
+ } else {
+ copyWithSelection()
+ }
showToast(`${label}已复制`, 'success')
} catch {
- showToast('复制失败,请手动选择文本复制', 'error')
+ showToast('复制失败,请长按或选中文本后手动复制', 'error')
}
}
@@ -1011,7 +1041,7 @@ export default function NotificationCenterPanel({ showToast }: NotificationCente
{confirmTestOpen && preparedTestRecipient && selectedTestProject &&
确认发送真实测试通知
- 项目
- {selectedTestProject.name} · {selectedTestProject.projectCode}
- 真实收件人
- {preparedTestRecipient.label}
- 标题
- {testTitle}
通知中心会统一在标题前加上项目名称;确认后会真实调用钉钉,不是页面预览。
}
- {oneTimeSecret &&
{oneTimeSecret.reason === 'created' ? '接入项目已创建' : '项目密钥已轮换'}
请把下面的 URL、projectCode 和项目密钥保存到调用方后端的安全环境变量中。关闭后仍可在“查看配置 / 编辑资料”里按需查看当前密钥。
统一通知中心 URL{oneTimeSecret.env.YYD_NOTIFICATION_CENTER_URL || '后端未返回'}
projectCode{oneTimeSecret.projectCode || oneTimeSecret.env.YYD_NOTIFICATION_CENTER_PROJECT_CODE || '后端未返回'}
项目密钥
{oneTimeSecret.secret}调用方环境变量
{Object.entries(oneTimeSecret.env).map(([key, value]) => `${key}=${value}`).join('\n')} }
+ {oneTimeSecret &&
{oneTimeSecret.reason === 'created' ? '接入项目已创建' : '项目密钥已轮换'}
把下面 3 项直接交给外部项目负责人,并要求只配置在项目后端。点击“复制全部配置”可一次复制;关闭后仍可在“查看配置 / 编辑资料”里按需查看当前密钥。
统一通知中心 URL{oneTimeSecret.env.YYD_NOTIFICATION_CENTER_URL || '后端未返回'}
projectCode{oneTimeSecret.projectCode || oneTimeSecret.env.YYD_NOTIFICATION_CENTER_PROJECT_CODE || '后端未返回'}
项目密钥
{oneTimeSecret.secret}调用方环境变量
{Object.entries(oneTimeSecret.env).map(([key, value]) => `${key}=${value}`).join('\n')} }
{confirmActionDialog}
)