create-order.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. } else {
  257. const cartPramsData = uni.getStorageSync('commitCartPramsData')
  258. this.subParams.cartType = 1
  259. this.params.cartIds = cartPramsData.cartIds
  260. this.productIds = cartPramsData.productIds
  261. this.allCount = cartPramsData.allCount
  262. }
  263. this.params.userId = this.userId
  264. this.subParams.userId = this.userId
  265. this.getInitCrearOrder(this.params)
  266. },
  267. // 获取可用优惠券
  268. fetchCouponList() {
  269. this.CouponService.GetCouponByProductIds({ userId: this.userId, productIds: this.productIds }).then(res => {
  270. this.receiveCouponList = setCouponUniqueId(res.data.receiveCouponList) // 去掉重复的优惠券
  271. this.filterCouponList()
  272. })
  273. },
  274. // 对优惠券进行分类排序
  275. filterCouponList() {
  276. this.goodsData.forEach(shop => this.productList.push(...shop.productList.map(prod => prod)))
  277. this.canUseCouponList = [] // 可以使用的优惠券
  278. this.notUseCouponList = [] // 需要凑单使用的优惠券
  279. this.receiveCouponList.forEach(coupon => {
  280. if (
  281. coupon.noThresholdFlag === 1 ||
  282. (coupon.productType === 1 && allProdoceUseCheck(this.productList, coupon)) ||
  283. (coupon.productType === 2 && someProductUseCheck(this.productList, coupon))
  284. ) {
  285. coupon.canSelect = true
  286. this.canUseCouponList.push(coupon)
  287. } else {
  288. coupon.canSelect = false
  289. this.notUseCouponList.push(coupon)
  290. }
  291. })
  292. // 金额高的排前面
  293. this.receiveCouponList = [...couponSort(this.canUseCouponList), ...couponSort(this.notUseCouponList)]
  294. // 当有可用优惠券时 默认选取第一个最优惠的
  295. if (this.canUseCouponList.length > 0) {
  296. this.currentCouponId = this.receiveCouponList[0].uniqueId
  297. this.currentCoupon = this.receiveCouponList[0]
  298. }
  299. // 显示界面
  300. this.isRequest = false
  301. },
  302. // 处理优惠券列表
  303. resetCouponList() {
  304. // 1将当前选中的优惠券从列表中删除
  305. // 2将当前选中的优惠券放入最前面
  306. // 3返回最新的优惠券列表
  307. // 4查找选中优惠券的索引
  308. const index = this.canUseCouponList.findIndex(coupon => coupon.uniqueId === this.currentCouponId)
  309. // 从列表中删除
  310. const currentCoupon = this.canUseCouponList.splice(index, 1)
  311. // 重新排序 将选中的优惠券放到最前面
  312. this.canUseCouponList = [...currentCoupon, ...couponSort(this.canUseCouponList)]
  313. // 重新生成receiveCouponList
  314. this.receiveCouponList = [...this.canUseCouponList, ...this.notUseCouponList]
  315. },
  316. // 确认选中
  317. closeCouponList() {
  318. this.couponVisible = false
  319. this.resetCouponList()
  320. },
  321. // 选中优惠券
  322. chooseCoupon(coupon) {
  323. if (coupon.couponId > -1) {
  324. this.currentCoupon = coupon
  325. this.currentCouponId = coupon.uniqueId
  326. } else {
  327. this.currentCoupon = null
  328. this.currentCouponId = -1
  329. }
  330. // this.couponVisible = false
  331. },
  332. //确认订单初始化信息
  333. getInitCrearOrder(params) {
  334. this.OrderService.QueryOrderConfirm(params)
  335. .then(res => {
  336. let data = res.data
  337. this.goodsData = this.bindRemark(data.shopList)
  338. console.log(this.goodsData)
  339. this.allPrice = data.totalPrice
  340. this.fetchCouponList()
  341. this.reduction = res.data.reduction
  342. })
  343. .catch(error => {
  344. this.$util.msg(error.msg, 2000)
  345. })
  346. },
  347. // 为共供应商绑定留言信息
  348. bindRemark(shopList) {
  349. return shopList.map(item => {
  350. item.note = ''
  351. return item
  352. })
  353. },
  354. //获取地址信息
  355. getAddressData() {
  356. this.UserService.QueryAddressList({ pageNum: 1, pageSize: 1, userId: this.userId }).then(res => {
  357. this.addressData = res.data.list[0]
  358. // 设置收货地址
  359. this.subParams.addressId = this.addressData.addressId
  360. })
  361. },
  362. orderSubmit() {
  363. //提交订单
  364. if (this.isSubLoading) return
  365. if (this.subParams.addressId == '') return this.$util.msg('请先添加收货地址~', 2000)
  366. // 优惠券,分享活动相关参数
  367. this.subParams.discountInfo = this.discountInfo
  368. // 要支付的金额
  369. this.subParams.payInfo.orderShouldPayFee = this.payAllPrice
  370. // 处理商品信息及留言
  371. this.subParams.orderInfo = this.goodsData.map(el => {
  372. let productInfo = []
  373. el.productList.forEach(pros => {
  374. productInfo.push({
  375. productId: pros.productId,
  376. productNum: pros.productCount,
  377. heUserId: pros.heUserId
  378. })
  379. })
  380. return { shopId: el.shopId, note: el.note ? el.note : '', productInfo }
  381. })
  382. // 调试提交参数
  383. console.log(this.subParams)
  384. // return
  385. this.isSubLoading = true
  386. this.loadingText = '正在创建订单...'
  387. this.OrderService.CreatedOrderSubmit(this.subParams)
  388. .then(response => {
  389. const data = response.data
  390. this.hanldOrder.order = response.data
  391. if (parseFloat(data.payableAmount) === 0) {
  392. uni.setStorageSync('orderInfo', this.hanldOrder.order)
  393. uni.redirectTo({ url: '/pages/user/order/success' })
  394. } else {
  395. this.miniWxPayFor(data)
  396. }
  397. })
  398. .catch(error => {
  399. this.isSubLoading = false
  400. this.$util.msg(error.msg, 2000)
  401. this.isSubLoading = false
  402. })
  403. }
  404. }
  405. }
  406. </script>
  407. <style lang="scss" scoped>
  408. .create-order {
  409. background: #f7f7f7;
  410. min-height: 100vh;
  411. padding-top: 134rpx;
  412. padding-bottom: 100rpx;
  413. box-sizing: border-box;
  414. .order-top {
  415. width: 100%;
  416. position: fixed;
  417. top: 0;
  418. left: 0;
  419. z-index: 999;
  420. }
  421. }
  422. .grid {
  423. width: 100%;
  424. height: 20rpx;
  425. background: #f7f7f7;
  426. }
  427. .order-list {
  428. background: #fff;
  429. .order-section {
  430. padding: 38rpx 0;
  431. }
  432. .goods-list {
  433. .order-goods {
  434. padding-top: 32rpx;
  435. &:first-child {
  436. padding-top: 0;
  437. }
  438. }
  439. }
  440. .origin {
  441. padding: 0 24rpx;
  442. margin-bottom: 16rpx;
  443. display: flex;
  444. justify-content: flex-start;
  445. align-items: center;
  446. .cover {
  447. width: 56rpx;
  448. height: 56rpx;
  449. border-radius: 4rpx;
  450. }
  451. .name {
  452. margin-left: 16rpx;
  453. font-size: 30rpx;
  454. color: #333333;
  455. }
  456. }
  457. }
  458. .total-price {
  459. padding: 24rpx;
  460. text-align: right;
  461. font-size: 26rpx;
  462. color: #333;
  463. background: #fff;
  464. .price {
  465. color: #ff457b;
  466. }
  467. }
  468. .remark {
  469. padding: 0 24rpx;
  470. display: flex;
  471. justify-content: space-between;
  472. align-items: center;
  473. background: #fff;
  474. .label {
  475. width: 78rpx;
  476. font-size: 26rpx;
  477. color: #999999;
  478. margin-right: 16rpx;
  479. }
  480. .control {
  481. flex: 1;
  482. font-size: 26rpx;
  483. color: #333333;
  484. padding: 8rpx 16rpx;
  485. background: #f7f7f7;
  486. border-radius: 8rpx;
  487. }
  488. }
  489. </style>