Files
kangyuan-mobile/App.vue
T
2026-05-26 15:52:30 +08:00

267 lines
6.2 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';
import {
gethotupdatelist
} from '@/sheep/api/index/index';
let globalData = ref({
statusBarHeight: 0, // 状态导航栏高度
navHeight: 0, // 总体高度
navigationBarHeight: 0, // 导航栏高度(标题栏高度)
windowHeight: 0,
});
let filePath = ref();
const wgtUp = async () => {
// console.log(plus.runtime);
let res = await gethotupdatelist();
if (res.code !== 0) return;
filePath.value = res.data.filePath;
let localVersion = uni.getSystemInfoSync().appWgtVersion;
if (res.data.versionNumber != localVersion) {
if (res.data.type == 'min') {
if (res.data.isForceUpdate) {
// 强制更新
downloadFile();
} else {
uni.showModal({
title: '提示',
content: '发现新的版本,是否更新?',
confirmColor: '#FA7E31',
success: (res) => {
if (res.confirm) {
downloadFile();
} else if (res.cancel) {
if (res.data.isForceUpdate) {
plus.runtime.quit(); //退出应用
}
}
},
});
}
} else {
uni.showModal({
title: '提示',
content: '发现新的版本,请到立即更新到最新版本',
confirmText: '立即更新',
cancelText: res.data.isForceUpdate ? '关闭' : '取消',
confirmColor: '#FA7E31',
success: (res) => {
if (res.confirm) {
updateApp(filePath.value);
} else if (res.cancel) {
if (res.data.isForceUpdate) {
plus.runtime.quit(); //退出应用
}
}
},
});
}
}
};
const updateApp = (url) => {
uni.showLoading({
title: '下载中',
});
uni.downloadFile({
url: url, //下载地址
success: (res) => {
console.log(res, '下载成功');
if (res.statusCode === 200) {
console.log('下载成功');
uni.hideLoading();
uni.showToast({
title: '下载成功',
icon: 'success',
});
uni.saveFile({
tempFilePath: res.tempFilePath,
success: function(res) {
uni.openDocument({
filePath: res.savedFilePath,
success: function(res) {
console.log(res, '打开安装包');
},
});
},
fail: (err) => {
console.log(err, '打开安装包-失败');
},
});
}
},
fail(e) {
uni.hideLoading();
uni.showToast({
title: '下载失败,请检查网络',
icon: 'none',
});
console.log(e);
},
});
};
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);
});
};
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;
// 热更新
// #ifdef APP-PLUS
wgtUp();
// #endif
// socket()
});
const downloadFile = () => {
let url = filePath.value;
uni.downloadFile({
url: url,
success: (downloadResult) => {
uni.showToast({
title: '更新中....',
icon: 'none',
});
if (downloadResult) {
if (downloadResult.statusCode === 200) {
plus.runtime.install(
downloadResult.tempFilePath, {
force: true,
},
function() {
uni.showModal({
title: '成功提示',
content: '热更新成功,请重启应用',
confirmColor: '#FA7E31',
showCancel: false,
success: function(res) {
if (res.confirm) {
plus.runtime.restart(); //自动重启软件
}
},
});
},
function(e) {
uni.showModal({
title: '失败提示',
content: e,
showCancel: false,
confirmColor: '#FA7E31',
});
},
);
}
}
},
});
};
onLaunch(() => {
// 隐藏原生导航栏 使用自定义底部导航
// uni.hideTabBar({
// fail: () => {},
// });
// 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(() => {
// #ifdef APP-PLUS
// 获取urlSchemes参数
const args = plus.runtime.arguments;
if (args) {}
// 获取剪贴板
uni.getClipboardData({
success: (res) => {},
});
// #endif
});
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>