Skip to content

Commit

Permalink
LocalStorage与Redis合并
Browse files Browse the repository at this point in the history
  • Loading branch information
lyj8330328 committed Oct 26, 2018
1 parent a33a1ce commit ebf7311
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 10 deletions.
31 changes: 29 additions & 2 deletions cart.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ <h4>全部商品<span>11</span></h4>
<div class="item-msg">
<span style="line-height:70px ">
{{cart.title}}<br/>
<span v-for="(v,k) in JSON.parse(JSON.parse(cart.ownSpec))" :key="k">
<span v-for="(v,k) in JSON.parse(cart.ownSpec)" :key="k">
{{v}}
</span>
</span>
Expand Down Expand Up @@ -288,11 +288,33 @@ <h4>全部商品<span>11</span></h4>
methods:{
loadCarts() {
//1.先判断登录状态
ly.verifyUser().then(() => {
ly.verifyUser().then(res => {
//2.已经登录
//2.1 检查LocalStorage中是否有数据
const temp = ly.store.get("carts");
if (temp !== null){
//2.2有数据,传到后台与redis数据库中的融合
temp.forEach( s => {
leyou.http.post("/cart/update",{skuId:s.skuId,num:s.num}).then()
});
//2.3合并完毕,删除本地数据
ly.store.del("carts");

}
leyou.http.get("/cart").then(({data}) => {
console.log(data);
this.carts = data;
this.selected = data;
}).catch(() => {
console.log("查询失败")
})

}).catch(() => {
//3.未登陆
this.carts = ly.store.get("carts") || [];
this.carts.forEach(s => {
s.ownSpec = JSON.parse(s.ownSpec);
})
this.selected = this.carts;
this.$refs.selectAllTop.checked = true;
this.$refs.selectAllBottom.checked = true;
Expand All @@ -302,6 +324,7 @@ <h4>全部商品<span>11</span></h4>
c.num++;
ly.verifyUser().then(() =>{
//已经登录,向后台发起请求
ly.http.put("/cart",ly.stringify({skuId:c.skuId,num:c.num}))
}).catch(() => {
//未登录,直接操作本地数据库
ly.store.set("carts",this.carts);
Expand All @@ -314,6 +337,7 @@ <h4>全部商品<span>11</span></h4>
c.num--;
ly.verifyUser().then(() =>{
//已经登录,向后台发起请求
ly.http.put("/cart",ly.stringify({skuId:c.skuId,num:c.num}))
}).catch(() => {
//未登录,直接操作本地数据库
ly.store.set("carts",this.carts);
Expand All @@ -322,6 +346,9 @@ <h4>全部商品<span>11</span></h4>
deleteCart(i){
ly.verifyUser().then(() =>{
//已经登录,向后台发起请求
ly.http.delete("/cart/"+this.carts[i].skuId).then(() => {
this.carts.splice(i,1);
})
}).catch(() => {
//未登录,直接操作本地数据库
this.carts.splice(i,1);
Expand Down
13 changes: 9 additions & 4 deletions js/pages/shortcut.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ const shortcut = {
</li>\
<li v-else class='f-item'> \
请<a href='javascript:void(0)' @click='gotoLogin'>登录</a>  \
<span><a href='register.html' target='_blank'>免费注册</a></span> \
<span><a href='javascript:void(0)' @click='gotoRegister' target='_blank'>免费注册</a></span> \
</li> \
</ul> \
<ul class='fr'> \
<li class='f-item'>我的订单</li> \
<li class='f-item space'></li> \
<li class='f-item'><a href='home.html' target='_blank'>我的乐优</a></li> \
<li class='f-item'><a href='javascript:void(0)' @click='gotoHome' target='_blank'>我的乐优</a></li> \
<li class='f-item space'></li> \
<li class='f-item'>乐优会员</li> \
<li class='f-item space'></li> \
Expand Down Expand Up @@ -51,8 +51,13 @@ const shortcut = {
},
methods: {
gotoLogin() {
window.location = "login.html?returnUrl=" + window.location;

window.location = "http://www.leyou.com/login.html?returnUrl=" + window.location;
},
gotoRegister(){
window.location = "http://www.leyou.com/register.html";
},
gotoHome(){
window.location = "http://www.leyou.com/home.html";
}
}
}
Expand Down
15 changes: 12 additions & 3 deletions js/pages/top.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const lyTop = {
<div class='py-container'> \
<div class='yui3-g Logo'> \
<div class='yui3-u Left logoArea'> \
<a class='logo-bd' title='乐优' href='index.html' target='_blank'></a> \
<a class='logo-bd' title='乐优' @click='gotoIndex' href='javascript:void(0)' target='_blank'></a> \
</div> \
<div class='yui3-u Center searchArea'> \
<div class='search'> \
Expand Down Expand Up @@ -36,7 +36,7 @@ const lyTop = {
<div class='fr shopcar'> \
<div class='show-shopcar' id='shopcar'> \
<span class='car'></span> \
<a class='sui-btn btn-default btn-xlarge' href='cart.html' target='_blank'> \
<a class='sui-btn btn-default btn-xlarge' href='javascript:void(0)' @click='gotoCart' target='_blank'> \
<span>我的购物车</span> \
<i class='shopnum'>0</i> \
</a> \
Expand All @@ -61,7 +61,7 @@ const lyTop = {
<li class='f-item'>闪购</li> \
<li class='f-item'>团购</li> \
<li class='f-item'>有趣</li> \
<li class='f-item'><a href='seckill-index.html' target='_blank'>秒杀</a></li> \
<li class='f-item'><a href='javascript:void(0)' @click='gotoSeckill' target='_blank'>秒杀</a></li> \
</ul> \
</div> \
<div class='yui3-u Right'></div> \
Expand All @@ -88,6 +88,15 @@ const lyTop = {
return decodeURI(r[2]);
}
return null;
},
gotoIndex(){
window.location = "http://www.leyou.com/index.html";
},
gotoSeckill(){
window.location = "http://www.leyou.com/seckill-index.html";
},
gotoCart(){
window.location = "http://www.leyou.com/cart.html";
}
},
created() {
Expand Down
5 changes: 4 additions & 1 deletion top.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<div class="py-container">
<div class="yui3-g Logo">
<div class="yui3-u Left logoArea">
<a class="logo-bd" title="乐优" href="index.html" target="_blank"></a>
<a class="logo-bd" title="乐优" @click="gotoHome()" href="javascript:void(0)" target="_blank"></a>
</div>
<div class="yui3-u Center searchArea">
<div class="search">
Expand Down Expand Up @@ -92,6 +92,9 @@ <h4>乐优精品</h4>
return decodeURI(r[2]);
}
return null;
},
gotoHome(){
window.location = "http://www.leyou.com/index.html";
}
},
created() {
Expand Down

0 comments on commit ebf7311

Please sign in to comment.