102 lines
2.9 KiB
JavaScript
102 lines
2.9 KiB
JavaScript
const api = require('../../utils/api')
|
|
|
|
const decodeOption = value => {
|
|
if (value === undefined || value === null) {
|
|
return ''
|
|
}
|
|
try {
|
|
return decodeURIComponent(String(value))
|
|
} catch (error) {
|
|
return String(value)
|
|
}
|
|
}
|
|
|
|
const valueText = (value, metricKind) => {
|
|
const numericValue = Number(value || 0)
|
|
if (metricKind === 'quantity') {
|
|
return `${numericValue} 件`
|
|
}
|
|
return api.formatDurationAllUnits(numericValue)
|
|
}
|
|
|
|
const metricTitle = metricKind => (metricKind === 'quantity' ? '清洗数量' : '总使用时长')
|
|
|
|
const normalizeFilters = options => ({
|
|
category: decodeOption(options.category),
|
|
id: decodeOption(options.id),
|
|
metricKind: decodeOption(options.metricKind),
|
|
startDate: decodeOption(options.startDate),
|
|
endDate: decodeOption(options.endDate),
|
|
attendancePointName: decodeOption(options.attendancePointName),
|
|
name: decodeOption(options.name),
|
|
productName: decodeOption(options.productName),
|
|
processName: decodeOption(options.processName),
|
|
stampingMethod: decodeOption(options.stampingMethod),
|
|
})
|
|
|
|
const decorateDetail = detail => {
|
|
const metricKind = detail.metricKind || ''
|
|
const dailyRows = detail.dailyRows || []
|
|
const maxDailyValue = Math.max(1, ...dailyRows.map(item => Number(item.value || 0)))
|
|
|
|
return {
|
|
...detail,
|
|
metricTitle: metricTitle(metricKind),
|
|
valueText: valueText(detail.value, metricKind),
|
|
dailyRows: dailyRows.map(item => ({
|
|
...item,
|
|
valueText: valueText(item.value, metricKind),
|
|
barPercent: Math.max(4, Math.round(Number(item.value || 0) / maxDailyValue * 100)),
|
|
})),
|
|
reportRows: (detail.reportRows || []).map(item => ({
|
|
...item,
|
|
valueText: valueText(item.value, metricKind),
|
|
employeeText: item.employeeName || item.employeePhone || '-',
|
|
})),
|
|
tags: Array.isArray(detail.tags) ? detail.tags : [],
|
|
}
|
|
}
|
|
|
|
Page({
|
|
data: {
|
|
filters: {},
|
|
detail: null,
|
|
loading: false,
|
|
loadError: false,
|
|
errorText: '',
|
|
},
|
|
|
|
onLoad(options) {
|
|
const filters = normalizeFilters(options || {})
|
|
this.setData({ filters })
|
|
this.load(filters)
|
|
},
|
|
|
|
async load(filters = this.data.filters) {
|
|
const loadSeq = (this.loadSeq || 0) + 1
|
|
this.loadSeq = loadSeq
|
|
this.setData({ loading: true, loadError: false, errorText: '' })
|
|
try {
|
|
const detail = await api.getUsageStatsDetail(filters)
|
|
if (loadSeq !== this.loadSeq) {
|
|
return
|
|
}
|
|
this.setData({ detail: decorateDetail(detail || {}), loadError: false, errorText: '' })
|
|
} catch (error) {
|
|
if (loadSeq === this.loadSeq) {
|
|
const errorText = error.message || '加载详情失败'
|
|
this.setData({ detail: null, loadError: true, errorText })
|
|
wx.showToast({ title: errorText, icon: 'none' })
|
|
}
|
|
} finally {
|
|
if (loadSeq === this.loadSeq) {
|
|
this.setData({ loading: false })
|
|
}
|
|
}
|
|
},
|
|
|
|
reload() {
|
|
this.load()
|
|
},
|
|
})
|