order-create.vue 19 KB

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