app.js
5.98 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
//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, busassistantApi } 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) {
/**挂载到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
this.clearFileSystem()
const systemInfo = wx.$storage.get('aicard_system_info')
if (!systemInfo)
wx.getSystemInfo({
success(res) {
wx.$storage.set('aicard_system_info', res)
}
})
if (options.path != 'pages/tab/index/index') {
this.login()
}
wx.showMeasure = !!(options.query && options.query.scene)
wx.onMemoryWarning(function () {
})
},
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
wx.$storage.set('aicard_info', wechatInfo)
wx.$authStatus = res.code != 0
openId = wechatInfo.openId
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
wx.$storage.set('scene', {
merchant: res.merchantId,
cardId: res.cardId
})
_this.getUserInfo(openId, returnResponse).then(result => {
wx.$Track.sendTrack(trackData.HOME, wechatInfo)
resolve(result)
})
})
} else {
// 其他的分享进来的页面走这里,在进页面的onshow中需对wx.$trackData先赋值
const shareType = wx.$trackData.shareType
_this.getUserInfo(openId, returnResponse).then(result => {
switch(shareType) {
case 'SHARE_HOME':
// 名片分享在这里设置埋点
wx.$storage.set('scene', wx.$trackData)
// wx.$Track.sendTrack(trackData.COMPANY, wechatInfo)
break
case 'SHARE_D':
wx.$storage.set('scene', wx.$trackData)
break
default:
if (wx.$trackData.cardId) {
wx.$storage.set('scene', wx.$trackData)
}
}
resolve(result)
})
}
})
.catch(() => {
wx.$authStatus = true
reject()
})
}
})
})
},
getUserInfo(openId, returnResponse) {
return new Promise((resolve) => {
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)
}
})
})
},
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)
})
})
},
// 获取公共数据
getBaseData() {
console.log("method")
return new Promise((resolve, reject) => {
busassistantApi.getDiscoverList().then(res => {
console.log(res, '555');
resolve(res.data)
})
})
},
/**
* 下载小程序新版本并重启应用
*/
autoUpdate: function () {
const updateManager = wx.getUpdateManager()
updateManager.onCheckForUpdate(function (res) {
})
updateManager.onUpdateReady(function () {
wx.showModal({
title: '更新提示',
content: '新版本已经准备好,是否重启应用?',
success: function (res) {
if (res.confirm) {
// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
updateManager.applyUpdate()
}
}
})
})
updateManager.onUpdateFailed(function () {
// 新版本下载失败
})
},
// 清内存
clearFileSystem: function() {
const fn = wx.getFileSystemManager()
fn.readdir({
dirPath: wx.env.USER_DATA_PATH,
success({
files
}) {
files.length && files.map(e => {
fn.unlink({
filePath: `${wx.env.USER_DATA_PATH}/${e}`
})
})
}
})
},
globalData: {},
onPageNotFound(res) {
setTimeout(() => {
wx.$router.go('/pages/tab/index/index', res.query)
}, 1000)
},
})