confirm.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <template>
  2. <div>
  3. <!-- 头部地址选择【配送地址】【自提地址】 -->
  4. <AddressSelection v-model="addressState" class="addressBox" />
  5. <!-- 购买的商品信息 -->
  6. <v-card class="goodsListBox mb-3 pa-3">
  7. <s-goods-item
  8. v-for="(item, index) in state.orderInfo.items"
  9. :key="item.skuId"
  10. :img="item.picUrl"
  11. :title="item.spuName"
  12. :skuText="item.properties.map((property) => property.valueName).join(' ')"
  13. :price="item.price"
  14. :num="item.count"
  15. :style="{'marginTop': index ? '8px' : '0px'}"
  16. />
  17. </v-card>
  18. <!-- 价格信息 -->
  19. <div>
  20. <div>
  21. <div class="order-item d-flex">
  22. <div class="item-title mr-3 ">商品金额:</div>
  23. <div>¥{{ fen2yuan(state.orderInfo.price.totalPrice) }}</div>
  24. </div>
  25. <!-- 快递配置时,信息的展示 -->
  26. <div
  27. class="order-item d-flex"
  28. v-if="addressState.deliveryType === 1"
  29. >
  30. <div class="item-title mr-3">运{{ spaces() }}费:</div>
  31. <div>
  32. <span class="text-red" v-if="state.orderInfo.price.deliveryPrice > 0">
  33. +¥{{ fen2yuan(state.orderInfo.price.deliveryPrice) }}
  34. </span>
  35. <div class="item-value" v-else>免运费</div>
  36. </div>
  37. </div>
  38. <!-- 门店自提时,需要填写姓名和手机号 -->
  39. </div>
  40. <div class="mt-5">
  41. <v-text-field
  42. v-model="state.orderPayload.remark"
  43. label="订单备注"
  44. placeholder="建议留言前先与商家沟通"
  45. variant="outlined"
  46. density="compact"
  47. color="primary"
  48. ></v-text-field>
  49. </div>
  50. <div class="total-box-footer d-flex flex-column align-end">
  51. <div class="d-flex">
  52. <div class="mr-3">
  53. <span class="total-num">共</span>
  54. <span class="mx-1" style="color: var(--v-primary-base);">{{ state.orderInfo.items.reduce((acc, item) => acc + item.count, 0) }}</span>
  55. <span class="total-num">件</span>
  56. </div>
  57. <div>合计:</div>
  58. <div class="total-num text-red"> ¥{{ fen2yuan(state.orderInfo.price.payPrice) }}</div>
  59. </div>
  60. </div>
  61. </div>
  62. </div>
  63. </template>
  64. <script setup>
  65. import { reactive, ref, watch } from 'vue'
  66. import AddressSelection from './addressSelection.vue'
  67. import sGoodsItem from '@/views/mall/components/s-goods-item'
  68. import { fen2yuan } from '@/hooks/web/useGoods'
  69. import { spaces } from '@/utils/index.js'
  70. import { getTradeConfig, createOrder, settlementOrder } from '@/api/mall/trade'
  71. import Snackbar from '@/plugins/snackbar'
  72. const emit = defineEmits(['orderCreated'])
  73. const props = defineProps({
  74. data: {
  75. type: String,
  76. default: '',
  77. },
  78. });
  79. const state = reactive({
  80. orderPayload: {},
  81. orderInfo: {
  82. items: [], // 商品项列表
  83. price: {}, // 价格信息
  84. },
  85. showCoupon: false, // 是否展示优惠劵
  86. couponInfo: [], // 优惠劵列表
  87. showDiscount: false, // 是否展示营销活动
  88. // ========== 积分 ==========
  89. pointStatus: false, //是否使用积分
  90. });
  91. // 检测支付环境
  92. const payState = reactive({
  93. orderType: 'goods', // 订单类型; goods - 商品订单, recharge - 充值订单
  94. orderInfo: {}, // 支付单信息
  95. payStatus: 0, // 0=检测支付环境, -2=未查询到支付单信息, -1=支付已过期, 1=待支付,2=订单已支付
  96. payMethods: [], // 可选的支付方式
  97. payment: '', // 选中的支付方式
  98. });
  99. const addressState = ref({
  100. addressInfo: {}, // 选择的收货地址
  101. deliveryType: undefined, // 收货方式:1-快递配送,2-门店自提
  102. isPickUp: true, // 门店自提是否开启
  103. pickUpInfo: {}, // 选择的自提门店信息
  104. receiverName: '', // 收件人名称
  105. receiverMobile: '', // 收件人手机
  106. });
  107. watch(
  108. () => props.data,
  109. (newVal) => {
  110. if (newVal) {
  111. state.orderPayload = JSON.parse(newVal);
  112. tradeConfig()
  113. }
  114. },
  115. { immediate: true },
  116. // { deep: true }
  117. )
  118. async function tradeConfig () {
  119. // 获取交易配置
  120. const data = await getTradeConfig();
  121. addressState.value.isPickUp = data.deliveryPickUpEnabled;
  122. // 价格计算
  123. // 情况一:先自动选择“快递物流”
  124. addressState.value.deliveryType = 1;
  125. let orderCode = await getOrderInfo();
  126. if (orderCode === 0) {
  127. return;
  128. }
  129. // 情况二:失败,再自动选择“门店自提”
  130. if (addressState.value.isPickUp) {
  131. addressState.value.deliveryType = 2;
  132. let orderCode = await getOrderInfo();
  133. if (orderCode === 0) {
  134. return;
  135. }
  136. }
  137. // 情况三:都失败,则不选择
  138. addressState.value.deliveryType = undefined;
  139. await getOrderInfo()
  140. }
  141. // 提交订单
  142. function onConfirm() {
  143. if (addressState.value.deliveryType === 1 && !addressState.value.addressInfo.id) {
  144. Snackbar.warning('请选择收货地址')
  145. return;
  146. }
  147. submitOrder()
  148. }
  149. // 创建订单&跳转
  150. async function submitOrder() {
  151. const data = await createOrder({
  152. items: state.orderPayload.items,
  153. couponId: state.orderPayload.couponId,
  154. remark: state.orderPayload.remark,
  155. deliveryType: addressState.value.deliveryType,
  156. addressId: addressState.value.addressInfo.id, // 收件地址编号
  157. pickUpStoreId: addressState.value.pickUpInfo.id, //自提门店编号
  158. receiverName: addressState.value.receiverName, // 选择门店自提时,该字段为联系人名
  159. receiverMobile: addressState.value.receiverMobile, // 选择门店自提时,该字段为联系人手机
  160. pointStatus: state.pointStatus,
  161. combinationActivityId: state.orderPayload.combinationActivityId,
  162. combinationHeadId: state.orderPayload.combinationHeadId,
  163. seckillActivityId: state.orderPayload.seckillActivityId,
  164. pointActivityId: state.orderPayload.pointActivityId,
  165. });
  166. if (!data.payOrderId && data.payOrderId > 0) return
  167. emit('orderCreated', data.payOrderId) // 更新购物车列表,如果来自购物车
  168. // payImmediately(data.payOrderId)
  169. }
  170. // 检查库存 & 计算订单价格
  171. async function getOrderInfo() {
  172. // 计算价格
  173. const data = await settlementOrder({
  174. items: state.orderPayload.items,
  175. couponId: state.orderPayload.couponId,
  176. deliveryType: addressState.value.deliveryType,
  177. addressId: addressState.value.addressInfo.id, // 收件地址编号
  178. pickUpStoreId: addressState.value.pickUpInfo.id, //自提门店编号
  179. receiverName: addressState.value.receiverName, // 选择门店自提时,该字段为联系人名
  180. receiverMobile: addressState.value.receiverMobile, // 选择门店自提时,该字段为联系人手机
  181. pointStatus: state.pointStatus,
  182. combinationActivityId: state.orderPayload.combinationActivityId,
  183. combinationHeadId: state.orderPayload.combinationHeadId,
  184. seckillActivityId: state.orderPayload.seckillActivityId,
  185. pointActivityId: state.orderPayload.pointActivityId,
  186. });
  187. state.orderInfo = data;
  188. state.couponInfo = data.coupons || [];
  189. // 设置收货地址
  190. if (state.orderInfo.address) {
  191. addressState.value.addressInfo = state.orderInfo.address;
  192. }
  193. return 0;
  194. }
  195. // 使用 watch 监听地址和配送方式的变化
  196. watch(addressState, async (newAddress, oldAddress) => {
  197. // 如果收货地址或配送方式有变化,则重新计算价格
  198. if (
  199. newAddress.addressInfo.id !== oldAddress.addressInfo.id ||
  200. newAddress.deliveryType !== oldAddress.deliveryType
  201. ) {
  202. await getOrderInfo();
  203. }
  204. });
  205. defineExpose({
  206. onConfirm
  207. })
  208. </script>
  209. <style lang="scss" scoped>
  210. .addressBox {
  211. margin-bottom: 20px;
  212. }
  213. .goodsListBox {
  214. // background: linear-gradient(to bottom, #e93323 0%, #e93323 100%);
  215. // background: linear-gradient(to bottom, var(--v-primary-base) 0%, var(--v-primary-base) 100%);
  216. border-radius: 4px;
  217. }
  218. .order-item {
  219. color: #7a7a7a;
  220. }
  221. .total-box-footer {
  222. .total-num {
  223. color: #7a7a7a;
  224. }
  225. }
  226. </style>