feat: add WeCom outbox management features including retry and ignore actions, permissions, and UI integration
This commit is contained in:
@@ -0,0 +1,206 @@
|
||||
"use client"
|
||||
|
||||
import { useState } from "react"
|
||||
import { BanIcon, RotateCcwIcon } from "lucide-react"
|
||||
import { toast } from "sonner"
|
||||
|
||||
import {
|
||||
DashboardListPage,
|
||||
type DashboardListRenderContext,
|
||||
} from "@/components/dashboard/list"
|
||||
import { Badge } from "@/components/ui/badge"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import {
|
||||
fetchWxWorkOutboxFailures,
|
||||
ignoreWxWorkOutbox,
|
||||
retryWxWorkOutbox,
|
||||
type ChannelMessageOutbox,
|
||||
} from "@/lib/api/admin"
|
||||
import { formatDateTime } from "@/lib/utils"
|
||||
|
||||
const STATUS_OPTIONS = [
|
||||
{ value: "failed", label: "失败" },
|
||||
{ value: "ignored", label: "已忽略" },
|
||||
{ value: "all", label: "全部" },
|
||||
] as const
|
||||
|
||||
function statusLabel(status: string) {
|
||||
if (status === "failed") return "失败"
|
||||
if (status === "ignored") return "已忽略"
|
||||
return status || "-"
|
||||
}
|
||||
|
||||
function statusVariant(status: string) {
|
||||
if (status === "failed") return "destructive" as const
|
||||
if (status === "ignored") return "outline" as const
|
||||
return "secondary" as const
|
||||
}
|
||||
|
||||
function formatOptionalTime(value: string) {
|
||||
return value ? formatDateTime(value) : "-"
|
||||
}
|
||||
|
||||
function OutboxActions({
|
||||
item,
|
||||
reload,
|
||||
}: {
|
||||
item: ChannelMessageOutbox
|
||||
reload: DashboardListRenderContext<ChannelMessageOutbox>["reload"]
|
||||
}) {
|
||||
const [runningAction, setRunningAction] = useState<"retry" | "ignore" | null>(null)
|
||||
|
||||
async function runAction(action: "retry" | "ignore") {
|
||||
setRunningAction(action)
|
||||
try {
|
||||
if (action === "retry") {
|
||||
await retryWxWorkOutbox(item.id)
|
||||
toast.success("已重新加入发送队列")
|
||||
} else {
|
||||
await ignoreWxWorkOutbox(item.id)
|
||||
toast.success("已忽略该失败记录")
|
||||
}
|
||||
await reload()
|
||||
} catch (error) {
|
||||
toast.error(error instanceof Error ? error.message : "操作失败")
|
||||
} finally {
|
||||
setRunningAction(null)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex items-center justify-end gap-2">
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="sm"
|
||||
disabled={runningAction !== null}
|
||||
onClick={() => void runAction("retry")}
|
||||
>
|
||||
<RotateCcwIcon />
|
||||
重试
|
||||
</Button>
|
||||
{item.sendStatus === "failed" ? (
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
disabled={runningAction !== null}
|
||||
onClick={() => void runAction("ignore")}
|
||||
>
|
||||
<BanIcon />
|
||||
忽略
|
||||
</Button>
|
||||
) : null}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default function DashboardWxWorkOutboxPage() {
|
||||
return (
|
||||
<DashboardListPage<ChannelMessageOutbox>
|
||||
filters={[
|
||||
{
|
||||
name: "sendStatus",
|
||||
label: "状态",
|
||||
defaultValue: "failed",
|
||||
type: "segment",
|
||||
options: STATUS_OPTIONS,
|
||||
},
|
||||
{
|
||||
name: "conversationId",
|
||||
label: "会话 ID",
|
||||
placeholder: "会话 ID",
|
||||
defaultValue: "",
|
||||
valueType: "number",
|
||||
className: "w-full sm:w-40",
|
||||
},
|
||||
{
|
||||
name: "messageId",
|
||||
label: "消息 ID",
|
||||
placeholder: "消息 ID",
|
||||
defaultValue: "",
|
||||
valueType: "number",
|
||||
className: "w-full sm:w-40",
|
||||
},
|
||||
]}
|
||||
fetchList={fetchWxWorkOutboxFailures}
|
||||
getItemId={(item) => item.id}
|
||||
columns={[
|
||||
{
|
||||
key: "id",
|
||||
label: "Outbox",
|
||||
className: "w-28 text-xs text-muted-foreground",
|
||||
render: (item) => `#${item.id}`,
|
||||
},
|
||||
{
|
||||
key: "message",
|
||||
label: "消息",
|
||||
className: "w-48",
|
||||
render: (item) => (
|
||||
<div className="space-y-1 text-xs">
|
||||
<div>会话 #{item.conversationId || "-"}</div>
|
||||
<div className="text-muted-foreground">消息 #{item.messageId || "-"}</div>
|
||||
</div>
|
||||
),
|
||||
},
|
||||
{
|
||||
key: "status",
|
||||
label: "状态",
|
||||
className: "w-28",
|
||||
render: (item) => (
|
||||
<Badge variant={statusVariant(item.sendStatus)}>
|
||||
{statusLabel(item.sendStatus)}
|
||||
</Badge>
|
||||
),
|
||||
},
|
||||
{
|
||||
key: "retry",
|
||||
label: "重试",
|
||||
className: "w-44 text-xs",
|
||||
render: (item) => (
|
||||
<div className="space-y-1">
|
||||
<div>{item.retryCount} 次</div>
|
||||
<div className="text-muted-foreground">
|
||||
下次 {formatOptionalTime(item.nextRetryAt)}
|
||||
</div>
|
||||
</div>
|
||||
),
|
||||
},
|
||||
{
|
||||
key: "error",
|
||||
label: "失败原因",
|
||||
className: "min-w-72 max-w-[32rem]",
|
||||
render: (item) =>
|
||||
item.lastError ? (
|
||||
<span className="block truncate text-xs text-destructive" title={item.lastError}>
|
||||
{item.lastError}
|
||||
</span>
|
||||
) : (
|
||||
"-"
|
||||
),
|
||||
},
|
||||
{
|
||||
key: "updatedAt",
|
||||
label: "更新时间",
|
||||
className: "w-44 text-xs text-muted-foreground",
|
||||
render: (item) => formatOptionalTime(item.updatedAt),
|
||||
},
|
||||
{
|
||||
key: "actions",
|
||||
label: <span className="block text-right">操作</span>,
|
||||
className: "w-44",
|
||||
render: (item, context) => (
|
||||
<OutboxActions item={item} reload={context.reload} />
|
||||
),
|
||||
},
|
||||
]}
|
||||
labels={{
|
||||
refresh: "刷新",
|
||||
query: "查询",
|
||||
loading: "正在加载企业微信 outbox...",
|
||||
empty: "暂无失败 outbox",
|
||||
loadFailed: "加载企业微信 outbox 失败",
|
||||
}}
|
||||
/>
|
||||
)
|
||||
}
|
||||
@@ -193,6 +193,23 @@ export type WxWorkKFAccount = {
|
||||
managePrivilege: boolean
|
||||
}
|
||||
|
||||
export type ChannelMessageOutbox = {
|
||||
id: number
|
||||
channelType: string
|
||||
conversationId: number
|
||||
messageId: number
|
||||
payload: string
|
||||
sendStatus: string
|
||||
retryCount: number
|
||||
nextRetryAt: string
|
||||
lastError: string
|
||||
sentAt: string
|
||||
createdAt: string
|
||||
updatedAt: string
|
||||
createUserName: string
|
||||
updateUserName: string
|
||||
}
|
||||
|
||||
export type CreateAdminChannelPayload = {
|
||||
channelType: string
|
||||
aiAgentId: number
|
||||
@@ -907,6 +924,28 @@ export function fetchWxWorkKFAccounts() {
|
||||
return request<WxWorkKFAccount[]>("/api/dashboard/channel/wxwork/kf/accounts")
|
||||
}
|
||||
|
||||
export function fetchWxWorkOutboxFailures(
|
||||
query?: Record<string, string | number | undefined>
|
||||
) {
|
||||
return request<PageResult<ChannelMessageOutbox>>(
|
||||
`/api/dashboard/channel/wxwork/outbox/failed/list${toQueryString(query)}`
|
||||
)
|
||||
}
|
||||
|
||||
export function retryWxWorkOutbox(id: number) {
|
||||
return request<void>("/api/dashboard/channel/wxwork/outbox/retry", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ id }),
|
||||
})
|
||||
}
|
||||
|
||||
export function ignoreWxWorkOutbox(id: number) {
|
||||
return request<void>("/api/dashboard/channel/wxwork/outbox/ignore", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ id }),
|
||||
})
|
||||
}
|
||||
|
||||
export function createChannel(payload: CreateAdminChannelPayload) {
|
||||
return request<AdminChannel>("/api/dashboard/channel/create", {
|
||||
method: "POST",
|
||||
|
||||
@@ -38,6 +38,13 @@ describe("isDashboardNavItemActive", () => {
|
||||
isDashboardNavItemActive("/dashboard/tickets-extra", "/dashboard/tickets"),
|
||||
false
|
||||
)
|
||||
assert.equal(
|
||||
isDashboardNavItemActive(
|
||||
"/dashboard/wxwork-outbox",
|
||||
"/dashboard/channels"
|
||||
),
|
||||
false
|
||||
)
|
||||
})
|
||||
|
||||
it("only marks the dashboard home item active on the exact dashboard path", async () => {
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
GlobeIcon,
|
||||
KeyRoundIcon,
|
||||
LayoutDashboardIcon,
|
||||
MessageSquareWarningIcon,
|
||||
MessageSquareCodeIcon,
|
||||
MessageSquareMoreIcon,
|
||||
ShieldCheckIcon,
|
||||
@@ -169,6 +170,12 @@ export const dashboardNavSections: DashboardNavSectionConfig[] = [
|
||||
icon: <GlobeIcon />,
|
||||
requiredPermission: "channel.view",
|
||||
},
|
||||
{
|
||||
titleKey: "nav.wxworkOutbox",
|
||||
url: "/dashboard/wxwork-outbox",
|
||||
icon: <MessageSquareWarningIcon />,
|
||||
requiredPermission: "wxworkOutbox.view",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
|
||||
@@ -36,6 +36,7 @@ const PERMISSION_RESOURCE_LABELS: Record<string, { singular: string; plural: str
|
||||
tag: { singular: "tag", plural: "tags" },
|
||||
company: { singular: "company", plural: "companies" },
|
||||
channel: { singular: "channel", plural: "channels" },
|
||||
wxworkOutbox: { singular: "WeCom outbox record", plural: "WeCom outbox records" },
|
||||
customer: { singular: "customer", plural: "customers" },
|
||||
agent: { singular: "agent", plural: "agents" },
|
||||
agentTeam: { singular: "agent team", plural: "agent teams" },
|
||||
@@ -60,6 +61,7 @@ const PERMISSION_NAME_OVERRIDES: Record<string, string> = {
|
||||
"channel.resetUserTokenSecret": "Reset channel user token secret",
|
||||
"agent.config": "Configure agent service rules",
|
||||
"agentTeamSchedule.batchGenerate": "Batch generate agent team schedules",
|
||||
"wxworkOutbox.update": "Handle WeCom outbox records",
|
||||
"mcp.view": "View MCP debug information",
|
||||
"mcp.call": "Call MCP tools",
|
||||
}
|
||||
|
||||
@@ -2341,6 +2341,7 @@
|
||||
"agents": "Human Agents",
|
||||
"agentTeamSchedules": "Team Schedules",
|
||||
"channels": "Channels",
|
||||
"wxworkOutbox": "WeCom Outbox",
|
||||
"aiCapabilities": "AI Capabilities",
|
||||
"knowledge": "Knowledge Base",
|
||||
"aiConfigs": "Model Settings",
|
||||
|
||||
@@ -2341,6 +2341,7 @@
|
||||
"agents": "人工客服",
|
||||
"agentTeamSchedules": "客服组排班",
|
||||
"channels": "接入渠道",
|
||||
"wxworkOutbox": "企微 Outbox",
|
||||
"aiCapabilities": "AI能力",
|
||||
"knowledge": "知识库",
|
||||
"aiConfigs": "AI模型",
|
||||
|
||||
Reference in New Issue
Block a user