products.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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>
  76. </view>
  77. </view>
  78. </view>
  79. <!-- 无任何层数据 -->
  80. <view v-if="currentFloors.length === 0" class="empty-container">
  81. <text class="empty-icon">📦</text>
  82. <text class="empty-main-text">暂无商品信息</text>
  83. <text class="empty-sub-text">该设备尚未配置商品</text>
  84. </view>
  85. </view>
  86. <!-- 底部占位,避免被固定栏遮挡 -->
  87. <view class="bottom-placeholder"></view>
  88. </view>
  89. <!-- 底部操作栏 -->
  90. <view v-if="!loading && !errorMsg" class="bottom-bar">
  91. <view class="bottom-trust">
  92. <BrandSlogan type="standard" :compact="true" />
  93. </view>
  94. <view class="bottom-actions">
  95. <view class="btn-back" @click="goBack">返回</view>
  96. <view class="btn-open" @click="handleOpenDoor">
  97. {{ opening ? '处理中...' : '开门购物' }}
  98. </view>
  99. </view>
  100. </view>
  101. </view>
  102. </template>
  103. <script setup lang="ts">
  104. import { ref, computed } from 'vue';
  105. import { onLoad } from '@dcloudio/uni-app';
  106. import { API_CONFIG } from '@/utils/config';
  107. import { getDeviceProducts, scanDoor } from '@/api/device';
  108. import { queryPayscoreByOutNo } from '@/api/payscore';
  109. import { isLoggedIn, getToken } from '@/utils/auth';
  110. import { logger } from '@/utils/logger';
  111. import BrandSlogan from '@/components/BrandSlogan.vue';
  112. import type { FloorConfig } from '@/api/device';
  113. const deviceId = ref('');
  114. const loading = ref(true);
  115. const errorMsg = ref('');
  116. const templateName = ref('');
  117. const shelfNum = ref(0);
  118. const deviceType = ref(0);
  119. const leftFloors = ref<FloorConfig[]>([]);
  120. const rightFloors = ref<FloorConfig[]>([]);
  121. const opening = ref(false);
  122. let pollingOutOrderNo = '';
  123. let pollTimer: ReturnType<typeof setInterval> | null = null;
  124. const activeDoor = ref<'left' | 'right'>('left');
  125. const hasRightDoor = computed(() => rightFloors.value.length > 0);
  126. const currentFloors = computed(() => {
  127. if (activeDoor.value === 'right' && hasRightDoor.value) {
  128. return rightFloors.value;
  129. }
  130. return leftFloors.value;
  131. });
  132. onLoad((options: any) => {
  133. let id = '';
  134. if (options?.deviceId) {
  135. id = options.deviceId;
  136. } else if (options?.q) {
  137. try {
  138. const url = decodeURIComponent(options.q);
  139. logger.log('扫码链接:', url);
  140. const match = url.match(/\/([A-Za-z0-9]+)(?:\?|$)/);
  141. if (match && match[1]) {
  142. id = match[1];
  143. }
  144. } catch (e) {
  145. logger.error('解析扫码链接失败:', e);
  146. }
  147. }
  148. if (id) {
  149. logger.log('设备ID:', id);
  150. deviceId.value = id;
  151. loadProducts();
  152. } else {
  153. errorMsg.value = '缺少设备ID参数';
  154. loading.value = false;
  155. }
  156. });
  157. const loadProducts = async () => {
  158. loading.value = true;
  159. errorMsg.value = '';
  160. try {
  161. const data = await getDeviceProducts(deviceId.value);
  162. templateName.value = data.templateName || '';
  163. shelfNum.value = data.shelfNum || 0;
  164. deviceType.value = data.deviceType || 0;
  165. leftFloors.value = data.leftFloors || [];
  166. rightFloors.value = data.rightFloors || [];
  167. } catch (error: any) {
  168. logger.error('加载商品数据失败:', error);
  169. errorMsg.value = error.message || '加载失败,请稍后重试';
  170. } finally {
  171. loading.value = false;
  172. }
  173. };
  174. const handleOpenDoor = () => {
  175. if (opening.value) return;
  176. if (!isLoggedIn()) {
  177. uni.showModal({
  178. title: '提示',
  179. content: '请先登录后再开门购物',
  180. showCancel: false,
  181. success: () => {
  182. uni.reLaunch({
  183. url: '/pages/login/login?redirect=' + encodeURIComponent('/pages/products/products?deviceId=' + deviceId.value)
  184. });
  185. }
  186. });
  187. return;
  188. }
  189. opening.value = true;
  190. uni.showLoading({ title: '准备中...', mask: true });
  191. // 使用 uni.request 回调方式,避免 async/await 打破手势链
  192. uni.request({
  193. url: API_CONFIG.baseUrl + '/payscore/pre-create',
  194. method: 'POST',
  195. header: {
  196. 'Content-Type': 'application/json',
  197. 'accessToken': getToken()
  198. },
  199. data: { deviceId: deviceId.value },
  200. success: (apiRes: any) => {
  201. uni.hideLoading();
  202. const body = apiRes.data;
  203. if (body.code !== 200 && body.code !== 0) {
  204. opening.value = false;
  205. uni.showToast({ title: body.message || '创建失败', icon: 'none' });
  206. return;
  207. }
  208. const data = body.data;
  209. if (!data || !data.package) {
  210. opening.value = false;
  211. uni.showToast({ title: data?.errorMsg || '创建支付分订单失败', icon: 'none' });
  212. return;
  213. }
  214. logger.log('[商品页] 预创建成功,直接拉起官方授权页');
  215. pollingOutOrderNo = data.outOrderNo;
  216. // 在原生回调中调用 openBusinessView,保持在手势链内
  217. wx.openBusinessView({
  218. businessType: 'wxpayScoreUse',
  219. extraData: {
  220. package: data.package,
  221. },
  222. success: () => {
  223. logger.log('[商品页] 从授权页返回,轮询确认状态');
  224. startPolling();
  225. },
  226. fail: (err: any) => {
  227. opening.value = false;
  228. const errMsg = err?.errMsg || JSON.stringify(err);
  229. logger.error('[商品页] openBusinessView fail:', errMsg);
  230. // 用户主动取消,静默处理
  231. if (errMsg.includes('cancel')) {
  232. return;
  233. }
  234. // 根据错误原因给出不同提示
  235. const lowScore = errMsg.includes('分') || errMsg.includes('credit') || errMsg.includes('score');
  236. const notEnabled = errMsg.includes('未开通') || errMsg.includes('未授权') || errMsg.includes('not signed') || errMsg.includes('not enabled');
  237. if (lowScore) {
  238. uni.showModal({
  239. title: '支付分不足',
  240. content: '您的微信支付分未达到使用门槛(550分),暂无法使用本服务。\n\n请保持良好的信用记录,后续再试。',
  241. showCancel: false,
  242. confirmText: '我知道了',
  243. });
  244. } else if (notEnabled) {
  245. uni.showModal({
  246. title: '未开通微信支付分',
  247. content: '您尚未开通微信支付分,请先开通后再使用本服务。',
  248. showCancel: false,
  249. confirmText: '去开通',
  250. success: (res: any) => {
  251. if (res.confirm) {
  252. uni.navigateTo({ url: '/pages/payscore/enable' });
  253. }
  254. },
  255. });
  256. } else {
  257. uni.showModal({
  258. title: '调起支付分失败',
  259. content: '支付分服务暂不可用,请稍后重试。',
  260. showCancel: false,
  261. confirmText: '我知道了',
  262. });
  263. }
  264. },
  265. });
  266. },
  267. fail: (err: any) => {
  268. uni.hideLoading();
  269. opening.value = false;
  270. logger.error('[商品页] API 请求失败', err);
  271. uni.showToast({ title: '网络异常,请重试', icon: 'none' });
  272. }
  273. });
  274. };
  275. const startPolling = () => {
  276. uni.showLoading({ title: '确认结果...', mask: true });
  277. let attempts = 0;
  278. const maxAttempts = 10;
  279. if (pollTimer) clearInterval(pollTimer);
  280. pollTimer = setInterval(async () => {
  281. attempts++;
  282. try {
  283. const res = await queryPayscoreByOutNo(pollingOutOrderNo);
  284. const state = res?.state;
  285. logger.log(`[商品页] 轮询第 ${attempts} 次, state:`, state);
  286. if (state === 'DOING') {
  287. if (pollTimer) { clearInterval(pollTimer); pollTimer = null; }
  288. uni.hideLoading();
  289. opening.value = false;
  290. logger.log('[商品页] 用户已确认,开门');
  291. doOpenDoor();
  292. return;
  293. }
  294. if (attempts >= maxAttempts) {
  295. if (pollTimer) { clearInterval(pollTimer); pollTimer = null; }
  296. uni.hideLoading();
  297. opening.value = false;
  298. logger.log('[商品页] 轮询超时,用户未确认');
  299. }
  300. } catch (e) {
  301. logger.error('[商品页] 轮询异常', e);
  302. if (attempts >= maxAttempts) {
  303. if (pollTimer) { clearInterval(pollTimer); pollTimer = null; }
  304. uni.hideLoading();
  305. opening.value = false;
  306. }
  307. }
  308. }, 1000);
  309. };
  310. const doOpenDoor = () => {
  311. uni.showLoading({ title: '正在开门...', mask: true });
  312. scanDoor(deviceId.value).then(response => {
  313. uni.hideLoading();
  314. uni.showToast({ title: '开门成功', icon: 'success' });
  315. uni.setStorageSync('currentDeviceId', response.deviceId);
  316. uni.setStorageSync('currentOutTradeNo', response.outTradeNo);
  317. uni.setStorageSync('currentOrderNo', response.orderNo);
  318. uni.removeStorageSync('shoppingPollingActive');
  319. setTimeout(() => {
  320. uni.redirectTo({ url: '/pages/shopping/shopping' });
  321. }, 1000);
  322. }).catch((error: any) => {
  323. uni.hideLoading();
  324. opening.value = false;
  325. logger.error('开门失败:', error);
  326. });
  327. };
  328. const goBack = () => {
  329. if (!isLoggedIn()) {
  330. uni.reLaunch({ url: '/pages/login/login' });
  331. return;
  332. }
  333. uni.navigateBack({
  334. fail: () => {
  335. uni.reLaunch({ url: '/pages/index/index' });
  336. }
  337. });
  338. };
  339. </script>
  340. <style lang="scss" scoped>
  341. .products-page {
  342. min-height: 100vh;
  343. background: $color-bg-secondary;
  344. }
  345. /* 加载状态 */
  346. .loading-container {
  347. display: flex;
  348. flex-direction: column;
  349. align-items: center;
  350. justify-content: center;
  351. height: 60vh;
  352. }
  353. .loading-spinner {
  354. width: 60rpx;
  355. height: 60rpx;
  356. border: 4rpx solid $color-border;
  357. border-top-color: $color-primary;
  358. border-radius: 50%;
  359. animation: spin 0.8s linear infinite;
  360. }
  361. @keyframes spin {
  362. to { transform: rotate(360deg); }
  363. }
  364. .loading-text {
  365. margin-top: $spacing-sm;
  366. font-size: 28rpx;
  367. color: $color-text-secondary;
  368. }
  369. /* 错误状态 */
  370. .error-container {
  371. display: flex;
  372. flex-direction: column;
  373. align-items: center;
  374. justify-content: center;
  375. height: 60vh;
  376. padding: 0 60rpx;
  377. }
  378. .error-icon {
  379. width: 80rpx;
  380. height: 80rpx;
  381. line-height: 80rpx;
  382. text-align: center;
  383. background: $color-error;
  384. color: $color-bg-primary;
  385. border-radius: 50%;
  386. font-size: 40rpx;
  387. font-weight: 700;
  388. }
  389. .error-text {
  390. margin-top: $spacing-md;
  391. font-size: 28rpx;
  392. color: $color-text-secondary;
  393. text-align: center;
  394. }
  395. .error-btn {
  396. margin-top: $spacing-xl;
  397. padding: $spacing-sm 60rpx;
  398. background: $color-primary;
  399. color: $color-text-primary;
  400. border-radius: $radius-xl;
  401. font-size: 28rpx;
  402. font-weight: 600;
  403. }
  404. /* 门Tab切换 */
  405. .door-tabs {
  406. display: flex;
  407. background: $color-bg-primary;
  408. border-bottom: 1rpx solid $color-border;
  409. }
  410. .door-tab {
  411. flex: 1;
  412. text-align: center;
  413. padding: 24rpx 0;
  414. font-size: 28rpx;
  415. color: $color-text-secondary;
  416. position: relative;
  417. }
  418. .door-tab.active {
  419. color: $color-text-primary;
  420. font-weight: 600;
  421. &::after {
  422. content: '';
  423. position: absolute;
  424. bottom: 0;
  425. left: 50%;
  426. transform: translateX(-50%);
  427. width: 60rpx;
  428. height: 4rpx;
  429. background: $color-primary;
  430. border-radius: 2rpx;
  431. }
  432. }
  433. /* 楼层列表 */
  434. .floors-list {
  435. padding: $spacing-sm;
  436. }
  437. .floor-card {
  438. background: $color-bg-primary;
  439. border-radius: $radius-md;
  440. margin-bottom: $spacing-sm;
  441. overflow: hidden;
  442. box-shadow: $shadow-sm;
  443. }
  444. .floor-header {
  445. display: flex;
  446. align-items: center;
  447. padding: 20rpx 24rpx;
  448. border-bottom: 1rpx solid $color-border;
  449. }
  450. .floor-badge {
  451. width: 44rpx;
  452. height: 44rpx;
  453. line-height: 44rpx;
  454. text-align: center;
  455. background: $color-primary;
  456. color: $color-text-primary;
  457. border-radius: $radius-sm;
  458. font-size: 24rpx;
  459. font-weight: 600;
  460. margin-right: $spacing-sm;
  461. }
  462. .floor-title {
  463. font-size: 28rpx;
  464. font-weight: 600;
  465. color: $color-text-primary;
  466. flex: 1;
  467. }
  468. .floor-count {
  469. font-size: 24rpx;
  470. color: $color-text-secondary;
  471. }
  472. /* 空楼层 */
  473. .empty-floor {
  474. padding: 40rpx 0;
  475. text-align: center;
  476. }
  477. .empty-text {
  478. font-size: 26rpx;
  479. color: $color-text-tertiary;
  480. }
  481. /* 商品列表 */
  482. .goods-list {
  483. padding: 0 24rpx;
  484. }
  485. .goods-item {
  486. display: flex;
  487. padding: 20rpx 0;
  488. border-bottom: 1rpx solid $color-bg-secondary;
  489. &:last-child {
  490. border-bottom: none;
  491. }
  492. }
  493. /* 商品图片 */
  494. .goods-image-wrap {
  495. width: 140rpx;
  496. height: 140rpx;
  497. border-radius: 12rpx;
  498. overflow: hidden;
  499. flex-shrink: 0;
  500. background: $color-bg-tertiary;
  501. }
  502. .goods-image {
  503. width: 100%;
  504. height: 100%;
  505. }
  506. .goods-image-placeholder {
  507. width: 100%;
  508. height: 100%;
  509. display: flex;
  510. align-items: center;
  511. justify-content: center;
  512. background: $color-bg-tertiary;
  513. }
  514. .placeholder-text {
  515. font-size: 20rpx;
  516. color: $color-text-tertiary;
  517. }
  518. /* 商品信息 */
  519. .goods-info {
  520. flex: 1;
  521. margin-left: 20rpx;
  522. display: flex;
  523. flex-direction: column;
  524. justify-content: center;
  525. }
  526. .goods-name {
  527. font-size: 28rpx;
  528. color: $color-text-primary;
  529. font-weight: 500;
  530. line-height: 1.4;
  531. display: -webkit-box;
  532. -webkit-box-orient: vertical;
  533. -webkit-line-clamp: 2;
  534. overflow: hidden;
  535. }
  536. .goods-type {
  537. font-size: 22rpx;
  538. color: $color-text-secondary;
  539. margin-top: 6rpx;
  540. }
  541. .goods-price-row {
  542. display: flex;
  543. align-items: baseline;
  544. gap: 12rpx;
  545. margin-top: 8rpx;
  546. }
  547. .goods-price {
  548. font-size: 32rpx;
  549. color: $color-error;
  550. font-weight: 600;
  551. }
  552. .goods-discount-price {
  553. font-size: 24rpx;
  554. color: $color-text-secondary;
  555. text-decoration: line-through;
  556. }
  557. /* 无数据 */
  558. .empty-container {
  559. display: flex;
  560. flex-direction: column;
  561. align-items: center;
  562. padding: $spacing-xxl 0;
  563. }
  564. .empty-icon {
  565. font-size: 80rpx;
  566. }
  567. .empty-main-text {
  568. margin-top: $spacing-md;
  569. font-size: 28rpx;
  570. color: $color-text-primary;
  571. font-weight: 600;
  572. }
  573. .empty-sub-text {
  574. margin-top: $spacing-xs;
  575. font-size: 24rpx;
  576. color: $color-text-secondary;
  577. }
  578. /* 底部占位 */
  579. .bottom-placeholder {
  580. height: 180rpx;
  581. }
  582. /* 底部操作栏 */
  583. .bottom-bar {
  584. position: fixed;
  585. bottom: 0;
  586. left: 0;
  587. right: 0;
  588. padding: $spacing-md 30rpx;
  589. padding-bottom: calc($spacing-md + env(safe-area-inset-bottom));
  590. background: $color-bg-primary;
  591. box-shadow: 0 -2rpx 16rpx rgba(0, 0, 0, 0.04);
  592. }
  593. .bottom-trust {
  594. display: flex;
  595. justify-content: center;
  596. margin-bottom: $spacing-sm;
  597. }
  598. .bottom-actions {
  599. display: flex;
  600. gap: $spacing-sm;
  601. }
  602. .btn-back {
  603. flex: 2;
  604. text-align: center;
  605. padding: 22rpx 0;
  606. border-radius: $radius-xl;
  607. font-size: 28rpx;
  608. font-weight: 500;
  609. color: $color-text-secondary;
  610. background: $color-bg-tertiary;
  611. &:active {
  612. background: $color-border;
  613. }
  614. }
  615. .btn-open {
  616. flex: 3;
  617. text-align: center;
  618. padding: 22rpx 0;
  619. border-radius: $radius-xl;
  620. font-size: 30rpx;
  621. font-weight: 600;
  622. color: $color-text-primary;
  623. background: linear-gradient(135deg, $color-primary, $color-primary-dark);
  624. box-shadow: 0 6rpx 20rpx rgba(255, 160, 0, 0.3);
  625. animation: btn-breathe 2.4s $ease-in-out infinite;
  626. &:active {
  627. opacity: 0.9;
  628. transform: scale(0.98);
  629. animation: none;
  630. }
  631. }
  632. @keyframes btn-breathe {
  633. 0%, 100% {
  634. transform: scale(1);
  635. }
  636. 50% {
  637. transform: scale(1.04);
  638. }
  639. }
  640. </style>