2026-04-09 10:01:23 +08:00
|
|
|
|
import { request } from "@/lib/api/client"
|
2026-04-24 19:17:07 +08:00
|
|
|
|
import { readKefuWidgetConfig } from "@/lib/kefu-widget-config"
|
2026-04-09 10:01:23 +08:00
|
|
|
|
import { generateUUID } from "@/lib/utils"
|
|
|
|
|
|
|
|
|
|
|
|
export type Paging = {
|
|
|
|
|
|
page: number
|
|
|
|
|
|
limit: number
|
|
|
|
|
|
total: number
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export type PageResult<T> = {
|
2026-04-24 19:17:07 +08:00
|
|
|
|
results?: T[] | null
|
2026-04-09 10:01:23 +08:00
|
|
|
|
page: Paging
|
2026-04-24 14:34:21 +08:00
|
|
|
|
cursor?: string
|
|
|
|
|
|
hasMore?: boolean
|
2026-04-09 10:01:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export type ImConversationTag = {
|
|
|
|
|
|
id: number
|
|
|
|
|
|
name: string
|
|
|
|
|
|
color: string
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export type ImConversationParticipant = {
|
|
|
|
|
|
id: number
|
|
|
|
|
|
participantType: string
|
|
|
|
|
|
participantId: number
|
|
|
|
|
|
externalParticipantId?: string
|
|
|
|
|
|
joinedAt?: string
|
|
|
|
|
|
leftAt?: string
|
|
|
|
|
|
status: number
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export type ImConversation = {
|
|
|
|
|
|
id: number
|
|
|
|
|
|
externalSource: string
|
|
|
|
|
|
externalId: string
|
|
|
|
|
|
subject: string
|
|
|
|
|
|
status: number
|
|
|
|
|
|
serviceMode: number
|
|
|
|
|
|
priority: number
|
|
|
|
|
|
currentAssigneeId: number
|
|
|
|
|
|
currentAssigneeName?: string
|
|
|
|
|
|
lastMessageId: number
|
|
|
|
|
|
lastMessageAt?: string
|
|
|
|
|
|
lastActiveAt?: string
|
|
|
|
|
|
lastMessageSummary?: string
|
|
|
|
|
|
customerUnreadCount: number
|
|
|
|
|
|
agentUnreadCount: number
|
|
|
|
|
|
customerLastReadMessageId: number
|
|
|
|
|
|
customerLastReadSeqNo: number
|
|
|
|
|
|
customerLastReadAt?: string
|
|
|
|
|
|
agentLastReadMessageId: number
|
|
|
|
|
|
agentLastReadSeqNo: number
|
|
|
|
|
|
agentLastReadAt?: string
|
|
|
|
|
|
closedAt?: string
|
|
|
|
|
|
tags?: ImConversationTag[]
|
|
|
|
|
|
participants?: ImConversationParticipant[]
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export type ImConversationDetail = ImConversation
|
|
|
|
|
|
|
|
|
|
|
|
export type ImMessage = {
|
|
|
|
|
|
id: number
|
|
|
|
|
|
conversationId: number
|
|
|
|
|
|
clientMsgId?: string
|
|
|
|
|
|
senderType: string
|
|
|
|
|
|
senderId: number
|
|
|
|
|
|
senderName?: string
|
|
|
|
|
|
senderAvatar?: string
|
|
|
|
|
|
messageType: string
|
|
|
|
|
|
content: string
|
|
|
|
|
|
payload?: string
|
|
|
|
|
|
seqNo: number
|
|
|
|
|
|
sendStatus: number
|
|
|
|
|
|
sentAt?: string
|
|
|
|
|
|
deliveredAt?: string
|
|
|
|
|
|
readAt?: string
|
|
|
|
|
|
customerRead: boolean
|
|
|
|
|
|
customerReadAt?: string
|
|
|
|
|
|
agentRead: boolean
|
|
|
|
|
|
agentReadAt?: string
|
|
|
|
|
|
recalledAt?: string
|
|
|
|
|
|
quotedMessageId?: number
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export type ImAsset = {
|
|
|
|
|
|
id: number
|
|
|
|
|
|
assetId: string
|
|
|
|
|
|
provider: string
|
2026-04-17 12:54:26 +08:00
|
|
|
|
storageKey: string
|
2026-04-09 10:01:23 +08:00
|
|
|
|
filename: string
|
|
|
|
|
|
fileSize: number
|
|
|
|
|
|
mimeType: string
|
|
|
|
|
|
status: number
|
|
|
|
|
|
url: string
|
|
|
|
|
|
createdAt: string
|
|
|
|
|
|
updatedAt: string
|
|
|
|
|
|
createUserId: number
|
|
|
|
|
|
createUserName: string
|
|
|
|
|
|
updateUserId: number
|
|
|
|
|
|
updateUserName: string
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-24 14:34:21 +08:00
|
|
|
|
export type ImWidgetConfig = {
|
2026-04-24 22:24:19 +08:00
|
|
|
|
channelId?: string
|
2026-04-27 15:42:04 +08:00
|
|
|
|
channelType?: string
|
|
|
|
|
|
externalSource?: string
|
2026-04-24 14:34:21 +08:00
|
|
|
|
title?: string
|
|
|
|
|
|
subtitle?: string
|
|
|
|
|
|
themeColor?: string
|
2026-04-24 22:24:19 +08:00
|
|
|
|
position?: "left" | "right"
|
|
|
|
|
|
width?: string
|
2026-04-24 14:34:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-09 10:01:23 +08:00
|
|
|
|
const VISITOR_STORAGE_KEY = "cs_agent_im_visitor_id"
|
|
|
|
|
|
const API_BASE_URL =
|
|
|
|
|
|
process.env.NEXT_PUBLIC_API_BASE_URL?.trim() || ""
|
|
|
|
|
|
const OPEN_IM_CHANNEL_ID =
|
|
|
|
|
|
process.env.NEXT_PUBLIC_OPEN_IM_CHANNEL_ID?.trim() || ""
|
|
|
|
|
|
const OPEN_IM_EXTERNAL_SOURCE =
|
|
|
|
|
|
process.env.NEXT_PUBLIC_OPEN_IM_EXTERNAL_SOURCE?.trim() || "web_chat"
|
|
|
|
|
|
|
|
|
|
|
|
function buildVisitorId() {
|
|
|
|
|
|
return `visitor_${generateUUID()}`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function getImVisitorId() {
|
|
|
|
|
|
if (typeof window === "undefined") {
|
|
|
|
|
|
return "visitor_ssr"
|
|
|
|
|
|
}
|
|
|
|
|
|
const existing = window.localStorage.getItem(VISITOR_STORAGE_KEY)?.trim()
|
|
|
|
|
|
if (existing) {
|
|
|
|
|
|
return existing
|
|
|
|
|
|
}
|
|
|
|
|
|
const visitorId = buildVisitorId()
|
|
|
|
|
|
window.localStorage.setItem(VISITOR_STORAGE_KEY, visitorId)
|
|
|
|
|
|
return visitorId
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-24 19:17:07 +08:00
|
|
|
|
function getRuntimeImConfig() {
|
|
|
|
|
|
const widgetConfig = readKefuWidgetConfig()
|
|
|
|
|
|
const baseUrl = (widgetConfig.apiBaseUrl || widgetConfig.baseUrl || API_BASE_URL)
|
|
|
|
|
|
.trim()
|
|
|
|
|
|
.replace(/\/$/, "")
|
2026-04-09 10:01:23 +08:00
|
|
|
|
return {
|
2026-04-24 19:17:07 +08:00
|
|
|
|
baseUrl,
|
|
|
|
|
|
channelId: widgetConfig.channelId || OPEN_IM_CHANNEL_ID,
|
|
|
|
|
|
externalSource:
|
|
|
|
|
|
(widgetConfig.externalSource || OPEN_IM_EXTERNAL_SOURCE).trim() || "web_chat",
|
2026-04-27 15:42:04 +08:00
|
|
|
|
externalId: (widgetConfig.externalId || "").trim(),
|
2026-04-24 19:17:07 +08:00
|
|
|
|
externalName: (widgetConfig.subject || "").trim(),
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function createImHeaders() {
|
|
|
|
|
|
const config = getRuntimeImConfig()
|
|
|
|
|
|
const headers: Record<string, string> = {
|
|
|
|
|
|
"X-External-Source": config.externalSource,
|
2026-04-27 15:42:04 +08:00
|
|
|
|
"X-External-Id": config.externalId || getImVisitorId(),
|
2026-04-24 19:17:07 +08:00
|
|
|
|
"X-Channel-Id": config.channelId,
|
|
|
|
|
|
}
|
|
|
|
|
|
if (config.externalName) {
|
|
|
|
|
|
headers["X-External-Name"] = encodeURIComponent(config.externalName)
|
|
|
|
|
|
}
|
|
|
|
|
|
return {
|
|
|
|
|
|
...headers,
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function createRequestOptions(
|
|
|
|
|
|
init?: RequestInit
|
|
|
|
|
|
): RequestInit & { baseUrl?: string; skipAuth?: boolean } {
|
|
|
|
|
|
return {
|
|
|
|
|
|
...init,
|
|
|
|
|
|
skipAuth: true,
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
...createImHeaders(),
|
|
|
|
|
|
...(init?.headers as Record<string, string> | undefined),
|
|
|
|
|
|
},
|
|
|
|
|
|
baseUrl: getRuntimeImConfig().baseUrl,
|
2026-04-09 10:01:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function toQueryString(query?: Record<string, string | number | undefined>) {
|
|
|
|
|
|
if (!query) {
|
|
|
|
|
|
return ""
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const params = new URLSearchParams()
|
|
|
|
|
|
Object.entries(query).forEach(([key, value]) => {
|
|
|
|
|
|
if (value === undefined || value === "") {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
params.set(key, String(value))
|
|
|
|
|
|
})
|
|
|
|
|
|
const output = params.toString()
|
|
|
|
|
|
return output ? `?${output}` : ""
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function fetchImConversationDetail(id: number) {
|
2026-04-25 10:27:43 +08:00
|
|
|
|
return request<ImConversationDetail>(`/api/conversation/${id}`, {
|
2026-04-24 19:17:07 +08:00
|
|
|
|
...createRequestOptions(),
|
2026-04-09 10:01:23 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function fetchImMessages(
|
|
|
|
|
|
query?: Record<string, string | number | undefined>
|
|
|
|
|
|
) {
|
|
|
|
|
|
return request<PageResult<ImMessage>>(
|
2026-04-25 10:27:43 +08:00
|
|
|
|
`/api/message/list${toQueryString(query)}`,
|
2026-04-24 19:17:07 +08:00
|
|
|
|
createRequestOptions()
|
2026-04-09 10:01:23 +08:00
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** 外部身份仅通过 createImHeaders()(X-External-*)传递,无 JSON body */
|
|
|
|
|
|
export function createOrMatchImConversation() {
|
2026-04-25 10:27:43 +08:00
|
|
|
|
return request<ImConversation>("/api/conversation/create_or_match", {
|
2026-04-24 19:17:07 +08:00
|
|
|
|
...createRequestOptions({ method: "POST" }),
|
2026-04-09 10:01:23 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-24 14:34:21 +08:00
|
|
|
|
export function fetchImWidgetConfig() {
|
|
|
|
|
|
return request<ImWidgetConfig>(
|
2026-04-25 10:27:43 +08:00
|
|
|
|
`/api/channel/config${toQueryString({
|
2026-04-24 19:17:07 +08:00
|
|
|
|
channelId: getRuntimeImConfig().channelId,
|
2026-04-24 14:34:21 +08:00
|
|
|
|
})}`,
|
2026-04-24 19:17:07 +08:00
|
|
|
|
createRequestOptions()
|
2026-04-24 14:34:21 +08:00
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function closeImConversation(conversationId: number) {
|
2026-04-25 10:27:43 +08:00
|
|
|
|
return request<void>("/api/conversation/close", {
|
2026-04-24 19:17:07 +08:00
|
|
|
|
...createRequestOptions({
|
|
|
|
|
|
method: "POST",
|
|
|
|
|
|
body: JSON.stringify({ conversationId }),
|
|
|
|
|
|
}),
|
2026-04-24 14:34:21 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-09 10:01:23 +08:00
|
|
|
|
export function sendImMessage(payload: {
|
|
|
|
|
|
conversationId: number
|
|
|
|
|
|
messageType: string
|
|
|
|
|
|
content: string
|
|
|
|
|
|
payload?: string
|
|
|
|
|
|
clientMsgId?: string
|
|
|
|
|
|
}) {
|
2026-04-25 10:27:43 +08:00
|
|
|
|
return request<ImMessage>("/api/message/send", {
|
2026-04-24 19:17:07 +08:00
|
|
|
|
...createRequestOptions({
|
|
|
|
|
|
method: "POST",
|
|
|
|
|
|
body: JSON.stringify(payload),
|
|
|
|
|
|
}),
|
2026-04-09 10:01:23 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function markImMessageRead(conversationId: number, messageId = 0) {
|
2026-04-25 10:27:43 +08:00
|
|
|
|
return request<void>("/api/message/read", {
|
2026-04-24 19:17:07 +08:00
|
|
|
|
...createRequestOptions({
|
|
|
|
|
|
method: "POST",
|
|
|
|
|
|
body: JSON.stringify({ conversationId, messageId }),
|
|
|
|
|
|
}),
|
2026-04-09 10:01:23 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function uploadImImage(conversationId: number, file: File) {
|
|
|
|
|
|
const formData = new FormData()
|
|
|
|
|
|
formData.set("conversationId", String(conversationId))
|
|
|
|
|
|
formData.set("file", file)
|
2026-04-25 10:27:43 +08:00
|
|
|
|
return request<ImAsset>("/api/message/upload_image", {
|
2026-04-24 19:17:07 +08:00
|
|
|
|
...createRequestOptions({
|
|
|
|
|
|
method: "POST",
|
|
|
|
|
|
body: formData,
|
|
|
|
|
|
}),
|
2026-04-09 10:01:23 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function uploadImAttachment(conversationId: number, file: File) {
|
|
|
|
|
|
const formData = new FormData()
|
|
|
|
|
|
formData.set("conversationId", String(conversationId))
|
|
|
|
|
|
formData.set("file", file)
|
2026-04-25 10:27:43 +08:00
|
|
|
|
return request<ImAsset>("/api/message/upload_attachment", {
|
2026-04-24 19:17:07 +08:00
|
|
|
|
...createRequestOptions({
|
|
|
|
|
|
method: "POST",
|
|
|
|
|
|
body: formData,
|
|
|
|
|
|
}),
|
2026-04-09 10:01:23 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|