audit.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <template>
  2. <view class="page">
  3. <NavBar title="库存盘点" showBack />
  4. <!-- 设备信息 -->
  5. <view class="device-bar">
  6. <text class="device-bar-text">{{ deviceId }}<text v-if="deviceName"> · {{ deviceName }}</text></text>
  7. </view>
  8. <!-- 搜索栏 -->
  9. <view class="search-bar">
  10. <view class="search-icon"></view>
  11. <input class="search-input" v-model="searchKeyword" placeholder="搜索商品名称或编码" confirm-type="search" />
  12. <view class="search-clear" v-if="searchKeyword" @click="searchKeyword = ''"><text>✕</text></view>
  13. </view>
  14. <!-- 加载中 -->
  15. <view class="empty-state" v-if="loading">
  16. <text class="empty-text">加载中...</text>
  17. </view>
  18. <!-- 空状态 -->
  19. <view class="empty-state" v-else-if="auditItems.length === 0">
  20. <view class="empty-icon-box"><view class="empty-doc-icon"></view></view>
  21. <text class="empty-text">暂无商品数据</text>
  22. </view>
  23. <!-- 搜索无结果 -->
  24. <view class="empty-state" v-else-if="currentFloors.length === 0">
  25. <text class="empty-text">未找到匹配商品</text>
  26. </view>
  27. <!-- 楼层列表 -->
  28. <view v-else class="floors-scroll">
  29. <view v-for="floor in currentFloors" :key="'floor-' + floor.floor" class="floor-card">
  30. <!-- 楼层头部(单层柜模版不显示层号) -->
  31. <view class="floor-header" v-if="currentFloors.length > 1">
  32. <view class="floor-badge">{{ floor.floor }}</view>
  33. <text class="floor-title">{{ floor.name }}</text>
  34. <text class="floor-count">{{ floor.entries.length }}件商品</text>
  35. </view>
  36. <view class="floor-header floor-header-single" v-else>
  37. <text class="floor-title">商品清单</text>
  38. <text class="floor-count">{{ floor.entries.length }}件商品</text>
  39. </view>
  40. <!-- 商品网格(3列) -->
  41. <view class="goods-grid">
  42. <view v-for="entry in floor.entries" :key="entry.idx" class="goods-cell">
  43. <view class="goods-cell-inner">
  44. <!-- 商品图片 -->
  45. <view class="cell-image-wrap">
  46. <image
  47. v-if="entry.item.productImage"
  48. :src="entry.item.productImage"
  49. class="cell-image"
  50. mode="aspectFill"
  51. />
  52. <view v-else class="cell-image-placeholder">
  53. <text>无图</text>
  54. </view>
  55. </view>
  56. <!-- 商品名称 -->
  57. <text class="cell-name">{{ entry.item.productName || '未知' }}</text>
  58. <!-- 状态标签 -->
  59. <view class="cell-badge" :class="getDiffClass(entry.item, entry.idx)">{{ getDiffLabel(entry.item, entry.idx) }}</view>
  60. <!-- 库存信息 -->
  61. <view class="cell-stock-row">
  62. <text class="cell-stock-label">标{{ entry.item.standardStock }}</text>
  63. <text class="cell-stock-cur">当{{ auditItems[entry.idx]?.newStock ?? 0 }}</text>
  64. </view>
  65. <!-- 数量调节 -->
  66. <view class="cell-qty">
  67. <view class="cell-qty-btn" @click="decrease(entry.idx)"><text>-</text></view>
  68. <input class="cell-qty-input" type="number" v-model.number="auditItems[entry.idx].newStock" />
  69. <view class="cell-qty-btn" @click="increase(entry.idx)"><text>+</text></view>
  70. </view>
  71. <!-- 快捷操作 -->
  72. <view class="cell-actions">
  73. <text class="cell-action-btn" @click="setToStandard(entry.idx)">设标</text>
  74. <text class="cell-action-divider">|</text>
  75. <text class="cell-action-btn cell-action-reset" @click="setToCurrent(entry.idx)">重置</text>
  76. </view>
  77. </view>
  78. </view>
  79. </view>
  80. </view>
  81. <view class="scroll-bottom-spacer"></view>
  82. </view>
  83. <!-- 底部操作栏 -->
  84. <view class="bottom-bar" v-if="auditItems.length > 0">
  85. <text class="total-label">修正 <text class="total-num">{{ changedCount }}</text> 项</text>
  86. <view class="submit-btn" :class="{ disabled: submitting || changedCount === 0 }" @click="handleSubmit">
  87. <view class="btn-spinner" v-if="submitting"></view>
  88. <text>{{ submitting ? '提交中' : '提交盘点' }}</text>
  89. </view>
  90. </view>
  91. </view>
  92. </template>
  93. <script setup lang="ts">
  94. import { ref, computed, onMounted } from 'vue';
  95. import NavBar from '@/components/NavBar.vue';
  96. import { getDeviceInventory } from '@/api/replenish';
  97. import { adjustStock } from '@/api/inventory';
  98. // ── 类型 ──
  99. interface AuditEntry {
  100. productId: number | null;
  101. productCode: string;
  102. productName: string;
  103. productImage: string;
  104. standardStock: number;
  105. newStock: number;
  106. originalStock: number;
  107. floorNumber: number;
  108. floorName: string;
  109. }
  110. interface FloorGroup {
  111. floor: number;
  112. name: string;
  113. entries: { item: AuditEntry; idx: number }[];
  114. }
  115. // ── 状态 ──
  116. const deviceId = ref('');
  117. const deviceName = ref('');
  118. const loading = ref(true);
  119. const auditItems = ref<AuditEntry[]>([]);
  120. const submitting = ref(false);
  121. const searchKeyword = ref('');
  122. // ── 搜索过滤 ──
  123. const filteredItems = computed(() => {
  124. const kw = searchKeyword.value.trim().toLowerCase();
  125. return auditItems.value
  126. .map((item, idx) => ({ item, idx }))
  127. .filter(({ item }) => {
  128. if (!kw) return true;
  129. const name = (item.productName || '').toLowerCase();
  130. const code = (item.productCode || '').toLowerCase();
  131. return name.includes(kw) || code.includes(kw);
  132. });
  133. });
  134. // ── 按楼层分组 ──
  135. const currentFloors = computed(() => {
  136. const map = new Map<number, { item: AuditEntry; idx: number }[]>();
  137. for (const { item, idx } of filteredItems.value) {
  138. const fn = item.floorNumber;
  139. if (!map.has(fn)) map.set(fn, []);
  140. map.get(fn)!.push({ item, idx });
  141. }
  142. return Array.from(map.entries())
  143. .sort((a, b) => a[0] - b[0])
  144. .map(([floor, entries]) => ({
  145. floor,
  146. name: entries[0]?.item.floorName || ('第' + floor + '层'),
  147. entries,
  148. }));
  149. });
  150. // ── 变更计数 ──
  151. const changedCount = computed(() =>
  152. auditItems.value.filter(i => i.productId != null && i.newStock !== i.originalStock).length
  153. );
  154. // ── 数量操作 ──
  155. function decrease(index: number) {
  156. const it = auditItems.value[index];
  157. if (it && it.newStock > 0) it.newStock--;
  158. }
  159. function increase(index: number) {
  160. const it = auditItems.value[index];
  161. if (it && it.newStock < 9999) it.newStock++;
  162. }
  163. function setToStandard(index: number) {
  164. const it = auditItems.value[index];
  165. if (it) it.newStock = it.standardStock;
  166. }
  167. function setToCurrent(index: number) {
  168. const it = auditItems.value[index];
  169. if (it) it.newStock = it.originalStock;
  170. }
  171. // ── 状态标签 ──
  172. function getDiffLabel(item: AuditEntry, index: number) {
  173. const cur = auditItems.value[index]?.newStock ?? item.originalStock;
  174. if (cur < item.standardStock) return '偏少';
  175. if (cur > item.standardStock) return '偏多';
  176. return '正常';
  177. }
  178. function getDiffClass(item: AuditEntry, index: number) {
  179. const cur = auditItems.value[index]?.newStock ?? item.originalStock;
  180. if (cur < item.standardStock) return 'warning';
  181. if (cur > item.standardStock) return 'danger';
  182. return 'normal';
  183. }
  184. // ── 提交 ──
  185. async function handleSubmit() {
  186. if (submitting.value || changedCount.value === 0) return;
  187. const changes = auditItems.value.filter(i => i.productId != null && i.newStock !== i.originalStock);
  188. if (!changes.length) { uni.showToast({ title: '无变更', icon: 'none' }); return; }
  189. submitting.value = true;
  190. let success = 0;
  191. let fail = 0;
  192. for (const item of changes) {
  193. try {
  194. await adjustStock({
  195. deviceId: deviceId.value,
  196. productId: item.productId!,
  197. newStock: item.newStock,
  198. remark: '小程序盘点修正',
  199. });
  200. success++;
  201. } catch {
  202. fail++;
  203. }
  204. }
  205. if (fail === 0) {
  206. uni.showToast({ title: `盘点完成,修正${success}项`, icon: 'success' });
  207. setTimeout(() => uni.navigateBack(), 800);
  208. } else {
  209. uni.showToast({ title: `成功${success}项,失败${fail}项`, icon: 'none' });
  210. }
  211. submitting.value = false;
  212. }
  213. // ── 初始化:单个 API 调用,后端已合并楼层+库存 ──
  214. onMounted(async () => {
  215. const pages = getCurrentPages();
  216. const opts = (pages[pages.length - 1] as any)?.$page?.options || (pages[pages.length - 1] as any)?.options || {};
  217. deviceId.value = opts.deviceId || '';
  218. if (!deviceId.value) {
  219. uni.showToast({ title: '缺少设备ID', icon: 'none' });
  220. setTimeout(() => uni.navigateBack(), 500);
  221. return;
  222. }
  223. try {
  224. const data = await getDeviceInventory(deviceId.value);
  225. deviceName.value = data?.name || '';
  226. // 后端已在 floors 中合并了模版楼层和库存数据
  227. const floors: any[] = data?.floors || [];
  228. const items: AuditEntry[] = [];
  229. for (const floor of floors) {
  230. const floorNum = floor.floorNumber || 1;
  231. const floorName = floor.floorName || ('第' + floorNum + '层');
  232. for (const slot of (floor.slots || [])) {
  233. const code = (slot.productCode || '').toString().trim();
  234. if (!code) continue;
  235. const curStock = slot.currentStock ?? 0;
  236. items.push({
  237. productId: slot.inventoryProductId ?? null,
  238. productCode: code,
  239. productName: slot.productName || code,
  240. productImage: slot.productImage || '',
  241. standardStock: slot.standardStock ?? 0,
  242. newStock: curStock,
  243. originalStock: curStock,
  244. floorNumber: floorNum,
  245. floorName,
  246. });
  247. }
  248. }
  249. auditItems.value = items;
  250. } catch (e: any) {
  251. uni.showToast({ title: e.message || '加载失败', icon: 'none' });
  252. } finally {
  253. loading.value = false;
  254. }
  255. });
  256. </script>
  257. <style lang="scss" scoped>
  258. .page { height: 100vh; background: $bg-color-page; display: flex; flex-direction: column; }
  259. /* ── 设备信息 ── */
  260. .device-bar { margin: 12rpx 24rpx; padding: 16rpx 20rpx; background: $bg-color-card; border-radius: $radius-md; box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.04); }
  261. .device-bar-text { font-size: $font-size-sm; font-weight: 600; color: $text-color-primary; }
  262. /* ── 搜索栏 ── */
  263. .search-bar { display: flex; align-items: center; margin: 0 24rpx 10rpx; padding: 0 16rpx; height: 60rpx; background: $bg-color-card; border: 1rpx solid $border-color; border-radius: $radius-base; }
  264. .search-icon { width: 28rpx; height: 28rpx; border: 2.5rpx solid $text-color-muted; border-radius: 50%; position: relative; flex-shrink: 0; margin-right: 10rpx;
  265. &::after { content: ''; position: absolute; bottom: -2rpx; right: -4rpx; width: 2.5rpx; height: 10rpx; background: $text-color-muted; border-radius: 1rpx; transform: rotate(-45deg); }
  266. }
  267. .search-input { flex: 1; font-size: $font-size-sm; color: $text-color-primary; height: 100%; }
  268. .search-clear { width: 36rpx; height: 36rpx; display: flex; align-items: center; justify-content: center; border-radius: 50%; background: $bg-color-secondary; flex-shrink: 0; margin-left: 8rpx;
  269. text { font-size: 20rpx; color: $text-color-muted; line-height: 1; }
  270. }
  271. /* ── 空状态 ── */
  272. .empty-state { display: flex; flex-direction: column; align-items: center; padding: 80rpx 0; }
  273. .empty-icon-box { width: 96rpx; height: 96rpx; background: $bg-color-secondary; border-radius: $radius-lg; display: flex; align-items: center; justify-content: center; margin-bottom: $spacing-2; }
  274. .empty-doc-icon { width: 40rpx; height: 48rpx; border: 2.5rpx solid $text-color-placeholder; border-radius: 4rpx; position: relative;
  275. &::after { content: ''; position: absolute; top: 14rpx; left: 8rpx; right: 8rpx; height: 2.5rpx; background: $text-color-placeholder; border-radius: 1rpx; box-shadow: 0 8rpx 0 $text-color-placeholder, 0 16rpx 0 $text-color-placeholder; }
  276. }
  277. .empty-text { font-size: $font-size-base; color: $text-color-muted; }
  278. /* ── 楼层列表滚动区 ── */
  279. .floors-scroll { flex: 1; height: 0; overflow-y: auto; padding: 0 24rpx; }
  280. /* ── 楼层卡片 ── */
  281. .floor-card { background: $bg-color-card; border-radius: $radius-md; margin-bottom: 12rpx; overflow: hidden; box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.04); }
  282. .floor-header { display: flex; align-items: center; padding: 18rpx 20rpx; border-bottom: 1rpx solid $border-color-light; }
  283. .floor-header-single { border-bottom: none; }
  284. .floor-badge { width: 40rpx; height: 40rpx; line-height: 40rpx; text-align: center; background: $primary-color; color: $text-color-primary; border-radius: $radius-sm; font-size: 22rpx; font-weight: 600; margin-right: 10rpx; }
  285. .floor-title { font-size: $font-size-base; font-weight: 600; color: $text-color-primary; flex: 1; }
  286. .floor-count { font-size: $font-size-sm; color: $text-color-muted; }
  287. /* ── 商品网格(3列) ── */
  288. .goods-grid { display: flex; flex-wrap: wrap; padding: 8rpx; }
  289. /* ── 商品单元格 ── */
  290. .goods-cell { width: 33.33%; padding: 6rpx; box-sizing: border-box; }
  291. .goods-cell-inner { display: flex; flex-direction: column; align-items: center; padding: 12rpx 8rpx; background: $bg-color-page; border-radius: $radius-sm; width: 100%; box-sizing: border-box; }
  292. .cell-image-wrap { width: 120rpx; height: 120rpx; border-radius: $radius-sm; overflow: hidden; background: $bg-color-tertiary; margin-bottom: 8rpx; flex-shrink: 0; }
  293. .cell-image { width: 100%; height: 100%; }
  294. .cell-image-placeholder { width: 100%; height: 100%; display: flex; align-items: center; justify-content: center;
  295. text { font-size: 20rpx; color: $text-color-muted; }
  296. }
  297. .cell-name { font-size: 22rpx; font-weight: 500; color: $text-color-primary; width: 100%; text-align: center; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-bottom: 4rpx; }
  298. .cell-badge { padding: 2rpx 8rpx; border-radius: 6rpx; font-size: 18rpx; font-weight: 500; margin-bottom: 6rpx;
  299. &.normal { background: $success-color-bg; color: $success-color; }
  300. &.warning { background: $warning-color-bg; color: $warning-color; }
  301. &.danger { background: $error-color-bg; color: $error-color; }
  302. }
  303. .cell-stock-row { display: flex; gap: 10rpx; margin-bottom: 6rpx; }
  304. .cell-stock-label { font-size: 20rpx; color: $text-color-muted; }
  305. .cell-stock-cur { font-size: 20rpx; font-weight: 500; color: $text-color-primary; }
  306. /* 数量调节 */
  307. .cell-qty { display: flex; align-items: center; gap: 0; border: 1rpx solid $border-color; border-radius: 8rpx; overflow: hidden; margin-bottom: 8rpx; }
  308. .cell-qty-btn { width: 52rpx; height: 52rpx; display: flex; align-items: center; justify-content: center; background: $bg-color-card;
  309. text { font-size: 32rpx; color: $text-color-secondary; line-height: 1; }
  310. &:active { background: $bg-color-secondary; }
  311. }
  312. .cell-qty-input { width: 64rpx; height: 52rpx; text-align: center; font-size: 26rpx; font-weight: 600; color: $text-color-primary; border-left: 1rpx solid $border-color; border-right: 1rpx solid $border-color; }
  313. /* 快捷操作 */
  314. .cell-actions { display: flex; align-items: center; gap: 4rpx; }
  315. .cell-action-btn { font-size: 18rpx; color: $text-color-secondary; padding: 2rpx 4rpx;
  316. &:active { opacity: 0.7; }
  317. }
  318. .cell-action-reset { color: $error-color; }
  319. .cell-action-divider { font-size: 18rpx; color: $border-color; }
  320. /* 滚动底部占位 */
  321. .scroll-bottom-spacer { height: 20rpx; }
  322. /* ── 底部操作栏 ── */
  323. .bottom-bar { display: flex; align-items: center; gap: 12rpx; padding: 16rpx 24rpx; padding-bottom: calc(16rpx + env(safe-area-inset-bottom)); background: $bg-color-card; border-top: 1rpx solid $border-color-light; flex-shrink: 0; }
  324. .total-label { font-size: $font-size-sm; color: $text-color-muted; flex-shrink: 0; }
  325. .total-num { font-size: $font-size-xl; font-weight: 700; color: $primary-color-dark; margin: 0 4rpx; }
  326. .submit-btn { flex: 1; display: flex; align-items: center; justify-content: center; gap: 12rpx; height: 72rpx; background: $primary-color; border-radius: $radius-base;
  327. text { font-size: $font-size-md; font-weight: 600; color: $text-color-primary; }
  328. &:active { opacity: 0.8; }
  329. &.disabled { opacity: 0.5; pointer-events: none; }
  330. }
  331. .btn-spinner { width: 28rpx; height: 28rpx; border: 3rpx solid rgba(30,41,59,0.2); border-top-color: $text-color-primary; border-radius: 50%; animation: spin 0.7s linear infinite; }
  332. @keyframes spin { to { transform: rotate(360deg); } }
  333. </style>