order-create.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. <template>
  2. <view class="order-create" :style="{ paddingBottom: showInstructions ? '164rpx' : '100rpx' }">
  3. <tui-skeleton v-if="isRequest" :loadingType="3" :isLoading="true"></tui-skeleton>
  4. <!-- 地址选择 -->
  5. <view class="sticky-top">
  6. <order-choose-address class="sticky-top" :addressInfo="addressInfo"></order-choose-address>
  7. </view>
  8. <simple-safe-view>
  9. <!-- 供应商商品列表 -->
  10. <view class="shop-list">
  11. <view class="section" v-for="(shopInfo, index) in shopList" :key="shopInfo.shopId">
  12. <order-supplier-area
  13. :shopInfo="shopInfo"
  14. :submitType="submitType"
  15. @countChange="onCountChange"
  16. ></order-supplier-area>
  17. <view class="section_postage">
  18. <view class="postText">
  19. 运费:
  20. </view>
  21. <view class="postAgeFlag">
  22. 包邮
  23. </view>
  24. </view>
  25. <view class="comment">
  26. <text>留言:</text>
  27. <input placeholder="请输入内容" placeholder-class="placeholder" v-model="shopInfo.note" />
  28. </view>
  29. <tui-divider :height="48" v-if="index < shopList.length - 1"></tui-divider>
  30. </view>
  31. </view>
  32. <!-- 优惠券 -->
  33. <!-- <view class="cell-section" @click="couponVisiable = true" v-if="receiveCouponList.length > 0">
  34. <tui-list-cell :arrow="true" :padding="cellPadding" :unlined="true">
  35. <view class="cell-content">
  36. <text class="label">优惠券</text>
  37. <text class="reduce tip">-¥{{ couponAmount | priceFormat }}</text>
  38. </view>
  39. </tui-list-cell>
  40. </view> -->
  41. <!-- 分享减免 -->
  42. <!-- <view class="cell-section" @click="onShare" v-if="reduction">
  43. <tui-list-cell :arrow="false" :padding="cellPadding" :unlined="true">
  44. <button class="cell-content share-btn">
  45. <text v-if="!shareStatus" class="active label">
  46. 金额满¥{{ reduction.touchPrice }}元,分享可立减¥{{ reduction.reducedAmount }}元
  47. </text>
  48. <text class="label" v-else>分享减免</text>
  49. <text class="reduce tip" v-if="!shareStatus">点击分享</text>
  50. <text class="active" v-else>-¥{{ reduction.reducedAmount | priceFormat }}</text>
  51. </button>
  52. </tui-list-cell>
  53. </view> -->
  54. <!-- 运费 -->
  55. <!-- <view class="cell-section">
  56. <tui-list-cell :arrow="false" :padding="cellPadding" :unlined="true">
  57. <view class="cell-content">
  58. <text class="label">运费</text>
  59. <text class="tip postage">免邮</text>
  60. </view>
  61. </tui-list-cell>
  62. </view> -->
  63. </simple-safe-view>
  64. <!-- 提交订单 -->
  65. <order-submit
  66. :orderInfo="orderInfo"
  67. @submit="onSubmit"
  68. :showAgreement="returnGoodsStutas"
  69. :agreementContent="helpContent"
  70. ></order-submit>
  71. <!-- 优惠券列表 -->
  72. <cm-coupon-popup
  73. :visiable="couponVisiable"
  74. :list="receiveCouponList"
  75. :hasTabs="false"
  76. :hasConfirm="true"
  77. :hasSafeArea="true"
  78. :hasCheckedNone="true"
  79. :checkedNone="checkedNone"
  80. @close="onCouponPopupClose"
  81. @couponClick="onCouponClick"
  82. @checkedNone="onCheckedNone"
  83. ></cm-coupon-popup>
  84. <!-- 分享 -->
  85. <cm-share-popup ref="sharePopup" :data="posterData" type="normal" @share="onAfterShare">
  86. <template #title>
  87. <view class="share-title">
  88. 分享立减
  89. <text>{{ reduction.reducedAmount }}</text>
  90. </view>
  91. </template>
  92. </cm-share-popup>
  93. <!-- 订单提交拦截 -->
  94. <tui-modal :show="preventModal" :content="preventText" @click="onPreventModalConfirm"></tui-modal>
  95. </view>
  96. </template>
  97. <script>
  98. import { orderConfirm, orderSubmit, orderReductionShare } from '@/services/api/order.js'
  99. import { fetchAddressList } from '@/services/api/user.js'
  100. import { initFormatCouponList, splitCouponList, chooseBestCoupon } from '@/common/business.helper.js'
  101. import { queryStringify } from '@/common/utils.js'
  102. import { mapGetters } from 'vuex'
  103. import wechatPayMixins from '@/pages/views/order/mixins/wechatPay.js'
  104. export default {
  105. mixins: [wechatPayMixins],
  106. data() {
  107. return {
  108. isRequest: true,
  109. submitType: 'cart', // 订单提交来源 cart product shareBuy
  110. // 确认订单参数信息
  111. confirmParams: {
  112. userId: '',
  113. cartIds: '',
  114. // skuIds: '',
  115. productId: '',
  116. productCount: '',
  117. heUserId: '',
  118. collageFlag: 0,
  119. couponId: ''
  120. },
  121. // 订单提交参数
  122. submitParams: {
  123. userId: 0,
  124. orderInfo: [], //提交的商品信息
  125. addressId: 0,
  126. cartType: 0,
  127. payInfo: {
  128. orderShouldPayFee: 0
  129. },
  130. discountInfo: null
  131. },
  132. // 地址
  133. addressList: [],
  134. addressInfo: null,
  135. // 商品信息
  136. shopList: [],
  137. // 优惠券
  138. couponVisiable: false,
  139. receiveCouponList: [],
  140. currentCoupon: null,
  141. checkedNone: true,
  142. notUseCouponList: [],
  143. canUseCouponList: [],
  144. // 拼团
  145. collageFlag: 0, //拼团标识:0不参与拼团,1参与拼团
  146. collageId: 0, //拼团id:参与拼团,若拼团id为空则发起拼团,不为空则与他人拼团
  147. // 分享抵扣
  148. shareReductStatus: false,
  149. reduction: null,
  150. reductionUserId: 0, //分享抵扣用户id
  151. allCount: 0, // 全部商品数量
  152. // 统计价格(接口返回)
  153. totalPrice: 0,
  154. preventModal: false,
  155. preventText: '',
  156. // 分享数据
  157. posterData: {},
  158. shareStatus: false,
  159. returnGoodsStutas: false,
  160. helpContent: ''
  161. }
  162. },
  163. computed: {
  164. ...mapGetters(['userId']),
  165. cellPadding() {
  166. return '24rpx 24rpx'
  167. },
  168. // 优惠券金额
  169. couponAmount() {
  170. if (this.currentCoupon) {
  171. return this.currentCoupon.couponAmount
  172. } else {
  173. return 0
  174. }
  175. },
  176. // 分享减免金额
  177. shareReducedAmount() {
  178. return this.reduction && this.shareStatus ? this.reduction.reducedAmount : 0
  179. },
  180. // 减免金额
  181. discountedPrice() {
  182. return this.couponAmount + this.shareReducedAmount
  183. },
  184. // 支付金额
  185. payAllPrice() {
  186. const price = this.totalPrice - this.discountedPrice
  187. if (price < 0) {
  188. return 0
  189. }
  190. return price
  191. },
  192. orderInfo() {
  193. return {
  194. payAllPrice: this.payAllPrice,
  195. discountedPrice: this.discountedPrice,
  196. allCount: this.allCount
  197. }
  198. },
  199. showInstructions() {
  200. return true
  201. }
  202. },
  203. onLoad(options) {
  204. this.initOptions(options)
  205. },
  206. onShow() {
  207. this.resetAddress()
  208. },
  209. beforeDestroy() {
  210. uni.removeStorageSync('COMMIT_CART_INFO')
  211. uni.removeStorageSync('COMMIT_PRODUCT_INFO')
  212. uni.removeStorageSync('CHOOSE_ADDRESS')
  213. },
  214. onShareAppMessage() {
  215. // 加密
  216. const state_str = encodeURIComponent(this.$crypto.encrypt({ type: 0 }))
  217. return {
  218. title: '护肤上颜选,正品有好货~',
  219. path: `/pages/index/index?state_str=${state_str}`,
  220. imageUrl: this.staticUrl + 'icon-share.png'
  221. }
  222. },
  223. methods: {
  224. // 分享事件
  225. onShare() {
  226. this.posterData = { query: queryStringify({ type: 0 }) }
  227. this.$refs.sharePopup.open()
  228. },
  229. // 分享结束
  230. async onAfterShare() {
  231. try {
  232. if (!this.reduction) return
  233. const { reductionId } = this.reduction
  234. const res = await orderReductionShare({ reductionId, shareType: 1, userId: this.userId })
  235. this.reductionUserId = res.data
  236. this.$toast('以为您减免金额')
  237. this.shareStatus = true
  238. } catch (e) {
  239. this.$toast('分享失败')
  240. }
  241. },
  242. // 商品数量变化
  243. onCountChange(value) {
  244. this.confirmParams.productCount = value
  245. this.initOrderInfo()
  246. },
  247. // 初始化参数信息
  248. initOptions(options) {
  249. this.submitType = options.type
  250. this.confirmParams.userId = this.userId
  251. if (this.submitType === 'product' || this.submitType === 'shareBuy') {
  252. const productInfo = uni.getStorageSync('COMMIT_PRODUCT_INFO')
  253. this.submitParams.cartType = 2 // 从商品详情 / 拼单入口提交(标识)
  254. this.confirmParams.productCount = productInfo.productCount
  255. this.confirmParams.productId = productInfo.productId
  256. this.confirmParams.skuId = productInfo.skuId
  257. this.confirmParams.heUserId = productInfo.heUserId || 0
  258. this.confirmParams.collageFlag = this.collageFlag = productInfo.collageFlag // 是否是拼团商品
  259. this.confirmParams.couponId = productInfo.couponId
  260. this.collageId = parseInt(productInfo.collageId) || 0 // 拼团id
  261. this.allCount = productInfo.allCount
  262. } else {
  263. const cartInfo = uni.getStorageSync('COMMIT_CART_INFO')
  264. this.submitParams.cartType = 1 // 从购物车提交(标识)
  265. this.confirmParams.cartIds = cartInfo.cartIds.join()
  266. // this.confirmParams.skuIds = cartInfo.skuIds.join()
  267. this.allCount = cartInfo.allCount
  268. }
  269. this.initOrderInfo()
  270. },
  271. // 提交订单
  272. async onSubmit() {
  273. // 用户id
  274. this.submitParams.userId = this.userId
  275. // 校验地址信息
  276. if (!this.addressInfo) {
  277. this.$toast('请先添加收货地址~')
  278. return
  279. }
  280. //设置收货地址
  281. this.submitParams.addressId = this.addressInfo.addressId
  282. // 优惠券,分享活动相关参数
  283. this.submitParams.discountInfo = this.getDiscountInfo()
  284. // 要支付的金额
  285. this.submitParams.payInfo.orderShouldPayFee = this.payAllPrice
  286. // 处理商品及留言信息
  287. this.submitParams.orderInfo = this.shopList.map(shop => {
  288. const productInfo = shop.productList.map(product => ({
  289. productId: product.productId,
  290. productNum: product.num,
  291. heUserId: product.heUserId,
  292. skuId: product.skuId
  293. }))
  294. return { shopId: shop.shopId, note: shop.note, productInfo, splitCode: shop.splitCode }
  295. })
  296. // 提交订单
  297. console.log(this.submitParams)
  298. try {
  299. const res = await orderSubmit(this.submitParams)
  300. this.collageId = res.data.collageId
  301. if (parseFloat(res.data.payableAmount) === 0) {
  302. uni.setStorageSync('PAY_ORDER_INFO', res.data)
  303. this.$router.redirectTo('order/pay-success')
  304. } else {
  305. // 子订单支付
  306. this.$router.redirectTo(`order/order-pay?orderId=${res.data.orderId}`)
  307. }
  308. } catch (e) {
  309. console.log(e)
  310. this.preventModal = true
  311. this.preventText = e.msg
  312. }
  313. },
  314. // 订单提交拦截确认
  315. onPreventModalConfirm() {
  316. this.preventModal = false
  317. uni.navigateBack({ delta: 1 })
  318. },
  319. // 优惠券,分享活动相关参数
  320. getDiscountInfo() {
  321. return {
  322. //优惠券ID
  323. couponId: this.currentCoupon ? this.currentCoupon.couponId : 0,
  324. //优惠券分享ID
  325. couponShareId: this.currentCoupon ? this.currentCoupon.couponShareId : 0,
  326. //拼团标识:0不参与拼团,1参与拼团
  327. collageFlag: this.collageFlag,
  328. //拼团id:参与拼团,若拼团id为空则发起拼团,不为空则与他人拼团
  329. collageId: this.collageId,
  330. //分享抵扣用户id
  331. reductionUserId: this.reductionUserId
  332. }
  333. },
  334. // 优惠券操作
  335. onCouponClick(coupon) {
  336. if (coupon.controlType === 'choose') {
  337. this.receiveCouponList.forEach(el => {
  338. if (coupon.couponId === el.couponId) {
  339. this.$set(el, 'checked', true)
  340. this.currentCoupon = el
  341. } else {
  342. this.$set(el, 'checked', false)
  343. }
  344. })
  345. this.checkedNone = false
  346. // this.couponVisiable = false
  347. // this.sortCouponList()
  348. }
  349. },
  350. // 不使用优惠券
  351. onCheckedNone() {
  352. this.receiveCouponList.forEach(el => {
  353. this.$set(el, 'checked', false)
  354. })
  355. this.currentCoupon = null
  356. this.checkedNone = true
  357. // this.sortCouponList()
  358. },
  359. // 优惠券列表关闭
  360. onCouponPopupClose() {
  361. this.couponVisiable = false
  362. this.sortCouponList()
  363. },
  364. // 初始化商品信息
  365. async initOrderInfo() {
  366. try {
  367. const res = await orderConfirm(this.confirmParams)
  368. this.totalPrice = res.data.totalPrice
  369. this.reduction = res.data.reduction
  370. this.shopList = this.bindNoteMessage(res.data.shopList)
  371. // 只有存在优惠券时才执行后续操作(阻止不必要的计算统计)
  372. if (res.data.receiveCouponList.length > 0) {
  373. this.initCouponList(res.data.receiveCouponList)
  374. }
  375. // 特殊商品退货须知
  376. this.returnGoodsStutas = res.data.returnGoodsStutas && res.data.returnGoodsStutas === 2 // 1:可以 2:不可以
  377. this.helpContent = res.data.helpContent
  378. } catch (e) {
  379. console.log(e)
  380. } finally {
  381. this.isRequest = false
  382. }
  383. },
  384. // 处理优惠券列表
  385. sortCouponList() {
  386. // 1将当前选中的优惠券从列表中删除
  387. // 2将当前选中的优惠券放入最前面
  388. // 3返回最新的优惠券列表
  389. // 4查找选中优惠券的索引
  390. if (this.currentCoupon) {
  391. const uniqueId = this.currentCoupon.uniqueId
  392. const index = this.canUseCouponList.findIndex(coupon => coupon.uniqueId === uniqueId)
  393. // 从列表中删除
  394. this.canUseCouponList.splice(index, 1)
  395. // 重新排序 将选中的优惠券放到最前面
  396. this.canUseCouponList = [
  397. this.currentCoupon,
  398. ...this.canUseCouponList.sort((a, b) => b.couponAmount - a.couponAmount)
  399. ]
  400. } else {
  401. this.canUseCouponList = this.canUseCouponList.sort((a, b) => b.couponAmount - a.couponAmount)
  402. }
  403. // 重新生成receiveCouponList
  404. this.receiveCouponList = [...this.canUseCouponList, ...this.notUseCouponList]
  405. console.log(this.receiveCouponList)
  406. },
  407. // 初始化优惠券列表
  408. initCouponList(couponList) {
  409. const allProductList = this.getAllProductList()
  410. let { canUseCouponList, notUseCouponList } = splitCouponList(couponList, allProductList)
  411. this.canUseCouponList = initFormatCouponList(canUseCouponList, 'choose', true)
  412. this.notUseCouponList = initFormatCouponList(notUseCouponList, 'buy', true)
  413. this.currentCoupon = this.canUseCouponList[0]
  414. if (this.currentCoupon) {
  415. this.checkedNone = false
  416. this.$set(this.currentCoupon, 'checked', true)
  417. }
  418. this.receiveCouponList = [...this.canUseCouponList, ...this.notUseCouponList]
  419. },
  420. // 获取全部商品
  421. getAllProductList() {
  422. const list = []
  423. this.shopList.forEach(shop => {
  424. list.push(...shop.productList)
  425. })
  426. return list
  427. },
  428. // 为共供应商绑定留言信息
  429. bindNoteMessage(shopList) {
  430. return shopList.map(item => {
  431. item.note = ''
  432. return item
  433. })
  434. },
  435. // 获取收货地址
  436. async getUserAddress() {
  437. try {
  438. const res = await fetchAddressList({ userId: this.userId })
  439. this.addressList = res.data.list
  440. if (res.data.list.length > 0) {
  441. this.addressInfo = res.data.list[0]
  442. console.log(this.addressInfo)
  443. }
  444. return res
  445. } catch (e) {
  446. console.log(e)
  447. }
  448. },
  449. // 初始化用户选中地址
  450. async resetAddress() {
  451. await this.getUserAddress()
  452. if (this.$router.checkRefreshType('chooseAddress') || this.$router.checkRefreshType('createAddressBack')) {
  453. const addressInfo = uni.getStorageSync('CHOOSE_ADDRESS')
  454. if (addressInfo) {
  455. this.addressInfo = this.addressList.find(address => address.addressId === addressInfo.addressId)
  456. }
  457. }
  458. }
  459. }
  460. }
  461. </script>
  462. <style lang="scss" scoped>
  463. .placeholder {
  464. font-size: 20rpx;
  465. }
  466. .order-create {
  467. // padding-bottom: 100rpx;
  468. color: #333;
  469. .shop-list {
  470. background-color: #fff;
  471. padding: 24rpx;
  472. .section {
  473. .section_postage {
  474. @extend .cm-flex-between;
  475. font-size: 24rpx;
  476. margin: 30rpx 0;
  477. .postAgeFlag {
  478. font-weight: bold;
  479. color: #2398FF;
  480. }
  481. }
  482. .comment {
  483. @extend .cm-flex-between;
  484. margin-top: 24rpx;
  485. text {
  486. width: 80rpx;
  487. font-size: 24rpx;
  488. }
  489. input {
  490. flex: 1;
  491. background-color: #f7f7f7;
  492. padding: 4rpx 10rpx;
  493. font-size: 24rpx;
  494. }
  495. }
  496. }
  497. }
  498. .cell-section {
  499. margin: 16rpx 0;
  500. .cell-content {
  501. @extend .cm-flex-between;
  502. .tip {
  503. margin-right: 40rpx;
  504. }
  505. .reduce {
  506. color: #f83c6c;
  507. }
  508. .postage {
  509. color: #0062cc;
  510. margin-right: 0;
  511. }
  512. .label {
  513. font-weight: bold;
  514. }
  515. &.share-btn {
  516. padding: 0;
  517. margin: 0;
  518. background-color: transparent;
  519. font-size: 26rpx;
  520. line-height: inherit;
  521. .active {
  522. color: #f83c6c;
  523. }
  524. .tip {
  525. margin-right: 0;
  526. }
  527. &::after {
  528. border: 0;
  529. }
  530. }
  531. }
  532. }
  533. .share-title {
  534. text-align: center;
  535. font-size: 34rpx;
  536. margin-bottom: 30rpx;
  537. text {
  538. color: #f83c6c;
  539. }
  540. }
  541. }
  542. </style>