巴青农资商城

checkout.vue 13KB

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