Files
cunkebao_v3/Touchkebao/src/utils/postApp.ts
超级老白兔 9b3181576f feat: 初始化项目基础架构与核心功能
添加项目基础文件结构、路由配置、API接口和核心组件
实现登录认证、权限控制、WebSocket通信等基础功能
引入antd-mobile UI组件库和Vite构建工具
配置TypeScript、ESLint、Prettier等开发环境
添加移动端适配方案和全局样式
完成首页、工作台、个人中心等基础页面框架
2025-09-11 15:00:57 +08:00

73 lines
1.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

export interface Message {
type: number; // 数据类型0数据交互 1App功能调用
data: any;
}
export const TYPE_EMUE = {
CONNECT: 0,
DATA: 1,
FUNCTION: 2,
CONFIG: 3,
};
// 向 App 发送消息
export const sendMessageToParent = (message: any, type: number) => {
const params: Message = {
type: type,
data: message,
};
if (window.uni && window.uni.postMessage) {
try {
window.uni.postMessage({
data: params,
});
console.log("[存客宝]SendMessage=>\n" + JSON.stringify(params));
} catch (e) {
console.error(
"[存客宝]SendMessage=>\n" + JSON.stringify(params) + "发送失败:",
e,
);
}
} else {
console.error(
"[存客宝]SendMessage=>\n" + JSON.stringify(params) + "无法发送消息",
);
}
};
// 解析 URL 参数中的消息
export const parseUrlMessage = (): Promise<any> => {
return new Promise((resolve, reject) => {
const search = window.location.search.substring(1);
let messageParam = null;
if (search) {
const pairs = search.split("&");
for (const pair of pairs) {
const [key, value] = pair.split("=");
if (key === "message" && value) {
messageParam = decodeURIComponent(value);
break;
}
}
}
if (messageParam) {
try {
const message = JSON.parse(decodeURIComponent(messageParam));
console.log("[存客宝]ReceiveMessage=>\n" + JSON.stringify(message));
resolve(message);
// 清除URL中的message参数
const newUrl =
window.location.pathname +
window.location.search
.replace(/[?&]message=[^&]*/, "")
.replace(/^&/, "?");
window.history.replaceState({}, "", newUrl);
} catch (e) {
console.error("解析URL消息失败:", e);
reject(e);
}
}
reject(null);
});
};