index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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/payscore.svg" 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. </view>
  58. </template>
  59. <script setup lang="ts">
  60. import { ref, onMounted } from 'vue'
  61. import { onShow } from '@dcloudio/uni-app'
  62. import { scanDoor } from '../../api/device'
  63. import { checkPayscoreEnabled } from '../../api/payscore'
  64. import { getCouponCount } from '../../api/coupon'
  65. import { isLoggedIn, getToken } from '../../utils/auth'
  66. import { logger } from '../../utils/logger'
  67. // 页面显示时检查 token
  68. onShow(() => {
  69. const token = getToken()
  70. logger.log('[首页 onShow] token 状态:', token ? '存在' : '不存在')
  71. })
  72. // 处理支付分开通成功
  73. const onPayscoreEnabled = () => {
  74. logger.log('[首页] 用户已开通支付分')
  75. // 可以自动触发扫码
  76. setTimeout(() => {
  77. scanCode()
  78. }, 500)
  79. }
  80. // 定义暴露给其他页面的方法
  81. defineExpose({
  82. onPayscoreEnabled
  83. })
  84. const scanCode = async () => {
  85. // 检查登录状态
  86. if (!isLoggedIn()) {
  87. uni.showModal({
  88. title: '提示',
  89. content: '请先登录后再扫码开门',
  90. showCancel: false,
  91. success: () => {
  92. uni.reLaunch({
  93. url: '/pages/login/login?redirect=/pages/index/index'
  94. });
  95. }
  96. });
  97. return;
  98. }
  99. // 检查微信支付分开通状态
  100. try {
  101. const payscoreResult = await checkPayscoreEnabled()
  102. if (!payscoreResult.enabled) {
  103. // 未开通,跳转到开通页面
  104. uni.navigateTo({
  105. url: '/pages/payscore/enable'
  106. })
  107. return
  108. }
  109. } catch (error: any) {
  110. logger.error('检查支付分状态失败:', error)
  111. // 如果检查失败,也跳转到开通页面
  112. uni.navigateTo({
  113. url: '/pages/payscore/enable'
  114. })
  115. return
  116. }
  117. // 调用摄像头扫码
  118. uni.scanCode({
  119. success: async function (res) {
  120. logger.log('扫码结果:', res.result);
  121. // 从扫码结果中解析设备ID
  122. // 二维码格式: https://hh.hahabianli.com/B142977?_wxpmm0=6009000C0000
  123. // 需要提取路径中的设备ID: B142977
  124. let deviceId = '';
  125. try {
  126. // 尝试从URL中提取deviceId
  127. const urlPattern = /\/([A-Z0-9]+)(\?|$)/;
  128. const match = res.result.match(urlPattern);
  129. if (match && match[1]) {
  130. deviceId = match[1];
  131. logger.log('提取到设备ID:', deviceId);
  132. } else {
  133. // 如果不是URL格式,尝试解析JSON格式
  134. try {
  135. const qrData = JSON.parse(res.result);
  136. if (qrData.deviceId) {
  137. deviceId = qrData.deviceId;
  138. }
  139. } catch (e) {
  140. // 不是JSON格式,直接使用扫码结果作为deviceId
  141. deviceId = res.result;
  142. }
  143. }
  144. if (!deviceId) {
  145. throw new Error('无法解析设备ID');
  146. }
  147. } catch (error) {
  148. logger.error('解析设备ID失败:', error);
  149. uni.showToast({
  150. title: '二维码格式错误',
  151. icon: 'none'
  152. });
  153. return;
  154. }
  155. // 弹出选择弹窗:查看柜内商品 or 直接开门
  156. uni.showActionSheet({
  157. itemList: ['查看柜内商品', '直接开门'],
  158. success: async (actionRes) => {
  159. if (actionRes.tapIndex === 0) {
  160. // 查看柜内商品 -> 跳转到商品陈列页
  161. uni.navigateTo({
  162. url: '/pages/products/products?deviceId=' + deviceId
  163. });
  164. } else if (actionRes.tapIndex === 1) {
  165. // 直接开门 -> 执行现有开门逻辑
  166. await doOpenDoor(deviceId);
  167. }
  168. },
  169. fail: () => {
  170. logger.log('用户取消选择');
  171. }
  172. });
  173. },
  174. fail: function (err) {
  175. logger.log('扫码取消:', err);
  176. uni.showToast({
  177. title: '扫码取消',
  178. icon: 'none'
  179. });
  180. }
  181. });
  182. };
  183. /**
  184. * 执行开门操作
  185. */
  186. const doOpenDoor = async (deviceId: string) => {
  187. uni.showLoading({
  188. title: '正在开门...',
  189. mask: true
  190. });
  191. try {
  192. const response = await scanDoor(deviceId);
  193. uni.hideLoading();
  194. uni.showToast({
  195. title: '开门成功',
  196. icon: 'success'
  197. });
  198. // 将设备信息存储到本地,供购物页面使用
  199. uni.setStorageSync('currentDeviceId', response.deviceId);
  200. uni.setStorageSync('currentOutTradeNo', response.outTradeNo);
  201. uni.setStorageSync('currentOrderNo', response.orderNo);
  202. uni.removeStorageSync('shoppingPollingActive');
  203. setTimeout(() => {
  204. uni.navigateTo({
  205. url: '/pages/shopping/shopping'
  206. });
  207. }, 1000);
  208. } catch (error: any) {
  209. uni.hideLoading();
  210. logger.error('开门失败:', error);
  211. }
  212. };
  213. const goToMy = () => {
  214. uni.navigateTo({
  215. url: '/pages/my/my'
  216. })
  217. }
  218. const goToOrders = () => {
  219. uni.navigateTo({
  220. url: '/pages/orders/orders'
  221. })
  222. }
  223. const goToCouponCenter = () => {
  224. uni.navigateTo({
  225. url: '/pages/couponCenter/couponCenter'
  226. })
  227. }
  228. // 客服会话消息发送成功回调
  229. const onContactSuccess = () => {
  230. logger.log('[首页] 客服会话已打开')
  231. }
  232. </script>
  233. <style lang="scss">
  234. .container {
  235. min-height: 100vh;
  236. background: $color-bg-secondary;
  237. display: flex;
  238. flex-direction: column;
  239. padding: $spacing-xxl $spacing-lg;
  240. padding-bottom: calc(100rpx + constant(safe-area-inset-bottom));
  241. padding-bottom: calc(100rpx + env(safe-area-inset-bottom));
  242. box-sizing: border-box;
  243. }
  244. /* ========== 品牌区 ========== */
  245. .brand-section {
  246. text-align: center;
  247. padding: $spacing-xxl 0 $spacing-xl;
  248. animation: slideUp 0.3s ease;
  249. will-change: transform, opacity;
  250. transform: translateZ(0);
  251. .brand-title {
  252. font-size: 56rpx;
  253. font-weight: 300;
  254. color: $color-text-primary;
  255. letter-spacing: 8rpx;
  256. display: block;
  257. margin-bottom: $spacing-sm;
  258. }
  259. .brand-slogan {
  260. font-size: 24rpx;
  261. color: $color-text-secondary;
  262. letter-spacing: 4rpx;
  263. }
  264. }
  265. /* ========== 核心操作区 ========== */
  266. .action-section {
  267. display: flex;
  268. justify-content: center;
  269. align-items: center;
  270. padding: $spacing-xl 0;
  271. animation: scaleIn 0.3s ease 0.1s both;
  272. will-change: transform, opacity;
  273. transform: translateZ(0);
  274. }
  275. .scan-button {
  276. position: relative;
  277. width: 400rpx;
  278. height: 400rpx;
  279. min-width: 400rpx;
  280. min-height: 400rpx;
  281. aspect-ratio: 1 / 1;
  282. border-radius: 50%;
  283. background: transparent;
  284. border: none;
  285. padding: 0;
  286. margin: 0;
  287. overflow: hidden;
  288. &::after {
  289. border: none;
  290. }
  291. &-inner {
  292. width: 100%;
  293. height: 100%;
  294. border-radius: 50%;
  295. background: linear-gradient(135deg, $color-primary-light 0%, $color-primary 100%);
  296. box-shadow: $shadow-primary;
  297. display: flex;
  298. flex-direction: column;
  299. align-items: center;
  300. justify-content: center;
  301. transition: all $duration-normal $ease-out;
  302. &:active {
  303. transform: scale(0.95);
  304. box-shadow: 0 4rpx 16rpx rgba(255, 193, 7, 0.3);
  305. }
  306. }
  307. &-ripple {
  308. position: absolute;
  309. top: 50%;
  310. left: 50%;
  311. width: 100%;
  312. height: 100%;
  313. border-radius: 50%;
  314. background: $color-primary;
  315. transform: translate(-50%, -50%);
  316. animation: pulse 3s cubic-bezier(0.42, 0, 0.58, 1) infinite;
  317. opacity: 0.2;
  318. z-index: -1;
  319. }
  320. }
  321. .scan-icon {
  322. width: 160rpx;
  323. height: 160rpx;
  324. margin-bottom: $spacing-md;
  325. flex-shrink: 0;
  326. display: block;
  327. }
  328. .scan-text {
  329. font-size: 40rpx;
  330. font-weight: 600;
  331. color: $color-text-primary;
  332. letter-spacing: 4rpx;
  333. }
  334. /* ========== 快捷功能区 ========== */
  335. .quick-actions {
  336. display: flex;
  337. justify-content: center;
  338. align-items: center;
  339. gap: $spacing-xl;
  340. margin: $spacing-xxl 0;
  341. animation: slideUp 0.35s ease 0.15s both;
  342. will-change: transform, opacity;
  343. transform: translateZ(0);
  344. &-item {
  345. display: flex;
  346. flex-direction: column;
  347. align-items: center;
  348. justify-content: center;
  349. padding: $spacing-md;
  350. position: relative;
  351. flex: 0 0 auto;
  352. &:active {
  353. opacity: 0.8;
  354. }
  355. }
  356. .action-icon {
  357. width: 120rpx;
  358. height: 120rpx;
  359. background: $color-bg-primary;
  360. border-radius: $radius-lg;
  361. display: flex;
  362. align-items: center;
  363. justify-content: center;
  364. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.06);
  365. margin-bottom: $spacing-sm;
  366. border: 2rpx solid $color-border;
  367. flex-shrink: 0;
  368. image {
  369. width: 64rpx;
  370. height: 64rpx;
  371. display: block;
  372. }
  373. &:active {
  374. box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.1);
  375. border-color: $color-primary-light;
  376. }
  377. }
  378. .action-label {
  379. font-size: 28rpx;
  380. color: $color-text-primary;
  381. font-weight: 500;
  382. text-align: center;
  383. display: block;
  384. width: auto;
  385. }
  386. }
  387. /* ========== 底部信息卡片 ========== */
  388. .info-card {
  389. background: $color-bg-primary;
  390. border-radius: $radius-lg;
  391. padding: $spacing-lg;
  392. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.04);
  393. animation: slideUp 0.35s ease 0.2s both;
  394. margin-top: auto;
  395. width: 100%;
  396. box-sizing: border-box;
  397. will-change: transform, opacity;
  398. transform: translateZ(0);
  399. .info-item {
  400. display: flex;
  401. align-items: center;
  402. justify-content: center;
  403. padding: $spacing-sm 0;
  404. min-height: 60rpx;
  405. &:not(:last-child) {
  406. border-bottom: 1rpx solid $color-border;
  407. padding-bottom: $spacing-md;
  408. margin-bottom: $spacing-md;
  409. }
  410. }
  411. .info-icon {
  412. font-size: 28rpx;
  413. margin-right: $spacing-sm;
  414. flex-shrink: 0;
  415. line-height: 1;
  416. &.payscore-icon {
  417. width: 36rpx;
  418. height: 36rpx;
  419. }
  420. }
  421. .info-text {
  422. font-size: 24rpx;
  423. color: $color-text-secondary;
  424. line-height: 1.5;
  425. text-align: center;
  426. }
  427. }
  428. </style>