audit.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <template>
  2. <view class="page">
  3. <NavBar title="库存盘点" showBack />
  4. <template>
  5. <!-- 设备信息 -->
  6. <view class="device-bar">
  7. <text class="device-bar-text">{{ deviceId }}<text v-if="deviceName"> · {{ deviceName }}</text></text>
  8. </view>
  9. <!-- 商品清单 -->
  10. <view class="inventory-section">
  11. <view class="section-head">
  12. <text class="section-title">盘点清单</text>
  13. <text class="section-count">{{ searchKeyword ? filteredInventoryList.length + ' / ' + inventoryList.length : inventoryList.length }} 种</text>
  14. </view>
  15. <view class="search-bar">
  16. <view class="search-icon"></view>
  17. <input class="search-input" v-model="searchKeyword" placeholder="搜索商品名称或编码" confirm-type="search" />
  18. <view class="search-clear" v-if="searchKeyword" @click="searchKeyword = ''"><text>✕</text></view>
  19. </view>
  20. <view class="empty-state" v-if="inventoryList.length === 0">
  21. <view class="empty-icon-box"><view class="empty-doc-icon"></view></view>
  22. <text class="empty-text">暂无商品库存数据</text>
  23. </view>
  24. <view class="empty-state" v-else-if="inventoryList.length > 0 && filteredInventoryList.length === 0">
  25. <text class="empty-text">未找到匹配商品</text>
  26. </view>
  27. <view class="inventory-card" v-for="({ item, idx }, i) in filteredInventoryList" :key="idx">
  28. <view class="item-row">
  29. <image v-if="item.product_image" :src="item.product_image" class="item-img" mode="aspectFill" />
  30. <view v-else class="item-img-placeholder"><text>无图</text></view>
  31. <view class="item-info">
  32. <view class="item-name-row">
  33. <text class="item-name">{{ item.product_name || item.productName || '未知商品' }}</text>
  34. <view class="stock-badge" :class="getDiffClass(item, idx)">{{ getDiffLabel(item, idx) }}</view>
  35. </view>
  36. <view class="item-meta">
  37. <text class="item-code" v-if="item.product_code || item.productCode">{{ item.product_code || item.productCode }}</text>
  38. <text class="item-std">标准 {{ item.standard_stock || item.standardStock || '-' }}</text>
  39. <text class="item-cur">当前 {{ item.stock || 0 }}</text>
  40. </view>
  41. </view>
  42. </view>
  43. <view class="audit-row">
  44. <view class="qty-control">
  45. <view class="qty-btn" @click="decrease(idx)"><text>-</text></view>
  46. <input class="qty-input" type="number" v-model.number="auditItems[idx].newStock" />
  47. <view class="qty-btn" @click="increase(idx)"><text>+</text></view>
  48. </view>
  49. <view class="quick-btn" @click="setToStandard(item, idx)"><text>设标</text></view>
  50. <view class="quick-btn quick-reset" @click="setToCurrentStock(item, idx)"><text>重置</text></view>
  51. </view>
  52. </view>
  53. </view>
  54. <!-- 底部操作栏 -->
  55. <view class="bottom-bar" v-if="inventoryList.length > 0">
  56. <text class="total-label">修正 <text class="total-num">{{ changedCount }}</text> 项</text>
  57. <view class="submit-btn" :class="{ disabled: submitting || changedCount === 0 }" @click="handleSubmit">
  58. <view class="btn-spinner" v-if="submitting"></view>
  59. <text>{{ submitting ? '提交中' : '提交盘点' }}</text>
  60. </view>
  61. </view>
  62. </template>
  63. </view>
  64. </template>
  65. <script setup lang="ts">
  66. import { ref, computed, onMounted } from 'vue';
  67. import NavBar from '@/components/NavBar.vue';
  68. import { getDeviceInventory } from '@/api/replenish';
  69. import { adjustStock } from '@/api/inventory';
  70. interface AuditInput {
  71. productId: number | null;
  72. productCode: string;
  73. productName: string;
  74. newStock: number;
  75. originalStock: number;
  76. }
  77. const deviceId = ref('');
  78. const deviceName = ref('');
  79. const inventoryList = ref<any[]>([]);
  80. const auditItems = ref<AuditInput[]>([]);
  81. const submitting = ref(false);
  82. const searchKeyword = ref('');
  83. const filteredInventoryList = computed(() => {
  84. const kw = searchKeyword.value.trim().toLowerCase();
  85. return inventoryList.value
  86. .map((item, idx) => ({ item, idx }))
  87. .filter(({ item }) => {
  88. if (!kw) return true;
  89. const name = (item.product_name || item.productName || '').toLowerCase();
  90. const code = (item.product_code || item.productCode || '').toLowerCase();
  91. return name.includes(kw) || code.includes(kw);
  92. });
  93. });
  94. const changedCount = computed(() =>
  95. auditItems.value.filter(i => i.productId != null && i.newStock !== i.originalStock).length
  96. );
  97. function decrease(index: number) {
  98. if (auditItems.value[index].newStock > 0) auditItems.value[index].newStock--;
  99. }
  100. function increase(index: number) {
  101. if (auditItems.value[index].newStock < 9999) auditItems.value[index].newStock++;
  102. }
  103. function setToCurrentStock(item: any, index: number) {
  104. auditItems.value[index].newStock = item.stock || 0;
  105. }
  106. function setToStandard(item: any, index: number) {
  107. const std = item.standard_stock || item.standardStock || 0;
  108. auditItems.value[index].newStock = std;
  109. }
  110. function getDiffLabel(item: any, index: number) {
  111. if (!auditItems.value[index]) return '正常';
  112. const cur = auditItems.value[index].newStock;
  113. const std = item.standard_stock || item.standardStock || 0;
  114. if (cur < std) return '偏少';
  115. if (cur > std) return '偏多';
  116. return '正常';
  117. }
  118. function getDiffClass(item: any, index: number) {
  119. if (!auditItems.value[index]) return 'normal';
  120. const cur = auditItems.value[index].newStock;
  121. const std = item.standard_stock || item.standardStock || 0;
  122. if (cur < std) return 'warning';
  123. if (cur > std) return 'danger';
  124. return 'normal';
  125. }
  126. async function handleSubmit() {
  127. if (submitting.value || changedCount.value === 0) return;
  128. const changes = auditItems.value.filter(i => i.productId != null && i.newStock !== i.originalStock);
  129. if (!changes.length) { uni.showToast({ title: '无变更', icon: 'none' }); return; }
  130. submitting.value = true;
  131. let success = 0;
  132. let fail = 0;
  133. for (const item of changes) {
  134. try {
  135. await adjustStock({
  136. deviceId: deviceId.value,
  137. productId: item.productId!,
  138. newStock: item.newStock,
  139. remark: '小程序盘点修正'
  140. });
  141. success++;
  142. } catch {
  143. fail++;
  144. }
  145. }
  146. if (fail === 0) {
  147. uni.showToast({ title: `盘点完成,修正${success}项`, icon: 'success' });
  148. setTimeout(() => uni.navigateBack(), 800);
  149. } else {
  150. uni.showToast({ title: `成功${success}项,失败${fail}项`, icon: 'none' });
  151. }
  152. submitting.value = false;
  153. }
  154. onMounted(async () => {
  155. const pages = getCurrentPages();
  156. const opts = (pages[pages.length - 1] as any)?.$page?.options || (pages[pages.length - 1] as any)?.options || {};
  157. deviceId.value = opts.deviceId || '';
  158. if (!deviceId.value) { uni.showToast({ title: '缺少设备ID', icon: 'none' }); setTimeout(() => uni.navigateBack(), 500); return; }
  159. try {
  160. const data = await getDeviceInventory(deviceId.value);
  161. deviceName.value = data?.name || '';
  162. const list = (data?.inventoryList || data || []) as any[];
  163. // 兼容两种返回格式
  164. if (Array.isArray(data)) {
  165. inventoryList.value = data;
  166. } else if (data?.inventoryList) {
  167. inventoryList.value = data.inventoryList;
  168. deviceName.value = data.name || '';
  169. } else {
  170. inventoryList.value = [];
  171. }
  172. auditItems.value = inventoryList.value.map((it: any) => ({
  173. productId: it.product_id || it.productId || null,
  174. productCode: it.product_code || it.productCode || '',
  175. productName: it.product_name || it.productName || '',
  176. newStock: it.stock || 0,
  177. originalStock: it.stock || 0
  178. }));
  179. } catch (e: any) {
  180. uni.showToast({ title: e.message || '加载失败', icon: 'none' });
  181. }
  182. });
  183. </script>
  184. <style lang="scss" scoped>
  185. .page { height: 100vh; background: $bg-color-page; display: flex; flex-direction: column; }
  186. .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); }
  187. .device-bar-text { font-size: $font-size-sm; font-weight: 600; color: $text-color-primary; }
  188. .inventory-section { padding: 0 24rpx; flex: 1; height: 0; overflow-y: auto; }
  189. .section-head { display: flex; align-items: center; justify-content: space-between; padding: 16rpx 0 12rpx; }
  190. .section-title { font-size: $font-size-md; font-weight: 700; color: $text-color-primary; }
  191. .section-count { font-size: $font-size-sm; color: $text-color-muted; }
  192. .search-bar { display: flex; align-items: center; background: $bg-color-card; border: 1rpx solid $border-color; border-radius: $radius-base; padding: 0 16rpx; height: 60rpx; margin-bottom: 12rpx; }
  193. .search-icon { width: 28rpx; height: 28rpx; border: 2.5rpx solid $text-color-muted; border-radius: 50%; position: relative; flex-shrink: 0; margin-right: 10rpx;
  194. &::after { content: ''; position: absolute; bottom: -2rpx; right: -4rpx; width: 2.5rpx; height: 10rpx; background: $text-color-muted; border-radius: 1rpx; transform: rotate(-45deg); }
  195. }
  196. .search-input { flex: 1; font-size: $font-size-sm; color: $text-color-primary; height: 100%; }
  197. .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;
  198. text { font-size: 20rpx; color: $text-color-muted; line-height: 1; }
  199. }
  200. .empty-state { display: flex; flex-direction: column; align-items: center; padding: 80rpx 0; }
  201. .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; }
  202. .empty-doc-icon { width: 40rpx; height: 48rpx; border: 2.5rpx solid $text-color-placeholder; border-radius: 4rpx; position: relative;
  203. &::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; }
  204. }
  205. .empty-text { font-size: $font-size-base; color: $text-color-muted; }
  206. .inventory-card { margin-bottom: 10rpx; padding: 16rpx; background: $bg-color-card; border-radius: $radius-md; box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.04); }
  207. .item-row { display: flex; align-items: center; margin-bottom: 10rpx; }
  208. .item-img { width: 56rpx; height: 56rpx; border-radius: $radius-sm; background: $bg-color-page; flex-shrink: 0; margin-right: 12rpx; }
  209. .item-img-placeholder { width: 56rpx; height: 56rpx; border-radius: $radius-sm; background: $bg-color-page; flex-shrink: 0; margin-right: 12rpx; display: flex; align-items: center; justify-content: center;
  210. text { font-size: 18rpx; color: $text-color-muted; }
  211. }
  212. .item-info { flex: 1; min-width: 0; overflow: hidden; }
  213. .item-name-row { display: flex; align-items: center; justify-content: space-between; margin-bottom: 4rpx; }
  214. .item-name { font-size: $font-size-base; font-weight: 600; color: $text-color-primary; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
  215. .item-meta { display: flex; gap: 16rpx; }
  216. .item-code { font-size: 20rpx; color: $text-color-muted; }
  217. .item-std { font-size: 20rpx; color: $text-color-muted; }
  218. .item-cur { font-size: 20rpx; font-weight: 500; color: $text-color-primary; }
  219. .stock-badge { padding: 2rpx 10rpx; border-radius: $radius-sm; font-size: 20rpx; font-weight: 500; flex-shrink: 0; margin-left: 8rpx;
  220. &.normal { background: $success-color-bg; color: $success-color; }
  221. &.warning { background: $warning-color-bg; color: $warning-color; }
  222. &.danger { background: $error-color-bg; color: $error-color; }
  223. }
  224. .audit-row { display: flex; align-items: center; gap: 10rpx; background: $bg-color-page; border-radius: $radius-base; padding: 8rpx 12rpx; }
  225. .qty-control { display: flex; align-items: center; border: 1rpx solid $border-color; border-radius: $radius-sm; overflow: hidden; }
  226. .qty-btn { width: 44rpx; height: 44rpx; display: flex; align-items: center; justify-content: center; background: $bg-color-card;
  227. text { font-size: 26rpx; color: $text-color-secondary; line-height: 1; }
  228. &:active { background: $bg-color-secondary; }
  229. }
  230. .qty-input { width: 64rpx; height: 44rpx; text-align: center; font-size: $font-size-base; font-weight: 600; color: $text-color-primary; border-left: 1rpx solid $border-color; border-right: 1rpx solid $border-color; }
  231. .quick-btn { padding: 8rpx 16rpx; background: $bg-color-secondary; border-radius: $radius-sm;
  232. text { font-size: 22rpx; color: $text-color-secondary; }
  233. &:active { opacity: 0.7; }
  234. &.quick-reset { background: $error-color-bg; text { color: $error-color; } }
  235. }
  236. .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; }
  237. .total-label { font-size: $font-size-sm; color: $text-color-muted; flex-shrink: 0; }
  238. .total-num { font-size: $font-size-xl; font-weight: 700; color: $primary-color-dark; margin: 0 4rpx; }
  239. .submit-btn { flex: 1; display: flex; align-items: center; justify-content: center; gap: 12rpx; height: 72rpx; background: $primary-color; border-radius: $radius-base;
  240. text { font-size: $font-size-md; font-weight: 600; color: $text-color-primary; }
  241. &:active { opacity: 0.8; }
  242. &.disabled { opacity: 0.5; pointer-events: none; }
  243. }
  244. .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; }
  245. @keyframes spin { to { transform: rotate(360deg); } }
  246. </style>