const { ensureMobileRole } = require('../../utils/roleGuard'); const { beginPanelShow, endPanelShow } = require('../../utils/panelShowGate'); const { prepareOrderImageBase64 } = require('../../utils/orderImage'); const { fetchSupplierQualityReports, fetchSupplierQualityReportTypes, uploadSupplierQualityReport, } = require('../../utils/request'); function validityClass(status) { if (status === 'in_range') return 'status-ok'; if (status === 'expired' || status === 'not_yet') return 'status-bad'; return 'status-warn'; } function mapUploaded(row) { const related = Array.isArray(row.related_batch_nos) ? row.related_batch_nos.filter(Boolean) : []; const batches = related.length ? related : row.batch_no ? [row.batch_no] : []; return { ...row, validityClass: validityClass(row.validity_status), batchText: batches.length ? batches.join('、') : '尚未入库', batchWarn: !batches.length, firstBatch: batches[0] || '', }; } Component({ options: { addGlobalClass: true }, properties: { embedded: { type: Boolean, value: false }, }, data: { tab: 'pending', loading: false, error: '', pending: [], uploaded: [], typeOptions: [], typeLabels: [], uploadTypeIndex: 0, uploadTypeCode: 'other', }, lifetimes: { attached() { this.onPanelShow(); }, }, pageLifetimes: { show() { this.onPanelShow(); }, }, methods: { async onPanelShow() { if (!beginPanelShow(this)) return; try { const ok = await ensureMobileRole('supplier', { message: '请切换到供应商工作台后再上传报告', }); if (!ok) return; this.load(); } finally { endPanelShow(this); } }, onTab(e) { const tab = e.currentTarget.dataset.tab; if (!tab || tab === this.data.tab) return; this.setData({ tab }); }, onTypePick(e) { const idx = Number(e.detail.value); const opt = (this.data.typeOptions || [])[idx]; if (!opt) return; this.setData({ uploadTypeIndex: idx, uploadTypeCode: opt.code, }); }, async load() { this.setData({ loading: true, error: '' }); try { const [result, typesRes] = await Promise.all([ fetchSupplierQualityReports({ days: 14 }), fetchSupplierQualityReportTypes().catch(() => null), ]); const data = result.data || result || {}; const pending = (data.pending || []).map((item) => ({ ...item, item_names_text: (item.item_names || []).join('、'), })); const uploaded = (data.uploaded || []).map(mapUploaded); const types = (typesRes && typesRes.data && typesRes.data.types) || []; const typeOptions = types.length ? types : [ { code: 'quarantine', label: '检疫报告' }, { code: 'inspection', label: '检验报告' }, { code: 'pesticide', label: '农残报告' }, { code: 'batch', label: '批次报告' }, { code: 'other', label: '其他报告' }, ]; const typeLabels = typeOptions.map((t) => t.label); const first = typeOptions[0] || { code: 'other', label: '其他报告' }; this.setData( { pending, uploaded, typeOptions, typeLabels, uploadTypeIndex: 0, uploadTypeCode: first.code, loading: false, }, () => { this.triggerEvent('badgechange', { count: pending.length }); } ); } catch (err) { this.setData({ loading: false, error: (err && err.message) || '加载失败', pending: [], uploaded: [], }); } }, uploadReport(e) { const id = e.currentTarget.dataset.id; const requiredType = e.currentTarget.dataset.type || ''; if (!id) return; const typeOptions = this.data.typeOptions || []; let reportType = this.data.uploadTypeCode || 'other'; if (requiredType) { const hit = typeOptions.findIndex((t) => t.code === requiredType); if (hit >= 0) { reportType = requiredType; this.setData({ uploadTypeIndex: hit, uploadTypeCode: requiredType }); } else { reportType = requiredType; } } wx.chooseMedia({ count: 3, mediaType: ['image'], sourceType: ['album', 'camera'], success: (res) => { const files = res.tempFiles || []; if (!files.length) return; wx.showLoading({ title: '上传中', mask: true }); Promise.all( files.map((file) => prepareOrderImageBase64(file.tempFilePath).then( (content) => `data:image/jpeg;base64,${content}` ) ) ) .then((payloads) => uploadSupplierQualityReport({ preparation_id: id, files: payloads, report_type: reportType, }) ) .then((result) => { const label = (result && result.data && result.data.validity_label) || '已上传'; wx.showToast({ title: `报告${label}`, icon: 'none' }); this.setData({ tab: 'uploaded' }); this.load(); }) .catch((err) => { wx.showToast({ title: (err && err.message) || '上传失败', icon: 'none' }); }) .finally(() => wx.hideLoading()); }, }); }, preview(e) { const urls = e.currentTarget.dataset.urls || []; const current = e.currentTarget.dataset.url; if (!urls.length) return; wx.previewImage({ urls, current }); }, goTrace(e) { const batch = e.currentTarget.dataset.batch; if (!batch) return; // 供应商无内部溯源权限:走公开 H5 /t/:code(设计 20260720) const config = require('../../config'); const webBase = String(config.webBase || '').replace(/\/$/, ''); if (!webBase) { wx.showToast({ title: '未配置公开溯源地址', icon: 'none' }); return; } const url = `${webBase}/t/${encodeURIComponent(batch)}`; wx.setClipboardData({ data: url, success: () => { wx.showModal({ title: '公开溯源', content: `链接已复制,可在浏览器打开:\n${url}`, showCancel: false, }); }, fail: () => { wx.showModal({ title: '公开溯源', content: url, showCancel: false, }); }, }); }, }, });