Skip to content

Commit 9249161

Browse files
committed
fix: uni-app 和 taro 性能测试代码兼容 支付宝小程序
1 parent 1ac0ace commit 9249161

File tree

9 files changed

+32
-27
lines changed

9 files changed

+32
-27
lines changed

taro/src/app.jsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ import Taro, { Component } from '@tarojs/taro'
22
import Index from './pages/index'
33
import './app.css'
44

5-
if (process.env.TARO_ENV === 'weapp') {
5+
if (process.env.TARO_ENV === 'weapp' || process.env.TARO_ENV === 'alipay') {
66
require('./utils/perf')
77
}
8-
98
// 如果需要在 h5 环境中开启 React Devtools
109
// 取消以下注释:
1110
// if (process.env.NODE_ENV !== 'production' && process.env.TARO_ENV === 'h5') {

taro/src/components/card/card.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default class Card extends Component {
1212
}
1313

1414
trigger = () => {
15-
if (process.env.TARO_ENV === 'weapp') {
15+
if (process.env.TARO_ENV === 'weapp' || process.env.TARO_ENV === 'alipay') {
1616
this.$scope && this.$scope.$perf && this.$scope.$perf.mark('setData')
1717
}
1818
this.setState({
@@ -73,7 +73,7 @@ export default class Card extends Component {
7373
<Text className='from-text'></Text>
7474
</View>
7575
<View className='uni-media_groups-item' onClick={this.trigger}>
76-
<Text className={["uni-media_groups-item-iconfont iconfont icon-dianzan1",isActive?'uni-media_groups-item-active':'']} ></Text>
76+
<Text className={`uni-media_groups-item-iconfont iconfont icon-dianzan1 ${isActive?'uni-media_groups-item-active':''}`} ></Text>
7777
<Text className='from-text'>{item.thumbs}</Text>
7878
<Text className='from-text'></Text>
7979
</View>

taro/src/pages/index/index.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default class Index extends Component {
1414
}
1515
}
1616
componentWillMount() {
17-
if (process.env.TARO_ENV === 'weapp') {
17+
if (process.env.TARO_ENV === 'weapp' || process.env.TARO_ENV === 'alipay') {
1818
this.$scope && this.$scope.$perf && this.$scope.$perf.mark('setData')
1919
}
2020

taro/src/utils/perf.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,12 @@ function wrapperSetData(namespace, shouldMeasureFn, contentFn, getAutoFn, oldSet
136136
}
137137
// console.log('....after', data)
138138
this.$perf.measure('setData.before')
139+
var _this = this
139140
oldSetData.call(this, data, function () {
140-
this.$perf.measure('setData.end')
141-
if (this.$perf.isEnd()) {
142-
this.$perf.ended = true
143-
const stat = getStat(this.$perf, namespace)
141+
_this.$perf.measure('setData.end')
142+
if (_this.$perf.isEnd()) {
143+
_this.$perf.ended = true
144+
const stat = getStat(_this.$perf, namespace)
144145
const content = contentFn(stat)
145146
hideToast()
146147
showModal({
@@ -151,15 +152,15 @@ function wrapperSetData(namespace, shouldMeasureFn, contentFn, getAutoFn, oldSet
151152
})
152153
console.log(content)
153154
if (PERF_USING_COMPONENTS === false && namespace === 'page') {
154-
initComponentPerf.call(this, oldSetData)
155+
initComponentPerf.call(_this, oldSetData)
155156
}
156157
return
157158
}
158-
if (this.$perf.auto) {
159-
const autoFn = getAutoFn.call(this)
160-
clearTimeout(this.$perf.timer)
161-
this.$perf.timer = setTimeout(() => {
162-
autoFn.call(this, this.$perf.autoArgs)
159+
if (_this.$perf.auto) {
160+
const autoFn = getAutoFn.call(_this)
161+
clearTimeout(_this.$perf.timer)
162+
_this.$perf.timer = setTimeout(() => {
163+
autoFn.call(_this, _this.$perf.autoArgs)
163164
}, 1000)
164165
}
165166
callback && callback()
@@ -203,7 +204,8 @@ Page = function (options) {
203204
const oldComponent = Component
204205
Component = function (options) {
205206
const oldCreated = options.created
206-
options.created = function (args) {
207+
// 兼容微信和支付宝
208+
options.created = options.onInit = function (args) {
207209
if (this.is === 'pages/index/index') {
208210
initPagePerf.call(this)
209211
} else {
@@ -213,6 +215,5 @@ Component = function (options) {
213215
}
214216
return oldComponent(options)
215217
}
216-
217218
export const PerfPage = Page
218219
export const PerfComponent = Component

uni-app/src/components/card.vue

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,13 @@
4545
isActive: this.item.active
4646
}
4747
},
48+
created(){
49+
// console.log('组件载入...')
50+
},
4851
methods: {
4952
trigger() {
50-
this.$mp && this.$mp.component && this.$mp.component.$perf && this.$mp.component.$perf.mark('setData')
53+
this.$scope && this.$scope.$perf && this.$scope.$perf.mark('setData')
54+
// this.$vm &&this.$vm.$mp && this.$vm.$mp.component && this.$vm.$mp.component.$perf && this.$vm.$mp.component.$perf.mark('setData')
5155
this.isActive = !this.isActive
5256
}
5357
}

uni-app/src/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Vue from 'vue'
22
import App from './App'
33

4-
//#ifdef MP-WEIXIN
4+
//#ifdef MP-WEIXIN || MP-ALIPAY
55
require('utils/perf.js')
66
//#endif
77

uni-app/src/utils/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const PERF_MAX = 50
1+
export const PERF_MAX = 10
22
export const PERF_LIKE_MAX = 20
33
export const PERF_AUTO = true
44
export const PERF_USING_COMPONENTS = true

uni-app/src/utils/interface.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export function shouldMeasurePage (data) {
1111
/**
1212
* 是否开始测试组件性能
1313
*/
14-
export function shouldMeasureComponent (data) {
14+
export function shouldMeasureComponent (data,perf) {
1515
return this.$perf.count > 0
1616
}
1717
export function getComponentTriggerFn () {

uni-app/src/utils/perf.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ class Perf {
6363
}
6464
if (this.auto) {
6565
showToast({
66-
title: '自动测试中,请稍候...',
67-
duration: 999999,
66+
title: '自动测试中,请稍候... ',
67+
// duration: 999999,
6868
icon: 'none',
69-
mask: true
69+
// mask: true
7070
})
7171
}
7272
this.count++
@@ -128,12 +128,12 @@ function wrapperSetData(namespace, shouldMeasureFn, contentFn, getAutoFn, oldSet
128128
this.setData = function setData(data, callback) {
129129
// console.log('....before', data)
130130
if (Object.keys(data).length === 0) return // 忽略空数据
131-
if (!shouldMeasureFn.call(this, data, this.$perf.autoArgs) || this.$perf.ended) {
131+
if (!shouldMeasureFn.call(this, data, this.$perf) || this.$perf.ended) {
132132
return oldSetData.call(this, data, callback)
133133
}
134134
// console.log('....after', data)
135135
this.$perf.measure('setData.before')
136-
oldSetData.call(this, data, function () {
136+
oldSetData.call(this, data, ()=> {
137137
this.$perf.measure('setData.end')
138138
if (this.$perf.isEnd()) {
139139
this.$perf.ended = true
@@ -200,7 +200,8 @@ Page = function (options) {
200200
const oldComponent = Component
201201
Component = function (options) {
202202
const oldCreated = options.created
203-
options.created = function (args) {
203+
// 兼容微信小程序和支付宝
204+
options.created = options.onInit = function (args) {
204205
if (this.is === 'pages/index/index') {
205206
initPagePerf.call(this)
206207
} else {

0 commit comments

Comments
 (0)