diff --git a/packages/uni-console/src/runtime/index.ts b/packages/uni-console/src/runtime/index.ts index 146d0419b6f..32a93af8a3f 100644 --- a/packages/uni-console/src/runtime/index.ts +++ b/packages/uni-console/src/runtime/index.ts @@ -33,6 +33,7 @@ export function initRuntimeSocketService(): Promise { ) return false } + initMiniProgramGlobalFlag() socket.onClose(() => { if (process.env.UNI_DEBUG) { originalConsole.log( @@ -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() diff --git a/packages/uni-mp-alipay/src/api/protocols.ts b/packages/uni-mp-alipay/src/api/protocols.ts index d40b6a1344b..37a817335f4 100644 --- a/packages/uni-mp-alipay/src/api/protocols.ts +++ b/packages/uni-mp-alipay/src/api/protocols.ts @@ -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, diff --git a/packages/uni-mp-baidu/src/api/protocols.ts b/packages/uni-mp-baidu/src/api/protocols.ts index 4480140b4f6..041fc9e2ef2 100644 --- a/packages/uni-mp-baidu/src/api/protocols.ts +++ b/packages/uni-mp-baidu/src/api/protocols.ts @@ -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' diff --git a/packages/uni-mp-core/src/api/protocols/index.ts b/packages/uni-mp-core/src/api/protocols/index.ts index 46e89aa766b..2c4bea0823b 100644 --- a/packages/uni-mp-core/src/api/protocols/index.ts +++ b/packages/uni-mp-core/src/api/protocols/index.ts @@ -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' diff --git a/packages/uni-mp-core/src/api/protocols/socket.ts b/packages/uni-mp-core/src/api/protocols/socket.ts new file mode 100644 index 00000000000..3d95185e755 --- /dev/null +++ b/packages/uni-mp-core/src/api/protocols/socket.ts @@ -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 diff --git a/packages/uni-mp-core/src/index.ts b/packages/uni-mp-core/src/index.ts index f4d8d89a30d..9bde923537b 100644 --- a/packages/uni-mp-core/src/index.ts +++ b/packages/uni-mp-core/src/index.ts @@ -64,6 +64,8 @@ export { getAppAuthorizeSetting, onError, offError, + onSocketOpen, + onSocketMessage, } from './api/protocols' // types export { MiniProgramAppOptions, MiniProgramAppInstance } from './runtime/app' diff --git a/packages/uni-mp-jd/src/api/protocols.ts b/packages/uni-mp-jd/src/api/protocols.ts index 00e352e13e1..4ecf4653efc 100644 --- a/packages/uni-mp-jd/src/api/protocols.ts +++ b/packages/uni-mp-jd/src/api/protocols.ts @@ -4,6 +4,8 @@ import { getSystemInfoSync, offError, onError, + onSocketMessage, + onSocketOpen, previewImage, redirectTo, } from '@dcloudio/uni-mp-core' @@ -15,6 +17,8 @@ export { getSystemInfoSync, onError, offError, + onSocketOpen, + onSocketMessage, } export const navigateTo = _navigateTo() diff --git a/packages/uni-mp-kuaishou/src/api/protocols.ts b/packages/uni-mp-kuaishou/src/api/protocols.ts index 5227a420921..3002aa8bc6c 100644 --- a/packages/uni-mp-kuaishou/src/api/protocols.ts +++ b/packages/uni-mp-kuaishou/src/api/protocols.ts @@ -4,6 +4,8 @@ import { getSystemInfoSync, offError, onError, + onSocketMessage, + onSocketOpen, previewImage, redirectTo, } from '@dcloudio/uni-mp-core' @@ -27,6 +29,8 @@ export { requestPayment, onError, offError, + onSocketOpen, + onSocketMessage, } export const navigateTo = _navigateTo() diff --git a/packages/uni-mp-qq/src/api/protocols.ts b/packages/uni-mp-qq/src/api/protocols.ts index fb96b14eb2d..ce9eaaedcb9 100644 --- a/packages/uni-mp-qq/src/api/protocols.ts +++ b/packages/uni-mp-qq/src/api/protocols.ts @@ -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() diff --git a/packages/uni-mp-weixin/src/api/protocols.ts b/packages/uni-mp-weixin/src/api/protocols.ts index 82f1bb24822..36dede232d3 100644 --- a/packages/uni-mp-weixin/src/api/protocols.ts +++ b/packages/uni-mp-weixin/src/api/protocols.ts @@ -12,6 +12,8 @@ export { getAppAuthorizeSetting, onError, offError, + onSocketOpen, + onSocketMessage, } from '@dcloudio/uni-mp-core' // #if _X_ diff --git a/packages/uni-mp-xhs/src/api/protocols.ts b/packages/uni-mp-xhs/src/api/protocols.ts index 00e352e13e1..4ecf4653efc 100644 --- a/packages/uni-mp-xhs/src/api/protocols.ts +++ b/packages/uni-mp-xhs/src/api/protocols.ts @@ -4,6 +4,8 @@ import { getSystemInfoSync, offError, onError, + onSocketMessage, + onSocketOpen, previewImage, redirectTo, } from '@dcloudio/uni-mp-core' @@ -15,6 +17,8 @@ export { getSystemInfoSync, onError, offError, + onSocketOpen, + onSocketMessage, } export const navigateTo = _navigateTo() diff --git a/packages/uni-quickapp-webview/src/api/protocols.ts b/packages/uni-quickapp-webview/src/api/protocols.ts index fb96b14eb2d..ce9eaaedcb9 100644 --- a/packages/uni-quickapp-webview/src/api/protocols.ts +++ b/packages/uni-quickapp-webview/src/api/protocols.ts @@ -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()