checkout.vue 10 KB

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