products.vue 14 KB

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