order-create.vue 20 KB

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