shopCar.vue 9.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
<template>
  <!-- 购物车 -->
  <view class="">
    <u-navbar bgColor="#F6F8FA" :placeholder="true" title="购物车" :autoBack="true"></u-navbar>
    <view class="minBox" v-if="data.shopList.length > 0">
      <!-- 商品管理 -->
      <view class="topBox flexJ">
        <view class="amount flexA" @click="toMyAddress">
          <image src="/static/shopCarIc/shopAddress.png" mode=""></image>
          <view class="adress ellipsis">{{ data.adress }}</view>
        </view>
        <view class="manage" @click="changingOver">{{ data.btnStatus ? '管理' : '完成' }}</view>
      </view>

      <!-- 商品 -->
      <view class="shopBox flexA" v-for="(item, index) in data.shopList" :key="item.id">
        <!-- 选择 -->
        <view class="checkIc" @click.stop="checkShop(index)">
          <image v-if="!item.checkType" src="/static/shopCarIc/checks.png" mode=""></image>
          <image v-else src="/static/shopCarIc/checked.png" mode=""></image>
        </view>
        <!-- 商品图片 -->
        <view class="shopPhoto">
          <image :src="item.image" mode=""></image>
          <image v-if="item.status == 0" class="shopStatus" src="/static/shopCarIc/shopType.png" mode=""></image>
        </view>
        <!-- 商品详细 -->
        <view class="infoBox">
          <!-- 商品名称 -->
          <view class="title ellipsis">{{ item.name }}</view>
          <!-- 商品数量加减 -->
          <view class="numBox flexJ">
            <view class="rightTitle flexA">
              <text>¥</text>
              {{ item.goods_price }}
            </view>
            <view class="and flexA">
              <image src="/static/shopCarIc/reduce.png" mode="" @click="numBtn(0, item)"></image>
              <view class="num flexC">{{ item.total_num }}</view>
              <image src="/static/shopCarIc/add.png" mode="" @click="numBtn(1, item)"></image>
            </view>
          </view>
        </view>
      </view>
      <view class="" style="height: 120rpx"></view>
    </view>
    <!-- 购物车为空 -->
    <view class="nullBox flexV" v-else>
      <image src="/static/shopCarIc/shopCarNull.png" mode=""></image>
      购物车为空哦
    </view>
    <!-- 底部按钮 -->
    <view class="payBox flexJ" v-if="data.shopList.length > 0">
      <!-- 全选按钮 -->
      <view class="allIc flexA" @click="checkAll">
        <image v-if="!allCheck" src="/static/shopCarIc/checks.png" mode=""></image>
        <image v-else src="/static/shopCarIc/checked.png" mode=""></image>
        <view class="">全选</view>
      </view>
      <!-- 合计金额 -->
      <view class="amountBox flexA">
        <view class="settleBox flexA" v-if="data.btnStatus">
          <view class="settle">合计:</view>
          <view class="money flexA">¥{{ +allMoney.toFixed(2) }}</view>
        </view>
        <!-- 结算和删除按钮 -->
        <view class="btns flexC" @click="settleBtn">
          {{ data.btnStatus ? '结算' : '删除' }}
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { onShow, onLoad } from '@dcloudio/uni-app'
import { getCarList, getCarAdd, getCarReduce, getDelCar } from '@/api/'
onShow(() => {
  getCarLists() //购物车列表
  // 默认地址
  uni.getStorageSync('defaultAdres') ? (data.adress = uni.getStorageSync('defaultAdres').diqu + uni.getStorageSync('defaultAdres').address) : ''
  console.log('默认', data.adress)
})
// 商品数组
let data = reactive({
  shopList: [],
  btnStatus: true, // 右上角按钮状态
  adress: '请新建默认地址'
})
// 修改右上角按钮状态
const changingOver = () => {
  console.log(data.btnStatus)
  data.btnStatus = !data.btnStatus
}
//新建收货地址
const toMyAddress = () => {
  uni.navigateTo({
    url: '/pages/mine/myAddress'
  })
}
// 勾选商品
const checkShop = index => {
  // let str = data.shopList.some(item=>item.goodstatus == 3)
  // console.log(str)
  data.shopList[index].checkType = !data.shopList[index].checkType
}
const settleBtn = () => {
  if (data.btnStatus) {
    // 结算的商品购物车id
    let arrId = data.shopList.filter(item => item.checkType).map(it => it.cart_id)
    uni.navigateTo({
      url: `/pages/shopCar/confirmOrder?ids=${arrId.join(',')}`
    })
  } else {
    let arr = data.shopList.filter(item => item.checkType).map(it => it.cart_id)
    getDelCars(arr.join(',')) //删除
  }
}
// 删除购物商品
const getDelCars = async ids => {
  try {
    const res = await getDelCar(ids)
    getCarLists()
    uni.showToast({ title: '成功移出购物车~', icon: 'none' })
    console.log('getDelCar', res)
    // 保存数据
  } catch (err) {
    uni.showToast({ title: err, icon: 'none' })
    console.log('getDelCar', err)
  }
}
// 数量加减按钮
const numBtn = (type, item) => {
  if (type == 0) {
    if (item.total_num == 1) return uni.showToast({ title: '至少一件哦~', icon: 'none' })
    getCarReduces(item.cart_id, item.total_num - 1)
    item.total_num = item.total_num - 1
  } else {
    getCarAdds(item.cart_id, item.total_num + 1)
    item.total_num = item.total_num + 1
  }
}
// 数量加
const getCarAdds = async (id, num) => {
  try {
    const res = await getCarAdd(id, num)
    // console.log('getCarAdd', res)
    getCarLists()
    // 保存数据
  } catch (err) {
    uni.showToast({ title: err, icon: 'none' })
    // console.log('getCarAdd', err)
  }
}
// 数量减
const getCarReduces = async (id, num) => {
  try {
    const res = await getCarReduce(id, num)
    getCarLists()
    // console.log('getCarReduce', res)
    // 保存数据
  } catch (err) {
    uni.showToast({ title: err, icon: 'none' })
    // console.log('getCarReduce', err)
  }
}
// 全选
const checkAll = () => {
  if (allCheck.value) {
    data.shopList.forEach(item => {
      item.checkType = false
    })
  } else {
    data.shopList.forEach(item => {
      item.checkType = true
    })
  }
}
// 购物车列表
const getCarLists = async () => {
  try {
    const res = await getCarList()
    res.goods_list.forEach(item => (item.checkType = false))
    data.shopList = res.goods_list
    console.log('是什么', data.shopList)
    console.log('getCarList', res)
    // 保存数据
  } catch (err) {
    console.log('getCarList', err)
  }
}
// 是否全选
const allCheck = computed(() => {
  return data.shopList.every(item => item.checkType === true)
})
//总价
const allMoney = computed(() => {
  return data.shopList
    .filter(it => it.checkType)
    .reduce((acc, item) => {
      return acc + item.total_price * 1
    }, 0)
})
</script>

