巴青农资商城

checkout-cart.vue 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. <template>
  2. <view class="checkout-page">
  3. <view v-if="pageLoading" class="checkout-page__loading">
  4. <u-loading-icon mode="circle" />
  5. </view>
  6. <view v-else-if="pageError" class="checkout-page__error">
  7. <u-empty mode="page" :text="pageError" icon-size="80" />
  8. <u-button type="primary" text="重试" size="small" custom-style="margin-top:24rpx" @click="loadPreview" />
  9. <u-button plain text="返回" size="small" custom-style="margin-top:16rpx" @click="onBack" />
  10. </view>
  11. <template v-else-if="preview">
  12. <scroll-view class="checkout-scroll" scroll-y :style="{ height: scrollHeight }">
  13. <address-bar :address="preview.address" @click="onAddressTap" />
  14. <view class="shop-head">
  15. <image class="shop-head__avatar" :src="preview.shop.shopAvatar" mode="aspectFill" />
  16. <text class="shop-head__name">{{ preview.shop.shopName }}</text>
  17. </view>
  18. <view class="goods-card">
  19. <checkout-multi-goods-row
  20. v-for="item in preview.goodsList"
  21. :key="item.cartItemId"
  22. :item="item"
  23. @quantity-change="(qty) => onQuantityChange(item.cartItemId, qty)"
  24. @remark-change="(text) => onRemarkChange(item.cartItemId, text)"
  25. />
  26. </view>
  27. <view class="amount-card">
  28. <view class="amount-row">
  29. <text class="amount-row__label">商品总价</text>
  30. <text class="amount-row__val">¥{{ preview.goodsAmountText }}</text>
  31. </view>
  32. <view class="amount-row">
  33. <text class="amount-row__label">运费</text>
  34. <view class="amount-row__right">
  35. <text class="amount-row__val">¥{{ preview.freightAmountText }}</text>
  36. <text v-if="preview.freightDesc" class="amount-row__desc">{{ preview.freightDesc }}</text>
  37. </view>
  38. </view>
  39. </view>
  40. <view class="pay-card">
  41. <text class="pay-card__label">支付方式</text>
  42. <view class="pay-card__item">
  43. <u-icon name="weixin-fill" color="#09bb07" size="22" />
  44. <text class="pay-card__text">微信支付</text>
  45. <u-icon name="checkbox-mark" color="#2e7d32" size="18" />
  46. </view>
  47. </view>
  48. </scroll-view>
  49. <view class="checkout-footer">
  50. <view class="checkout-footer__sum">
  51. <text class="checkout-footer__label">应付:</text>
  52. <text class="checkout-footer__price">¥{{ preview.payAmountText }}</text>
  53. </view>
  54. <button class="checkout-footer__btn" :disabled="submitting" @click="onSubmit">
  55. {{ submitting ? '提交中...' : '提交订单' }}
  56. </button>
  57. </view>
  58. </template>
  59. <u-popup :show="addressPopupShow" mode="bottom" round="16" @close="addressPopupShow = false">
  60. <view class="addr-popup">
  61. <view class="addr-popup__head">
  62. <text class="addr-popup__title">选择收货地址</text>
  63. <text class="addr-popup__add" @click="goAddAddress">新增地址</text>
  64. </view>
  65. <scroll-view scroll-y class="addr-popup__list">
  66. <view
  67. v-for="item in addressOptions"
  68. :key="item.addressId"
  69. class="addr-option"
  70. :class="{ 'addr-option--on': item.addressId === selectedAddressId }"
  71. @click="onSelectAddress(item)"
  72. >
  73. <view class="addr-option__head">
  74. <text class="addr-option__name">{{ item.consigneeName }}</text>
  75. <text class="addr-option__mobile">{{ item.mobile }}</text>
  76. <text v-if="item.isDefault" class="addr-option__tag">默认</text>
  77. </view>
  78. <text class="addr-option__detail">{{ item.fullAddress }}</text>
  79. </view>
  80. <view v-if="!addressOptions.length" class="addr-popup__empty">
  81. <text>暂无收货地址,请先新增</text>
  82. </view>
  83. </scroll-view>
  84. </view>
  85. </u-popup>
  86. <u-modal
  87. :show="payModalShow"
  88. title="微信支付"
  89. :content="payModalContent"
  90. show-cancel-button
  91. confirm-text="确认支付"
  92. cancel-text="取消支付"
  93. @confirm="onConfirmPay"
  94. @cancel="onCancelPay"
  95. @close="payModalShow = false"
  96. />
  97. </view>
  98. </template>
  99. <script setup>
  100. import { ref } from 'vue'
  101. import { onLoad, onShow } from '@dcloudio/uni-app'
  102. import { previewCheckout, submitCheckout } from '@/api/checkout'
  103. import { payOrder, cancelPay } from '@/api/order'
  104. import { getAddressList } from '@/api/member'
  105. import { mapCheckoutPreviewCart, mapAddressOption } from '@/utils/checkoutDisplay'
  106. import {
  107. CHECKOUT_SOURCE_CART,
  108. CHECKOUT_PAY_TYPE_WECHAT,
  109. CART_CHECKOUT_IDS_KEY
  110. } from '@/constants/checkout'
  111. import { ensureApiToken } from '@/utils/apiAuth'
  112. import { PAGE_ADDRESS_EDIT, PAGE_PAY_RESULT } from '@/utils/pageRoute'
  113. import AddressBar from '@/components/order/AddressBar.vue'
  114. import CheckoutMultiGoodsRow from '@/components/order/CheckoutMultiGoodsRow.vue'
  115. const cartItemIds = ref([])
  116. const selectedAddressId = ref(null)
  117. const preview = ref(null)
  118. const pageLoading = ref(true)
  119. const pageError = ref('')
  120. const submitting = ref(false)
  121. const previewing = ref(false)
  122. const scrollHeight = ref('600px')
  123. const addressPopupShow = ref(false)
  124. const addressOptions = ref([])
  125. const payModalShow = ref(false)
  126. const payModalContent = ref('')
  127. const pendingOrderId = ref(null)
  128. const paying = ref(false)
  129. let previewTimer = null
  130. function getRemarkMap() {
  131. const map = {}
  132. ;(preview.value?.goodsList || []).forEach((row) => {
  133. if (row.cartItemId) {
  134. map[row.cartItemId] = row.buyerRemark || ''
  135. }
  136. })
  137. return map
  138. }
  139. function buildLineItems() {
  140. return (preview.value?.goodsList || []).map((row) => ({
  141. cartItemId: row.cartItemId,
  142. goodsId: row.goodsId,
  143. quantity: row.quantity
  144. }))
  145. }
  146. function buildPreviewBody() {
  147. return {
  148. source: CHECKOUT_SOURCE_CART,
  149. addressId: selectedAddressId.value || undefined,
  150. cartItemIds: cartItemIds.value,
  151. items: buildLineItems()
  152. }
  153. }
  154. function buildSubmitBody() {
  155. return {
  156. source: CHECKOUT_SOURCE_CART,
  157. addressId: selectedAddressId.value,
  158. payType: CHECKOUT_PAY_TYPE_WECHAT,
  159. cartItemIds: cartItemIds.value,
  160. items: (preview.value?.goodsList || []).map((row) => ({
  161. cartItemId: row.cartItemId,
  162. goodsId: row.goodsId,
  163. quantity: row.quantity,
  164. buyerRemark: (row.buyerRemark || '').trim()
  165. }))
  166. }
  167. }
  168. function calcScrollHeight() {
  169. try {
  170. const sys = uni.getSystemInfoSync()
  171. scrollHeight.value = `${(sys.windowHeight || 600) - 56}px`
  172. } catch (e) {
  173. scrollHeight.value = '600px'
  174. }
  175. }
  176. async function loadPreview() {
  177. if (!cartItemIds.value.length || previewing.value) return
  178. previewing.value = true
  179. pageLoading.value = !preview.value
  180. pageError.value = ''
  181. try {
  182. const res = await previewCheckout(buildPreviewBody())
  183. const mapped = mapCheckoutPreviewCart(res.data, getRemarkMap())
  184. if (!mapped || !mapped.goodsList.length) {
  185. throw new Error('订单预览数据异常')
  186. }
  187. preview.value = mapped
  188. if (mapped.address && mapped.address.addressId) {
  189. selectedAddressId.value = mapped.address.addressId
  190. }
  191. } catch (e) {
  192. pageError.value = (e && e.msg) || (e && e.message) || '加载失败'
  193. if (!preview.value) {
  194. preview.value = null
  195. }
  196. } finally {
  197. pageLoading.value = false
  198. previewing.value = false
  199. }
  200. }
  201. function schedulePreview() {
  202. if (previewTimer) clearTimeout(previewTimer)
  203. previewTimer = setTimeout(() => {
  204. loadPreview()
  205. }, 300)
  206. }
  207. function onQuantityChange(cartItemId, qty) {
  208. const list = preview.value?.goodsList || []
  209. const row = list.find((item) => item.cartItemId === cartItemId)
  210. if (row) {
  211. row.quantity = qty
  212. schedulePreview()
  213. }
  214. }
  215. function onRemarkChange(cartItemId, text) {
  216. const list = preview.value?.goodsList || []
  217. const row = list.find((item) => item.cartItemId === cartItemId)
  218. if (row) {
  219. row.buyerRemark = text
  220. }
  221. }
  222. async function onAddressTap() {
  223. try {
  224. const res = await getAddressList()
  225. addressOptions.value = (res.data || []).map(mapAddressOption).filter(Boolean)
  226. } catch (e) {
  227. addressOptions.value = []
  228. }
  229. addressPopupShow.value = true
  230. }
  231. function onSelectAddress(item) {
  232. selectedAddressId.value = item.addressId
  233. addressPopupShow.value = false
  234. schedulePreview()
  235. }
  236. function goAddAddress() {
  237. addressPopupShow.value = false
  238. uni.navigateTo({ url: `${PAGE_ADDRESS_EDIT}?mode=add` })
  239. }
  240. async function onSubmit() {
  241. if (!selectedAddressId.value) {
  242. uni.showToast({ title: '请选择收货地址', icon: 'none' })
  243. return
  244. }
  245. if (submitting.value) return
  246. submitting.value = true
  247. try {
  248. const res = await submitCheckout(buildSubmitBody())
  249. const data = res.data || {}
  250. pendingOrderId.value = data.orderId
  251. payModalContent.value = `订单号:${data.orderNo || '—'}\n应付金额:¥${data.payAmount != null ? data.payAmount : preview.value?.payAmountText || '—'}`
  252. payModalShow.value = true
  253. } catch (e) {
  254. // request 已 Toast
  255. } finally {
  256. submitting.value = false
  257. }
  258. }
  259. async function onConfirmPay() {
  260. if (!pendingOrderId.value || paying.value) return
  261. paying.value = true
  262. payModalShow.value = false
  263. try {
  264. await payOrder(pendingOrderId.value)
  265. try {
  266. uni.removeStorageSync(CART_CHECKOUT_IDS_KEY)
  267. } catch (e) {
  268. // ignore
  269. }
  270. uni.redirectTo({
  271. url: `${PAGE_PAY_RESULT}?status=success&orderId=${pendingOrderId.value}`
  272. })
  273. } catch (e) {
  274. // request 已 Toast
  275. } finally {
  276. paying.value = false
  277. }
  278. }
  279. async function onCancelPay() {
  280. if (!pendingOrderId.value || paying.value) return
  281. paying.value = true
  282. payModalShow.value = false
  283. try {
  284. await cancelPay(pendingOrderId.value)
  285. uni.redirectTo({
  286. url: `${PAGE_PAY_RESULT}?status=closed&orderId=${pendingOrderId.value}`
  287. })
  288. } catch (e) {
  289. // request 已 Toast
  290. } finally {
  291. paying.value = false
  292. }
  293. }
  294. function onBack() {
  295. uni.navigateBack()
  296. }
  297. function parseCartItemIds(options) {
  298. const fromQuery = (options.cartItemIds || '')
  299. .split(',')
  300. .map((id) => id.trim())
  301. .filter(Boolean)
  302. if (fromQuery.length) {
  303. return fromQuery
  304. }
  305. try {
  306. const stored = uni.getStorageSync(CART_CHECKOUT_IDS_KEY)
  307. if (Array.isArray(stored) && stored.length) {
  308. return stored.map(String)
  309. }
  310. } catch (e) {
  311. // ignore
  312. }
  313. return []
  314. }
  315. onLoad((options) => {
  316. calcScrollHeight()
  317. cartItemIds.value = parseCartItemIds(options)
  318. if (!cartItemIds.value.length) {
  319. pageError.value = '结算商品信息缺失'
  320. pageLoading.value = false
  321. setTimeout(() => onBack(), 800)
  322. }
  323. })
  324. onShow(() => {
  325. if (!ensureApiToken(false)) return
  326. if (!cartItemIds.value.length) return
  327. loadPreview()
  328. })
  329. </script>
  330. <style lang="scss" scoped>
  331. .checkout-page {
  332. min-height: 100vh;
  333. background: #f5f6f8;
  334. padding-bottom: env(safe-area-inset-bottom);
  335. }
  336. .checkout-page__loading,
  337. .checkout-page__error {
  338. padding: 120rpx 48rpx;
  339. display: flex;
  340. flex-direction: column;
  341. align-items: center;
  342. }
  343. .checkout-scroll {
  344. box-sizing: border-box;
  345. }
  346. .shop-head {
  347. display: flex;
  348. align-items: center;
  349. padding: 16rpx 24rpx;
  350. margin: 0 24rpx 2rpx;
  351. background: #fff;
  352. border-radius: 12rpx 12rpx 0 0;
  353. }
  354. .shop-head__avatar {
  355. width: 48rpx;
  356. height: 48rpx;
  357. border-radius: 8rpx;
  358. background: #eee;
  359. }
  360. .shop-head__name {
  361. margin-left: 12rpx;
  362. font-size: 28rpx;
  363. color: #333;
  364. font-weight: 500;
  365. }
  366. .goods-card {
  367. margin: 0 24rpx;
  368. background: #fff;
  369. border-radius: 0 0 12rpx 12rpx;
  370. overflow: hidden;
  371. }
  372. .amount-card,
  373. .pay-card {
  374. margin: 16rpx 24rpx;
  375. padding: 24rpx;
  376. background: #fff;
  377. border-radius: 12rpx;
  378. }
  379. .amount-row {
  380. display: flex;
  381. justify-content: space-between;
  382. align-items: flex-start;
  383. padding: 12rpx 0;
  384. }
  385. .amount-row__label {
  386. font-size: 28rpx;
  387. color: #666;
  388. }
  389. .amount-row__right {
  390. text-align: right;
  391. }
  392. .amount-row__val {
  393. font-size: 28rpx;
  394. color: #333;
  395. }
  396. .amount-row__desc {
  397. display: block;
  398. margin-top: 4rpx;
  399. font-size: 22rpx;
  400. color: #999;
  401. }
  402. .pay-card__label {
  403. font-size: 28rpx;
  404. color: #333;
  405. font-weight: 500;
  406. }
  407. .pay-card__item {
  408. margin-top: 16rpx;
  409. display: flex;
  410. align-items: center;
  411. gap: 12rpx;
  412. }
  413. .pay-card__text {
  414. flex: 1;
  415. font-size: 28rpx;
  416. color: #333;
  417. }
  418. .checkout-footer {
  419. position: fixed;
  420. left: 0;
  421. right: 0;
  422. bottom: 0;
  423. display: flex;
  424. align-items: center;
  425. padding: 16rpx 24rpx;
  426. padding-bottom: calc(16rpx + env(safe-area-inset-bottom));
  427. background: #fff;
  428. border-top: 1rpx solid #eee;
  429. z-index: 10;
  430. }
  431. .checkout-footer__sum {
  432. flex: 1;
  433. }
  434. .checkout-footer__label {
  435. font-size: 26rpx;
  436. color: #333;
  437. }
  438. .checkout-footer__price {
  439. font-size: 36rpx;
  440. color: #e53935;
  441. font-weight: 600;
  442. }
  443. .checkout-footer__btn {
  444. min-width: 220rpx;
  445. height: 72rpx;
  446. line-height: 72rpx;
  447. padding: 0 32rpx;
  448. background: linear-gradient(135deg, #43a047 0%, #2e7d32 100%);
  449. color: #fff;
  450. font-size: 28rpx;
  451. border-radius: 36rpx;
  452. border: none;
  453. }
  454. .checkout-footer__btn[disabled] {
  455. opacity: 0.6;
  456. }
  457. .addr-popup {
  458. padding: 24rpx 24rpx calc(24rpx + env(safe-area-inset-bottom));
  459. max-height: 70vh;
  460. }
  461. .addr-popup__head {
  462. display: flex;
  463. justify-content: space-between;
  464. align-items: center;
  465. margin-bottom: 16rpx;
  466. }
  467. .addr-popup__title {
  468. font-size: 30rpx;
  469. font-weight: 600;
  470. color: #333;
  471. }
  472. .addr-popup__add {
  473. font-size: 26rpx;
  474. color: #2e7d32;
  475. }
  476. .addr-popup__list {
  477. max-height: 50vh;
  478. }
  479. .addr-option {
  480. padding: 20rpx;
  481. margin-bottom: 12rpx;
  482. background: #f9f9f9;
  483. border-radius: 12rpx;
  484. border: 2rpx solid transparent;
  485. }
  486. .addr-option--on {
  487. border-color: #2e7d32;
  488. background: #f1f8f2;
  489. }
  490. .addr-option__head {
  491. display: flex;
  492. align-items: center;
  493. flex-wrap: wrap;
  494. gap: 12rpx;
  495. }
  496. .addr-option__name {
  497. font-size: 28rpx;
  498. font-weight: 500;
  499. color: #333;
  500. }
  501. .addr-option__mobile {
  502. font-size: 26rpx;
  503. color: #666;
  504. }
  505. .addr-option__tag {
  506. font-size: 22rpx;
  507. color: #2e7d32;
  508. background: #e8f5e9;
  509. padding: 4rpx 10rpx;
  510. border-radius: 6rpx;
  511. }
  512. .addr-option__detail {
  513. display: block;
  514. margin-top: 8rpx;
  515. font-size: 24rpx;
  516. color: #666;
  517. line-height: 1.5;
  518. }
  519. .addr-popup__empty {
  520. padding: 48rpx;
  521. text-align: center;
  522. color: #999;
  523. font-size: 26rpx;
  524. }
  525. </style>