unify-external-plugin-platform 0.0.3-87 → 0.0.3-88

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.
@@ -0,0 +1,35 @@
1
+ [ 6040655ms] [WARNING] [vue-router] Duplicate named routes definition: { name: "id-card-reader-page", path: "/id-card-reader" } @ webpack-internal:///./node_modules/vue-router/dist/vue-router.esm.js:25
2
+ [ 6040655ms] [WARNING] [vue-router] Duplicate named routes definition: { name: "scan-page", path: "/scan" } @ webpack-internal:///./node_modules/vue-router/dist/vue-router.esm.js:25
3
+ [ 6040655ms] [WARNING] [vue-router] Duplicate named routes definition: { name: "scanner-page", path: "/scanner" } @ webpack-internal:///./node_modules/vue-router/dist/vue-router.esm.js:25
4
+ [ 6040655ms] [WARNING] [vue-router] Duplicate named routes definition: { name: "sign-page", path: "/sign" } @ webpack-internal:///./node_modules/vue-router/dist/vue-router.esm.js:25
5
+ [ 6040684ms] [INFO] Download the Vue Devtools extension for a better development experience:
6
+ https://github.com/vuejs/vue-devtools @ webpack-internal:///./node_modules/vue/dist/vue.runtime.esm.js:8818
7
+ [ 6045041ms] [ERROR] WebSocket connection to 'ws://127.0.0.1:1818/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED @ webpack-internal:///./packages/service/external-plugins/jie-yu-plugin.service.ts:73
8
+ [ 6045042ms] Error: 统一插件平台捷宇设备功能加载失败
9
+ at JieYueService.handleError (webpack-internal:///./packages/service/external-plugins/jie-yu-plugin.service.ts:99:13)
10
+ at ws.onerror (webpack-internal:///./packages/service/external-plugins/jie-yu-plugin.service.ts:86:28)
11
+ [ 6064767ms] [ERROR] WebSocket connection to 'ws://127.0.0.1:8341/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED @ webpack-internal:///./packages/service/external-plugins/uds-plugin-socket.service.ts:30
12
+ [ 6064768ms] Error: 统一插件平台紫图速拍仪设备功能加载失败
13
+ at UdsPluginSocketService.handleError (webpack-internal:///./packages/service/external-plugins/uds-plugin-socket.service.ts:55:11)
14
+ at ws.onerror (webpack-internal:///./packages/service/external-plugins/uds-plugin-socket.service.ts:43:29)
15
+ [ 6068294ms] NavigationDuplicated: Avoided redundant navigation to current location: "/home/scan".
16
+ at createRouterError (webpack-internal:///./node_modules/vue-router/dist/vue-router.esm.js:2056:15)
17
+ at createNavigationDuplicatedError (webpack-internal:///./node_modules/vue-router/dist/vue-router.esm.js:2026:15)
18
+ at HashHistory.confirmTransition (webpack-internal:///./node_modules/vue-router/dist/vue-router.esm.js:2343:18)
19
+ at HashHistory.transitionTo (webpack-internal:///./node_modules/vue-router/dist/vue-router.esm.js:2270:8)
20
+ at HashHistory.push (webpack-internal:///./node_modules/vue-router/dist/vue-router.esm.js:2725:10)
21
+ at eval (webpack-internal:///./node_modules/vue-router/dist/vue-router.esm.js:3046:24)
22
+ at new Promise (<anonymous>)
23
+ at VueRouter.push (webpack-internal:///./node_modules/vue-router/dist/vue-router.esm.js:3045:12)
24
+ at VueComponent.handleSelectMenu (webpack-internal:///./node_modules/babel-loader/lib/index.js!./node_modules/ts-loader/index.js??clonedRuleSet-41.use[1]!./node_modules/@vue/cli-service/node_modules/@vue/vue-loader-v15/lib/index.js??vue-loader-options!./src/views/layout/home-layout.vue?vue&type=script&lang=ts&:29:18)
25
+ at invokeWithErrorHandling (webpack-internal:///./node_modules/vue/dist/vue.runtime.esm.js:3058:30)
26
+ [12760387ms] [ERROR] WebSocket connection to 'ws://localhost:8080/ws' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED @ webpack-internal:///./node_modules/webpack-dev-server/client/clients/WebSocketClient.js:20
27
+ [12762406ms] [ERROR] WebSocket connection to 'ws://localhost:8080/ws' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED @ webpack-internal:///./node_modules/webpack-dev-server/client/clients/WebSocketClient.js:20
28
+ [12766460ms] [ERROR] WebSocket connection to 'ws://localhost:8080/ws' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED @ webpack-internal:///./node_modules/webpack-dev-server/client/clients/WebSocketClient.js:20
29
+ [12774556ms] [ERROR] WebSocket connection to 'ws://localhost:8080/ws' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED @ webpack-internal:///./node_modules/webpack-dev-server/client/clients/WebSocketClient.js:20
30
+ [12790625ms] [ERROR] WebSocket connection to 'ws://localhost:8080/ws' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED @ webpack-internal:///./node_modules/webpack-dev-server/client/clients/WebSocketClient.js:20
31
+ [12822666ms] [ERROR] WebSocket connection to 'ws://localhost:8080/ws' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED @ webpack-internal:///./node_modules/webpack-dev-server/client/clients/WebSocketClient.js:20
32
+ [12886724ms] [ERROR] WebSocket connection to 'ws://localhost:8080/ws' failed: Error during WebSocket handshake: Unexpected response code: 200 @ webpack-internal:///./node_modules/webpack-dev-server/client/clients/WebSocketClient.js:20
33
+ [13014784ms] [ERROR] WebSocket connection to 'ws://localhost:8080/ws' failed: Error during WebSocket handshake: Unexpected response code: 200 @ webpack-internal:///./node_modules/webpack-dev-server/client/clients/WebSocketClient.js:20
34
+ [13270864ms] [ERROR] WebSocket connection to 'ws://localhost:8080/ws' failed: Error during WebSocket handshake: Unexpected response code: 200 @ webpack-internal:///./node_modules/webpack-dev-server/client/clients/WebSocketClient.js:20
35
+ [13782964ms] [ERROR] WebSocket connection to 'ws://localhost:8080/ws' failed: Error during WebSocket handshake: Unexpected response code: 200 @ webpack-internal:///./node_modules/webpack-dev-server/client/clients/WebSocketClient.js:20
@@ -0,0 +1,3 @@
1
+ [ 139ms] [LOG] 页面初始化,localStorage 记住的设备: (无) @ http://localhost:8399/camera-device-test.html:143
2
+ [ 152ms] [ERROR] Failed to load resource: the server responded with a status of 404 (File not found) @ http://localhost:8399/favicon.ico:1
3
+ [ 2126ms] [LOG] 枚举到视频设备 1 个(label 为空,先开流换授权) @ http://localhost:8399/camera-device-test.html:143
@@ -0,0 +1,8 @@
1
+ [ 226ms] [LOG] 页面初始化,localStorage 记住的设备: (无) @ http://localhost:8399/camera-device-test.html:143
2
+ [ 278ms] [ERROR] Failed to load resource: the server responded with a status of 404 (File not found) @ http://localhost:8399/favicon.ico:1
3
+ [ 1727ms] [LOG] 枚举到视频设备 1 个(label 为空,先开流换授权) @ http://localhost:8399/camera-device-test.html:143
4
+ [ 5525ms] [LOG] 页面初始化,localStorage 记住的设备: (无) @ http://localhost:8399/camera-device-test.html:143
5
+ [ 5543ms] [LOG] 枚举到视频设备 2 个 @ http://localhost:8399/camera-device-test.html:143
6
+ [ 5550ms] [LOG] 默认选择: <Lenovo Virtual Camera>,原因: 没有匹配项,取第一个 @ http://localhost:8399/camera-device-test.html:143
7
+ [ 5551ms] [LOG] getUserMedia: 指定设备 fa9510fde0…(64) @ http://localhost:8399/camera-device-test.html:143
8
+ [ 5852ms] [LOG] 摄像头打开成功(没有匹配项,取第一个) @ http://localhost:8399/camera-device-test.html:143
@@ -0,0 +1,10 @@
1
+ [ 79ms] [LOG] getUserMedia({video: true}) 试开中… @ http://localhost:8399/camera-device-test.html:157
2
+ [ 324ms] [LOG] 试开成功,已释放测试流 @ http://localhost:8399/camera-device-test.html:157
3
+ [ 334ms] [LOG] 枚举到视频设备 2 个 @ http://localhost:8399/camera-device-test.html:157
4
+ [ 336ms] [LOG] 默认选择: <Lenovo Virtual Camera>,原因: 没有匹配项,取第一个 @ http://localhost:8399/camera-device-test.html:157
5
+ [ 336ms] [LOG] getUserMedia: 指定设备 fa9510fde0…(64) @ http://localhost:8399/camera-device-test.html:157
6
+ [ 1446ms] [LOG] 摄像头打开成功(没有匹配项,取第一个) @ http://localhost:8399/camera-device-test.html:157
7
+ [ 1747ms] [LOG] 检测到设备变化,重新枚举… @ http://localhost:8399/camera-device-test.html:157
8
+ [ 1748ms] [LOG] 枚举到视频设备 2 个 @ http://localhost:8399/camera-device-test.html:157
9
+ [ 3306ms] [LOG] 检测到设备变化,重新枚举… @ http://localhost:8399/camera-device-test.html:157
10
+ [ 3312ms] [LOG] 枚举到视频设备 2 个 @ http://localhost:8399/camera-device-test.html:157
@@ -0,0 +1,14 @@
1
+ - generic [active] [ref=e1]:
2
+ - heading "摄像头设备选择测试" [level=2] [ref=e2]
3
+ - generic [ref=e3]:
4
+ - text: 默认设备优先级:上次手动选择的 > label 以 USB 开头的 > 第一个。 与项目共用同一个 localStorage key(
5
+ - code [ref=e4]: unify-default-camera-device-id
6
+ - text: ),在这里手动切换后,弹窗/组件里也会记住。
7
+ - generic [ref=e8]:
8
+ - generic [ref=e9]:
9
+ - combobox [ref=e10]:
10
+ - option "— 请选择摄像头 —" [selected]
11
+ - button "刷新设备列表" [ref=e11] [cursor=pointer]
12
+ - button "拍照" [disabled] [ref=e12]
13
+ - generic [ref=e13]: 正在初始化…
14
+ - generic [ref=e14]: "17:05:49 页面初始化,localStorage 记住的设备: (无)"
@@ -0,0 +1,16 @@
1
+ - generic [active] [ref=e1]:
2
+ - heading "摄像头设备选择测试" [level=2] [ref=e2]
3
+ - generic [ref=e3]:
4
+ - text: 默认设备优先级:上次手动选择的 > label 以 USB 开头的 > 第一个。 与项目共用同一个 localStorage key(
5
+ - code [ref=e4]: unify-default-camera-device-id
6
+ - text: ),在这里手动切换后,弹窗/组件里也会记住。
7
+ - generic [ref=e8]:
8
+ - generic [ref=e9]:
9
+ - combobox [ref=e10]:
10
+ - option "— 请选择摄像头 —" [selected]
11
+ - button "刷新设备列表" [ref=e11] [cursor=pointer]
12
+ - button "拍照" [disabled] [ref=e12]
13
+ - generic [ref=e13]: 正在初始化…
14
+ - generic [ref=e14]:
15
+ - generic [ref=e15]: "17:05:49 页面初始化,localStorage 记住的设备: (无)"
16
+ - generic [ref=e16]: 17:05:51 枚举到视频设备 1 个(label 为空,先开流换授权)
@@ -0,0 +1,14 @@
1
+ - generic [active] [ref=e1]:
2
+ - heading "摄像头设备选择测试" [level=2] [ref=e2]
3
+ - generic [ref=e3]:
4
+ - text: 默认设备优先级:上次手动选择的 > label 以 USB 开头的 > 第一个。 与项目共用同一个 localStorage key(
5
+ - code [ref=e4]: unify-default-camera-device-id
6
+ - text: ),在这里手动切换后,弹窗/组件里也会记住。
7
+ - generic [ref=e8]:
8
+ - generic [ref=e9]:
9
+ - combobox [ref=e10]:
10
+ - option "— 请选择摄像头 —" [selected]
11
+ - button "刷新设备列表" [ref=e11] [cursor=pointer]
12
+ - button "拍照" [disabled] [ref=e12]
13
+ - generic [ref=e13]: 正在初始化…
14
+ - generic [ref=e14]: "17:11:09 页面初始化,localStorage 记住的设备: (无)"
@@ -0,0 +1,13 @@
1
+ - generic [active] [ref=f2e1]:
2
+ - heading "摄像头设备选择测试(诊断版)" [level=2] [ref=f2e2]
3
+ - generic [ref=f2e3]: 页面打开后会自动逐层检测:打开方式 → 安全上下文 → API → 权限 → 设备枚举 → 开流,哪一层断了会直接给出原因和解决办法。
4
+ - generic [ref=f2e4]: 正在检测…
5
+ - generic [ref=f2e8]:
6
+ - generic [ref=f2e9]:
7
+ - combobox [ref=f2e10]:
8
+ - option "— 请选择摄像头 —" [selected]
9
+ - button "刷新设备列表" [ref=f2e11] [cursor=pointer]
10
+ - button "重新检测" [ref=f2e12] [cursor=pointer]
11
+ - button "拍照" [disabled] [ref=f2e13]
12
+ - generic [ref=f2e14]: 等待检测…
13
+ - generic [ref=f2e15]: "17:13:24 getUserMedia({video: true}) 试开中…"
@@ -0,0 +1,362 @@
1
+ <!DOCTYPE html>
2
+ <html lang="zh-CN">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <title>摄像头设备选择测试(USB 开头优先)</title>
6
+ <style>
7
+ * { box-sizing: border-box; }
8
+ body {
9
+ font-family: "Microsoft YaHei", sans-serif;
10
+ margin: 0;
11
+ padding: 20px;
12
+ background: #f5f7fa;
13
+ color: #333;
14
+ }
15
+ h2 { margin: 0 0 6px; }
16
+ .tip { color: #888; font-size: 13px; margin-bottom: 14px; }
17
+ .panel { display: flex; gap: 16px; align-items: flex-start; }
18
+ video {
19
+ width: 480px;
20
+ height: 360px;
21
+ background: #000;
22
+ border-radius: 6px;
23
+ object-fit: cover;
24
+ }
25
+ .side { flex: 1; min-width: 340px; }
26
+ .row { margin-bottom: 12px; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
27
+ select {
28
+ max-width: 320px;
29
+ padding: 6px 8px;
30
+ border: 1px solid #ccc;
31
+ border-radius: 4px;
32
+ }
33
+ button {
34
+ padding: 6px 14px;
35
+ border: 1px solid #409eff;
36
+ background: #409eff;
37
+ color: #fff;
38
+ border-radius: 4px;
39
+ cursor: pointer;
40
+ }
41
+ button.secondary { background: #fff; color: #409eff; }
42
+ button:disabled { opacity: .5; cursor: not-allowed; }
43
+ .info { font-size: 13px; line-height: 1.8; background: #fff; border: 1px solid #e4e7ed; border-radius: 6px; padding: 10px 12px; }
44
+ .diag { font-size: 13px; line-height: 1.9; background: #fffbe6; border: 1px solid #ffe58f; border-radius: 6px; padding: 10px 12px; margin-bottom: 12px; }
45
+ .diag b.ok { color: #52c41a; }
46
+ .diag b.err { color: #f5222d; }
47
+ .fix {
48
+ background: #fff2e8;
49
+ border: 1px solid #ffbb96;
50
+ border-radius: 4px;
51
+ padding: 8px 10px;
52
+ margin-top: 6px;
53
+ color: #d4380d;
54
+ font-size: 13px;
55
+ }
56
+ .log {
57
+ margin-top: 12px;
58
+ font-size: 12px;
59
+ line-height: 1.7;
60
+ background: #1e1e1e;
61
+ color: #d4d4d4;
62
+ border-radius: 6px;
63
+ padding: 10px 12px;
64
+ max-height: 220px;
65
+ overflow-y: auto;
66
+ white-space: pre-wrap;
67
+ word-break: break-all;
68
+ }
69
+ #photoImg {
70
+ margin-top: 16px;
71
+ max-width: 480px;
72
+ border: 1px solid #ccc;
73
+ border-radius: 6px;
74
+ display: none;
75
+ }
76
+ .ok { color: #67c23a; }
77
+ .err { color: #f56c6c; }
78
+ </style>
79
+ </head>
80
+ <body>
81
+
82
+ <h2>摄像头设备选择测试(诊断版)</h2>
83
+ <div class="tip">
84
+ 页面打开后会自动逐层检测:打开方式 → 安全上下文 → API → 权限 → 设备枚举 → 开流,哪一层断了会直接给出原因和解决办法。
85
+ </div>
86
+
87
+ <div class="diag" id="diag">正在检测…</div>
88
+
89
+ <div class="panel">
90
+ <div>
91
+ <video id="monitor" autoplay muted playsinline></video>
92
+ <div><img id="photoImg" alt="拍照结果"/></div>
93
+ </div>
94
+ <div class="side">
95
+ <div class="row">
96
+ <select id="deviceSelect"><option value="">— 请选择摄像头 —</option></select>
97
+ <button class="secondary" id="refreshBtn">刷新设备列表</button>
98
+ <button class="secondary" id="retryBtn">重新检测</button>
99
+ <button id="takePhotoBtn" disabled>拍照</button>
100
+ </div>
101
+ <div class="info" id="deviceInfo">等待检测…</div>
102
+ <div class="log" id="log"></div>
103
+ </div>
104
+ </div>
105
+
106
+ <canvas id="photoCanvas" style="display:none"></canvas>
107
+
108
+ <script>
109
+ // ======== 与 packages/utils/camera-device-utils.ts 保持一致的逻辑 ========
110
+ const CAMERA_DEVICE_STORAGE_KEY = 'unify-default-camera-device-id';
111
+
112
+ // 获取视频输入设备列表(未授权时 label 为空,先短暂开流换取授权)
113
+ async function listVideoInputDevices() {
114
+ let devices = await navigator.mediaDevices.enumerateDevices();
115
+ let videoInputs = devices.filter(device => device.kind === 'videoinput');
116
+ log('枚举到视频设备 ' + videoInputs.length + ' 个' + (videoInputs.some(d => !d.label) ? '(label 为空,先开流换授权)' : ''));
117
+ if (videoInputs.length && videoInputs.some(device => !device.label)) {
118
+ const tempStream = await navigator.mediaDevices.getUserMedia({video: true});
119
+ tempStream.getTracks().forEach(track => track.stop());
120
+ videoInputs = (await navigator.mediaDevices.enumerateDevices())
121
+ .filter(device => device.kind === 'videoinput');
122
+ log('授权后重新枚举完成');
123
+ }
124
+ return videoInputs;
125
+ }
126
+
127
+ // 默认设备:上次手动选择的 > USB 开头的 > 第一个
128
+ function pickDefaultCameraDevice(devices) {
129
+ if (!devices || devices.length === 0) {
130
+ return { device: null, reason: '' };
131
+ }
132
+ const selectedId = localStorage.getItem(CAMERA_DEVICE_STORAGE_KEY);
133
+ const cached = devices.find(device => device.deviceId === selectedId);
134
+ if (cached) {
135
+ return { device: cached, reason: '上次手动选择的设备' };
136
+ }
137
+ const usbDevice = devices.find(device => device.label && device.label.toUpperCase().startsWith('USB'));
138
+ if (usbDevice) {
139
+ return { device: usbDevice, reason: 'label 以 USB 开头' };
140
+ }
141
+ return { device: devices[0], reason: '没有匹配项,取第一个' };
142
+ }
143
+
144
+ function saveSelectedCameraDeviceId(deviceId) {
145
+ localStorage.setItem(CAMERA_DEVICE_STORAGE_KEY, deviceId);
146
+ }
147
+
148
+ // ======== 测试页面逻辑 ========
149
+ const $ = id => document.getElementById(id);
150
+ const log = (msg, cls) => {
151
+ const el = $('log');
152
+ const line = document.createElement('div');
153
+ if (cls) line.className = cls;
154
+ line.textContent = new Date().toLocaleTimeString() + ' ' + msg;
155
+ el.appendChild(line);
156
+ el.scrollTop = el.scrollHeight;
157
+ console.log(msg);
158
+ };
159
+ const shortId = id => id ? (id.slice(0, 10) + '…(' + id.length + ')') : '(无)';
160
+ const esc = s => String(s).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
161
+
162
+ let currentStream = null;
163
+ let currentDeviceId = '';
164
+
165
+ function diagLine(label, ok, value) {
166
+ return label + ': <b class="' + (ok ? 'ok' : 'err') + '">' + value + '</b><br>';
167
+ }
168
+
169
+ // getUserMedia 错误名 → 原因和解决办法
170
+ const ERROR_FIX_MAP = {
171
+ NotAllowedError: '摄像头<b>权限被拒绝</b>。<br>① 点浏览器地址栏左侧的图标,把摄像头权限改为"允许",然后点"重新检测";<br>② 如果之前点过"拒绝/屏蔽":Chrome → 设置 → 隐私和安全 → 网站设置 → 摄像头,找到本网站删除阻止记录;<br>③ Windows 设置 → 隐私和安全性 → 相机,确认"相机访问"和"让应用访问相机"都已开启。',
172
+ NotFoundError: '浏览器<b>没发现任何摄像头设备</b>。<br>① 确认高拍仪/摄像头已插好、电源已开;<br>② 设备管理器里看"照相机"下设备是否有感叹号;<br>③ Windows 设置 → 隐私和安全性 → 相机,确认"相机访问"总开关已开启。',
173
+ NotReadableError: '摄像头<b>被其他程序占用</b>。<br>① 高拍仪厂商的本地插件服务可能正占用摄像头,先关掉;<br>② 关掉微信/钉钉/腾讯会议等正在用摄像头的程序;<br>③ 关掉后点"重新检测"。',
174
+ OverconstrainedError: '摄像头<b>不支持当前参数</b>:' + '请点"重新检测"重试,并告知开发。',
175
+ SecurityError: '当前页面<b>不是安全上下文</b>,浏览器禁用了摄像头。<br>请用 Chrome/Edge 打开本文件(file:// 双击即可),或通过 http://localhost / https 访问;不要用 IP 地址的 http 链接。',
176
+ AbortError: '摄像头打开被中断,请点"重新检测"重试。'
177
+ };
178
+
179
+ async function runDiagnostics() {
180
+ $('diag').innerHTML = '正在检测…';
181
+ $('takePhotoBtn').disabled = true;
182
+ const lines = [];
183
+ const add = (label, ok, value) => lines.push(diagLine(label, ok, value));
184
+ let fatal = '';
185
+
186
+ // 第 1 层:打开方式 / 安全上下文
187
+ const secure = window.isSecureContext;
188
+ add('打开方式', true, esc(location.protocol === 'file:' ? '本地文件 file://(可用)' : location.href));
189
+ add('安全上下文', secure, secure ? '是' : '否——浏览器会禁用摄像头!');
190
+
191
+ // 第 2 层:API 是否存在
192
+ const hasMedia = !!(navigator.mediaDevices && navigator.mediaDevices.getUserMedia && navigator.mediaDevices.enumerateDevices);
193
+ add('mediaDevices API', hasMedia, hasMedia ? '存在' : '不存在(非安全上下文或浏览器内核太旧)');
194
+
195
+ if (!secure || !hasMedia) {
196
+ lines.push('<div class="fix">' + ERROR_FIX_MAP.SecurityError + '</div>');
197
+ $('diag').innerHTML = lines.join('');
198
+ log('诊断:环境不支持摄像头调用(secure=' + secure + ', api=' + hasMedia + ')', 'err');
199
+ return;
200
+ }
201
+
202
+ // 第 3 层:权限状态
203
+ let permState = '浏览器不支持权限查询(忽略)';
204
+ try {
205
+ const st = await navigator.permissions.query({name: 'camera'});
206
+ permState = {granted: '已授权', denied: '已拒绝', prompt: '待用户授权'}[st.state] || st.state;
207
+ add('摄像头权限', st.state !== 'denied', permState);
208
+ if (st.state === 'denied') {
209
+ lines.push('<div class="fix">' + ERROR_FIX_MAP.NotAllowedError + '</div>');
210
+ }
211
+ } catch (e) {
212
+ add('摄像头权限', true, permState);
213
+ }
214
+
215
+ // 第 4 层:原始枚举(授权前后 label 不同的现象本身就是证据)
216
+ let devices = [];
217
+ try {
218
+ devices = await navigator.mediaDevices.enumerateDevices();
219
+ } catch (e) {
220
+ add('设备枚举', false, '失败: ' + e.name);
221
+ }
222
+ const videos = devices.filter(d => d.kind === 'videoinput');
223
+ const noLabel = videos.length > 0 && videos.some(d => !d.label);
224
+ add('设备枚举', devices.length > 0, '共 ' + devices.length + ' 个设备,其中视频输入 ' + videos.length + ' 个' + (noLabel ? '(label 为空 = 尚未授权)' : ''));
225
+
226
+ if (videos.length === 0) {
227
+ log('诊断:视频输入设备 0 个', 'err');
228
+ }
229
+
230
+ // 第 5 层:直接试开(拿到真实报错名)
231
+ let stream = null;
232
+ try {
233
+ log('getUserMedia({video: true}) 试开中…');
234
+ stream = await navigator.mediaDevices.getUserMedia({video: true});
235
+ add('摄像头打开', true, '成功(设备: ' + esc(stream.getVideoTracks()[0].label || '(未知)') + ')');
236
+ stream.getTracks().forEach(t => t.stop());
237
+ log('试开成功,已释放测试流');
238
+ } catch (e) {
239
+ add('摄像头打开', false, e.name + ': ' + esc(e.message || ''));
240
+ lines.push('<div class="fix">' + (ERROR_FIX_MAP[e.name] || ('未知错误: ' + esc(e.name) + ' ' + esc(e.message || ''))) + '</div>');
241
+ log('试开失败: ' + e.name + ' ' + (e.message || ''), 'err');
242
+ }
243
+
244
+ $('diag').innerHTML = lines.join('');
245
+
246
+ // 诊断通过 → 走正式的设备选择流程
247
+ if (stream) {
248
+ await startDeviceFlow();
249
+ }
250
+ }
251
+
252
+ async function startDeviceFlow() {
253
+ try {
254
+ const devices = await listVideoInputDevices();
255
+ await fillDeviceSelect(devices);
256
+ const {device, reason} = pickDefaultCameraDevice(devices);
257
+ if (device) {
258
+ log('默认选择: <' + (device.label || '(未知设备)') + '>,原因: ' + reason);
259
+ } else {
260
+ log('没有可用摄像头设备', 'err');
261
+ }
262
+ await openCamera(device ? device.deviceId : undefined, reason);
263
+ } catch (e) {
264
+ log('初始化失败: ' + (e && (e.name + ': ' + e.message) || e), 'err');
265
+ $('deviceInfo').innerHTML = '<span class="err">初始化失败: ' + esc(e && e.name || '') + '</span>';
266
+ }
267
+ // 热插拔监听:插拔设备后自动刷新列表,选中项仍存在则保持
268
+ navigator.mediaDevices.ondevicechange = async () => {
269
+ log('检测到设备变化,重新枚举…');
270
+ const devices = await listVideoInputDevices();
271
+ await fillDeviceSelect(devices);
272
+ if (currentDeviceId && !devices.find(d => d.deviceId === currentDeviceId)) {
273
+ log('当前设备已拔出,回退到默认设备', 'err');
274
+ const {device, reason} = pickDefaultCameraDevice(devices);
275
+ await openCamera(device ? device.deviceId : undefined, reason);
276
+ }
277
+ };
278
+ }
279
+
280
+ async function openCamera(deviceId, reason) {
281
+ if (currentStream) {
282
+ currentStream.getVideoTracks().forEach(t => t.stop());
283
+ log('已停止之前的视频流');
284
+ }
285
+ const videoObj = {
286
+ audio: false,
287
+ video: deviceId ? {deviceId: {exact: deviceId}, width: 1920, height: 1080} : {width: 1920, height: 1080}
288
+ };
289
+ log('getUserMedia: ' + (deviceId ? '指定设备 ' + shortId(deviceId) : '浏览器默认设备'));
290
+ const stream = await navigator.mediaDevices.getUserMedia(videoObj);
291
+ $('monitor').srcObject = stream;
292
+ await $('monitor').play();
293
+ currentStream = stream;
294
+ // 以实际打开的设备为准,同步下拉框选中值
295
+ currentDeviceId = stream.getVideoTracks()[0].getSettings().deviceId || deviceId || '';
296
+ $('deviceSelect').value = currentDeviceId;
297
+ $('takePhotoBtn').disabled = false;
298
+ log('摄像头打开成功' + (reason ? '(' + reason + ')' : ''), 'ok');
299
+ }
300
+
301
+ async function fillDeviceSelect(devices) {
302
+ const select = $('deviceSelect');
303
+ select.innerHTML = '';
304
+ devices.forEach(device => {
305
+ const option = document.createElement('option');
306
+ option.value = device.deviceId;
307
+ option.textContent = device.label || '(未知设备)';
308
+ select.appendChild(option);
309
+ });
310
+ $('deviceInfo').innerHTML = devices.length
311
+ ? '共 ' + devices.length + ' 个视频设备:<br>' + devices.map((d, i) =>
312
+ (i + 1) + '. <b>' + esc(d.label || '(未知设备)') + '</b>' +
313
+ (d.label && d.label.toUpperCase().startsWith('USB') ? ' <span class="ok">[USB 开头]</span>' : '') +
314
+ '<br>&nbsp;&nbsp;&nbsp;deviceId: ' + shortId(d.deviceId)
315
+ ).join('<br>')
316
+ : '<span class="err">未枚举到任何视频设备(参见上方黄色诊断区的错误提示)</span>';
317
+ }
318
+
319
+ $('deviceSelect').addEventListener('change', async () => {
320
+ const deviceId = $('deviceSelect').value;
321
+ try {
322
+ await openCamera(deviceId, '用户手动切换');
323
+ saveSelectedCameraDeviceId(deviceId);
324
+ log('已记住选择: ' + ($('deviceSelect').selectedOptions[0] || {}).textContent, 'ok');
325
+ } catch (e) {
326
+ log('切换失败: ' + (e && (e.name + ': ' + e.message) || e), 'err');
327
+ }
328
+ });
329
+
330
+ $('refreshBtn').addEventListener('click', async () => {
331
+ const devices = await listVideoInputDevices();
332
+ await fillDeviceSelect(devices);
333
+ if (currentDeviceId) $('deviceSelect').value = currentDeviceId;
334
+ log('设备列表已刷新');
335
+ });
336
+
337
+ $('retryBtn').addEventListener('click', () => {
338
+ if (currentStream) {
339
+ currentStream.getVideoTracks().forEach(t => t.stop());
340
+ currentStream = null;
341
+ }
342
+ runDiagnostics();
343
+ });
344
+
345
+ $('takePhotoBtn').addEventListener('click', () => {
346
+ if (!currentStream) return;
347
+ const video = $('monitor');
348
+ const canvas = $('photoCanvas');
349
+ canvas.width = video.videoWidth;
350
+ canvas.height = video.videoHeight;
351
+ canvas.getContext('2d').drawImage(video, 0, 0, canvas.width, canvas.height);
352
+ const img = $('photoImg');
353
+ img.src = canvas.toDataURL('image/png');
354
+ img.style.display = 'block';
355
+ log('拍照完成,分辨率 ' + canvas.width + '×' + canvas.height, 'ok');
356
+ });
357
+
358
+ // 启动:先诊断,通过后再进入设备选择流程
359
+ runDiagnostics();
360
+ </script>
361
+ </body>
362
+ </html>