order-create.vue 20 KB

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