From 55123b6f8e8b48d1c2d86b9d512cca5fae60c98b Mon Sep 17 00:00:00 2001 From: yin <3313488199@qq.com> Date: Fri, 25 Sep 2026 18:00:10 +0800 Subject: [PATCH] 1 --- src/api/index/index.ts | 99 +++ src/api/stock/external-stock.ts | 137 ++++ src/api/user/user.ts | 12 +- .../entry-sections/entry-sections.vue | 76 +++ src/composables/useEntrySections.ts | 115 ++++ src/pages.json | 13 + src/pages/index/index.vue | 136 +--- src/pages/mine/mine.vue | 3 + src/pagesA/demo/public.vue | 8 +- src/pagesA/external-stock/list.vue | 615 ++++++++++++++++++ src/pagesA/test/index.vue | 8 +- src/router/entries.ts | 92 ++- src/router/pages.ts | 42 +- src/store/modules/permission.ts | 6 +- src/utils/gateway.ts | 21 + 我的页面-分类入口改造-分析与任务清单.md | 438 +++++++++++++ 16 files changed, 1658 insertions(+), 163 deletions(-) create mode 100644 src/api/index/index.ts create mode 100644 src/api/stock/external-stock.ts create mode 100644 src/components/entry-sections/entry-sections.vue create mode 100644 src/composables/useEntrySections.ts create mode 100644 src/pagesA/external-stock/list.vue create mode 100644 src/utils/gateway.ts create mode 100644 我的页面-分类入口改造-分析与任务清单.md diff --git a/src/api/index/index.ts b/src/api/index/index.ts new file mode 100644 index 0000000..c53f6ad --- /dev/null +++ b/src/api/index/index.ts @@ -0,0 +1,99 @@ +/** + * 通用下拉数据(后端 `index.*` 模块) + * + * 目前用于「外仓商品」页的**货主 / 仓库**筛选。等第二个页面也需要时,再考虑把它 + * 与具体业务解耦(现在放这里就够了,避免过早抽象)。 + * + * ⚠️ **响应结构不统一**(2026-09-25 实测 `index.loadStores`): + * + * ```json + * { "flag":"success", "code":200, "message":"ok", + * "stores":[{ "value":1000147, "label":"广州天豹云仓" }, ...], "timestamp":1790326225 } + * ``` + * + * 1. 业务字段在**响应顶层**(**没有 `data` 包裹**,与 `wechat.getMyLoginCode` 同类) + * 2. 列表键名是模块化的:货主 `stores`、仓库 `warehouses` + * 3. 项已经是 `{ value, label }`(后端归一化好了,直接用) + * + * 所以下面用 `pickRows()` 依次兼容:顶层模块键 → `list`/`items` → `data` 包裹 → 直接数组; + * 项则先认 `value`/`label`,没有才回退到各表自己的字段名。 + * 仓库接口的返回结构尚未实测,同样走这套容错。 + * + * 权限点:`index.loadStores`(wms_permission id=444);未授权时接口失败并由 request 层统一 toast, + * 页面只显示「全部」。 + */ + +import request from '@/utils/request' +import { SYSTEM_URL, gatewayParams } from '@/utils/gateway' + +/** 下拉选项:`value` 为后端 id;空字符串留给页面自己加的「全部」项 */ +export interface SelectOption { + value: string | number + label: string +} + +/** 从响应里取出列表数组(兼容顶层模块键 / list / items / data 包裹 / 直接数组) */ +function pickRows(payload: any, rowKeys: string[]): any[] { + if (Array.isArray(payload)) return payload + + for (const key of rowKeys) { + if (Array.isArray(payload?.[key])) return payload[key] + } + + // 兼容「data 包裹」的写法({ data: { stores: [...] } }) + const inner = payload?.data + if (inner && inner !== payload) return pickRows(inner, rowKeys) + + return [] +} + +/** 把列表项归一化成 `{ value, label }` */ +function toOptions(rows: any[], idKeys: string[], nameKeys: string[]): SelectOption[] { + return rows + .map((item: any) => { + const value = + item?.value ?? + idKeys.map((key) => item?.[key]).find((v) => v !== undefined && v !== null && v !== '') + const label = + [item?.label, ...nameKeys.map((key) => item?.[key])].find( + (v) => typeof v === 'string' && v.trim() + ) + return { value, label: label || (value !== undefined ? `#${value}` : '') } + }) + .filter( + (option: SelectOption) => + option.value !== undefined && option.value !== null && !!option.label + ) +} + +/** 货主列表(`index.loadStores`) */ +export async function fetchStoreOptions(): Promise { + const res = await request({ + url: SYSTEM_URL, + method: 'POST', + params: gatewayParams('index.loadStores'), + data: {}, + auth: true + }) + return toOptions( + pickRows(res, ['stores', 'list', 'items']), + ['store_id', 'id'], + ['store_name', 'name', 'company_name'] + ) +} + +/** 仓库列表(`index.loadWarehouses`) */ +export async function fetchWarehouseOptions(): Promise { + const res = await request({ + url: SYSTEM_URL, + method: 'POST', + params: gatewayParams('index.loadWarehouses'), + data: {}, + auth: true + }) + return toOptions( + pickRows(res, ['warehouses', 'list', 'items']), + ['warehouse_id', 'id'], + ['warehouse_name', 'warehouse_remask', 'name'] + ) +} diff --git a/src/api/stock/external-stock.ts b/src/api/stock/external-stock.ts new file mode 100644 index 0000000..59617db --- /dev/null +++ b/src/api/stock/external-stock.ts @@ -0,0 +1,137 @@ +/** + * 外仓商品列表(`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 { + const { page = 1, pageSize = EXTERNAL_STOCK_PAGE_SIZE, keyword, store_id, warehouse_id } = query + + const where: Record = {} + 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> { + return request({ + 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) +} diff --git a/src/api/user/user.ts b/src/api/user/user.ts index 9ba1cc0..10e14fd 100644 --- a/src/api/user/user.ts +++ b/src/api/user/user.ts @@ -7,17 +7,7 @@ import request from '@/utils/request' import type { BizResponse } from '@/utils/request' - -/** /system 网关统一入口 */ -const SYSTEM_URL = '/system' - -/** - * 构造 /system 网关的公共 query 参数 - * 与 new3 完全一致:method + v + timestamp - */ -function gatewayParams(method: string): Record { - return { method, v: '1.0', timestamp: Date.now() } -} +import { SYSTEM_URL, gatewayParams } from '@/utils/gateway' // ────────────────────────────── 类型 ────────────────────────────── diff --git a/src/components/entry-sections/entry-sections.vue b/src/components/entry-sections/entry-sections.vue new file mode 100644 index 0000000..a111864 --- /dev/null +++ b/src/components/entry-sections/entry-sections.vue @@ -0,0 +1,76 @@ + + + + + diff --git a/src/composables/useEntrySections.ts b/src/composables/useEntrySections.ts new file mode 100644 index 0000000..38e0a6f --- /dev/null +++ b/src/composables/useEntrySections.ts @@ -0,0 +1,115 @@ +/** + * 首页入口区渲染状态 + * + * 首页的入口区 = **本地「常用功能」列表**(`LOCAL_COMMON_ENTRIES`,前端写死)+ 后端菜单段 + * (`user.info` 的 menus × `PAGE_REGISTRY`),两段形态一致、并排展示,合成逻辑在 `toHomeSections()`。 + * + * 这个 composable 负责把「取数 / 加载态 / 空态文案 / 点击跳转」收口在一处, + * 页面只负责 `onShow` 调一次 `loadEntries()`,然后把 `sections` 交给模板。 + * + * 登录态差异:未登录时后端菜单为空 ⇒ 只剩下「常用功能」段(公开页仍可浏览,满足微信审核要求)。 + */ + +import { computed, ref } from 'vue' +import type { ComputedRef, Ref } from 'vue' +import { useUserStore } from '@/store/modules/user' +import { usePermissionStore } from '@/store/modules/permission' +import { usePageAuth } from '@/composables/usePageAuth' +import { toHomeSections } from '@/router/entries' +import type { EntryLeaf, EntrySection } from '@/router/entries' +import type { AuthMode } from '@/router/pages' +import { navigateToPath } from '@/utils/navigate' + +export interface UseEntrySectionsOptions { + /** 无分组叶子的兜底段标题(默认「其他」) */ + looseTitle?: string +} + +export interface UseEntrySectionsReturn { + /** 是否已登录 */ + isLogin: ComputedRef + /** 页面直接 `v-for` 渲染的分段(段标题 + 九宫格) */ + sections: ComputedRef + /** 是否正在拉取 `user.info` */ + loading: Ref + /** + * 拉取用户信息(= 菜单 + 权限点) + * @param force 是否强制刷新(「重试」按钮用;`onShow` 不传,避免切 Tab 就重拉) + */ + loadEntries: (force?: boolean) => Promise + /** 点击入口:需登录而未登录 → 页内引导;否则按 tabBar 与否跳转 */ + onEntry: (leaf: EntryLeaf) => void + /** 唤起登录(**就地弹抽屉**,不跳页) */ + goLogin: () => void + /** 去「我的」页(绑定账号 / 查看全部功能) */ + goMine: () => void + /** `empty-state` 组件的属性(未绑定账号时会覆盖默认文案) */ + emptyStateProps: ComputedRef> +} + +/** + * @param auth 本页在 `PAGE_REGISTRY` 里声明的登录策略(首页为 `'public'`) + */ +export function useEntrySections( + auth: AuthMode, + options: UseEntrySectionsOptions = {} +): UseEntrySectionsReturn { + const { looseTitle } = options + + const userStore = useUserStore() + const permissionStore = usePermissionStore() + const { isLogin, goLogin, requireLogin } = usePageAuth(auth) + + const loading = ref(false) + + /** 本地「常用功能」段 + 后端菜单段(未登录时后端段为空,只剩下常用功能) */ + const sections = computed(() => toHomeSections(permissionStore.entries, { looseTitle })) + + const emptyStateProps = computed>(() => { + if (userStore.isWechatUnbound) { + return { text: '微信已登录,绑定 WMS 账号后即可使用各功能', icon: 'account' } + } + // 返回空对象不覆盖 empty-state 的默认值(默认文案 = 服务器出差了,请稍后再试) + return {} + }) + + const loadEntries = async (force = false): Promise => { + if (!userStore.isLogin) return + // `user.info` 已由 App 启动统一拉取并存在 store;这里只在没有数据时补拉或显式重试 + if (userStore.hasUserInfo && !force) return + // 未绑定的微信登录态没有菜单可拉(绑定后 user.info 会重新灌菜单) + if (userStore.isWechatUnbound && !force) return + + loading.value = true + try { + await userStore.getInfo(force) + } catch { + // 失败 → sections 为空 → 走空态文案 + 「重试」(提示由 request 层统一 toast) + } finally { + loading.value = false + } + } + + const onEntry = (leaf: EntryLeaf): void => { + if (leaf.auth === 'required' && !isLogin.value) { + requireLogin(`「${leaf.title}」需要登录后使用`) + return + } + navigateToPath(leaf.path, leaf.title) + } + + const goMine = (): void => { + uni.switchTab({ url: '/pages/mine/mine' }) + } + + return { + isLogin, + sections, + loading, + loadEntries, + onEntry, + goLogin, + goMine, + emptyStateProps + } +} diff --git a/src/pages.json b/src/pages.json index 0361cd6..3917cca 100644 --- a/src/pages.json +++ b/src/pages.json @@ -60,6 +60,15 @@ "style": { "navigationBarTitleText": "测试页" } + }, + { + "path": "external-stock/list", + "style": { + "navigationBarTitleText": "外仓商品", + "enablePullDownRefresh": true, + "backgroundTextStyle": "dark", + "onReachBottomDistance": 80 + } } ] } @@ -110,6 +119,10 @@ { "name": "测试页", "path": "pagesA/test/index" + }, + { + "name": "外仓商品", + "path": "pagesA/external-stock/list" } ] }, diff --git a/src/pages/index/index.vue b/src/pages/index/index.vue index 3a79277..6cc5eb9 100644 --- a/src/pages/index/index.vue +++ b/src/pages/index/index.vue @@ -6,34 +6,21 @@ {{ heroDesc }} - + - 常用功能 - - - - - {{ section.title }} - - - - {{ item.title }} - - - - + + - + @@ -76,32 +63,26 @@ import { computed, ref } from 'vue' import { onShow } from '@dcloudio/uni-app' import { useUserStore } from '@/store/modules/user' -import { usePermissionStore } from '@/store/modules/permission' -import { usePageAuth } from '@/composables/usePageAuth' +import { useEntrySections } from '@/composables/useEntrySections' import { useEntryScene } from '@/composables/useEntryScene' import AuthModal from '@/components/auth-modal/auth-modal.vue' -import { LOCAL_PUBLIC_ENTRIES, toSections } from '@/router/entries' -import type { EntryLeaf } from '@/router/entries' -import { navigateToPath } from '@/utils/navigate' +import EntrySections from '@/components/entry-sections/entry-sections.vue' const version = ref(uni.$u.config.v) const platform = ref(uni.getSystemInfoSync().uniPlatform) const userStore = useUserStore() -const permissionStore = usePermissionStore() -// 本页是 public:未登录也能正常浏览 -const { isLogin, goLogin, requireLogin } = usePageAuth('public') - -const loading = ref(false) /** - * 入口来源(见《页面渲染框架》§3.5): - * - 未登录 → 本地公共入口清单(确定性,不依赖后端是否配置) - * - 已登录 → 后端菜单 × PAGE_REGISTRY 的可见入口树 + * 入口区状态(与后端菜单解耦,见 `useEntrySections`) + * + * - 「常用功能」段 = **本地列表**(`PageMeta.common === true` 的页面:公开示例 + 测试页) + * → 不受登录态 / 授权 / 接口故障影响,开发联调随时能进测试页 + * - 其后是后端菜单段(`user.info` 的 menus × `PAGE_REGISTRY`,如「外仓商品」分类) + * - 未登录时后端段为空,只剩下「常用功能」(公开页仍可浏览) */ -const sections = computed(() => - toSections(isLogin.value ? permissionStore.entries : LOCAL_PUBLIC_ENTRIES) -) +const { isLogin, sections, loading, loadEntries, onEntry, goLogin, goMine, emptyStateProps } = + useEntrySections('public') const heroDesc = computed(() => { if (!isLogin.value) return 'WMS 仓储管理 · 微信小程序端' @@ -115,52 +96,7 @@ const heroDesc = computed(() => { return parts.length ? parts.join(' · ') : 'WMS 仓储管理 · 微信小程序端' }) -/** - * 空态文案 / 图标:未绑定与接口失败必须区分 - * - * 返回空对象时不覆盖 `empty-state` 的默认值(默认是「服务器出差了,请稍后再试」), - * 否则未绑定用户会误以为是网络故障。 - */ -const emptyStateProps = computed(() => - userStore.isWechatUnbound - ? { text: '微信已登录,绑定 WMS 账号后即可使用各功能', icon: 'account' } - : {} -) - -/** 引导去「我的」页绑定(那里是常驻的绑定入口;登录页因已有 token 会直接放行) */ -const goBind = () => { - uni.switchTab({ url: '/pages/mine/mine' }) -} - -/** - * 拉取用户信息(= 菜单 + 权限点,失败提示由 request 层统一 toast) - * @param force 是否强制刷新(“重试”按钮用;onShow 不传,避免切 Tab 就重拉) - */ -const loadEntries = async (force = false) => { - if (!userStore.isLogin) return - // `user.info` 已由 App 启动统一拉取并存在 store;这里只在没有数据时补拉或显式重试 - if (userStore.hasUserInfo && !force) return - // 未绑定的微信登录态没有菜单可拉(绑定后 user.info 会重新灌菜单) - if (userStore.isWechatUnbound && !force) return - - loading.value = true - try { - await userStore.getInfo(force) - } catch { - // 失败 → sections 为空 → 走空态文案 + 「重试」 - } finally { - loading.value = false - } -} - -/** 点击入口:需登录而未登录 → 页内引导(不自动跳转);否则按 tabBar 与否跳转 */ -const onEntry = (item: EntryLeaf) => { - if (item.auth === 'required' && !isLogin.value) { - requireLogin(`「${item.title}」需要登录后使用`) - return - } - navigateToPath(item.path, item.title) -} +/* 空态文案、取数(loadEntries)、点击跳转(onEntry)均收口在 useEntrySections */ // onShow:tabBar 页切换只触发 onShow、不触发 onLoad; // 已加载过会直接返回,所以放在这里既能在冷启动补拉,也不会重复请求 @@ -210,17 +146,7 @@ useEntryScene() } } -.sections { - padding-bottom: 8rpx; -} - -.section { - &__title { - padding: 16rpx 32rpx 0; - font-size: 26rpx; - color: #909399; - } -} +/* 段标题 + 九宫格的样式在 components/entry-sections(首页与「我的」共用,勿在此重复定义) */ .loading { display: flex; @@ -228,24 +154,4 @@ useEntryScene() justify-content: center; padding: 56rpx 0; } - -.grid { - display: flex; - flex-wrap: wrap; - padding: 8rpx 0 24rpx; - - &__item { - width: 33.333%; - display: flex; - flex-direction: column; - align-items: center; - padding: 24rpx 0; - } - - &__item-text { - margin-top: 12rpx; - font-size: 26rpx; - color: #606266; - } -} diff --git a/src/pages/mine/mine.vue b/src/pages/mine/mine.vue index 6f19a6f..c0b9170 100644 --- a/src/pages/mine/mine.vue +++ b/src/pages/mine/mine.vue @@ -45,6 +45,9 @@ * - 头部用户信息区 = **唯一的登录入口**(未登录点击弹抽屉;已登录进「用户信息」页) * - 菜单里的细项(身份信息 / 待绑定 / 微信资料 / 电脑端登录码 / **退出登录**)全部移到 * 二级页 `pages/mine/user-info`,本页不再堆卡片 + * + * ⚠️ 功能入口(九宫格)统一放在**首页** = 本地「常用功能」列表 + 后端菜单分类段; + * 本页不再渲染页面列表(2026-09-25 调整)。 */ import { computed } from 'vue' import { onShow } from '@dcloudio/uni-app' diff --git a/src/pagesA/demo/public.vue b/src/pagesA/demo/public.vue index ebdd08d..126d45c 100644 --- a/src/pagesA/demo/public.vue +++ b/src/pagesA/demo/public.vue @@ -10,13 +10,13 @@ 这页验证什么 - 它在 PAGE_REGISTRY 里声明为 auth: 'public' 且未设 hidden,因此会 - 自动出现在未登录时的首页九宫格 - 里(LOCAL_PUBLIC_ENTRIES)。 + 它在 PAGE_REGISTRY 里声明为 auth: 'public' 且 + common: true,因此会出现在首页 + 「常用功能」段里(LOCAL_COMMON_ENTRIES),**未登录也能看到**。 这正是「未登录也必须有可浏览内容」这条微信审核要求的落点: - 公共入口由前端本地清单决定,不依赖后端菜单是否配置。 + 「常用功能」由前端本地清单决定,不依赖后端菜单是否配置。 diff --git a/src/pagesA/external-stock/list.vue b/src/pagesA/external-stock/list.vue new file mode 100644 index 0000000..fa853bc --- /dev/null +++ b/src/pagesA/external-stock/list.vue @@ -0,0 +1,615 @@ + + + + + diff --git a/src/pagesA/test/index.vue b/src/pagesA/test/index.vue index aee4999..5b70101 100644 --- a/src/pagesA/test/index.vue +++ b/src/pagesA/test/index.vue @@ -10,11 +10,7 @@ - + @@ -42,7 +38,7 @@ 后端没有下发任何菜单 → 说明 platform=miniProgram 时 menus 未取自 - miniprogram_pages,或当前账号的角色未授权 627。 + miniprogram_pages,或当前账号的角色未授权 628。