create-order.vue 19 KB

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