confirmCreateOrder.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. <template>
  2. <view>
  3. <view class="inner-box">
  4. <!-- 收货人 -->
  5. <view class="address-box" v-if="address==null" @click="openAddress()">
  6. <view class="left">
  7. <view class="name-box">
  8. <text class="text name">添加收货地址</text>
  9. </view>
  10. </view>
  11. <view class="arrow-box">
  12. <image src="/static/images/arrow_gray.png" mode=""></image>
  13. </view>
  14. </view>
  15. <view class="address-box" v-if="address!=null" @click="openAddress()">
  16. <view class="left">
  17. <view class="name-box">
  18. <text class="text name">{{address.realName}}</text>
  19. <text class="text" v-if="address.phone!=null">{{address.phone}}</text>
  20. </view>
  21. <view class="address">
  22. {{address.province}}{{address.city}}{{address.district}}{{address.detail}}
  23. </view>
  24. </view>
  25. <view class="arrow-box">
  26. <image src="/static/images/arrow_gray.png" mode=""></image>
  27. </view>
  28. </view>
  29. <!-- 积分 -->
  30. <view class="price-info">
  31. <view class="price-info-title">价格明细</view>
  32. <view class="points">
  33. <view class="left">
  34. <text class="text">商品总价</text>
  35. </view>
  36. <view class="right" style="align-items: baseline;">
  37. <text class="price-info-unit">¥</text>
  38. <text class="price-info-num">{{(orderData.totalPrice||0).toFixed(2)}}</text>
  39. </view>
  40. </view>
  41. <view class="points" @click="couponVisible = true;">
  42. <view class="left">
  43. <text class="text">优惠券</text>
  44. </view>
  45. <view class="right">
  46. <text class="text">{{couponText}}</text>
  47. <image src="/static/images/arrow4.png" mode=""></image>
  48. </view>
  49. </view>
  50. <view class="points">
  51. <view class="left">
  52. <text class="text">运费</text>
  53. </view>
  54. <view class="right">
  55. <text class="text"
  56. v-if="address!=null">{{orderData.payDelivery==null||orderData.payDelivery==0?'免运费':orderData.payDelivery.toFixed(2)}}</text>
  57. <text class="text" v-if="address==null">--</text>
  58. </view>
  59. </view>
  60. <view class="points">
  61. <view class="left">
  62. <text class="text">合计</text>
  63. </view>
  64. <view class="right" style="align-items: baseline;">
  65. <text class="price-info-unit">¥</text>
  66. <text class="price-info-num">{{(orderData.payPrice||0).toFixed(2)}}</text>
  67. </view>
  68. </view>
  69. </view>
  70. </view>
  71. <view class="btn-foot">
  72. <view class="right">
  73. <view class="total">
  74. <text class="label">合计:</text>
  75. <view class="price">
  76. <text class="unit">¥</text>
  77. <text class="num">{{(orderData.payPrice||0).toFixed(2)}}</text>
  78. </view>
  79. </view>
  80. <view class="btn" @click="submitOrder">提交订单</view>
  81. </view>
  82. </view>
  83. <popupBottom ref="popup" :visible.sync="couponVisible" title=" " bgColor="#f5f5f5" radius="30" maxHeight="60%">
  84. <view class="coupon" style="height:650rpx;">
  85. <div class="coupon-list" v-if="couponsList.length > 0">
  86. <div class="item acea-row row-center-wrapper" v-for="(item, index) in couponsList" :key="index">
  87. <div class="money">
  88. <image v-if="item.status==0&&item.useMinPrice<=orderData.totalPrice" class="img"
  89. src="../../static/images/coupon1.png" mode="widthFix"></image>
  90. <image v-if="item.status!=0||item.useMinPrice>orderData.totalPrice" class="img"
  91. src="../../static/images/coupon2.png" mode="widthFix"></image>
  92. <div style="z-index: 999;">
  93. ¥<span class="num">{{ item.couponPrice }}</span>
  94. </div>
  95. <div class="pic-num">满{{ item.useMinPrice }}元可用</div>
  96. </div>
  97. <div class="text">
  98. <div class="condition line1">
  99. {{ item.couponTitle }}
  100. </div>
  101. <div class="data acea-row row-between-wrapper">
  102. <div>{{ item.limitTime }}到期</div>
  103. <div v-if=" item.status==0&&item.useMinPrice<=orderData.totalPrice"
  104. class="bnt bg-color-red" @click="couponSelect(item)">选择</div>
  105. <div v-else-if="item.status!=0" class="bnt " style="background-color: #878787;">已失效
  106. </div>
  107. <div v-else-if="item.useMinPrice>orderData.totalPrice" class="bnt "
  108. style="background-color: #878787;">未达门槛</div>
  109. </div>
  110. </div>
  111. </div>
  112. </div>
  113. <view v-if="couponsList.length == 0" class="no-data-box">
  114. <image src="/static/images/no_data.png" mode="aspectFit"></image>
  115. <view class="empty-title">暂无数据</view>
  116. </view>
  117. </view>
  118. </popupBottom>
  119. </view>
  120. </template>
  121. <script>
  122. import {
  123. createliveOrder, // 创建订单
  124. createReward, // 创建中奖订单
  125. userAddr, // 获取用户收货地址
  126. computed,
  127. computedReward,
  128. liveOrderKey, // 生成订单key
  129. } from "@/api/order.js"
  130. import {
  131. curCoupon
  132. } from "@/api/living.js"
  133. import popupBottom from '@/components/px-popup-bottom/px-popup-bottom.vue'
  134. export default {
  135. components: {
  136. popupBottom
  137. },
  138. data() {
  139. return {
  140. orderData: {},
  141. cityId: null,
  142. address: null,
  143. addressId: null,
  144. totalNum: null,
  145. orderKey: null,
  146. goodsId: null,
  147. liveId: null,
  148. orderList: [],
  149. temps: [],
  150. couponUserId: null,
  151. couponText: "请选择",
  152. couponsList: [],
  153. couponVisible: false,
  154. carts: [],
  155. checked: false,
  156. cartIds: null,
  157. confirmParam: [],
  158. type: '',
  159. isFirstLoad: true,
  160. }
  161. },
  162. onLoad(options) {
  163. console.log("确认订单", options)
  164. this.orderKey = options.orderKey;
  165. this.liveId = options.liveId
  166. this.goodsId = options.goodsId
  167. this.productId = options.productId
  168. this.totalNum = Number(options.totalNum) || 0
  169. this.type = options.type
  170. this.isFirstLoad = true;
  171. if (options.type == 'win') {
  172. this.getKey()
  173. }
  174. },
  175. onShow() {
  176. this.openCoupon()
  177. this.getUserAddr()
  178. uni.$on('updateAddress', (e) => {
  179. this.address = e;
  180. this.addressId = e.addressId;
  181. this.cityId = e.cityId;
  182. });
  183. },
  184. mounted() {
  185. // uni.$on('updateAddress', (e) => {
  186. // this.address = e;
  187. // this.addressId = e.addressId;
  188. // });
  189. },
  190. onUnload() {
  191. uni.$off('updateAddress', this.handleAddressUpdate);
  192. },
  193. beforeDestroy() {
  194. uni.$off('updateAddress');
  195. },
  196. methods: {
  197. handleAddressUpdate(item) {
  198. console.log('接收到地址数据:', item);
  199. this.cityId = item.cityId;
  200. this.address = item;
  201. this.addressId = item.addressId;
  202. if (this.type == 'win') {
  203. this.computedRewardOrder()
  204. } else {
  205. this.computedOrder()
  206. }
  207. },
  208. // 获取用户收货地址
  209. getUserAddr() {
  210. userAddr().then(res => {
  211. if (res.code == 200) {
  212. console.log("用户收货地址>>>>", res.data)
  213. if (res.data && !this.address) {
  214. this.address = res.data;
  215. }
  216. if (res.data && res.data.cityId) {
  217. this.cityId = res.data.cityId;
  218. }
  219. if (this.type == 'win') {
  220. this.computedRewardOrder()
  221. } else {
  222. this.computedOrder()
  223. }
  224. } else {
  225. uni.showToast({
  226. title: res.msg,
  227. icon: 'none'
  228. });
  229. }
  230. },
  231. rej => {}
  232. );
  233. },
  234. // 获得key
  235. getKey() {
  236. liveOrderKey().then(res => {
  237. if (res.code == 200) {
  238. this.orderKey = res.orderKey
  239. } else {
  240. uni.showToast({
  241. title: res.msg,
  242. icon: 'none'
  243. });
  244. }
  245. },
  246. rej => {}
  247. );
  248. },
  249. // 查询创建订单信息
  250. computedOrder() {
  251. let data = {
  252. cityId: this.cityId || '',
  253. totalNum: this.totalNum,
  254. productId: this.productId,
  255. orderKey: this.orderKey,
  256. couponUserId: this.couponUserId || ''
  257. }
  258. computed(data).then(res => {
  259. if (res.code == 200) {
  260. console.log("查询创建订单信息>>>>", res.data)
  261. this.orderData = res.data
  262. } else {
  263. uni.showToast({
  264. title: res.msg,
  265. icon: 'none'
  266. });
  267. }
  268. },
  269. rej => {}
  270. );
  271. },
  272. // 查询中奖创建订单信息
  273. computedRewardOrder() {
  274. let data = {
  275. cityId: this.cityId || '',
  276. productId: this.productId,
  277. orderKey: this.orderKey,
  278. totalNum: 1,
  279. }
  280. computedReward(data).then(res => {
  281. if (res.code == 200) {
  282. console.log("查询创建订单信息>>>>", res.data)
  283. this.orderData = res.data || {}
  284. } else {
  285. uni.showToast({
  286. title: res.msg,
  287. icon: 'none'
  288. });
  289. }
  290. },
  291. rej => {}
  292. );
  293. },
  294. // 创建订单
  295. createLiveOrder() {
  296. let data = {
  297. cityId: this.cityId,
  298. liveId: this.liveId,
  299. orderKey: this.orderKey,
  300. userName: this.address.realName,
  301. userPhone: this.address.phone,
  302. userAddress: this.address.province + ' ' + this.address.city + ' ' + this.address.district + ' ' +
  303. this.address
  304. .detail,
  305. cartId: "5",
  306. productId: this.productId,
  307. totalNum: this.totalNum,
  308. couponUserId: this.couponUserId
  309. }
  310. return createliveOrder(data).then(res => {
  311. if (res.code == 200) {
  312. this.orderList = res.order;
  313. return res.order;
  314. } else {
  315. uni.showToast({
  316. title: res.msg,
  317. icon: 'none'
  318. });
  319. throw new Error(res.msg);
  320. }
  321. });
  322. },
  323. // 创建抽奖订单
  324. createRewardOrder() {
  325. let data = {
  326. cityId: this.cityId,
  327. liveId: this.liveId,
  328. orderKey: this.orderKey,
  329. userName: this.address.realName,
  330. userPhone: this.address.phone,
  331. userAddress: this.address.province + ' ' + this.address.city + ' ' + this.address.district + ' ' +
  332. this.address.detail,
  333. cartId: "5",
  334. productId: this.productId,
  335. totalNum: 1,
  336. couponUserId: this.couponUserId
  337. }
  338. return createReward(data).then(res => {
  339. if (res.code == 200) {
  340. this.orderList = res.order || {};
  341. return res.order || {};
  342. } else {
  343. uni.showToast({
  344. title: res.msg,
  345. icon: 'none'
  346. });
  347. throw new Error(res.msg);
  348. }
  349. });
  350. },
  351. couponSelect(item) {
  352. this.couponText = "-¥" + (item.couponPrice || 0).toFixed(2);
  353. this.couponUserId = item.id;
  354. this.couponVisible = false;
  355. if (this.type == 'win') {
  356. this.computedRewardOrder()
  357. } else {
  358. this.computedOrder()
  359. }
  360. },
  361. // 自动选择最大优惠券
  362. autoSelectMaxCoupon() {
  363. if (this.couponsList.length === 0 || !this.isFirstLoad) {
  364. return;
  365. }
  366. const availableCoupons = this.couponsList.filter(item =>
  367. item.status === 0 && item.useMinPrice <= (this.orderData.totalPrice || 0)
  368. );
  369. if (availableCoupons.length === 0) {
  370. return;
  371. }
  372. availableCoupons.sort((a, b) => b.couponPrice - a.couponPrice);
  373. const maxCoupon = availableCoupons[0];
  374. this.couponText = "-¥" + (maxCoupon.couponPrice || 0).toFixed(2);
  375. this.couponUserId = maxCoupon.id;
  376. console.log('自动选择最大优惠券:', maxCoupon);
  377. this.isFirstLoad = false;
  378. // 重新计算订单价格
  379. if (this.type == 'win') {
  380. this.computedRewardOrder()
  381. } else {
  382. this.computedOrder()
  383. }
  384. },
  385. openCoupon() {
  386. console.log("点了优惠券列表")
  387. if (!this.liveId || !this.goodsId) {
  388. console.log('缺少必要参数,跳过优惠券请求');
  389. return;
  390. }
  391. const data = {
  392. liveId: this.liveId,
  393. goodsId: this.goodsId
  394. };
  395. curCoupon(data).then(res => {
  396. this.couponsList = res.data || [];
  397. if (this.isFirstLoad) {
  398. setTimeout(() => {
  399. this.autoSelectMaxCoupon();
  400. }, 100);
  401. }
  402. })
  403. },
  404. openAddress() {
  405. uni.navigateTo({
  406. url: '/pages_user/user/address'
  407. })
  408. },
  409. // 提交订单
  410. async submitOrder() {
  411. try {
  412. if (this.orderKey == null) {
  413. uni.showToast({
  414. icon: 'none',
  415. title: '订单KEY不存在',
  416. });
  417. return;
  418. }
  419. if (this.address == null) {
  420. uni.showToast({
  421. icon: 'none',
  422. title: '收货地址不能为空',
  423. });
  424. return;
  425. }
  426. let orderList;
  427. if (this.type == "win") {
  428. orderList = await this.createRewardOrder(); // 中奖订单
  429. } else {
  430. orderList = await this.createLiveOrder(); // 购买订单
  431. }
  432. console.log("orderList>>", orderList)
  433. const orderListStr = encodeURIComponent(JSON.stringify(orderList));
  434. uni.navigateTo({
  435. url: `/pages_shopping/live/paymentOrder?orderList=${orderListStr}&couponUserId=${this.couponUserId}&type=${this.type}`
  436. });
  437. } catch (error) {
  438. console.error('提交订单错误:', error);
  439. uni.showToast({
  440. title: error.message || '提交订单失败',
  441. icon: 'none'
  442. });
  443. }
  444. },
  445. }
  446. }
  447. </script>
  448. <style lang="scss">
  449. .inner-box {
  450. padding: 20rpx 20rpx 140rpx;
  451. .address-box {
  452. margin-bottom: 20rpx;
  453. box-sizing: border-box;
  454. min-height: 171rpx;
  455. background: #FFFFFF;
  456. border-radius: 16rpx;
  457. background-repeat: no-repeat;
  458. background-size: 100% 30rpx;
  459. background-position: left bottom;
  460. padding: 38rpx 30rpx 36rpx;
  461. display: flex;
  462. align-items: center;
  463. justify-content: space-between;
  464. .left {
  465. width: 92%;
  466. .name-box {
  467. display: flex;
  468. align-items: center;
  469. .text {
  470. font-size: 32rpx;
  471. font-family: PingFang SC;
  472. font-weight: bold;
  473. color: #111111;
  474. line-height: 1;
  475. &.name {
  476. margin-right: 30rpx;
  477. }
  478. }
  479. }
  480. .address {
  481. font-size: 28rpx;
  482. font-family: PingFang SC;
  483. font-weight: 500;
  484. color: #666666;
  485. line-height: 42rpx;
  486. text-align: left;
  487. margin-top: 23rpx;
  488. }
  489. }
  490. .arrow-box {
  491. width: 12rpx;
  492. height: 23rpx;
  493. display: flex;
  494. align-items: cenetr;
  495. justify-content: cenetr;
  496. image {
  497. width: 100%;
  498. height: 100%;
  499. }
  500. }
  501. }
  502. .price-info {
  503. background: #FFFFFF;
  504. border-radius: 16rpx;
  505. &-title {
  506. padding: 30rpx 30rpx 20rpx 30rpx;
  507. font-family: PingFang SC, PingFang SC;
  508. font-weight: 500;
  509. font-size: 30rpx;
  510. color: #111;
  511. }
  512. &-unit {
  513. font-size: 24rpx;
  514. }
  515. &-num {
  516. font-size: 28rpx;
  517. }
  518. }
  519. .points {
  520. height: 88rpx;
  521. padding: 0 30rpx;
  522. background: #FFFFFF;
  523. border-radius: 16rpx;
  524. display: flex;
  525. align-items: center;
  526. justify-content: space-between;
  527. .left {
  528. display: flex;
  529. align-items: center;
  530. image {
  531. width: 28rpx;
  532. height: 28rpx;
  533. margin-right: 20rpx;
  534. }
  535. .text {
  536. font-size: 28rpx;
  537. font-family: PingFang SC;
  538. font-weight: 500;
  539. color: #666666;
  540. }
  541. }
  542. .right {
  543. display: flex;
  544. align-items: center;
  545. .text {
  546. font-size: 28rpx;
  547. font-family: PingFang SC;
  548. font-weight: 500;
  549. color: #111111;
  550. }
  551. image {
  552. margin-left: 15rpx;
  553. width: 14rpx;
  554. height: 24rpx;
  555. }
  556. }
  557. }
  558. }
  559. .btn-foot {
  560. box-sizing: border-box;
  561. width: 100%;
  562. height: 121rpx;
  563. background: #FFFFFF;
  564. padding: 16rpx 30rpx 16rpx 60rpx;
  565. display: flex;
  566. align-items: center;
  567. justify-content: flex-end;
  568. position: fixed;
  569. left: 0;
  570. bottom: 0;
  571. z-index: 99;
  572. .right {
  573. display: flex;
  574. align-items: center;
  575. .total {
  576. display: flex;
  577. align-items: flex-end;
  578. margin-right: 36rpx;
  579. .label {
  580. font-size: 26rpx;
  581. font-family: PingFang SC;
  582. font-weight: 500;
  583. color: #999999;
  584. line-height: 1.5;
  585. }
  586. .price {
  587. display: flex;
  588. align-items: flex-end;
  589. .unit {
  590. font-size: 32rpx;
  591. font-family: PingFang SC;
  592. font-weight: bold;
  593. color: #FF6633;
  594. line-height: 1.2;
  595. margin-right: 10rpx;
  596. }
  597. .num {
  598. font-size: 50rpx;
  599. font-family: PingFang SC;
  600. font-weight: bold;
  601. color: #FF6633;
  602. line-height: 1;
  603. }
  604. }
  605. }
  606. .btn {
  607. width: 200rpx;
  608. height: 88rpx;
  609. line-height: 88rpx;
  610. text-align: center;
  611. font-size: 30rpx;
  612. font-family: PingFang SC;
  613. font-weight: bold;
  614. color: #FFFFFF;
  615. background: #2BC7B9;
  616. border-radius: 44rpx;
  617. }
  618. }
  619. }
  620. .coupon {
  621. height: 100%;
  622. }
  623. /*优惠券列表公共*/
  624. .coupon-list {}
  625. .coupon-list .item {
  626. display: flex;
  627. flex-direction: column;
  628. justify-content: center;
  629. align-items: center;
  630. width: 100%;
  631. height: 1.7 * 100rpx;
  632. margin-bottom: 0.16 * 100rpx;
  633. }
  634. .coupon-list .item .money {
  635. background-size: 100% 100%;
  636. width: 2.4 * 100rpx;
  637. height: 100%;
  638. color: #fff;
  639. font-size: 0.36 * 100rpx;
  640. font-weight: bold;
  641. text-align: center;
  642. display: flex;
  643. flex-direction: column;
  644. align-items: center;
  645. justify-content: center;
  646. position: relative;
  647. }
  648. .coupon-list .item .money .img {
  649. position: absolute;
  650. width: 2.4 * 100rpx;
  651. height: 100%;
  652. color: #fff;
  653. }
  654. .coupon-list .item .money .num {
  655. font-size: 0.6 * 100rpx;
  656. }
  657. .coupon-list .item .money .pic-num {
  658. font-size: 20rpx;
  659. z-index: 99;
  660. }
  661. .coupon-list .item .text {
  662. width: 4.5 * 100rpx;
  663. padding: 0 0.17 * 100rpx 0 0.24 * 100rpx;
  664. background-color: #fff;
  665. box-sizing: border-box;
  666. }
  667. .coupon-list .item .text .condition {
  668. font-size: 0.3 * 100rpx;
  669. color: #282828;
  670. height: 0.93 * 100rpx;
  671. line-height: 0.93 * 100rpx;
  672. border-bottom: 1px solid #f0f0f0;
  673. }
  674. .coupon-list .item .text .data {
  675. font-size: 0.2 * 100rpx;
  676. color: #999;
  677. height: 0.76 * 100rpx;
  678. }
  679. .coupon-list .item .text .data .bnt {
  680. width: 1.36 * 100rpx;
  681. height: 0.44 * 100rpx;
  682. border-radius: 0.22 * 100rpx;
  683. font-size: 0.22 * 100rpx;
  684. color: #fff;
  685. text-align: center;
  686. line-height: 0.44 * 100rpx;
  687. background-color: red;
  688. }
  689. </style>