From 3a24f0fc5b45a6b1f93af5c7933ca6e5ffa141cc Mon Sep 17 00:00:00 2001 From: yindong999 <1015182017@qq.com> Date: Mon, 10 Jun 2019 17:03:44 +0800 Subject: [PATCH] =?UTF-8?q?=E7=89=88=E6=9C=AC=E5=8D=87=E7=BA=A7---yindong?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 - src/main.js | 9 +- src/store/actions.js | 90 +++--- src/store/const.js | 6 +- src/store/getters.js | 22 +- src/store/index.js | 15 - src/store/mutations.js | 13 +- src/store/state.js | 1 + src/store/store.js | 14 + src/views/category/detailCategory.vue | 1 - src/views/category/moreCategoryList.vue | 34 +-- src/views/eat/index.vue | 2 +- src/views/home/index.vue | 14 +- src/views/shoppingCart/index.vue | 363 +++++++++++------------- 14 files changed, 301 insertions(+), 284 deletions(-) delete mode 100644 src/store/index.js create mode 100644 src/store/store.js diff --git a/package.json b/package.json index ce3b980..fa6ffe1 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,6 @@ "axios": "^0.19.0", "babel-plugin-component": "^1.1.1", "core-js": "^2.6.5", - "mint-ui": "^2.2.13", "swiper": "^4.5.0", "vue": "^2.6.10", "vue-router": "^3.0.3", diff --git a/src/main.js b/src/main.js index 96daf40..d35ce4c 100644 --- a/src/main.js +++ b/src/main.js @@ -1,18 +1,21 @@ import Vue from 'vue' import App from './App.vue' import router from './router' -import store from './store' +import store from './store/store' import "./modules/rem.js" import "@/stylesheets/main.scss" import "swiper/dist/css/swiper.min.css" import axios from "axios" import SIdentify from './views/login/identify.vue' -import { Navbar, TabItem,Lazyload,InfiniteScroll,Cell,Popup} from 'mint-ui' - +import {Checklist, Navbar, TabItem,Lazyload,InfiniteScroll,Cell,Popup,Button} from 'mint-ui' + +Vue.component("my-checklist", Checklist); Vue.component("mt-popup", Popup); Vue.component("mt-navbar", Navbar); Vue.component("mt-tab-item", TabItem); +Vue.component("my-button", Button); + Vue.use(Lazyload); Vue.use(SIdentify); diff --git a/src/store/actions.js b/src/store/actions.js index bb67e73..94f6329 100644 --- a/src/store/actions.js +++ b/src/store/actions.js @@ -1,41 +1,55 @@ -import { SYNC_UPDATE } from "./const" +import {SYNC_UPDATE,REDUCENUM} from './const' + export default { - transfer(store,obj){ + transfer(store,obj){ store.commit('transfer',obj) }, - initCar(store){ - let cars = getCar(); - store.commit(SYNC_UPDATE,cars) - }, - addGoodInCar(store,goodInfo){ - let cars = getCar(); - let isHas = cars.some(item=>{ - if (item._id === goodInfo._id){ - item.num++; - return true; - } - }); - if(!isHas){ - goodInfo.num = 1; - cars.push(goodInfo); - }; - localStorage.cars = JSON.stringify(cars); - store.commit(SYNC_UPDATE,cars) - }, - reduceGoodIncar(store,goodInfo){ - let cars =getCar(); - cars = cars.filter(item=>{ - if(item._id == goodInfo._id){ - item.num--; - if(item.num <= 0) return false; - } - return true; - }) - localStorage.cars = JSON.stringify(cars) - store.commit(SYNC_UPDATE, cars) - }, - deleteGoodInCar(store,goodInfo){ - let cars =getCar(); + initCar(store){ + let cars = getData() + store.commit(SYNC_UPDATE,cars) + }, + /* 减少 */ + reduceNum(store,reduceInfor){ + // 使用定时器操作; + + let reduceCars = getData() ;// 从locaStorage中获取数据 + let newArr = reduceCars.filter(item=>{ + if(item.CommodityId === reduceInfor.CommodityId){ + item.num -- + if(item.num<=0) + return false + } + return true + }) + localStorage.setItem('cars',JSON.stringify(newArr)) // 通知模拟的后台去更改数据 + store.commit(SYNC_UPDATE,newArr) + + }, + /* 添加物品到购物车的方法 */ + addGoodsToCar(store,goodsInfor){ + console.log(localStorage.getItem('cars')) + + setTimeout(()=>{ + let cars = getData() + let res = cars.some(item=>{ + if(item.CommodityId === goodsInfor.CommodityId){ + item.num ++ // 如果相等说明已经存在了要添加的数据;把数量加1就可以了; + return true + } + return false + }) + if(!res){ // 如果res为false;说明没有要添加的数据; + goodsInfor.num = 1 + cars.push(goodsInfor) + + } + store.commit(SYNC_UPDATE,cars) // 通知mutations中的相关方法更改state中的数据 + localStorage.setItem('cars',JSON.stringify(cars)) // 通知模拟的后台去更改数据 + },1) + + }, + deleteGoodInCar(store,goodInfo){ + let cars =getData(); cars = cars.filter(item=>{ if(item._id == goodInfo._id){ return false; @@ -46,6 +60,8 @@ export default { store.commit(SYNC_UPDATE, cars) } } -function getCar(){ - return JSON.parse(localStorage.cars?localStorage.cars:"[]"); + +// 依靠localStorage模拟后台 +function getData(){ + return JSON.parse(localStorage.getItem('cars')?localStorage.getItem('cars'):'[]') } \ No newline at end of file diff --git a/src/store/const.js b/src/store/const.js index 10b30ed..77dd506 100644 --- a/src/store/const.js +++ b/src/store/const.js @@ -1,2 +1,4 @@ -const SYNC_UPDATE = "SYNC_UPDATE" -export { SYNC_UPDATE} \ No newline at end of file +const SYNC_UPDATE = 'SYNC_UPDATE' +const REDUCENUM = 'REDUCENUM' + +export {SYNC_UPDATE,REDUCENUM} \ No newline at end of file diff --git a/src/store/getters.js b/src/store/getters.js index 7e79dd4..661e36e 100644 --- a/src/store/getters.js +++ b/src/store/getters.js @@ -1,12 +1,14 @@ export default { - computeTotal(state){ - let cars = state.cars; - let total = { price: 0, num: 0 } - cars.forEach(item=>{ - total.price += item.commoditysComponentList[0].commodityPrice * item.num; - total.num += item.num - }) - total.price = total.price.toFixed(2);//保留2位小数 3.45 - return total - } + computeTotal(state){ + let arr = state.cars + var total = {price:0,num:0} + + arr.forEach(item=>{ + total.num += item.num + + total.price += Number(item.SellPrice) * item.num + }) + total.price = total.price.toFixed(2) + return total + } } \ No newline at end of file diff --git a/src/store/index.js b/src/store/index.js deleted file mode 100644 index db3a1f8..0000000 --- a/src/store/index.js +++ /dev/null @@ -1,15 +0,0 @@ -import Vue from 'vue' -import Vuex from 'vuex' - -Vue.use(Vuex) - -import state from "./state" -import mutations from "./mutations" -import actions from "./actions" -import getters from "./getters" -export default new Vuex.Store({ - getters, - mutations, - actions, - state -}) \ No newline at end of file diff --git a/src/store/mutations.js b/src/store/mutations.js index 1cb961b..e20154a 100644 --- a/src/store/mutations.js +++ b/src/store/mutations.js @@ -1,9 +1,10 @@ -import {SYNC_UPDATE} from "./const" -export default { - [SYNC_UPDATE](state,newCar){ - state.cars = newCar; - }, - transfer(state,obj){ +import {SYNC_UPDATE,REDUCENUM} from './const' + +export default{ + [SYNC_UPDATE](state,newCar){ + state.cars = newCar + } , + transfer(state,obj){ state.obj = obj } } \ No newline at end of file diff --git a/src/store/state.js b/src/store/state.js index 54c680b..05b5a86 100644 --- a/src/store/state.js +++ b/src/store/state.js @@ -1,4 +1,5 @@ export default { + msg:1, cars:[], city:'北京', obj:Object diff --git a/src/store/store.js b/src/store/store.js new file mode 100644 index 0000000..3ad0172 --- /dev/null +++ b/src/store/store.js @@ -0,0 +1,14 @@ +import Vue from 'vue' +import Vuex from 'vuex' + +Vue.use(Vuex) + +// 引入state.js getters.js mutations.js actions.js 模块 +import state from './state' +import getters from './getters' +import mutations from './mutations' +import actions from './actions' + +export default new Vuex.Store({ + state,getters,mutations,actions +}) diff --git a/src/views/category/detailCategory.vue b/src/views/category/detailCategory.vue index 442d216..b61bd82 100644 --- a/src/views/category/detailCategory.vue +++ b/src/views/category/detailCategory.vue @@ -65,7 +65,6 @@ export default { obj:{ handler(n,o){ this.url = this.obj.interf - } } } diff --git a/src/views/category/moreCategoryList.vue b/src/views/category/moreCategoryList.vue index 0223cc2..faf8e6b 100644 --- a/src/views/category/moreCategoryList.vue +++ b/src/views/category/moreCategoryList.vue @@ -1,5 +1,6 @@ diff --git a/src/views/eat/index.vue b/src/views/eat/index.vue index 64a93f2..a7b64e8 100644 --- a/src/views/eat/index.vue +++ b/src/views/eat/index.vue @@ -6,7 +6,7 @@ > diff --git a/src/views/shoppingCart/index.vue b/src/views/shoppingCart/index.vue index 619ba92..aa9ecb1 100644 --- a/src/views/shoppingCart/index.vue +++ b/src/views/shoppingCart/index.vue @@ -1,197 +1,180 @@ \ No newline at end of file