-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.ts
141 lines (128 loc) · 4.21 KB
/
index.ts
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
import { decodeParams } from '@antmjs/utils'
declare let App: any
declare let Page: any
declare let Component: any
declare const getCurrentPages: any
// 下面的逻辑为了抹平个端对于参数返回不一致的差异
function smoothOutParams() {
const oriApp = App
const oriPage = Page
const oriComponent = Component
let firstShow = true
let firstShowOpt: any = null
/**
* 处理页面生命周期
*/
const handlePageCycle = function (
methodName: string,
args: any,
isApp = false,
) {
switch (methodName) {
case 'onLaunch': {
// 微信和抖音需要decode,支付宝、快手自己decode过了
if (process.env.TARO_ENV === 'weapp' || process.env.TARO_ENV === 'tt') {
// 微信、支付宝、快手、抖音launch之后马上触发的appshow会使用当前的参数,所以这里decode过了,appshow就不需要decode了
args[0].query = decodeParams(args[0].query || {})
}
firstShow = true
// console.log('inner launch', JSON.stringify(args[0]))
break
}
case 'onLoad': {
// 微信和抖音需要decode,支付宝、快手自己decode过了
// 实测抖音第一次进来的时候不需要decode
if (process.env.TARO_ENV === 'weapp') {
const pages = getCurrentPages()
const currentPage = (pages && pages[pages.length - 1]) || {}
args[0] = decodeParams(args[0] || {})
currentPage.options = args[0]
}
if (process.env.TARO_ENV === 'tt') {
const strParams = JSON.stringify(args[0] || {})
const pages = getCurrentPages()
const currentPage = (pages && pages[pages.length - 1]) || {}
if (!firstShowOpt || (firstShowOpt && strParams !== firstShowOpt)) {
args[0] = decodeParams(args[0] || {})
}
currentPage.options = args[0]
currentPage.minifixWithTT = JSON.stringify(args[0])
}
firstShowOpt = null
// console.log('inner load', JSON.stringify(args[0]))
break
}
case 'onShow': {
if (isApp) {
if (firstShow) {
firstShow = false
firstShowOpt = JSON.stringify(args[0].query || {})
// console.log('inner first show', JSON.stringify(args[0]))
} else {
if (process.env.TARO_ENV === 'weapp') {
args[0].query = decodeParams(args[0].query || {})
}
if (process.env.TARO_ENV === 'tt') {
const strParams = JSON.stringify(args[0].query || {})
const pages = getCurrentPages()
const currentPage = (pages && pages[pages.length - 1]) || {}
if (strParams !== currentPage.minifixWithTT) {
args[0].query = decodeParams(args[0].query || {})
}
}
// console.log('inner not first show', JSON.stringify(args[0]))
}
}
break
}
default:
break
}
return args
}
const wrapMethod = function (target: any, methodName: string, isApp = false) {
const methodFunc = target[methodName]
target[methodName] = function (...args: any) {
try {
const factoryOptions = handlePageCycle(methodName, args, isApp)
return methodFunc.apply(this, factoryOptions)
} catch (e) {
console.info('antmjs wrapMethod error', e)
}
return methodFunc.apply(this, args)
}
}
const filterFunctions = (obj: any, isApp = false) => {
try {
if (obj) {
Object.keys(obj)
.filter((prop) => typeof obj[prop] === 'function')
.forEach((methodName) => {
wrapMethod(obj, methodName, isApp)
})
}
} catch (e) {
console.info('antmjs filterFunctions error', e)
}
}
Page = (page: any) => {
filterFunctions(page)
oriPage(page)
}
Component = (component: any) => {
component.methods && filterFunctions(component.methods)
oriComponent(component)
}
App = (page: any) => {
filterFunctions(page, true)
oriApp(page)
}
}
if (
process.env.TARO_ENV === 'weapp' ||
process.env.TARO_ENV === 'alipay' ||
process.env.TARO_ENV === 'kwai' ||
process.env.TARO_ENV === 'tt'
) {
smoothOutParams()
}