confirm.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. import { getOrderPayStatus, payOrderSubmit } from '@/api/common'
  73. import Confirm from '@/plugins/confirm'
  74. import { useRouter } from 'vue-router'; const router = useRouter()
  75. import { useUserStore } from '@/store/user'; const userStore = useUserStore()
  76. import { useI18n } from '@/hooks/web/useI18n'; const { t } = useI18n()
  77. const emit = defineEmits(['paySuccess'])
  78. const props = defineProps({
  79. data: {
  80. type: String,
  81. default: '',
  82. },
  83. });
  84. const state = reactive({
  85. orderPayload: {},
  86. orderInfo: {
  87. items: [], // 商品项列表
  88. price: {}, // 价格信息
  89. },
  90. showCoupon: false, // 是否展示优惠劵
  91. couponInfo: [], // 优惠劵列表
  92. showDiscount: false, // 是否展示营销活动
  93. // ========== 积分 ==========
  94. pointStatus: false, //是否使用积分
  95. });
  96. // 检测支付环境
  97. const payState = reactive({
  98. orderType: 'goods', // 订单类型; goods - 商品订单, recharge - 充值订单
  99. orderInfo: {}, // 支付单信息
  100. payStatus: 0, // 0=检测支付环境, -2=未查询到支付单信息, -1=支付已过期, 1=待支付,2=订单已支付
  101. payMethods: [], // 可选的支付方式
  102. payment: '', // 选中的支付方式
  103. });
  104. const addressState = ref({
  105. addressInfo: {}, // 选择的收货地址
  106. deliveryType: undefined, // 收货方式:1-快递配送,2-门店自提
  107. isPickUp: true, // 门店自提是否开启
  108. pickUpInfo: {}, // 选择的自提门店信息
  109. receiverName: '', // 收件人名称
  110. receiverMobile: '', // 收件人手机
  111. });
  112. watch(
  113. () => props.data,
  114. (newVal) => {
  115. if (newVal) {
  116. state.orderPayload = JSON.parse(newVal);
  117. tradeConfig()
  118. }
  119. },
  120. { immediate: true },
  121. // { deep: true }
  122. )
  123. async function tradeConfig () {
  124. // 获取交易配置
  125. const data = await getTradeConfig();
  126. addressState.value.isPickUp = data.deliveryPickUpEnabled;
  127. // 价格计算
  128. // 情况一:先自动选择“快递物流”
  129. addressState.value.deliveryType = 1;
  130. let orderCode = await getOrderInfo();
  131. if (orderCode === 0) {
  132. return;
  133. }
  134. // 情况二:失败,再自动选择“门店自提”
  135. if (addressState.value.isPickUp) {
  136. addressState.value.deliveryType = 2;
  137. let orderCode = await getOrderInfo();
  138. if (orderCode === 0) {
  139. return;
  140. }
  141. }
  142. // 情况三:都失败,则不选择
  143. addressState.value.deliveryType = undefined;
  144. await getOrderInfo()
  145. }
  146. // 提交订单
  147. function onConfirm() {
  148. if (addressState.value.deliveryType === 1 && !addressState.value.addressInfo.id) {
  149. Snackbar.warning('请选择收货地址')
  150. return;
  151. }
  152. // if (addressState.value.deliveryType === 2) {
  153. // if (!addressState.value.pickUpInfo.id) {
  154. // Snackbar.warning('请选择自提门店地址')
  155. // return;
  156. // }
  157. // if (addressState.value.receiverName === '' || addressState.value.receiverMobile === '') {
  158. // Snackbar.warning('请填写联系人或联系人电话')
  159. // return;
  160. // }
  161. // if (!/^[\u4e00-\u9fa5\w]{2,16}$/.test(addressState.value.receiverName)) {
  162. // Snackbar.warning('请填写您的真实姓名')
  163. // return;
  164. // }
  165. // if (!/^1(3|4|5|7|8|9|6)\d{9}$/.test(addressState.value.receiverMobile)) {
  166. // Snackbar.warning('请填写正确的手机号')
  167. // return;
  168. // }
  169. // }
  170. submitOrder();
  171. }
  172. const userAccount = ref(JSON.parse(localStorage.getItem('userAccount')) || {}) // 账户信息
  173. userStore.$subscribe((mutation, state) => {
  174. if (Object.keys(state.userAccount).length) userAccount.value = state.userAccount
  175. })
  176. // 获得支付方式 // 暂时只对接余额支付(wallet)
  177. const channel = 'wallet'
  178. const payOrderSubmitFun = async () => {
  179. // 提交支付订单
  180. let obj = {
  181. id: payState.orderInfo.id,
  182. channelCode: channel,
  183. channelExtras: {},
  184. };
  185. const res = await payOrderSubmit(obj)
  186. // console.log('提交支付订单payOrderSubmit:', res)
  187. Snackbar.success('支付成功,请前往我的订单查看!')
  188. emit('paySuccess')
  189. userStore.getUserAccountBalance()
  190. }
  191. // 设置支付订单信息
  192. const payImmediately = async (id) => {
  193. // 获得支付订单信息
  194. const orderRes = await getOrderPayStatus({ id, sync: true })
  195. if (!orderRes) {
  196. payState.payStatus = -2;
  197. return;
  198. }
  199. payState.orderInfo = orderRes;
  200. // 对比余额是否不足 订单金额:orderInfo?.price-0
  201. const balance = userAccount.value?.balance ? userAccount.value.balance-0 : 0
  202. // const balanceShow = userAccount.value?.balance && userAccount.value?.balance > 0 ? (userAccount.value?.balance / 100.0).toFixed(2) : 0
  203. if (balance < payState?.orderInfo?.price-0) {
  204. // 余额不足
  205. Confirm(t('common.confirmTitle'), '余额不足,是否前往充值?', {sureText: '立即前往'}).then(() => {
  206. router.push({ path: '/personalRecharge' })
  207. })
  208. } else {
  209. Confirm(t('common.confirmTitle'), `将会从余额扣除¥${fen2yuan(state.orderInfo.price.payPrice)},是否确定支付?`).then(() => {
  210. payOrderSubmitFun()
  211. })
  212. }
  213. }
  214. // 创建订单&跳转
  215. async function submitOrder() {
  216. const data = await createOrder({
  217. items: state.orderPayload.items,
  218. couponId: state.orderPayload.couponId,
  219. remark: state.orderPayload.remark,
  220. deliveryType: addressState.value.deliveryType,
  221. addressId: addressState.value.addressInfo.id, // 收件地址编号
  222. pickUpStoreId: addressState.value.pickUpInfo.id, //自提门店编号
  223. receiverName: addressState.value.receiverName, // 选择门店自提时,该字段为联系人名
  224. receiverMobile: addressState.value.receiverMobile, // 选择门店自提时,该字段为联系人手机
  225. pointStatus: state.pointStatus,
  226. combinationActivityId: state.orderPayload.combinationActivityId,
  227. combinationHeadId: state.orderPayload.combinationHeadId,
  228. seckillActivityId: state.orderPayload.seckillActivityId,
  229. pointActivityId: state.orderPayload.pointActivityId,
  230. });
  231. if (data.payOrderId && data.payOrderId > 0) payImmediately(data.payOrderId)
  232. // 更新购物车列表,如果来自购物车
  233. // if (state.orderPayload.items[0].cartId > 0) {
  234. // sheep.$store('cart').getList();
  235. // }
  236. // // 跳转到支付页面
  237. // if (data.payOrderId && data.payOrderId > 0) {
  238. // sheep.$router.redirect('/pages/pay/index', {
  239. // id: data.payOrderId,
  240. // });
  241. // } else {
  242. // sheep.$router.redirect('/pages/order/detail', {
  243. // id: data.id,
  244. // });
  245. // }
  246. }
  247. // 检查库存 & 计算订单价格
  248. async function getOrderInfo() {
  249. // 计算价格
  250. const data = await settlementOrder({
  251. items: state.orderPayload.items,
  252. couponId: state.orderPayload.couponId,
  253. deliveryType: addressState.value.deliveryType,
  254. addressId: addressState.value.addressInfo.id, // 收件地址编号
  255. pickUpStoreId: addressState.value.pickUpInfo.id, //自提门店编号
  256. receiverName: addressState.value.receiverName, // 选择门店自提时,该字段为联系人名
  257. receiverMobile: addressState.value.receiverMobile, // 选择门店自提时,该字段为联系人手机
  258. pointStatus: state.pointStatus,
  259. combinationActivityId: state.orderPayload.combinationActivityId,
  260. combinationHeadId: state.orderPayload.combinationHeadId,
  261. seckillActivityId: state.orderPayload.seckillActivityId,
  262. pointActivityId: state.orderPayload.pointActivityId,
  263. });
  264. state.orderInfo = data;
  265. state.couponInfo = data.coupons || [];
  266. // 设置收货地址
  267. if (state.orderInfo.address) {
  268. addressState.value.addressInfo = state.orderInfo.address;
  269. }
  270. return 0;
  271. }
  272. // 使用 watch 监听地址和配送方式的变化
  273. watch(addressState, async (newAddress, oldAddress) => {
  274. // 如果收货地址或配送方式有变化,则重新计算价格
  275. if (
  276. newAddress.addressInfo.id !== oldAddress.addressInfo.id ||
  277. newAddress.deliveryType !== oldAddress.deliveryType
  278. ) {
  279. await getOrderInfo();
  280. }
  281. });
  282. defineExpose({
  283. onConfirm
  284. })
  285. </script>
  286. <style lang="scss" scoped>
  287. .addressBox {
  288. margin-bottom: 20px;
  289. }
  290. .goodsListBox {
  291. // background: linear-gradient(to bottom, #e93323 0%, #e93323 100%);
  292. // background: linear-gradient(to bottom, var(--v-primary-base) 0%, var(--v-primary-base) 100%);
  293. border-radius: 4px;
  294. }
  295. .order-item {
  296. color: #7a7a7a;
  297. }
  298. .total-box-footer {
  299. .total-num {
  300. color: #7a7a7a;
  301. }
  302. }
  303. </style>