create-order.vue 19 KB

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