products.vue 18 KB

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