create-order.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <template>
  2. <view class="create-order">
  3. <!-- 收货地址 -->
  4. <view class="order-top">
  5. <cm-choose-address :addressData="addressData"></cm-choose-address>
  6. </view>
  7. <view class="grid"></view>
  8. <!-- 订单列表 -->
  9. <view class="order-list">
  10. <view class="order-section" v-for="(shopInfo, shopInfoIndex) in goodsData" :key="shopInfoIndex">
  11. <!-- 供应商 -->
  12. <view class="origin">
  13. <image class="cover" :src="shopInfo.logo"></image>
  14. <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>
  26. <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 class="grid"></view>
  36. <!-- 优惠券 -->
  37. <cm-order-coupon-section
  38. @click="couponVisible = true"
  39. :coupon="currentCoupon"
  40. v-if="receiveCouponList.length > 0"
  41. ></cm-order-coupon-section>
  42. <view class="grid"></view>
  43. <!-- 运费 -->
  44. <cm-order-freight-section></cm-order-freight-section>
  45. <view class="grid"></view>
  46. <!-- 提交导航 -->
  47. <cm-order-submit-nav :orderInfo="submitOrderInfo" @commit="orderSubmit"></cm-order-submit-nav>
  48. <!-- 优惠券列表 -->
  49. <cm-coupon-list
  50. title="优惠券"
  51. listType="use"
  52. :visible="couponVisible"
  53. @close="closeCouponList"
  54. :chooseAble="true"
  55. :showStatus="false"
  56. :couponList="receiveCouponList"
  57. @chooseCoupon="chooseCoupon"
  58. @confirm="closeCouponList"
  59. :currentId="currentCouponId"
  60. ></cm-coupon-list>
  61. <cm-loading :visible="isSubLoading" :text="loadingText"></cm-loading>
  62. <view class="reserved" v-if="isIphoneX"></view>
  63. </view>
  64. </template>
  65. <script>
  66. import CmChooseAddress from '@/components/cm-module/cm-choose-address/cm-choose-address.vue'
  67. import CmOrderProdcut from '@/components/cm-module/cm-order-prodcut/cm-order-prodcut.vue'
  68. import CmOrderSubmitNav from '@/components/cm-module/cm-order-submit-nav/cm-order-submit-nav.vue'
  69. import CmOrderCouponSection from '@/components/cm-module/cm-order-coupon-section/cm-order-coupon-section.vue'
  70. import CmOrderFreightSection from '@/components/cm-module/cm-order-freight-section/cm-order-freight-section.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. CmCouponList,
  86. CmLoading
  87. },
  88. data() {
  89. return {
  90. // 收货地址
  91. addressData: {},
  92. productIds: '',
  93. params: {},
  94. subParams: {
  95. userId: 0,
  96. orderInfo: [], //提交的商品信息
  97. addressId: 0,
  98. cartType: 0,
  99. payInfo: {
  100. orderShouldPayFee: 0
  101. }
  102. },
  103. goodsData: [], // 供应商下的商品
  104. allPrice: 0,
  105. productList: [], // 商品列表
  106. allCount: 0,
  107. hanldOrder: {},
  108. // 优惠券
  109. couponVisible: false,
  110. receiveCouponList: [],
  111. currentCouponId: -1,
  112. currentCoupon: {},
  113. canUseCouponList: [],
  114. notUseCouponList: [],
  115. }
  116. },
  117. computed: {
  118. ...mapGetters(['userId', 'isIphoneX']),
  119. // 选中的优惠券的金额
  120. couponAmount() {
  121. return this.currentCouponId > -1 ? this.currentCoupon.couponAmount : 0
  122. },
  123. // 优惠价格
  124. discountedPrice() {
  125. return this.couponAmount
  126. },
  127. // 支付金额
  128. payAllPrice() {
  129. const payAllPrice = this.allPrice - this.couponAmount
  130. return payAllPrice <= 0 ? 0 : payAllPrice
  131. },
  132. submitOrderInfo(){
  133. return {
  134. allCount: this.allCount,
  135. payAllPrice: this.payAllPrice,
  136. discountedPrice: this.discountedPrice
  137. }
  138. }
  139. },
  140. onLoad(options) {
  141. this.getAddressData()
  142. this.initOptions(options)
  143. },
  144. beforeDestroy() {
  145. uni.removeStorageSync('commitProductInfo')
  146. uni.removeStorageSync('commitCartPramsData')
  147. },
  148. methods: {
  149. // 初始化参数信息
  150. initOptions(options) {
  151. // 从商品详情进入
  152. if (options.type == 'prodcut') {
  153. const productInfo = uni.getStorageSync('commitProductInfo')
  154. this.params.productCount = productInfo.productCount
  155. this.params.productId = productInfo.productId
  156. this.params.heUserId = productInfo.heUserId || 0
  157. this.productIds = productInfo.productId.toString()
  158. this.allCount = productInfo.allCount
  159. } else {
  160. const cartPramsData = uni.getStorageSync('commitCartPramsData')
  161. this.params.cartIds = cartPramsData.cartIds
  162. this.productIds = cartPramsData.productIds
  163. this.allCount = cartPramsData.allCount
  164. }
  165. this.params.userId = this.userId
  166. this.subParams.userId = this.userId
  167. this.getInitCrearOrder(this.params)
  168. },
  169. // 获取可用优惠券
  170. fetchCouponList() {
  171. this.CouponService.GetCouponByProductIds({ userId: this.userId, productIds: this.productIds }).then(res => {
  172. this.receiveCouponList = setCouponUniqueId(res.data.receiveCouponList) // 去掉重复的优惠券
  173. this.filterCouponList()
  174. })
  175. },
  176. // 对优惠券进行分类排序
  177. filterCouponList() {
  178. this.goodsData.forEach(shop => this.productList.push(...shop.productList.map(prod => prod)))
  179. this.canUseCouponList = [] // 可以使用的优惠券
  180. this.notUseCouponList = [] // 需要凑单使用的优惠券
  181. this.receiveCouponList.forEach(coupon => {
  182. if (
  183. coupon.noThresholdFlag === 1 ||
  184. (coupon.productType === 1 && allProdoceUseCheck(this.productList, coupon)) ||
  185. (coupon.productType === 2 && someProductUseCheck(this.productList, coupon))
  186. ) {
  187. coupon.canSelect = true
  188. this.canUseCouponList.push(coupon)
  189. } else {
  190. coupon.canSelect = false
  191. this.notUseCouponList.push(coupon)
  192. }
  193. })
  194. // 金额高的排前面
  195. this.receiveCouponList = [...couponSort(this.canUseCouponList), ...couponSort(this.notUseCouponList)]
  196. // 当有可用优惠券时 默认选取第一个最优惠的
  197. if (this.canUseCouponList.length > 0) {
  198. this.currentCouponId = this.receiveCouponList[0].uniqueId
  199. this.currentCoupon = this.receiveCouponList[0]
  200. }
  201. // 显示界面
  202. this.isRequest = false
  203. },
  204. // 处理优惠券列表
  205. resetCouponList() {
  206. // 1将当前选中的优惠券从列表中删除
  207. // 2将当前选中的优惠券放入最前面
  208. // 3返回最新的优惠券列表
  209. // 4查找选中优惠券的索引
  210. const index = this.canUseCouponList.findIndex(coupon => coupon.uniqueId === this.currentCouponId)
  211. // 从列表中删除
  212. const currentCoupon = this.canUseCouponList.splice(index, 1)
  213. // 重新排序 将选中的优惠券放到最前面
  214. this.canUseCouponList = [...currentCoupon, ...couponSort(this.canUseCouponList)]
  215. // 重新生成receiveCouponList
  216. this.receiveCouponList = [...this.canUseCouponList, ...this.notUseCouponList]
  217. },
  218. // 确认选中
  219. closeCouponList() {
  220. this.couponVisible = false
  221. this.resetCouponList()
  222. },
  223. // 选中优惠券
  224. chooseCoupon(coupon) {
  225. if (coupon.couponId > -1) {
  226. this.currentCoupon = coupon
  227. this.currentCouponId = coupon.uniqueId
  228. } else {
  229. this.currentCoupon = null
  230. this.currentCouponId = -1
  231. }
  232. // this.couponVisible = false
  233. },
  234. //确认订单初始化信息
  235. getInitCrearOrder(params) {
  236. this.OrderService.QueryOrderConfirm(params)
  237. .then(res => {
  238. let data = res.data
  239. this.goodsData = this.bindRemark(data.shopList)
  240. console.log(this.goodsData)
  241. this.allPrice = data.totalPrice
  242. this.fetchCouponList()
  243. })
  244. .catch(error => {
  245. this.$util.msg(error.msg, 2000)
  246. })
  247. },
  248. // 为共供应商绑定留言信息
  249. bindRemark(shopList){
  250. return shopList.map(item=>{
  251. item.note = ''
  252. return item
  253. })
  254. },
  255. //获取地址信息
  256. getAddressData() {
  257. this.UserService.QueryAddressList({ pageNum: 1, pageSize: 1, userId: this.userId }).then(res => {
  258. this.addressData = res.data.list[0]
  259. this.subParams.addressId = this.addressData.addressId
  260. })
  261. },
  262. orderSubmit() {
  263. //提交订单
  264. if (this.isSubLoading) return
  265. if (this.subParams.addressId == '') return this.$util.msg('请先添加收货地址~', 2000)
  266. // 选中的优惠券id
  267. this.subParams.couponId = this.currentCouponId === -1 ? '' : this.currentCoupon.couponId
  268. this.subParams.couponShareId = this.currentCoupon ? this.currentCoupon.couponShareId : null
  269. this.subParams.payInfo.orderShouldPayFee = this.payAllPrice
  270. // 处理商品信息及留言
  271. this.subParams.orderInfo = this.goodsData.map(el => {
  272. let productInfo = []
  273. el.productList.forEach(pros => {
  274. productInfo.push({
  275. productId: pros.productId,
  276. productNum: pros.productCount,
  277. heUserId: pros.heUserId
  278. })
  279. })
  280. return { shopId: el.shopId, note: el.note ? el.note : '', productInfo }
  281. })
  282. this.isSubLoading = true
  283. this.loadingText = '正在创建订单...'
  284. this.OrderService.CreatedOrderSubmit(this.subParams)
  285. .then(response => {
  286. const data = response.data
  287. this.hanldOrder.order = response.data
  288. if (parseFloat(data.payableAmount) === 0) {
  289. uni.setStorageSync('orderInfo', this.hanldOrder.order)
  290. uni.redirectTo({ url: '/pages/user/order/success' })
  291. } else {
  292. this.miniWxPayFor(data)
  293. }
  294. })
  295. .catch(error => {
  296. this.isSubLoading = false
  297. this.$util.msg(error.msg, 2000)
  298. this.isSubLoading = false
  299. })
  300. },
  301. }
  302. }
  303. </script>
  304. <style lang="scss" scoped>
  305. .create-order {
  306. background: #f7f7f7;
  307. min-height: 100vh;
  308. padding-top: 134rpx;
  309. padding-bottom: 100rpx;
  310. box-sizing: border-box;
  311. .order-top{
  312. width: 100%;
  313. position: fixed;
  314. top: 0;
  315. left: 0;
  316. z-index: 999;
  317. }
  318. }
  319. .grid {
  320. width: 100%;
  321. height: 20rpx;
  322. background: #f7f7f7;
  323. }
  324. .order-list {
  325. background: #fff;
  326. .order-section {
  327. padding: 38rpx 0;
  328. }
  329. .goods-list {
  330. .order-goods {
  331. padding-top: 32rpx;
  332. &:first-child {
  333. padding-top: 0;
  334. }
  335. }
  336. }
  337. .origin {
  338. padding: 0 24rpx;
  339. margin-bottom: 16rpx;
  340. display: flex;
  341. justify-content: flex-start;
  342. align-items: center;
  343. .cover {
  344. width: 56rpx;
  345. height: 56rpx;
  346. border-radius: 4rpx;
  347. }
  348. .name {
  349. margin-left: 16rpx;
  350. font-size: 30rpx;
  351. color: #333333;
  352. }
  353. }
  354. }
  355. .total-price {
  356. padding: 24rpx;
  357. text-align: right;
  358. font-size: 26rpx;
  359. color: #333;
  360. background: #fff;
  361. .price {
  362. color: #ff457b;
  363. }
  364. }
  365. .remark {
  366. padding: 0 24rpx;
  367. display: flex;
  368. justify-content: space-between;
  369. align-items: center;
  370. background: #fff;
  371. .label {
  372. width: 78rpx;
  373. font-size: 26rpx;
  374. color: #999999;
  375. margin-right: 16rpx;
  376. }
  377. .control {
  378. flex: 1;
  379. font-size: 26rpx;
  380. color: #333333;
  381. padding: 8rpx 16rpx;
  382. background: #f7f7f7;
  383. border-radius: 8rpx;
  384. }
  385. }
  386. </style>