vite-uni-dev-tool 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +46 -0
- package/dist/const.cjs +1 -1
- package/dist/const.d.ts +12 -0
- package/dist/const.d.ts.map +1 -1
- package/dist/const.js +1 -1
- package/dist/core-shared.d.ts +1 -1
- package/dist/core-shared.d.ts.map +1 -1
- package/dist/core-shared.js +1 -1
- package/dist/core.d.ts +10 -3
- package/dist/core.d.ts.map +1 -1
- package/dist/core.js +2 -2
- package/dist/i18n/locales/en.cjs +1 -1
- package/dist/i18n/locales/en.d.ts +81 -0
- package/dist/i18n/locales/en.d.ts.map +1 -1
- package/dist/i18n/locales/en.js +1 -1
- package/dist/i18n/locales/zh-Hans.cjs +1 -1
- package/dist/i18n/locales/zh-Hans.d.ts +82 -1
- package/dist/i18n/locales/zh-Hans.d.ts.map +1 -1
- package/dist/i18n/locales/zh-Hans.js +1 -1
- package/dist/modules/devConsole/index.cjs +1 -1
- package/dist/modules/devConsole/index.js +3 -3
- package/dist/modules/devEvent/index.cjs +3 -3
- package/dist/modules/devEvent/index.d.ts +1 -0
- package/dist/modules/devEvent/index.d.ts.map +1 -1
- package/dist/modules/devEvent/index.js +3 -3
- package/dist/modules/devIntercept/index.cjs +14 -13
- package/dist/modules/devIntercept/index.d.ts +19 -0
- package/dist/modules/devIntercept/index.d.ts.map +1 -1
- package/dist/modules/devIntercept/index.js +14 -13
- package/dist/modules/devStore/index.cjs +1 -1
- package/dist/modules/devStore/index.d.ts +21 -0
- package/dist/modules/devStore/index.d.ts.map +1 -1
- package/dist/modules/devStore/index.js +1 -1
- package/dist/plugins/uniDevTool/transform/transformMain.cjs +1 -1
- package/dist/plugins/uniDevTool/transform/transformMain.js +1 -1
- package/dist/type.d.ts +47 -2
- package/dist/type.d.ts.map +1 -1
- package/dist/utils/language.cjs +1 -1
- package/dist/utils/language.d.ts.map +1 -1
- package/dist/utils/language.js +1 -1
- package/dist/utils/object.cjs +1 -1
- package/dist/utils/object.d.ts.map +1 -1
- package/dist/utils/object.js +1 -1
- package/dist/v3/DevTool/components/BluetoothList/BluetoothItem.vue +199 -0
- package/dist/v3/DevTool/components/BluetoothList/BluetoothTool.vue +730 -0
- package/dist/v3/DevTool/components/BluetoothList/index.vue +167 -0
- package/dist/v3/{CaptureScreen → DevTool/components/CaptureScreen}/index.vue +109 -109
- package/dist/v3/{ConsoleList → DevTool/components/ConsoleList}/ConsoleItem.vue +225 -224
- package/dist/v3/{ConsoleList → DevTool/components/ConsoleList}/RunJSInput.vue +247 -249
- package/dist/v3/{ConsoleList → DevTool/components/ConsoleList}/index.vue +171 -160
- package/dist/v3/{ConsoleList → DevTool/components/ConsoleList}/staticTips.ts +1145 -1145
- package/dist/v3/{DevToolButton → DevTool/components/DevToolButton}/index.vue +7 -4
- package/dist/v3/{DevToolTitle → DevTool/components/DevToolTitle}/index.vue +24 -24
- package/dist/v3/{DevToolWindow → DevTool/components/DevToolWindow}/DevToolOverlay.vue +197 -182
- package/dist/v3/{DevToolWindow → DevTool/components/DevToolWindow}/const.ts +28 -5
- package/dist/v3/{DevToolWindow → DevTool/components/DevToolWindow}/hooks/dataUtils.ts +48 -48
- package/dist/v3/{DevToolWindow → DevTool/components/DevToolWindow}/hooks/useDevToolData.ts +387 -338
- package/dist/v3/{DevToolWindow → DevTool/components/DevToolWindow}/hooks/useDevToolHandlers.ts +629 -549
- package/dist/v3/{DevToolWindow → DevTool/components/DevToolWindow}/hooks/useDevToolOverlay.ts +197 -184
- package/dist/v3/{DevToolWindow → DevTool/components/DevToolWindow}/index.vue +67 -16
- package/dist/v3/{ElEvent → DevTool/components/ElEvent}/ElEventItem.vue +105 -105
- package/dist/v3/{ElEvent → DevTool/components/ElEvent}/index.vue +106 -109
- package/dist/v3/{Instance → DevTool/components/Instance}/components/InstanceTreeNode.vue +265 -265
- package/dist/v3/{Instance → DevTool/components/Instance}/flatten.ts +226 -226
- package/dist/v3/{Instance → DevTool/components/Instance}/index.vue +94 -94
- package/dist/v3/{Instance → DevTool/components/Instance}/registry.ts +49 -49
- package/dist/v3/{Instance → DevTool/components/Instance}/transformTree.ts +375 -375
- package/dist/v3/{Instance → DevTool/components/Instance}/transformTreeCtx.ts +268 -268
- package/dist/v3/{Instance → DevTool/components/Instance}/typing.d.ts +43 -43
- package/dist/v3/{InstanceDetail → DevTool/components/InstanceDetail}/index.vue +485 -485
- package/dist/v3/{JsonDetail → DevTool/components/JsonDetail}/index.vue +70 -70
- package/dist/v3/{NFCList → DevTool/components/NFCList}/NFCItem.vue +112 -113
- package/dist/v3/{NFCList → DevTool/components/NFCList}/NFCTool.vue +454 -478
- package/dist/v3/{NFCList → DevTool/components/NFCList}/const.ts +56 -56
- package/dist/v3/{NFCList → DevTool/components/NFCList}/index.vue +94 -98
- package/dist/v3/{NetworkList → DevTool/components/NetworkList}/InterceptConfig.vue +624 -608
- package/dist/v3/{NetworkList → DevTool/components/NetworkList}/InterceptItem.vue +140 -140
- package/dist/v3/{NetworkList → DevTool/components/NetworkList}/NetworkDetail.vue +287 -296
- package/dist/v3/{NetworkList → DevTool/components/NetworkList}/NetworkIntercept.vue +88 -93
- package/dist/v3/{NetworkList → DevTool/components/NetworkList}/NetworkItem.vue +163 -167
- package/dist/v3/{NetworkList → DevTool/components/NetworkList}/NetworkSend.vue +589 -556
- package/dist/v3/{NetworkList → DevTool/components/NetworkList}/const.ts +4 -4
- package/dist/v3/{NetworkList → DevTool/components/NetworkList}/hooks/useNetworkForm.ts +86 -86
- package/dist/v3/{NetworkList → DevTool/components/NetworkList}/index.vue +160 -160
- package/dist/v3/{NetworkList → DevTool/components/NetworkList}/utils.ts +101 -101
- package/dist/v3/{Performance → DevTool/components/Performance}/index.vue +498 -495
- package/dist/v3/{Performance → DevTool/components/Performance}/modules/PerformanceMetrics.vue +153 -153
- package/dist/v3/{Performance → DevTool/components/Performance}/modules/PerformanceWidget.vue +12 -9
- package/dist/v3/{Performance → DevTool/components/Performance}/modules/usePerformanceChart.ts +460 -460
- package/dist/v3/{Performance → DevTool/components/Performance}/modules/usePerformanceData.ts +258 -258
- package/dist/v3/{PiniaList → DevTool/components/PiniaList}/index.vue +93 -94
- package/dist/v3/{RouteList → DevTool/components/RouteList}/index.vue +21 -24
- package/dist/v3/{RunJS → DevTool/components/RunJS}/index.vue +148 -148
- package/dist/v3/{ScanCodeList → DevTool/components/ScanCodeList}/ScanCodeItem.vue +97 -98
- package/dist/v3/{ScanCodeList → DevTool/components/ScanCodeList}/index.vue +100 -104
- package/dist/v3/{SettingButton → DevTool/components/SettingButton}/index.vue +45 -45
- package/dist/v3/{SettingList → DevTool/components/SettingList}/index.vue +218 -150
- package/dist/v3/DevTool/components/SettingList/modules/SettingBarrage.vue +304 -0
- package/dist/v3/{SettingList → DevTool/components/SettingList}/modules/SettingDevTool.vue +212 -208
- package/dist/v3/{SettingList → DevTool/components/SettingList}/modules/SettingInfo.vue +157 -119
- package/dist/v3/{SettingList → DevTool/components/SettingList}/modules/SettingLanguage.vue +74 -74
- package/dist/v3/{SettingList → DevTool/components/SettingList}/modules/SettingLog.vue +230 -230
- package/dist/v3/{SettingList → DevTool/components/SettingList}/modules/SettingNetwork.vue +3 -3
- package/dist/v3/{SettingList → DevTool/components/SettingList}/modules/SettingTheme.vue +37 -7
- package/dist/v3/{SettingList → DevTool/components/SettingList}/typing.d.ts +2 -2
- package/dist/v3/{SourceCode → DevTool/components/SourceCode}/Line.vue +127 -116
- package/dist/v3/{SourceCode → DevTool/components/SourceCode}/index.vue +8 -8
- package/dist/v3/{SourceCode → DevTool/components/SourceCode}/parseCode.ts +609 -701
- package/dist/v3/{StorageList → DevTool/components/StorageList}/index.vue +174 -174
- package/dist/v3/{TransferList → DevTool/components/TransferList}/TransferDetail.vue +268 -268
- package/dist/v3/{TransferList → DevTool/components/TransferList}/TransferItem.vue +4 -4
- package/dist/v3/{TransferList → DevTool/components/TransferList}/index.vue +8 -8
- package/dist/v3/{UniEvent → DevTool/components/UniEvent}/UniEventItem.vue +6 -7
- package/dist/v3/{UniEvent → DevTool/components/UniEvent}/index.vue +6 -6
- package/dist/v3/{VuexList → DevTool/components/VuexList}/index.vue +84 -84
- package/dist/v3/{WebSocket → DevTool/components/WebSocket}/WebSocketDetail.vue +8 -8
- package/dist/v3/{WebSocket → DevTool/components/WebSocket}/WebSocketItem.vue +4 -4
- package/dist/v3/{WebSocket → DevTool/components/WebSocket}/index.vue +8 -8
- package/dist/v3/DevTool/index.vue +179 -5
- package/dist/v3/{AppTransition → components/AppTransition}/index.vue +176 -170
- package/dist/v3/{AutoSizer → components/AutoSizer}/index.vue +192 -192
- package/dist/v3/{AutoSizer → components/AutoSizer}/index1.vue +184 -184
- package/dist/v3/{AutoSizer → components/AutoSizer}/utils.ts +49 -49
- package/dist/v3/components/Barrage/BarrageItem.vue +137 -0
- package/dist/v3/components/Barrage/index.vue +202 -0
- package/dist/v3/{CircularButton → components/CircularButton}/index.vue +84 -84
- package/dist/v3/{CustomSwiper → components/CustomSwiper}/CustomSwiperItem.vue +49 -49
- package/dist/v3/{CustomSwiper → components/CustomSwiper}/index.vue +104 -104
- package/dist/v3/{DraggableContainer → components/DraggableContainer}/index.vue +1 -1
- package/dist/v3/{Empty → components/Empty}/index.vue +29 -29
- package/dist/v3/{FilterInput → components/FilterInput}/index.vue +1 -1
- package/dist/v3/{FilterSelect → components/FilterSelect}/index.vue +179 -179
- package/dist/v3/{JsonPretty → components/JsonPretty}/components/Brackets/index.vue +27 -27
- package/dist/v3/{JsonPretty → components/JsonPretty}/components/Carets/index.vue +59 -59
- package/dist/v3/{JsonPretty → components/JsonPretty}/components/CheckController/index.vue +136 -136
- package/dist/v3/{JsonPretty → components/JsonPretty}/components/TreeNode/index.vue +387 -381
- package/dist/v3/{JsonPretty → components/JsonPretty}/hooks/useClipboard.ts +21 -21
- package/dist/v3/{JsonPretty → components/JsonPretty}/hooks/useError.ts +21 -21
- package/dist/v3/{JsonPretty → components/JsonPretty}/index.vue +16 -13
- package/dist/v3/{JsonPretty → components/JsonPretty}/type.ts +127 -126
- package/dist/v3/{JsonPretty → components/JsonPretty}/utils/index.ts +169 -169
- package/dist/v3/{MovableContainer → components/MovableContainer}/index.vue +1 -1
- package/dist/v3/{Pick → components/Pick}/index.vue +322 -322
- package/dist/v3/{Tabs → components/Tabs}/index.vue +30 -4
- package/dist/v3/{Tag → components/Tag}/index.vue +113 -113
- package/dist/v3/{VirtualList → components/VirtualList}/AutoSize.vue +40 -40
- package/dist/v3/{VirtualList → components/VirtualList}/index.vue +416 -412
- package/dist/v3/hooks/useBluetooth/index.ts +561 -0
- package/dist/v3/hooks/useContainerStyle.ts +153 -153
- package/dist/v3/hooks/useNFC/index.ts +107 -107
- package/dist/v3/hooks/useNFC/typing.d.ts +396 -396
- package/dist/v3/hooks/useNFC/useNFCAndroid.ts +966 -966
- package/dist/v3/hooks/useNFC/useNFCMpWeiXin.ts +812 -812
- package/dist/v3/hooks/useNFC/utils.ts +754 -754
- package/dist/v3/hooks/useRequest/index.ts +586 -573
- package/dist/v3/hooks/useRequest/utils.ts +267 -267
- package/dist/v3/hooks/useScanCode/index.ts +206 -206
- package/dist/v3/hooks/useWebsocket/README.md +79 -0
- package/dist/v3/hooks/useWebsocket/index.ts +253 -0
- package/dist/v3/styles/theme.css +17 -10
- package/dist/v3/styles/theme.ts +12 -12
- package/package.json +59 -64
- package/dist/plugins/uniParseStock/index.d.ts +0 -10
- package/dist/plugins/uniParseStock/index.d.ts.map +0 -1
- /package/dist/v3/{DevToolWindow → DevTool/components/DevToolWindow}/index.css +0 -0
- /package/dist/v3/{SettingList → DevTool/components/SettingList}/index.css +0 -0
- /package/dist/v3/{Empty → components/Empty}/empty.png +0 -0
- /package/dist/v3/{VirtualList → components/VirtualList}/readme.md +0 -0
|
@@ -1,206 +1,206 @@
|
|
|
1
|
-
import { onHide, onShow, onUnload } from '@dcloudio/uni-app';
|
|
2
|
-
import { ref, watch } from 'vue';
|
|
3
|
-
import type { Ref } from 'vue';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* android PDA
|
|
7
|
-
*
|
|
8
|
-
* h5 扫码枪
|
|
9
|
-
*
|
|
10
|
-
* @export
|
|
11
|
-
* @param {(code: string) => void} callback
|
|
12
|
-
* @param {{
|
|
13
|
-
* disabled?: Ref<boolean>;
|
|
14
|
-
* broadcastAction?: string;
|
|
15
|
-
* broadcastStringDataLabel?: string;
|
|
16
|
-
* scanDelay?: number;
|
|
17
|
-
* onScanEnd: (code: string) => void;
|
|
18
|
-
* }} [options]
|
|
19
|
-
*/
|
|
20
|
-
export default function useScanCode(
|
|
21
|
-
callback: (code: string) => void,
|
|
22
|
-
options?: {
|
|
23
|
-
/** 禁用扫描 */
|
|
24
|
-
disabled?: Ref<boolean>;
|
|
25
|
-
/** 广播动作 */
|
|
26
|
-
broadcastAction?: string;
|
|
27
|
-
/** 广播标签 */
|
|
28
|
-
broadcastStringDataLabel?: string;
|
|
29
|
-
/**
|
|
30
|
-
* 扫描延迟 单位 ms
|
|
31
|
-
* 默认 150
|
|
32
|
-
*/
|
|
33
|
-
scanDelay?: number;
|
|
34
|
-
/**
|
|
35
|
-
* 扫描结束
|
|
36
|
-
* @param {string} code
|
|
37
|
-
*/
|
|
38
|
-
onScanEnd?: (code: string) => void;
|
|
39
|
-
},
|
|
40
|
-
) {
|
|
41
|
-
let main: any;
|
|
42
|
-
const show = ref(true);
|
|
43
|
-
|
|
44
|
-
let lastTime: number | null = null;
|
|
45
|
-
let nextTime: number | null = null;
|
|
46
|
-
let lastCode: string | null = null;
|
|
47
|
-
let nextCode: string | null = null;
|
|
48
|
-
|
|
49
|
-
let code = '';
|
|
50
|
-
|
|
51
|
-
let _codeQueryTag = false;
|
|
52
|
-
|
|
53
|
-
let receiver: PlusAndroidInstanceObject;
|
|
54
|
-
|
|
55
|
-
let filter: {
|
|
56
|
-
addAction: (arg0: string) => void;
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
// #ifdef APP-PLUS
|
|
60
|
-
|
|
61
|
-
function startScan() {
|
|
62
|
-
main.registerReceiver(receiver, filter);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
function stopScan() {
|
|
66
|
-
main.unregisterReceiver(receiver);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function queryCode(code: string) {
|
|
70
|
-
if (_codeQueryTag) return false;
|
|
71
|
-
|
|
72
|
-
_codeQueryTag = true;
|
|
73
|
-
|
|
74
|
-
setTimeout(function () {
|
|
75
|
-
_codeQueryTag = false;
|
|
76
|
-
}, options?.scanDelay || 150);
|
|
77
|
-
|
|
78
|
-
if (show.value) {
|
|
79
|
-
callback?.(code);
|
|
80
|
-
options?.onScanEnd?.(code);
|
|
81
|
-
// 单独使用该 hooks 时,删除收集
|
|
82
|
-
// @ts-ignore
|
|
83
|
-
uni?.__dev_tool_scan_code_collection__?.(code, 'broadcast');
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
function initScan() {
|
|
88
|
-
if (!options?.broadcastAction || !options.broadcastStringDataLabel) {
|
|
89
|
-
throw new Error(
|
|
90
|
-
'PAD 扫描必须传入 broadcastAction, broadcastStringDataLabel',
|
|
91
|
-
);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
main = plus.android.runtimeMainActivity(); //获取activity
|
|
95
|
-
|
|
96
|
-
const IntentFilter: any = plus.android.importClass(
|
|
97
|
-
'android.content.IntentFilter',
|
|
98
|
-
);
|
|
99
|
-
|
|
100
|
-
filter = new IntentFilter();
|
|
101
|
-
|
|
102
|
-
filter.addAction(options?.broadcastAction); // 换你的广播动作
|
|
103
|
-
|
|
104
|
-
receiver = plus.android.implements(
|
|
105
|
-
'io.dcloud.feature.internal.reflect.BroadcastReceiver',
|
|
106
|
-
{
|
|
107
|
-
onReceive: function (_context: any, intent: any) {
|
|
108
|
-
plus.android.importClass(intent);
|
|
109
|
-
const code = intent.getStringExtra(options?.broadcastStringDataLabel); // 换你的广播标签
|
|
110
|
-
queryCode(code);
|
|
111
|
-
},
|
|
112
|
-
},
|
|
113
|
-
);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
onShow(() => {
|
|
117
|
-
initScan();
|
|
118
|
-
startScan();
|
|
119
|
-
show.value = !options?.disabled?.value;
|
|
120
|
-
});
|
|
121
|
-
onHide(() => {
|
|
122
|
-
show.value = false;
|
|
123
|
-
stopScan();
|
|
124
|
-
});
|
|
125
|
-
onUnload(() => {
|
|
126
|
-
show.value = false;
|
|
127
|
-
stopScan();
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
watch(
|
|
131
|
-
() => [options?.disabled?.value],
|
|
132
|
-
() => {
|
|
133
|
-
show.value = !options?.disabled?.value;
|
|
134
|
-
},
|
|
135
|
-
);
|
|
136
|
-
|
|
137
|
-
// #endif
|
|
138
|
-
|
|
139
|
-
// #ifdef H5
|
|
140
|
-
|
|
141
|
-
function queryCodeH5(e: KeyboardEvent) {
|
|
142
|
-
if (_codeQueryTag) return;
|
|
143
|
-
|
|
144
|
-
if (e.key === 'Enter') {
|
|
145
|
-
e.preventDefault();
|
|
146
|
-
}
|
|
147
|
-
if (e.key === 'CapsLock') {
|
|
148
|
-
e.preventDefault();
|
|
149
|
-
}
|
|
150
|
-
nextCode = e.key;
|
|
151
|
-
|
|
152
|
-
nextTime = Date.now();
|
|
153
|
-
|
|
154
|
-
if (lastCode !== null && lastTime !== null && nextTime - lastTime <= 40) {
|
|
155
|
-
code += lastCode;
|
|
156
|
-
} else if (
|
|
157
|
-
lastCode !== null &&
|
|
158
|
-
lastTime !== null &&
|
|
159
|
-
nextTime - lastTime > 100
|
|
160
|
-
) {
|
|
161
|
-
code = '';
|
|
162
|
-
}
|
|
163
|
-
lastCode = nextCode;
|
|
164
|
-
lastTime = nextTime;
|
|
165
|
-
|
|
166
|
-
if (e.key === 'Enter' && code.length > 0) {
|
|
167
|
-
_codeQueryTag = true;
|
|
168
|
-
setTimeout(function () {
|
|
169
|
-
_codeQueryTag = false;
|
|
170
|
-
}, options?.scanDelay || 150);
|
|
171
|
-
callback?.(code);
|
|
172
|
-
options?.onScanEnd?.(code);
|
|
173
|
-
// 单独使用该 hooks 时,删除收集
|
|
174
|
-
// @ts-ignore
|
|
175
|
-
uni?.__dev_tool_scan_code_collection__?.(code, 'keypress');
|
|
176
|
-
code = '';
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
onShow(() => {
|
|
181
|
-
show.value = true;
|
|
182
|
-
});
|
|
183
|
-
onHide(() => {
|
|
184
|
-
window.removeEventListener('keypress', queryCodeH5);
|
|
185
|
-
});
|
|
186
|
-
onUnload(() => {
|
|
187
|
-
window.removeEventListener('keypress', queryCodeH5);
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
watch(
|
|
191
|
-
() => [options?.disabled?.value, show.value],
|
|
192
|
-
() => {
|
|
193
|
-
if (!options?.disabled?.value && show.value) {
|
|
194
|
-
window.removeEventListener('keypress', queryCodeH5);
|
|
195
|
-
window.addEventListener('keypress', queryCodeH5);
|
|
196
|
-
} else {
|
|
197
|
-
window.removeEventListener('keypress', queryCodeH5);
|
|
198
|
-
}
|
|
199
|
-
},
|
|
200
|
-
{
|
|
201
|
-
immediate: true,
|
|
202
|
-
},
|
|
203
|
-
);
|
|
204
|
-
|
|
205
|
-
// #endif
|
|
206
|
-
}
|
|
1
|
+
import { onHide, onShow, onUnload } from '@dcloudio/uni-app';
|
|
2
|
+
import { ref, watch } from 'vue';
|
|
3
|
+
import type { Ref } from 'vue';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* android PDA
|
|
7
|
+
*
|
|
8
|
+
* h5 扫码枪
|
|
9
|
+
*
|
|
10
|
+
* @export
|
|
11
|
+
* @param {(code: string) => void} callback
|
|
12
|
+
* @param {{
|
|
13
|
+
* disabled?: Ref<boolean>;
|
|
14
|
+
* broadcastAction?: string;
|
|
15
|
+
* broadcastStringDataLabel?: string;
|
|
16
|
+
* scanDelay?: number;
|
|
17
|
+
* onScanEnd: (code: string) => void;
|
|
18
|
+
* }} [options]
|
|
19
|
+
*/
|
|
20
|
+
export default function useScanCode(
|
|
21
|
+
callback: (code: string) => void,
|
|
22
|
+
options?: {
|
|
23
|
+
/** 禁用扫描 */
|
|
24
|
+
disabled?: Ref<boolean>;
|
|
25
|
+
/** 广播动作 */
|
|
26
|
+
broadcastAction?: string;
|
|
27
|
+
/** 广播标签 */
|
|
28
|
+
broadcastStringDataLabel?: string;
|
|
29
|
+
/**
|
|
30
|
+
* 扫描延迟 单位 ms
|
|
31
|
+
* 默认 150
|
|
32
|
+
*/
|
|
33
|
+
scanDelay?: number;
|
|
34
|
+
/**
|
|
35
|
+
* 扫描结束
|
|
36
|
+
* @param {string} code
|
|
37
|
+
*/
|
|
38
|
+
onScanEnd?: (code: string) => void;
|
|
39
|
+
},
|
|
40
|
+
) {
|
|
41
|
+
let main: any;
|
|
42
|
+
const show = ref(true);
|
|
43
|
+
|
|
44
|
+
let lastTime: number | null = null;
|
|
45
|
+
let nextTime: number | null = null;
|
|
46
|
+
let lastCode: string | null = null;
|
|
47
|
+
let nextCode: string | null = null;
|
|
48
|
+
|
|
49
|
+
let code = '';
|
|
50
|
+
|
|
51
|
+
let _codeQueryTag = false;
|
|
52
|
+
|
|
53
|
+
let receiver: PlusAndroidInstanceObject;
|
|
54
|
+
|
|
55
|
+
let filter: {
|
|
56
|
+
addAction: (arg0: string) => void;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
// #ifdef APP-PLUS
|
|
60
|
+
|
|
61
|
+
function startScan() {
|
|
62
|
+
main.registerReceiver(receiver, filter);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function stopScan() {
|
|
66
|
+
main.unregisterReceiver(receiver);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function queryCode(code: string) {
|
|
70
|
+
if (_codeQueryTag) return false;
|
|
71
|
+
|
|
72
|
+
_codeQueryTag = true;
|
|
73
|
+
|
|
74
|
+
setTimeout(function () {
|
|
75
|
+
_codeQueryTag = false;
|
|
76
|
+
}, options?.scanDelay || 150);
|
|
77
|
+
|
|
78
|
+
if (show.value) {
|
|
79
|
+
callback?.(code);
|
|
80
|
+
options?.onScanEnd?.(code);
|
|
81
|
+
// 单独使用该 hooks 时,删除收集
|
|
82
|
+
// @ts-ignore
|
|
83
|
+
uni?.__dev_tool_scan_code_collection__?.(code, 'broadcast');
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function initScan() {
|
|
88
|
+
if (!options?.broadcastAction || !options.broadcastStringDataLabel) {
|
|
89
|
+
throw new Error(
|
|
90
|
+
'PAD 扫描必须传入 broadcastAction, broadcastStringDataLabel',
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
main = plus.android.runtimeMainActivity(); //获取activity
|
|
95
|
+
|
|
96
|
+
const IntentFilter: any = plus.android.importClass(
|
|
97
|
+
'android.content.IntentFilter',
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
filter = new IntentFilter();
|
|
101
|
+
|
|
102
|
+
filter.addAction(options?.broadcastAction); // 换你的广播动作
|
|
103
|
+
|
|
104
|
+
receiver = plus.android.implements(
|
|
105
|
+
'io.dcloud.feature.internal.reflect.BroadcastReceiver',
|
|
106
|
+
{
|
|
107
|
+
onReceive: function (_context: any, intent: any) {
|
|
108
|
+
plus.android.importClass(intent);
|
|
109
|
+
const code = intent.getStringExtra(options?.broadcastStringDataLabel); // 换你的广播标签
|
|
110
|
+
queryCode(code);
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
onShow(() => {
|
|
117
|
+
initScan();
|
|
118
|
+
startScan();
|
|
119
|
+
show.value = !options?.disabled?.value;
|
|
120
|
+
});
|
|
121
|
+
onHide(() => {
|
|
122
|
+
show.value = false;
|
|
123
|
+
stopScan();
|
|
124
|
+
});
|
|
125
|
+
onUnload(() => {
|
|
126
|
+
show.value = false;
|
|
127
|
+
stopScan();
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
watch(
|
|
131
|
+
() => [options?.disabled?.value],
|
|
132
|
+
() => {
|
|
133
|
+
show.value = !options?.disabled?.value;
|
|
134
|
+
},
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
// #endif
|
|
138
|
+
|
|
139
|
+
// #ifdef H5
|
|
140
|
+
|
|
141
|
+
function queryCodeH5(e: KeyboardEvent) {
|
|
142
|
+
if (_codeQueryTag) return;
|
|
143
|
+
|
|
144
|
+
if (e.key === 'Enter') {
|
|
145
|
+
e.preventDefault();
|
|
146
|
+
}
|
|
147
|
+
if (e.key === 'CapsLock') {
|
|
148
|
+
e.preventDefault();
|
|
149
|
+
}
|
|
150
|
+
nextCode = e.key;
|
|
151
|
+
|
|
152
|
+
nextTime = Date.now();
|
|
153
|
+
|
|
154
|
+
if (lastCode !== null && lastTime !== null && nextTime - lastTime <= 40) {
|
|
155
|
+
code += lastCode;
|
|
156
|
+
} else if (
|
|
157
|
+
lastCode !== null &&
|
|
158
|
+
lastTime !== null &&
|
|
159
|
+
nextTime - lastTime > 100
|
|
160
|
+
) {
|
|
161
|
+
code = '';
|
|
162
|
+
}
|
|
163
|
+
lastCode = nextCode;
|
|
164
|
+
lastTime = nextTime;
|
|
165
|
+
|
|
166
|
+
if (e.key === 'Enter' && code.length > 0) {
|
|
167
|
+
_codeQueryTag = true;
|
|
168
|
+
setTimeout(function () {
|
|
169
|
+
_codeQueryTag = false;
|
|
170
|
+
}, options?.scanDelay || 150);
|
|
171
|
+
callback?.(code);
|
|
172
|
+
options?.onScanEnd?.(code);
|
|
173
|
+
// 单独使用该 hooks 时,删除收集
|
|
174
|
+
// @ts-ignore
|
|
175
|
+
uni?.__dev_tool_scan_code_collection__?.(code, 'keypress');
|
|
176
|
+
code = '';
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
onShow(() => {
|
|
181
|
+
show.value = true;
|
|
182
|
+
});
|
|
183
|
+
onHide(() => {
|
|
184
|
+
window.removeEventListener('keypress', queryCodeH5);
|
|
185
|
+
});
|
|
186
|
+
onUnload(() => {
|
|
187
|
+
window.removeEventListener('keypress', queryCodeH5);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
watch(
|
|
191
|
+
() => [options?.disabled?.value, show.value],
|
|
192
|
+
() => {
|
|
193
|
+
if (!options?.disabled?.value && show.value) {
|
|
194
|
+
window.removeEventListener('keypress', queryCodeH5);
|
|
195
|
+
window.addEventListener('keypress', queryCodeH5);
|
|
196
|
+
} else {
|
|
197
|
+
window.removeEventListener('keypress', queryCodeH5);
|
|
198
|
+
}
|
|
199
|
+
},
|
|
200
|
+
{
|
|
201
|
+
immediate: true,
|
|
202
|
+
},
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
// #endif
|
|
206
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# useWebsocket
|
|
2
|
+
|
|
3
|
+
一个用于在 uni-app 中处理 WebSocket 的 Vue 3 Hook,设计模式参考了 [ahooks useWebSocket](https://ahooks.js.org/zh-CN/hooks/use-web-socket)。
|
|
4
|
+
|
|
5
|
+
## 作用
|
|
6
|
+
|
|
7
|
+
- **简化连接管理**:封装了 `uni.connectSocket`,自动管理 `SocketTask` 任务的创建与清理。
|
|
8
|
+
- **响应式状态**:通过 Vue 3 的 `ref` 提供连接状态 (`readyState`) 和最新消息 (`latestMessage`)。
|
|
9
|
+
- **自动重连**:内置重连逻辑,当连接因非人为原因(如网络抖动)断开时,支持配置重连次数和间隔。
|
|
10
|
+
- **心跳机制**:支持配置自动心跳包发送,保持长连接活跃,防止被服务器超时剔除。
|
|
11
|
+
- **生命周期集成**:在组件卸载 (`onUnmounted`) 时自动关闭连接,避免内存泄漏和异常监听。
|
|
12
|
+
- **手动控制**:既支持初始自动连接,也支持手动调用 `connect()` 或 `disconnect()`。
|
|
13
|
+
|
|
14
|
+
## 参数
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
const { ... } = useWebsocket(url, options);
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
### Options
|
|
21
|
+
|
|
22
|
+
| 参数 | 说明 | 类型 | 默认值 |
|
|
23
|
+
| ----------------- | ----------------- | ----------------------- | -------- |
|
|
24
|
+
| reconnectLimit | 重连次数限制 | `number` | `3` |
|
|
25
|
+
| reconnectInterval | 重连时间间隔 (ms) | `number` | `3000` |
|
|
26
|
+
| heartbeat | 是否开启心跳检查 | `boolean` | `false` |
|
|
27
|
+
| heartbeatInterval | 心跳检查间隔 (ms) | `number` | `30000` |
|
|
28
|
+
| heartbeatMessage | 心跳发送的内容 | `string \| ArrayBuffer` | `'ping'` |
|
|
29
|
+
| manual | 是否手动控制连接 | `boolean` | `false` |
|
|
30
|
+
| protocols | WebSocket 子协议 | `string[]` | - |
|
|
31
|
+
| onOpen | 连接开启的回调 | `(task, res) => void` | - |
|
|
32
|
+
| onClose | 连接关闭的回调 | `(res) => void` | - |
|
|
33
|
+
| onMessage | 收到消息的回调 | `(res) => void` | - |
|
|
34
|
+
| onError | 连接错误的回调 | `(res) => void` | - |
|
|
35
|
+
|
|
36
|
+
## 返回值
|
|
37
|
+
|
|
38
|
+
| 属性 | 说明 | 类型 |
|
|
39
|
+
| ------------- | ---------------------- | --------------------------------------- |
|
|
40
|
+
| latestMessage | 最近收到的一条消息内容 | `Ref<OnSocketMessageCallbackResult>` |
|
|
41
|
+
| readyState | 当前连接状态 | `Ref<ReadyState>` |
|
|
42
|
+
| connect | 手动连接方法 | `() => void` |
|
|
43
|
+
| disconnect | 手动断开方法 | `() => void` |
|
|
44
|
+
| sendMessage | 发送消息方法 | `(data: string \| ArrayBuffer) => void` |
|
|
45
|
+
|
|
46
|
+
## 状态定义 (ReadyState)
|
|
47
|
+
|
|
48
|
+
- `0`: `Connecting` (连接中)
|
|
49
|
+
- `1`: `Open` (已连接)
|
|
50
|
+
- `2`: `Closing` (关闭中)
|
|
51
|
+
- `3`: `Closed` (已关闭)
|
|
52
|
+
|
|
53
|
+
## 使用示例
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
import useWebsocket, { ReadyState } from '../hooks/useWebsocket';
|
|
57
|
+
|
|
58
|
+
const { readyState, latestMessage, sendMessage, connect, disconnect } =
|
|
59
|
+
useWebsocket('wss://echo.websocket.org', {
|
|
60
|
+
reconnectLimit: 5,
|
|
61
|
+
heartbeat: true, // 开启心跳
|
|
62
|
+
heartbeatInterval: 30000,
|
|
63
|
+
heartbeatMessage: JSON.stringify({ type: 'ping' }),
|
|
64
|
+
onOpen: () => {
|
|
65
|
+
console.log('连接已开启');
|
|
66
|
+
},
|
|
67
|
+
onMessage: (res) => {
|
|
68
|
+
console.log('收到消息:', res.data);
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
// 计算属性:判断是否在线
|
|
73
|
+
const isOnline = computed(() => readyState.value === ReadyState.Open);
|
|
74
|
+
|
|
75
|
+
// 发送数据
|
|
76
|
+
const send = () => {
|
|
77
|
+
sendMessage('Hello Antigravity!');
|
|
78
|
+
};
|
|
79
|
+
```
|