-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathApp.uvue
More file actions
143 lines (128 loc) · 4.2 KB
/
App.uvue
File metadata and controls
143 lines (128 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<script lang="uts">
import { CONVERSATION_PAYLOAD } from '@/constants/common.constants';
import { apiAgentComponentPageResultUpdate } from '@/servers/agentDev';
import { initEventPolling, startEventPolling, stopEventPolling } from '@/hooks/useEventPolling';
// #ifdef MP-WEIXIN
import { htmlToMarkdown } from '@/utils/htmlToMarkdown';
// #endif
// #ifdef H5
function fix100vh() {
// 检测是否有输入框聚焦(键盘可能弹起)
const focusedElement = document.activeElement;
const isInputFocused = focusedElement && (
focusedElement.tagName === 'TEXTAREA' ||
focusedElement.tagName === 'INPUT'
);
// 如果有输入框聚焦(键盘弹起),不更新 --vh 变量,保持页面容器高度稳定
// 这样可以防止输入框因页面高度变化而"飘起"
if (isInputFocused) {
return;
}
let vh = window.innerHeight;
if (window.visualViewport && window.visualViewport.height) {
vh = Math.round(window.visualViewport.height);
} else {
vh = window.innerHeight;
}
document.documentElement.style.setProperty('--vh', `${vh}px`);
}
fix100vh();
window.addEventListener('resize', fix100vh);
// 解决iOS键盘问题的代码 使用Visual Viewport API
window.visualViewport && window.visualViewport.addEventListener('resize', fix100vh);
// #endif
// #ifdef APP-ANDROID || APP-HARMONY
let firstBackTime = 0
// #endif
export default {
onLaunch: function () {
// 使用 globalThis 定义全局变量
globalThis.appConfig = {
redirectUrl: null,
}
// 清理会话payload,解决h5端,用户在主页点击推荐问题,或者发送消息后,进入智能体主页,然后手动直接修改url地址回到主页后,再次点击智能体,进入智能体主页,会话payload因为不清理的问题,导致进入页面后立马发送以前的消息
uni.removeStorageSync(CONVERSATION_PAYLOAD)
// 微信小程序事件绑定
// #ifdef MP-WEIXIN
uni.$on('browser_navigate_page', async(data) => {
console.log('browser_navigate_page 事件触发2', data)
const html = data.html;
if (!data.method) return;
// 获取 iframe 内容
let str = '';
// 如果是 html
if (data.data_type === 'html') {
str = html;
}
// 如果是 markdown
if (data.data_type === 'markdown') {
// 将 HTML 转换为 Markdown
str = htmlToMarkdown(html);
console.log('HTML 已转换为 Markdown');
}
if (!str) {
return
}
if (data.method === 'browser_navigate_page') {
const params = {
requestId: data.request_id || '',
html: str,
};
await apiAgentComponentPageResultUpdate(params);
}
})
// #endif
// 初始化全局事件轮询
// H5: 自动监听 visibilitychange,切换标签页时自动暂停/恢复
// 小程序: 需要依赖 onHide/onShow 手动控制
initEventPolling();
},
onShow: function () {
console.log('App Show')
// #ifndef H5
// 非 H5 端:从后台回到前台,恢复轮询(H5 由 visibilitychange 自动处理)
startEventPolling();
// #endif
},
onHide: function () {
console.log('App Hide')
// #ifndef H5
// 非 H5 端:切换到后台,暂停轮询(H5 由 visibilitychange 自动处理)
stopEventPolling();
// #endif
},
// #ifdef APP-ANDROID || APP-HARMONY
onLastPageBackPress: function () {
console.log('App LastPageBackPress')
if (firstBackTime == 0) {
uni.showToast({
title: '再按一次退出应用',
position: 'bottom',
})
firstBackTime = Date.now()
setTimeout(() => {
firstBackTime = 0
}, 2000)
} else if (Date.now() - firstBackTime < 2000) {
firstBackTime = Date.now()
uni.exit()
}
},
// #endif
onExit: function () {
console.log('App Exit')
},
}
</script>
<style lang="scss">
@import "@/static/iconfont/iconfont.css"; // 引入图标库
@import '@/styles/global.scss';
.page-container {
/* #ifdef H5 */
height: var(--vh, 100svh); /* 能识别 svh 就用 svh,否则用 JS 算出来的 --vh */
/* #endif */
/* #ifndef H5 */
height: 100vh;
/* #endif */
}
</style>