//引入使用容器
import axios from 'axios'
import { defineStore } from "pinia"
import { onBeforeMount, onMounted, reactive, ref, watch, computed } from "vue"
//向外暴露要使用的容器
//采用组合式API的方式定义一个容器
export const useAxiosStore = defineStore('getAxios', () => {
//采用axios技术请求服务端的数据
let axiosData = reactive([])
onMounted(() => {
axios.get("http://vueshop.glbuys.com/api/home/index/goodsLevel?token=1ec949a15fb709370f").then((val) => {
for (let i = 0; i < val.data.data[0].items.length; i++) {
axiosData.push(val.data.data[0].items[i])
}
})
})
//存储一个详情页的数据
let detailsData = reactive(JSON.parse(sessionStorage.getItem("detailsData")) || [])
//将要跳转到详情页的数据存储到detailsData里面
function shortTime(data) {
//声明一个开关
let flag = true;
//判断临时存储里面有没有这条数据,有的话走缓存,没有的话进行存储
for (let i = 0; i < detailsData.length; i++) {
if (detailsData[i].gid == data.gid) {
console.log("临时存储里面有该数据")
flag = false
break;
}
}
if (flag) {
detailsData.push(data)
sessionStorage.setItem("detailsData", JSON.stringify(detailsData))
}
}
//存储一个购物车的数据
let shopCartData = reactive(JSON.parse(localStorage.getItem("shopCartData")) || [])
//存储购物车数据
function shopShuju(shuju, shuliang) {
//声明一个开关
let flag = true;
//判断本地存储里面有没有这条数据,有的话走缓存,没有的话进行存储
for (let i = 0; i < shopCartData.length; i++) {
if (shopCartData[i].gid == shuju.gid) {
flag = false
let wheater = confirm("该商品已在购物车序列中,是否进行数量的加减")
if (wheater) {
shopCartData[i].num = shuliang
let newObj = {
cid: shopCartData[i].cid,
gid: shopCartData[i].gid,
image: shopCartData[i].image,
num: shuliang,
price: shopCartData[i].price,
title: shopCartData[i].title,
isChecked: shopCartData[i].isChecked
}
shopCartData.splice(i, 1)
shopCartData.push(newObj)
alert("该商品的数量已更新")
}
break;
}
}
if (flag) {
shopCartData.push(shuju)
}
}
watch(shopCartData, (newValue) => {
localStorage.setItem("shopCartData", JSON.stringify(newValue));
});
//购车里的数量加
function cartAdd(id) {
for (let i = 0; i < shopCartData.length; i++) {
if (shopCartData[i].gid == id) {
shopCartData[i].num++
shopCartData[i].isChecked=true
}
}
}
//购车里的数量减
function cartReduce(id) {
for (let i = 0; i < shopCartData.length; i++) {
if (shopCartData[i].gid == id) {
shopCartData[i].num--
shopCartData[i].isChecked=true
if (shopCartData[i].num < 1) {
shopCartData[i].num = 1
}
}
}
}
//购物车的商品删除
function cartDelete(id) {
for (let i = 0; i < shopCartData.length; i++) {
if (shopCartData[i].gid == id) {
shopCartData.splice(i, 1)
}
}
}
//修改单选框的状态
function select(id) {
for (let i = 0; i < shopCartData.length; i++) {
if (shopCartData[i].gid == id) {
shopCartData[i].isChecked = !shopCartData[i].isChecked
}
}
}
//计算单选框的状态,全为true,全选选中
let allselect = computed(() => {
return shopCartData.every(item => item.isChecked) && shopCartData.length > 0
})
//全选选中,单选全部选中
function quanxuan(checked) {
for (let i = 0; i < shopCartData.length; i++) {
shopCartData[i].isChecked = checked
}
}
//总价
let allmoney=computed(()=>{
let zja=0;
shopCartData.filter(item=>{
if (item.isChecked) {
zja+=Number(item.num*Number(item.price))
}
})
return zja.toFixed(2)
})
return {
axiosData,
detailsData,
shortTime,
shopCartData,
shopShuju,
cartAdd,
cartReduce,
cartDelete,
select,
allselect,
quanxuan,
allmoney
}
})
vue3 简易版 购物车(数据持久化处理、axios数据请求、移动端适配) 包括【列表页、详情页、购物车页面】需要自己安装依赖
需积分: 0 66 浏览量
2023-07-05
15:58:31
上传
评论
收藏 14KB ZIP 举报
QH-Theory-Web
- 粉丝: 5
- 资源: 1
最新资源
- 基于Typescript和PHP的编程知识储备库设计源码 - study-php
- Screenshot_2024-05-28-11-40-58-177_com.tencent.mm.jpg
- 基于Dart的Flutter小提琴调音器APP设计源码 - violinhelper
- 基于JavaScript和CSS的随寻订购网页设计源码 - web-order
- 基于MATLAB的声纹识别系统设计源码 - VoiceprintRecognition
- 基于Java的微服务插件集合设计源码 - wsy-plugins
- 基于Vue和微信小程序的监理日志系统设计源码 - supervisionLog
- 基于Java和LCN分布式事务框架的设计源码 - tx-lcn
- 基于Java和JavaScript的茶叶评级管理系统设计源码 - tea
- IMG_5680.JPG
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