Files
shanxia2.0/miniprogram/api/db.js
T
2026-07-17 21:59:47 +08:00

127 lines
3.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 地磅模块 API(直出 JSON,对齐原 dibang;路径 /plans /weighing /inventory /dashboard)
*/
import { request, parseResponseJson } from '@/utils/request'
function extractError(body) {
if (!body || typeof body !== 'object') return '请求失败'
const detail = body.detail
if (typeof detail === 'string') return detail
if (Array.isArray(detail)) {
return detail.map((x) => x?.msg || x?.message || '').filter(Boolean).join(';') || '请求失败'
}
return body.msg || body.message || '请求失败'
}
function unwrap(res) {
if (res.statusCode === 204) return null
if (res.statusCode >= 400) {
throw new Error(extractError(parseResponseJson(res)))
}
const body = parseResponseJson(res)
if (body == null) return body
if (Array.isArray(body)) return body
if (typeof body === 'object' && body.code != null) {
if (body.code !== 200 && body.code !== 0) {
throw new Error(body.message || body.msg || '请求失败')
}
return body.data != null ? body.data : body
}
return body
}
async function get(url, params = {}) {
const res = await request({ url, method: 'GET', data: params, params })
return unwrap(res)
}
async function post(url, data = {}) {
const res = await request({ url, method: 'POST', data })
return unwrap(res)
}
async function patch(url, data = {}) {
const res = await request({ url, method: 'PATCH', data })
return unwrap(res)
}
function asList(data) {
return Array.isArray(data) ? data : []
}
/** 集合根须带尾斜杠,避免 307 丢 Authorization */
function root(path) {
return path.endsWith('/') ? path : `${path}/`
}
// ── 仪表盘 ──
export async function getDashboardStats() {
return (await get('/dashboard/stats')) || {}
}
export async function getInboundByCargo() {
return asList(await get('/dashboard/inbound/by-cargo'))
}
export async function getOutboundByCargo() {
return asList(await get('/dashboard/outbound/by-cargo'))
}
// ── 计划 ──
export async function listPlans(params = {}) {
return asList(await get(root('/plans'), params))
}
export async function getPlan(id) {
return get(`/plans/${String(id)}`)
}
export async function getPlanRelated(id) {
return (await get(`/plans/${String(id)}/related`)) || { inventories: [], weighings: [] }
}
// ── 地磅单 ──
export async function listWeighing(params = {}) {
return asList(await get(root('/weighing'), params))
}
export async function getWeighing(id) {
return get(`/weighing/${String(id)}`)
}
export async function getWeighingRelated(id) {
return (await get(`/weighing/${String(id)}/related`)) || { plan: null, inventory: null }
}
export function updateWeighing(id, data) {
return patch(`/weighing/${String(id)}`, data)
}
export function voidWeighing(id) {
return post(`/weighing/${String(id)}/void`, {})
}
// ── 出入库 ──
export async function listInventory(params = {}) {
return asList(await get(root('/inventory'), params))
}
export async function getInventory(id) {
return get(`/inventory/${String(id)}`)
}
export async function getInventoryRelated(id) {
return (await get(`/inventory/${String(id)}/related`)) || { plan: null, weighing: null }
}
export function createInventoryLinkForPlan(purchasePlanId, remarks) {
return post('/inventory/create-link-for-plan', {
purchasePlanId: String(purchasePlanId),
remarks
})
}