app.js
6.12 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
206
207
208
// app.js
import router from './router/index.js'
import util from './utils/util.js'
import {
ssoApi,
commonApi,
shopmallApi
} from './api/api'
import './utils/Track'
import eventbus from './utils/evenbus'
import userApi from './api/userApi'
App({
onLaunch(options) {
/**挂载到wx对象 */
wx.$authStatus = false
wx.$util = util
wx.$router = router
// wx.$imgUrl = 'https://img02.glsx.com.cn/weapp/resource/miniprogram/cgacar/'
wx.$imgUrl = 'https://img02.glsx.com.cn/weapp/resource/miniprogram-test/cgacar/'
wx.$eventbus = eventbus
this.globalData.appId = wx.getAccountInfoSync().miniProgram.appId
const systemInfo = wx.getStorageSync('srcm_shopmall_system_info')
if (!systemInfo) wx.getSystemInfo({
success(res) {
util.setStorageSync('srcm_shopmall_system_info', res)
}
})
},
/**页面不存在时触发 */
onPageNotFound(e) {
wx.$router.goHome()
},
getUpdateManager() {
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 () {
// 新版本下载失败
})
},
onShow(options) {
this.getUpdateManager()
let query = Object.keys(options.query)
if (query.length) query.map(e => {
let params = decodeURIComponent(e).split('&')
params.map(i => {
let [key, val] = i.split('=')
if (!val) val = options.query[key]
this.setPageParmas({
[key]: val
})
})
})
else {
if (this.autoLogin) return
this.login()
}
},
setPageParmas(query) {
if (query.merchantId) util.setStorageSync('srcm_shopmall_merchant_id', query.merchantId)
if (query.customerId) util.setStorageSync('srcm_shopmall_customer_id', query.customerId)
let storeCode = query.storeCode || util.filterUrlParam(decodeURIComponent(query.scene))['storeCode']
if (query.scene) {
let scene = decodeURIComponent(query.scene)
let [key, val] = scene.split('=')
if (key == 'storeCode') {
this.getGHMerchant(val)
return
}
shopmallApi.getLiveActivityId({
id: val
}).then(({
code,
data,
}) => {
if (code == 0) {
let op = {}
data.scene.split('&').map(item => {
const [key, val] = item.split("=")
op[key] = val
})
if (op.merchantId) util.setStorageSync('srcm_shopmall_merchant_id', op.merchantId)
if (op.openId) this.openId = op.openId
if (op.storeCode) this.getGHMerchant(op.storeCode)
if (op.packageType==1&&op.id) util.setStorageSync('packageId', +op.id)
}
})
return
}
if (storeCode) {
this.getGHMerchant(storeCode)
return
}
if (query.merchantId) {
util.setStorageSync('srcm_shopmall_merchant_id', query.merchantId)
this.login()
return
}
},
getGHMerchant(storeCode, callBack) {
util.setStorageSync('srcm_shopmall_store_code', storeCode)
commonApi.getBySapCode({
sapCode: storeCode
}).then(({
code,
data
}) => {
if (code == '0') util.setStorageSync('srcm_shopmall_merchant_id', data.merchantNo)
if (this.openId) {
this.loginByOpenId(this.openId, data)
return
}
this.login().then(res => {
if (callBack) callBack(res)
})
})
},
login(returnResponse) {
return new Promise(async (resolve, reject) => {
try {
const res = await userApi.login()
wx.$isGroup = res.data.group
wx.$authStatus = res.code != 0
if (res && res.data) {
this.globalData.openId = res.data.openId
if (res.data.sapCode) util.setStorageSync('srcm_shopmall_store_code', res.data.sapCode)
}
if (returnResponse) {
resolve(res)
} else {
if (res.data) wx.setStorageSync('merchant_info', {
merchantId: res.data.merchantId,
merchantName: res.data.merchantName,
sapCode: res.data.sapCode || '',
})
if (res.code != -1) {
if (res.code == 0) {
util.setUserInfo(res.data)
util.setSessionId(res.data.sessionId)
let customerId = util.getStorageSync('srcm_shopmall_customer_id')
let merchantId = util.getStorageSync('srcm_shopmall_merchant_id')
if (customerId) {
ssoApi.updataCustomerPhone({
customerId,
merchantId
}).then(() => {
wx.removeStorageSync('srcm_shopmall_customer_id')
})
}
// 登录完加载权益会员信息接口(目前只针对权益商品页面)
util.customerbehavior() // sop埋点
eventbus.emit('EQUITYGOODS', true)
wx.removeStorageSync('isShowPreModal')
} else {
util.showToast(res.message || '网络异常')
}
}
resolve(res)
}
} catch (error) {
wx.$authStatus = true
reject()
}
})
},
// 通过openId静默登录
loginByOpenId(openId, merchants) {
ssoApi.loginByOpenId({
openId
}).then(res => {
res.merchantId = merchants.merchantId
res.merchantName = merchants.merchantName
this.autoLogin = true
util.setUserInfo(res)
util.setSessionId(res.sessionId)
util.setStorageSync('showRevicesModal', true)
})
},
globalData: {
latesParam: {
appearance: 1,
pageSize: 0,
pageCount: 3,
},
loaded: false,
commodityList: []
// appId: '',
// merchantId: '44189923' // 44184606 44189923
},
onHide() {
util.customerbehavior(2)
}
})