order-create.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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. <!-- 供应商商品列表 -->
  9. <view class="shop-list">
  10. <view class="section" v-for="(shopInfo, index) in shopList" :key="shopInfo.shopId">
  11. <order-supplier-area
  12. :shopInfo="shopInfo"
  13. :submitType="submitType"
  14. @countChange="onCountChange"
  15. ></order-supplier-area>
  16. <view class="comment">
  17. <text>留言:</text>
  18. <input placeholder="请输入内容" placeholder-class="placeholder" v-model="shopInfo.note" />
  19. </view>
  20. <tui-divider :height="48" v-if="index < shopList.length - 1"></tui-divider>
  21. </view>
  22. </view>
  23. <!-- 优惠券 -->
  24. <view class="cell-section" @click="couponVisiable = true" v-if="receiveCouponList.length > 0">
  25. <tui-list-cell :arrow="true" :padding="cellPadding" :unlined="true">
  26. <view class="cell-content">
  27. <text class="label">优惠券</text>
  28. <text class="reduce tip">-¥{{ couponAmount | priceFormat }}</text>
  29. </view>
  30. </tui-list-cell>
  31. </view>
  32. <!-- 分享减免 -->
  33. <view class="cell-section" @click="onShare" v-if="reduction">
  34. <tui-list-cell :arrow="false" :padding="cellPadding" :unlined="true">
  35. <button class="cell-content share-btn">
  36. <text v-if="!shareStatus" class="active label">
  37. 金额满¥{{ reduction.touchPrice }}元,分享可立减¥{{ reduction.reducedAmount }}元
  38. </text>
  39. <text class="label" v-else>分享减免</text>
  40. <text class="reduce tip" v-if="!shareStatus">点击分享</text>
  41. <text class="active" v-else>-¥{{ reduction.reducedAmount | priceFormat }}</text>
  42. </button>
  43. </tui-list-cell>
  44. </view>
  45. <!-- 运费 -->
  46. <view class="cell-section">
  47. <tui-list-cell :arrow="false" :padding="cellPadding" :unlined="true">
  48. <view class="cell-content">
  49. <text class="label">运费</text>
  50. <text class="tip postage">免邮</text>
  51. </view>
  52. </tui-list-cell>
  53. </view>
  54. <!-- 提交订单 -->
  55. <order-submit :orderInfo="orderInfo" @submit="onSubmit"></order-submit>
  56. <!-- 优惠券列表 -->
  57. <cm-coupon-popup
  58. :visiable="couponVisiable"
  59. :list="receiveCouponList"
  60. :hasTabs="false"
  61. :hasConfirm="true"
  62. :hasSafeArea="true"
  63. :hasCheckedNone="true"
  64. :checkedNone="checkedNone"
  65. @close="onCouponPopupClose"
  66. @couponClick="onCouponClick"
  67. @checkedNone="onCheckedNone"
  68. ></cm-coupon-popup>
  69. <!-- 分享 -->
  70. <cm-share-popup ref="sharePopup" :data="posterData" type="normal" @share="onAfterShare">
  71. <template #title>
  72. <view class="share-title">
  73. 分享立减
  74. <text>{{ reduction.reducedAmount }}</text>
  75. </view>
  76. </template>
  77. </cm-share-popup>
  78. <!-- 订单提交拦截 -->
  79. <tui-modal :show="preventModal" :content="preventText" @click="onPreventModalConfirm"></tui-modal>
  80. <!-- 安全区域 -->
  81. <cm-safe-area-bottom></cm-safe-area-bottom>
  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.heUserId = productInfo.heUserId || 0
  238. this.confirmParams.collageFlag = this.collageFlag = productInfo.collageFlag // 是否是拼团商品
  239. this.confirmParams.couponId = productInfo.couponId
  240. this.collageId = parseInt(productInfo.collageId) || 0 // 拼团id
  241. this.allCount = productInfo.allCount
  242. } else {
  243. const cartInfo = uni.getStorageSync('COMMIT_CART_INFO')
  244. this.submitParams.cartType = 1 // 从购物车提交(标识)
  245. this.confirmParams.cartIds = cartInfo.cartIds.join()
  246. this.allCount = cartInfo.allCount
  247. }
  248. this.initOrderInfo()
  249. },
  250. // 提交订单
  251. async onSubmit() {
  252. // 用户id
  253. this.submitParams.userId = this.userId
  254. // 校验地址信息
  255. if (!this.addressInfo) {
  256. this.$toast('请先添加收货地址~')
  257. return
  258. }
  259. //设置收货地址
  260. this.submitParams.addressId = this.addressInfo.addressId
  261. // 优惠券,分享活动相关参数
  262. this.submitParams.discountInfo = this.getDiscountInfo()
  263. // 要支付的金额
  264. this.submitParams.payInfo.orderShouldPayFee = this.payAllPrice
  265. // 处理商品及留言信息
  266. this.submitParams.orderInfo = this.shopList.map(shop => {
  267. const productInfo = shop.productList.map(product => ({
  268. productId: product.productId,
  269. productNum: product.num,
  270. heUserId: product.heUserId
  271. }))
  272. return { shopId: shop.shopId, note: shop.note, productInfo, splitCode: shop.splitCode }
  273. })
  274. // 提交订单
  275. console.log(this.submitParams)
  276. try {
  277. const res = await orderSubmit(this.submitParams)
  278. this.collageId = res.data.collageId
  279. if (parseFloat(res.data.payableAmount) === 0) {
  280. uni.setStorageSync('PAY_ORDER_INFO', res.data)
  281. this.$router.redirectTo('order/pay-success')
  282. } else {
  283. // 子订单支付
  284. this.$router.redirectTo(`order/order-pay?orderId=${res.data.orderId}`)
  285. }
  286. } catch (e) {
  287. console.log(e)
  288. this.preventModal = true
  289. this.preventText = e.msg
  290. }
  291. },
  292. // 订单提交拦截确认
  293. onPreventModalConfirm() {
  294. this.preventModal = false
  295. uni.navigateBack({ delta: 1 })
  296. },
  297. // 优惠券,分享活动相关参数
  298. getDiscountInfo() {
  299. return {
  300. //优惠券ID
  301. couponId: this.currentCoupon ? this.currentCoupon.couponId : 0,
  302. //优惠券分享ID
  303. couponShareId: this.currentCoupon ? this.currentCoupon.couponShareId : 0,
  304. //拼团标识:0不参与拼团,1参与拼团
  305. collageFlag: this.collageFlag,
  306. //拼团id:参与拼团,若拼团id为空则发起拼团,不为空则与他人拼团
  307. collageId: this.collageId,
  308. //分享抵扣用户id
  309. reductionUserId: this.reductionUserId
  310. }
  311. },
  312. // 优惠券操作
  313. onCouponClick(coupon) {
  314. if (coupon.controlType === 'choose') {
  315. this.receiveCouponList.forEach(el => {
  316. if (coupon.couponId === el.couponId) {
  317. this.$set(el, 'checked', true)
  318. this.currentCoupon = el
  319. } else {
  320. this.$set(el, 'checked', false)
  321. }
  322. })
  323. this.checkedNone = false
  324. // this.couponVisiable = false
  325. // this.sortCouponList()
  326. }
  327. },
  328. // 不使用优惠券
  329. onCheckedNone() {
  330. this.receiveCouponList.forEach(el => {
  331. this.$set(el, 'checked', false)
  332. })
  333. this.currentCoupon = null
  334. this.checkedNone = true
  335. // this.sortCouponList()
  336. },
  337. // 优惠券列表关闭
  338. onCouponPopupClose() {
  339. this.couponVisiable = false
  340. this.sortCouponList()
  341. },
  342. // 初始化商品信息
  343. async initOrderInfo() {
  344. try {
  345. const res = await orderConfirm(this.confirmParams)
  346. this.totalPrice = res.data.totalPrice
  347. this.reduction = res.data.reduction
  348. this.shopList = this.bindNoteMessage(res.data.shopList)
  349. // 只有存在优惠券时才执行后续操作(阻止不必要的计算统计)
  350. if (res.data.receiveCouponList.length > 0) {
  351. this.initCouponList(res.data.receiveCouponList)
  352. }
  353. } catch (e) {
  354. console.log(e)
  355. } finally {
  356. this.isRequest = false
  357. }
  358. },
  359. // 处理优惠券列表
  360. sortCouponList() {
  361. // 1将当前选中的优惠券从列表中删除
  362. // 2将当前选中的优惠券放入最前面
  363. // 3返回最新的优惠券列表
  364. // 4查找选中优惠券的索引
  365. if (this.currentCoupon) {
  366. const uniqueId = this.currentCoupon.uniqueId
  367. const index = this.canUseCouponList.findIndex(coupon => coupon.uniqueId === uniqueId)
  368. // 从列表中删除
  369. this.canUseCouponList.splice(index, 1)
  370. // 重新排序 将选中的优惠券放到最前面
  371. this.canUseCouponList = [
  372. this.currentCoupon,
  373. ...this.canUseCouponList.sort((a, b) => b.couponAmount - a.couponAmount)
  374. ]
  375. } else {
  376. this.canUseCouponList = this.canUseCouponList.sort((a, b) => b.couponAmount - a.couponAmount)
  377. }
  378. // 重新生成receiveCouponList
  379. this.receiveCouponList = [...this.canUseCouponList, ...this.notUseCouponList]
  380. console.log(this.receiveCouponList)
  381. },
  382. // 初始化优惠券列表
  383. initCouponList(couponList) {
  384. const allProductList = this.getAllProductList()
  385. let { canUseCouponList, notUseCouponList } = splitCouponList(couponList, allProductList)
  386. this.canUseCouponList = initFormatCouponList(canUseCouponList, 'choose', true)
  387. this.notUseCouponList = initFormatCouponList(notUseCouponList, 'buy', true)
  388. this.currentCoupon = this.canUseCouponList[0]
  389. if (this.currentCoupon) {
  390. this.checkedNone = false
  391. this.$set(this.currentCoupon, 'checked', true)
  392. }
  393. this.receiveCouponList = [...this.canUseCouponList, ...this.notUseCouponList]
  394. },
  395. // 获取全部商品
  396. getAllProductList() {
  397. const list = []
  398. this.shopList.forEach(shop => {
  399. list.push(...shop.productList)
  400. })
  401. return list
  402. },
  403. // 为共供应商绑定留言信息
  404. bindNoteMessage(shopList) {
  405. return shopList.map(item => {
  406. item.note = ''
  407. return item
  408. })
  409. },
  410. // 获取收货地址
  411. async getUserAddress() {
  412. try {
  413. const res = await fetchAddressList({ userId: this.userId })
  414. this.addressList = res.data.list
  415. if (res.data.list.length > 0) {
  416. this.addressInfo = res.data.list[0]
  417. console.log(this.addressInfo)
  418. }
  419. return res
  420. } catch (e) {
  421. console.log(e)
  422. }
  423. },
  424. // 初始化用户选中地址
  425. async resetAddress() {
  426. await this.getUserAddress()
  427. if (this.$router.checkRefreshType('chooseAddress') || this.$router.checkRefreshType('createAddressBack')) {
  428. const addressInfo = uni.getStorageSync('CHOOSE_ADDRESS')
  429. if (addressInfo) {
  430. this.addressInfo = this.addressList.find(address => address.addressId === addressInfo.addressId)
  431. }
  432. }
  433. }
  434. }
  435. }
  436. </script>
  437. <style lang="scss" scoped>
  438. .placeholder {
  439. font-size: 20rpx;
  440. }
  441. .order-create {
  442. padding-bottom: 100rpx;
  443. color: #333;
  444. .shop-list {
  445. background-color: #fff;
  446. padding: 24rpx;
  447. .section {
  448. .comment {
  449. @extend .cm-flex-between;
  450. margin-top: 24rpx;
  451. text {
  452. width: 80rpx;
  453. font-size: 24rpx;
  454. }
  455. input {
  456. flex: 1;
  457. background-color: #f7f7f7;
  458. padding: 4rpx 10rpx;
  459. font-size: 24rpx;
  460. }
  461. }
  462. }
  463. }
  464. .cell-section {
  465. margin: 16rpx 0;
  466. .cell-content {
  467. @extend .cm-flex-between;
  468. .tip {
  469. margin-right: 40rpx;
  470. }
  471. .reduce {
  472. color: #f83c6c;
  473. }
  474. .postage {
  475. color: #0062cc;
  476. margin-right: 0;
  477. }
  478. .label {
  479. font-weight: bold;
  480. }
  481. &.share-btn {
  482. padding: 0;
  483. margin: 0;
  484. background-color: transparent;
  485. font-size: 26rpx;
  486. line-height: inherit;
  487. .active {
  488. color: #f83c6c;
  489. }
  490. .tip {
  491. margin-right: 0;
  492. }
  493. &::after {
  494. border: 0;
  495. }
  496. }
  497. }
  498. }
  499. .share-title {
  500. text-align: center;
  501. font-size: 34rpx;
  502. margin-bottom: 30rpx;
  503. text {
  504. color: #f83c6c;
  505. }
  506. }
  507. }
  508. </style>