confirmCreateOrder.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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. recordId:null,
  161. selected:false,
  162. productValueSelect: ''
  163. }
  164. },
  165. onLoad(options) {
  166. console.log("确认订单", options)
  167. this.orderKey = options.orderKey;
  168. this.liveId = options.liveId
  169. this.goodsId = options.goodsId
  170. this.productId = options.productId
  171. this.totalNum = Number(options.totalNum) || 0
  172. this.type = options.type
  173. this.isFirstLoad = true;
  174. this.recordId=options.recordId
  175. this.productValueSelect = options.productValueSelect || ''
  176. if (options.type == 'win') {
  177. this.getKey()
  178. }
  179. },
  180. onShow() {
  181. this.openCoupon()
  182. this.getUserAddr()
  183. uni.$on('updateAddress', (e) => {
  184. this.address = e;
  185. this.addressId = e.addressId;
  186. this.cityId = e.cityId;
  187. this.selected=true
  188. });
  189. },
  190. mounted() {
  191. // uni.$on('updateAddress', (e) => {
  192. // this.address = e;
  193. // this.addressId = e.addressId;
  194. // });
  195. },
  196. onUnload() {
  197. uni.$off('updateAddress', this.handleAddressUpdate);
  198. },
  199. beforeDestroy() {
  200. uni.$off('updateAddress');
  201. },
  202. methods: {
  203. handleAddressUpdate(item) {
  204. console.log('接收到地址数据:', item);
  205. this.cityId = item.cityId;
  206. this.address = item;
  207. this.addressId = item.addressId;
  208. if (this.type == 'win') {
  209. this.computedRewardOrder()
  210. } else {
  211. this.computedOrder()
  212. }
  213. },
  214. // 获取用户收货地址
  215. getUserAddr() {
  216. userAddr().then(res => {
  217. if (res.code == 200) {
  218. console.log("用户收货地址>>>>", res.data)
  219. if (res.data && !this.address) {
  220. this.address = res.data;
  221. }
  222. if (res.data && res.data.cityId&&!this.selected) {
  223. this.cityId = res.data.cityId;
  224. }
  225. if (this.type == 'win') {
  226. this.computedRewardOrder()
  227. } else {
  228. this.computedOrder()
  229. }
  230. } else {
  231. uni.showToast({
  232. title: res.msg,
  233. icon: 'none'
  234. });
  235. }
  236. },
  237. rej => {}
  238. );
  239. },
  240. // 获得key
  241. getKey() {
  242. liveOrderKey().then(res => {
  243. if (res.code == 200) {
  244. this.orderKey = res.orderKey
  245. } else {
  246. uni.showToast({
  247. title: res.msg,
  248. icon: 'none'
  249. });
  250. }
  251. },
  252. rej => {}
  253. );
  254. },
  255. // 查询创建订单信息
  256. computedOrder() {
  257. let data = {
  258. cityId: this.cityId || '',
  259. totalNum: this.totalNum,
  260. productId: this.productId,
  261. orderKey: this.orderKey,
  262. couponUserId: this.couponUserId || '',
  263. attrValueId: this.productValueSelect
  264. }
  265. computed(data).then(res => {
  266. if (res.code == 200) {
  267. console.log("查询创建订单信息>>>>", res.data)
  268. this.orderData = res.data
  269. } else {
  270. uni.showToast({
  271. title: res.msg,
  272. icon: 'none'
  273. });
  274. }
  275. },
  276. rej => {}
  277. );
  278. },
  279. // 查询中奖创建订单信息
  280. computedRewardOrder() {
  281. let data = {
  282. cityId: this.cityId || '',
  283. productId: this.productId ,
  284. orderKey: this.orderKey,
  285. totalNum: 1,
  286. }
  287. computedReward(data).then(res => {
  288. if (res.code == 200) {
  289. console.log("查询创建订单信息>>>>", res.data)
  290. this.orderData = res.data||{}
  291. } else {
  292. uni.showToast({
  293. title: res.msg,
  294. icon: 'none'
  295. });
  296. }
  297. },
  298. rej => {}
  299. );
  300. },
  301. // 创建订单
  302. createLiveOrder() {
  303. let data = {
  304. cityId: this.cityId,
  305. liveId: this.liveId,
  306. orderKey: this.orderKey,
  307. userName: this.address.realName,
  308. userPhone: this.address.phone,
  309. userAddress: this.address.province + ' ' + this.address.city + ' ' + this.address.district + ' ' +
  310. this.address
  311. .detail,
  312. cartId: "5",
  313. productId: this.productId,
  314. totalNum: this.totalNum,
  315. couponUserId: this.couponUserId,
  316. attrValueId: this.productValueSelect,
  317. }
  318. return createliveOrder(data).then(res => {
  319. if (res.code == 200) {
  320. this.orderList = res.order;
  321. return res.order;
  322. } else {
  323. uni.showToast({
  324. title: res.msg,
  325. icon: 'none'
  326. });
  327. throw new Error(res.msg);
  328. }
  329. });
  330. },
  331. // 创建抽奖订单
  332. createRewardOrder() {
  333. let data = {
  334. cityId: this.cityId,
  335. liveId: this.liveId,
  336. orderKey: this.orderKey,
  337. userName: this.address.realName,
  338. userPhone: this.address.phone,
  339. userAddress: this.address.province + ' ' + this.address.city + ' ' + this.address.district + ' ' + this.address.detail,
  340. cartId: "5",
  341. productId: this.productId,
  342. totalNum: 1,
  343. couponUserId: this.couponUserId,
  344. recordId:this.recordId
  345. }
  346. return createReward(data).then(res => {
  347. if (res.code == 200) {
  348. this.orderList = res.order || {};
  349. return res.order || {};
  350. } else {
  351. uni.showToast({
  352. title: res.msg,
  353. icon: 'none'
  354. });
  355. throw new Error(res.msg);
  356. }
  357. });
  358. },
  359. couponSelect(item) {
  360. this.couponText = "-¥" + (item.couponPrice || 0).toFixed(2);
  361. this.couponUserId = item.id;
  362. this.couponVisible = false;
  363. if (this.type == 'win') {
  364. this.computedRewardOrder()
  365. } else {
  366. this.computedOrder()
  367. }
  368. },
  369. // 自动选择最大优惠券
  370. autoSelectMaxCoupon() {
  371. if (this.couponsList.length === 0 || !this.isFirstLoad) {
  372. return;
  373. }
  374. const availableCoupons = this.couponsList.filter(item =>
  375. item.status === 0 && item.useMinPrice <= (this.orderData.totalPrice || 0)
  376. );
  377. if (availableCoupons.length === 0) {
  378. return;
  379. }
  380. availableCoupons.sort((a, b) => b.couponPrice - a.couponPrice);
  381. const maxCoupon = availableCoupons[0];
  382. this.couponText = "-¥" + (maxCoupon.couponPrice || 0).toFixed(2);
  383. this.couponUserId = maxCoupon.id;
  384. console.log('自动选择最大优惠券:', maxCoupon);
  385. this.isFirstLoad = false;
  386. // 重新计算订单价格
  387. if (this.type == 'win') {
  388. this.computedRewardOrder()
  389. } else {
  390. this.computedOrder()
  391. }
  392. },
  393. openCoupon() {
  394. console.log("点了优惠券列表")
  395. if (!this.liveId || !this.goodsId) {
  396. console.log('缺少必要参数,跳过优惠券请求');
  397. return;
  398. }
  399. const data = {
  400. liveId: this.liveId,
  401. goodsId: this.goodsId
  402. };
  403. curCoupon(data).then(res => {
  404. this.couponsList = res.data || [];
  405. if (this.isFirstLoad) {
  406. setTimeout(() => {
  407. this.autoSelectMaxCoupon();
  408. }, 100);
  409. }
  410. })
  411. },
  412. openAddress() {
  413. uni.navigateTo({
  414. url: '/pages_user/user/address'
  415. })
  416. },
  417. // 提交订单
  418. async submitOrder() {
  419. try {
  420. // 检查本地缓存userData是否有电话号码
  421. let userData = uni.getStorageSync('userData');
  422. // 检查userData是否是字符串,如果是,解析为对象
  423. if (typeof userData === 'string') {
  424. try {
  425. userData = JSON.parse(userData);
  426. } catch (error) {
  427. console.error('解析userData失败:', error);
  428. userData = null;
  429. }
  430. }
  431. console.log("跳转登录",userData)
  432. console.log("跳转登录",userData?.phone)
  433. if (!userData || !userData.phone) {
  434. // 跳转到登录页面
  435. uni.showToast({
  436. title: '请先登录',
  437. icon: 'none'
  438. });
  439. uni.navigateTo({
  440. url: '/pages/auth/login'
  441. });
  442. return;
  443. }
  444. if (this.orderKey == null) {
  445. uni.showToast({
  446. icon: 'none',
  447. title: '订单KEY不存在',
  448. });
  449. return;
  450. }
  451. if (this.address == null) {
  452. uni.showToast({
  453. icon: 'none',
  454. title: '收货地址不能为空',
  455. });
  456. return;
  457. }
  458. let orderList;
  459. if (this.type == "win") {
  460. orderList = await this.createRewardOrder(); // 中奖订单
  461. } else {
  462. orderList = await this.createLiveOrder(); // 购买订单
  463. }
  464. console.log("orderList>>", orderList)
  465. const orderListStr = encodeURIComponent(JSON.stringify(orderList));
  466. uni.redirectTo({
  467. url: `/pages_shopping/live/paymentOrder?orderList=${orderListStr}&couponUserId=${this.couponUserId}&type=${this.type}`
  468. });
  469. } catch (error) {
  470. console.error('提交订单错误:', error);
  471. uni.showToast({
  472. title: error.message || '提交订单失败',
  473. icon: 'none'
  474. });
  475. }
  476. },
  477. }
  478. }
  479. </script>
  480. <style lang="scss">
  481. .inner-box {
  482. padding: 20rpx 20rpx 140rpx;
  483. .address-box {
  484. margin-bottom: 20rpx;
  485. box-sizing: border-box;
  486. min-height: 171rpx;
  487. background: #FFFFFF;
  488. border-radius: 16rpx;
  489. background-repeat: no-repeat;
  490. background-size: 100% 30rpx;
  491. background-position: left bottom;
  492. padding: 38rpx 30rpx 36rpx;
  493. display: flex;
  494. align-items: center;
  495. justify-content: space-between;
  496. .left {
  497. width: 92%;
  498. .name-box {
  499. display: flex;
  500. align-items: center;
  501. .text {
  502. font-size: 32rpx;
  503. font-family: PingFang SC;
  504. font-weight: bold;
  505. color: #111111;
  506. line-height: 1;
  507. &.name {
  508. margin-right: 30rpx;
  509. }
  510. }
  511. }
  512. .address {
  513. font-size: 28rpx;
  514. font-family: PingFang SC;
  515. font-weight: 500;
  516. color: #666666;
  517. line-height: 42rpx;
  518. text-align: left;
  519. margin-top: 23rpx;
  520. }
  521. }
  522. .arrow-box {
  523. width: 12rpx;
  524. height: 23rpx;
  525. display: flex;
  526. align-items: cenetr;
  527. justify-content: cenetr;
  528. image {
  529. width: 100%;
  530. height: 100%;
  531. }
  532. }
  533. }
  534. .price-info {
  535. background: #FFFFFF;
  536. border-radius: 16rpx;
  537. &-title {
  538. padding: 30rpx 30rpx 20rpx 30rpx;
  539. font-family: PingFang SC, PingFang SC;
  540. font-weight: 500;
  541. font-size: 30rpx;
  542. color: #111;
  543. }
  544. &-unit {
  545. font-size: 24rpx;
  546. }
  547. &-num {
  548. font-size: 28rpx;
  549. }
  550. }
  551. .points {
  552. height: 88rpx;
  553. padding: 0 30rpx;
  554. background: #FFFFFF;
  555. border-radius: 16rpx;
  556. display: flex;
  557. align-items: center;
  558. justify-content: space-between;
  559. .left {
  560. display: flex;
  561. align-items: center;
  562. image {
  563. width: 28rpx;
  564. height: 28rpx;
  565. margin-right: 20rpx;
  566. }
  567. .text {
  568. font-size: 28rpx;
  569. font-family: PingFang SC;
  570. font-weight: 500;
  571. color: #666666;
  572. }
  573. }
  574. .right {
  575. display: flex;
  576. align-items: center;
  577. .text {
  578. font-size: 28rpx;
  579. font-family: PingFang SC;
  580. font-weight: 500;
  581. color: #111111;
  582. }
  583. image {
  584. margin-left: 15rpx;
  585. width: 14rpx;
  586. height: 24rpx;
  587. }
  588. }
  589. }
  590. }
  591. .btn-foot {
  592. box-sizing: border-box;
  593. width: 100%;
  594. height: 121rpx;
  595. background: #FFFFFF;
  596. padding: 16rpx 30rpx 16rpx 60rpx;
  597. display: flex;
  598. align-items: center;
  599. justify-content: flex-end;
  600. position: fixed;
  601. left: 0;
  602. bottom: 0;
  603. z-index: 99;
  604. .right {
  605. display: flex;
  606. align-items: center;
  607. .total {
  608. display: flex;
  609. align-items: flex-end;
  610. margin-right: 36rpx;
  611. .label {
  612. font-size: 26rpx;
  613. font-family: PingFang SC;
  614. font-weight: 500;
  615. color: #999999;
  616. line-height: 1.5;
  617. }
  618. .price {
  619. display: flex;
  620. align-items: flex-end;
  621. .unit {
  622. font-size: 32rpx;
  623. font-family: PingFang SC;
  624. font-weight: bold;
  625. color: #FF6633;
  626. line-height: 1.2;
  627. margin-right: 10rpx;
  628. }
  629. .num {
  630. font-size: 50rpx;
  631. font-family: PingFang SC;
  632. font-weight: bold;
  633. color: #FF6633;
  634. line-height: 1;
  635. }
  636. }
  637. }
  638. .btn {
  639. width: 200rpx;
  640. height: 88rpx;
  641. line-height: 88rpx;
  642. text-align: center;
  643. font-size: 30rpx;
  644. font-family: PingFang SC;
  645. font-weight: bold;
  646. color: #FFFFFF;
  647. background: #2BC7B9;
  648. border-radius: 44rpx;
  649. }
  650. }
  651. }
  652. .coupon {
  653. height: 100%;
  654. }
  655. /*优惠券列表公共*/
  656. .coupon-list {}
  657. .coupon-list .item {
  658. display: flex;
  659. flex-direction: column;
  660. justify-content: center;
  661. align-items: center;
  662. width: 100%;
  663. height: 1.7 * 100rpx;
  664. margin-bottom: 0.16 * 100rpx;
  665. }
  666. .coupon-list .item .money {
  667. background-size: 100% 100%;
  668. width: 2.4 * 100rpx;
  669. height: 100%;
  670. color: #fff;
  671. font-size: 0.36 * 100rpx;
  672. font-weight: bold;
  673. text-align: center;
  674. display: flex;
  675. flex-direction: column;
  676. align-items: center;
  677. justify-content: center;
  678. position: relative;
  679. }
  680. .coupon-list .item .money .img {
  681. position: absolute;
  682. width: 2.4 * 100rpx;
  683. height: 100%;
  684. color: #fff;
  685. }
  686. .coupon-list .item .money .num {
  687. font-size: 0.6 * 100rpx;
  688. }
  689. .coupon-list .item .money .pic-num {
  690. font-size: 20rpx;
  691. z-index: 99;
  692. }
  693. .coupon-list .item .text {
  694. width: 4.5 * 100rpx;
  695. padding: 0 0.17 * 100rpx 0 0.24 * 100rpx;
  696. background-color: #fff;
  697. box-sizing: border-box;
  698. }
  699. .coupon-list .item .text .condition {
  700. font-size: 0.3 * 100rpx;
  701. color: #282828;
  702. height: 0.93 * 100rpx;
  703. line-height: 0.93 * 100rpx;
  704. border-bottom: 1px solid #f0f0f0;
  705. }
  706. .coupon-list .item .text .data {
  707. font-size: 0.2 * 100rpx;
  708. color: #999;
  709. height: 0.76 * 100rpx;
  710. }
  711. .coupon-list .item .text .data .bnt {
  712. width: 1.36 * 100rpx;
  713. height: 0.44 * 100rpx;
  714. border-radius: 0.22 * 100rpx;
  715. font-size: 0.22 * 100rpx;
  716. color: #fff;
  717. text-align: center;
  718. line-height: 0.44 * 100rpx;
  719. background-color: red;
  720. }
  721. </style>