forked from wechat-miniprogram/miniprogram-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
136 lines (131 loc) · 3.6 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
const config = require('./config')
const themeListeners = []
global.isDemo = true
App({
onLaunch(opts, data) {
const that = this
const canIUseSetBackgroundFetchToken = wx.canIUse('setBackgroundFetchToken')
if (canIUseSetBackgroundFetchToken) {
wx.setBackgroundFetchToken({
token: 'getBackgroundFetchToken',
})
}
if (wx.getBackgroundFetchData) {
wx.getBackgroundFetchData({
fetchType: 'pre',
success(res) {
that.globalData.backgroundFetchData = res
console.log('读取预拉取数据成功', res)
},
fail() {
console.log('读取预拉取数据失败')
wx.showToast({
title: '无缓存数据',
icon: 'none'
})
},
complete() {
console.log('结束读取')
}
})
}
console.log('App Launch', opts)
if (data && data.path) {
wx.navigateTo({
url: data.path,
})
}
if (!wx.cloud) {
console.error('请使用 2.2.3 或以上的基础库以使用云能力')
} else {
wx.cloud.init({
env: config.envId,
traceUser: true,
})
}
// skyline
const systemInfo = wx.getSystemInfoSync()
console.log('@@@ systemInfo ', systemInfo)
Object.assign(this.globalData, systemInfo)
// eslint-disable-next-line promise/always-return
require.async('./packageSkyline/common/custom-route/index.js').then(utils => {
console.log('--------begin installRouteBuilder')
utils.installRouteBuilder() // 'common'
}).catch(({mod, errMsg}) => {
console.error(`installRouteBuilder path: ${mod}, ${errMsg}`)
})
},
onShow(opts) {
console.log('App Show', opts)
console.log('USER_DATA_PATH', wx.env.USER_DATA_PATH)
// console.log(wx.getSystemInfoSync())
},
onHide() {
console.log('App Hide')
},
onThemeChange({theme}) {
this.globalData.theme = theme
themeListeners.forEach((listener) => {
listener(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)
}
},
globalData: {
theme: wx.getSystemInfoSync().theme,
hasLogin: false,
openid: null,
iconTabbar: '/page/extend/images/icon_tabbar.png',
systemInfo: {}
},
// lazy loading openid
getUserOpenId(callback) {
const self = this
if (self.globalData.openid) {
callback(null, self.globalData.openid)
} else {
wx.login({
success() {
wx.cloud.callFunction({
name: 'login',
data: {
action: 'openid'
},
success: res => {
console.log('拉取openid成功', res)
self.globalData.openid = res.result.openid
callback(null, self.globalData.openid)
},
fail: err => {
console.log('拉取用户openid失败,将无法正常使用开放接口等服务', err)
callback(err)
}
})
},
fail(err) {
console.log('wx.login 接口调用失败,将无法正常使用开放接口等服务', err)
callback(err)
}
})
}
},
// 通过云函数获取用户 openid,支持回调或 Promise
getUserOpenIdViaCloud() {
return wx.cloud.callFunction({
name: 'wxContext',
data: {}
}).then(res => {
this.globalData.openid = res.result.openid
return res.result.openid
})
}
})