index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. <template>
  2. <view class="container">
  3. <!-- 顶部品牌区 -->
  4. <view class="brand-section">
  5. <text class="brand-title">AI零售柜</text>
  6. <text class="brand-slogan">智能视觉 · 即拿即走</text>
  7. </view>
  8. <!-- 核心操作区 -->
  9. <view class="action-section">
  10. <button class="scan-button" @click="scanCode">
  11. <view class="scan-button-inner">
  12. <image class="scan-icon" src="/static/icons/scan.svg" mode="aspectFill"></image>
  13. <text class="scan-text">扫码开门</text>
  14. </view>
  15. <view class="scan-button-ripple"></view>
  16. </button>
  17. </view>
  18. <!-- 快捷功能区 -->
  19. <view class="quick-actions">
  20. <view class="quick-action-item" @click="goToMy">
  21. <view class="action-icon">
  22. <image src="/static/icons/my.svg" mode="aspectFit"></image>
  23. </view>
  24. <text class="action-label">我的</text>
  25. </view>
  26. <view class="quick-action-item" @click="goToOrders">
  27. <view class="action-icon">
  28. <image src="/static/icons/orders.svg" mode="aspectFit"></image>
  29. </view>
  30. <text class="action-label">订单</text>
  31. </view>
  32. <view class="quick-action-item" @click="goToCouponCenter">
  33. <view class="action-icon">
  34. <image src="/static/icons/coupon.svg" mode="aspectFit"></image>
  35. </view>
  36. <text class="action-label">优惠券</text>
  37. </view>
  38. </view>
  39. <!-- 底部信息卡片 -->
  40. <view class="info-card">
  41. <view class="info-item">
  42. <image class="info-icon payscore-icon" src="/static/icons/pay-socre-logo.png" mode="aspectFit"></image>
  43. <text class="info-text">微信支付分 | 550分及以上优享</text>
  44. </view>
  45. <view class="info-item">
  46. <CustomerServiceButton
  47. mode="inline"
  48. title="首页-联系客服"
  49. :path="'/pages/index/index'"
  50. @contact="onContactSuccess"
  51. >
  52. <text class="info-icon">💬</text>
  53. <text class="info-text">在线客服</text>
  54. </CustomerServiceButton>
  55. </view>
  56. </view>
  57. <!-- 支付分授权确认弹窗 -->
  58. <view v-if="showAuthPopup" class="auth-overlay" @click.stop>
  59. <view class="auth-card">
  60. <view class="auth-icon-wrap">
  61. <image class="auth-icon" src="/static/icons/pay-socre-logo.png" mode="aspectFit"></image>
  62. </view>
  63. <text class="auth-title">微信支付分授权</text>
  64. <text class="auth-desc">需授权微信支付分后开门购物,费用离店后自动结算</text>
  65. <view class="auth-actions">
  66. <button class="auth-btn cancel" @click="cancelAuth">取消</button>
  67. <button class="auth-btn confirm" @click="invokePayScore">确认授权</button>
  68. </view>
  69. </view>
  70. </view>
  71. </view>
  72. </template>
  73. <script setup lang="ts">
  74. import { ref } from 'vue'
  75. import { onShow } from '@dcloudio/uni-app'
  76. import { scanDoor } from '../../api/device'
  77. import { preCreatePayscoreOrder } from '../../api/payscore'
  78. import { isLoggedIn, getToken } from '../../utils/auth'
  79. import { logger } from '../../utils/logger'
  80. // 授权弹窗状态
  81. const showAuthPopup = ref(false)
  82. const authDeviceId = ref('')
  83. const authPackage = ref('')
  84. const authMchId = ref('')
  85. const authLoading = ref(false)
  86. // 页面显示时检查 token
  87. onShow(() => {
  88. const token = getToken()
  89. logger.log('[首页 onShow] token 状态:', token ? '存在' : '不存在')
  90. })
  91. const scanCode = async () => {
  92. // 检查登录状态
  93. if (!isLoggedIn()) {
  94. uni.showModal({
  95. title: '提示',
  96. content: '请先登录后再扫码开门',
  97. showCancel: false,
  98. success: () => {
  99. uni.reLaunch({
  100. url: '/pages/login/login?redirect=/pages/index/index'
  101. });
  102. }
  103. });
  104. return;
  105. }
  106. // 调用摄像头扫码
  107. uni.scanCode({
  108. success: async function (res) {
  109. logger.log('扫码结果:', res.result);
  110. let deviceId = '';
  111. try {
  112. const urlPattern = /\/([A-Z0-9]+)(\?|$)/;
  113. const match = res.result.match(urlPattern);
  114. if (match && match[1]) {
  115. deviceId = match[1];
  116. } else {
  117. try {
  118. const qrData = JSON.parse(res.result);
  119. if (qrData.deviceId) deviceId = qrData.deviceId;
  120. } catch (e) {
  121. deviceId = res.result;
  122. }
  123. }
  124. if (!deviceId) {
  125. throw new Error('无法解析设备ID');
  126. }
  127. } catch (error) {
  128. logger.error('解析设备ID失败:', error);
  129. uni.showToast({ title: '二维码格式错误', icon: 'none' });
  130. return;
  131. }
  132. logger.log('提取到设备ID:', deviceId);
  133. // 预创建支付分订单,获取 package
  134. await preparePayScoreAuth(deviceId);
  135. },
  136. fail: function (err) {
  137. logger.log('扫码取消:', err);
  138. }
  139. });
  140. };
  141. /**
  142. * 预创建支付分订单,成功后在页面内展示授权按钮
  143. */
  144. const preparePayScoreAuth = async (deviceId: string) => {
  145. uni.showLoading({ title: '准备中...', mask: true });
  146. try {
  147. const result = await preCreatePayscoreOrder({ deviceId });
  148. uni.hideLoading();
  149. if (!result.success || !result.package) {
  150. uni.showToast({
  151. title: result.errorMsg || '创建支付分订单失败',
  152. icon: 'none',
  153. });
  154. return;
  155. }
  156. logger.log('[支付分] 预创建成功, package:', result.package);
  157. // 存到状态中,等用户点击"确认授权"按钮
  158. authDeviceId.value = deviceId;
  159. authPackage.value = result.package;
  160. authMchId.value = result.mchId || '';
  161. showAuthPopup.value = true;
  162. } catch (error: any) {
  163. uni.hideLoading();
  164. logger.error('[支付分] 预创建失败', error);
  165. uni.showToast({
  166. title: error?.message || '创建支付分订单失败',
  167. icon: 'none',
  168. });
  169. }
  170. };
  171. /**
  172. * 用户点击"确认授权"按钮,直接触发 wx.openBusinessView
  173. * 关键:此调用在用户点击事件的同步调用链中,不会被微信拦截
  174. */
  175. const invokePayScore = () => {
  176. if (authLoading.value) return;
  177. authLoading.value = true;
  178. logger.log('[支付分] 用户点击确认授权,调起 openBusinessView');
  179. wx.openBusinessView({
  180. businessType: 'payscore',
  181. extraData: {
  182. mch_id: authMchId.value,
  183. package: authPackage.value,
  184. },
  185. success: async () => {
  186. authLoading.value = false;
  187. showAuthPopup.value = false;
  188. logger.log('[支付分] 用户确认成功,开始开门');
  189. await doOpenDoor(authDeviceId.value);
  190. },
  191. fail: (err: any) => {
  192. authLoading.value = false;
  193. logger.error('[支付分] openBusinessView 失败', JSON.stringify(err));
  194. // 用户取消不弹窗,真正的错误才提示
  195. if (err && err.errMsg && err.errMsg.indexOf('cancel') === -1) {
  196. uni.showModal({
  197. title: '授权失败',
  198. content: JSON.stringify(err),
  199. showCancel: false,
  200. });
  201. }
  202. },
  203. });
  204. };
  205. /**
  206. * 用户取消授权
  207. */
  208. const cancelAuth = () => {
  209. showAuthPopup.value = false;
  210. logger.log('[支付分] 用户取消授权');
  211. };
  212. /**
  213. * 执行开门操作
  214. */
  215. const doOpenDoor = async (deviceId: string) => {
  216. uni.showLoading({ title: '正在开门...', mask: true });
  217. try {
  218. const response = await scanDoor(deviceId);
  219. uni.hideLoading();
  220. uni.showToast({ title: '开门成功', icon: 'success' });
  221. uni.setStorageSync('currentDeviceId', response.deviceId);
  222. uni.setStorageSync('currentOutTradeNo', response.outTradeNo);
  223. uni.setStorageSync('currentOrderNo', response.orderNo);
  224. uni.removeStorageSync('shoppingPollingActive');
  225. setTimeout(() => {
  226. uni.navigateTo({ url: '/pages/shopping/shopping' });
  227. }, 1000);
  228. } catch (error: any) {
  229. uni.hideLoading();
  230. logger.error('开门失败:', error);
  231. }
  232. };
  233. const goToMy = () => {
  234. uni.navigateTo({ url: '/pages/my/my' })
  235. }
  236. const goToOrders = () => {
  237. uni.navigateTo({ url: '/pages/orders/orders' })
  238. }
  239. const goToCouponCenter = () => {
  240. uni.navigateTo({ url: '/pages/couponCenter/couponCenter' })
  241. }
  242. const onContactSuccess = () => {
  243. logger.log('[首页] 客服会话已打开')
  244. }
  245. </script>
  246. <style lang="scss">
  247. .container {
  248. min-height: 100vh;
  249. background: $color-bg-secondary;
  250. display: flex;
  251. flex-direction: column;
  252. padding: $spacing-xxl $spacing-lg;
  253. padding-bottom: calc(100rpx + constant(safe-area-inset-bottom));
  254. padding-bottom: calc(100rpx + env(safe-area-inset-bottom));
  255. box-sizing: border-box;
  256. }
  257. /* ========== 品牌区 ========== */
  258. .brand-section {
  259. text-align: center;
  260. padding: $spacing-xxl 0 $spacing-xl;
  261. animation: slideUp 0.3s ease;
  262. .brand-title {
  263. font-size: 56rpx;
  264. font-weight: 300;
  265. color: $color-text-primary;
  266. letter-spacing: 8rpx;
  267. display: block;
  268. margin-bottom: $spacing-sm;
  269. }
  270. .brand-slogan {
  271. font-size: 24rpx;
  272. color: $color-text-secondary;
  273. letter-spacing: 4rpx;
  274. }
  275. }
  276. /* ========== 核心操作区 ========== */
  277. .action-section {
  278. display: flex;
  279. justify-content: center;
  280. align-items: center;
  281. padding: $spacing-xl 0;
  282. }
  283. .scan-button {
  284. position: relative;
  285. width: 400rpx;
  286. height: 400rpx;
  287. min-width: 400rpx;
  288. min-height: 400rpx;
  289. border-radius: 50%;
  290. background: transparent;
  291. border: none;
  292. padding: 0;
  293. margin: 0;
  294. overflow: hidden;
  295. &::after {
  296. border: none;
  297. }
  298. &-inner {
  299. width: 100%;
  300. height: 100%;
  301. border-radius: 50%;
  302. background: linear-gradient(135deg, $color-primary-light 0%, $color-primary 100%);
  303. box-shadow: $shadow-primary;
  304. display: flex;
  305. flex-direction: column;
  306. align-items: center;
  307. justify-content: center;
  308. transition: all $duration-normal $ease-out;
  309. &:active {
  310. transform: scale(0.95);
  311. box-shadow: 0 4rpx 16rpx rgba(255, 193, 7, 0.3);
  312. }
  313. }
  314. &-ripple {
  315. position: absolute;
  316. top: 50%;
  317. left: 50%;
  318. width: 100%;
  319. height: 100%;
  320. border-radius: 50%;
  321. background: $color-primary;
  322. transform: translate(-50%, -50%);
  323. animation: pulse 3s cubic-bezier(0.42, 0, 0.58, 1) infinite;
  324. opacity: 0.2;
  325. z-index: -1;
  326. }
  327. }
  328. .scan-icon {
  329. width: 160rpx;
  330. height: 160rpx;
  331. margin-bottom: $spacing-md;
  332. flex-shrink: 0;
  333. display: block;
  334. }
  335. .scan-text {
  336. font-size: 40rpx;
  337. font-weight: 600;
  338. color: $color-text-primary;
  339. letter-spacing: 4rpx;
  340. }
  341. /* ========== 快捷功能区 ========== */
  342. .quick-actions {
  343. display: flex;
  344. justify-content: center;
  345. align-items: center;
  346. gap: $spacing-xl;
  347. margin: $spacing-xxl 0;
  348. &-item {
  349. display: flex;
  350. flex-direction: column;
  351. align-items: center;
  352. justify-content: center;
  353. padding: $spacing-md;
  354. position: relative;
  355. flex: 0 0 auto;
  356. &:active { opacity: 0.8; }
  357. }
  358. .action-icon {
  359. width: 120rpx;
  360. height: 120rpx;
  361. background: $color-bg-primary;
  362. border-radius: $radius-lg;
  363. display: flex;
  364. align-items: center;
  365. justify-content: center;
  366. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.06);
  367. margin-bottom: $spacing-sm;
  368. border: 2rpx solid $color-border;
  369. flex-shrink: 0;
  370. image {
  371. width: 64rpx;
  372. height: 64rpx;
  373. display: block;
  374. }
  375. &:active {
  376. box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.1);
  377. border-color: $color-primary-light;
  378. }
  379. }
  380. .action-label {
  381. font-size: 28rpx;
  382. color: $color-text-primary;
  383. font-weight: 500;
  384. text-align: center;
  385. display: block;
  386. width: auto;
  387. }
  388. }
  389. /* ========== 底部信息卡片 ========== */
  390. .info-card {
  391. background: $color-bg-primary;
  392. border-radius: $radius-lg;
  393. padding: $spacing-lg;
  394. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.04);
  395. margin-top: auto;
  396. width: 100%;
  397. box-sizing: border-box;
  398. .info-item {
  399. display: flex;
  400. align-items: center;
  401. justify-content: center;
  402. padding: $spacing-sm 0;
  403. min-height: 60rpx;
  404. &:not(:last-child) {
  405. border-bottom: 1rpx solid $color-border;
  406. padding-bottom: $spacing-md;
  407. margin-bottom: $spacing-md;
  408. }
  409. }
  410. .info-icon {
  411. font-size: 28rpx;
  412. margin-right: $spacing-sm;
  413. flex-shrink: 0;
  414. line-height: 1;
  415. &.payscore-icon {
  416. width: 36rpx;
  417. height: 36rpx;
  418. }
  419. }
  420. .info-text {
  421. font-size: 24rpx;
  422. color: $color-text-secondary;
  423. line-height: 1.5;
  424. text-align: center;
  425. }
  426. }
  427. /* ========== 授权弹窗 ========== */
  428. .auth-overlay {
  429. position: fixed;
  430. top: 0;
  431. left: 0;
  432. right: 0;
  433. bottom: 0;
  434. background: rgba(0, 0, 0, 0.5);
  435. display: flex;
  436. align-items: center;
  437. justify-content: center;
  438. z-index: 999;
  439. animation: fadeIn 0.2s ease;
  440. }
  441. .auth-card {
  442. width: 600rpx;
  443. background: #fff;
  444. border-radius: 24rpx;
  445. padding: 60rpx 40rpx 40rpx;
  446. display: flex;
  447. flex-direction: column;
  448. align-items: center;
  449. animation: scaleIn 0.25s ease;
  450. }
  451. .auth-icon-wrap {
  452. width: 100rpx;
  453. height: 100rpx;
  454. margin-bottom: 24rpx;
  455. }
  456. .auth-icon {
  457. width: 100%;
  458. height: 100%;
  459. }
  460. .auth-title {
  461. font-size: 36rpx;
  462. font-weight: 600;
  463. color: $color-text-primary;
  464. margin-bottom: 16rpx;
  465. }
  466. .auth-desc {
  467. font-size: 26rpx;
  468. color: $color-text-secondary;
  469. text-align: center;
  470. line-height: 1.6;
  471. margin-bottom: 40rpx;
  472. }
  473. .auth-actions {
  474. display: flex;
  475. gap: 24rpx;
  476. width: 100%;
  477. }
  478. .auth-btn {
  479. flex: 1;
  480. height: 88rpx;
  481. line-height: 88rpx;
  482. text-align: center;
  483. border-radius: 16rpx;
  484. font-size: 30rpx;
  485. font-weight: 500;
  486. border: none;
  487. padding: 0;
  488. &::after { border: none; }
  489. &.cancel {
  490. background: #f5f5f5;
  491. color: #666;
  492. }
  493. &.confirm {
  494. background: linear-gradient(135deg, $color-primary-light, $color-primary);
  495. color: $color-text-primary;
  496. }
  497. &:active {
  498. opacity: 0.85;
  499. }
  500. }
  501. </style>