125 lines
3.5 KiB
Plaintext
125 lines
3.5 KiB
Plaintext
import { apiPublishedAgentInfo } from "@/servers/agentDev";
|
|
import { HideChatAreaEnum, ExpandPageAreaEnum } from "@/types/enums/agent";
|
|
import { API_BASE_URL } from "@/constants/config";
|
|
import { SUCCESS_CODE } from "@/constants/codes.constants";
|
|
import { apiUserTicketCreate } from "@/servers/agentDev";
|
|
import { getCurrentPageFullPath } from "@/utils/common";
|
|
import { t } from "@/utils/i18n";
|
|
|
|
/**
|
|
* 获取ticket
|
|
*/
|
|
export const getTicket = async (): Promise<string> => {
|
|
const ticketResult = await apiUserTicketCreate();
|
|
let _ticket: string = "";
|
|
if (ticketResult.code === SUCCESS_CODE && ticketResult.data) {
|
|
_ticket = ticketResult.data;
|
|
}
|
|
return _ticket;
|
|
};
|
|
|
|
/**
|
|
* 判断是否是根页面或id页面
|
|
* @param str
|
|
* @returns
|
|
*/
|
|
export const isRootOrId = (str: string) => {
|
|
return str === "/" || /^\/\?id=\d+$/.test(str);
|
|
};
|
|
|
|
/**
|
|
* 获取当前页面地址不包含url参数 H5 || WEB
|
|
*/
|
|
export const getCurrentPagePath = () => {
|
|
// #ifdef H5 || WEB
|
|
const currentUrl = getCurrentPageFullPath()?.split("#")?.[1] || "";
|
|
return currentUrl === "/" ? "/pages/index/index" : currentUrl;
|
|
// #endif
|
|
|
|
// #ifdef MP-WEIXIN
|
|
return "/" + getCurrentPageFullPath();
|
|
// #endif
|
|
};
|
|
|
|
/**
|
|
* 将ticket添加到url并跳转至webview页面
|
|
* @param url
|
|
* @param jump_type
|
|
* @returns
|
|
*/
|
|
export const addTicketAndJumpToWebview = async (
|
|
url: string,
|
|
jump_type: "inner" | "outer" = "inner",
|
|
title: string = "",
|
|
): Promise<void> => {
|
|
const resolvedTitle = title || t("Mobile.Common.appName");
|
|
const andStr = url.includes("?") ? "&" : "?";
|
|
const baseWebviewUrl = `${API_BASE_URL}${url}${andStr}title=${resolvedTitle}`;
|
|
let webviewUrl = "";
|
|
// #ifdef H5 || WEB
|
|
// 当前页面地址
|
|
const backUrl = getCurrentPageFullPath()?.split("#")?.[1] || "";
|
|
// 保存需要返回的地址在storage中
|
|
uni.setStorageSync("backUrl", backUrl);
|
|
webviewUrl = encodeURIComponent(baseWebviewUrl);
|
|
// #endif
|
|
|
|
// #ifdef MP-WEIXIN
|
|
const _ticket = await getTicket();
|
|
webviewUrl = encodeURIComponent(
|
|
baseWebviewUrl + `&_ticket=${_ticket}&jump_type=${jump_type}`,
|
|
);
|
|
// #endif
|
|
|
|
uni.navigateTo({
|
|
url: `/subpackages/pages/webview/webview?url=${webviewUrl}`,
|
|
});
|
|
};
|
|
|
|
// 跳转至智能体详情页面
|
|
export const jumpToAgentDetailPage = async (
|
|
agentId: number,
|
|
conversationId: number = null,
|
|
agentType: "ChatBot" | "PageApp" | "TaskAgent" = "ChatBot",
|
|
title: string = "",
|
|
) => {
|
|
const resolvedTitle = title || t("Mobile.Common.appName");
|
|
let url = `/subpackages/pages/agent-detail/agent-detail?id=${agentId}`;
|
|
if (conversationId) {
|
|
url = url + "&conversationId=" + conversationId;
|
|
}
|
|
|
|
// 问答型智能体、任务型智能体直接跳转至智能体详情页面
|
|
if (agentType === "ChatBot" || agentType === "TaskAgent") {
|
|
uni.navigateTo({ url });
|
|
return;
|
|
}
|
|
|
|
if (agentType === "PageApp") {
|
|
try {
|
|
uni.showLoading({ title: t("Mobile.Page.loading") });
|
|
const { code, data, message } = await apiPublishedAgentInfo(agentId);
|
|
if (code === SUCCESS_CODE) {
|
|
await addTicketAndJumpToWebview(
|
|
data.pageHomeIndex,
|
|
"inner",
|
|
resolvedTitle,
|
|
);
|
|
}
|
|
} finally {
|
|
uni.hideLoading();
|
|
}
|
|
}
|
|
};
|
|
|
|
// 判断当前页面是否是tab页面
|
|
export const isTabPage = (url: string) => {
|
|
const tabPageList = [
|
|
"/pages/index/index", // 首页
|
|
"/pages/agent-union-record/agent-union-record", // 智联录
|
|
"/pages/agent-list/agent-list", // 智能体
|
|
"/pages/page-app/page-app", // 应用
|
|
];
|
|
return tabPageList.includes(url);
|
|
};
|