checkout.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. <template>
  2. <view class="container">
  3. <view v-if="loading" class="loading-wrap">
  4. <text class="loading-text">加载中...</text>
  5. </view>
  6. <scroll-view v-else scroll-y class="content">
  7. <!-- Address Section -->
  8. <view class="section address-section" @click="goSelectAddress">
  9. <view v-if="selectedAddress" class="address-content">
  10. <view class="address-top">
  11. <text class="receiver-name">{{ selectedAddress.receiverName }}</text>
  12. <text class="receiver-phone">{{ formatPhone(selectedAddress.phone) }}</text>
  13. <text v-if="selectedAddress.isDefault === 1" class="default-badge">默认</text>
  14. </view>
  15. <text class="address-detail">
  16. {{ selectedAddress.province }}{{ selectedAddress.city }}{{ selectedAddress.district }}{{ selectedAddress.street }}
  17. </text>
  18. <text class="address-arrow">›</text>
  19. </view>
  20. <view v-else class="address-empty" @click.stop="goAddAddress">
  21. <text class="address-empty-icon">📍</text>
  22. <text class="address-empty-text">请添加收货地址</text>
  23. <text class="address-arrow">›</text>
  24. </view>
  25. </view>
  26. <!-- Order Items Section -->
  27. <view class="section">
  28. <text class="section-title">商品信息</text>
  29. <view
  30. v-for="item in items"
  31. :key="item.productId"
  32. class="order-item"
  33. >
  34. <image
  35. class="item-image"
  36. :src="item.coverImage || '/static/default-product.png'"
  37. mode="aspectFill"
  38. />
  39. <view class="item-info">
  40. <text class="item-name">{{ item.productName }}</text>
  41. <text class="item-qty">x{{ item.quantity }}</text>
  42. </view>
  43. <text class="item-price">{{ formatPriceWithSymbol(item.unitPrice) }}</text>
  44. </view>
  45. </view>
  46. <!-- Freight Section -->
  47. <view class="section">
  48. <view class="info-row">
  49. <text class="info-label">运费</text>
  50. <text class="info-value">{{ formatPriceWithSymbol(freight) }}</text>
  51. </view>
  52. </view>
  53. <!-- Coupon Section -->
  54. <view class="section coupon-section" @click="openCouponPicker">
  55. <view class="info-row">
  56. <text class="info-label">优惠券</text>
  57. <view class="coupon-selected" v-if="selectedCoupon">
  58. <text class="coupon-discount">-{{ formatPriceWithSymbol(selectedCoupon.value) }}</text>
  59. <text class="coupon-name-tag">{{ selectedCoupon.name }}</text>
  60. </view>
  61. <view class="coupon-placeholder" v-else>
  62. <text class="placeholder-text">选择优惠券</text>
  63. </view>
  64. <text class="address-arrow">›</text>
  65. </view>
  66. </view>
  67. <!-- Remark Section -->
  68. <view class="section">
  69. <view class="info-row">
  70. <text class="info-label">备注</text>
  71. <input
  72. class="remark-input"
  73. v-model="remark"
  74. placeholder="选填,给商家留言"
  75. placeholder-style="color: #ccc; font-size: 26rpx;"
  76. />
  77. </view>
  78. </view>
  79. <view class="bottom-spacer"></view>
  80. <!-- Coupon Picker Bottom Sheet -->
  81. <view class="modal-mask" v-if="showCouponPicker" @click="closeCouponPicker">
  82. <view class="coupon-picker" @click.stop>
  83. <view class="picker-header">
  84. <text class="picker-title">选择优惠券</text>
  85. <text class="picker-close" @click="closeCouponPicker">关闭</text>
  86. </view>
  87. <scroll-view scroll-y class="picker-list">
  88. <view
  89. v-for="coupon in availableCoupons"
  90. :key="coupon.id"
  91. class="picker-item"
  92. :class="selectedCoupon && selectedCoupon.id === coupon.id ? 'picker-item-active' : ''"
  93. @click="selectCoupon(coupon)"
  94. >
  95. <view class="picker-item-left">
  96. <text class="picker-item-value">{{ formatPriceWithSymbol(coupon.value) }}</text>
  97. </view>
  98. <view class="picker-item-right">
  99. <text class="picker-item-name">{{ coupon.name }}</text>
  100. <text class="picker-item-condition">{{ getConditionText(coupon.minSpend) }}</text>
  101. </view>
  102. <view class="picker-item-check" v-if="selectedCoupon && selectedCoupon.id === coupon.id">
  103. <text class="check-icon">✓</text>
  104. </view>
  105. </view>
  106. <view class="picker-empty" v-if="availableCoupons.length === 0">
  107. <text>暂无可用优惠券</text>
  108. </view>
  109. </scroll-view>
  110. <view class="picker-footer" v-if="selectedCoupon">
  111. <button class="picker-btn-remove" @click="removeCoupon">不使用优惠券</button>
  112. </view>
  113. </view>
  114. </view>
  115. </scroll-view>
  116. <view class="bottom-bar">
  117. <view class="bottom-left">
  118. <text class="total-label">合计: </text>
  119. <text class="total-amount">{{ formatPriceWithSymbol(finalAmount) }}</text>
  120. <text v-if="selectedCoupon" class="total-original">{{ formatPriceWithSymbol(actualAmount + freight) }}</text>
  121. </view>
  122. <button
  123. :class="['submit-btn', submitting ? 'disabled' : '']"
  124. :disabled="submitting"
  125. @click="onSubmit"
  126. >提交订单</button>
  127. </view>
  128. </view>
  129. </template>
  130. <script>
  131. import config from '@/config.js'
  132. import { getCouponList } from '../../../utils/api.js'
  133. export default {
  134. data() {
  135. return {
  136. items: [],
  137. selectedAddress: null,
  138. freight: 0,
  139. remark: '',
  140. loading: false,
  141. submitting: false,
  142. actualAmount: 0,
  143. coupons: [],
  144. selectedCoupon: null,
  145. showCouponPicker: false
  146. }
  147. },
  148. computed: {
  149. finalAmount() {
  150. var total = (this.actualAmount || 0) + (this.freight || 0)
  151. if (this.selectedCoupon) {
  152. total = total - this.selectedCoupon.value
  153. if (total < 0) total = 0
  154. }
  155. return total
  156. },
  157. availableCoupons() {
  158. var self = this
  159. return this.coupons.filter(function(c) {
  160. if (c.status && c.status !== 'AVAILABLE') return false
  161. if (c.applicableTo && c.applicableTo !== 'ALL') return false
  162. if (c.minSpend && c.minSpend > self.actualAmount) return false
  163. return true
  164. })
  165. }
  166. },
  167. onLoad(options) {
  168. var that = this
  169. var stored = uni.getStorageSync('checkoutItems')
  170. if (stored && stored.length > 0) {
  171. this.items = stored
  172. this.calcAmount()
  173. } else if (options.productId) {
  174. // Load product directly from detail page
  175. var productData = {
  176. productId: parseInt(options.productId),
  177. productName: options.productName || '商品',
  178. coverImage: options.coverImage || '',
  179. unitPrice: parseInt(options.price) || 0,
  180. quantity: parseInt(options.quantity) || 1
  181. }
  182. if (options.skuId) {
  183. productData.skuId = parseInt(options.skuId)
  184. }
  185. this.items = [productData]
  186. this.calcAmount()
  187. }
  188. this.loadDefaultAddress()
  189. this.loadCoupons()
  190. },
  191. methods: {
  192. calcAmount() {
  193. var total = 0
  194. for (var i = 0; i < this.items.length; i++) {
  195. total = total + (this.items[i].unitPrice * this.items[i].quantity)
  196. }
  197. this.actualAmount = total
  198. },
  199. async loadCoupons() {
  200. try {
  201. var res = await getCouponList()
  202. this.coupons = res.data || []
  203. } catch (e) {
  204. this.coupons = []
  205. }
  206. },
  207. openCouponPicker() {
  208. if (this.availableCoupons.length === 0 && !this.selectedCoupon) {
  209. uni.showToast({ title: '暂无可用优惠券', icon: 'none' })
  210. return
  211. }
  212. this.showCouponPicker = true
  213. },
  214. closeCouponPicker() {
  215. this.showCouponPicker = false
  216. },
  217. selectCoupon(coupon) {
  218. this.selectedCoupon = coupon
  219. this.showCouponPicker = false
  220. },
  221. removeCoupon() {
  222. this.selectedCoupon = null
  223. this.showCouponPicker = false
  224. },
  225. getConditionText(minSpend) {
  226. if (!minSpend || minSpend <= 0) return '无门槛'
  227. return '满' + (minSpend / 100).toFixed(2) + '元可用'
  228. },
  229. loadDefaultAddress() {
  230. this.loading = true
  231. var that = this
  232. uni.request({
  233. url: config.api('/api/user/address/list'),
  234. method: 'POST',
  235. data: {},
  236. header: {
  237. 'Content-Type': 'application/json',
  238. 'Authorization': 'Bearer ' + uni.getStorageSync('token')
  239. },
  240. success: function(res) {
  241. that.loading = false
  242. if (res.data && res.data.code === 200) {
  243. var list = res.data.data || []
  244. for (var i = 0; i < list.length; i++) {
  245. if (list[i].isDefault === 1) {
  246. that.selectedAddress = list[i]
  247. break
  248. }
  249. }
  250. if (!that.selectedAddress && list.length > 0) {
  251. that.selectedAddress = list[0]
  252. }
  253. }
  254. },
  255. fail: function() {
  256. that.loading = false
  257. }
  258. })
  259. },
  260. goSelectAddress() {
  261. var self = this
  262. uni.navigateTo({
  263. url: '/pages/shop/address/address?selectMode=1',
  264. events: {
  265. onAddressSelect: function(data) {
  266. self.selectedAddress = data
  267. }
  268. },
  269. success: function(res) {
  270. res.eventChannel.on('onAddressSelect', function(data) {
  271. self.selectedAddress = data
  272. })
  273. }
  274. })
  275. },
  276. goAddAddress() {
  277. uni.navigateTo({ url: '/pages/shop/address-edit/address-edit' })
  278. },
  279. onSubmit() {
  280. if (!this.selectedAddress) {
  281. uni.showToast({ title: '请选择收货地址', icon: 'none' })
  282. return
  283. }
  284. if (this.items.length === 0) {
  285. uni.showToast({ title: '请选择商品', icon: 'none' })
  286. return
  287. }
  288. this.submitting = true
  289. var orderItems = []
  290. for (var i = 0; i < this.items.length; i++) {
  291. var itemData = {
  292. productId: this.items[i].productId,
  293. quantity: this.items[i].quantity
  294. }
  295. if (this.items[i].skuId) {
  296. itemData.skuId = this.items[i].skuId
  297. }
  298. orderItems.push(itemData)
  299. }
  300. var addressSnapshot = JSON.stringify({
  301. receiverName: this.selectedAddress.receiverName,
  302. phone: this.selectedAddress.phone,
  303. province: this.selectedAddress.province,
  304. city: this.selectedAddress.city,
  305. district: this.selectedAddress.district,
  306. street: this.selectedAddress.street
  307. })
  308. var that = this
  309. uni.request({
  310. url: config.api('/api/product/order/create'),
  311. method: 'POST',
  312. data: {
  313. items: orderItems,
  314. remark: this.remark,
  315. addressSnapshot: addressSnapshot
  316. },
  317. header: {
  318. 'Content-Type': 'application/json',
  319. 'Authorization': 'Bearer ' + uni.getStorageSync('token')
  320. },
  321. success: function(res) {
  322. that.submitting = false
  323. if (res.data && res.data.code === 200) {
  324. var order = res.data.data
  325. uni.showToast({ title: '下单成功', icon: 'success' })
  326. uni.removeStorageSync('checkoutItems')
  327. setTimeout(function() {
  328. uni.navigateTo({
  329. url: '/pages/shop/payment/payment?orderNo=' + order.orderNo + '&amount=' + order.totalAmount
  330. })
  331. }, 1000)
  332. } else {
  333. uni.showToast({ title: res.data.message || '下单失败', icon: 'none' })
  334. }
  335. },
  336. fail: function() {
  337. that.submitting = false
  338. uni.showToast({ title: '网络请求失败', icon: 'none' })
  339. }
  340. })
  341. },
  342. formatPrice(price) {
  343. if (price === null || price === undefined) return '0.00'
  344. return (Number(price) / 100).toFixed(2)
  345. },
  346. formatPhone(phone) {
  347. if (!phone) return ''
  348. if (phone.length === 11) {
  349. return phone.substr(0, 3) + '****' + phone.substr(7)
  350. }
  351. return phone
  352. }
  353. }
  354. }
  355. </script>
  356. <style scoped>
  357. .container {
  358. min-height: 100vh;
  359. background: #f5f5f5;
  360. }
  361. .loading-wrap {
  362. display: flex;
  363. justify-content: center;
  364. padding-top: 200rpx;
  365. }
  366. .loading-text {
  367. font-size: 28rpx;
  368. color: #999;
  369. }
  370. .content {
  371. height: calc(100vh - 120rpx);
  372. }
  373. .section {
  374. background: #fff;
  375. margin: 20rpx;
  376. border-radius: 16rpx;
  377. padding: 24rpx;
  378. }
  379. .section-title {
  380. font-size: 28rpx;
  381. font-weight: bold;
  382. color: #333;
  383. margin-bottom: 20rpx;
  384. display: block;
  385. }
  386. .address-section {
  387. position: relative;
  388. }
  389. .address-content {
  390. position: relative;
  391. }
  392. .address-top {
  393. display: flex;
  394. align-items: center;
  395. margin-bottom: 10rpx;
  396. }
  397. .receiver-name {
  398. font-size: 30rpx;
  399. font-weight: bold;
  400. color: #333;
  401. margin-right: 20rpx;
  402. }
  403. .receiver-phone {
  404. font-size: 26rpx;
  405. color: #666;
  406. }
  407. .default-badge {
  408. font-size: 20rpx;
  409. color: #F97316;
  410. background: #fff7e6;
  411. padding: 2rpx 12rpx;
  412. border-radius: 20rpx;
  413. margin-left: 12rpx;
  414. }
  415. .address-detail {
  416. font-size: 26rpx;
  417. color: #999;
  418. line-height: 1.5;
  419. }
  420. .address-arrow {
  421. position: absolute;
  422. right: 0;
  423. top: 50%;
  424. font-size: 40rpx;
  425. color: #ccc;
  426. transform: translateY(-50%);
  427. }
  428. .address-empty {
  429. display: flex;
  430. align-items: center;
  431. justify-content: center;
  432. padding: 30rpx 0;
  433. position: relative;
  434. }
  435. .address-empty-icon {
  436. font-size: 40rpx;
  437. margin-right: 12rpx;
  438. }
  439. .address-empty-text {
  440. font-size: 28rpx;
  441. color: #999;
  442. }
  443. .order-item {
  444. display: flex;
  445. align-items: center;
  446. padding: 16rpx 0;
  447. border-bottom: 1rpx solid #f5f5f5;
  448. }
  449. .order-item:last-child {
  450. border-bottom: none;
  451. }
  452. .item-image {
  453. width: 80rpx;
  454. height: 80rpx;
  455. border-radius: 8rpx;
  456. background: #f0f0f0;
  457. margin-right: 16rpx;
  458. }
  459. .item-info {
  460. flex: 1;
  461. display: flex;
  462. flex-direction: column;
  463. min-width: 0;
  464. }
  465. .item-name {
  466. font-size: 26rpx;
  467. color: #333;
  468. overflow: hidden;
  469. text-overflow: ellipsis;
  470. white-space: nowrap;
  471. margin-bottom: 6rpx;
  472. }
  473. .item-qty {
  474. font-size: 22rpx;
  475. color: #999;
  476. }
  477. .item-price {
  478. font-size: 26rpx;
  479. color: #F97316;
  480. font-weight: bold;
  481. margin-left: 16rpx;
  482. }
  483. .info-row {
  484. display: flex;
  485. justify-content: space-between;
  486. align-items: center;
  487. padding: 8rpx 0;
  488. }
  489. .info-label {
  490. font-size: 26rpx;
  491. color: #999;
  492. }
  493. .info-value {
  494. font-size: 26rpx;
  495. color: #333;
  496. }
  497. .remark-input {
  498. flex: 1;
  499. text-align: right;
  500. font-size: 26rpx;
  501. color: #333;
  502. }
  503. /* ===== Coupon Section ===== */
  504. .coupon-section {
  505. position: relative;
  506. }
  507. .coupon-section:active {
  508. background: #fafafa;
  509. }
  510. .coupon-selected {
  511. display: flex;
  512. align-items: center;
  513. flex: 1;
  514. justify-content: flex-end;
  515. margin-right: 30rpx;
  516. }
  517. .coupon-discount {
  518. font-size: 26rpx;
  519. color: #F97316;
  520. font-weight: 600;
  521. margin-right: 12rpx;
  522. }
  523. .coupon-name-tag {
  524. font-size: 22rpx;
  525. color: #fff;
  526. background: #F97316;
  527. padding: 2rpx 12rpx;
  528. border-radius: 6rpx;
  529. max-width: 200rpx;
  530. overflow: hidden;
  531. text-overflow: ellipsis;
  532. white-space: nowrap;
  533. }
  534. .coupon-placeholder {
  535. flex: 1;
  536. text-align: right;
  537. margin-right: 30rpx;
  538. }
  539. .placeholder-text {
  540. font-size: 26rpx;
  541. color: #ccc;
  542. }
  543. .total-original {
  544. font-size: 22rpx;
  545. color: #bbb;
  546. text-decoration: line-through;
  547. margin-left: 8rpx;
  548. }
  549. /* ===== Coupon Picker Modal ===== */
  550. .modal-mask {
  551. position: fixed;
  552. top: 0;
  553. left: 0;
  554. right: 0;
  555. bottom: 0;
  556. background: rgba(0,0,0,0.5);
  557. z-index: 999;
  558. display: flex;
  559. align-items: flex-end;
  560. }
  561. .coupon-picker {
  562. background: #fff;
  563. border-radius: 24rpx 24rpx 0 0;
  564. width: 100%;
  565. max-height: 70vh;
  566. display: flex;
  567. flex-direction: column;
  568. }
  569. .picker-header {
  570. display: flex;
  571. justify-content: space-between;
  572. align-items: center;
  573. padding: 30rpx;
  574. border-bottom: 1rpx solid #eee;
  575. }
  576. .picker-title {
  577. font-size: 30rpx;
  578. font-weight: 600;
  579. color: #333;
  580. }
  581. .picker-close {
  582. font-size: 26rpx;
  583. color: #999;
  584. }
  585. .picker-list {
  586. max-height: 50vh;
  587. padding: 0 20rpx;
  588. }
  589. .picker-item {
  590. display: flex;
  591. align-items: center;
  592. padding: 24rpx 16rpx;
  593. border-bottom: 1rpx solid #f5f5f5;
  594. }
  595. .picker-item:active {
  596. background: #fafafa;
  597. }
  598. .picker-item-active {
  599. background: #FFF7ED;
  600. }
  601. .picker-item-left {
  602. width: 120rpx;
  603. text-align: center;
  604. flex-shrink: 0;
  605. }
  606. .picker-item-value {
  607. font-size: 32rpx;
  608. font-weight: bold;
  609. color: #F97316;
  610. }
  611. .picker-item-right {
  612. flex: 1;
  613. margin: 0 16rpx;
  614. }
  615. .picker-item-name {
  616. font-size: 26rpx;
  617. color: #333;
  618. display: block;
  619. margin-bottom: 4rpx;
  620. }
  621. .picker-item-condition {
  622. font-size: 22rpx;
  623. color: #999;
  624. }
  625. .picker-item-check {
  626. width: 40rpx;
  627. height: 40rpx;
  628. background: #F97316;
  629. border-radius: 50%;
  630. display: flex;
  631. align-items: center;
  632. justify-content: center;
  633. flex-shrink: 0;
  634. }
  635. .check-icon {
  636. color: #fff;
  637. font-size: 24rpx;
  638. font-weight: bold;
  639. }
  640. .picker-empty {
  641. text-align: center;
  642. padding: 60rpx 0;
  643. font-size: 26rpx;
  644. color: #999;
  645. }
  646. .picker-footer {
  647. padding: 20rpx;
  648. border-top: 1rpx solid #eee;
  649. }
  650. .picker-btn-remove {
  651. background: #f5f5f5;
  652. color: #666;
  653. font-size: 26rpx;
  654. border-radius: 12rpx;
  655. border: none;
  656. }
  657. .picker-btn-remove::after {
  658. border: none;
  659. }
  660. .bottom-spacer {
  661. height: 140rpx;
  662. }
  663. .bottom-bar {
  664. position: fixed;
  665. bottom: 0;
  666. left: 0;
  667. right: 0;
  668. height: 100rpx;
  669. background: #fff;
  670. border-top: 1rpx solid #eee;
  671. display: flex;
  672. align-items: center;
  673. justify-content: space-between;
  674. padding: 0 30rpx;
  675. z-index: 100;
  676. }
  677. .bottom-left {
  678. display: flex;
  679. align-items: baseline;
  680. }
  681. .total-label {
  682. font-size: 26rpx;
  683. color: #666;
  684. }
  685. .total-amount {
  686. font-size: 36rpx;
  687. color: #F97316;
  688. font-weight: bold;
  689. }
  690. .submit-btn {
  691. background: linear-gradient(135deg, #F97316, #FB923C);
  692. color: #fff;
  693. font-size: 28rpx;
  694. font-weight: bold;
  695. padding: 0 60rpx;
  696. height: 72rpx;
  697. line-height: 72rpx;
  698. border-radius: 36rpx;
  699. border: none;
  700. }
  701. .submit-btn::after {
  702. border: none;
  703. }
  704. .submit-btn.disabled {
  705. background: #ddd;
  706. color: #999;
  707. }
  708. </style>