老师 帮忙看下实付金额的问题 谢谢

老师 帮忙看下实付金额的问题 谢谢

https://img1.sycdn.imooc.com//climg/625829300938b5ec04150740.jpg

Order.vue页面:

<template>

  <div class="order">

    <div class="order__price">实付金额 <b>¥{{calculations.price}}</b></div>

    <div class="order__btn"

         @click="() => handleShowConfirmChange(true)">提交订单</div>

  </div>

  <div class="mask"

       v-show="showConfirm"

       @click="() => handleShowConfirmChange(false)">

    <div class="mask__content"

         @click.stop>

      <h3 class="mask__content__title">确认要离开收银台?</h3>

      <p class="mask__content__desc">请尽快完成支付,否则将被取消</p>

      <div class="mask__content__btns">

        <div class="mask__content__btn mask__content__btn--first"

             @click="() => handleConfirmOrder(true)">取消订单</div>

        <div class="mask__content__btn mask__content__btn--last"

             @click="() => handleConfirmOrder(false)">确认支付</div>

      </div>

    </div>

  </div>

</template>


<script>

import { ref } from 'vue'

import { useRouter, useRoute } from 'vue-router'

import { useStore } from 'vuex'

import { post } from '../../utils/request'

import { useCommonCartEffect } from '../../effects/cartEffects'


// 下单相关逻辑

const useMakeOrderEffect = (shopId, shopName, productList) => {

  const router = useRouter()

  const store = useStore()


  const handleConfirmOrder = async (isCanceled) => {

    const products = []

    for (const i in productList.value) {

      const product = productList.value[i]

      products.push({ id: parseInt(product.id, 10), num: product.count })

    }

    try {

      const result = await post('/order/create', {

        addressId: 1,

        shopId,

        shopName: shopName.value,

        isCanceled,

        products

      })

      if (result?.errno === 0) {

        store.commit('clearCartData', shopId)

        router.push({ name: 'OrderList' })

      }

    } catch (e) {

      // 提示下单失败

    }

  }

  return { handleConfirmOrder }

}


// 蒙层展示相关的逻辑

const useShowMaskEffect = () => {

  const showConfirm = ref(false)

  const handleShowConfirmChange = (status) => {

    showConfirm.value = status

  }

  return { showConfirm, handleShowConfirmChange }

}


export default {

  name: 'Order',

  setup () {

    const route = useRoute()

    const shopId = parseInt(route.params.id, 10)

    const { calculations, shopName, productList } = useCommonCartEffect(shopId)

    const { handleConfirmOrder } = useMakeOrderEffect(shopId, shopName, productList)

    const { showConfirm, handleShowConfirmChange } = useShowMaskEffect()

    return { showConfirm, handleShowConfirmChange, calculations, handleConfirmOrder }

  }

}

</script>


<style lang="scss" scoped>

@import "../../style/viriables.scss";

.order {

  position: absolute;

  left: 0;

  right: 0;

  bottom: 0;

  display: flex;

  height: 0.49rem;

  line-height: 0.49rem;

  background: $bgColor;

  &__price {

    flex: 1;

    text-indent: 0.24rem;

    font-size: 0.14rem;

    color: $content-fontcolor;

  }

  &__btn {

    width: 0.98rem;

    background: #4fb0f9;

    color: $bgColor;

    text-align: center;

    font-size: 0.14rem;

  }

}

.mask {

  z-index: 1;

  position: absolute;

  left: 0;

  right: 0;

  bottom: 0;

  top: 0;

  background: rgba(0, 0, 0, 0.5);

  &__content {

    position: absolute;

    top: 50%;

    left: 50%;

    transform: translate(-50%, -50%);

    width: 3rem;

    height: 1.56rem;

    background: $bgColor;

    text-align: center;

    border-radius: 0.04rem;

    &__title {

      margin: 0.24rem 0 0 0;

      line-height: 0.26rem;

      font-size: 0.18rem;

      color: $content-fontcolor;

    }

    &__desc {

      margin: 0.08rem 0 0 0;

      font-size: 0.14rem;

      color: $medium-fontColor;

    }

    &__btns {

      display: flex;

      margin: 0.24rem 0.58rem;

    }

    &__btn {

      flex: 1;

      width: 0.8rem;

      line-height: 0.32rem;

      border-radius: 0.16rem;

      font-size: 0.14rem;

      &--first {

        margin-right: 0.12rem;

        border: 0.01rem solid #4fb0f9;

        color: #4fb0f9;

      }

      &--last {

        margin-left: 0.12rem;

        background: #4fb0f9;

        color: $bgColor;

      }

    }

  }

}

</style>

===========================

cartEffects.js页面:

import { computed } from 'vue'

import { useStore } from 'vuex'

// 购物车相关逻辑

export const useCommonCartEffect = (shopId) => {

  const store = useStore()

  const cartList = store.state.cartList

  const changeCartItemInfo = (shopId, productId, productInfo, num) => {

    store.commit('changeCartItemInfo', {

      shopId, productId, productInfo, num

    })

  }


  const productList = computed(() => {

    const productList = cartList[shopId]?.productList || {}

    const notEmptyProductList = {}

    for (const i in productList) {

      const product = productList[i]

      if (product.count > 0) {

        notEmptyProductList[i] = product

      }

    }

    return notEmptyProductList

  })


  const shopName = computed(() => {

    const shopName = cartList[shopId]?.shopName || ''

    return shopName

  })


  const calculations = computed(() => {

    const productList = cartList[shopId]?.productList

    const result = { total: 0, price: 0, allChecked: true }

    if (productList) {

      for (const i in productList) {

        const product = productList[i]

        result.total += product.count

        if (product.check) {

          result.price += (product.count * product.price)

        }

        if (product.count > 0 && !product.check) {

          result.allChecked = false

        }

      }

    }

    result.price = result.price.toFixed(2)

    return result

  })

  return { cartList, shopName, productList, calculations, changeCartItemInfo }

}



正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学你好,老师使用同学的代码并没有发现问题点,且同学的代码在老师这边运行时是没有问题的。如下:

https://img1.sycdn.imooc.com//climg/6258d58209bbf57f16260763.jpg

同学可以尝试打印一下关键点的数据,如下:

https://img1.sycdn.imooc.com//climg/6258d5ec095fe31309600707.jpg

https://img1.sycdn.imooc.com//climg/6258d61f0952485310670675.jpg

同学自己试试,祝学习愉快!

  • lcy_18 提问者 #1

    https://img1.sycdn.imooc.com//climg/6258fc95094dbb8110030516.jpg

    https://img1.sycdn.imooc.com//climg/6258fc880976a57312630735.jpg

    https://img1.sycdn.imooc.com//climg/6258fcb909a7b6e410370599.jpg

    2022-04-15 13:04:14
  • 好帮手慕小李 回复 提问者 lcy_18 #2

    同学你好,有没有报错?如果有请贴出来,这里打印不出来可能会存在多种情况,老师猜不出来。另外已经让客服去联系同学了,同学看到以后给客服微信回个消息,祝学习愉快!

    2022-04-15 14:34:33
  • lcy_18 提问者 回复 好帮手慕小李 #3

    没有看到报错信息

    2022-04-16 08:43:37
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师