JhHardwareWRS/pages/smartMonitor/smartMonitor.js
2026-06-24 15:19:14 +08:00

194 lines
5.6 KiB
JavaScript

const api = require('../../utils/api')
Page({
data: {
locks: [],
unreadRows: [],
readRows: [],
unreadPage: 1,
readPage: 1,
unreadPageInput: '1',
readPageInput: '1',
unreadTotal: 0,
readTotal: 0,
unreadTotalPages: 1,
readTotalPages: 1,
loading: false,
refreshing: false,
detailVisible: false,
detailType: '',
selectedLock: null,
selectedFeedback: null,
},
onShow() {
this.load().catch(error => {
wx.showToast({ title: error.message || '加载失败', icon: 'none' })
})
},
async onPullDownRefresh() {
this.setData({ refreshing: true })
try {
await this.load()
} finally {
this.setData({ refreshing: false })
if (wx.stopPullDownRefresh) {
wx.stopPullDownRefresh()
}
}
},
async load() {
this.setData({ loading: true })
try {
await Promise.all([
this.loadLocks(),
this.loadFeedbacks('unread', this.data.unreadPage),
this.loadFeedbacks('read', this.data.readPage),
])
} finally {
this.setData({ loading: false })
}
},
async loadLocks() {
const locks = await api.listMonitorLocks()
this.setData({ locks })
},
async loadFeedbacks(box, page = 1) {
const result = await api.listMoldLockFeedbacks({ box, page, pageSize: 10 })
if (box === 'read') {
this.setData({
readRows: result.rows || [],
readPage: result.page || 1,
readPageInput: String(result.page || 1),
readTotal: result.total || 0,
readTotalPages: result.totalPages || 1,
})
return
}
this.setData({
unreadRows: result.rows || [],
unreadPage: result.page || 1,
unreadPageInput: String(result.page || 1),
unreadTotal: result.total || 0,
unreadTotalPages: result.totalPages || 1,
})
},
async changeFeedbackPage(e) {
const box = e.currentTarget.dataset.box
const direction = Number(e.currentTarget.dataset.direction || 0)
const pageKey = box === 'read' ? 'readPage' : 'unreadPage'
const totalPagesKey = box === 'read' ? 'readTotalPages' : 'unreadTotalPages'
const nextPage = Math.max(1, Math.min(this.data[totalPagesKey], this.data[pageKey] + direction))
await this.loadFeedbacks(box, nextPage)
},
onFeedbackPageInput(e) {
const box = e.currentTarget.dataset.box
const inputKey = box === 'read' ? 'readPageInput' : 'unreadPageInput'
this.setData({ [inputKey]: e.detail.value })
},
async jumpFeedbackPage(e) {
wx.hideKeyboard()
const box = e.currentTarget.dataset.box
const pageKey = box === 'read' ? 'readPage' : 'unreadPage'
const inputKey = box === 'read' ? 'readPageInput' : 'unreadPageInput'
const totalPagesKey = box === 'read' ? 'readTotalPages' : 'unreadTotalPages'
const inputValue = e && e.detail && e.detail.value !== undefined ? e.detail.value : this.data[inputKey]
const target = Math.max(1, Math.min(Number(inputValue) || this.data[pageKey], this.data[totalPagesKey]))
if (target === this.data[pageKey]) {
this.setData({ [inputKey]: String(this.data[pageKey] || 1) })
return
}
await this.loadFeedbacks(box, target)
},
openLock(e) {
const index = Number(e.currentTarget.dataset.index)
const selectedLock = this.data.locks[index]
if (!selectedLock) {
return
}
this.setData({
detailVisible: true,
detailType: 'lock',
selectedLock,
selectedFeedback: null,
})
},
async openFeedback(e) {
const box = e.currentTarget.dataset.box
const index = Number(e.currentTarget.dataset.index)
const rows = box === 'read' ? this.data.readRows : this.data.unreadRows
const feedback = rows[index]
if (!feedback) {
return
}
wx.showLoading({ title: '加载中' })
try {
const selectedFeedback = box === 'unread'
? await api.readMoldLockFeedback(feedback.id)
: feedback
this.setData({
detailVisible: true,
detailType: 'feedback',
selectedLock: null,
selectedFeedback,
})
if (box === 'unread') {
await Promise.all([
this.loadFeedbacks('unread', this.data.unreadPage),
this.loadFeedbacks('read', 1),
])
}
} catch (error) {
wx.showToast({ title: error.message || '加载失败', icon: 'none' })
} finally {
wx.hideLoading()
}
},
closeDetail() {
this.setData({
detailVisible: false,
detailType: '',
selectedLock: null,
selectedFeedback: null,
})
},
noop() {},
async releaseFromLock() {
const lock = this.data.selectedLock
if (!lock) {
return
}
wx.showLoading({ title: '解除中' })
try {
await api.releaseMoldLock(lock.id)
wx.showToast({ title: '已解除占用', icon: 'success' })
this.closeDetail()
await this.load()
} catch (error) {
wx.showToast({ title: error.message || '解除失败', icon: 'none' })
} finally {
wx.hideLoading()
}
},
async releaseFromFeedback() {
const feedback = this.data.selectedFeedback
if (!feedback || !feedback.canRelease) {
return
}
wx.showLoading({ title: '解除中' })
try {
const selectedFeedback = await api.releaseMoldLockByFeedback(feedback.id)
wx.showToast({ title: '已解除占用', icon: 'success' })
this.setData({ selectedFeedback })
await Promise.all([
this.loadLocks(),
this.loadFeedbacks('unread', this.data.unreadPage),
this.loadFeedbacks('read', this.data.readPage),
])
} catch (error) {
wx.showToast({ title: error.message || '解除失败', icon: 'none' })
} finally {
wx.hideLoading()
}
},
})