order-create.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. <template>
  2. <view class="create-order">
  3. <tui-skeleton v-if="isRequest" loadingType="2"></tui-skeleton>
  4. <!-- 收货地址 -->
  5. <view class="order-top"><cm-choose-address :addressData="addressData"></cm-choose-address></view>
  6. <view class="grid"></view>
  7. <!-- 订单列表 -->
  8. <view class="order-list">
  9. <view v-for="(shopInfo, shopInfoIndex) in goodsData" :key="shopInfoIndex">
  10. <view class="line"></view>
  11. <view class="order-section">
  12. <!-- 供应商 -->
  13. <view class="origin">
  14. <image class="cover" :src="shopInfo.logo"></image>
  15. <view class="name">{{ shopInfo.name }}</view>
  16. </view>
  17. <!-- 商品列表 -->
  18. <view class="goods-list">
  19. <!-- 商品信息 -->
  20. <view class="order-goods" v-for="goods in shopInfo.productList" :key="goods.productId">
  21. <cm-order-prodcut :goods-data="goods"></cm-order-prodcut>
  22. </view>
  23. </view>
  24. <!-- 合计 -->
  25. <!-- <view class="total-price">
  26. <text>合计:</text> <text class="price">¥{{ shopInfo.shopTotalPrice | formatPrice }}</text>
  27. </view> -->
  28. <!-- 留言 -->
  29. <view class="remark">
  30. <view class="label">留言:</view>
  31. <input class="control" type="text" v-model="shopInfo.note" placeholder="请输入内容" />
  32. </view>
  33. </view>
  34. </view>
  35. </view>
  36. <!-- 优惠券 -->
  37. <template v-if="receiveCouponList.length > 0">
  38. <view class="grid"></view>
  39. <cm-order-coupon-section @click="couponVisible = true" :coupon="currentCoupon"></cm-order-coupon-section>
  40. </template>
  41. <!-- 分享减免 -->
  42. <template v-if="reduction">
  43. <view class="grid"></view>
  44. <cm-order-share-reduce :reductionData="shareReductionData"></cm-order-share-reduce>
  45. </template>
  46. <!-- 运费 -->
  47. <view class="grid"></view>
  48. <cm-order-freight-section></cm-order-freight-section>
  49. <view class="grid"></view>
  50. <!-- 提交导航 -->
  51. <cm-order-submit-nav :orderInfo="submitOrderInfo" @commit="orderSubmit"></cm-order-submit-nav>
  52. <!-- 优惠券列表 -->
  53. <cm-coupon-list
  54. title="优惠券"
  55. listType="use"
  56. :visible="couponVisible"
  57. @close="closeCouponList"
  58. :chooseAble="true"
  59. :showStatus="false"
  60. :couponList="receiveCouponList"
  61. @chooseCoupon="chooseCoupon"
  62. @confirm="closeCouponList"
  63. :currentId="currentCouponUniqueId"
  64. ></cm-coupon-list>
  65. <cm-loading :visible="isSubLoading" :text="loadingText"></cm-loading>
  66. <view class="reserved" v-if="isIphoneX"></view>
  67. </view>
  68. </template>
  69. <script>
  70. import CmChooseAddress from '@/components/cm-module/cm-choose-address/cm-choose-address.vue'
  71. import CmOrderProdcut from '@/components/cm-module/cm-order-prodcut/cm-order-prodcut.vue'
  72. import CmOrderSubmitNav from '@/components/cm-module/cm-order-submit-nav/cm-order-submit-nav.vue'
  73. import CmOrderCouponSection from '@/components/cm-module/cm-order-coupon-section/cm-order-coupon-section.vue'
  74. import CmOrderFreightSection from '@/components/cm-module/cm-order-freight-section/cm-order-freight-section.vue'
  75. import CmOrderShareReduce from '@/components/cm-module/cm-order-share-reduce/cm-order-share-reduce.vue'
  76. import CmLoading from '@/components/cm-module/cm-loading/cm-loading.vue'
  77. import CmCouponList from '@/components/cm-module/cm-coupon-list/cm-coupon-list'
  78. import { allProdoceUseCheck, someProductUseCheck, couponSort, setCouponUniqueId } from '@/common/couponUtil.js'
  79. import { mapGetters } from 'vuex'
  80. import wechatPay from './mixins/wechatPay.js'
  81. export default {
  82. // 混入
  83. mixins: [wechatPay],
  84. components: {
  85. CmOrderProdcut,
  86. CmChooseAddress,
  87. CmOrderSubmitNav,
  88. CmOrderCouponSection,
  89. CmOrderFreightSection,
  90. CmOrderShareReduce,
  91. CmCouponList,
  92. CmLoading
  93. },
  94. data() {
  95. return {
  96. isRequest: true,
  97. // 收货地址
  98. addressData: {},
  99. productIds: '',
  100. // 初始化订单信息参数列表
  101. params: {},
  102. // 订单提交参数
  103. subParams: {
  104. userId: 0,
  105. orderInfo: [], //提交的商品信息
  106. addressId: 0,
  107. cartType: 0,
  108. payInfo: {
  109. orderShouldPayFee: 0
  110. },
  111. discountInfo: null
  112. },
  113. goodsData: [], // 供应商下的商品
  114. allPrice: 0,
  115. productList: [], // 商品列表
  116. allCount: 0,
  117. hanldOrder: {},
  118. // 优惠券
  119. couponVisible: false,
  120. receiveCouponList: [], // 已领取优惠券列表
  121. currentCouponUniqueId: -1, // 选中的优惠券唯一id 注意:非优惠券id,选中优惠券id通过currentCoupon.couponId获取
  122. currentCoupon: {}, // 选中的优惠券信息
  123. canUseCouponList: [], // 能够使用的优惠券列表
  124. notUseCouponList: [], // 暂未满足条件的优惠券列表
  125. // 拼团
  126. collageFlag: 0, //拼团标识:0不参与拼团,1参与拼团
  127. collageId: 0, //拼团id:参与拼团,若拼团id为空则发起拼团,不为空则与他人拼团
  128. // 分享抵扣
  129. shareReductStatus: false,
  130. reduction: null,
  131. reductionUserId: 0, //分享抵扣用户id
  132. shareData: {
  133. type: 0
  134. }
  135. }
  136. },
  137. computed: {
  138. ...mapGetters(['userId', 'isIphoneX']),
  139. // 选中的优惠券的金额
  140. couponAmount() {
  141. return this.currentCoupon.couponAmount || 0
  142. },
  143. // 最终支付金额
  144. payAllPrice() {
  145. const payAllPrice = this.allPrice - this.couponAmount - this.shareReducedAmount
  146. return payAllPrice <= 0 ? 0 : payAllPrice
  147. },
  148. // 总优惠价格
  149. discountedPrice() {
  150. return this.couponAmount + this.shareReducedAmount
  151. },
  152. // 提交组件状态信息
  153. submitOrderInfo() {
  154. return {
  155. allCount: this.allCount,
  156. payAllPrice: this.payAllPrice,
  157. discountedPrice: this.discountedPrice
  158. }
  159. },
  160. // 优惠券,分享活动相关参数
  161. discountInfo() {
  162. return {
  163. //优惠券ID
  164. couponId: this.currentCoupon.couponId || 0,
  165. //优惠券分享ID
  166. couponShareId: this.currentCoupon.couponShareId || 0,
  167. //拼团标识:0不参与拼团,1参与拼团
  168. collageFlag: this.collageFlag,
  169. //拼团id:参与拼团,若拼团id为空则发起拼团,不为空则与他人拼团
  170. collageId: this.collageId,
  171. //分享抵扣用户id
  172. reductionUserId: this.reductionUserId
  173. }
  174. },
  175. // 分享减免金额
  176. shareReducedAmount() {
  177. return this.reduction && this.shareReductStatus ? this.reduction.reducedAmount : 0
  178. },
  179. // 分享减免组件状态信息
  180. shareReductionData() {
  181. return {
  182. status: this.shareReductStatus,
  183. reduction: this.reduction
  184. }
  185. }
  186. },
  187. onLoad(options) {
  188. this.isRequest = true
  189. this.getAddressData()
  190. this.initOptions(options)
  191. // this.orderPaySuccess() // 支付回调
  192. },
  193. onShow() {
  194. let pages = getCurrentPages()
  195. let currPage = pages[pages.length - 1]
  196. if (currPage.data.select == 'select') {
  197. this.isAddress = true
  198. let SelectData = uni.getStorageSync('selectAddress')
  199. this.subParams.addressId = SelectData.addressId
  200. this.addressData = SelectData
  201. } else {
  202. this.getAddressData()
  203. }
  204. },
  205. beforeDestroy() {
  206. uni.removeStorageSync('commitProductInfo')
  207. uni.removeStorageSync('commitCartPramsData')
  208. },
  209. // 分享朋友圈
  210. // onShareTimeline() {
  211. // this.handleShare(2)
  212. // // 加密
  213. // const state_str = encodeURIComponent(this.$crypto.encrypt(this.shareData))
  214. // return {
  215. // title: '护肤上颜选,正品有好货~',
  216. // query: `/pages/tabBar/index/index?state_str=${state_str}`,
  217. // imageUrl: this.$Static + 'icon-index-share.jpg'
  218. // }
  219. // },
  220. // 分享
  221. onShareAppMessage(e) {
  222. this.handleShare(1)
  223. // 加密
  224. const state_str = encodeURIComponent(this.$crypto.encrypt(this.shareData))
  225. return {
  226. title: '护肤上颜选,正品有好货~',
  227. path: `/pages/tabBar/index/index?state_str=${state_str}`,
  228. imageUrl: this.$Static + 'icon-index-share.jpg'
  229. }
  230. },
  231. methods: {
  232. // 支付回调执行函数
  233. orderPaySuccess() {
  234. this.$on('orderPaySuccess', () => {
  235. if (this.collageFlag === 1) {
  236. uni.redirectTo({ url: `/pages/fight-order/fight-detail?collageId=${this.collageId}` })
  237. } else {
  238. uni.redirectTo({ url: '/pages/order/success' })
  239. }
  240. })
  241. },
  242. // 分享
  243. handleShare(shareType) {
  244. if (!this.reduction) return
  245. const { reductionId } = this.reduction
  246. this.OrderService.OrderReductionShare({ reductionId, shareType, userId: this.userId })
  247. .then(res => {
  248. this.$util.msg(res.msg, 2000)
  249. this.reductionUserId = res.data
  250. this.shareReductStatus = true
  251. })
  252. .catch(err => {
  253. this.$util.msg(error.msg, 2000)
  254. this.shareReductStatus = false
  255. })
  256. },
  257. // 初始化参数信息
  258. initOptions(options) {
  259. // 从商品详情进入
  260. if (options.type == 'prodcut') {
  261. const productInfo = uni.getStorageSync('commitProductInfo')
  262. this.subParams.cartType = 2 // 从商品详情提交
  263. this.params.productCount = productInfo.productCount
  264. this.params.productId = productInfo.productId
  265. this.params.heUserId = productInfo.heUserId || 0
  266. this.params.collageFlag = this.collageFlag = productInfo.collageFlag
  267. this.productIds = productInfo.productId.toString()
  268. this.allCount = productInfo.allCount
  269. this.collageId = parseInt(productInfo.collageId) || 0
  270. } else {
  271. const cartPramsData = uni.getStorageSync('commitCartPramsData')
  272. this.subParams.cartType = 1 // 从购物车提交
  273. this.params.cartIds = cartPramsData.cartIds
  274. this.productIds = cartPramsData.productIds
  275. this.allCount = cartPramsData.allCount
  276. }
  277. this.params.userId = this.userId
  278. this.subParams.userId = this.userId
  279. this.getInitCrearOrder(this.params)
  280. },
  281. // 获取可用优惠券
  282. fetchCouponList() {
  283. this.CouponService.GetCouponByProductIds({ userId: this.userId, productIds: this.productIds }).then(res => {
  284. this.receiveCouponList = setCouponUniqueId(res.data.receiveCouponList) // 去掉重复的优惠券
  285. this.filterCouponList()
  286. })
  287. },
  288. // 对优惠券进行分类排序
  289. filterCouponList() {
  290. this.goodsData.forEach(shop => this.productList.push(...shop.productList.map(prod => prod)))
  291. this.canUseCouponList = [] // 可以使用的优惠券
  292. this.notUseCouponList = [] // 需要凑单使用的优惠券
  293. this.receiveCouponList.forEach(coupon => {
  294. if (
  295. coupon.noThresholdFlag === 1 ||
  296. (coupon.productType === 1 && allProdoceUseCheck(this.productList, coupon)) ||
  297. (coupon.productType === 2 && someProductUseCheck(this.productList, coupon))
  298. ) {
  299. coupon.canSelect = true
  300. this.canUseCouponList.push(coupon)
  301. } else {
  302. coupon.canSelect = false
  303. this.notUseCouponList.push(coupon)
  304. }
  305. })
  306. // 金额高的排前面
  307. this.receiveCouponList = [...couponSort(this.canUseCouponList), ...couponSort(this.notUseCouponList)]
  308. // 当有可用优惠券时 默认选取第一个最优惠的
  309. if (this.canUseCouponList.length > 0) {
  310. this.currentCouponUniqueId = this.receiveCouponList[0].uniqueId
  311. this.currentCoupon = this.receiveCouponList[0] || {}
  312. }
  313. // 显示界面
  314. this.isRequest = false
  315. },
  316. // 处理优惠券列表
  317. resetCouponList() {
  318. // 1将当前选中的优惠券从列表中删除
  319. // 2将当前选中的优惠券放入最前面
  320. // 3返回最新的优惠券列表
  321. // 4查找选中优惠券的索引
  322. const index = this.canUseCouponList.findIndex(coupon => coupon.uniqueId === this.currentCouponUniqueId)
  323. // 从列表中删除
  324. const currentCoupon = this.canUseCouponList.splice(index, 1)
  325. // 重新排序 将选中的优惠券放到最前面
  326. this.canUseCouponList = [...currentCoupon, ...couponSort(this.canUseCouponList)]
  327. // 重新生成receiveCouponList
  328. this.receiveCouponList = [...this.canUseCouponList, ...this.notUseCouponList]
  329. },
  330. // 确认选中
  331. closeCouponList() {
  332. this.couponVisible = false
  333. this.resetCouponList()
  334. },
  335. // 选中优惠券
  336. chooseCoupon(coupon) {
  337. if (coupon.couponId > -1) {
  338. this.currentCoupon = coupon
  339. this.currentCouponUniqueId = coupon.uniqueId
  340. } else {
  341. this.currentCoupon = {}
  342. this.currentCouponUniqueId = -1
  343. }
  344. // this.couponVisible = false
  345. },
  346. //确认订单初始化信息
  347. getInitCrearOrder(params) {
  348. this.OrderService.QueryOrderConfirm(params)
  349. .then(res => {
  350. let data = res.data
  351. this.goodsData = this.bindRemark(data.shopList)
  352. console.log(this.goodsData)
  353. this.allPrice = data.totalPrice
  354. this.fetchCouponList()
  355. this.reduction = res.data.reduction
  356. this.isRequest = false
  357. })
  358. .catch(error => {
  359. this.$util.msg(error.msg, 2000)
  360. })
  361. },
  362. // 为共供应商绑定留言信息
  363. bindRemark(shopList) {
  364. return shopList.map(item => {
  365. item.note = ''
  366. return item
  367. })
  368. },
  369. //获取地址信息
  370. getAddressData() {
  371. this.UserService.QueryAddressList({ pageNum: 1, pageSize: 1, userId: this.userId }).then(res => {
  372. this.addressData = res.data.list[0]
  373. // 设置收货地址
  374. this.subParams.addressId = this.addressData.addressId
  375. })
  376. },
  377. orderSubmit() {
  378. //提交订单
  379. if (this.isSubLoading) return
  380. if (this.subParams.addressId == '') return this.$util.msg('请先添加收货地址~', 2000)
  381. // 优惠券,分享活动相关参数
  382. this.subParams.discountInfo = this.discountInfo
  383. // 要支付的金额
  384. this.subParams.payInfo.orderShouldPayFee = this.payAllPrice
  385. //
  386. // 处理商品信息及留言
  387. this.subParams.orderInfo = this.goodsData.map(el => {
  388. let productInfo = []
  389. el.productList.forEach(pros => {
  390. productInfo.push({
  391. productId: pros.productId,
  392. productNum: pros.productCount,
  393. heUserId: pros.heUserId
  394. })
  395. })
  396. return { shopId: el.shopId, note: el.note ? el.note : '', productInfo, splitCode: el.splitCode }
  397. })
  398. // 调试提交参数
  399. console.log(this.subParams)
  400. // return
  401. this.isSubLoading = true
  402. this.loadingText = '正在创建订单...'
  403. this.OrderService.CreatedOrderSubmit(this.subParams)
  404. .then(response => {
  405. this.isSubLoading = false
  406. // const data = response.data
  407. // this.hanldOrder.order = response.data
  408. // this.collageId = data.collageId
  409. // if (parseFloat(data.payableAmount) === 0) {
  410. // uni.setStorageSync('orderInfo', this.hanldOrder.order)
  411. // uni.redirectTo({ url: '/pages/order/success' })
  412. // } else {
  413. // this.miniWxPayFor(data)
  414. // }
  415. uni.redirectTo({ url: `/pages/order/order-pay?orderId=${response.data.orderId}` })
  416. })
  417. .catch(error => {
  418. this.$util.msg(error.msg, 2000)
  419. })
  420. }
  421. }
  422. }
  423. </script>
  424. <style lang="scss" scoped>
  425. .create-order {
  426. background: #f7f7f7;
  427. min-height: 100vh;
  428. padding-top: 134rpx;
  429. padding-bottom: 100rpx;
  430. box-sizing: border-box;
  431. .order-top {
  432. width: 100%;
  433. position: fixed;
  434. top: 0;
  435. left: 0;
  436. z-index: 999;
  437. }
  438. }
  439. .grid {
  440. width: 100%;
  441. height: 20rpx;
  442. background: #f7f7f7;
  443. }
  444. .line {
  445. width: 702rpx;
  446. height: 1rpx;
  447. margin: 0 auto;
  448. background: #f7f7f7;
  449. }
  450. .order-list {
  451. background: #fff;
  452. .order-section {
  453. padding: 38rpx 0;
  454. }
  455. .goods-list {
  456. .order-goods {
  457. padding-top: 32rpx;
  458. &:first-child {
  459. padding-top: 0;
  460. }
  461. }
  462. }
  463. .origin {
  464. padding: 0 24rpx;
  465. margin-bottom: 16rpx;
  466. display: flex;
  467. justify-content: flex-start;
  468. align-items: center;
  469. .cover {
  470. width: 56rpx;
  471. height: 56rpx;
  472. border-radius: 4rpx;
  473. }
  474. .name {
  475. margin-left: 16rpx;
  476. font-size: 30rpx;
  477. color: #333333;
  478. }
  479. }
  480. }
  481. .total-price {
  482. padding: 24rpx;
  483. text-align: right;
  484. font-size: 26rpx;
  485. color: #333;
  486. background: #fff;
  487. .price {
  488. color: #ff457b;
  489. }
  490. }
  491. .remark {
  492. padding: 24rpx 24rpx 0;
  493. display: flex;
  494. justify-content: space-between;
  495. align-items: center;
  496. background: #fff;
  497. .label {
  498. width: 78rpx;
  499. font-size: 26rpx;
  500. color: #999999;
  501. margin-right: 16rpx;
  502. }
  503. .control {
  504. flex: 1;
  505. font-size: 26rpx;
  506. color: #333333;
  507. padding: 8rpx 16rpx;
  508. background: #f7f7f7;
  509. border-radius: 8rpx;
  510. }
  511. }
  512. </style>