Skip to content

Commit

Permalink
feat(console): 补充 onSocketOpen,onSocketMessage 调用的警告信息
Browse files Browse the repository at this point in the history
  • Loading branch information
fxy060608 committed Jan 4, 2025
1 parent bcde6e1 commit 2c97be9
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 1 deletion.
26 changes: 26 additions & 0 deletions packages/uni-console/src/runtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export function initRuntimeSocketService(): Promise<boolean> {
)
return false
}
initMiniProgramGlobalFlag()
socket.onClose(() => {
if (process.env.UNI_DEBUG) {
originalConsole.log(
Expand Down Expand Up @@ -75,4 +76,29 @@ function wrapError(error: string) {
return `${ERROR_CHAR}${error}${ERROR_CHAR}`
}

function initMiniProgramGlobalFlag() {
if (typeof wx !== 'undefined') {
// @ts-expect-error
wx.__uni_console__ = true
} else if (typeof my !== 'undefined') {
my.__uni_console__ = true
} else if (typeof tt !== 'undefined') {
tt.__uni_console__ = true
} else if (typeof swan !== 'undefined') {
swan.__uni_console__ = true
} else if (typeof qq !== 'undefined') {
qq.__uni_console__ = true
} else if (typeof ks !== 'undefined') {
ks.__uni_console__ = true
} else if (typeof jd !== 'undefined') {
jd.__uni_console__ = true
} else if (typeof xhs !== 'undefined') {
xhs.__uni_console__ = true
} else if (typeof has !== 'undefined') {
has.__uni_console__ = true
} else if (typeof qa !== 'undefined') {
qa.__uni_console__ = true
}
}

initRuntimeSocketService()
8 changes: 7 additions & 1 deletion packages/uni-mp-alipay/src/api/protocols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ import {

import { getStorageSync } from './shims'

export { redirectTo, onError, offError } from '@dcloudio/uni-mp-core'
export {
redirectTo,
onError,
offError,
onSocketOpen,
onSocketMessage,
} from '@dcloudio/uni-mp-core'

function handleNetworkInfo(
fromRes: my.IGetNetworkTypeSuccessResult,
Expand Down
2 changes: 2 additions & 0 deletions packages/uni-mp-baidu/src/api/protocols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export {
getSystemInfoSync,
onError,
offError,
onSocketOpen,
onSocketMessage,
} from '@dcloudio/uni-mp-core'
import { navigateTo as _navigateTo, isSyncApi } from '@dcloudio/uni-mp-core'

Expand Down
1 change: 1 addition & 0 deletions packages/uni-mp-core/src/api/protocols/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ export { getAppBaseInfo } from './getAppBaseInfo'
export { getWindowInfo } from './getWindowInfo'
export { getAppAuthorizeSetting } from './getAppAuthorizeSetting'
export { onError, offError } from './onError'
export { onSocketOpen, onSocketMessage } from './socket'
export * from './types'
17 changes: 17 additions & 0 deletions packages/uni-mp-core/src/api/protocols/socket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { MPProtocol } from './types'

export const onSocketOpen: MPProtocol = {
args() {
if (__GLOBAL__.__uni_console__) {
if (__GLOBAL__.__uni_console_warned__) {
return
}
__GLOBAL__.__uni_console_warned__ = true
console.warn(
`开发模式下小程序日志回显会使用 socket 连接,为了避免冲突,建议使用 SocketTask 的方式去管理 WebSocket 或手动关闭日志回显功能。[详情](https://uniapp.dcloud.net.cn/tutorial/run/mp-log.html)`
)
}
},
}

export const onSocketMessage: MPProtocol = onSocketOpen
2 changes: 2 additions & 0 deletions packages/uni-mp-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export {
getAppAuthorizeSetting,
onError,
offError,
onSocketOpen,
onSocketMessage,
} from './api/protocols'
// types
export { MiniProgramAppOptions, MiniProgramAppInstance } from './runtime/app'
Expand Down
4 changes: 4 additions & 0 deletions packages/uni-mp-jd/src/api/protocols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
getSystemInfoSync,
offError,
onError,
onSocketMessage,
onSocketOpen,
previewImage,
redirectTo,
} from '@dcloudio/uni-mp-core'
Expand All @@ -15,6 +17,8 @@ export {
getSystemInfoSync,
onError,
offError,
onSocketOpen,
onSocketMessage,
}

export const navigateTo = _navigateTo()
4 changes: 4 additions & 0 deletions packages/uni-mp-kuaishou/src/api/protocols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
getSystemInfoSync,
offError,
onError,
onSocketMessage,
onSocketOpen,
previewImage,
redirectTo,
} from '@dcloudio/uni-mp-core'
Expand All @@ -27,6 +29,8 @@ export {
requestPayment,
onError,
offError,
onSocketOpen,
onSocketMessage,
}

export const navigateTo = _navigateTo()
2 changes: 2 additions & 0 deletions packages/uni-mp-qq/src/api/protocols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export {
getSystemInfoSync,
onError,
offError,
onSocketOpen,
onSocketMessage,
} from '@dcloudio/uni-mp-core'
import { navigateTo as _navigateTo } from '@dcloudio/uni-mp-core'
export const navigateTo = _navigateTo()
2 changes: 2 additions & 0 deletions packages/uni-mp-weixin/src/api/protocols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export {
getAppAuthorizeSetting,
onError,
offError,
onSocketOpen,
onSocketMessage,
} from '@dcloudio/uni-mp-core'

// #if _X_
Expand Down
4 changes: 4 additions & 0 deletions packages/uni-mp-xhs/src/api/protocols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
getSystemInfoSync,
offError,
onError,
onSocketMessage,
onSocketOpen,
previewImage,
redirectTo,
} from '@dcloudio/uni-mp-core'
Expand All @@ -15,6 +17,8 @@ export {
getSystemInfoSync,
onError,
offError,
onSocketOpen,
onSocketMessage,
}

export const navigateTo = _navigateTo()
2 changes: 2 additions & 0 deletions packages/uni-quickapp-webview/src/api/protocols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export {
getSystemInfoSync,
onError,
offError,
onSocketOpen,
onSocketMessage,
} from '@dcloudio/uni-mp-core'
import { navigateTo as _navigateTo } from '@dcloudio/uni-mp-core'
export const navigateTo = _navigateTo()

0 comments on commit 2c97be9

Please sign in to comment.