Files
JayuCloud/WeChatProgram/admin/pages/inventory/index.js

205 lines
4.8 KiB
JavaScript

const inventoryApi = require('../../api/inventory')
const navigation = require('../../utils/navigation')
const permission = require('../../utils/permission')
const format = require('../../utils/format')
const { markDirty } = require('../../utils/page-state')
function safeList(response) {
if (Array.isArray(response && response.list)) {
return response.list
}
if (Array.isArray(response && response.records)) {
return response.records
}
return []
}
Page({
data: {
loading: false,
list: [],
total: 0,
canManage: false,
form: {
itemName: '',
category: '',
unit: '件',
stockQuantity: '0',
warningThreshold: '1'
}
},
onShow() {
if (!navigation.ensureLogin('进入库存管理')) {
return
}
const permissionCodes = getApp().globalData.permissionCodes || []
this.setData({
canManage: permission.hasPermission(permissionCodes, 'inventory:manage')
})
this.loadItems()
},
onPullDownRefresh() {
this.loadItems().finally(() => {
wx.stopPullDownRefresh()
})
},
async loadItems() {
this.setData({ loading: true })
try {
const response = await inventoryApi.listItems({
page: 1,
size: 30
})
this.setData({
list: safeList(response).map(item => this.formatItem(item)),
total: Number(response && response.total) || 0
})
} catch (error) {
wx.showToast({
title: error.message || '库存加载失败',
icon: 'none'
})
} finally {
this.setData({ loading: false })
}
},
formatItem(item) {
return {
...item,
displayStock: format.formatPrice(item.stockQuantity),
displayWarning: format.formatPrice(item.warningThreshold),
statusText: Number(item.status) === 0 ? '停用' : '启用'
}
},
handleFormInput(event) {
const field = event.currentTarget.dataset.field
if (!field) {
return
}
this.setData({
[`form.${field}`]: event.detail.value
})
},
async handleSaveItem() {
if (!this.data.canManage) {
wx.showToast({
title: '当前账号没有库存维护权限',
icon: 'none'
})
return
}
const form = this.data.form
if (!String(form.itemName || '').trim()) {
wx.showToast({
title: '请填写物料名称',
icon: 'none'
})
return
}
try {
await inventoryApi.saveItem({
itemName: String(form.itemName || '').trim(),
category: String(form.category || '').trim(),
unit: String(form.unit || '').trim() || '件',
stockQuantity: Number(form.stockQuantity) || 0,
warningThreshold: Number(form.warningThreshold) || 0,
status: 1
})
markDirty('admin-workbench')
wx.showToast({
title: '物料已保存',
icon: 'none'
})
this.setData({
form: {
itemName: '',
category: '',
unit: '件',
stockQuantity: '0',
warningThreshold: '1'
}
})
this.loadItems()
} catch (error) {
wx.showToast({
title: error.message || '物料保存失败',
icon: 'none'
})
}
},
promptAdjust(item) {
return new Promise(resolve => {
wx.showModal({
title: `调整 ${item.itemName}`,
content: '填写正数入库,负数出库。',
editable: true,
placeholderText: '例如 5 或 -2',
confirmText: '调整',
success: result => {
if (!result.confirm) {
resolve(null)
return
}
resolve(String(result.content || '').trim())
},
fail: () => resolve(null)
})
})
},
async handleAdjust(event) {
if (!this.data.canManage) {
wx.showToast({
title: '当前账号没有库存调整权限',
icon: 'none'
})
return
}
const item = event.currentTarget.dataset.item
if (!item) {
return
}
const value = await this.promptAdjust(item)
if (value === null) {
return
}
const quantity = Number(value)
if (!isFinite(quantity) || quantity === 0) {
wx.showToast({
title: '请输入非 0 数量',
icon: 'none'
})
return
}
try {
await inventoryApi.adjustStock(item.id, {
changeType: quantity > 0 ? 'IN' : 'OUT',
changeQuantity: quantity,
remark: '管理端小程序调整'
})
markDirty('admin-workbench')
wx.showToast({
title: '库存已调整',
icon: 'none'
})
this.loadItems()
} catch (error) {
wx.showToast({
title: error.message || '库存调整失败',
icon: 'none'
})
}
},
handleBack() {
navigation.goBack('/pages/manage/index')
}
})