Files
cunkebao_v3/Appbuild/unpackage/dist/dev/app-plus/app-service.js

1094 lines
40 KiB
JavaScript
Raw Normal View History

if (typeof Promise !== "undefined" && !Promise.prototype.finally) {
Promise.prototype.finally = function(callback) {
const promise = this.constructor;
return this.then(
(value) => promise.resolve(callback()).then(() => value),
(reason) => promise.resolve(callback()).then(() => {
throw reason;
})
);
};
}
;
if (typeof uni !== "undefined" && uni && uni.requireGlobal) {
const global = uni.requireGlobal();
ArrayBuffer = global.ArrayBuffer;
Int8Array = global.Int8Array;
Uint8Array = global.Uint8Array;
Uint8ClampedArray = global.Uint8ClampedArray;
Int16Array = global.Int16Array;
Uint16Array = global.Uint16Array;
Int32Array = global.Int32Array;
Uint32Array = global.Uint32Array;
Float32Array = global.Float32Array;
Float64Array = global.Float64Array;
BigInt64Array = global.BigInt64Array;
BigUint64Array = global.BigUint64Array;
}
;
if (uni.restoreGlobal) {
uni.restoreGlobal(Vue, weex, plus, setTimeout, clearTimeout, setInterval, clearInterval);
}
(function(vue) {
"use strict";
function formatAppLog(type, filename, ...args) {
if (uni.__log__) {
uni.__log__(type, filename, ...args);
} else {
console[type].apply(console, [...args, filename]);
}
}
const config = {
// 目标网站配置
webConfig: {
// 主要目标网站URL请替换为您的实际网站
mainUrl: "https://kr-op.quwanzhi.com",
// // 备用网站URL如果主网站无法访问
fallbackUrl: "https://kr-op.quwanzhi.com",
// 测试页面URL用于开发测试
testUrl: "https://kr-op.quwanzhi.com/test/postMessage",
// testUrl: "http://localhost:3000/test/postMessage",
// 是否启用HTTPS强制跳转
forceHttps: false,
// 是否允许跨域请求
allowCrossOrigin: true
},
// 应用配置
appConfig: {
// 应用名称
appName: "存客宝",
// 应用版本
version: "1.0.0",
// 是否启用超时检测
enableTimeout: false,
// 设为 false 禁用超时检测
// 加载超时时间(毫秒)- 设为0禁用超时检测
loadingTimeout: 3e4,
// 增加到30秒
// 是否启用调试模式
debug: true,
// 开发时设为 true生产时设为 false
// 是否使用测试页面
useTestPage: true
// 开发时设为 true生产时设为 false
},
// 通信配置
communication: {
// 消息类型定义
messageTypes: {
GET_USER_INFO: "getUserInfo",
GET_DEVICE_INFO: "getDeviceInfo",
NAVIGATE: "navigate",
SHARE: "share",
PAYMENT: "payment",
TOAST: "toast",
ALERT: "alert",
CONFIRM: "confirm"
},
// 允许的域名白名单
allowedDomains: ["kr-op.quwanzhi.com", "quwanzhi.com", "localhost"]
},
// 用户配置
userConfig: {
// 默认用户信息
defaultUser: {
id: "user123",
name: "测试用户",
avatar: "https://example.com/avatar.jpg",
phone: "13800138000",
email: "test@example.com"
}
}
};
const _export_sfc = (sfc, props) => {
const target = sfc.__vccOpts || sfc;
for (const [key, val] of props) {
target[key] = val;
}
return target;
};
const _sfc_main$1 = {
data() {
return {
config,
currentUrl: "",
// 初始化为空字符串
loading: false,
error: "",
loadingTimer: null,
messageQueue: [],
// 返回键监听相关
backButtonListener: null,
backButtonCount: 0,
backButtonTimer: null,
canExit: false,
lastBackTime: 0,
// 返回键状态管理
iframeCanGoBack: false,
backTimeoutId: null,
isProcessingBack: false
};
},
onLoad() {
this.initApp();
this.initBackButtonListener();
},
onShow() {
this.resumeMessageQueue();
this.resumeBackButtonListener();
},
onHide() {
formatAppLog("log", "at pages/index/index.vue:63", "WebView页面隐藏");
this.pauseBackButtonListener();
},
onUnload() {
this.cleanup();
this.removeBackButtonListener();
},
methods: {
// 获取初始URL
getInitialUrl() {
formatAppLog("log", "at pages/index/index.vue:74", "获取初始URLconfig:", this.config);
if (this.config && this.config.appConfig && this.config.appConfig.useTestPage) {
return this.config.webConfig.testUrl;
}
return this.config.webConfig.mainUrl;
},
// 初始化应用
initApp() {
formatAppLog("log", "at pages/index/index.vue:83", "UniApp: 开始初始化应用");
this.currentUrl = this.getInitialUrl();
formatAppLog("log", "at pages/index/index.vue:86", "UniApp: 设置URL:", this.currentUrl);
this.setupLoadingTimeout();
this.initMessageHandler();
formatAppLog("log", "at pages/index/index.vue:92", "UniApp: 立即注入桥接代码");
this.injectBridgeCode();
},
// 设置加载超时
setupLoadingTimeout() {
if (this.loadingTimer) {
clearTimeout(this.loadingTimer);
}
if (!this.config.appConfig.enableTimeout) {
formatAppLog("log", "at pages/index/index.vue:105", "超时检测已禁用");
return;
}
if (this.config.appConfig.loadingTimeout > 0) {
formatAppLog("log", "at pages/index/index.vue:110", "设置加载超时:", this.config.appConfig.loadingTimeout + "ms");
this.loadingTimer = setTimeout(() => {
if (this.loading) {
formatAppLog("warn", "at pages/index/index.vue:113", "页面加载超时,当前状态:", this.loading);
this.handleTimeout();
}
}, this.config.appConfig.loadingTimeout);
}
},
// 处理加载超时
handleTimeout() {
formatAppLog("warn", "at pages/index/index.vue:122", "触发页面加载超时处理");
this.error = "页面加载超时,请检查网络连接";
this.loading = false;
if (this.loadingTimer) {
clearTimeout(this.loadingTimer);
this.loadingTimer = null;
}
},
// 初始化消息处理器
initMessageHandler() {
formatAppLog("log", "at pages/index/index.vue:134", "UniApp: 初始化消息处理器");
window.addEventListener("message", (event) => {
formatAppLog("log", "at pages/index/index.vue:137", "UniApp: 收到window.message事件:", event.data);
this.handleIframeMessage(event);
});
},
// 设置WebView桥接
setupWebViewBridge() {
this.injectBridgeCode();
},
// 注入桥接代码
injectBridgeCode() {
formatAppLog("log", "at pages/index/index.vue:150", "UniApp: 开始注入桥接代码");
const bridgeCode = `
// UniApp WebView 桥接代码
__f__('log','at pages/index/index.vue:154','开始注入UniApp桥接代码...');
// 检查是否已经存在桥接
if (window.uniAppBridge) {
__f__('log','at pages/index/index.vue:158','UniApp桥接已存在跳过注入');
return;
}
window.uniAppBridge = {
// 发送消息到UniApp
postMessage: function(type, data) {
__f__('log','at pages/index/index.vue:165','UniApp桥接发送消息:', type, data);
window.parent.postMessage({
type: type,
data: data,
timestamp: Date.now()
}, '*');
},
// 获取用户信息
getUserInfo: function() {
__f__('log','at pages/index/index.vue:175','UniApp桥接: 请求用户信息');
this.postMessage('getUserInfo', {});
},
// 获取设备信息
getDeviceInfo: function() {
__f__('log','at pages/index/index.vue:181','UniApp桥接: 请求设备信息');
this.postMessage('getDeviceInfo', {});
},
// 显示Toast
showToast: function(message, duration = 2000) {
__f__('log','at pages/index/index.vue:187','UniApp桥接: 显示Toast', message, duration);
this.postMessage('toast', { message, duration });
},
// 显示Alert
showAlert: function(title, content) {
__f__('log','at pages/index/index.vue:193','UniApp桥接: 显示Alert', title, content);
this.postMessage('alert', { title, content });
},
// 显示Confirm
showConfirm: function(title, content) {
__f__('log','at pages/index/index.vue:199','UniApp桥接: 显示Confirm', title, content);
this.postMessage('confirm', { title, content });
},
// 分享
share: function(data) {
__f__('log','at pages/index/index.vue:205','UniApp桥接: 分享', data);
this.postMessage('share', data);
},
// 支付
payment: function(data) {
__f__('log','at pages/index/index.vue:211','UniApp桥接: 支付', data);
this.postMessage('payment', data);
},
// 页面导航
navigate: function(url) {
__f__('log','at pages/index/index.vue:217','UniApp桥接: 导航', url);
this.postMessage('navigate', { url });
},
// 自定义消息
sendCustomMessage: function(type, data) {
__f__('log','at pages/index/index.vue:223','UniApp桥接: 自定义消息', type, data);
this.postMessage(type, data);
},
// 页面准备就绪
notifyPageReady: function(data) {
__f__('log','at pages/index/index.vue:229','UniApp桥接: 页面准备就绪', data);
this.postMessage('pageReady', data);
},
// 处理返回键
handleBackButton: function(action) {
__f__('log','at pages/index/index.vue:235','UniApp桥接: 处理返回键', action);
switch(action) {
case 'tryBack':
// 尝试iframe内部返回
this.tryIframeBack();
break;
case 'showExitConfirm':
// 在 web-view 内显示退出确认
if (confirm('确定要退出应用吗?')) {
this.postMessage('exitApp', {});
}
break;
case 'exitApp':
// 通知 UniApp 退出应用
this.postMessage('exitApp', {});
break;
}
},
// 尝试iframe内部返回
tryIframeBack: function() {
__f__('log','at pages/index/index.vue:257','UniApp桥接: 尝试iframe内部返回');
try {
// 检查是否可以返回
if (window.history && window.history.length > 1) {
__f__('log','at pages/index/index.vue:262','iframe可以返回执行返回操作');
window.history.back();
// 通知UniApp返回成功
this.postMessage('backSuccess', { canGoBack: true });
} else {
__f__('log','at pages/index/index.vue:268','iframe无法返回通知UniApp');
this.postMessage('backFailed', {
canGoBack: false,
reason: 'no_history'
});
}
} catch (error) {
__f__('error','at pages/index/index.vue:275','iframe返回操作失败:', error);
this.postMessage('backFailed', {
canGoBack: false,
reason: 'error',
error: error.message
});
}
},
// 监听返回键事件
listenBackButton: function() {
// 监听浏览器的后退事件
window.addEventListener('popstate', (event) => {
__f__('log','at pages/index/index.vue:288','UniApp桥接: 检测到 popstate 事件');
this.postMessage('backButton', { action: 'popstate' });
});
// 监听键盘事件
document.addEventListener('keydown', (event) => {
if (event.key === 'Backspace' || event.keyCode === 8) {
__f__('log','at pages/index/index.vue:295','UniApp桥接: 检测到返回键');
this.postMessage('backButton', { action: 'keydown' });
}
});
// 监听页面可见性变化
document.addEventListener('visibilitychange', (event) => {
__f__('log','at pages/index/index.vue:302','UniApp桥接: 页面可见性变化', document.hidden);
if (!document.hidden) {
// 页面重新可见时,检查是否可以返回
this.checkBackStatus();
}
});
// 监听页面加载完成
window.addEventListener('load', (event) => {
__f__('log','at pages/index/index.vue:311','UniApp桥接: 页面加载完成');
this.checkBackStatus();
});
},
// 检查返回状态
checkBackStatus: function() {
try {
const canGoBack = window.history && window.history.length > 1;
__f__('log','at pages/index/index.vue:320','UniApp桥接: 检查返回状态, canGoBack:', canGoBack);
// 通知UniApp当前返回状态
this.postMessage('backStatus', {
canGoBack: canGoBack,
historyLength: window.history ? window.history.length : 0,
currentUrl: window.location.href
});
} catch (error) {
__f__('error','at pages/index/index.vue:329','UniApp桥接: 检查返回状态失败:', error);
}
}
};
// 监听来自UniApp的消息
window.addEventListener('message', function(event) {
__f__('log','at pages/index/index.vue:336','UniApp桥接收到消息:', event.data);
if (event.data && event.data.type) {
// 触发自定义事件
const customEvent = new CustomEvent('uniAppMessage', {
detail: event.data
});
window.dispatchEvent(customEvent);
}
});
__f__('log','at pages/index/index.vue:346','UniApp桥接代码注入成功');
// 通知UniApp页面已加载完成
window.parent.postMessage({
type: 'pageLoaded',
data: { url: window.location.href },
timestamp: Date.now()
}, '*');
// 初始化返回键监听
window.uniAppBridge.listenBackButton();
`;
formatAppLog("log", "at pages/index/index.vue:360", "UniApp: 立即注入桥接代码");
this.evalJS(bridgeCode);
setTimeout(() => {
formatAppLog("log", "at pages/index/index.vue:365", "UniApp: 延迟500ms后再次注入");
this.evalJS(bridgeCode);
}, 500);
setTimeout(() => {
formatAppLog("log", "at pages/index/index.vue:371", "UniApp: 延迟1000ms后备用注入");
this.evalJS(bridgeCode);
}, 1e3);
},
// 执行JavaScript代码
evalJS(code) {
formatAppLog("log", "at pages/index/index.vue:378", "UniApp: 执行JavaScript代码");
formatAppLog("log", "at pages/index/index.vue:379", "UniApp: 当前平台:", uni.getSystemInfoSync().platform);
const webview = this.$refs.webview;
formatAppLog("log", "at pages/index/index.vue:382", "UniApp: webview引用:", webview);
if (webview && webview.evalJS) {
try {
webview.evalJS(code);
formatAppLog("log", "at pages/index/index.vue:389", "UniApp: JavaScript代码执行成功 (App环境)");
} catch (error) {
formatAppLog("error", "at pages/index/index.vue:391", "UniApp: JavaScript代码执行失败:", error);
}
} else {
formatAppLog("warn", "at pages/index/index.vue:394", "UniApp: webview或evalJS方法不存在 (App环境)");
this.tryAlternativeInjection(code);
}
},
// 备用注入方案
tryAlternativeInjection(code) {
formatAppLog("log", "at pages/index/index.vue:413", "UniApp: 尝试备用注入方案");
try {
window.postMessage({
type: "injectCode",
data: { code },
timestamp: Date.now()
}, "*");
formatAppLog("log", "at pages/index/index.vue:421", "UniApp: 备用方案1执行成功");
} catch (error) {
formatAppLog("error", "at pages/index/index.vue:423", "UniApp: 备用方案1失败:", error);
try {
const webview = this.$refs.webview;
if (webview && webview.postMessage) {
webview.postMessage({
type: "injectCode",
data: { code }
});
formatAppLog("log", "at pages/index/index.vue:433", "UniApp: 备用方案2执行成功");
} else {
formatAppLog("error", "at pages/index/index.vue:435", "UniApp: 备用方案2失败 - webview.postMessage不存在");
}
} catch (error2) {
formatAppLog("error", "at pages/index/index.vue:438", "UniApp: 备用方案2失败:", error2);
}
}
},
// 处理web-view组件的消息
handleMessage(event) {
formatAppLog("log", "at pages/index/index.vue:445", "收到web-view消息:", event.detail);
try {
const data = event.detail.data;
if (data && data.type) {
this.processMessage(data);
}
} catch (error) {
formatAppLog("error", "at pages/index/index.vue:452", "处理消息失败:", error);
}
},
// 处理iframe消息
handleIframeMessage(event) {
formatAppLog("log", "at pages/index/index.vue:458", "收到iframe消息:", event.data);
try {
if (event.data && event.data.type) {
if (event.data.type === "pageLoaded") {
formatAppLog("log", "at pages/index/index.vue:463", "收到页面加载完成消息:", event.data.data);
if (this.loadingTimer) {
clearTimeout(this.loadingTimer);
this.loadingTimer = null;
}
this.loading = false;
this.processMessageQueue();
return;
}
this.processMessage(event.data);
}
} catch (error) {
formatAppLog("error", "at pages/index/index.vue:478", "处理iframe消息失败:", error);
}
},
// 处理消息
processMessage(data) {
formatAppLog("log", "at pages/index/index.vue:484", "UniApp处理消息:", data.type, data);
switch (data.type) {
case this.config.communication.messageTypes.GET_USER_INFO:
formatAppLog("log", "at pages/index/index.vue:488", "UniApp: 处理获取用户信息请求");
this.sendUserInfo();
break;
case this.config.communication.messageTypes.GET_DEVICE_INFO:
formatAppLog("log", "at pages/index/index.vue:492", "UniApp: 处理获取设备信息请求");
this.sendDeviceInfo();
break;
case this.config.communication.messageTypes.NAVIGATE:
formatAppLog("log", "at pages/index/index.vue:496", "UniApp: 处理导航请求");
this.handleNavigation(data);
break;
case this.config.communication.messageTypes.SHARE:
formatAppLog("log", "at pages/index/index.vue:500", "UniApp: 处理分享请求");
this.handleShare(data);
break;
case this.config.communication.messageTypes.PAYMENT:
formatAppLog("log", "at pages/index/index.vue:504", "UniApp: 处理支付请求");
this.handlePayment(data);
break;
case this.config.communication.messageTypes.TOAST:
formatAppLog("log", "at pages/index/index.vue:508", "UniApp: 处理Toast请求");
this.handleToast(data);
break;
case this.config.communication.messageTypes.ALERT:
formatAppLog("log", "at pages/index/index.vue:512", "UniApp: 处理Alert请求");
this.handleAlert(data);
break;
case this.config.communication.messageTypes.CONFIRM:
formatAppLog("log", "at pages/index/index.vue:516", "UniApp: 处理Confirm请求");
this.handleConfirm(data);
break;
case "pageReady":
formatAppLog("log", "at pages/index/index.vue:520", "UniApp: 处理页面准备就绪消息");
break;
case "backButton":
formatAppLog("log", "at pages/index/index.vue:524", "UniApp: 处理返回键消息");
this.handleBackButtonMessage(data);
break;
case "backSuccess":
formatAppLog("log", "at pages/index/index.vue:528", "UniApp: 处理iframe返回成功消息");
this.handleBackSuccess(data);
break;
case "backFailed":
formatAppLog("log", "at pages/index/index.vue:532", "UniApp: 处理iframe返回失败消息");
this.handleBackFailed(data);
break;
case "backStatus":
formatAppLog("log", "at pages/index/index.vue:536", "UniApp: 处理iframe返回状态消息");
this.handleBackStatus(data);
break;
case "exitApp":
formatAppLog("log", "at pages/index/index.vue:540", "UniApp: 处理退出应用消息");
this.handleExitApp();
break;
default:
formatAppLog("log", "at pages/index/index.vue:544", "UniApp: 未知消息类型:", data.type);
}
},
// 发送用户信息到iframe
sendUserInfo() {
formatAppLog("log", "at pages/index/index.vue:550", "UniApp: 发送用户信息");
const userInfo = {
type: "userInfo",
data: this.config.userConfig.defaultUser
};
this.sendMessageToIframe(userInfo);
},
// 发送设备信息到iframe
sendDeviceInfo() {
formatAppLog("log", "at pages/index/index.vue:560", "UniApp: 发送设备信息");
const systemInfo = uni.getSystemInfoSync();
const deviceInfo = {
type: "deviceInfo",
data: {
platform: systemInfo.platform,
model: systemInfo.model,
version: systemInfo.version,
appVersion: this.config.appConfig.version,
appName: this.config.appConfig.appName,
screenWidth: systemInfo.screenWidth,
screenHeight: systemInfo.screenHeight,
statusBarHeight: systemInfo.statusBarHeight
}
};
this.sendMessageToIframe(deviceInfo);
},
// 处理导航
handleNavigation(data) {
if (data.url) {
formatAppLog("log", "at pages/index/index.vue:581", "导航到:", data.url);
}
},
// 处理分享
handleShare(data) {
formatAppLog("log", "at pages/index/index.vue:588", "UniApp: 处理分享请求", data);
uni.share({
provider: "weixin",
scene: "WXSceneSession",
type: 0,
href: data.url || this.currentUrl,
title: data.title || this.config.appConfig.appName,
summary: data.summary || "分享内容",
success: (res) => {
formatAppLog("log", "at pages/index/index.vue:597", "UniApp: 分享成功:", res);
this.sendToIframe("shareResult", { success: true });
},
fail: (err) => {
formatAppLog("error", "at pages/index/index.vue:601", "UniApp: 分享失败:", err);
this.sendToIframe("shareResult", { success: false, error: err });
}
});
},
// 处理支付
handlePayment(data) {
formatAppLog("log", "at pages/index/index.vue:609", "UniApp: 处理支付请求", data);
setTimeout(() => {
this.sendToIframe("paymentResult", {
success: true,
orderId: data.orderId || "order123"
});
}, 1e3);
},
// 处理Toast
handleToast(data) {
formatAppLog("log", "at pages/index/index.vue:622", "UniApp: 处理Toast请求", data);
uni.showToast({
title: data.message,
icon: "none",
duration: data.duration || 2e3
});
},
// 处理Alert
handleAlert(data) {
formatAppLog("log", "at pages/index/index.vue:632", "UniApp: 处理Alert请求", data);
uni.showModal({
title: data.title || "提示",
content: data.content,
showCancel: false
});
},
// 处理Confirm
handleConfirm(data) {
formatAppLog("log", "at pages/index/index.vue:642", "UniApp: 处理Confirm请求", data);
uni.showModal({
title: data.title || "确认",
content: data.content,
success: (res) => {
formatAppLog("log", "at pages/index/index.vue:647", "UniApp: Confirm结果:", res);
this.sendToIframe("confirmResult", {
confirmed: res.confirm
});
}
});
},
// 发送消息到iframe
sendMessageToIframe(message) {
this.messageQueue.push(message);
this.processMessageQueue();
},
// 处理消息队列
processMessageQueue() {
formatAppLog("log", "at pages/index/index.vue:663", "UniApp: 处理消息队列, 队列长度:", this.messageQueue.length, "加载状态:", this.loading);
if (this.messageQueue.length > 0 && !this.loading) {
const message = this.messageQueue.shift();
formatAppLog("log", "at pages/index/index.vue:666", "UniApp: 发送消息到iframe:", message);
this.evalJS(`
window.postMessage(${JSON.stringify(message)}, '*');
`);
}
},
// 恢复消息队列处理
resumeMessageQueue() {
this.processMessageQueue();
},
// 向iframe发送消息的通用方法
sendToIframe(type, data) {
const message = {
type,
data,
timestamp: Date.now()
};
this.sendMessageToIframe(message);
},
// 处理加载状态
handleLoading(event) {
formatAppLog("log", "at pages/index/index.vue:690", "WebView加载状态变化:", event.detail);
const newLoadingState = event.detail.loading;
formatAppLog("log", "at pages/index/index.vue:692", "加载状态从", this.loading, "变为", newLoadingState);
this.loading = newLoadingState;
if (!this.loading) {
formatAppLog("log", "at pages/index/index.vue:698", "页面加载完成,清除超时定时器");
if (this.loadingTimer) {
clearTimeout(this.loadingTimer);
this.loadingTimer = null;
}
this.injectBridgeCode();
this.processMessageQueue();
} else {
formatAppLog("log", "at pages/index/index.vue:710", "页面开始加载,设置超时检测");
this.setupLoadingTimeout();
}
},
// 处理错误
handleError(event) {
formatAppLog("error", "at pages/index/index.vue:717", "WebView错误:", event.detail);
this.error = "页面加载失败,请检查网络连接";
this.loading = false;
clearTimeout(this.loadingTimer);
},
// 重新加载页面
reloadPage() {
this.error = "";
this.loading = true;
this.setupLoadingTimeout();
const webview = this.$refs.webview;
if (webview) {
webview.reload();
}
},
// 切换到备用地址
switchToFallback() {
this.currentUrl = this.config.webConfig.fallbackUrl;
this.reloadPage();
},
// 清除超时
clearTimeout() {
formatAppLog("log", "at pages/index/index.vue:743", "手动清除超时");
if (this.loadingTimer) {
clearTimeout(this.loadingTimer);
this.loadingTimer = null;
}
this.error = "";
this.loading = false;
formatAppLog("log", "at pages/index/index.vue:750", "超时已清除");
},
// 动态设置web-view高度
setWebViewHeight(height) {
formatAppLog("log", "at pages/index/index.vue:755", "设置web-view高度:", height);
if (this.$refs.webview) {
this.$refs.webview.$el.style.height = height + "px";
this.$refs.webview.$el.className = "webview custom-height";
this.$nextTick(() => {
this.$refs.webview.$el.style.setProperty("height", height + "px", "important");
});
}
},
// 设置web-view为全屏
setWebViewFullscreen() {
const fullHeight = uni.getSystemInfoSync().windowHeight;
formatAppLog("log", "at pages/index/index.vue:773", "设置web-view全屏高度:", fullHeight);
this.setWebViewHeight(fullHeight);
},
// 设置web-view为部分高度
setWebViewPartialHeight(percentage = 0.8) {
const windowHeight = uni.getSystemInfoSync().windowHeight;
const partialHeight = windowHeight * percentage;
formatAppLog("log", "at pages/index/index.vue:781", "设置web-view部分高度:", partialHeight, "百分比:", percentage);
this.setWebViewHeight(partialHeight);
},
// 初始化返回键监听
initBackButtonListener() {
formatAppLog("log", "at pages/index/index.vue:787", "初始化返回键监听");
try {
uni.addInterceptor("navigateBack", {
invoke(e) {
formatAppLog("log", "at pages/index/index.vue:794", "拦截到返回事件:", e);
return false;
}
});
} catch (error) {
formatAppLog("warn", "at pages/index/index.vue:799", "addInterceptor 失败:", error);
}
this.setupPhysicalBackButton();
this.setupWebViewBackListener();
},
// 设置物理返回键监听
setupPhysicalBackButton() {
try {
plus.key.addEventListener("backbutton", (e) => {
formatAppLog("log", "at pages/index/index.vue:816", "检测到物理返回键事件:", e);
this.handleBackButton();
});
} catch (error) {
formatAppLog("warn", "at pages/index/index.vue:820", "物理返回键监听失败:", error);
}
},
// 设置 web-view 内返回监听
setupWebViewBackListener() {
window.addEventListener("popstate", (event) => {
formatAppLog("log", "at pages/index/index.vue:829", "检测到 popstate 事件:", event);
this.handleBackButton();
});
window.addEventListener("beforeunload", (event) => {
formatAppLog("log", "at pages/index/index.vue:835", "检测到 beforeunload 事件:", event);
this.handleBackButton();
});
},
// 处理返回键事件
handleBackButton() {
const now = Date.now();
const timeDiff = now - this.lastBackTime;
formatAppLog("log", "at pages/index/index.vue:845", "处理返回键事件, 时间差:", timeDiff, "ms, iframeCanGoBack:", this.iframeCanGoBack);
if (timeDiff > 2e3) {
this.backButtonCount = 0;
}
this.backButtonCount++;
this.lastBackTime = now;
if (this.isProcessingBack) {
formatAppLog("log", "at pages/index/index.vue:857", "正在处理返回,忽略新的返回键");
return;
}
if (this.backButtonCount === 1) {
if (this.iframeCanGoBack) {
this.tryIframeBack();
} else {
this.showExitConfirm();
}
} else if (this.backButtonCount >= 2) {
this.exitApp();
}
},
// 尝试iframe内部返回
tryIframeBack() {
formatAppLog("log", "at pages/index/index.vue:879", "尝试iframe内部返回");
this.isProcessingBack = true;
if (this.backTimeoutId) {
clearTimeout(this.backTimeoutId);
}
this.sendBackButtonMessage("tryBack");
this.evalJS(`
try {
// 检查是否可以返回
if (window.history && window.history.length > 1) {
__f__('log','at pages/index/index.vue:897','iframe可以返回执行返回操作');
window.history.back();
// 通知UniApp返回成功
window.parent.postMessage({
type: 'backSuccess',
data: { canGoBack: true },
timestamp: Date.now()
}, '*');
} else {
__f__('log','at pages/index/index.vue:907','iframe无法返回通知UniApp');
window.parent.postMessage({
type: 'backFailed',
data: { canGoBack: false, reason: 'no_history' },
timestamp: Date.now()
}, '*');
}
} catch (error) {
__f__('error','at pages/index/index.vue:915','iframe返回操作失败:', error);
window.parent.postMessage({
type: 'backFailed',
data: { canGoBack: false, reason: 'error', error: error.message },
timestamp: Date.now()
}, '*');
}
`);
this.backTimeoutId = setTimeout(() => {
if (this.backButtonCount === 1 && this.isProcessingBack) {
formatAppLog("log", "at pages/index/index.vue:927", "iframe返回超时显示退出确认");
this.isProcessingBack = false;
this.showExitConfirm();
}
}, 1500);
},
// 显示退出确认
showExitConfirm() {
formatAppLog("log", "at pages/index/index.vue:936", "显示退出确认");
uni.showModal({
title: "退出确认",
content: "确定要退出应用吗?",
confirmText: "退出",
cancelText: "取消",
success: (res) => {
if (res.confirm) {
this.exitApp();
}
}
});
this.sendBackButtonMessage("showExitConfirm");
},
// 退出应用
exitApp() {
formatAppLog("log", "at pages/index/index.vue:959", "退出应用");
try {
plus.runtime.quit();
} catch (error) {
formatAppLog("error", "at pages/index/index.vue:966", "退出应用失败:", error);
}
this.sendBackButtonMessage("exitApp");
},
// 发送返回键消息到 web-view
sendBackButtonMessage(action) {
if (this.$refs.webview) {
try {
this.$refs.webview.postMessage({
type: "backButton",
action,
timestamp: Date.now()
});
} catch (error) {
formatAppLog("error", "at pages/index/index.vue:985", "发送返回键消息失败:", error);
}
}
},
// 处理返回键消息
handleBackButtonMessage(data) {
formatAppLog("log", "at pages/index/index.vue:992", "处理返回键消息:", data);
switch (data.action) {
case "popstate":
case "keydown":
this.handleBackButton();
break;
default:
formatAppLog("log", "at pages/index/index.vue:1000", "未知的返回键动作:", data.action);
}
},
// 处理退出应用
handleExitApp() {
formatAppLog("log", "at pages/index/index.vue:1006", "处理退出应用");
try {
plus.runtime.quit();
} catch (error) {
formatAppLog("error", "at pages/index/index.vue:1012", "退出应用失败:", error);
}
},
// 处理iframe返回成功消息
handleBackSuccess(data) {
formatAppLog("log", "at pages/index/index.vue:1019", "处理iframe返回成功消息:", data);
this.isProcessingBack = false;
this.iframeCanGoBack = true;
this.backButtonCount = 0;
this.lastBackTime = 0;
if (this.backTimeoutId) {
clearTimeout(this.backTimeoutId);
this.backTimeoutId = null;
}
},
// 处理iframe返回失败消息
handleBackFailed(data) {
formatAppLog("log", "at pages/index/index.vue:1034", "处理iframe返回失败消息:", data);
this.isProcessingBack = false;
this.iframeCanGoBack = false;
this.backButtonCount = 0;
this.lastBackTime = 0;
if (this.backTimeoutId) {
clearTimeout(this.backTimeoutId);
this.backTimeoutId = null;
}
this.showExitConfirm();
},
// 处理iframe返回状态消息
handleBackStatus(data) {
formatAppLog("log", "at pages/index/index.vue:1052", "处理iframe返回状态消息:", data);
this.iframeCanGoBack = data.data.canGoBack;
formatAppLog("log", "at pages/index/index.vue:1054", "更新iframe返回状态:", this.iframeCanGoBack);
},
// 恢复返回键监听
resumeBackButtonListener() {
formatAppLog("log", "at pages/index/index.vue:1059", "恢复返回键监听");
this.backButtonCount = 0;
this.lastBackTime = 0;
},
// 暂停返回键监听
pauseBackButtonListener() {
formatAppLog("log", "at pages/index/index.vue:1066", "暂停返回键监听");
this.backButtonCount = 0;
},
// 移除返回键监听
removeBackButtonListener() {
formatAppLog("log", "at pages/index/index.vue:1072", "移除返回键监听");
try {
plus.key.removeEventListener("backbutton");
uni.removeInterceptor("navigateBack");
} catch (error) {
formatAppLog("warn", "at pages/index/index.vue:1082", "移除返回键监听失败:", error);
}
if (this.backButtonTimer) {
clearTimeout(this.backButtonTimer);
this.backButtonTimer = null;
}
},
// 清理资源
cleanup() {
if (this.loadingTimer) {
clearTimeout(this.loadingTimer);
}
if (this.backTimeoutId) {
clearTimeout(this.backTimeoutId);
this.backTimeoutId = null;
}
this.removeBackButtonListener();
}
}
};
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
return vue.openBlock(), vue.createElementBlock("view", { class: "container" }, [
vue.createCommentVNode(" 全屏web-view容器 "),
vue.createElementVNode("web-view", {
src: $data.currentUrl,
class: "webview",
ref: "webview",
onMessage: _cache[0] || (_cache[0] = (...args) => $options.handleMessage && $options.handleMessage(...args)),
onError: _cache[1] || (_cache[1] = (...args) => $options.handleError && $options.handleError(...args)),
onLoading: _cache[2] || (_cache[2] = (...args) => $options.handleLoading && $options.handleLoading(...args))
}, null, 40, ["src"]),
vue.createCommentVNode(" 加载状态 "),
$data.loading ? (vue.openBlock(), vue.createElementBlock("view", {
key: 0,
class: "loading-container"
}, [
vue.createElementVNode("view", { class: "loading-spinner" }),
vue.createElementVNode("text", { class: "loading-text" }, "加载中...")
])) : vue.createCommentVNode("v-if", true),
vue.createCommentVNode(" 错误状态 "),
$data.error ? (vue.openBlock(), vue.createElementBlock("view", {
key: 1,
class: "error-container"
}, [
vue.createElementVNode(
"text",
{ class: "error-text" },
vue.toDisplayString($data.error),
1
/* TEXT */
),
vue.createElementVNode("button", {
onClick: _cache[3] || (_cache[3] = (...args) => $options.reloadPage && $options.reloadPage(...args)),
class: "reload-btn"
}, "重新加载"),
vue.createElementVNode("button", {
onClick: _cache[4] || (_cache[4] = (...args) => $options.switchToFallback && $options.switchToFallback(...args)),
class: "fallback-btn"
}, "使用备用地址"),
vue.createElementVNode("button", {
onClick: _cache[5] || (_cache[5] = (...args) => $options.clearTimeout && $options.clearTimeout(...args)),
class: "clear-btn"
}, "清除超时")
])) : vue.createCommentVNode("v-if", true),
vue.createCommentVNode(" 调试信息(仅在调试模式下显示) "),
$data.config.appConfig.debug ? (vue.openBlock(), vue.createElementBlock("view", {
key: 2,
class: "debug-info"
}, [
vue.createElementVNode(
"text",
null,
"当前URL: " + vue.toDisplayString($data.currentUrl),
1
/* TEXT */
),
vue.createElementVNode(
"text",
null,
"加载状态: " + vue.toDisplayString($data.loading ? "加载中" : "已加载"),
1
/* TEXT */
)
])) : vue.createCommentVNode("v-if", true)
]);
}
const PagesIndexIndex = /* @__PURE__ */ _export_sfc(_sfc_main$1, [["render", _sfc_render], ["__file", "E:/Cunkebao/Cunkebao01/Appbuild/pages/index/index.vue"]]);
__definePage("pages/index/index", PagesIndexIndex);
const _sfc_main = {};
const App = /* @__PURE__ */ _export_sfc(_sfc_main, [["__file", "E:/Cunkebao/Cunkebao01/Appbuild/App.vue"]]);
function createApp() {
const app = vue.createVueApp(App);
return {
app
};
}
const { app: __app__, Vuex: __Vuex__, Pinia: __Pinia__ } = createApp();
uni.Vuex = __Vuex__;
uni.Pinia = __Pinia__;
__app__.provide("__globalStyles", __uniConfig.styles);
__app__._component.mpType = "app";
__app__._component.render = () => {
};
__app__.mount("#app");
})(Vue);