init: PLC data collection system
This commit is contained in:
commit
ae90bc9368
38 files changed
+5234
No files matched your search
@@ -0,0 +1,33 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>质量追溯系统</title>
|
||||
<script src="https://cdn.tailwindcss.com"></script>
|
||||
</head>
|
||||
<body class="bg-gray-50 text-gray-800 min-h-screen">
|
||||
<nav class="bg-indigo-900 text-white px-6 py-3 flex items-center gap-6 shadow-md">
|
||||
<h1 class="text-lg font-bold tracking-wide">质量追溯系统</h1>
|
||||
<div class="flex gap-1">
|
||||
<button data-page="query" class="tab-btn active px-4 py-1.5 rounded-lg text-sm hover:bg-white/10 transition">追溯查询</button>
|
||||
<button data-page="config" class="tab-btn px-4 py-1.5 rounded-lg text-sm hover:bg-white/10 transition">设备配置</button>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<div id="page-query" class="page active max-w-3xl mx-auto p-6">
|
||||
<div class="flex gap-2 mb-6">
|
||||
<input id="q-input" type="text" placeholder="输入产品编号,如 PROD-003" class="flex-1 px-4 py-2.5 border border-gray-300 rounded-xl text-sm focus:outline-none focus:ring-2 focus:ring-indigo-400 focus:border-transparent" />
|
||||
<button id="q-btn" class="px-6 py-2.5 bg-indigo-700 text-white rounded-xl text-sm font-medium hover:bg-indigo-800 transition">查询</button>
|
||||
</div>
|
||||
<div id="q-result"></div>
|
||||
</div>
|
||||
|
||||
<div id="page-config" class="page max-w-5xl mx-auto p-6">
|
||||
<div id="mp-list"></div>
|
||||
<div class="mt-8" id="plc-list"></div>
|
||||
</div>
|
||||
|
||||
<script type="module" src="./src/main.ts"></script>
|
||||
</body>
|
||||
</html>
|
||||
Generated
+1352
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "scanner-record-web",
|
||||
"version": "1.0.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
"build": "vite build",
|
||||
"preview": "vite preview"
|
||||
},
|
||||
"devDependencies": {
|
||||
"autoprefixer": "^10.5.0",
|
||||
"postcss": "^8.5.15",
|
||||
"tailwindcss": "^4.3.0",
|
||||
"typescript": "^5.7.0",
|
||||
"vite": "^6.3.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
const BASE = '/api'
|
||||
|
||||
async function get<T>(path: string): Promise<T> {
|
||||
const res = await fetch(`${BASE}${path}`)
|
||||
if (!res.ok) throw new Error(`HTTP ${res.status}`)
|
||||
return res.json()
|
||||
}
|
||||
|
||||
async function post<T>(path: string, body: unknown): Promise<T> {
|
||||
const res = await fetch(`${BASE}${path}`, {
|
||||
method: 'POST', headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(body),
|
||||
})
|
||||
if (!res.ok) throw new Error(`HTTP ${res.status}`)
|
||||
return res.json()
|
||||
}
|
||||
|
||||
async function put<T>(path: string, body: unknown): Promise<T> {
|
||||
const res = await fetch(`${BASE}${path}`, {
|
||||
method: 'PUT', headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(body),
|
||||
})
|
||||
if (!res.ok) throw new Error(`HTTP ${res.status}`)
|
||||
return res.json()
|
||||
}
|
||||
|
||||
async function del(path: string): Promise<void> {
|
||||
const res = await fetch(`${BASE}${path}`, { method: 'DELETE' })
|
||||
if (!res.ok) throw new Error(`HTTP ${res.status}`)
|
||||
}
|
||||
|
||||
export interface PLC { Id: number; PLCName: string; IPAddress: string; DBNumber: number; StationCount: number; SortOrder: number }
|
||||
export interface MP { Id: number; PointName: string; FieldUnit: string; SortOrder: number }
|
||||
export interface Station { Id: number; PLCId: number; StationIndex: number; StationName: string; PointId: number; SortOrder: number; pointName?: string }
|
||||
export interface QPoint { pointId: number; pointName: string; fieldUnit: string; recordTime: string; isQualified: number; inspectionCount: number; upperLimit: number | null; lowerLimit: number | null; measuredValue: number | null }
|
||||
export interface QResult { productId: string; data: QPoint[]; message?: string }
|
||||
export interface IResult { currentStation: string; currentPoint: string; prevStation: string | null; prevResult: number; message?: string }
|
||||
export interface Status { plcCount: number; stationCount: number; pointCount: number; dataTableCount: number; totalRecords: number }
|
||||
|
||||
export const api = {
|
||||
getPLCs: () => get<PLC[]>('/config/plcs'),
|
||||
getMPs: () => get<MP[]>('/config/measurement-points'),
|
||||
getStations: (plcId?: number) => get<Station[]>(`/config/stations${plcId ? `?plcId=${plcId}` : ''}`),
|
||||
addPLC: (d: Partial<PLC>) => post<{ Id: number }>('/config/plcs', d),
|
||||
updatePLC: (id: number, d: Partial<PLC>) => put(`/config/plcs/${id}`, d),
|
||||
delPLC: (id: number) => del(`/config/plcs/${id}`),
|
||||
addMP: (d: Partial<MP>) => post<{ Id: number }>('/config/measurement-points', d),
|
||||
updateMP: (id: number, d: Partial<MP>) => put(`/config/measurement-points/${id}`, d),
|
||||
delMP: (id: number) => del(`/config/measurement-points/${id}`),
|
||||
addStation: (d: Partial<Station>) => post<{ Id: number }>('/config/stations', d),
|
||||
updateStation: (id: number, d: Partial<Station>) => put(`/config/stations/${id}`, d),
|
||||
delStation: (id: number) => del(`/config/stations/${id}`),
|
||||
queryProduct: (pid: string) => get<QResult>(`/query/${pid}`),
|
||||
queryInterlock: (pid: string, sn: string) => get<IResult>(`/query/${pid}/interlock/${encodeURIComponent(sn)}`),
|
||||
getStatus: () => get<Status>('/monitor/status'),
|
||||
}
|
||||
@@ -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()
|
||||
})
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
.page { display: none; }
|
||||
.page.active { display: block; }
|
||||
.tab-btn.active { background: rgba(255,255,255,0.2); color: #fff; font-weight: 600; }
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2020",
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "bundler",
|
||||
"strict": true,
|
||||
"skipLibCheck": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"include": ["src"]
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { defineConfig } from 'vite'
|
||||
|
||||
export default defineConfig({
|
||||
base: '/web/',
|
||||
server: {
|
||||
port: 5173,
|
||||
proxy: {
|
||||
'/api': {
|
||||
target: 'http://localhost:3000',
|
||||
changeOrigin: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
build: {
|
||||
outDir: 'dist',
|
||||
},
|
||||
})
|
||||
Reference in new issue
Block a user