init: PLC data collection system

This commit is contained in:
zlm committed 2026-06-09 14:32:13 +08:00
commit ae90bc9368
38 files changed
+5234

No files matched your search

+193
View File
@@ -0,0 +1,193 @@
import './styles.css'
import { api, type PLC, type MP, type Station } from './api/client.js'
/* ===== Navigation ===== */
const $ = (s: string) => document.querySelector(s)!
const $$ = (s: string) => document.querySelectorAll(s)
$$('.tab-btn').forEach(btn => btn.addEventListener('click', () => {
$$('.tab-btn').forEach(b => b.classList.remove('active'))
$$('.page').forEach(p => p.classList.remove('active'))
btn.classList.add('active')
document.getElementById(`page-${btn.dataset.page}`)!.classList.add('active')
}))
/* ===== Query Page ===== */
const qInput = $('#q-input') as HTMLInputElement
const qBtn = $('#q-btn') as HTMLButtonElement
const qResult = $('#q-result') as HTMLDivElement
async function doQuery() {
const pid = qInput.value.trim()
if (!pid) return
qResult.innerHTML = '<div class="text-gray-400 text-center py-8">查询中...</div>'
try {
const r = await api.queryProduct(pid)
qResult.innerHTML = ''
if (!r.data.length) {
qResult.innerHTML = `<div class="text-gray-400 text-center py-8">${r.message || '暂无数据'}</div>`
return
}
for (const d of r.data) {
const ok = d.isQualified === 1, rw = d.inspectionCount > 1
qResult.innerHTML += `
<div class="bg-white rounded-xl shadow-sm border border-gray-100 p-5 mb-3">
<div class="flex items-center gap-2 mb-3 pb-2 border-b border-gray-100">
<span class="w-2 h-2 rounded-full ${ok ? 'bg-green-500' : 'bg-red-500'}"></span>
<span class="font-semibold text-gray-800">${d.pointName}</span>
<span class="text-xs text-gray-400">${d.fieldUnit || ''}</span>
<span class="ml-auto text-xs text-gray-400">${d.recordTime}</span>
</div>
<div class="grid grid-cols-3 gap-4 text-sm">
<div><span class="text-gray-400">检测次数</span><br><span class="font-medium ${rw ? 'text-orange-600' : ''}">${d.inspectionCount}${rw ? '(返修)' : ''}</span></div>
<div><span class="text-gray-400">合格判定</span><br><span class="font-medium ${ok ? 'text-green-600' : 'text-red-600'}">${ok ? '合格' : '不合格'}</span></div>
<div><span class="text-gray-400">实测值</span><br><span class="font-medium">${d.measuredValue ?? '-'}</span></div>
</div>
<div class="grid grid-cols-3 gap-4 text-sm mt-2">
<div><span class="text-gray-400">上限</span><br><span>${d.upperLimit ?? '-'}</span></div>
<div><span class="text-gray-400">下限</span><br><span>${d.lowerLimit ?? '-'}</span></div>
<div></div>
</div>
</div>`
}
} catch (e: any) {
qResult.innerHTML = `<div class="text-red-500 text-center py-8">${e.message}</div>`
}
}
qBtn.addEventListener('click', doQuery)
qInput.addEventListener('keydown', e => { if (e.key === 'Enter') doQuery() })
/* ===== Config Page ===== */
let configLoaded = false
async function loadConfig() {
configLoaded = true
const [plcs, mps, stations] = await Promise.all([api.getPLCs(), api.getMPs(), api.getStations()])
renderMPTable(mps)
renderPLCTable(plcs, stations, mps)
}
function renderMPTable(mps: MP[]) {
const el = document.getElementById('mp-list')!
el.innerHTML = `<h3 class="text-lg font-semibold text-gray-800 mb-3">检测点</h3>
<div class="overflow-x-auto">
<table class="w-full text-sm">
<thead><tr class="bg-gray-50 text-gray-500">
<th class="text-left p-2">名称</th><th class="text-left p-2">单位</th><th class="p-2"></th>
</tr></thead><tbody>
${mps.map(m => `<tr class="border-t border-gray-100">
<td class="p-2"><input class="border rounded px-2 py-1 w-28 text-sm" value="${m.PointName}" data-mp-name="${m.Id}" /></td>
<td class="p-2"><input class="border rounded px-2 py-1 w-20 text-sm" value="${m.FieldUnit}" data-mp-unit="${m.Id}" /></td>
<td class="p-2 text-right">
<button class="text-indigo-600 hover:text-indigo-800 text-xs mr-2" data-mp-save="${m.Id}">保存</button>
<button class="text-red-500 hover:text-red-700 text-xs" data-mp-del="${m.Id}">删除</button>
</td>
</tr>`).join('')}
<tr class="border-t border-gray-100">
<td class="p-2"><input id="mp-new-name" class="border rounded px-2 py-1 w-28 text-sm" placeholder="名称" /></td>
<td class="p-2"><input id="mp-new-unit" class="border rounded px-2 py-1 w-20 text-sm" placeholder="单位" /></td>
<td class="p-2 text-right"><button id="mp-add" class="text-green-600 hover:text-green-800 text-xs">添加</button></td>
</tr>
</tbody>
</table>
</div>`
document.querySelectorAll('[data-mp-save]').forEach(b => b.addEventListener('click', () => {
const id = Number((b as HTMLElement).dataset.mpSave)
api.updateMP(id, {
PointName: (document.querySelector(`[data-mp-name="${id}"]`) as HTMLInputElement).value,
FieldUnit: (document.querySelector(`[data-mp-unit="${id}"]`) as HTMLInputElement).value,
}).then(() => loadConfig())
}))
document.querySelectorAll('[data-mp-del]').forEach(b => b.addEventListener('click', () => {
if (!confirm('删除检测点会同时删除关联的工序,确定?')) return
api.delMP(Number((b as HTMLElement).dataset.mpDel)).then(() => loadConfig())
}))
document.getElementById('mp-add')?.addEventListener('click', () => {
const name = (document.getElementById('mp-new-name') as HTMLInputElement).value
if (!name) return
api.addMP({
PointName: name,
FieldUnit: (document.getElementById('mp-new-unit') as HTMLInputElement).value,
}).then(() => loadConfig())
})
}
function renderPLCTable(plcs: PLC[], stations: Station[], mps: MP[]) {
const el = document.getElementById('plc-list')!
el.innerHTML = `<h3 class="text-lg font-semibold text-gray-800 mb-3">PLC</h3>
<div class="overflow-x-auto">
<table class="w-full text-sm">
<thead><tr class="bg-gray-50 text-gray-500">
<th class="text-left p-2">名称</th><th class="text-left p-2">IP</th><th class="text-left p-2">DB</th><th class="p-2"></th>
</tr></thead><tbody>
${plcs.map(p => `<tr class="border-t border-gray-100">
<td class="p-2"><input class="border rounded px-2 py-1 w-28 text-sm" value="${p.PLCName}" data-plc-name="${p.Id}" /></td>
<td class="p-2"><input class="border rounded px-2 py-1 w-32 text-sm" value="${p.IPAddress}" data-plc-ip="${p.Id}" /></td>
<td class="p-2"><input class="border rounded px-2 py-1 w-16 text-sm" type="number" value="${p.DBNumber}" data-plc-db="${p.Id}" /></td>
<td class="p-2 text-right">
<button class="text-indigo-600 hover:text-indigo-800 text-xs mr-2" data-plc-save="${p.Id}">保存</button>
<button class="text-red-500 hover:text-red-700 text-xs" data-plc-del="${p.Id}">删除</button>
</td>
</tr>
<tr class="bg-gray-50/50">
<td colspan="4" class="p-2 pl-8">
<span class="text-xs text-gray-400">工序: </span>
${stations.filter(s => s.PLCId === p.Id).map(s => `<span class="inline-block bg-white border rounded px-2 py-0.5 text-xs mr-1 mb-1">${s.StationName} <button class="text-red-400 hover:text-red-600 ml-1" data-sta-del="${s.Id}">×</button></span>`).join('')}
<div class="flex gap-1 items-center mt-1">
<select class="border rounded px-1 py-0.5 text-xs" data-sta-point="${p.Id}">
<option value="">选检测点</option>
${mps.map(m => `<option value="${m.Id}">${m.PointName}</option>`).join('')}
</select>
<button class="text-xs text-green-600 hover:text-green-700" data-sta-add="${p.Id}">添加</button>
</div>
</td>
</tr>`).join('')}
<tr class="border-t border-gray-100">
<td class="p-2"><input id="plc-new-name" class="border rounded px-2 py-1 w-28 text-sm" placeholder="名称" /></td>
<td class="p-2"><input id="plc-new-ip" class="border rounded px-2 py-1 w-32 text-sm" placeholder="IP" /></td>
<td class="p-2"><input id="plc-new-db" class="border rounded px-2 py-1 w-16 text-sm" type="number" placeholder="DB" /></td>
<td class="p-2 text-right"><button id="plc-add" class="text-green-600 hover:text-green-800 text-xs">添加</button></td>
</tr>
</tbody>
</table>
</div>`
document.querySelectorAll('[data-plc-save]').forEach(b => b.addEventListener('click', () => {
const id = Number((b as HTMLElement).dataset.plcSave)
api.updatePLC(id, {
PLCName: (document.querySelector(`[data-plc-name="${id}"]`) as HTMLInputElement).value,
IPAddress: (document.querySelector(`[data-plc-ip="${id}"]`) as HTMLInputElement).value,
DBNumber: Number((document.querySelector(`[data-plc-db="${id}"]`) as HTMLInputElement).value),
}).then(() => loadConfig())
}))
document.querySelectorAll('[data-plc-del]').forEach(b => b.addEventListener('click', () => {
if (!confirm('删除PLC会同时删除其工序,确定?')) return
api.delPLC(Number((b as HTMLElement).dataset.plcDel)).then(() => loadConfig())
}))
document.querySelectorAll('[data-sta-del]').forEach(b => b.addEventListener('click', () => {
api.delStation(Number((b as HTMLElement).dataset.staDel)).then(() => loadConfig())
}))
document.querySelectorAll('[data-sta-add]').forEach(b => b.addEventListener('click', async () => {
const plcId = Number((b as HTMLElement).dataset.staAdd)
const sel = document.querySelector(`[data-sta-point="${plcId}"]`) as HTMLSelectElement
const pointId = Number(sel.value)
if (!pointId) return
const pointName = sel.options[sel.selectedIndex]?.text || ''
api.addStation({ PLCId: plcId, StationName: pointName, PointId: pointId, StationIndex: 0, SortOrder: 0 }).then(() => loadConfig())
}))
document.getElementById('plc-add')?.addEventListener('click', () => {
const name = (document.getElementById('plc-new-name') as HTMLInputElement).value
if (!name) return
api.addPLC({
PLCName: name,
IPAddress: (document.getElementById('plc-new-ip') as HTMLInputElement).value,
DBNumber: Number((document.getElementById('plc-new-db') as HTMLInputElement).value),
}).then(() => loadConfig())
})
}
document.querySelector('[data-page="config"]')?.addEventListener('click', () => {
if (!configLoaded) loadConfig()
})