confirm.vue 18 KB

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