checkout.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <template>
  2. <view class="wrap">
  3. <view class="uv-m-t-5">
  4. <uv-cell-group>
  5. <uv-cell is-link icon="map" :title="addr.name+' '+addr.tel" @click="chooseAddr"
  6. :label="addr.wholeAddressInfo">
  7. </uv-cell>
  8. </uv-cell-group>
  9. </view>
  10. <uv-list>
  11. <uv-list-item v-for="(item,index) in cartList" :key="item.id" border>
  12. <template #body>
  13. <view class="gl-item">
  14. <view class="gl-img">
  15. <uv-image width="170rpx" height="170rpx" :src="shopImage(item.goods.pic)">
  16. </uv-image>
  17. </view>
  18. <view class="gl-content">
  19. <view class="gl-name">{{item.title}}</view>
  20. <view class="gl-descript">{{item.goods.descript}}</view>
  21. <view class="gl-price">
  22. <uv-row>
  23. <uv-col :span="10">
  24. ¥{{formatPrice(item.price)}}
  25. </uv-col>
  26. <uv-col :span="2">
  27. x{{item.count}}
  28. </uv-col>
  29. </uv-row>
  30. </view>
  31. </view>
  32. </view>
  33. </template>
  34. </uv-list-item>
  35. </uv-list>
  36. <view class="navigation">
  37. <view class="left">
  38. <view class="item total-price">
  39. 合计: ¥{{formatPrice(totalPrice)}}
  40. </view>
  41. </view>
  42. <view class="right">
  43. <uv-button type="error" shape="circle" @click="submit">提交订单</uv-button>
  44. </view>
  45. </view>
  46. </view>
  47. </template>
  48. <script>
  49. import {
  50. getRequest,
  51. saveOrder
  52. } from '@/request/api/shop.js'
  53. export default {
  54. data() {
  55. return {
  56. ids: '',
  57. addr: {
  58. name: '',
  59. tel: ''
  60. },
  61. chooseAddrId: '',
  62. cartList: []
  63. }
  64. },
  65. computed: {
  66. totalPrice() {
  67. return this.cartList.reduce((total, item) => total + (parseFloat(item.price) * item.count), 0)
  68. }
  69. },
  70. onShow() {
  71. let chooseAddrId = uni.getStorageSync('chooseAddrId');
  72. this.chooseAddrId = chooseAddrId;
  73. this.init();
  74. },
  75. onLoad(option) {
  76. this.ids = option.ids
  77. uni.setStorageSync('idCarts', option.ids);
  78. this.init();
  79. },
  80. methods: {
  81. async init() {
  82. if (!this.ids) return;
  83. let url = 'user/order/prepareCheckout?idCarts=' + this.ids
  84. if (this.chooseAddrId) {
  85. url += '&chosenAddressId=' + this.chooseAddrId
  86. }
  87. let requestData = await getRequest(url);
  88. if (requestData.state) {
  89. let res = requestData.data;
  90. let addr = res.addr
  91. if (!addr || !addr.name) {
  92. this.addr.name = '请选择收获地址'
  93. } else {
  94. this.addr = addr
  95. this.chooseAddrId = addr.id
  96. }
  97. let cartList = res.list
  98. for (const index in cartList) {
  99. let cart = cartList[index]
  100. }
  101. this.cartList = cartList;
  102. }
  103. },
  104. formatPrice(price) {
  105. if (!price) return 0;
  106. return (price / 100).toFixed(2)
  107. },
  108. chooseAddr() {
  109. this.$navigateTo('/subPages/shopPage/addressList/addressList?choose=true&chooseAddrId=' + this
  110. .chooseAddrId);
  111. },
  112. async submit() {
  113. if (!this.addr || !this.addr.tel || this.addr.tel == '') {
  114. this.$u.toast('请选择收货地址')
  115. return
  116. }
  117. let idCarts = ''
  118. for (var i in this.cartList) {
  119. idCarts += this.cartList[i].id + ','
  120. }
  121. const params = {
  122. idAddress: this.chooseAddrId,
  123. idCarts: idCarts
  124. }
  125. let saveData = await saveOrder(this.chooseAddrId, idCarts);
  126. if (saveData.state) {
  127. const order = saveData.data;
  128. uni.setStorageSync('chooseAddrId', undefined);
  129. uni.getStorageSync('idCarts', undefined);
  130. uni.redirectTo({
  131. url: '/subPages/shopPage/payment/payment?orderSn=' + order.orderSn + '&totalPrice=' +
  132. order.totalPrice
  133. });
  134. }
  135. }
  136. }
  137. }
  138. </script>
  139. <style lang="scss" scoped>
  140. .wrap {
  141. padding: 30rpx 0rpx;
  142. .uv-m-t-5 {
  143. background: #ffffff;
  144. margin-bottom: 30rpx;
  145. }
  146. .gl-content {
  147. flex: 1;
  148. width: 0;
  149. }
  150. .gl-item {
  151. display: flex;
  152. align-items: center;
  153. width: 100%;
  154. }
  155. .gl-name {
  156. font-size: 26rpx;
  157. }
  158. .gl-img {
  159. margin: 0 20rpx;
  160. }
  161. .gl-descript {
  162. margin-top: 14rpx;
  163. font-size: 20rpx;
  164. }
  165. .gl-price {
  166. margin-top: 26rpx;
  167. font-size: 24rpx;
  168. color: #FA3534;
  169. }
  170. .navigation {
  171. background-color: #ffffff;
  172. box-shadow: 0px 2px 10px rgba(3, 3, 3, 0.1);
  173. position: fixed;
  174. bottom: 0;
  175. left: 0;
  176. right: 0;
  177. height: 100rpx;
  178. display: flex;
  179. align-items: center;
  180. justify-content: flex-end;
  181. padding-bottom: 0;
  182. padding-bottom: constant(safe-area-inset-bottom);
  183. padding-bottom: env(safe-area-inset-bottom);
  184. padding-right: 40rpx;
  185. padding-left: 40rpx;
  186. justify-content: space-between;
  187. .left {
  188. display: flex;
  189. font-size: 20rpx;
  190. .item {
  191. margin: 0 8rpx;
  192. }
  193. .total-price {
  194. font-size: 30rpx;
  195. color: red;
  196. padding-top: 20rpx;
  197. }
  198. }
  199. .right {
  200. display: flex;
  201. font-size: 28rpx;
  202. align-items: center;
  203. }
  204. }
  205. }
  206. </style>