db.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * 地磅模块 API(直出 JSON,对齐原 dibang;路径 /plans /weighing /inventory /dashboard)
  3. */
  4. import { request, parseResponseJson } from '@/utils/request'
  5. function extractError(body) {
  6. if (!body || typeof body !== 'object') return '请求失败'
  7. const detail = body.detail
  8. if (typeof detail === 'string') return detail
  9. if (Array.isArray(detail)) {
  10. return detail.map((x) => x?.msg || x?.message || '').filter(Boolean).join(';') || '请求失败'
  11. }
  12. return body.msg || body.message || '请求失败'
  13. }
  14. function unwrap(res) {
  15. if (res.statusCode === 204) return null
  16. if (res.statusCode >= 400) {
  17. throw new Error(extractError(parseResponseJson(res)))
  18. }
  19. const body = parseResponseJson(res)
  20. if (body == null) return body
  21. if (Array.isArray(body)) return body
  22. if (typeof body === 'object' && body.code != null) {
  23. if (body.code !== 200 && body.code !== 0) {
  24. throw new Error(body.message || body.msg || '请求失败')
  25. }
  26. return body.data != null ? body.data : body
  27. }
  28. return body
  29. }
  30. async function get(url, params = {}) {
  31. const res = await request({ url, method: 'GET', data: params, params })
  32. return unwrap(res)
  33. }
  34. async function post(url, data = {}) {
  35. const res = await request({ url, method: 'POST', data })
  36. return unwrap(res)
  37. }
  38. async function patch(url, data = {}) {
  39. const res = await request({ url, method: 'PATCH', data })
  40. return unwrap(res)
  41. }
  42. function asList(data) {
  43. return Array.isArray(data) ? data : []
  44. }
  45. /** 集合根须带尾斜杠,避免 307 丢 Authorization */
  46. function root(path) {
  47. return path.endsWith('/') ? path : `${path}/`
  48. }
  49. // ── 仪表盘 ──
  50. export async function getDashboardStats() {
  51. return (await get('/dashboard/stats')) || {}
  52. }
  53. export async function getInboundByCargo() {
  54. return asList(await get('/dashboard/inbound/by-cargo'))
  55. }
  56. export async function getOutboundByCargo() {
  57. return asList(await get('/dashboard/outbound/by-cargo'))
  58. }
  59. // ── 计划 ──
  60. export async function listPlans(params = {}) {
  61. return asList(await get(root('/plans'), params))
  62. }
  63. export async function getPlan(id) {
  64. return get(`/plans/${String(id)}`)
  65. }
  66. export async function getPlanRelated(id) {
  67. return (await get(`/plans/${String(id)}/related`)) || { inventories: [], weighings: [] }
  68. }
  69. // ── 地磅单 ──
  70. export async function listWeighing(params = {}) {
  71. return asList(await get(root('/weighing'), params))
  72. }
  73. export async function getWeighing(id) {
  74. return get(`/weighing/${String(id)}`)
  75. }
  76. export async function getWeighingRelated(id) {
  77. return (await get(`/weighing/${String(id)}/related`)) || { plan: null, inventory: null }
  78. }
  79. export function updateWeighing(id, data) {
  80. return patch(`/weighing/${String(id)}`, data)
  81. }
  82. export function voidWeighing(id) {
  83. return post(`/weighing/${String(id)}/void`, {})
  84. }
  85. // ── 出入库 ──
  86. export async function listInventory(params = {}) {
  87. return asList(await get(root('/inventory'), params))
  88. }
  89. export async function getInventory(id) {
  90. return get(`/inventory/${String(id)}`)
  91. }
  92. export async function getInventoryRelated(id) {
  93. return (await get(`/inventory/${String(id)}/related`)) || { plan: null, weighing: null }
  94. }
  95. export function createInventoryLinkForPlan(purchasePlanId, remarks) {
  96. return post('/inventory/create-link-for-plan', {
  97. purchasePlanId: String(purchasePlanId),
  98. remarks
  99. })
  100. }