4 changed files with 234 additions and 0 deletions
-
2src/api/qc/qc.js
-
183src/views/modules/qc/QcSopBatchUpload.vue
-
5src/views/modules/qc/qcPartAttribute.vue
-
44src/views/modules/qc/sopBatchTemplate.js
@ -0,0 +1,183 @@ |
|||||
|
<template> |
||||
|
<el-dialog class="sop-batch" title="SOP批量上传" :visible="true" width="820px" :close-on-click-modal="false" |
||||
|
:close-on-press-escape="!busy" :show-close="!busy" :before-close="close"> |
||||
|
<el-row :gutter="24"> |
||||
|
<el-col :xs="24" :sm="12"> |
||||
|
<h4>SOP 文件</h4> |
||||
|
<el-upload v-if="!started" action="" drag multiple :auto-upload="false" |
||||
|
:show-file-list="false" :on-change="addFile"> |
||||
|
<i class="el-icon-upload"></i> |
||||
|
<div class="el-upload__text">拖入多个文件,或<em>选择文件</em></div> |
||||
|
</el-upload> |
||||
|
<ul class="files"> |
||||
|
<li v-for="(file, index) in files" :key="file.id"> |
||||
|
<span>{{ file.raw.name }}</span> |
||||
|
<el-button v-if="!started" type="text" @click="files.splice(index, 1)">移除</el-button> |
||||
|
<span v-else>{{ file.status }}</span> |
||||
|
<div v-if="file.error" class="error">{{ file.error }}</div> |
||||
|
</li> |
||||
|
</ul> |
||||
|
</el-col> |
||||
|
<el-col :xs="24" :sm="12"> |
||||
|
<h4>物料清单</h4> |
||||
|
<el-button size="small" @click="download">下载模板</el-button> |
||||
|
<el-button type="primary" size="small" :loading="checking" :disabled="started || checking" |
||||
|
@click="$refs.templateInput.click()">上传模板</el-button> |
||||
|
<input ref="templateInput" type="file" accept=".xlsx" hidden @change="readTemplate"> |
||||
|
<p>从第二行开始填写。BU 可填该 Site 下的名称或编码,校验通过后显示 BU 编码。物料名称可留空,检验类型可填名称或编码。</p> |
||||
|
<p v-if="templateName">{{ templateName }}</p> |
||||
|
<p v-if="templateError" class="error" role="alert">{{ templateError }}</p> |
||||
|
</el-col> |
||||
|
</el-row> |
||||
|
<template v-if="results.length"> |
||||
|
<p role="status">模板校验成功 {{ validRows.length }} 条,失败 {{ results.length - validRows.length }} 条。保存时,每份 SOP 将关联到全部校验成功的记录。</p> |
||||
|
<el-table :data="results" max-height="230" border size="small"> |
||||
|
<el-table-column prop="rowNumber" label="行号" width="60"></el-table-column> |
||||
|
<el-table-column prop="site" label="Site" width="80" show-overflow-tooltip></el-table-column> |
||||
|
<el-table-column prop="buNo" label="BU" width="80" show-overflow-tooltip></el-table-column> |
||||
|
<el-table-column prop="partNo" label="物料编号" min-width="110"></el-table-column> |
||||
|
<el-table-column prop="partDesc" label="物料名称" min-width="140" show-overflow-tooltip></el-table-column> |
||||
|
<el-table-column prop="inspectionType" label="检验类型" width="90"></el-table-column> |
||||
|
<el-table-column label="校验结果" min-width="180" show-overflow-tooltip> |
||||
|
<template slot-scope="scope"><span :class="{error: scope.row.error}">{{ scope.row.error || '通过' }}</span></template> |
||||
|
</el-table-column> |
||||
|
</el-table> |
||||
|
</template> |
||||
|
<span slot="footer"> |
||||
|
<el-button :disabled="busy" @click="close">关闭</el-button> |
||||
|
<el-button type="primary" :loading="saving" :disabled="busy || !pendingFiles.length || !validRows.length" |
||||
|
@click="save">{{ saving ? '保存中' : saveLabel }}</el-button> |
||||
|
</span> |
||||
|
</el-dialog> |
||||
|
</template> |
||||
|
|
||||
|
<script> |
||||
|
/* global __webpack_public_path__ */ |
||||
|
import { saveAs } from 'file-saver' |
||||
|
import { validateSopBatch, saveSopBatch } from '@/api/qc/qc' |
||||
|
import { parseSopTemplate } from './sopBatchTemplate' |
||||
|
|
||||
|
function uploadId () { |
||||
|
const bytes = window.crypto.getRandomValues(new Uint8Array(16)) |
||||
|
bytes[6] = (bytes[6] & 15) | 64 |
||||
|
bytes[8] = (bytes[8] & 63) | 128 |
||||
|
const hex = Array.from(bytes, b => ('0' + b.toString(16)).slice(-2)).join('') |
||||
|
return [hex.slice(0, 8), hex.slice(8, 12), hex.slice(12, 16), hex.slice(16, 20), hex.slice(20)].join('-') |
||||
|
} |
||||
|
|
||||
|
export default { |
||||
|
data () { |
||||
|
return { |
||||
|
files: [], rows: [], results: [], templateName: '', templateError: '', |
||||
|
checking: false, saving: false, started: false |
||||
|
} |
||||
|
}, |
||||
|
computed: { |
||||
|
busy () { return this.checking || this.saving }, |
||||
|
saveLabel () { return this.started && this.pendingFiles.length ? '重试未保存文件' : '保存' }, |
||||
|
validRows () { return this.results.filter(row => !row.error) }, |
||||
|
pendingFiles () { return this.files.filter(file => !file.saved) } |
||||
|
}, |
||||
|
methods: { |
||||
|
close () { |
||||
|
if (!this.busy) this.$emit('close') |
||||
|
}, |
||||
|
addFile ({raw}) { |
||||
|
if (this.started) return |
||||
|
if (!raw.size) return this.$message.warning(raw.name + ' 是空文件') |
||||
|
if (this.files.some(file => file.raw.name === raw.name && file.raw.size === raw.size && file.raw.lastModified === raw.lastModified)) { |
||||
|
return this.$message.warning(raw.name + ' 已在文件清单中') |
||||
|
} |
||||
|
this.files.push({id: uploadId(), raw, saved: false, status: '待保存', error: ''}) |
||||
|
}, |
||||
|
download () { |
||||
|
const name = 'SOP批量上传模板.xlsx' |
||||
|
saveAs(__webpack_public_path__ + 'static/templates/' + name, name) |
||||
|
}, |
||||
|
async readTemplate (event) { |
||||
|
const file = event.target.files[0] |
||||
|
event.target.value = '' |
||||
|
if (!file || this.busy || this.started) return |
||||
|
this.rows = [] |
||||
|
this.results = [] |
||||
|
this.templateError = '' |
||||
|
this.templateName = file.name |
||||
|
this.checking = true |
||||
|
try { |
||||
|
if (!/\.xlsx$/i.test(file.name)) throw new Error('请选择 .xlsx 模板文件') |
||||
|
const buffer = await new Promise((resolve, reject) => { |
||||
|
const reader = new FileReader() |
||||
|
reader.onload = () => resolve(reader.result) |
||||
|
reader.onerror = () => reject(new Error('模板文件读取失败')) |
||||
|
reader.readAsArrayBuffer(file) |
||||
|
}) |
||||
|
this.rows = parseSopTemplate(buffer) |
||||
|
await this.validate() |
||||
|
} catch (error) { |
||||
|
this.templateError = error.message |
||||
|
} finally { |
||||
|
this.checking = false |
||||
|
} |
||||
|
}, |
||||
|
async validate () { |
||||
|
if (!this.rows.length || this.started) return |
||||
|
this.checking = true |
||||
|
this.templateError = '' |
||||
|
this.results = [] |
||||
|
try { |
||||
|
const {data} = await validateSopBatch(this.rows) |
||||
|
if (data.code !== 0) throw new Error(data.msg || '模板校验失败') |
||||
|
this.results = data.rows |
||||
|
} catch (error) { |
||||
|
this.templateError = error.message |
||||
|
} finally { |
||||
|
this.checking = false |
||||
|
} |
||||
|
}, |
||||
|
async save () { |
||||
|
if (this.busy || !this.validRows.length || !this.pendingFiles.length) return |
||||
|
this.saving = true |
||||
|
this.started = true |
||||
|
try { |
||||
|
const rows = new Blob([JSON.stringify(this.validRows)], {type: 'application/json'}) |
||||
|
for (const file of this.pendingFiles) { |
||||
|
file.status = '保存中' |
||||
|
file.error = '' |
||||
|
try { |
||||
|
const form = new FormData() |
||||
|
form.append('file', file.raw) |
||||
|
form.append('rows', rows) |
||||
|
form.append('uploadId', file.id) |
||||
|
const {data} = await saveSopBatch(form) |
||||
|
if (data.code !== 0) throw new Error(data.msg || '保存失败') |
||||
|
file.saved = true |
||||
|
file.status = '已保存' |
||||
|
} catch (error) { |
||||
|
file.status = '待重试' |
||||
|
file.error = error.message |
||||
|
} |
||||
|
} |
||||
|
const failures = this.pendingFiles.length |
||||
|
this.$message({ |
||||
|
type: failures ? 'warning' : 'success', |
||||
|
message: 'SOP 文件保存成功 ' + (this.files.length - failures) + ' 份,未确认保存 ' + failures + ' 份' |
||||
|
}) |
||||
|
} finally { |
||||
|
this.saving = false |
||||
|
} |
||||
|
if (!this.pendingFiles.length) this.close() |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
</script> |
||||
|
|
||||
|
<style scoped> |
||||
|
.sop-batch /deep/ .el-dialog { max-width: 96vw; } |
||||
|
.sop-batch /deep/ .el-upload, .sop-batch /deep/ .el-upload-dragger { width: 100%; } |
||||
|
h4 { margin: 0 0 12px; } |
||||
|
p { line-height: 1.6; } |
||||
|
.files { padding: 0; list-style: none; max-height: 200px; overflow-y: auto; } |
||||
|
.files li { overflow-wrap: anywhere; } |
||||
|
.files li > span:first-child { margin-right: 10px; } |
||||
|
.error { color: #b42318; } |
||||
|
</style> |
||||
@ -0,0 +1,44 @@ |
|||||
|
import * as XLSX from 'xlsx' |
||||
|
|
||||
|
const fields = ['Site', 'BU', '物料编号', '物料名称', '检验类型'] |
||||
|
|
||||
|
export function parseSopTemplate (buffer) { |
||||
|
const bytes = new Uint8Array(buffer) |
||||
|
const formatError = '无法读取 XLSX 工作簿,请使用下载的 .xlsx 模板' |
||||
|
// SheetJS 也会读取文本和旧版 Excel,这里只接受打包的工作簿。
|
||||
|
if (bytes[0] !== 0x50 || bytes[1] !== 0x4B || bytes[2] !== 3 || bytes[3] !== 4) throw new Error(formatError) |
||||
|
let workbook |
||||
|
try { |
||||
|
workbook = XLSX.read(bytes, {type: 'array'}) |
||||
|
} catch (error) { |
||||
|
throw new Error(formatError) |
||||
|
} |
||||
|
const sheet = workbook.Sheets['物料清单'] || workbook.Sheets[workbook.SheetNames[0]] |
||||
|
if (!sheet || !workbook.Directory || !workbook.Directory.workbooks.some(path => /\.xml$/i.test(path))) { |
||||
|
throw new Error(formatError) |
||||
|
} |
||||
|
const rows = new Map() |
||||
|
// 只遍历有内容的单元格,整列文本格式和空行不会产生物料记录。
|
||||
|
Object.keys(sheet).forEach(address => { |
||||
|
if (address[0] === '!') return |
||||
|
const value = XLSX.utils.format_cell(sheet[address]).trim() |
||||
|
if (!value) return |
||||
|
const {r, c} = XLSX.utils.decode_cell(address) |
||||
|
const rowNumber = r + 1 |
||||
|
if (c >= fields.length) throw new Error('第' + rowNumber + '行超出模板的五列') |
||||
|
if (!rows.has(rowNumber)) rows.set(rowNumber, Array(fields.length).fill('')) |
||||
|
rows.get(rowNumber)[c] = value |
||||
|
}) |
||||
|
const header = (rows.get(1) || []).map(value => value.toLowerCase()) |
||||
|
const columns = fields.map(field => header.indexOf(field.toLowerCase())) |
||||
|
if (columns.includes(-1)) { |
||||
|
throw new Error('请使用最新模板,第一行表头应包含' + fields.join('、')) |
||||
|
} |
||||
|
rows.delete(1) |
||||
|
if (!rows.size) throw new Error('模板中没有物料记录,请从第二行开始填写') |
||||
|
return Array.from(rows.keys()).sort((a, b) => a - b).map(rowNumber => { |
||||
|
const values = rows.get(rowNumber) |
||||
|
const [site, buNo, partNo, partDesc, inspectionType] = columns.map(column => values[column]) |
||||
|
return {rowNumber, partNo, partDesc, inspectionType, site, buNo} |
||||
|
}) |
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue