refactor: support i18n
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { readSession } from "@/lib/auth"
|
||||
import { request } from "@/lib/api/client"
|
||||
import { createWebSocketBaseUrl } from "@/lib/api/websocket"
|
||||
import { translateCurrentMessage } from "@/i18n/messages"
|
||||
|
||||
export type Paging = {
|
||||
page: number
|
||||
@@ -576,7 +577,7 @@ function toQueryString(query?: Record<string, string | number | undefined>) {
|
||||
export function createAdminWebSocketUrl() {
|
||||
const session = readSession()
|
||||
if (!session?.accessToken) {
|
||||
throw new Error("未登录或登录已过期")
|
||||
throw new Error(translateCurrentMessage("api.authExpired"))
|
||||
}
|
||||
|
||||
const baseUrl = createWebSocketBaseUrl()
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import { expireSession, readSession } from "@/lib/auth"
|
||||
import { readStoredLocale } from "@/i18n/config"
|
||||
import { translateCurrentMessage } from "@/i18n/messages"
|
||||
|
||||
const API_BASE_URL =
|
||||
process.env.NEXT_PUBLIC_API_BASE_URL?.trim() || ""
|
||||
@@ -22,7 +24,7 @@ async function parseResult<T>(response: Response) {
|
||||
if (payload.errorCode === 3000 || payload.errorCode === 3002) {
|
||||
expireSession()
|
||||
}
|
||||
const error = new Error(payload.message || "请求失败")
|
||||
const error = new Error(payload.message || translateCurrentMessage("api.requestFailed"))
|
||||
;(error as Error & { errorCode?: number }).errorCode = payload.errorCode
|
||||
throw error
|
||||
}
|
||||
@@ -49,6 +51,9 @@ export async function request<T>(
|
||||
) {
|
||||
authHeaders.set("Content-Type", "application/json")
|
||||
}
|
||||
const locale = readStoredLocale()
|
||||
authHeaders.set("Accept-Language", locale)
|
||||
authHeaders.set("X-Locale", locale)
|
||||
|
||||
const requestBaseUrl = baseUrl !== undefined ? baseUrl : API_BASE_URL
|
||||
const response = await fetch(`${requestBaseUrl}${path}`, {
|
||||
|
||||
@@ -31,7 +31,7 @@ export type UpdateAdminCustomerPayload = CreateAdminCustomerPayload & {
|
||||
id: number
|
||||
}
|
||||
|
||||
/** 与 POST /customer/save/profile 请求体一致 */
|
||||
/** Matches POST /customer/save/profile request body. */
|
||||
export type SaveCustomerProfileContactLine = {
|
||||
id?: number
|
||||
contactType: ContactType | string
|
||||
@@ -49,14 +49,14 @@ export type SaveCustomerProfilePayload = {
|
||||
contacts: SaveCustomerProfileContactLine[]
|
||||
}
|
||||
|
||||
/** 与 POST /customer/list JSON Body 一致 */
|
||||
/** Matches POST /customer/list JSON body. */
|
||||
export type CustomerListRequest = {
|
||||
page: number
|
||||
limit: number
|
||||
status?: number
|
||||
gender?: number
|
||||
companyId?: number
|
||||
/** 模糊匹配:客户名、主手机、主邮箱、联系方式、公司名称 */
|
||||
/** Fuzzy match against customer name, primary phone, primary email, contacts, and company name. */
|
||||
keyword?: string
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ export function createCustomer(payload: CreateAdminCustomerPayload) {
|
||||
})
|
||||
}
|
||||
|
||||
/** 单请求 + 单事务保存客户主信息与联系方式全量 */
|
||||
/** Saves the customer profile and the full contact list in one request and transaction. */
|
||||
export function saveCustomerProfile(payload: SaveCustomerProfilePayload) {
|
||||
return request<AdminCustomer>("/api/dashboard/customer/save_profile", {
|
||||
method: "POST",
|
||||
|
||||
+2
-1
@@ -1,4 +1,5 @@
|
||||
import { request } from "@/lib/api/client"
|
||||
import { translateCurrentMessage } from "@/i18n/messages"
|
||||
import { readKefuChatRuntimeConfig } from "@/lib/sdk/runtime-config"
|
||||
import { generateUUID } from "@/lib/utils"
|
||||
|
||||
@@ -280,7 +281,7 @@ function createExchangeHeaders() {
|
||||
function createImHeaders() {
|
||||
const sessionToken = getCustomerSessionToken()
|
||||
if (!sessionToken) {
|
||||
throw new Error("客服会话未初始化")
|
||||
throw new Error(translateCurrentMessage("api.customerSessionNotReady"))
|
||||
}
|
||||
return {
|
||||
...createChannelHeaders(),
|
||||
|
||||
@@ -2,6 +2,7 @@ import { readSession } from "@/lib/auth"
|
||||
import { request } from "@/lib/api/client"
|
||||
import { createWebSocketBaseUrl } from "@/lib/api/websocket"
|
||||
import type { PageResult } from "@/lib/api/admin"
|
||||
import { translateCurrentMessage } from "@/i18n/messages"
|
||||
|
||||
export type NotificationReadStatus = "all" | "unread" | "read"
|
||||
|
||||
@@ -71,7 +72,7 @@ export function markAllNotificationsRead() {
|
||||
export function createNotificationWebSocketUrl() {
|
||||
const session = readSession()
|
||||
if (!session?.accessToken) {
|
||||
throw new Error("未登录或登录已过期")
|
||||
throw new Error(translateCurrentMessage("api.authExpired"))
|
||||
}
|
||||
|
||||
const params = new URLSearchParams({
|
||||
|
||||
Reference in New Issue
Block a user