order-create.vue 19 KB

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