Files
kangyuan-mobile/App.vue
T

254 lines
6.3 KiB
Vue

<script setup>
import {
ref,
onMounted,
onBeforeMount,
onBeforeUnmount,
onBeforeUpdate
} from 'vue';
import {
onLoad,
onLaunch,
onShow,
onError
} from '@dcloudio/uni-app';
import {
ShoproInit
} from './sheep';
let globalData = ref({
statusBarHeight: 0, // 状态导航栏高度
navHeight: 0, // 总体高度
navigationBarHeight: 0, // 导航栏高度(标题栏高度)
windowHeight: 0,
});
const socket = () => {
let url = 'ws://192.168.101.229:48080/aishop/ws';
let socketTask = uni.connectSocket({
url: url,
success(res) {
console.log('连接成功', res);
},
fail() {},
});
socketTask.onOpen((res) => {
console.log('WebSocket 连接已打开');
});
socketTask.onMessage((res) => {
console.log('收到服务器消息', res);
});
socketTask.onClose((res) => {
console.log('WebSocket 已关闭!');
});
socketTask.onError((res) => {
console.log('WebSocket 错误!', res);
});
};
const parseTitleToProgress = (title) => {
const text = String(title || '');
const percentMatch = text.match(/(\d{1,3})%/);
const percent = percentMatch ? Number(percentMatch[1]) : null;
const cleanText = text.replace(/\s*\d{1,3}%\s*$/, '').trim() || text;
const normalizedPercent =
percent !== null && Number.isFinite(percent) ?
Math.max(0, Math.min(100, percent)) :
text.includes('安装') && !text.includes('下载') ? 100 : null;
return {
percent: normalizedPercent,
text: cleanText
};
};
const shouldUseUpdateOverlay = (title) => {
const text = String(title || '');
if (!text) return false;
if (/下载|安装|更新/.test(text) && (/%/.test(text) || /下载中|安装中/.test(text))) {
return true;
}
return false;
};
const ensureLoadingPatched = () => {
if (!uni || uni.__updateLoadingPatched) return;
const originalShowLoading = uni.showLoading;
const originalHideLoading = uni.hideLoading;
if (typeof originalShowLoading !== 'function' || typeof originalHideLoading !== 'function') {
uni.__updateLoadingPatched = true;
return;
}
const HOT_UPDATE_PROGRESS_EVENT = '__hot_update_progress__';
const canEmit = () => typeof uni !== 'undefined' && typeof uni.$emit === 'function';
const emitProgress = (payload) => {
if (!canEmit()) return false;
uni.__hotUpdatePendingProgress = payload;
uni.$emit(HOT_UPDATE_PROGRESS_EVENT, payload);
return true;
};
uni.showLoading = (options = {}) => {
const title = options?.title ?? '';
if (shouldUseUpdateOverlay(title) && canEmit()) {
const parsed = parseTitleToProgress(title);
if (!uni.__updateProgressShown) {
uni.__updateProgressShown = true;
emitProgress({
type: 'show',
text: parsed.text || '下载中...'
});
}
if (parsed.percent !== null) {
emitProgress({
type: 'set',
percent: parsed.percent,
text: parsed.text || ''
});
}
return;
}
return originalShowLoading.call(uni, options);
};
uni.hideLoading = () => {
if (uni.__updateProgressShown && canEmit()) {
uni.__updateProgressShown = false;
emitProgress({
type: 'hide'
});
return;
}
return originalHideLoading.call(uni);
};
uni.__updateLoadingPatched = true;
};
const waitForPlusRuntime = ({
timeoutMs = 5000,
intervalMs = 50
} = {}) =>
new Promise((resolve) => {
const ready = () => typeof plus !== 'undefined' && !!plus?.runtime;
if (ready()) {
resolve(true);
return;
}
const startedAt = Date.now();
const timer = setInterval(() => {
if (ready()) {
clearInterval(timer);
resolve(true);
return;
}
if (Date.now() - startedAt >= timeoutMs) {
clearInterval(timer);
resolve(false);
}
}, intervalMs);
});
const ensureGlobalHotUpdateChecked = async () => {
if (uni.__globalHotUpdateCheckedInApp) return;
uni.__globalHotUpdateCheckedInApp = true;
const ok = await waitForPlusRuntime();
if (!ok) return;
if (typeof uni.__globalHotUpdateCheck === 'function') {
await uni.__globalHotUpdateCheck({
force: true,
source: 'app'
});
}
};
onMounted(() => {
// #ifdef APP-PLUS
// plus.nativeUI.setUIStyle('dark');
// #endif
// #ifdef H5
// 以下是H5端的代码
// document.body.classList.remove('dark', 'light', 'auto');
// document.body.classList.add('dark');
// #endif
// 初始化全局数据
globalData.value = {
...globalData.value,
...uni.getStorageSync('globalData'),
};
globalData.value.statusBarHeight = uni.getSystemInfoSync()?.statusBarHeight;
globalData.value.windowHeight = uni.getSystemInfoSync()?.windowHeight;
ensureLoadingPatched();
ensureGlobalHotUpdateChecked();
// socket()
});
onLaunch(() => {
try {
uni.hideTabBar({
fail: () => {},
});
} catch (e) {}
// globalData.value.statusBarHeight = uni.getSystemInfoSync()?.statusBarHeight;
// globalData.value.windowHeight = uni.getSystemInfoSync()?.windowHeight;
// 获取微信胶囊的位置信息 width,height,top,right,left,bottom
let custom = {
top: 0,
height: 0,
};
// #ifdef MP-WEIXIN
custom = wx.getMenuButtonBoundingClientRect();
// #endif
// 导航栏高度(标题栏高度) = 胶囊高度 + (顶部距离 - 状态栏高度) * 2
globalData.value.navigationBarHeight =
custom.height + (custom.top - uni.getSystemInfoSync()?.statusBarHeight) * 2;
globalData.value.navHeight =
globalData.value.navigationBarHeight + uni.getSystemInfoSync()?.statusBarHeight;
uni.setStorageSync('globalData', globalData.value);
// 加载Shopro底层依赖
ShoproInit();
});
onShow(() => {
try {
uni.hideTabBar({
fail: () => {},
});
} catch (e) {}
// #ifdef APP-PLUS
// 获取urlSchemes参数
const args =
typeof plus !== 'undefined' && plus?.runtime ? plus.runtime.arguments : '';
if (args) {}
// 获取剪贴板
uni.getClipboardData({
success: (res) => {},
});
// #endif
});
if (typeof uni.onTabBarMidButtonTap === 'function') {
uni.onTabBarMidButtonTap(() => {
uni.navigateTo({
url: '/pages/home/seed',
});
});
}
</script>
<style lang="scss">
@import '@/sheep/scss/index.scss';
.uni-tabbar-border {
background-color: transparent !important;
/* 移除背景色 */
height: 0 !important;
/* 隐藏边框元素 */
border: none !important;
/* 确保无边框 */
}
.uni-app--showtopwindow uni-page-head {
display: none;
}
</style>