Skip to content

Commit

Permalink
版本升级---yindong
Browse files Browse the repository at this point in the history
  • Loading branch information
yindong999 committed Jun 10, 2019
1 parent 1ad0c02 commit 3a24f0f
Show file tree
Hide file tree
Showing 14 changed files with 301 additions and 284 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 6 additions & 3 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
90 changes: 53 additions & 37 deletions src/store/actions.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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'):'[]')
}
6 changes: 4 additions & 2 deletions src/store/const.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
const SYNC_UPDATE = "SYNC_UPDATE"
export { SYNC_UPDATE}
const SYNC_UPDATE = 'SYNC_UPDATE'
const REDUCENUM = 'REDUCENUM'

export {SYNC_UPDATE,REDUCENUM}
22 changes: 12 additions & 10 deletions src/store/getters.js
Original file line number Diff line number Diff line change
@@ -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
}
}
15 changes: 0 additions & 15 deletions src/store/index.js

This file was deleted.

13 changes: 7 additions & 6 deletions src/store/mutations.js
Original file line number Diff line number Diff line change
@@ -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
}
}
1 change: 1 addition & 0 deletions src/store/state.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export default {
msg:1,
cars:[],
city:'北京',
obj:Object
Expand Down
14 changes: 14 additions & 0 deletions src/store/store.js
Original file line number Diff line number Diff line change
@@ -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
})
1 change: 0 additions & 1 deletion src/views/category/detailCategory.vue
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ export default {
obj:{
handler(n,o){
this.url = this.obj.interf
}
}
}
Expand Down
34 changes: 17 additions & 17 deletions src/views/category/moreCategoryList.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<template>
<div>

<div class="header">
<mt-navbar v-model="selected">
<mt-tab-item id="1" @click.native="sortSell">销量</mt-tab-item>
Expand All @@ -25,7 +26,7 @@
<span>¥{{f.SellPrice}}</span>
<span>{{f.Spec}}</span>
<span>
<i class="fa fa-plus-circle"></i>
<i class="fa fa-plus-circle" @click='addGoodsToCar(f)'></i>
</span>
</div>
</div>
Expand All @@ -41,7 +42,13 @@
</template>
<script>
import axios from 'axios'
import {mapState} from 'vuex'
import { Toast } from 'mint-ui';
export default {
computed:{
...mapState(['cars'])
},
data() {
return {
selected: "1",
Expand All @@ -59,6 +66,14 @@ export default {
};
},
methods: {
addGoodsToCar(val){
// console.log(val)
this.$store.dispatch('addGoodsToCar',val)
Toast({
message: '加入购物车成功',
duration: 1000
});
},
getMsg(url,likeName){
this.$http.get(url).then(res => {
// console.log(res.data.data.object_list);
Expand Down Expand Up @@ -143,16 +158,14 @@ export default {
this.topp = topo
});
console.log(this.$route.params.id, this.$route.params.name);
// console.log(this.$route.params.id, this.$route.params.name);
var url = this.$route.params.id;
var likeName = this.$route.params.name;
/* 用于存放结果的数据 */
if (url == undefined) {
this.$http.get("/api/yg/allCategoryData").then(res => {
// console.log(res.data.data.object_list);
// console.log(res)
var arrFruit = res.data.data.object_list;
var resFruit = arrFruit.filter(item => {
if (item.CommodityName.indexOf(likeName) !== -1) {
Expand All @@ -161,27 +174,14 @@ export default {
return false;
}
});
// console.log(resFruit)
this.fruits = resFruit
}
);
}else{
this.getMsg(url,likeName)
}
// 加载完数据执行 按销量排序的方法
this.sortSell();
},
beforeRouteEnter: (to, from, next) => {
if (from.name === "category") {
// this.$http.get(this.url).then(res=>{
// console.log(res)
// })
}
next();
}
};
</script>
Expand Down
2 changes: 1 addition & 1 deletion src/views/eat/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
>
<mt-cell
title="返回"
href="home"
to="/home"
>
</mt-cell>
<router-link
Expand Down
14 changes: 13 additions & 1 deletion src/views/home/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
</div>
</template>
<script>
import { Toast } from 'mint-ui';
import {mapActions} from "vuex"
import homeHeader from "@/components/header"
import homeBanner from "@/components/banner"
Expand Down Expand Up @@ -90,7 +91,14 @@
}
},
methods:{
...mapActions(["addGoodInCar"])
// ...mapActions(["addGoodInCar"])
addGoodInCar(val){
this.$store.dispatch('addGoodInCar',val)
Toast({
message: '加入购物车成功',
duration: 1000
});
}
},
created(){
this.$http.get("/api/yg/dataBanner").then(res=>{
Expand All @@ -102,6 +110,10 @@
this.$http.get("/api/yg/dataSort").then(res=>{
this.dataSort = res.data.data.object_list;
})
},
beforeRouteEnter (to, from, next) {
console.log(to.name)
next()
}
}
</script>
Expand Down
Loading

0 comments on commit 3a24f0f

Please sign in to comment.