confirm.vue 17 KB

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