checkout.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. <!-- Remark Section -->
  54. <view class="section">
  55. <view class="info-row">
  56. <text class="info-label">备注</text>
  57. <input
  58. class="remark-input"
  59. v-model="remark"
  60. placeholder="选填,给商家留言"
  61. placeholder-style="color: #ccc; font-size: 26rpx;"
  62. />
  63. </view>
  64. </view>
  65. <view class="bottom-spacer"></view>
  66. </scroll-view>
  67. <view class="bottom-bar">
  68. <view class="bottom-left">
  69. <text class="total-label">合计: </text>
  70. <text class="total-amount">{{ formatPriceWithSymbol(actualAmount + freight) }}</text>
  71. </view>
  72. <button
  73. :class="['submit-btn', submitting ? 'disabled' : '']"
  74. :disabled="submitting"
  75. @click="onSubmit"
  76. >提交订单</button>
  77. </view>
  78. </view>
  79. </template>
  80. <script>
  81. import config from '@/config.js'
  82. export default {
  83. data() {
  84. return {
  85. items: [],
  86. selectedAddress: null,
  87. freight: 0,
  88. remark: '',
  89. loading: false,
  90. submitting: false,
  91. actualAmount: 0
  92. }
  93. },
  94. onLoad() {
  95. var stored = uni.getStorageSync('checkoutItems')
  96. if (stored) {
  97. this.items = stored
  98. this.calcAmount()
  99. }
  100. this.loadDefaultAddress()
  101. },
  102. methods: {
  103. calcAmount() {
  104. var total = 0
  105. for (var i = 0; i < this.items.length; i++) {
  106. total = total + (this.items[i].unitPrice * this.items[i].quantity)
  107. }
  108. this.actualAmount = total
  109. },
  110. loadDefaultAddress() {
  111. this.loading = true
  112. var that = this
  113. uni.request({
  114. url: config.api('/api/user/address/list'),
  115. method: 'POST',
  116. data: {},
  117. header: {
  118. 'Content-Type': 'application/json',
  119. 'Authorization': 'Bearer ' + uni.getStorageSync('token')
  120. },
  121. success: function(res) {
  122. that.loading = false
  123. if (res.data && res.data.code === 200) {
  124. var list = res.data.data || []
  125. for (var i = 0; i < list.length; i++) {
  126. if (list[i].isDefault === 1) {
  127. that.selectedAddress = list[i]
  128. break
  129. }
  130. }
  131. if (!that.selectedAddress && list.length > 0) {
  132. that.selectedAddress = list[0]
  133. }
  134. }
  135. },
  136. fail: function() {
  137. that.loading = false
  138. }
  139. })
  140. },
  141. goSelectAddress() {
  142. var self = this
  143. uni.navigateTo({
  144. url: '/pages/shop/address/address?selectMode=1',
  145. events: {
  146. onAddressSelect: function(data) {
  147. self.selectedAddress = data
  148. }
  149. },
  150. success: function(res) {
  151. res.eventChannel.on('onAddressSelect', function(data) {
  152. self.selectedAddress = data
  153. })
  154. }
  155. })
  156. },
  157. goAddAddress() {
  158. uni.navigateTo({ url: '/pages/shop/address-edit/address-edit' })
  159. },
  160. onSubmit() {
  161. if (!this.selectedAddress) {
  162. uni.showToast({ title: '请选择收货地址', icon: 'none' })
  163. return
  164. }
  165. if (this.items.length === 0) {
  166. uni.showToast({ title: '请选择商品', icon: 'none' })
  167. return
  168. }
  169. this.submitting = true
  170. var orderItems = []
  171. for (var i = 0; i < this.items.length; i++) {
  172. orderItems.push({
  173. productId: this.items[i].productId,
  174. quantity: this.items[i].quantity
  175. })
  176. }
  177. var addressSnapshot = JSON.stringify({
  178. receiverName: this.selectedAddress.receiverName,
  179. phone: this.selectedAddress.phone,
  180. province: this.selectedAddress.province,
  181. city: this.selectedAddress.city,
  182. district: this.selectedAddress.district,
  183. street: this.selectedAddress.street
  184. })
  185. var that = this
  186. uni.request({
  187. url: config.api('/api/product/order/create'),
  188. method: 'POST',
  189. data: {
  190. items: orderItems,
  191. remark: this.remark,
  192. addressSnapshot: addressSnapshot
  193. },
  194. header: {
  195. 'Content-Type': 'application/json',
  196. 'Authorization': 'Bearer ' + uni.getStorageSync('token')
  197. },
  198. success: function(res) {
  199. that.submitting = false
  200. if (res.data && res.data.code === 200) {
  201. var order = res.data.data
  202. uni.showToast({ title: '下单成功', icon: 'success' })
  203. uni.removeStorageSync('checkoutItems')
  204. setTimeout(function() {
  205. uni.navigateTo({
  206. url: '/pages/shop/payment/payment?orderNo=' + order.orderNo + '&amount=' + order.totalAmount
  207. })
  208. }, 1000)
  209. } else {
  210. uni.showToast({ title: res.data.message || '下单失败', icon: 'none' })
  211. }
  212. },
  213. fail: function() {
  214. that.submitting = false
  215. uni.showToast({ title: '网络请求失败', icon: 'none' })
  216. }
  217. })
  218. },
  219. formatPrice(price) {
  220. if (price === null || price === undefined) return '0.00'
  221. return (Number(price) / 100).toFixed(2)
  222. },
  223. formatPhone(phone) {
  224. if (!phone) return ''
  225. if (phone.length === 11) {
  226. return phone.substr(0, 3) + '****' + phone.substr(7)
  227. }
  228. return phone
  229. }
  230. }
  231. }
  232. </script>
  233. <style scoped>
  234. .container {
  235. min-height: 100vh;
  236. background: #f5f5f5;
  237. }
  238. .loading-wrap {
  239. display: flex;
  240. justify-content: center;
  241. padding-top: 200rpx;
  242. }
  243. .loading-text {
  244. font-size: 28rpx;
  245. color: #999;
  246. }
  247. .content {
  248. height: calc(100vh - 120rpx);
  249. }
  250. .section {
  251. background: #fff;
  252. margin: 20rpx;
  253. border-radius: 16rpx;
  254. padding: 24rpx;
  255. }
  256. .section-title {
  257. font-size: 28rpx;
  258. font-weight: bold;
  259. color: #333;
  260. margin-bottom: 20rpx;
  261. display: block;
  262. }
  263. .address-section {
  264. position: relative;
  265. }
  266. .address-content {
  267. position: relative;
  268. }
  269. .address-top {
  270. display: flex;
  271. align-items: center;
  272. margin-bottom: 10rpx;
  273. }
  274. .receiver-name {
  275. font-size: 30rpx;
  276. font-weight: bold;
  277. color: #333;
  278. margin-right: 20rpx;
  279. }
  280. .receiver-phone {
  281. font-size: 26rpx;
  282. color: #666;
  283. }
  284. .default-badge {
  285. font-size: 20rpx;
  286. color: #F97316;
  287. background: #fff7e6;
  288. padding: 2rpx 12rpx;
  289. border-radius: 20rpx;
  290. margin-left: 12rpx;
  291. }
  292. .address-detail {
  293. font-size: 26rpx;
  294. color: #999;
  295. line-height: 1.5;
  296. }
  297. .address-arrow {
  298. position: absolute;
  299. right: 0;
  300. top: 50%;
  301. font-size: 40rpx;
  302. color: #ccc;
  303. transform: translateY(-50%);
  304. }
  305. .address-empty {
  306. display: flex;
  307. align-items: center;
  308. justify-content: center;
  309. padding: 30rpx 0;
  310. position: relative;
  311. }
  312. .address-empty-icon {
  313. font-size: 40rpx;
  314. margin-right: 12rpx;
  315. }
  316. .address-empty-text {
  317. font-size: 28rpx;
  318. color: #999;
  319. }
  320. .order-item {
  321. display: flex;
  322. align-items: center;
  323. padding: 16rpx 0;
  324. border-bottom: 1rpx solid #f5f5f5;
  325. }
  326. .order-item:last-child {
  327. border-bottom: none;
  328. }
  329. .item-image {
  330. width: 80rpx;
  331. height: 80rpx;
  332. border-radius: 8rpx;
  333. background: #f0f0f0;
  334. margin-right: 16rpx;
  335. }
  336. .item-info {
  337. flex: 1;
  338. display: flex;
  339. flex-direction: column;
  340. min-width: 0;
  341. }
  342. .item-name {
  343. font-size: 26rpx;
  344. color: #333;
  345. overflow: hidden;
  346. text-overflow: ellipsis;
  347. white-space: nowrap;
  348. margin-bottom: 6rpx;
  349. }
  350. .item-qty {
  351. font-size: 22rpx;
  352. color: #999;
  353. }
  354. .item-price {
  355. font-size: 26rpx;
  356. color: #F97316;
  357. font-weight: bold;
  358. margin-left: 16rpx;
  359. }
  360. .info-row {
  361. display: flex;
  362. justify-content: space-between;
  363. align-items: center;
  364. padding: 8rpx 0;
  365. }
  366. .info-label {
  367. font-size: 26rpx;
  368. color: #999;
  369. }
  370. .info-value {
  371. font-size: 26rpx;
  372. color: #333;
  373. }
  374. .remark-input {
  375. flex: 1;
  376. text-align: right;
  377. font-size: 26rpx;
  378. color: #333;
  379. }
  380. .bottom-spacer {
  381. height: 140rpx;
  382. }
  383. .bottom-bar {
  384. position: fixed;
  385. bottom: 0;
  386. left: 0;
  387. right: 0;
  388. height: 100rpx;
  389. background: #fff;
  390. border-top: 1rpx solid #eee;
  391. display: flex;
  392. align-items: center;
  393. justify-content: space-between;
  394. padding: 0 30rpx;
  395. z-index: 100;
  396. }
  397. .bottom-left {
  398. display: flex;
  399. align-items: baseline;
  400. }
  401. .total-label {
  402. font-size: 26rpx;
  403. color: #666;
  404. }
  405. .total-amount {
  406. font-size: 36rpx;
  407. color: #F97316;
  408. font-weight: bold;
  409. }
  410. .submit-btn {
  411. background: linear-gradient(135deg, #F97316, #FB923C);
  412. color: #fff;
  413. font-size: 28rpx;
  414. font-weight: bold;
  415. padding: 0 60rpx;
  416. height: 72rpx;
  417. line-height: 72rpx;
  418. border-radius: 36rpx;
  419. border: none;
  420. }
  421. .submit-btn::after {
  422. border: none;
  423. }
  424. .submit-btn.disabled {
  425. background: #ddd;
  426. color: #999;
  427. }
  428. </style>