confirm.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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) state.orderPayload.email = '123@qq.com' // 测试
  320. if (spuType.value !== '0' && !state.orderPayload.email) {
  321. uni.showToast({ title: '请填写接收邮箱!', icon: 'none', duration: 2000 })
  322. return
  323. }
  324. submitOrder()
  325. }
  326. // 创建订单&跳转
  327. async function submitOrder() {
  328. const { code, data } = await OrderApi.createOrder({
  329. items: state.orderPayload.items,
  330. couponId: state.orderPayload.couponId,
  331. remark: state.orderPayload.remark,
  332. deliveryType: addressState.value.deliveryType,
  333. addressId: addressState.value.addressInfo.id, // 收件地址编号
  334. pickUpStoreId: addressState.value.pickUpInfo.id, //自提门店编号
  335. receiverName: addressState.value.receiverName, // 选择门店自提时,该字段为联系人名
  336. receiverMobile: addressState.value.receiverMobile, // 选择门店自提时,该字段为联系人手机
  337. pointStatus: state.pointStatus,
  338. combinationActivityId: state.orderPayload.combinationActivityId,
  339. combinationHeadId: state.orderPayload.combinationHeadId,
  340. seckillActivityId: state.orderPayload.seckillActivityId,
  341. pointActivityId: state.orderPayload.pointActivityId,
  342. receiverExtend: {
  343. email: state.orderPayload.email
  344. }
  345. });
  346. if (code !== 0) {
  347. return;
  348. }
  349. if (state.orderPayload?.city?.length) setTargetData(data.id)
  350. // 更新购物车列表,如果来自购物车
  351. if (state.orderPayload.items[0].cartId > 0) {
  352. sheep.$store('cart').getList();
  353. }
  354. // 跳转到支付页面
  355. if (data.payOrderId && data.payOrderId > 0) {
  356. sheep.$router.redirect('/pages/pay/index', {
  357. id: data.payOrderId,
  358. salesLotterySpuId,
  359. });
  360. } else {
  361. sheep.$router.redirect('/pages/order/detail', {
  362. id: data.id,
  363. });
  364. }
  365. }
  366. const spuType = ref('')
  367. let salesLotterySpuId = ''
  368. // 检查库存 & 计算订单价格
  369. async function getOrderInfo() {
  370. // 计算价格
  371. const { data, code } = await OrderApi.settlementOrder({
  372. items: state.orderPayload.items,
  373. couponId: state.orderPayload.couponId,
  374. deliveryType: addressState.value.deliveryType,
  375. addressId: addressState.value.addressInfo.id, // 收件地址编号
  376. pickUpStoreId: addressState.value.pickUpInfo.id, //自提门店编号
  377. receiverName: addressState.value.receiverName, // 选择门店自提时,该字段为联系人名
  378. receiverMobile: addressState.value.receiverMobile, // 选择门店自提时,该字段为联系人手机
  379. pointStatus: state.pointStatus,
  380. combinationActivityId: state.orderPayload.combinationActivityId,
  381. combinationHeadId: state.orderPayload.combinationHeadId,
  382. seckillActivityId: state.orderPayload.seckillActivityId,
  383. pointActivityId: state.orderPayload.pointActivityId,
  384. });
  385. if (code !== 0) {
  386. return code;
  387. }
  388. state.orderInfo = data;
  389. spuType.value = state.orderInfo?.items?.length && state.orderInfo.items.every(k => k.spuType === '0') ? '0' : ''
  390. salesLotterySpuId = Boolean(!spuType.value) ? state.orderPayload?.spuId : ''
  391. state.couponInfo = data.coupons || [];
  392. // 设置收货地址
  393. if (state.orderInfo.address) {
  394. addressState.value.addressInfo = state.orderInfo.address;
  395. }
  396. return code;
  397. }
  398. onLoad(async (options) => {
  399. // 解析参数
  400. if (!options.data) {
  401. sheep.$helper.toast('参数不正确,请检查!');
  402. return;
  403. }
  404. state.orderPayload = JSON.parse(options.data);
  405. // 获取交易配置
  406. const { data, code } = await TradeConfigApi.getTradeConfig();
  407. if (code === 0) {
  408. addressState.value.isPickUp = data.deliveryPickUpEnabled;
  409. }
  410. // 价格计算
  411. // 情况一:先自动选择“快递物流”
  412. addressState.value.deliveryType = 1;
  413. let orderCode = await getOrderInfo();
  414. if (orderCode === 0) {
  415. return;
  416. }
  417. // 情况二:失败,再自动选择“门店自提”
  418. if (addressState.value.isPickUp) {
  419. addressState.value.deliveryType = 2;
  420. let orderCode = await getOrderInfo();
  421. if (orderCode === 0) {
  422. return;
  423. }
  424. }
  425. // 情况三:都失败,则不选择
  426. addressState.value.deliveryType = undefined;
  427. await getOrderInfo();
  428. });
  429. // 使用 watch 监听地址和配送方式的变化
  430. watch(addressState, async (newAddress, oldAddress) => {
  431. // 如果收货地址或配送方式有变化,则重新计算价格
  432. if (
  433. newAddress.addressInfo.id !== oldAddress.addressInfo.id ||
  434. newAddress.deliveryType !== oldAddress.deliveryType
  435. ) {
  436. await getOrderInfo();
  437. }
  438. });
  439. </script>
  440. <style lang="scss" scoped>
  441. :deep() {
  442. .uni-input-wrapper {
  443. width: 320rpx;
  444. }
  445. .uni-easyinput__content-input {
  446. font-size: 28rpx;
  447. height: 72rpx;
  448. text-align: right !important;
  449. padding-right: 0 !important;
  450. .uni-input-input {
  451. font-weight: 500;
  452. color: #333333;
  453. font-size: 26rpx;
  454. height: 32rpx;
  455. margin-top: 4rpx;
  456. }
  457. }
  458. .uni-easyinput__content {
  459. display: flex !important;
  460. align-items: center !important;
  461. justify-content: right !important;
  462. }
  463. }
  464. .score-img {
  465. width: 36rpx;
  466. height: 36rpx;
  467. margin: 0 4rpx;
  468. }
  469. .order-item {
  470. height: 80rpx;
  471. .item-title {
  472. font-size: 28rpx;
  473. font-weight: 400;
  474. }
  475. .item-value {
  476. font-size: 28rpx;
  477. font-weight: 500;
  478. font-family: OPPOSANS;
  479. }
  480. .text-disabled {
  481. color: #bbbbbb;
  482. }
  483. .item-icon {
  484. color: $dark-9;
  485. }
  486. .remark-input {
  487. text-align: right;
  488. }
  489. .item-placeholder {
  490. color: $dark-9;
  491. font-size: 26rpx;
  492. text-align: right;
  493. }
  494. }
  495. .total-box-footer {
  496. height: 90rpx;
  497. .total-num {
  498. color: #333333;
  499. font-family: OPPOSANS;
  500. }
  501. }
  502. .footer-box {
  503. height: 100rpx;
  504. .submit-btn {
  505. width: 240rpx;
  506. height: 70rpx;
  507. font-size: 28rpx;
  508. font-weight: 500;
  509. .goto-pay-text {
  510. line-height: 28rpx;
  511. }
  512. }
  513. .cancel-btn {
  514. width: 240rpx;
  515. height: 80rpx;
  516. font-size: 26rpx;
  517. background-color: #e5e5e5;
  518. color: $dark-9;
  519. }
  520. }
  521. .title {
  522. font-size: 36rpx;
  523. font-weight: bold;
  524. color: #333333;
  525. }
  526. .subtitle {
  527. font-size: 28rpx;
  528. color: #999999;
  529. }
  530. .cicon-checkbox {
  531. font-size: 36rpx;
  532. color: var(--ui-BG-Main);
  533. }
  534. .cicon-box {
  535. font-size: 36rpx;
  536. color: #999999;
  537. }
  538. .emailErrorMessage {
  539. background: #fff;
  540. text-align: right;
  541. color: #ff3000;
  542. font-size: 24rpx;
  543. }
  544. </style>