operation.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. <template>
  2. <view class="page">
  3. <NavBar title="补货操作" showBack />
  4. <view class="loading-container" v-if="loading">
  5. <view class="loading-spinner"></view>
  6. <text class="loading-text">加载设备信息...</text>
  7. </view>
  8. <template v-else>
  9. <!-- 补货单提示 -->
  10. <view class="order-banner" v-if="orderNo">
  11. <text class="order-banner-text">补货单 {{ orderNo }} · 数量已按计划预填</text>
  12. </view>
  13. <!-- 设备信息 -->
  14. <view class="device-section">
  15. <view class="device-card">
  16. <view class="device-header">
  17. <view class="device-icon"></view>
  18. <view class="device-info">
  19. <text class="device-name">{{ deviceInfo.name || deviceInfo.deviceId }}</text>
  20. <text class="device-id" v-if="deviceInfo.address">{{ deviceInfo.address }}</text>
  21. </view>
  22. </view>
  23. </view>
  24. </view>
  25. <!-- 商品库存清单 -->
  26. <view class="inventory-section">
  27. <view class="section-header">
  28. <text class="section-title">商品库存清单</text>
  29. </view>
  30. <!-- 空状态 -->
  31. <view class="empty-state" v-if="inventoryList.length === 0">
  32. <text class="empty-text">暂无商品库存数据</text>
  33. </view>
  34. <!-- 商品列表 -->
  35. <view
  36. class="inventory-card"
  37. v-for="(item, index) in inventoryList"
  38. :key="index"
  39. >
  40. <view class="item-main">
  41. <image
  42. v-if="item.product_image"
  43. :src="item.product_image"
  44. class="item-img"
  45. mode="aspectFill"
  46. />
  47. <view v-else class="item-img-placeholder"><text>无图</text></view>
  48. <view class="item-info">
  49. <text class="item-name">{{ item.product_name || item.productName || '未知商品' }}</text>
  50. <text class="item-code" v-if="item.product_code || item.productCode">{{ item.product_code || item.productCode }}</text>
  51. </view>
  52. <view :class="['stock-badge', getStockStatus(item)]">
  53. {{ getStockStatusText(item) }}
  54. </view>
  55. </view>
  56. <view class="item-stock">
  57. <view class="stock-row">
  58. <text class="stock-label">标准库存</text>
  59. <text class="stock-value muted">{{ item.warning_threshold || item.warningThreshold || '-' }}</text>
  60. </view>
  61. <view class="stock-row">
  62. <text class="stock-label">剩余库存</text>
  63. <text :class="['stock-value', { 'low-stock': isLowStock(item), 'out-of-stock': isOutOfStock(item) }]">
  64. {{ item.stock || 0 }}
  65. </text>
  66. </view>
  67. </view>
  68. <!-- 补货输入 -->
  69. <view class="replenish-input-section">
  70. <text class="input-label">补货数量</text>
  71. <view class="quantity-control">
  72. <view class="qty-btn" @click="decrease(item, index)">-</view>
  73. <input
  74. class="qty-input"
  75. type="number"
  76. v-model="replenishItems[index].quantity"
  77. @blur="validateQuantity(index)"
  78. />
  79. <view class="qty-btn" @click="increase(item, index)">+</view>
  80. </view>
  81. <view class="suggest-btn" @click="suggestQuantity(item, index)">
  82. 一键补满
  83. </view>
  84. </view>
  85. </view>
  86. </view>
  87. <!-- 底部提交 -->
  88. <view class="bottom-actions" v-if="inventoryList.length > 0">
  89. <view class="total-info">
  90. <text class="total-label">共补货</text>
  91. <text class="total-value">{{ totalReplenishCount }}</text>
  92. <text class="total-label">件商品</text>
  93. </view>
  94. <button class="submit-btn" :loading="submitting" :disabled="submitting || totalReplenishCount === 0" @click="handleSubmit">
  95. <text v-if="!submitting">确认补货</text>
  96. <text v-else>提交中...</text>
  97. </button>
  98. </view>
  99. </template>
  100. </view>
  101. </template>
  102. <script setup lang="ts">
  103. import { ref, computed, onMounted } from 'vue';
  104. import NavBar from '@/components/NavBar.vue';
  105. import { getDeviceInventory, replenishStock } from '@/api/replenish';
  106. interface InventoryItem {
  107. product_id?: number;
  108. productId?: number;
  109. product_code?: string;
  110. productCode?: string;
  111. product_name?: string;
  112. productName?: string;
  113. stock?: number;
  114. warning_threshold?: number;
  115. warningThreshold?: number;
  116. shelf_num?: number;
  117. shelfNum?: number;
  118. position?: string;
  119. }
  120. interface ReplenishInput {
  121. productId: number;
  122. productCode?: string;
  123. productName?: string;
  124. quantity: number;
  125. shelfNum?: number;
  126. position?: string;
  127. }
  128. const deviceId = ref('');
  129. const deviceInfo = ref<any>({});
  130. const inventoryList = ref<InventoryItem[]>([]);
  131. const replenishItems = ref<ReplenishInput[]>([]);
  132. const loading = ref(true);
  133. const submitting = ref(false);
  134. const totalReplenishCount = computed(() => {
  135. return replenishItems.value.reduce((sum, item) => sum + (item.quantity || 0), 0);
  136. });
  137. const isLowStock = (item: any): boolean => {
  138. const stock = item.stock || 0;
  139. const threshold = item.warning_threshold || item.warningThreshold || 5;
  140. return stock > 0 && stock <= threshold;
  141. };
  142. const isOutOfStock = (item: any): boolean => {
  143. return (item.stock || 0) === 0;
  144. };
  145. const getStockStatus = (item: any): string => {
  146. if (isOutOfStock(item)) return 'danger';
  147. if (isLowStock(item)) return 'warning';
  148. return 'normal';
  149. };
  150. const getStockStatusText = (item: any): string => {
  151. if ((item.stock || 0) === 0) return '缺货';
  152. const threshold = item.warning_threshold || item.warningThreshold || 5;
  153. if ((item.stock || 0) <= threshold) return '低库存';
  154. return '正常';
  155. };
  156. const decrease = (item: any, index: number) => {
  157. if (replenishItems.value[index].quantity > 0) {
  158. replenishItems.value[index].quantity--;
  159. }
  160. };
  161. const MAX_QUANTITY = 9999;
  162. const increase = (item: any, index: number) => {
  163. if (replenishItems.value[index].quantity < MAX_QUANTITY) {
  164. replenishItems.value[index].quantity++;
  165. }
  166. };
  167. const suggestQuantity = (item: any, index: number) => {
  168. const threshold = item.warning_threshold || item.warningThreshold || 5;
  169. const currentStock = item.stock || 0;
  170. // 建议补满到预警阈值的3倍(即建议库存水平)
  171. const suggestLevel = threshold * 3;
  172. const suggest = Math.max(suggestLevel - currentStock, 0);
  173. replenishItems.value[index].quantity = suggest;
  174. };
  175. const validateQuantity = (index: number) => {
  176. if (replenishItems.value[index].quantity < 0) {
  177. replenishItems.value[index].quantity = 0;
  178. }
  179. };
  180. const handleSubmit = async () => {
  181. if (totalReplenishCount.value === 0) {
  182. uni.showToast({ title: '请至少补货一件商品', icon: 'none' });
  183. return;
  184. }
  185. // 过滤掉数量为0的项
  186. const items = replenishItems.value
  187. .filter(item => item.quantity > 0)
  188. .map(item => ({
  189. productId: item.productId,
  190. productCode: item.productCode,
  191. productName: item.productName,
  192. quantity: item.quantity,
  193. shelfNum: item.shelfNum,
  194. position: item.position
  195. }));
  196. if (items.length === 0) {
  197. uni.showToast({ title: '请至少补货一件商品', icon: 'none' });
  198. return;
  199. }
  200. submitting.value = true;
  201. try {
  202. const payload: any = { deviceId: deviceId.value, items };
  203. if (orderId.value) payload.orderId = orderId.value;
  204. const result = await replenishStock(payload);
  205. uni.removeStorageSync('pendingReplenishOrder');
  206. uni.showToast({ title: `补货完成,成功${result.success}项`, icon: 'success' });
  207. setTimeout(() => {
  208. uni.navigateBack();
  209. }, 800);
  210. } catch (error: any) {
  211. uni.showToast({ title: error.message || '补货失败', icon: 'none' });
  212. } finally {
  213. submitting.value = false;
  214. }
  215. };
  216. const orderId = ref('');
  217. const orderNo = ref('');
  218. onMounted(async () => {
  219. const pages = getCurrentPages();
  220. const currentPage = pages[pages.length - 1];
  221. const options = (currentPage as any).$page?.options || (currentPage as any).options || {};
  222. deviceId.value = options.deviceId || '';
  223. orderId.value = options.orderId || '';
  224. if (!deviceId.value) {
  225. uni.showToast({ title: '缺少设备ID', icon: 'none' });
  226. setTimeout(() => uni.navigateBack(), 500);
  227. return;
  228. }
  229. try {
  230. const data = await getDeviceInventory(deviceId.value);
  231. deviceInfo.value = data;
  232. const list = data.inventoryList || [];
  233. inventoryList.value = list;
  234. // 加载补货单预填数据
  235. const orderStr = uni.getStorageSync('pendingReplenishOrder');
  236. let orderItems: any[] = [];
  237. if (orderStr) {
  238. try {
  239. const orderData = JSON.parse(orderStr);
  240. if (orderData.orderId === orderId.value) {
  241. orderNo.value = orderData.orderNo || '';
  242. orderItems = orderData.items || [];
  243. }
  244. } catch { /* ignore parse error */ }
  245. }
  246. // 初始化补货输入,匹配补货单预填数量
  247. replenishItems.value = list
  248. .filter((item: any) => (item.product_id || item.productId))
  249. .map((item: any) => {
  250. const code = item.product_code || item.productCode || '';
  251. const matched = orderItems.find((oi: any) => oi.productCode === code);
  252. return {
  253. productId: item.product_id || item.productId,
  254. productCode: code,
  255. productName: item.product_name || item.productName || '',
  256. quantity: matched ? (matched.plannedQuantity || 0) : 0,
  257. shelfNum: item.shelf_num || item.shelfNum || null,
  258. position: item.position || null
  259. };
  260. });
  261. } catch (error: any) {
  262. uni.showToast({ title: error.message || '加载失败', icon: 'none' });
  263. } finally {
  264. loading.value = false;
  265. }
  266. });
  267. </script>
  268. <style lang="scss" scoped>
  269. .page {
  270. min-height: 100vh;
  271. background: $bg-color-page;
  272. padding-bottom: 200rpx;
  273. }
  274. /* 补货单提示 */
  275. .order-banner { margin: $spacing-2 $spacing-3; padding: $spacing-2 $spacing-3; background: $primary-color-bg; border-radius: $radius-sm; border-left: 4rpx solid $primary-color; }
  276. .order-banner-text { font-size: $font-size-sm; color: $primary-color-dark; }
  277. /* 加载 */
  278. .loading-container {
  279. display: flex;
  280. flex-direction: column;
  281. align-items: center;
  282. padding: 100rpx 0;
  283. .loading-text {
  284. margin-top: 16rpx;
  285. font-size: 26rpx;
  286. color: $text-color-muted;
  287. }
  288. }
  289. /* 设备信息 */
  290. .device-section {
  291. padding: 16rpx 24rpx 0;
  292. .device-card {
  293. background: $bg-color-card;
  294. border: 1rpx solid $border-color;
  295. border-radius: 16rpx;
  296. padding: 24rpx;
  297. .device-header {
  298. display: flex;
  299. align-items: center;
  300. .device-icon {
  301. width: 48rpx;
  302. height: 48rpx;
  303. background: $success-color-bg;
  304. border-radius: 12rpx;
  305. margin-right: 16rpx;
  306. position: relative;
  307. &::before {
  308. content: '';
  309. position: absolute;
  310. top: 8rpx;
  311. left: 50%;
  312. transform: translateX(-50%);
  313. width: 20rpx;
  314. height: 16rpx;
  315. border: 3rpx solid $primary-color;
  316. border-radius: 4rpx;
  317. }
  318. &::after {
  319. content: '';
  320. position: absolute;
  321. bottom: 4rpx;
  322. left: 50%;
  323. transform: translateX(-50%);
  324. width: 12rpx;
  325. height: 6rpx;
  326. background: $primary-color;
  327. border-radius: 0 0 4rpx 4rpx;
  328. }
  329. }
  330. .device-info {
  331. flex: 1;
  332. .device-name {
  333. display: block;
  334. font-size: 30rpx;
  335. font-weight: 600;
  336. color: $text-color-primary;
  337. }
  338. .device-id {
  339. font-size: 24rpx;
  340. color: $text-color-tertiary;
  341. }
  342. }
  343. }
  344. }
  345. }
  346. /* 库存区域 */
  347. .inventory-section {
  348. padding: 0 24rpx;
  349. }
  350. .section-header {
  351. padding: 24rpx 0 16rpx;
  352. .section-title {
  353. font-size: 30rpx;
  354. font-weight: 700;
  355. color: $text-color-primary;
  356. }
  357. }
  358. .empty-state {
  359. text-align: center;
  360. padding: 60rpx 0;
  361. .empty-text {
  362. font-size: 26rpx;
  363. color: $text-color-muted;
  364. }
  365. }
  366. /* 商品卡片 */
  367. .inventory-card {
  368. background: $bg-color-card;
  369. border: 1rpx solid $border-color;
  370. border-radius: 16rpx;
  371. padding: 24rpx;
  372. margin-bottom: 16rpx;
  373. .item-main { display: flex; align-items: center; margin-bottom: 16rpx; }
  374. .item-img { width: 80rpx; height: 80rpx; border-radius: $radius-sm; background: $bg-color-page; flex-shrink: 0; margin-right: 16rpx; }
  375. .item-img-placeholder { width: 80rpx; height: 80rpx; border-radius: $radius-sm; background: $bg-color-page; flex-shrink: 0; margin-right: 16rpx; display: flex; align-items: center; justify-content: center;
  376. text { font-size: 20rpx; color: $text-color-muted; }
  377. }
  378. .item-info { flex: 1; min-width: 0; overflow: hidden; }
  379. .item-name { display: block; font-size: $font-size-base; font-weight: 600; color: $text-color-primary; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  380. .item-code { font-size: 22rpx; color: $text-color-muted; }
  381. .stock-badge { padding: 4rpx 12rpx; border-radius: $radius-sm; font-size: 20rpx; font-weight: 500; flex-shrink: 0; margin-left: 12rpx;
  382. &.normal { background: $success-color-bg; color: $success-color; }
  383. &.warning { background: $warning-color-bg; color: $warning-color; }
  384. &.danger { background: $error-color-bg; color: $error-color; }
  385. }
  386. .item-stock {
  387. display: flex;
  388. gap: 24rpx;
  389. margin-bottom: 16rpx;
  390. .stock-row {
  391. flex: 1;
  392. display: flex;
  393. align-items: center;
  394. .stock-label {
  395. font-size: 24rpx;
  396. color: $text-color-muted;
  397. margin-right: 8rpx;
  398. }
  399. .stock-value {
  400. font-size: 28rpx;
  401. font-weight: 700;
  402. color: $text-color-primary;
  403. &.muted {
  404. color: $text-color-muted;
  405. }
  406. &.low-stock {
  407. color: $warning-color;
  408. }
  409. &.out-of-stock {
  410. color: $error-color;
  411. }
  412. }
  413. }
  414. }
  415. .replenish-input-section {
  416. display: flex;
  417. align-items: center;
  418. background: $bg-color-page;
  419. border-radius: 12rpx;
  420. padding: 16rpx;
  421. .input-label {
  422. font-size: 24rpx;
  423. color: $text-color-tertiary;
  424. margin-right: 16rpx;
  425. }
  426. .quantity-control {
  427. display: flex;
  428. align-items: center;
  429. border: 1rpx solid $border-color;
  430. border-radius: 8rpx;
  431. overflow: hidden;
  432. .qty-btn {
  433. width: 56rpx;
  434. height: 56rpx;
  435. display: flex;
  436. align-items: center;
  437. justify-content: center;
  438. font-size: 32rpx;
  439. font-weight: 500;
  440. color: $text-color-tertiary;
  441. background: $bg-color-card;
  442. &:active {
  443. background: $bg-color-secondary;
  444. }
  445. }
  446. .qty-input {
  447. width: 80rpx;
  448. height: 56rpx;
  449. text-align: center;
  450. font-size: 28rpx;
  451. font-weight: 600;
  452. color: $text-color-primary;
  453. border-left: 1rpx solid $border-color;
  454. border-right: 1rpx solid $border-color;
  455. }
  456. }
  457. .suggest-btn {
  458. margin-left: auto;
  459. padding: 10rpx 20rpx;
  460. background: $success-color-bg;
  461. border-radius: 8rpx;
  462. font-size: 22rpx;
  463. color: $primary-color;
  464. font-weight: 500;
  465. &:active {
  466. background: $success-color-bg;
  467. }
  468. }
  469. }
  470. }
  471. /* 底部操作 */
  472. .bottom-actions {
  473. position: fixed;
  474. bottom: 0;
  475. left: 0;
  476. right: 0;
  477. background: $bg-color-card;
  478. border-top: 1rpx solid $border-color;
  479. padding: 20rpx 24rpx;
  480. padding-bottom: calc(20rpx + constant(safe-area-inset-bottom));
  481. padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
  482. display: flex;
  483. align-items: center;
  484. gap: 20rpx;
  485. .total-info {
  486. display: flex;
  487. align-items: center;
  488. .total-label {
  489. font-size: 24rpx;
  490. color: $text-color-tertiary;
  491. }
  492. .total-value {
  493. font-size: 36rpx;
  494. font-weight: 700;
  495. color: $primary-color;
  496. margin: 0 8rpx;
  497. }
  498. }
  499. .submit-btn {
  500. flex: 1;
  501. height: 88rpx;
  502. line-height: 88rpx;
  503. padding: 0;
  504. background: $primary-color;
  505. color: #fff;
  506. font-size: 30rpx;
  507. font-weight: 600;
  508. border-radius: 12rpx;
  509. display: flex;
  510. align-items: center;
  511. justify-content: center;
  512. &::after {
  513. border: none;
  514. }
  515. &:active {
  516. background: $primary-color-dark;
  517. }
  518. &[disabled] {
  519. opacity: 0.6;
  520. }
  521. }
  522. }
  523. </style>