confirm.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. <template>
  2. <s-layout title="确认订单">
  3. <!-- 头部地址选择【配送地址】【自提地址】 -->
  4. <AddressSelection v-model="addressState" />
  5. <!-- 产品信息 -->
  6. <view class="order-card-box ss-m-b-14">
  7. <s-goods-item
  8. v-for="item in state.orderInfo.items"
  9. :key="item.skuId"
  10. :img="item.picUrl"
  11. :num="item.count"
  12. :price="item.price"
  13. :skuText="item.properties.map((property) => property.valueName).join(' ')"
  14. :title="item.spuName"
  15. marginBottom="10"
  16. />
  17. <view class="order-item ss-flex ss-col-center ss-row-between ss-p-x-20 bg-white ss-r-10">
  18. <view class="item-title">订单备注</view>
  19. <view class="ss-flex ss-col-center">
  20. <uni-easyinput
  21. v-model="state.orderPayload.remark"
  22. :clearable="false"
  23. :inputBorder="false"
  24. maxlength="20"
  25. placeholder="建议留言前先与商家沟通"
  26. />
  27. </view>
  28. </view>
  29. </view>
  30. <!-- 价格信息 -->
  31. <view class="bg-white total-card-box ss-p-20 ss-m-b-14 ss-r-10">
  32. <view class="total-box-content border-bottom">
  33. <view class="order-item ss-flex ss-col-center ss-row-between">
  34. <view class="item-title">产品金额</view>
  35. <view class="ss-flex ss-col-center">
  36. <text class="item-value ss-m-r-24">
  37. ¥{{ fen2yuan(state.orderInfo.price.totalPrice) }}
  38. </text>
  39. </view>
  40. </view>
  41. <view
  42. v-if="state.orderPayload.pointActivityId"
  43. class="order-item ss-flex ss-col-center ss-row-between"
  44. >
  45. <view class="item-title">兑换积分</view>
  46. <view class="ss-flex ss-col-center">
  47. <image
  48. :src="sheep.$url.static('/static/img/shop/goods/score1.svg')"
  49. class="score-img"
  50. />
  51. <text class="item-value ss-m-r-24">
  52. {{ state.orderInfo.usePoint }}
  53. </text>
  54. </view>
  55. </view>
  56. <view
  57. v-if="state.orderInfo.type === 0 || state.orderPayload.pointActivityId"
  58. class="order-item ss-flex ss-col-center ss-row-between"
  59. >
  60. <view class="item-title">积分抵扣</view>
  61. <view class="ss-flex ss-col-center">
  62. {{ state.pointStatus || state.orderPayload.pointActivityId ? '剩余积分' : '当前积分' }}
  63. <image
  64. :src="sheep.$url.static('/static/img/shop/goods/score1.svg')"
  65. class="score-img"
  66. />
  67. <text class="item-value ss-m-r-24">
  68. {{
  69. state.pointStatus || state.orderPayload.pointActivityId
  70. ? state.orderInfo.totalPoint - state.orderInfo.usePoint
  71. : state.orderInfo.totalPoint || 0
  72. }}
  73. </text>
  74. <checkbox-group v-if="!state.orderPayload.pointActivityId" @change="changeIntegral">
  75. <checkbox
  76. :checked="state.pointStatus"
  77. :disabled="!state.orderInfo.totalPoint || state.orderInfo.totalPoint <= 0"
  78. />
  79. </checkbox-group>
  80. </view>
  81. </view>
  82. <!-- 快递配置时,信息的展示 -->
  83. <view
  84. v-if="addressState.deliveryType === 1"
  85. class="order-item ss-flex ss-col-center ss-row-between"
  86. >
  87. <view class="item-title">运费</view>
  88. <view class="ss-flex ss-col-center">
  89. <text v-if="state.orderInfo.price.deliveryPrice > 0" class="item-value ss-m-r-24">
  90. +¥{{ fen2yuan(state.orderInfo.price.deliveryPrice) }}
  91. </text>
  92. <view v-else class="item-value ss-m-r-24">免运费</view>
  93. </view>
  94. </view>
  95. <!-- 门店自提时,需要填写姓名和手机号 -->
  96. <view
  97. v-if="addressState.deliveryType === 2"
  98. class="order-item ss-flex ss-col-center ss-row-between"
  99. >
  100. <view class="item-title">联系人</view>
  101. <view class="ss-flex ss-col-center">
  102. <uni-easyinput
  103. v-model="addressState.receiverName"
  104. :clearable="false"
  105. :inputBorder="false"
  106. maxlength="20"
  107. placeholder="请填写您的联系姓名"
  108. />
  109. </view>
  110. </view>
  111. <view
  112. v-if="addressState.deliveryType === 2"
  113. class="order-item ss-flex ss-col-center ss-row-between"
  114. >
  115. <view class="item-title">联系电话</view>
  116. <view class="ss-flex ss-col-center">
  117. <uni-easyinput
  118. v-model="addressState.receiverMobile"
  119. :clearable="false"
  120. :inputBorder="false"
  121. maxlength="20"
  122. placeholder="请填写您的联系电话"
  123. />
  124. </view>
  125. </view>
  126. <!-- 优惠劵:只有 type = 0 普通订单(非拼团、秒杀、砍价),才可以使用优惠劵 -->
  127. <view
  128. v-if="state.orderInfo.type === 0"
  129. class="order-item ss-flex ss-col-center ss-row-between"
  130. >
  131. <view class="item-title">优惠券</view>
  132. <view class="ss-flex ss-col-center" @tap="state.showCoupon = true">
  133. <text v-if="state.orderPayload.couponId > 0" class="item-value text-red">
  134. -¥{{ fen2yuan(state.orderInfo.price.couponPrice) }}
  135. </text>
  136. <text
  137. v-else
  138. :class="
  139. state.couponInfo.filter((coupon) => coupon.match).length > 0
  140. ? 'text-red'
  141. : 'text-disabled'
  142. "
  143. class="item-value"
  144. >
  145. {{
  146. state.couponInfo.filter((coupon) => coupon.match).length > 0
  147. ? state.couponInfo.filter((coupon) => coupon.match).length + ' 张可用'
  148. : '暂无可用优惠券'
  149. }}
  150. </text>
  151. <text class="_icon-forward item-icon" />
  152. </view>
  153. </view>
  154. <view
  155. v-if="state.orderInfo.price.discountPrice > 0"
  156. class="order-item ss-flex ss-col-center ss-row-between"
  157. >
  158. <view class="item-title">活动优惠</view>
  159. <view class="ss-flex ss-col-center" @tap="state.showDiscount = true">
  160. <text class="item-value text-red">
  161. -¥{{ fen2yuan(state.orderInfo.price.discountPrice) }}
  162. </text>
  163. <text class="_icon-forward item-icon" />
  164. </view>
  165. </view>
  166. <view
  167. v-if="state.orderInfo.price.vipPrice > 0"
  168. class="order-item ss-flex ss-col-center ss-row-between"
  169. >
  170. <view class="item-title">会员优惠</view>
  171. <view class="ss-flex ss-col-center">
  172. <text class="item-value text-red">
  173. -¥{{ fen2yuan(state.orderInfo.price.vipPrice) }}
  174. </text>
  175. </view>
  176. </view>
  177. </view>
  178. <view class="total-box-footer ss-font-28 ss-flex ss-row-right ss-col-center ss-m-r-28">
  179. <view class="total-num ss-m-r-20">
  180. 共{{ state.orderInfo.items.reduce((acc, item) => acc + item.count, 0) }}件
  181. </view>
  182. <view>合计:</view>
  183. <view class="total-num text-red"> ¥{{ fen2yuan(state.orderInfo.price.payPrice) }}</view>
  184. </view>
  185. </view>
  186. <!-- 选择优惠券弹框 -->
  187. <s-coupon-select
  188. v-model="state.couponInfo"
  189. :show="state.showCoupon"
  190. @close="state.showCoupon = false"
  191. @confirm="onSelectCoupon"
  192. />
  193. <!-- 满额折扣弹框 TODO @puhui999:【折扣】后续要把优惠信息打进去 -->
  194. <s-discount-list
  195. v-model="state.orderInfo"
  196. :show="state.showDiscount"
  197. @close="state.showDiscount = false"
  198. />
  199. <!-- 底部 -->
  200. <su-fixed :index="200" :noFixed="false" :opacity="false" bg="bg-white" bottom placeholder>
  201. <view class="footer-box border-top ss-flex ss-row-between ss-p-x-20 ss-col-center">
  202. <view class="total-box-footer ss-flex ss-col-center">
  203. <view class="total-num ss-font-30 text-red">
  204. ¥{{ fen2yuan(state.orderInfo.price.payPrice) }}
  205. </view>
  206. </view>
  207. <button
  208. class="ss-reset-button ui-BG-Main-Gradient ss-r-40 submit-btn ui-Shadow-Main"
  209. @tap="onConfirm"
  210. >
  211. 提交订单
  212. </button>
  213. </view>
  214. </su-fixed>
  215. </s-layout>
  216. </template>
  217. <script setup>
  218. import { reactive, ref, watch } from 'vue';
  219. import { onLoad } from '@dcloudio/uni-app';
  220. import AddressSelection from '@/pages/order/addressSelection.vue';
  221. import sheep from '@/sheep';
  222. import OrderApi from '@/sheep/api/trade/order';
  223. import TradeConfigApi from '@/sheep/api/trade/config';
  224. import { fen2yuan } from '@/sheep/hooks/useGoods';
  225. import { DeliveryTypeEnum } from '@/sheep/util/const';
  226. const state = reactive({
  227. orderPayload: {},
  228. orderInfo: {
  229. items: [], // 产品项列表
  230. price: {}, // 价格信息
  231. },
  232. showCoupon: false, // 是否展示优惠劵
  233. couponInfo: [], // 优惠劵列表
  234. showDiscount: false, // 是否展示营销活动
  235. // ========== 积分 ==========
  236. pointStatus: false, //是否使用积分
  237. });
  238. const addressState = ref({
  239. addressInfo: {}, // 选择的收货地址
  240. deliveryType: undefined, // 收货方式:1-快递配送,2-门店自提
  241. isPickUp: true, // 门店自提是否开启
  242. pickUpInfo: {}, // 选择的自提门店信息
  243. receiverName: '', // 收件人名称
  244. receiverMobile: '', // 收件人手机
  245. });
  246. // ========== 积分 ==========
  247. /**
  248. * 使用积分抵扣
  249. */
  250. const changeIntegral = async () => {
  251. state.pointStatus = !state.pointStatus;
  252. await getOrderInfo();
  253. };
  254. // 选择优惠券
  255. async function onSelectCoupon(couponId) {
  256. state.orderPayload.couponId = couponId;
  257. await getOrderInfo();
  258. state.showCoupon = false;
  259. }
  260. // 提交订单
  261. function onConfirm() {
  262. if (addressState.value.deliveryType === 1 && !addressState.value.addressInfo.id) {
  263. sheep.$helper.toast('请选择收货地址');
  264. return;
  265. }
  266. if (addressState.value.deliveryType === 2) {
  267. if (!addressState.value.pickUpInfo.id) {
  268. sheep.$helper.toast('请选择自提门店地址');
  269. return;
  270. }
  271. if (addressState.value.receiverName === '' || addressState.value.receiverMobile === '') {
  272. sheep.$helper.toast('请填写联系人或联系人电话');
  273. return;
  274. }
  275. if (!/^[\u4e00-\u9fa5\w]{2,16}$/.test(addressState.value.receiverName)) {
  276. sheep.$helper.toast('请填写您的真实姓名');
  277. return;
  278. }
  279. if (!/^1(3|4|5|7|8|9|6)\d{9}$/.test(addressState.value.receiverMobile)) {
  280. sheep.$helper.toast('请填写正确的手机号');
  281. return;
  282. }
  283. }
  284. submitOrder();
  285. }
  286. // 创建订单&跳转
  287. async function submitOrder() {
  288. const { code, data } = await OrderApi.createOrder({
  289. items: state.orderPayload.items,
  290. couponId: state.orderPayload.couponId,
  291. remark: state.orderPayload.remark,
  292. deliveryType: addressState.value.deliveryType,
  293. addressId: addressState.value.addressInfo.id, // 收件地址编号
  294. pickUpStoreId: addressState.value.pickUpInfo.id, //自提门店编号
  295. receiverName: addressState.value.receiverName, // 选择门店自提时,该字段为联系人名
  296. receiverMobile: addressState.value.receiverMobile, // 选择门店自提时,该字段为联系人手机
  297. pointStatus: state.pointStatus,
  298. combinationActivityId: state.orderPayload.combinationActivityId,
  299. combinationHeadId: state.orderPayload.combinationHeadId,
  300. seckillActivityId: state.orderPayload.seckillActivityId,
  301. pointActivityId: state.orderPayload.pointActivityId,
  302. });
  303. if (code !== 0) {
  304. return;
  305. }
  306. // 更新购物车列表,如果来自购物车
  307. if (state.orderPayload.items[0].cartId > 0) {
  308. sheep.$store('cart').getList();
  309. }
  310. // 跳转到支付页面
  311. if (data.payOrderId && data.payOrderId > 0) {
  312. sheep.$router.redirect('/pages/pay/index', {
  313. id: data.payOrderId,
  314. });
  315. } else {
  316. sheep.$router.redirect('/pages/order/detail', {
  317. id: data.id,
  318. });
  319. }
  320. }
  321. // 检查库存 & 计算订单价格
  322. async function getOrderInfo() {
  323. // 计算价格
  324. const { data, code } = await OrderApi.settlementOrder({
  325. items: state.orderPayload.items,
  326. couponId: state.orderPayload.couponId,
  327. deliveryType: addressState.value.deliveryType,
  328. addressId: addressState.value.addressInfo.id, // 收件地址编号
  329. pickUpStoreId: addressState.value.pickUpInfo.id, //自提门店编号
  330. receiverName: addressState.value.receiverName, // 选择门店自提时,该字段为联系人名
  331. receiverMobile: addressState.value.receiverMobile, // 选择门店自提时,该字段为联系人手机
  332. pointStatus: state.pointStatus,
  333. combinationActivityId: state.orderPayload.combinationActivityId,
  334. combinationHeadId: state.orderPayload.combinationHeadId,
  335. seckillActivityId: state.orderPayload.seckillActivityId,
  336. pointActivityId: state.orderPayload.pointActivityId,
  337. });
  338. if (code !== 0) {
  339. return code;
  340. }
  341. state.orderInfo = data;
  342. state.couponInfo = data.coupons || [];
  343. // 设置收货地址
  344. if (state.orderInfo.address) {
  345. addressState.value.addressInfo = state.orderInfo.address;
  346. }
  347. return code;
  348. }
  349. onLoad(async (options) => {
  350. // 解析参数
  351. if (!options.data) {
  352. sheep.$helper.toast('参数不正确,请检查!');
  353. return;
  354. }
  355. state.orderPayload = JSON.parse(options.data);
  356. // 获取交易配置
  357. const { data, code } = await TradeConfigApi.getTradeConfig();
  358. if (code === 0) {
  359. addressState.value.isPickUp = data.deliveryPickUpEnabled;
  360. }
  361. // 价格计算
  362. // 情况一:先自动选择“快递物流”
  363. addressState.value.deliveryType = DeliveryTypeEnum.EXPRESS.type;
  364. let orderCode = await getOrderInfo();
  365. if (orderCode === 0) {
  366. return;
  367. }
  368. // 情况二:失败,再自动选择“门店自提”
  369. if (addressState.value.isPickUp) {
  370. addressState.value.deliveryType = DeliveryTypeEnum.PICK_UP.type;
  371. let orderCode = await getOrderInfo();
  372. if (orderCode === 0) {
  373. return;
  374. }
  375. }
  376. // 情况三:都失败,则不选择
  377. addressState.value.deliveryType = undefined;
  378. await getOrderInfo();
  379. });
  380. // 使用 watch 监听地址和配送方式的变化
  381. watch(addressState, async (newAddress, oldAddress) => {
  382. // 如果收货地址或配送方式有变化,则重新计算价格
  383. if (
  384. newAddress.addressInfo.id !== oldAddress.addressInfo.id ||
  385. newAddress.deliveryType !== oldAddress.deliveryType
  386. ) {
  387. await getOrderInfo();
  388. }
  389. });
  390. </script>
  391. <style lang="scss" scoped>
  392. :deep() {
  393. .uni-input-wrapper {
  394. width: 320rpx;
  395. }
  396. .uni-easyinput__content-input {
  397. font-size: 28rpx;
  398. height: 72rpx;
  399. text-align: right !important;
  400. padding-right: 0 !important;
  401. .uni-input-input {
  402. font-weight: 500;
  403. color: #333333;
  404. font-size: 26rpx;
  405. height: 32rpx;
  406. margin-top: 4rpx;
  407. }
  408. }
  409. .uni-easyinput__content {
  410. display: flex !important;
  411. align-items: center !important;
  412. justify-content: right !important;
  413. }
  414. }
  415. .score-img {
  416. width: 36rpx;
  417. height: 36rpx;
  418. margin: 0 4rpx;
  419. }
  420. .order-item {
  421. height: 80rpx;
  422. .item-title {
  423. font-size: 28rpx;
  424. font-weight: 400;
  425. }
  426. .item-value {
  427. font-size: 28rpx;
  428. font-weight: 500;
  429. font-family: OPPOSANS;
  430. }
  431. .text-disabled {
  432. color: #bbbbbb;
  433. }
  434. .item-icon {
  435. color: $dark-9;
  436. }
  437. .remark-input {
  438. text-align: right;
  439. }
  440. .item-placeholder {
  441. color: $dark-9;
  442. font-size: 26rpx;
  443. text-align: right;
  444. }
  445. }
  446. .total-box-footer {
  447. height: 90rpx;
  448. .total-num {
  449. color: #333333;
  450. font-family: OPPOSANS;
  451. }
  452. }
  453. .footer-box {
  454. height: 100rpx;
  455. .submit-btn {
  456. width: 240rpx;
  457. height: 70rpx;
  458. font-size: 28rpx;
  459. font-weight: 500;
  460. .goto-pay-text {
  461. line-height: 28rpx;
  462. }
  463. }
  464. .cancel-btn {
  465. width: 240rpx;
  466. height: 80rpx;
  467. font-size: 26rpx;
  468. background-color: #e5e5e5;
  469. color: $dark-9;
  470. }
  471. }
  472. .title {
  473. font-size: 36rpx;
  474. font-weight: bold;
  475. color: #333333;
  476. }
  477. .subtitle {
  478. font-size: 28rpx;
  479. color: #999999;
  480. }
  481. .cicon-checkbox {
  482. font-size: 36rpx;
  483. color: var(--ui-BG-Main);
  484. }
  485. .cicon-box {
  486. font-size: 36rpx;
  487. color: #999999;
  488. }
  489. </style>