loading.js
2.52 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
// pages/loading/loading.js
Page({
/**
* 页面的初始数据
*/
data: {
},
/**
* 生命周期函数--监听页面加载
*/
async onLoad(options) {
console.log(options, '===')
this.tabList = __wxConfig.tabBar.list
wx.p.wxApi.showLoading()
//scenc获取页面参数并跳转
if (options.scene) {
return this.getSceneById(options.scene)
}
if (options.url) {
return this.login(options)
}
this.login()
},
async login(options = {}) {
if (options.merchantId) wx.$merchant_id = options.merchantId
try {
const {
code,
data,
message
} = await wx.p.api.userApi.login()
wx.$authStatus = code != 0
if (data) wx.p.storage.set('merchant_info', {
merchantId: data.merchantId,
merchantName: data.merchantName
})
if (code == '0') {
wx.p.storage.set('userInfo', data)
wx.p.storage.set('sessionId', data.sessionId)
if (!data) throw message
} else {
wx.p.storage.rm('userInfo')
wx.p.storage.rm('sessionId')
}
console.log(data, '=============')
if (options.url) {
return this.jumpPage(options.url)
}
// 默认跳转到首页
wx.p.router.goHome()
} catch (error) {
console.log(error)
wx.p.wxApi.showToast(String(error))
wx.p.router.goHome()
}
},
async getSceneById(scene) {
const id = decodeURIComponent(scene).split('=').at(-1)
try {
const {
code,
data
} = await wx.p.api.wechatApi.getSceneById({
id
})
const params = data.scene.split('url='),
options = {}
if (params.length > 1) options.url = params.at(-1)
if (code == 0) this.login(options)
else throw {}
} catch (error) {
wx.p.wxApi.showToast(error || '获取scene失败')
this.login()
}
},
jumpPage(val) {
const targetPath = decodeURIComponent(val),
{
url,
events
} = this.setRouteData(targetPath),
routeName = this.tabList.findIndex(e => `/${e.pagePath}` == `${url}.html`) > -1 ? 'switchTab' : 'redirectTo'
events.notIntercept = true
wx.p.router[routeName](url, events)
},
setRouteData(targetPath) {
let [url, query] = targetPath.split('?'),
events = {}
if (query) {
let arr = query.split('&')
arr.map(e => {
const [key, val] = e.split('=')
Object.assign(events, {
[key]: val
})
})
}
return {
url,
events
}
}
})