feat:优化上线后的改造
This commit is contained in:
@@ -2,6 +2,56 @@ const GLOBAL_HIDDEN_KEY = "tb_qiaobao_hidden_all";
|
||||
const PAGE_HIDDEN_KEY = "tb_qiaobao_hidden_pages";
|
||||
const DOCK_EDGE_KEY = "tb_qiaobao_dock_edge";
|
||||
const POSITION_KEY = "tb_qiaobao_position";
|
||||
const FIRST_OPEN_KEY = "tb_qiaobao_ai_first_open_v1";
|
||||
const FIRST_LOGIN_PREFIX = "tb_qiaobao_ai_first_login_v1_";
|
||||
const CHAT_SESSION_KEY = "tb_qiaobao_ai_session_v1";
|
||||
const CHAT_HISTORY_PREFIX = "tb_qiaobao_ai_history_v1_";
|
||||
|
||||
// Memory-only flag: closing Qiaobao lasts until the App/WebView process restarts.
|
||||
// It is intentionally not written to storage so users can always recover it by
|
||||
// reopening the App.
|
||||
let sessionClosed = false;
|
||||
|
||||
function getChatHistoryKey(scope) {
|
||||
const safeScope = String(scope || "guest").replace(/[^a-zA-Z0-9_-]/g, "_");
|
||||
return `${CHAT_HISTORY_PREFIX}${safeScope}`;
|
||||
}
|
||||
|
||||
export function getQiaobaoChatSessionId() {
|
||||
let value = uni.getStorageSync(CHAT_SESSION_KEY);
|
||||
if (!value) {
|
||||
value = `qb-${Date.now()}-${Math.random().toString(36).slice(2, 10)}`;
|
||||
uni.setStorageSync(CHAT_SESSION_KEY, value);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
export function getQiaobaoChatHistory(scope = "guest") {
|
||||
const value = uni.getStorageSync(getChatHistoryKey(scope));
|
||||
return Array.isArray(value) ? value.slice(-30) : [];
|
||||
}
|
||||
|
||||
export function saveQiaobaoChatHistory(messages, scope = "guest") {
|
||||
const safe = Array.isArray(messages)
|
||||
? messages.filter((item) => item && ["user", "assistant"].includes(item.role) && item.content).slice(-30)
|
||||
: [];
|
||||
uni.setStorageSync(getChatHistoryKey(scope), safe);
|
||||
}
|
||||
|
||||
export function consumeQiaobaoAutoIntro(userId) {
|
||||
if (uni.getStorageSync(FIRST_OPEN_KEY) !== true) {
|
||||
uni.setStorageSync(FIRST_OPEN_KEY, true);
|
||||
return "first-open";
|
||||
}
|
||||
if (userId !== undefined && userId !== null && String(userId)) {
|
||||
const key = `${FIRST_LOGIN_PREFIX}${String(userId)}`;
|
||||
if (uni.getStorageSync(key) !== true) {
|
||||
uni.setStorageSync(key, true);
|
||||
return "first-login";
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
export function getQiaobaoDockEdge() {
|
||||
return uni.getStorageSync(DOCK_EDGE_KEY) === "left" ? "left" : "right";
|
||||
@@ -44,6 +94,15 @@ export function isQiaobaoHidden(pagePath = getCurrentPagePath()) {
|
||||
return hiddenAll === true || (Array.isArray(hiddenPages) && hiddenPages.includes(pagePath));
|
||||
}
|
||||
|
||||
export function isQiaobaoClosedForSession() {
|
||||
return sessionClosed;
|
||||
}
|
||||
|
||||
export function closeQiaobaoForSession() {
|
||||
sessionClosed = true;
|
||||
uni.$emit("qiaobao-visibility-change", { scope: "close" });
|
||||
}
|
||||
|
||||
export function hideQiaobaoForPage(pagePath = getCurrentPagePath(), edge = "right") {
|
||||
const hiddenPages = uni.getStorageSync(PAGE_HIDDEN_KEY);
|
||||
const nextPages = Array.isArray(hiddenPages) ? [...hiddenPages] : [];
|
||||
|
||||
Reference in New Issue
Block a user