forked from fresns/client-demo-wechat
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
executable file
·150 lines (132 loc) · 3.82 KB
/
app.js
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
/*!
* Fresns 微信小程序 (https://fresns.cn)
* Copyright 2021-Present 唐杰
* Licensed under the Apache-2.0 license
*/
import './libs/Mixins';
import { fresnsApi } from './api/api';
import { globalInfo } from './utils/fresnsGlobalInfo';
import { cachePut } from './utils/fresnsUtilities';
const themeListeners = [];
App({
// 全局数据
globalData: {
// fresns extensions
fresnsExtensions: {},
extensionsUrl: '',
extensionsTitle: '',
},
// 监听小程序初始化
onLaunch: async function () {
// 版本检测
if (wx.canIUse('getUpdateManager')) {
const updateManager = wx.getUpdateManager();
updateManager.onCheckForUpdate(function (res) {
if (res.hasUpdate) {
updateManager.onUpdateReady(function () {
wx.showModal({
title: '更新提示',
content: '新版本已经准备好,重新进入小程序使用新版',
success: function (res) {
if (res.confirm) {
updateManager.applyUpdate();
}
},
});
});
updateManager.onUpdateFailed(function () {
wx.showModal({
title: '已经有新版本了哟~',
content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~',
showCancel: false,
confirmText: false,
});
});
}
});
} else {
wx.showModal({
title: '提示',
content: '当前微信版本过低,无法使用本小程序,请升级到最新版微信。',
showCancel: false,
confirmText: false,
});
}
},
// 监听小程序启动
onShow: async function () {
await globalInfo.init();
// 站点状态
try {
const fresnsStatus = await fresnsApi.global.globalStatus();
const langTag = wx.getStorageSync('langTag');
if (!fresnsStatus.activate) {
const deactivateDescribe = fresnsStatus.deactivateDescribe[langTag] || fresnsStatus.deactivateDescribe.default;
wx.showModal({
content: deactivateDescribe,
showCancel: false,
confirmText: false,
});
}
} catch (e) {
console.log('fresnsStatus', e);
}
// 全局配置
try {
const configValue = wx.getStorageSync('fresnsConfigs');
if (!configValue) {
console.log('globalConfigs', 'fresnsConfigs');
const result = await fresnsApi.global.globalConfigs();
const cacheMinutes = result.data.cache_minutes || 30;
if (result.code === 0 && result?.data) {
cachePut('fresnsConfigs', result.data, cacheMinutes);
}
}
} catch (e) {
console.log('fresnsConfigs', e);
}
// 监听进入 App 的事件
const appInfo = wx.getStorageSync('appInfo');
if (appInfo.isApp) {
wx.miniapp.registOpenURL((param) => {
const paramData = param.data;
switch (param.action) {
case 'scheme':
// Android
break;
case 'webpageURL':
// iOS
break;
case 'opensdkOnRep':
// WeChat
break;
default:
console.log('registOpenURL', param);
return;
}
});
}
},
// 监听系统主题变化
onThemeChange(result) {
this.globalData.theme = result.theme;
themeListeners.forEach((listener) => {
listener(result.theme);
});
},
// 主题变更
themeChanged(theme) {
this.onThemeChange({ theme });
},
watchThemeChange(listener) {
if (themeListeners.indexOf(listener) < 0) {
themeListeners.push(listener);
}
},
unWatchThemeChange(listener) {
const index = themeListeners.indexOf(listener);
if (index > -1) {
themeListeners.splice(index, 1);
}
},
});