-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
193 lines (167 loc) · 5.16 KB
/
Copy pathapp.js
File metadata and controls
193 lines (167 loc) · 5.16 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import createBus from './utils/eventBus';
const api = require('./utils/linkbridge/api');
const UNREAD_BY_SESSION_KEY = 'lb_unread_by_session_v1';
function getCurrentRoute() {
try {
const pages = getCurrentPages();
const cur = pages[pages.length - 1];
return cur?.route || '';
} catch (e) {
return '';
}
}
function getCurrentChatSessionId() {
try {
const pages = getCurrentPages();
const cur = pages[pages.length - 1];
if (cur?.route !== 'pages/chat/index') return '';
return cur?.data?.sessionId || cur?.options?.sessionId || '';
} catch (e) {
return '';
}
}
function getSessionIdFromMessageEnv(env) {
const msg = env?.payload?.message;
return (
msg?.sessionId ||
msg?.sessionID ||
env?.payload?.sessionId ||
env?.payload?.sessionID ||
env?.sessionId ||
env?.sessionID ||
''
);
}
let wsRegistered = false;
const WECHAT_BIND_TS_KEY = 'lb_wechat_bound_at_ms_v1';
App({
globalData: {
unreadNum: 0,
unreadBySession: {},
},
/** 全局事件总线(custom-tab-bar 依赖) */
eventBus: createBus(),
onLaunch() {
// Restore unread map (best-effort). This avoids losing unread state after a cold start.
try {
const raw = wx.getStorageSync(UNREAD_BY_SESSION_KEY);
if (raw) {
const obj = typeof raw === 'string' ? JSON.parse(raw) : raw;
if (obj && typeof obj === 'object') this.globalData.unreadBySession = obj;
}
} catch (e) {
// ignore
}
this.recalcUnreadNum();
const updateManager = wx.getUpdateManager();
updateManager.onUpdateReady(() => {
wx.showModal({
title: '更新提示',
content: '新版本已经准备好,是否重启应用?',
success(res) {
if (res.confirm) updateManager.applyUpdate();
},
});
});
},
onShow() {
if (!api.isLoggedIn()) return;
api.connectWebSocket();
if (wsRegistered) return;
wsRegistered = true;
// Best effort: keep WeChat session bound for VoIP signature.
try {
const lastBindAt = Number(wx.getStorageSync(WECHAT_BIND_TS_KEY) || 0);
const now = Date.now();
if (!Number.isFinite(lastBindAt) || now - lastBindAt > 12 * 60 * 60 * 1000) {
api
.bindWeChatSession()
.catch(() => null)
.then(() => wx.setStorageSync(WECHAT_BIND_TS_KEY, Date.now()));
}
} catch (e) {
// ignore
}
api.addWebSocketHandler((env) => {
if (env?.type !== 'message.created') return;
const sid = getSessionIdFromMessageEnv(env);
if (!sid) return;
// Best-effort unread badge: if user is currently in the same chat session, don't increment.
const route = getCurrentRoute();
if (route === 'pages/chat/index') {
const openSid = getCurrentChatSessionId();
if (openSid && sid && openSid === sid) return;
}
this.incrementSessionUnread(sid, 1);
});
api.addWebSocketHandler((env) => {
if (env?.type !== 'call.invite') return;
const call = env?.payload?.call;
const callId = call?.id || '';
if (!callId) return;
const callerName = env?.payload?.caller?.displayName || '对方';
wx.showModal({
title: '语音通话',
content: `${callerName} 邀请你语音通话`,
confirmText: '接听',
cancelText: '拒绝',
success: (res) => {
if (res.confirm) {
const url =
`/pages/call/index?callId=${encodeURIComponent(callId)}` +
`&incoming=1&autoAccept=1` +
(callerName ? `&peerName=${encodeURIComponent(callerName)}` : '');
wx.navigateTo({ url });
} else if (res.cancel) {
api.rejectCall(callId).catch(() => null);
}
},
});
});
},
/** 设置未读消息数量(用于 TabBar badge) */
setUnreadNum(unreadNum) {
this.globalData.unreadNum = unreadNum;
this.eventBus.emit('unread-num-change', unreadNum);
},
recalcUnreadNum() {
const map = this.globalData.unreadBySession || {};
let total = 0;
Object.keys(map).forEach((k) => {
const n = Number(map[k] || 0) || 0;
if (n > 0) total += n;
});
this.setUnreadNum(total);
},
setSessionUnread(sessionId, count) {
const sid = String(sessionId || '').trim();
if (!sid) return;
const map = this.globalData.unreadBySession || {};
const next = Number(count || 0) || 0;
if (next > 0) map[sid] = next;
else delete map[sid];
this.globalData.unreadBySession = map;
try {
wx.setStorageSync(UNREAD_BY_SESSION_KEY, JSON.stringify(map));
} catch (e) {
// ignore
}
this.recalcUnreadNum();
},
incrementSessionUnread(sessionId, delta = 1) {
const sid = String(sessionId || '').trim();
if (!sid) return;
const map = this.globalData.unreadBySession || {};
const cur = Number(map[sid] || 0) || 0;
const next = Math.max(0, cur + (Number(delta || 0) || 0));
if (next > 0) map[sid] = next;
else delete map[sid];
this.globalData.unreadBySession = map;
try {
wx.setStorageSync(UNREAD_BY_SESSION_KEY, JSON.stringify(map));
} catch (e) {
// ignore
}
this.recalcUnreadNum();
},
});