-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.vue
More file actions
348 lines (346 loc) · 9.71 KB
/
Copy pathApp.vue
File metadata and controls
348 lines (346 loc) · 9.71 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<script>
import CONFIG from '@/config.js'
import AUTH from '@/common/auth.js'
export default {
globalData: {
curLat: undefined,
curLong: undefined,
adminLogined: false, // 登陆获取后台的 x-token,只需运行一次
goLogin: false,
},
onLaunch: function() {
uni.addInterceptor('request', {
success: (res) => {
if(res.data.code == 2000) {
// 未登录
// #ifdef H5
uni.redirectTo({
url: '/pages/login/login'
})
// #endif
}
},
})
this.vuex('versionNum', uni.getAppBaseInfo().appVersion)
this.checkForUpdate(); // 检查新版本
this.queryConfigBatch();
},
onShow(e) {
if (e && e.query && e.query.inviter_id) {
this.vuex('referrer', e.query.inviter_id)
}
if (e && e.query && e.query.code) {
// 微信登陆
this.wxmpLogin(e.query.code)
return
}
AUTH.checkHasLogined().then(isLogined => {
if (!isLogined) {
// #ifdef MP-WEIXIN
AUTH.login20241224().then(res => {
if(res.code == 0) {
this.getUserApiInfo()
// this.adminLogin()
}
})
// #endif
// // #ifdef H5
// const isLogined = await this.checkHasLoginedH5()
// if (!isLogined) {
// // 判断是普通浏览器还是微信浏览器
// const ua = window.navigator.userAgent.toLowerCase();
// if (ua.match(/MicroMessenger/i) == 'micromessenger' && this.globalData.wxh5autologin) {
// // 微信内置浏览器打开的
// // https://www.yuque.com/apifm/nu0f75/fpvc3m
// const res = await this.$wxapi.siteStatistics()
// const wxMpAppid = res.data.wxMpAppid
// let _domian = CONFIG.h5Domain + '/pages/index/index'
// _domian = encodeURIComponent(_domian)
// if (!this.globalData.goLogin) {
// this.globalData.goLogin = true
// window.parent.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
// wxMpAppid + '&redirect_uri=' + _domian +
// '&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect'
// }
// } else {
// // 其他浏览器打开的, 按需登陆,不能直接跳转到登陆界面
// // uni.navigateTo({
// // url: "/pages/login/login"
// // })
// }
// }
// // #endif
} else {
this.getUserApiInfo()
// this.adminLogin()
}
})
},
onHide: function() {
console.log('App Hide')
},
onPageNotFound(e) {
// 页面不存在 {path: '/1212', query: {a: '123'}, isEntryPage: true}
console.error(e)
},
methods: {
async queryConfigBatch() {
// https://www.yuque.com/apifm/nu0f75/dis5tl
const res = await this.$wxapi.queryConfigBatch(CONFIG.sysconfigkeys)
if (res.code == 0) {
const sysconfigMap = {}
res.data.forEach(config => {
if(config.key == 'bargain_data') {
config.value = JSON.parse(config.value)
}
sysconfigMap[config.key] = config.value
})
this.vuex('sysconfigMap', sysconfigMap)
uni.$emit('sysconfigOK', sysconfigMap)
}
},
checkForUpdate() {
// #ifdef MP
const updateManager = uni.getUpdateManager();
updateManager.onCheckForUpdate(function(res) {
// 请求完新版本信息的回调
console.log(res.hasUpdate);
});
updateManager.onUpdateReady(function(res) {
uni.showModal({
title: '更新提示',
content: '新版本已经准备好,是否重启应用?',
success(res) {
if (res.confirm) {
// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
updateManager.applyUpdate();
}
}
});
});
updateManager.onUpdateFailed(function(res) {
// 新的版本下载失败
});
// #endif
},
async getUserApiInfo() {
const _this = this.$vm ? this.$vm : this
// https://www.yuque.com/apifm/nu0f75/zgf8pu
const res = await _this.$wxapi.userDetail(_this.token)
if (res.code == 0) {
const apiUserInfoMap = res.data
_this.vuex('apiUserInfoMap', apiUserInfoMap)
return apiUserInfoMap
} else {
return null
}
},
async getLocation() {
// 统一获取经纬度
if (this.globalData.curLong && this.globalData.curLat) {
return {
long: this.globalData.curLong,
lat: this.globalData.curLat
}
}
const res = await this.$wxapi.getLocation()
if (res) {
this.globalData.curLong = res.long
this.globalData.curLat = res.lat
return res
} else {
return {
long: 0,
lat: 0
}
}
},
getLocation: () => {
return new Promise(function(resolve, reject) {
// #ifdef H5
if(CONFIG.h5MapSimulator) {
resolve({
long: 0,
lat: 0
})
return
}
// #endif
uni.showLoading({
title: ''
})
// #ifdef MP-WEIXIN
uni.getLocation({
type: "gcj02", // 默认为 wgs84 返回 gps 坐标,gcj02 返回国测局坐标,可用于 uni.openLocation 和 map 组件坐标,App 和 H5 需配置定位 SDK 信息才可支持 gcj02。
success: res => {
resolve({
long: res.longitude,
lat: res.latitude
})
},
fail: err => {
console.error(err)
reject(err)
if (err.errMsg.indexOf('getLocation:fail 频繁调用会增加电量损耗') != -1) {
uni.showToast({
title: "请勿频繁定位",
icon: "none",
});
} else if (err.errMsg === "getLocation:fail auth deny") {
// 未授权
uni.showToast({
title: "无法定位,请重新获取位置信息",
icon: "none",
});
} else if (
err.errMsg ===
"getLocation:fail:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF"
) {
uni.showModal({
content: "请开启手机定位服务",
showCancel: false,
});
} else {
uni.showModal({
content: "无法获取手机定位 \n 请先确认定位服务是否已开启",
showCancel: false,
});
}
},
complete: () => {
uni.hideLoading()
},
});
// #endif
// #ifndef MP-WEIXIN
uni.getLocation({
type: "wgs84", // 默认为 wgs84 返回 gps 坐标,gcj02 返回国测局坐标,可用于 uni.openLocation 和 map 组件坐标,App 和 H5 需配置定位 SDK 信息才可支持 gcj02。
success: res => {
console.log("定位获取:", res);
let platform = uni.getSystemInfoSync().platform;
if (platform == "ios") {
//toFixed() 方法可把 Number 四舍五入为指定小数位数的数字。
resolve({
long: res.longitude.toFixed(6),
lat: res.latitude.toFixed(6)
})
} else {
resolve({
long: res.longitude,
lat: res.latitude
})
}
},
fail: err => {
console.error(err)
reject(err)
if (err.errMsg.indexOf('getLocation:fail 频繁调用会增加电量损耗') != -1) {
uni.showToast({
title: "请勿频繁定位",
icon: "none",
});
} else if (err.errMsg === "getLocation:fail auth deny") {
// 未授权
uni.showToast({
title: "无法定位,请重新获取位置信息",
icon: "none",
});
} else if (
err.errMsg ===
"getLocation:fail:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF"
) {
uni.showModal({
content: "请开启手机定位服务",
showCancel: false,
});
} else {
uni.showModal({
content: "无法获取手机定位 \n 请先确认定位服务是否已开启",
showCancel: false,
});
}
},
complete: () => {
uni.hideLoading()
},
});
// #endif
});
},
async adminLogin() {
if(this.globalData.adminLogined) {
return
}
const _this = this.$vm ? this.$vm : this
/**
* https://www.yuque.com/apifm/nu0f75/nl01pr
* 用当前用户的token登陆后台获取调用后台api接口的 x-token
*/
const res = await _this.$wxapi.request('https://user.api.it120.cc/login/token', false, 'POST', {
token: _this.token
})
if (res.code == 0) {
this.globalData.adminLogined = true
const apiUserInfoMap = res.data
_this.vuex('xtoken', res.data.token)
_this.vuex('shopIds', res.data.shopIds)
}
},
/**
* 读取购物车数据
* https://www.yuque.com/apifm/nu0f75/awql14
*/
async _shippingCarInfo() {
const _this = this.$vm ? this.$vm : this
const res = await _this.$wxapi.shippingCarInfo(_this.token)
if (res.code == 0) {
_this.vuex('shippingCarInfo', res.data)
} else {
_this.vuex('shippingCarInfo', null)
}
},
contactService() {
const _this = this.$vm ? this.$vm : this
// #ifdef MP-WEIXIN
_this.handleMiniProgramBanner()
// #endif
// #ifndef MP-WEIXIN
uni.makePhoneCall({
phoneNumber: _this.sysconfigMap.kf_tel
})
// #endif
},
handleMiniProgramBanner() {
const _this = this.$vm ? this.$vm : this
wx.openCustomerServiceChat({
extInfo: {url: _this.sysconfigMap.customerServiceChatUrl },
corpId: _this.sysconfigMap.customerServiceChatCorpId,
success: res => {},
fail: err => {
console.error(err)
}
})
},
async wxmpLogin(code) {
const _this = this.$vm ? this.$vm : this
// https://www.yuque.com/apifm/nu0f75/lh6cd3
const res = await this.$wxapi.wxmpAuth({
code
})
if (res.code == 0) {
_this.vuex('token', res.data.token)
_this.vuex('uid', res.data.uid)
_this.vuex('openid', res.data.openid)
setTimeout(() => {
uni.$emit('loginOK', {})
}, 500)
}
},
}
}
</script>
<style>
/* #ifdef H5 */
uni-page-head { display: none}
/* #endif */
</style>