order-create.vue 19 KB

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