50 lines
1.2 KiB
TypeScript
50 lines
1.2 KiB
TypeScript
import type { TitleData, TitleType } from "../types/titleLibrary"
|
|
import type { TitleItem } from "@/api/titles"
|
|
|
|
export const typeLabel = (type: TitleType): string => {
|
|
switch (type) {
|
|
case "hot":
|
|
return "爆款"
|
|
case "normal":
|
|
return "常规"
|
|
case "creative":
|
|
return "创意"
|
|
}
|
|
}
|
|
|
|
/** 后端 TitleItem → 前端 TitleData 映射 */
|
|
export const toTitleData = (item: TitleItem): TitleData => ({
|
|
id: item.id,
|
|
content: item.content,
|
|
type: (item.category as TitleType) || "normal",
|
|
industry: "general",
|
|
category: item.category || "未分类",
|
|
usageCount: 0,
|
|
isFavorited: false,
|
|
createdAt: item.created_at?.slice(0, 10) || "",
|
|
})
|
|
|
|
/** 复制文本到剪贴板 */
|
|
export const copyToClipboard = async (text: string): Promise<boolean> => {
|
|
try {
|
|
await navigator.clipboard.writeText(text)
|
|
return true
|
|
} catch {
|
|
/* 降级方案 */
|
|
const textarea = document.createElement("textarea")
|
|
textarea.value = text
|
|
textarea.style.position = "fixed"
|
|
textarea.style.opacity = "0"
|
|
document.body.appendChild(textarea)
|
|
textarea.select()
|
|
try {
|
|
document.execCommand("copy")
|
|
return true
|
|
} catch {
|
|
return false
|
|
} finally {
|
|
document.body.removeChild(textarea)
|
|
}
|
|
}
|
|
}
|