Skip to content

Commit 2c97be9

Browse files
committed
feat(console): 补充 onSocketOpen,onSocketMessage 调用的警告信息
1 parent bcde6e1 commit 2c97be9

File tree

12 files changed

+73
-1
lines changed

12 files changed

+73
-1
lines changed

packages/uni-console/src/runtime/index.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export function initRuntimeSocketService(): Promise<boolean> {
3333
)
3434
return false
3535
}
36+
initMiniProgramGlobalFlag()
3637
socket.onClose(() => {
3738
if (process.env.UNI_DEBUG) {
3839
originalConsole.log(
@@ -75,4 +76,29 @@ function wrapError(error: string) {
7576
return `${ERROR_CHAR}${error}${ERROR_CHAR}`
7677
}
7778

79+
function initMiniProgramGlobalFlag() {
80+
if (typeof wx !== 'undefined') {
81+
// @ts-expect-error
82+
wx.__uni_console__ = true
83+
} else if (typeof my !== 'undefined') {
84+
my.__uni_console__ = true
85+
} else if (typeof tt !== 'undefined') {
86+
tt.__uni_console__ = true
87+
} else if (typeof swan !== 'undefined') {
88+
swan.__uni_console__ = true
89+
} else if (typeof qq !== 'undefined') {
90+
qq.__uni_console__ = true
91+
} else if (typeof ks !== 'undefined') {
92+
ks.__uni_console__ = true
93+
} else if (typeof jd !== 'undefined') {
94+
jd.__uni_console__ = true
95+
} else if (typeof xhs !== 'undefined') {
96+
xhs.__uni_console__ = true
97+
} else if (typeof has !== 'undefined') {
98+
has.__uni_console__ = true
99+
} else if (typeof qa !== 'undefined') {
100+
qa.__uni_console__ = true
101+
}
102+
}
103+
78104
initRuntimeSocketService()

packages/uni-mp-alipay/src/api/protocols.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ import {
1010

1111
import { getStorageSync } from './shims'
1212

13-
export { redirectTo, onError, offError } from '@dcloudio/uni-mp-core'
13+
export {
14+
redirectTo,
15+
onError,
16+
offError,
17+
onSocketOpen,
18+
onSocketMessage,
19+
} from '@dcloudio/uni-mp-core'
1420

1521
function handleNetworkInfo(
1622
fromRes: my.IGetNetworkTypeSuccessResult,

packages/uni-mp-baidu/src/api/protocols.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ export {
55
getSystemInfoSync,
66
onError,
77
offError,
8+
onSocketOpen,
9+
onSocketMessage,
810
} from '@dcloudio/uni-mp-core'
911
import { navigateTo as _navigateTo, isSyncApi } from '@dcloudio/uni-mp-core'
1012

packages/uni-mp-core/src/api/protocols/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ export { getAppBaseInfo } from './getAppBaseInfo'
1414
export { getWindowInfo } from './getWindowInfo'
1515
export { getAppAuthorizeSetting } from './getAppAuthorizeSetting'
1616
export { onError, offError } from './onError'
17+
export { onSocketOpen, onSocketMessage } from './socket'
1718
export * from './types'
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import type { MPProtocol } from './types'
2+
3+
export const onSocketOpen: MPProtocol = {
4+
args() {
5+
if (__GLOBAL__.__uni_console__) {
6+
if (__GLOBAL__.__uni_console_warned__) {
7+
return
8+
}
9+
__GLOBAL__.__uni_console_warned__ = true
10+
console.warn(
11+
`开发模式下小程序日志回显会使用 socket 连接,为了避免冲突,建议使用 SocketTask 的方式去管理 WebSocket 或手动关闭日志回显功能。[详情](https://uniapp.dcloud.net.cn/tutorial/run/mp-log.html)`
12+
)
13+
}
14+
},
15+
}
16+
17+
export const onSocketMessage: MPProtocol = onSocketOpen

packages/uni-mp-core/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ export {
6464
getAppAuthorizeSetting,
6565
onError,
6666
offError,
67+
onSocketOpen,
68+
onSocketMessage,
6769
} from './api/protocols'
6870
// types
6971
export { MiniProgramAppOptions, MiniProgramAppInstance } from './runtime/app'

packages/uni-mp-jd/src/api/protocols.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import {
44
getSystemInfoSync,
55
offError,
66
onError,
7+
onSocketMessage,
8+
onSocketOpen,
79
previewImage,
810
redirectTo,
911
} from '@dcloudio/uni-mp-core'
@@ -15,6 +17,8 @@ export {
1517
getSystemInfoSync,
1618
onError,
1719
offError,
20+
onSocketOpen,
21+
onSocketMessage,
1822
}
1923

2024
export const navigateTo = _navigateTo()

packages/uni-mp-kuaishou/src/api/protocols.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import {
44
getSystemInfoSync,
55
offError,
66
onError,
7+
onSocketMessage,
8+
onSocketOpen,
79
previewImage,
810
redirectTo,
911
} from '@dcloudio/uni-mp-core'
@@ -27,6 +29,8 @@ export {
2729
requestPayment,
2830
onError,
2931
offError,
32+
onSocketOpen,
33+
onSocketMessage,
3034
}
3135

3236
export const navigateTo = _navigateTo()

packages/uni-mp-qq/src/api/protocols.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ export {
55
getSystemInfoSync,
66
onError,
77
offError,
8+
onSocketOpen,
9+
onSocketMessage,
810
} from '@dcloudio/uni-mp-core'
911
import { navigateTo as _navigateTo } from '@dcloudio/uni-mp-core'
1012
export const navigateTo = _navigateTo()

packages/uni-mp-weixin/src/api/protocols.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export {
1212
getAppAuthorizeSetting,
1313
onError,
1414
offError,
15+
onSocketOpen,
16+
onSocketMessage,
1517
} from '@dcloudio/uni-mp-core'
1618

1719
// #if _X_

0 commit comments

Comments
 (0)