<style lang="scss">
.u-icon__icon {
  display: none !important;
}

page {
  background: #f6f8fa;
}

.minBox {
  width: 100%;
  padding: 24rpx;
  box-sizing: border-box;

  .topBox {
    margin: 24rpx 0;

    .amount {
      image {
        margin-right: 8rpx;
        width: 24rpx;
        height: 24rpx;
      }

      .adress {
        width: 424rpx;
        line-height: 32rpx;
        color: #323233ff;
        font-size: 24rpx;
      }
    }

    .manage {
      color: #323233ff;
      font-size: 32rpx;
      line-height: 40rpx;
    }
  }

  .shopBox {
    padding: 32rpx 24rpx 24rpx 24rpx;
    border-radius: 24rpx;
    background: #ffffffff;
    margin-bottom: 24rpx;

    .checkIc {
      margin-right: 24rpx;

      image {
        width: 36rpx;
        height: 36rpx;
      }
    }

    .shopPhoto {
      position: relative;
      margin-right: 24rpx;
      width: 180rpx;
      height: 180rpx;

      image {
        width: 180rpx;
        height: 180rpx;
        border-radius: 16rpx;
        background: #f5f5f5ff;
      }

      .shopStatus {
        position: absolute;
        left: 0;
        top: 0;
        width: 180rpx;
        height: 180rpx;
        border-radius: 16rpx;
        opacity: 0.9;
      }
    }

    .infoBox {
      width: 100%;
      height: 180rpx;
      display: flex;
      flex-direction: column;
      justify-content: space-between;

      .title {
        color: #323233ff;
        font-size: 28rpx;
      }

      // .modes {
      // 	color: #00000072;

      // 	text {
      // 		font-size: 22rpx;
      // 		padding: 0 16rpx;
      // 		background: rgba(0, 0, 0, 0.1);
      // 		border-radius: 16rpx;
      // 	}
      // }

      .numBox {
        .rightTitle {
          color: #f33f2e;
          font-size: 40rpx;
          font-weight: 700;

          text {
            margin-top: 10rpx;
            color: #f33f2eff;
            font-size: 24rpx;
            font-weight: 700;
          }
        }

        .and {
          image {
            width: 56rpx;
            height: 56rpx;
          }

          .num {
            width: 64rpx;
            height: 56rpx;
            color: #333333ff;
            font-size: 28rpx;
            background: #f2f3f5ff;
            margin: 0 4rpx;
          }
        }
      }
    }
  }
}

.payBox {
  position: fixed;
  bottom: 0;
  width: 100%;
  height: 96rpx;
  background-color: #fff;

  .allIc {
    margin-left: 34rpx;
    color: #646566ff;
    font-size: 26rpx;

    image {
      margin-right: 10rpx;
      width: 32rpx;
      height: 32rpx;
    }
  }

  .amountBox {
    margin-right: 32rpx;

    .settleBox {
      margin-right: 16rpx;

      .settle {
        color: #323233ff;
        font-size: 26rpx;
      }

      .money {
        color: #f33f2eff;
        font-size: 32rpx;
        font-weight: 700;
      }
    }

    .btns {
      width: 236rpx;
      height: 72rpx;
      border-radius: 80rpx;
      opacity: 1;
      background: linear-gradient(-88deg, #fe6434ff 0%, #ff2f2fff 100%);
      color: #ffffffff;
      font-size: 28rpx;
      font-weight: 700;
    }
  }
}

.nullBox {
  margin-top: 266rpx;

  image {
    width: 262rpx;
    height: 240rpx;
    margin-bottom: 24rpx;
  }

  color: #c6c5c8;
  font-size: 28rpx;
}
</style>