优化了示例

This commit is contained in:
iqudoo
2026-04-30 15:21:15 +08:00
parent 6b27975a45
commit 0d1698439e
3 changed files with 468 additions and 141 deletions

338
demo.html
View File

@@ -1,113 +1,298 @@
<html>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<title>Demo</title>
<!-- 添加vconsole-->
<title>IScan Demo</title>
<script src="https://cdn.jsdelivr.net/npm/vconsole@3.12.0/dist/vconsole.min.js"></script>
<script>
var vconsole = new VConsole();
</script>
<style>
.btn {
width: auto;
height: 40px;
line-height: 40px;
margin-bottom: 5px;
margin-left: 5px;
border: 1px solid rgb(28, 79, 180);
background: rgb(71, 123, 228);
color: #ffffff;
text-align: center;
* {
box-sizing: border-box;
}
.output {
border: 1px solid #eeeeee;
background: #f1f1f1;
padding: 10px;
min-height: 40px;
max-height: 400px;
body {
margin: 0;
color: #17233d;
background: #f5f7fb;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Arial, sans-serif;
line-height: 1.6;
}
.page {
max-width: 960px;
margin: 0 auto;
padding: 20px 16px 40px;
}
.hero {
padding: 28px 20px;
color: #ffffff;
border-radius: 18px;
background: linear-gradient(135deg, #1b63f4, #13c2c2);
box-shadow: 0 16px 36px rgba(27, 99, 244, 0.2);
}
.hero h1 {
margin: 0 0 8px;
font-size: 30px;
line-height: 1.2;
}
.hero p {
margin: 0;
opacity: 0.9;
}
.grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 16px;
margin-top: 16px;
}
.card {
padding: 18px;
border: 1px solid #e8edf7;
border-radius: 16px;
background: #ffffff;
box-shadow: 0 10px 28px rgba(23, 35, 61, 0.06);
}
.card h2 {
margin: 0 0 12px;
font-size: 18px;
}
.card p {
margin: 8px 0;
color: #53627c;
}
.feature-list {
margin: 0;
padding-left: 18px;
color: #53627c;
}
.feature-list li {
margin: 6px 0;
}
.actions {
display: flex;
flex-wrap: wrap;
gap: 12px;
margin-top: 12px;
}
.btn {
min-width: 128px;
height: 44px;
padding: 0 18px;
border: 0;
border-radius: 999px;
color: #ffffff;
background: #1b63f4;
font-size: 15px;
font-weight: 600;
box-shadow: 0 8px 18px rgba(27, 99, 244, 0.24);
}
.btn.secondary {
color: #1b63f4;
background: #eef4ff;
box-shadow: none;
}
.status {
display: inline-flex;
align-items: center;
min-height: 28px;
padding: 4px 12px;
border-radius: 999px;
color: #1b63f4;
background: #eef4ff;
font-weight: 600;
}
pre {
margin: 0;
overflow: auto;
white-space: pre-wrap;
word-wrap: break-word;
word-break: break-word;
font-family: Menlo, Consolas, monospace;
font-size: 13px;
}
.panel {
min-height: 72px;
padding: 12px;
border-radius: 12px;
background: #f7f9fc;
color: #53627c;
}
.result {
border: 1px solid rgb(28, 79, 180);
background: rgb(71, 123, 228);
color: #ffffff;
padding: 10px;
min-height: 40px;
max-height: 1000px;
white-space: pre-wrap;
word-wrap: break-word;
display: none;
color: #0f5132;
background: #d1f5e0;
}
.error {
border: 1px solid #6e2020;
background: #804646;
color: #ffffff;
padding: 10px;
min-height: 40px;
max-height: 1000px;
white-space: pre-wrap;
word-wrap: break-word;
display: none;
color: #842029;
background: #f8d7da;
}
.code {
color: #334155;
background: #0f172a;
}
.code pre {
color: #dbeafe;
}
@media (max-width: 720px) {
.grid {
grid-template-columns: 1fr;
}
.hero h1 {
font-size: 26px;
}
.btn {
flex: 1;
}
}
section {
margin-bottom: 16px;
}
</style>
</head>
<body>
<h1 style="text-align: center;">IScan</h1>
<div style="text-align: center;">
<button onclick="stopScan()" class="btn">stopScan</button>
<button onclick="startScan()" class="btn">startScan</button>
</div>
<pre id="output" class="output"></pre>
<pre id="result" class="result"></pre>
<pre id="error" class="error"></pre>
</br>
<script>
<main class="page">
<section class="hero">
<h1>IScan 扫码 SDK Demo</h1>
<p>统一接入桥接扫码、微信 JSSDK 扫码、Web 摄像头扫码、图片识别和扫码枪输入。</p>
</section>
(function (params) {
<section class="grid">
<div class="card">
<h2>功能说明</h2>
<ul class="feature-list">
<li>桥接环境优先调用 App 原生扫码。</li>
<li>微信环境支持初始化 JSSDK 并调用 scanQRCode。</li>
<li>浏览器环境使用 BarcodeDetector 识别二维码和条形码。</li>
<li>摄像头不可用时可选择图片识别。</li>
<li>支持扫码枪快速输入,并与扫码监听规则统一回调。</li>
</ul>
</div>
<div class="card">
<h2>当前状态</h2>
<p>SDK 状态:<span id="status" class="status">loading</span></p>
<p>运行环境:</p>
<pre id="output" class="panel"></pre>
</div>
</section>
<section class="card">
<h2>操作</h2>
<p>点击开始后会按桥接、微信、Web 摄像头、图片识别的顺序选择可用扫码方式。</p>
<div class="actions">
<button onclick="startScan()" class="btn">开始扫码</button>
<button onclick="stopScan()" class="btn secondary">停止扫码</button>
</div>
</section>
<section class="grid">
<div class="card">
<h2>扫码结果</h2>
<pre id="result" class="panel result"></pre>
</div>
<div class="card">
<h2>错误信息</h2>
<pre id="error" class="panel error"></pre>
</div>
</section>
<section class="card code">
<h2>接入方式</h2>
<pre>IScan.config({
webCanvasEnabled: true,
webScanBeepEnabled: true,
initWechatJssdk: {
apiUrl: "https://your-domain.com/wechat/jssdk-config"
}
}).then(function () {
IScan.setStatusListener(function () {
console.log("status:", IScan.getStatus());
});
IScan.onScanListener(function (res) {
console.log("scan result:", res);
}, "scan", null, 100);
});
IScan.startScan();
IScan.stopScan();</pre>
</section>
</main>
<script>
(function () {
output(window.navigator.userAgent);
window.onerror = function (params) {
error(params);
window.onerror = function (message, source, lineno, colno, err) {
error(err || message);
}
})();
function setStatus(status) {
document.getElementById("status").innerHTML = status || "unknown";
}
function hide() {
document.getElementById("result").style.display = "none"
document.getElementById("result").style.display = "none";
document.getElementById("result").innerHTML = "";
document.getElementById("error").style.display = "none"
document.getElementById("error").style.display = "none";
document.getElementById("error").innerHTML = "";
}
function format(data) {
if (typeof data === "string") {
return data;
}
try {
return JSON.stringify(data, null, 2);
} catch (e) {
return String(data);
}
}
function error(data) {
document.getElementById("result").style.display = "none"
document.getElementById("result").style.display = "none";
document.getElementById("result").innerHTML = "";
document.getElementById("error").style.display = "block"
document.getElementById("error").innerHTML = JSON.stringify(data);
// console.log("error:", data);
document.getElementById("error").style.display = "block";
document.getElementById("error").innerHTML = format(data);
}
function result(data) {
document.getElementById("error").style.display = "none"
document.getElementById("error").style.display = "none";
document.getElementById("error").innerHTML = "";
document.getElementById("result").style.display = "block"
document.getElementById("result").innerHTML = JSON.stringify(data);
// console.log("result:", data);
document.getElementById("result").style.display = "block";
document.getElementById("result").innerHTML = format(data);
}
function output(key, data) {
document.getElementById("output").innerHTML = key;
if (data) {
document.getElementById("output").innerHTML += "\n\n";
document.getElementById("output").innerHTML += JSON.stringify(data);
document.getElementById("output").innerHTML += "\n\n";
// console.log("output:", data);
}
function output(data) {
document.getElementById("output").innerHTML = format(data);
}
function initSDK(options, callback) {
@@ -120,44 +305,43 @@
} else {
window.addEventListener("IScanReady", function () {
initSDK(options, callback);
})
}, { once: true })
}
}
function ready() {
output("call ready");
hide();
setStatus("initializing");
var url = "https://vet.iqudoo.com/api?action=api.biz.wechat.JSSDKConfig";
initSDK({
initSDK({
webCanvasEnabled: true,
webScanBeepEnabled: true,
initWechatJssdk: { apiUrl: url }
initWechatJssdk: { apiUrl: url }
}, function () {
setStatus(IScan.getStatus());
result("初始化成功");
IScan.setStatusListener(function (res) {
output("status", IScan.getStatus());
IScan.setStatusListener(function () {
setStatus(IScan.getStatus());
});
IScan.onScanListener(function (res) {
result(res);
}, "scan", null, 100);
output(window.navigator.userAgent);
});
}
function stopScan() {
output("call stopScan");
hide();
IScan.stopScan();
setStatus(IScan.getStatus());
}
function startScan() {
output("call startScan");
hide();
IScan.startScan();
setStatus(IScan.getStatus());
}
ready();
</script>
</body>