warning.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. <template>
  2. <view class="page">
  3. <!-- 导航栏 -->
  4. <NavBar title="库存预警" :showBack="true" />
  5. <!-- 预警统计 -->
  6. <view class="alert-section">
  7. <view class="alert-grid">
  8. <view class="alert-card warning">
  9. <view class="alert-icon">
  10. <view class="icon-alert"></view>
  11. </view>
  12. <view class="alert-info">
  13. <text class="alert-value">{{ stats.lowStockCount || 0 }}</text>
  14. <text class="alert-label">低库存商品</text>
  15. </view>
  16. </view>
  17. <view class="alert-card error">
  18. <view class="alert-icon">
  19. <view class="icon-out"></view>
  20. </view>
  21. <view class="alert-info">
  22. <text class="alert-value">{{ stats.zeroStockCount || 0 }}</text>
  23. <text class="alert-label">缺货商品</text>
  24. </view>
  25. </view>
  26. </view>
  27. </view>
  28. <!-- 标签切换 -->
  29. <view class="tab-section">
  30. <view class="tab-list">
  31. <view
  32. class="tab-item"
  33. :class="{ active: currentTab === 'low' }"
  34. @click="changeTab('low')"
  35. >
  36. <text>低库存</text>
  37. </view>
  38. <view
  39. class="tab-item"
  40. :class="{ active: currentTab === 'out' }"
  41. @click="changeTab('out')"
  42. >
  43. <text>已缺货</text>
  44. </view>
  45. </view>
  46. </view>
  47. <!-- 预警列表 -->
  48. <scroll-view
  49. class="warning-scroll"
  50. scroll-y
  51. @scrolltolower="loadMore"
  52. >
  53. <view class="warning-list">
  54. <view
  55. class="warning-card"
  56. v-for="item in warningList"
  57. :key="item.id"
  58. >
  59. <view class="card-header">
  60. <view class="product-info">
  61. <text class="product-name">{{ item.productName }}</text>
  62. <text class="location">{{ item.shopName }} · {{ item.deviceName }}</text>
  63. </view>
  64. <view class="stock-badge" :class="currentTab">
  65. <text>{{ item.currentStock || 0 }}</text>
  66. </view>
  67. </view>
  68. <view class="card-body">
  69. <view class="progress-bar">
  70. <view
  71. class="progress-fill"
  72. :class="currentTab"
  73. :style="{ width: getProgressWidth(item) + '%' }"
  74. ></view>
  75. </view>
  76. <view class="stock-detail">
  77. <text class="detail-text">预警线: {{ item.minStock || 0 }}</text>
  78. <text class="detail-text">上限: {{ item.maxStock || 0 }}</text>
  79. </view>
  80. </view>
  81. </view>
  82. </view>
  83. <view class="loading-more" v-if="loading">
  84. <view class="loading-spinner"></view>
  85. <text>加载中...</text>
  86. </view>
  87. <view class="no-more" v-if="!hasMore && warningList.length > 0">
  88. <text>— 没有更多了 —</text>
  89. </view>
  90. <view class="empty-state" v-if="!loading && warningList.length === 0">
  91. <view class="empty-icon">
  92. <view class="empty-icon-inner"></view>
  93. </view>
  94. <text class="empty-text">{{ currentTab === 'low' ? '暂无低库存商品' : '暂无缺货商品' }}</text>
  95. </view>
  96. </scroll-view>
  97. </view>
  98. </template>
  99. <script setup lang="ts">
  100. import { ref, onMounted } from 'vue';
  101. import { logger } from '@/utils/logger';
  102. import NavBar from '@/components/NavBar.vue';
  103. import { getInventoryList, getInventoryStatistics } from '@/api/inventory';
  104. const currentTab = ref('low');
  105. const warningList = ref<any[]>([]);
  106. const stats = ref({
  107. lowStockCount: 0,
  108. zeroStockCount: 0
  109. });
  110. const loading = ref(false);
  111. const hasMore = ref(true);
  112. const page = ref(1);
  113. const pageSize = 10;
  114. const getProgressWidth = (item: any) => {
  115. if (!item.maxStock || item.maxStock === 0) return 0;
  116. return Math.min(100, ((item.currentStock || 0) / item.maxStock) * 100);
  117. };
  118. const loadStats = async () => {
  119. try {
  120. const res = await getInventoryStatistics();
  121. if (res) {
  122. stats.value.lowStockCount = res.lowStockCount || 0;
  123. stats.value.zeroStockCount = res.zeroStockCount || 0;
  124. }
  125. } catch (error) {
  126. logger.warn('加载库存统计失败', error);
  127. }
  128. };
  129. const changeTab = (tab: string) => {
  130. currentTab.value = tab;
  131. page.value = 1;
  132. hasMore.value = true;
  133. loadWarnings();
  134. };
  135. const loadWarnings = async () => {
  136. loading.value = true;
  137. try {
  138. const params: any = { page: page.value, pageSize };
  139. if (currentTab.value === 'low') {
  140. params.lowStock = true;
  141. } else if (currentTab.value === 'out') {
  142. params.stockStatus = 0;
  143. }
  144. const res = await getInventoryList(params);
  145. if (!res) return;
  146. const list = res.list || [];
  147. if (page.value === 1) {
  148. warningList.value = list;
  149. } else {
  150. warningList.value = [...warningList.value, ...list];
  151. }
  152. hasMore.value = warningList.value.length < (res.total || 0);
  153. } catch (error) {
  154. logger.warn('加载预警列表失败', error);
  155. } finally {
  156. loading.value = false;
  157. }
  158. };
  159. const loadMore = () => {
  160. if (loading.value || !hasMore.value) return;
  161. page.value++;
  162. loadWarnings();
  163. };
  164. onMounted(() => {
  165. loadStats();
  166. loadWarnings();
  167. });
  168. </script>
  169. <style lang="scss" scoped>
  170. .page {
  171. min-height: 100vh;
  172. background: $bg-color-page;
  173. display: flex;
  174. flex-direction: column;
  175. }
  176. /* 预警统计 */
  177. .alert-section {
  178. padding: 16rpx 24rpx;
  179. background: $bg-color-card;
  180. border-bottom: 1rpx solid $border-color;
  181. }
  182. .alert-grid {
  183. display: grid;
  184. grid-template-columns: 1fr 1fr;
  185. gap: 12rpx;
  186. }
  187. .alert-card {
  188. display: flex;
  189. align-items: center;
  190. padding: 24rpx 20rpx;
  191. border-radius: 16rpx;
  192. border: 1rpx solid $border-color;
  193. &.warning {
  194. background: $accent-color-bg;
  195. border-color: $accent-color;
  196. }
  197. &.error {
  198. background: $error-color-bg;
  199. border-color: $error-color;
  200. }
  201. }
  202. .alert-icon {
  203. width: 64rpx;
  204. height: 64rpx;
  205. border-radius: 16rpx;
  206. background: $bg-color-card;
  207. display: flex;
  208. align-items: center;
  209. justify-content: center;
  210. margin-right: 16rpx;
  211. flex-shrink: 0;
  212. .icon-alert {
  213. width: 28rpx;
  214. height: 28rpx;
  215. border: 3rpx solid $accent-color;
  216. border-radius: 50%;
  217. position: relative;
  218. &::before {
  219. content: '';
  220. position: absolute;
  221. top: 50%;
  222. left: 50%;
  223. transform: translate(-50%, -50%);
  224. width: 3rpx;
  225. height: 12rpx;
  226. background: $accent-color;
  227. }
  228. &::after {
  229. content: '';
  230. position: absolute;
  231. bottom: 4rpx;
  232. left: 50%;
  233. transform: translateX(-50%);
  234. width: 3rpx;
  235. height: 3rpx;
  236. background: $accent-color;
  237. border-radius: 50%;
  238. }
  239. }
  240. .icon-out {
  241. width: 28rpx;
  242. height: 28rpx;
  243. border: 3rpx solid $error-color;
  244. border-radius: 50%;
  245. position: relative;
  246. &::before {
  247. content: '';
  248. position: absolute;
  249. top: 50%;
  250. left: 50%;
  251. transform: translate(-50%, -50%) rotate(45deg);
  252. width: 16rpx;
  253. height: 3rpx;
  254. background: $error-color;
  255. }
  256. &::after {
  257. content: '';
  258. position: absolute;
  259. top: 50%;
  260. left: 50%;
  261. transform: translate(-50%, -50%) rotate(-45deg);
  262. width: 16rpx;
  263. height: 3rpx;
  264. background: $error-color;
  265. }
  266. }
  267. }
  268. .alert-info {
  269. .alert-value {
  270. display: block;
  271. font-size: 40rpx;
  272. font-weight: 700;
  273. color: $text-color-primary;
  274. margin-bottom: 4rpx;
  275. }
  276. .alert-label {
  277. font-size: 24rpx;
  278. color: $text-color-tertiary;
  279. }
  280. }
  281. /* 标签切换 */
  282. .tab-section {
  283. padding: 12rpx 24rpx;
  284. background: $bg-color-card;
  285. }
  286. .tab-list {
  287. display: flex;
  288. gap: 10rpx;
  289. .tab-item {
  290. flex: 1;
  291. padding: 14rpx 0;
  292. text-align: center;
  293. background: $bg-color-page;
  294. border: 2rpx solid $border-color;
  295. border-radius: 20rpx;
  296. font-size: 26rpx;
  297. color: $text-color-tertiary;
  298. transition: all 0.15s;
  299. &.active {
  300. background: $success-color-bg;
  301. border-color: $primary-color;
  302. color: $primary-color;
  303. font-weight: 500;
  304. }
  305. }
  306. }
  307. /* 预警列表 */
  308. .warning-scroll {
  309. flex: 1;
  310. height: 0;
  311. }
  312. .warning-list {
  313. padding: 16rpx 24rpx;
  314. }
  315. .warning-card {
  316. background: $bg-color-card;
  317. border: 1rpx solid $border-color;
  318. border-radius: 16rpx;
  319. margin-bottom: 12rpx;
  320. overflow: hidden;
  321. transition: transform 0.15s;
  322. &:active {
  323. transform: scale(0.98);
  324. }
  325. }
  326. .card-header {
  327. display: flex;
  328. justify-content: space-between;
  329. align-items: flex-start;
  330. padding: 20rpx 24rpx 12rpx;
  331. .product-info {
  332. flex: 1;
  333. min-width: 0;
  334. .product-name {
  335. display: block;
  336. font-size: 28rpx;
  337. font-weight: 600;
  338. color: $text-color-primary;
  339. margin-bottom: 4rpx;
  340. overflow: hidden;
  341. text-overflow: ellipsis;
  342. white-space: nowrap;
  343. }
  344. .location {
  345. font-size: 22rpx;
  346. color: $text-color-muted;
  347. }
  348. }
  349. }
  350. .stock-badge {
  351. padding: 8rpx 20rpx;
  352. border-radius: 12rpx;
  353. font-size: 28rpx;
  354. font-weight: 700;
  355. margin-left: 12rpx;
  356. flex-shrink: 0;
  357. &.low {
  358. background: $accent-color-bg;
  359. color: $accent-color;
  360. }
  361. &.out {
  362. background: $error-color-bg;
  363. color: $error-color;
  364. }
  365. }
  366. .card-body {
  367. padding: 0 24rpx 20rpx;
  368. .progress-bar {
  369. height: 10rpx;
  370. background: $bg-color-secondary;
  371. border-radius: 5rpx;
  372. overflow: hidden;
  373. margin-bottom: 10rpx;
  374. .progress-fill {
  375. height: 100%;
  376. border-radius: 5rpx;
  377. &.low { background: $accent-color; }
  378. &.out { background: $error-color; }
  379. }
  380. }
  381. .stock-detail {
  382. display: flex;
  383. justify-content: space-between;
  384. .detail-text {
  385. font-size: 22rpx;
  386. color: $text-color-muted;
  387. }
  388. }
  389. }
  390. /* 加载状态 */
  391. .loading-more {
  392. display: flex;
  393. align-items: center;
  394. justify-content: center;
  395. gap: 12rpx;
  396. padding: 32rpx;
  397. color: $text-color-muted;
  398. font-size: 24rpx;
  399. .loading-spinner {
  400. width: 32rpx;
  401. height: 32rpx;
  402. border: 3rpx solid $border-color;
  403. border-top-color: $primary-color;
  404. border-radius: 50%;
  405. animation: spin 1s linear infinite;
  406. }
  407. }
  408. @keyframes spin {
  409. to { transform: rotate(360deg); }
  410. }
  411. .no-more {
  412. text-align: center;
  413. padding: 32rpx;
  414. font-size: 24rpx;
  415. color: $text-color-placeholder;
  416. }
  417. .empty-state {
  418. display: flex;
  419. flex-direction: column;
  420. align-items: center;
  421. padding: 100rpx 0;
  422. .empty-icon {
  423. width: 120rpx;
  424. height: 120rpx;
  425. background: $bg-color-secondary;
  426. border-radius: 24rpx;
  427. margin-bottom: 20rpx;
  428. display: flex;
  429. align-items: center;
  430. justify-content: center;
  431. .empty-icon-inner {
  432. width: 48rpx;
  433. height: 48rpx;
  434. border: 4rpx solid $text-color-placeholder;
  435. border-radius: 50%;
  436. position: relative;
  437. &::before {
  438. content: '';
  439. position: absolute;
  440. top: 50%;
  441. left: 50%;
  442. transform: translate(-50%, -50%);
  443. width: 20rpx;
  444. height: 20rpx;
  445. background: $text-color-placeholder;
  446. border-radius: 50%;
  447. }
  448. }
  449. }
  450. .empty-text {
  451. font-size: 28rpx;
  452. color: $text-color-muted;
  453. }
  454. }
  455. </style>