app.js
7.6 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
194
195
196
197
198
199
200
201
202
203
204
205
//app.js
import * as Sentry from './utils/lib/sentry-miniapp'
const accountInfo = wx.getAccountInfoSync()
//测试和生产才开发sentry
const env = ['trial', 'release']
env.includes(accountInfo.miniProgram.envVersion) &&
Sentry.init({
// dsn: 'http://ebcf31c417cc4ce185782f046e4a87a2@sentry.glsx.com.cn/5'
dsn: 'https://e440585f391e46278c3c72ddabb1d17f@sentry.glsx.com.cn/31'
})
import router from './router/index.js'
import util from './utils/util.js'
import './utils/Track'
import { ssoApi, wechatApi } from './api/api'
import { trackData } from './utils/data'
import eventbus from './utils/evenbus'
// 实例化对象存储
import { Storage } from './utils/cache/storage'
const $Storage = new Storage()
App({
onLaunch: function (options) {
console.log(options)
/**挂载到wx对象 */
this.autoUpdate()
wx.$storage = $Storage /** 挂载全局缓存方法 */
wx.$eventbus = eventbus
wx.$util = util
wx.$router = router
wx.$imgUrl = 'https://img02.glsx.com.cn/weapp/resource/miniprogram/ai-card/'
wx.$appId = wx.getAccountInfoSync().miniProgram.appId
const systemInfo = wx.getStorageSync('aicard_system_info')
if (!systemInfo)
wx.getSystemInfo({
success(res) {
util.setStorageSync('aicard_system_info', res)
}
})
if (options.path != 'pages/tab/index/index') {
this.login()
}
// wx.showMeasure = false
wx.showMeasure = !!options.query.scene
},
login(returnResponse) {
let _this = this
let openId = null
let wechatInfo = null
return new Promise((resolve, reject) => {
wx.login({
success({ code }) {
ssoApi
.jsCodeLogin({
code,
appId: wx.$appId
})
.then(res => {
wechatInfo = res.data
util.setStorageSync('aicard_info', wechatInfo)
wx.$authStatus = res.code != 0
util.setSessionId(wechatInfo.sessionId)
openId = wechatInfo.openId
// util.setStorageSync('openId', openId)
console.log(wx.$trackData)
wx.showMeasure = !!wx.$trackData.scene
if (wx.$trackData.scene) {
// 初次进入小程序默认埋首页
_this.getScene(wx.$trackData.scene).then(res => {
wx.$trackData.merchant = res.merchantId
wx.$trackData.cardId = res.cardId
console.log('wx.$trackData.cardId', wx.$trackData.cardId)
util.setStorageSync('scene', res)
const systemInfo = JSON.parse(wx.getStorageSync('aicard_system_info'))
wx.$trackData.type = systemInfo.environment ? 2 : 1
wx.$Track.sendTrack(trackData.COMPANY, wechatInfo)
})
} else if (wx.$trackData.detailShare) {
// d+产品分享在这里设置埋点
const detailShare = wx.$trackData.detailShare
console.log(1211)
for (let i in trackData) {
console.log(i)
if (trackData[i].pageName == detailShare) {
console.log(trackData[i])
wx.$Track.sendTrack(trackData[i], wechatInfo)
break
}
}
} else if (wx.$trackData.isShare) {
util.setStorageSync('scene', wx.$trackData)
const systemInfo = JSON.parse(wx.getStorageSync('aicard_system_info'))
wx.$trackData.type = systemInfo.environment ? 2 : 1
} else if (!wx.$trackData.detailShare && wx.$trackData.cardId) {
// 名片分享在这里设置埋点
util.setStorageSync('scene', wx.$trackData)
const systemInfo = JSON.parse(wx.getStorageSync('aicard_system_info'))
wx.$trackData.type = systemInfo.environment ? 2 : 1
wx.$Track.sendTrack(trackData.COMPANY, wechatInfo)
}
ssoApi
.getUserInfo({
openId
})
.then(result => {
if (returnResponse) {
resolve(result)
} else {
if (result.code == 0) {
util.setUserInfo(result.data)
} else {
util.showToast(result.message || '网络异常')
}
resolve(result)
}
})
})
.catch(() => {
wx.$authStatus = true
reject()
})
}
})
})
},
getScene(scene) {
return new Promise((resolve, reject) => {
wechatApi
.getSceneById({
id: decodeURIComponent(scene).split('=')[1]
})
.then(({ data }) => {
let op = {}
data.scene.split('&').map(item => {
const [key, val] = item.split('=')
op[key] = val
})
resolve(op)
})
})
},
/**
* 下载小程序新版本并重启应用
*/
autoUpdate: function () {
// console.log(new Date())
const self = this
// 获取小程序更新机制兼容
if (wx.canIUse('getUpdateManager')) {
const updateManager = wx.getUpdateManager()
//1. 检查小程序是否有新版本发布
updateManager.onCheckForUpdate(function (res) {
// 请求完新版本信息的回调
if (res.hasUpdate) {
//2. 小程序有新版本,则静默下载新版本,做好更新准备
updateManager.onUpdateReady(function () {
console.log(new Date())
wx.showModal({
title: '更新提示',
content: '新版本已经准备好,是否重启应用?',
success: function (res) {
if (res.confirm) {
//3. 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
updateManager.applyUpdate()
} else if (res.cancel) {
//如果需要强制更新,则给出二次弹窗,如果不需要,则这里的代码都可以删掉了
// wx.showModal({
// title: '温馨提示~',
// content: '本次版本更新涉及到新的功能添加,旧版本无法正常访问的哦~',
// success: function (res) {
// self.autoUpdate()
// return;
// //第二次提示后,强制更新
// if (res.confirm) {
// // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
// updateManager.applyUpdate()
// } else if (res.cancel) {
// //重新回到版本更新提示
// self.autoUpdate()
// }
// }
// })
}
}
})
})
updateManager.onUpdateFailed(function () {
// 新的版本下载失败
wx.showModal({
title: '已经有新版本了哟~',
content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~'
})
})
}
})
} else {
// 如果希望用户在最新版本的客户端上体验您的小程序,可以这样子提示
wx.showModal({
title: '提示',
content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
})
}
},
globalData: {}
})