118 lines
2.9 KiB
Vue
118 lines
2.9 KiB
Vue
<script>
|
||
import { getGlobalSocket } from '@/utils/socket'
|
||
import { emitAppReturnFromBackground } from '@/utils/backgroundRefreshBus.js'
|
||
|
||
// 记录上次隐藏时间的 storage key
|
||
const HIDE_TIME_KEY = 'app_last_hide_time'
|
||
|
||
export default {
|
||
onLaunch: function() {
|
||
console.log('App Launch')
|
||
// 不在这里初始化 AI Chat 连接,由各页面自行管理
|
||
},
|
||
onShow: function() {
|
||
console.log('App Show')
|
||
this.handleBackgroundReturn()
|
||
},
|
||
onHide: function() {
|
||
console.log('App Hide')
|
||
// 关闭所有 WebSocket 连接
|
||
this.closeWebSocket()
|
||
// 记录切到后台的时间,用于 onShow 时判断是否"从后台返回"
|
||
uni.setStorageSync(HIDE_TIME_KEY, Date.now())
|
||
},
|
||
methods: {
|
||
initWebSocket() {
|
||
const token = uni.getStorageSync('access_token')
|
||
if (token) {
|
||
console.log('初始化全局 WebSocket 连接')
|
||
const globalSocket = getGlobalSocket()
|
||
globalSocket.init(token)
|
||
}
|
||
},
|
||
closeWebSocket() {
|
||
console.log('关闭全局 WebSocket 连接')
|
||
const globalSocket = getGlobalSocket()
|
||
globalSocket.closeAll()
|
||
},
|
||
/**
|
||
* 处理"从后台切回前台"事件
|
||
* 通过 storage 中的上次隐藏时间判断本次 onShow 是否由后台返回触发
|
||
* 若是,则通知所有订阅了 useBackgroundRefresh 的页面执行刷新
|
||
*/
|
||
handleBackgroundReturn() {
|
||
const lastHide = uni.getStorageSync(HIDE_TIME_KEY) || 0
|
||
if (!lastHide) return
|
||
// 清理标记,避免下次普通 onShow(如首次启动)误触发
|
||
uni.removeStorageSync(HIDE_TIME_KEY)
|
||
// 时间异常保护
|
||
if (Date.now() - lastHide < 0) return
|
||
emitAppReturnFromBackground()
|
||
}
|
||
}
|
||
}
|
||
</script>
|
||
|
||
<template>
|
||
<view class="app-container">
|
||
</view>
|
||
</template>
|
||
|
||
<style>
|
||
/*每个页面公共css */
|
||
|
||
/* 引入 TheMiladiatorRegular 字体 */
|
||
@font-face {
|
||
font-family: 'TheMiladiatorRegular';
|
||
src: url('/static/fonts/The Miladiator Regular.ttf') format('truetype');
|
||
font-weight: normal;
|
||
font-style: normal;
|
||
font-display: swap;
|
||
}
|
||
|
||
/* 引入 ZaoZiGongFangJianHei-1 字体 */
|
||
@font-face {
|
||
font-family: 'ZaoZiGongFangJianHei-1';
|
||
src: url('/static/fonts/ZaoZiGongFangJianHei-1.ttf') format('truetype');
|
||
font-weight: normal;
|
||
font-style: normal;
|
||
font-display: swap;
|
||
}
|
||
|
||
/* 引入 ZaoZiGongFangJianHei-1 字体 */
|
||
@font-face {
|
||
font-family: 'JDLTYuanTiJian';
|
||
src: url('/static/fonts/JDLTYuanTiJian.ttf') format('truetype');
|
||
font-weight: normal;
|
||
font-style: normal;
|
||
font-display: swap;
|
||
}
|
||
|
||
/* 圆体 JDLTYuanTiJian.ttf 在部分 Android WebView 上报 OTS/cmap 解析失败,暂不 @font-face 加载,避免控制台告警与渲染异常 */
|
||
|
||
/* 全局字体设置 */
|
||
body {
|
||
font-family:
|
||
'JDLTYuanTiJian',
|
||
-apple-system,
|
||
BlinkMacSystemFont,
|
||
'PingFang SC',
|
||
'Hiragino Sans GB',
|
||
'Microsoft YaHei',
|
||
'Noto Sans SC',
|
||
sans-serif;
|
||
}
|
||
|
||
/* App 容器 */
|
||
.app-container {
|
||
width: 100%;
|
||
min-height: 100vh;
|
||
position: relative;
|
||
}
|
||
|
||
.page-content {
|
||
width: 100%;
|
||
min-height: 100vh;
|
||
}
|
||
</style>
|