138 lines
5.0 KiB
TypeScript
138 lines
5.0 KiB
TypeScript
/**
|
||
* 外仓商品列表(`stock.warehouse.externalList`)
|
||
*
|
||
* 与 new3 Web 端 `src/views/stock/external_stock/index.vue` **同一个接口、同一套参数结构**:
|
||
*
|
||
* ```
|
||
* body = {
|
||
* warehouse_id?: number, // 顶层(new3 已验证可用)
|
||
* pagination: { page, pageSize },
|
||
* where: { keyword?, store_id? }, // 通用过滤条件
|
||
* sort?: { sortField, sortOrder } // 小程序端不用(见下)
|
||
* }
|
||
* 返回 data = { list: [...], pagination: { total }, summary?: {...} }
|
||
* ```
|
||
*
|
||
* 小程序端只取 3 个字段展示(商品图片 / 商品名称 / 可用库存),并且**去掉**了排序、
|
||
* 每页条数选择、页码跳转等不适合移动端的交互 —— 分页改为「上拉加载」,
|
||
* 见 `pagesA/external-stock/list.vue`。
|
||
*
|
||
* ⚠️ **`store_id` 的位置尚未与后端确认**:new3 只用了 `where.keyword` 与顶层
|
||
* `warehouse_id`,没有货主过滤。小程序端按「通用过滤条件」的语义放进 `where.store_id`。
|
||
* 联调时若货主筛选不生效,**只改 `buildBody()` 一处**(例如提到顶层与 `warehouse_id` 并列)。
|
||
*/
|
||
|
||
import request from '@/utils/request'
|
||
import type { BizResponse } from '@/utils/request'
|
||
import { SYSTEM_URL, gatewayParams } from '@/utils/gateway'
|
||
|
||
/** 每页条数:移动端不做「每页条数」选择器,固定即可(与 new3 默认值一致) */
|
||
export const EXTERNAL_STOCK_PAGE_SIZE = 20
|
||
|
||
/** 列表项(只声明小程序用到的字段,其余用索引签名兜住) */
|
||
export interface ExternalStockItem {
|
||
id?: number
|
||
/** 商品编码 */
|
||
item_code?: string
|
||
/** 商品名称 */
|
||
item_name?: string
|
||
/** 图片地址数组(列表只取第一张作封面) */
|
||
images?: string[] | string | null
|
||
/** 可用库存:优先后端算好的这个字段 */
|
||
available_quantity?: number | null
|
||
/** 库存数量(`available_quantity` 缺失时用它减锁定库存兜底) */
|
||
quantity?: number | null
|
||
/** 订单锁定库存 */
|
||
locked_quantity?: number | null
|
||
/** 库存单位 */
|
||
stock_unit?: string
|
||
[key: string]: any
|
||
}
|
||
|
||
/** 列表响应体 `data` */
|
||
export interface ExternalStockPayload {
|
||
list?: ExternalStockItem[]
|
||
total?: number
|
||
pagination?: { page?: number; pageSize?: number; total?: number }
|
||
[key: string]: any
|
||
}
|
||
|
||
/** 查询条件(页面传入) */
|
||
export interface ExternalStockQuery {
|
||
page?: number
|
||
pageSize?: number
|
||
/** 关键词(商品名称 / 编码),空字符串表示不过滤 */
|
||
keyword?: string
|
||
/** 货主 id,空字符串表示全部 */
|
||
store_id?: number | string
|
||
/** 仓库 id,空字符串表示全部 */
|
||
warehouse_id?: number | string
|
||
}
|
||
|
||
function isFilled(value: unknown): boolean {
|
||
return value !== undefined && value !== null && value !== ''
|
||
}
|
||
|
||
/** 组装请求体(⚠️ 货主过滤位置要调整时改这里) */
|
||
function buildBody(query: ExternalStockQuery): Record<string, any> {
|
||
const { page = 1, pageSize = EXTERNAL_STOCK_PAGE_SIZE, keyword, store_id, warehouse_id } = query
|
||
|
||
const where: Record<string, any> = {}
|
||
if (keyword && String(keyword).trim()) where.keyword = String(keyword).trim()
|
||
if (isFilled(store_id)) where.store_id = store_id
|
||
|
||
return {
|
||
...(isFilled(warehouse_id) ? { warehouse_id } : {}),
|
||
pagination: { page, pageSize },
|
||
where
|
||
}
|
||
}
|
||
|
||
/** 外仓商品列表(分页) */
|
||
export function externalStockList(
|
||
query: ExternalStockQuery = {}
|
||
): Promise<BizResponse<ExternalStockPayload>> {
|
||
return request<ExternalStockPayload>({
|
||
url: SYSTEM_URL,
|
||
method: 'POST',
|
||
params: gatewayParams('stock.warehouse.externalList'),
|
||
data: buildBody(query),
|
||
// 需要登录:未登录时不发请求,直接弹登录抽屉
|
||
auth: true
|
||
})
|
||
}
|
||
|
||
/** 总数:后端放在 `pagination.total`(也可能在顶层 `total`) */
|
||
export function readTotal(payload: ExternalStockPayload | undefined | null): number {
|
||
const total = payload?.pagination?.total ?? payload?.total ?? 0
|
||
const num = Number(total)
|
||
return Number.isFinite(num) && num > 0 ? num : 0
|
||
}
|
||
|
||
/** 列表项的封面图(`images` 可能是数组、字符串或空) */
|
||
export function readCover(item: ExternalStockItem | undefined | null): string {
|
||
const images = item?.images
|
||
if (Array.isArray(images)) return images.find((url) => !!url) || ''
|
||
if (typeof images === 'string') return images
|
||
return ''
|
||
}
|
||
|
||
/**
|
||
* 可用库存
|
||
*
|
||
* 优先后端算好的 `available_quantity`;缺失时用 `quantity - locked_quantity` 兜底
|
||
* (两者是同一张表 `wms_stock_warehouse` 上的字段)。
|
||
* 返回 `null` 表示后端没给数据,页面显示 `—`(**不要显示 0**,那会被误读成「没库存」)。
|
||
*/
|
||
export function readAvailableQuantity(item: ExternalStockItem | undefined | null): number | null {
|
||
if (isFilled(item?.available_quantity)) {
|
||
const value = Number(item?.available_quantity)
|
||
if (Number.isFinite(value)) return value
|
||
}
|
||
|
||
const quantity = Number(item?.quantity)
|
||
if (!Number.isFinite(quantity)) return null
|
||
const locked = Number(item?.locked_quantity)
|
||
return quantity - (Number.isFinite(locked) ? locked : 0)
|
||
}
|