products.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. <template>
  2. <view class="products-page">
  3. <!-- 加载状态 -->
  4. <view v-if="loading" class="loading-container">
  5. <view class="loading-spinner"></view>
  6. <text class="loading-text">加载中...</text>
  7. </view>
  8. <!-- 错误状态 -->
  9. <view v-else-if="errorMsg" class="error-container">
  10. <text class="error-icon">!</text>
  11. <text class="error-text">{{ errorMsg }}</text>
  12. <view class="error-btn" @click="goBack">返回</view>
  13. </view>
  14. <!-- 商品内容 -->
  15. <view v-else class="content">
  16. <!-- 顶部设备信息 -->
  17. <view class="device-header">
  18. <view class="device-name">{{ templateName || '智能零售柜' }}</view>
  19. <view class="device-info">
  20. <text class="info-tag">{{ deviceTypeText }}</text>
  21. <text class="info-tag">共{{ shelfNum }}层</text>
  22. </view>
  23. </view>
  24. <!-- 双门柜Tab切换 -->
  25. <view v-if="hasRightDoor" class="door-tabs">
  26. <view
  27. class="door-tab"
  28. :class="{ active: activeDoor === 'left' }"
  29. @click="activeDoor = 'left'"
  30. >左门</view>
  31. <view
  32. class="door-tab"
  33. :class="{ active: activeDoor === 'right' }"
  34. @click="activeDoor = 'right'"
  35. >右门</view>
  36. </view>
  37. <!-- 商品列表 -->
  38. <view class="floors-list">
  39. <view
  40. v-for="floor in currentFloors"
  41. :key="floor.floor"
  42. class="floor-card"
  43. >
  44. <view class="floor-header">
  45. <view class="floor-badge">{{ floor.floor }}</view>
  46. <text class="floor-title">第{{ floor.floor }}层</text>
  47. <text class="floor-count">{{ floor.goods?.length || 0 }}件商品</text>
  48. </view>
  49. <!-- 该层无商品 -->
  50. <view v-if="!floor.goods || floor.goods.length === 0" class="empty-floor">
  51. <text class="empty-text">该层暂无商品</text>
  52. </view>
  53. <!-- 商品列表 -->
  54. <view v-else class="goods-list">
  55. <view
  56. v-for="(item, index) in floor.goods"
  57. :key="index"
  58. class="goods-item"
  59. >
  60. <!-- 商品图片 -->
  61. <view class="goods-image-wrap">
  62. <image
  63. v-if="item.pic"
  64. :src="item.pic"
  65. class="goods-image"
  66. mode="aspectFill"
  67. />
  68. <view v-else class="goods-image-placeholder">
  69. <text class="placeholder-text">暂无图片</text>
  70. </view>
  71. </view>
  72. <!-- 商品信息 -->
  73. <view class="goods-info">
  74. <text class="goods-name">{{ item.label || '未知商品' }}</text>
  75. <text v-if="item.type" class="goods-type">{{ item.type }}</text>
  76. <view class="goods-price-row">
  77. <text class="goods-price">¥{{ item.c_price || '0.00' }}</text>
  78. <text
  79. v-if="item.dis_price && item.dis_price !== item.c_price"
  80. class="goods-discount-price"
  81. >¥{{ item.dis_price }}</text>
  82. </view>
  83. <view v-if="item.stock !== undefined && item.stock !== null" class="goods-stock">
  84. <text class="stock-label">库存: </text>
  85. <text :class="item.stock > 0 ? 'stock-normal' : 'stock-empty'">{{ item.stock }}</text>
  86. </view>
  87. </view>
  88. </view>
  89. </view>
  90. </view>
  91. <!-- 无任何层数据 -->
  92. <view v-if="currentFloors.length === 0" class="empty-container">
  93. <text class="empty-icon">📦</text>
  94. <text class="empty-main-text">暂无商品信息</text>
  95. <text class="empty-sub-text">该设备尚未配置商品</text>
  96. </view>
  97. </view>
  98. <!-- 底部占位,避免被固定栏遮挡 -->
  99. <view class="bottom-placeholder"></view>
  100. </view>
  101. <!-- 底部操作栏 -->
  102. <view v-if="!loading && !errorMsg" class="bottom-bar">
  103. <view class="btn-exit" @click="goBack">退出</view>
  104. <view class="btn-open" @click="handleOpenDoor">开门购物</view>
  105. </view>
  106. </view>
  107. </template>
  108. <script setup lang="ts">
  109. import { ref, computed } from 'vue';
  110. import { onLoad } from '@dcloudio/uni-app';
  111. import { getDeviceProducts, scanDoor } from '@/api/device';
  112. import type { FloorConfig } from '@/api/device';
  113. // 页面参数
  114. const deviceId = ref('');
  115. // 数据状态
  116. const loading = ref(true);
  117. const errorMsg = ref('');
  118. const templateName = ref('');
  119. const shelfNum = ref(0);
  120. const deviceType = ref(0);
  121. const leftFloors = ref<FloorConfig[]>([]);
  122. const rightFloors = ref<FloorConfig[]>([]);
  123. const opening = ref(false);
  124. // 双门柜切换
  125. const activeDoor = ref<'left' | 'right'>('left');
  126. // 是否双门柜
  127. const hasRightDoor = computed(() => rightFloors.value.length > 0);
  128. // 设备类型文字
  129. const deviceTypeText = computed(() => {
  130. return hasRightDoor.value ? '双门柜' : '单门柜';
  131. });
  132. // 当前显示的层数据
  133. const currentFloors = computed(() => {
  134. if (activeDoor.value === 'right' && hasRightDoor.value) {
  135. return rightFloors.value;
  136. }
  137. return leftFloors.value;
  138. });
  139. // 页面加载
  140. onLoad((options: any) => {
  141. if (options?.deviceId) {
  142. deviceId.value = options.deviceId;
  143. loadProducts();
  144. } else {
  145. errorMsg.value = '缺少设备ID参数';
  146. loading.value = false;
  147. }
  148. });
  149. // 加载商品数据
  150. const loadProducts = async () => {
  151. loading.value = true;
  152. errorMsg.value = '';
  153. try {
  154. const data = await getDeviceProducts(deviceId.value);
  155. templateName.value = data.templateName || '';
  156. shelfNum.value = data.shelfNum || 0;
  157. deviceType.value = data.deviceType || 0;
  158. leftFloors.value = data.leftFloors || [];
  159. rightFloors.value = data.rightFloors || [];
  160. } catch (error: any) {
  161. console.error('加载商品数据失败:', error);
  162. errorMsg.value = error.message || '加载失败,请稍后重试';
  163. } finally {
  164. loading.value = false;
  165. }
  166. };
  167. // 开门购物
  168. const handleOpenDoor = async () => {
  169. if (opening.value) return;
  170. opening.value = true;
  171. uni.showLoading({
  172. title: '正在开门...',
  173. mask: true
  174. });
  175. try {
  176. const response = await scanDoor(deviceId.value);
  177. uni.hideLoading();
  178. uni.showToast({
  179. title: '开门成功',
  180. icon: 'success'
  181. });
  182. // 存储设备信息
  183. uni.setStorageSync('currentDeviceId', response.deviceId);
  184. uni.setStorageSync('currentOutTradeNo', response.outTradeNo);
  185. uni.setStorageSync('currentOrderNo', response.orderNo);
  186. uni.removeStorageSync('shoppingPollingActive');
  187. // 跳转到购物页面
  188. setTimeout(() => {
  189. uni.redirectTo({
  190. url: '/pages/shopping/shopping'
  191. });
  192. }, 1000);
  193. } catch (error: any) {
  194. uni.hideLoading();
  195. opening.value = false;
  196. console.error('开门失败:', error);
  197. }
  198. };
  199. // 返回
  200. const goBack = () => {
  201. uni.navigateBack({
  202. fail: () => {
  203. uni.reLaunch({ url: '/pages/index/index' });
  204. }
  205. });
  206. };
  207. </script>
  208. <style lang="scss" scoped>
  209. .products-page {
  210. min-height: 100vh;
  211. background: #f5f5f5;
  212. }
  213. /* 加载状态 */
  214. .loading-container {
  215. display: flex;
  216. flex-direction: column;
  217. align-items: center;
  218. justify-content: center;
  219. height: 60vh;
  220. }
  221. .loading-spinner {
  222. width: 60rpx;
  223. height: 60rpx;
  224. border: 4rpx solid #e0e0e0;
  225. border-top-color: #FFD700;
  226. border-radius: 50%;
  227. animation: spin 0.8s linear infinite;
  228. }
  229. @keyframes spin {
  230. to { transform: rotate(360deg); }
  231. }
  232. .loading-text {
  233. margin-top: 20rpx;
  234. font-size: 28rpx;
  235. color: #999;
  236. }
  237. /* 错误状态 */
  238. .error-container {
  239. display: flex;
  240. flex-direction: column;
  241. align-items: center;
  242. justify-content: center;
  243. height: 60vh;
  244. padding: 0 60rpx;
  245. }
  246. .error-icon {
  247. width: 80rpx;
  248. height: 80rpx;
  249. line-height: 80rpx;
  250. text-align: center;
  251. background: #ff4d4f;
  252. color: #fff;
  253. border-radius: 50%;
  254. font-size: 40rpx;
  255. font-weight: bold;
  256. }
  257. .error-text {
  258. margin-top: 24rpx;
  259. font-size: 28rpx;
  260. color: #666;
  261. text-align: center;
  262. }
  263. .error-btn {
  264. margin-top: 40rpx;
  265. padding: 16rpx 60rpx;
  266. background: #FFD700;
  267. color: #333;
  268. border-radius: 40rpx;
  269. font-size: 28rpx;
  270. font-weight: bold;
  271. }
  272. /* 设备信息头部 */
  273. .device-header {
  274. background: linear-gradient(135deg, #FFD700, #FFA500);
  275. padding: 30rpx;
  276. color: #333;
  277. }
  278. .device-name {
  279. font-size: 36rpx;
  280. font-weight: bold;
  281. }
  282. .device-info {
  283. display: flex;
  284. gap: 16rpx;
  285. margin-top: 12rpx;
  286. }
  287. .info-tag {
  288. font-size: 24rpx;
  289. background: rgba(255,255,255,0.5);
  290. padding: 4rpx 16rpx;
  291. border-radius: 20rpx;
  292. color: #555;
  293. }
  294. /* 门Tab切换 */
  295. .door-tabs {
  296. display: flex;
  297. background: #fff;
  298. border-bottom: 1rpx solid #eee;
  299. }
  300. .door-tab {
  301. flex: 1;
  302. text-align: center;
  303. padding: 24rpx 0;
  304. font-size: 28rpx;
  305. color: #666;
  306. position: relative;
  307. }
  308. .door-tab.active {
  309. color: #333;
  310. font-weight: bold;
  311. }
  312. .door-tab.active::after {
  313. content: '';
  314. position: absolute;
  315. bottom: 0;
  316. left: 50%;
  317. transform: translateX(-50%);
  318. width: 60rpx;
  319. height: 4rpx;
  320. background: #FFD700;
  321. border-radius: 2rpx;
  322. }
  323. /* 楼层列表 */
  324. .floors-list {
  325. padding: 20rpx;
  326. }
  327. .floor-card {
  328. background: #fff;
  329. border-radius: 16rpx;
  330. margin-bottom: 20rpx;
  331. overflow: hidden;
  332. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.05);
  333. }
  334. .floor-header {
  335. display: flex;
  336. align-items: center;
  337. padding: 20rpx 24rpx;
  338. border-bottom: 1rpx solid #f0f0f0;
  339. }
  340. .floor-badge {
  341. width: 44rpx;
  342. height: 44rpx;
  343. line-height: 44rpx;
  344. text-align: center;
  345. background: #FFD700;
  346. color: #333;
  347. border-radius: 10rpx;
  348. font-size: 24rpx;
  349. font-weight: bold;
  350. margin-right: 16rpx;
  351. }
  352. .floor-title {
  353. font-size: 28rpx;
  354. font-weight: bold;
  355. color: #333;
  356. flex: 1;
  357. }
  358. .floor-count {
  359. font-size: 24rpx;
  360. color: #999;
  361. }
  362. /* 空楼层 */
  363. .empty-floor {
  364. padding: 40rpx 0;
  365. text-align: center;
  366. }
  367. .empty-text {
  368. font-size: 26rpx;
  369. color: #ccc;
  370. }
  371. /* 商品列表 */
  372. .goods-list {
  373. padding: 0 24rpx;
  374. }
  375. .goods-item {
  376. display: flex;
  377. padding: 20rpx 0;
  378. border-bottom: 1rpx solid #f5f5f5;
  379. }
  380. .goods-item:last-child {
  381. border-bottom: none;
  382. }
  383. /* 商品图片 */
  384. .goods-image-wrap {
  385. width: 140rpx;
  386. height: 140rpx;
  387. border-radius: 12rpx;
  388. overflow: hidden;
  389. flex-shrink: 0;
  390. background: #f9f9f9;
  391. }
  392. .goods-image {
  393. width: 100%;
  394. height: 100%;
  395. }
  396. .goods-image-placeholder {
  397. width: 100%;
  398. height: 100%;
  399. display: flex;
  400. align-items: center;
  401. justify-content: center;
  402. background: #f0f0f0;
  403. }
  404. .placeholder-text {
  405. font-size: 20rpx;
  406. color: #ccc;
  407. }
  408. /* 商品信息 */
  409. .goods-info {
  410. flex: 1;
  411. margin-left: 20rpx;
  412. display: flex;
  413. flex-direction: column;
  414. justify-content: center;
  415. }
  416. .goods-name {
  417. font-size: 28rpx;
  418. color: #333;
  419. font-weight: 500;
  420. line-height: 1.4;
  421. display: -webkit-box;
  422. -webkit-box-orient: vertical;
  423. -webkit-line-clamp: 2;
  424. overflow: hidden;
  425. }
  426. .goods-type {
  427. font-size: 22rpx;
  428. color: #999;
  429. margin-top: 6rpx;
  430. }
  431. .goods-price-row {
  432. display: flex;
  433. align-items: baseline;
  434. gap: 12rpx;
  435. margin-top: 8rpx;
  436. }
  437. .goods-price {
  438. font-size: 32rpx;
  439. color: #ff4d4f;
  440. font-weight: bold;
  441. }
  442. .goods-discount-price {
  443. font-size: 24rpx;
  444. color: #999;
  445. text-decoration: line-through;
  446. }
  447. .goods-stock {
  448. margin-top: 6rpx;
  449. font-size: 22rpx;
  450. }
  451. .stock-label {
  452. color: #999;
  453. }
  454. .stock-normal {
  455. color: #52c41a;
  456. }
  457. .stock-empty {
  458. color: #ff4d4f;
  459. }
  460. /* 无数据 */
  461. .empty-container {
  462. display: flex;
  463. flex-direction: column;
  464. align-items: center;
  465. padding: 80rpx 0;
  466. }
  467. .empty-icon {
  468. font-size: 80rpx;
  469. }
  470. .empty-main-text {
  471. margin-top: 20rpx;
  472. font-size: 30rpx;
  473. color: #333;
  474. font-weight: bold;
  475. }
  476. .empty-sub-text {
  477. margin-top: 8rpx;
  478. font-size: 26rpx;
  479. color: #999;
  480. }
  481. /* 底部占位 */
  482. .bottom-placeholder {
  483. height: 140rpx;
  484. }
  485. /* 底部操作栏 */
  486. .bottom-bar {
  487. position: fixed;
  488. bottom: 0;
  489. left: 0;
  490. right: 0;
  491. display: flex;
  492. align-items: center;
  493. gap: 20rpx;
  494. padding: 20rpx 30rpx;
  495. padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
  496. background: #fff;
  497. box-shadow: 0 -2rpx 10rpx rgba(0,0,0,0.08);
  498. }
  499. .btn-exit {
  500. flex: 1;
  501. text-align: center;
  502. padding: 22rpx 0;
  503. border-radius: 44rpx;
  504. font-size: 30rpx;
  505. font-weight: bold;
  506. color: #666;
  507. background: #f0f0f0;
  508. }
  509. .btn-open {
  510. flex: 2;
  511. text-align: center;
  512. padding: 22rpx 0;
  513. border-radius: 44rpx;
  514. font-size: 30rpx;
  515. font-weight: bold;
  516. color: #333;
  517. background: linear-gradient(135deg, #FFD700, #FFC107);
  518. box-shadow: 0 4rpx 12rpx rgba(255, 215, 0, 0.4);
  519. }
  520. </style>