console
const { createApp } = PetiteVue
createApp({
result: [],
genCodeImg(idx, code) {
JsBarcode(`#barcode_${idx}`, code, {
displayValue: false
});
},
genNow(el) {
const THIS = this
const dataset = el.dataset
const amount = parseInt(dataset.amount)
const lengths = dataset.lengths.trim().replace(/ /g,'').split(/\,/).map(Number)
const pattern = dataset.pattern
for (let i=0; i<amount; i++) {
this.result.push(THIS.genCodeSafe(lengths, pattern))
}
},
genCodeSafe(groups = [4, 4, 4, 12], sep = "-") {
const letters = "ABCDEFGHJKMNPQRSTUVWXYZ"; //移除 I & O
const digits = "23456789"; //remove 0 & 1
const pool = letters + digits;
const randFrom = s => s[Math.floor(Math.random() * s.length)];
function genSegment(len) {
// 先保证至少有一个字母与一个数字(当 len >= 2 时)
const arr = [];
if (len >= 2) {
arr.push(randFrom(letters));
arr.push(randFrom(digits));
while (arr.length < len) arr.push(randFrom(pool));
// 洗牌
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[arr[i], arr[j]] = [arr[j], arr[i]];
}
return arr.join("");
} else {
// 极短段落就纯随机
return Array.from({ length: len }, () => randFrom(pool)).join("");
}
}
return groups.map(genSegment).join(sep);
},
useCode(str, el) {
this.copyText(str)
el.style.pointerEvents = 'none'
el.style.background = '#999'
el.parentNode.parentNode.style.opacity = .3
},
copyText(str) {
return navigator.clipboard.writeText(str)
.then(() => true)
.catch(err => {
console.error("复制失败:", err);
return false;
});
}
}).mount('#app')
<div id="app">
<div class="bar">
<button data-lengths="4,4,4,12" data-pattern="-" data-amount="25" @click="genNow($el)">立即生成</button>
<button @click="result=[]" style="width:20%;" class="secondary">清空</button>
</div>
<div class="container">
<table id="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">序列码</th>
<th scope="col">条形码</th>
<th scope="col">操作</th>
</tr>
</thead>
<tbody>
<tr v-for="(item,index) in result">
<th scope="row">{{index+1}}</th>
<td>{{item}}</td>
<td class="barcode"><svg v-effect="genCodeImg(index,item)" :id="`barcode_${index}`"></svg></td>
<td><span class="btn" @click="useCode(item, $el)">Copy</span></td>
</tr>
</tbody>
</table>
</div>
</div>
html, body, #app {
height: 100%;
}
.container {
width: 100%;
padding: 0 0 20px;
margin-top: 90px;
max-width: none;
}
#table {
font-family: monospace;
}
#table thead {
background: #fbfbfb;
}
#table th, #table td {
font-size: 14px;
}
#table tr:nth-child(2n) {
background: #fbfbfb;
}
#table tbody tr:hover {
background: #00ccff33;
}
button {
margin: 0 !important;
}
.btn {
background: #0bad00;
color: white;
font-size: 12px;
padding: 4px 6px;
border-radius: 3px;
cursor: pointer;
transition: .15s;
}
.btn:hover {
filter: brightness(1.2);
}
.btn:active {
filter: brightness(0.9);
}
.bar {
position: fixed;
display: flex;
gap: 10px;
padding: 20px;
top: 0;
bottom: 0;
width: 100%;
height: 90px;
background: rgba(255 255 255/90%);
box-shadow: 0 2px 4px rgb(0 0 0/5%);
backdrop-filter: blur(10px);
z-index: 9999;
}
.barcode {
display: flex;
max-width: 300px;
}
.barcode svg {
display: block;
width: 100%;
height: auto;
}