fix(miniapp): add missing uuid and message-display utils
Commit the utility modules required by api.js, messages.js, and chat page
so WeChat DevTools can resolve require('./uuid') at runtime.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+53
-14
@@ -1,4 +1,5 @@
|
||||
const SESSION_KEY = 'tkmind_session';
|
||||
const { createUuid } = require('./uuid');
|
||||
|
||||
let apiBaseUrl = '';
|
||||
let cookie = '';
|
||||
@@ -15,6 +16,36 @@ function getMiniProgramAppId() {
|
||||
}
|
||||
}
|
||||
|
||||
function isTouristMode() {
|
||||
const appId = getMiniProgramAppId();
|
||||
return !appId || appId === 'touristappid';
|
||||
}
|
||||
|
||||
function getApiBaseUrl() {
|
||||
return apiBaseUrl;
|
||||
}
|
||||
|
||||
function formatRequestError(res, url) {
|
||||
const status = Number(res.statusCode || 0);
|
||||
const code = res.data?.error?.code || '';
|
||||
const serverMessage = res.data?.message || res.data?.error?.message || '';
|
||||
if (status === 403 && code === 'csrf_failed') {
|
||||
if (/m\.tkmind\.cn/i.test(apiBaseUrl)) {
|
||||
return '线上 Portal 尚未部署小程序登录接口(403 来源校验失败)。请先用账号密码登录,或改用本地 Portal 联调。';
|
||||
}
|
||||
return serverMessage || '请求被服务器拒绝(来源校验失败)';
|
||||
}
|
||||
if (status === 404) {
|
||||
return '登录接口不存在,请重启本地 Portal(pnpm dev)后再试';
|
||||
}
|
||||
if (serverMessage) return serverMessage;
|
||||
if (status === 503) return '服务暂不可用,请稍后重试或使用账号密码登录';
|
||||
if (status === 405) {
|
||||
return `接口不支持当前请求方法,请检查 API_BASE_URL 是否指向 Portal:${url}`;
|
||||
}
|
||||
return `请求失败 (${status || 'network'})`;
|
||||
}
|
||||
|
||||
function getStoredSession() {
|
||||
const session = wx.getStorageSync(SESSION_KEY);
|
||||
if (session && typeof session === 'object') {
|
||||
@@ -60,7 +91,7 @@ function request(path, options = {}) {
|
||||
wx.request({
|
||||
url,
|
||||
method: options.method || 'GET',
|
||||
data: options.body || undefined,
|
||||
data: options.body == null ? undefined : options.body,
|
||||
header,
|
||||
timeout: options.timeout || 20000,
|
||||
success(res) {
|
||||
@@ -75,12 +106,7 @@ function request(path, options = {}) {
|
||||
resolve(res.data);
|
||||
return;
|
||||
}
|
||||
const message =
|
||||
res.data?.message ||
|
||||
res.data?.error?.message ||
|
||||
(status === 405
|
||||
? `接口不支持当前请求方法,请检查 API_BASE_URL 是否指向 Portal 服务:${url}`
|
||||
: `请求失败 (${status || 'network'})`);
|
||||
const message = formatRequestError(res, url);
|
||||
const error = new Error(message);
|
||||
error.status = status;
|
||||
error.body = res.data;
|
||||
@@ -133,9 +159,10 @@ function wxLogin() {
|
||||
}
|
||||
|
||||
async function loginWithWechatCode(loginPath) {
|
||||
const appId = getMiniProgramAppId();
|
||||
if (!appId || appId === 'touristappid') {
|
||||
throw new Error('当前是游客模式,微信一键登录需要在 project.config.json 配置真实小程序 AppID。');
|
||||
if (isTouristMode()) {
|
||||
throw new Error(
|
||||
'当前是游客模式(无 AppID 关联)。请在微信开发者工具用 AppID wx3e79ecd530c88da4 重新导入项目,并确认登录 DevTools 的微信号已是该小程序管理员。'
|
||||
);
|
||||
}
|
||||
const code = await wxLogin();
|
||||
const result = await portalRequest(loginPath, {
|
||||
@@ -164,16 +191,25 @@ async function loadSession(sessionId) {
|
||||
return apiRequest(`/sessions/${encodeURIComponent(sessionId)}`);
|
||||
}
|
||||
|
||||
async function createAgentRun(input) {
|
||||
return apiRequest('/agent/runs', {
|
||||
async function createAgentRun(input = {}) {
|
||||
const body = {
|
||||
...input,
|
||||
request_id: input.request_id || createUuid(),
|
||||
};
|
||||
if (!body.session_id) {
|
||||
delete body.session_id;
|
||||
}
|
||||
const result = await apiRequest('/agent/runs', {
|
||||
method: 'POST',
|
||||
body: input,
|
||||
body,
|
||||
timeout: 60000
|
||||
});
|
||||
return result?.run ?? result;
|
||||
}
|
||||
|
||||
async function getAgentRun(runId) {
|
||||
return apiRequest(`/agent/runs/${encodeURIComponent(runId)}`);
|
||||
const result = await apiRequest(`/agent/runs/${encodeURIComponent(runId)}`);
|
||||
return result?.run ?? result;
|
||||
}
|
||||
|
||||
async function listMindSpacePages(options = {}) {
|
||||
@@ -186,6 +222,9 @@ async function listMindSpacePages(options = {}) {
|
||||
|
||||
module.exports = {
|
||||
setApiBaseUrl,
|
||||
getApiBaseUrl,
|
||||
getMiniProgramAppId,
|
||||
isTouristMode,
|
||||
getStoredSession,
|
||||
saveSession,
|
||||
clearSession,
|
||||
|
||||
Reference in New Issue
Block a user