create-order.vue 18 KB

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