B页面setStorageSync app.js里getStorageSyn
发布于 5 年前 作者 jing96 8005 次浏览 来自 问答

app.js里的代码

onLaunch: function () {

that.timer = setInterval(function () {

that.getCartTotalNum(that)

}, 100);

},

getCartTotalNum: function () {

let carts = wx.getStorageSync(‘goods_cart’);//这里的值不会改变

let cartTotalNum = 0

if (carts.stock) {

let stock = carts.stock.split(’,’);

for (var i = 0; i < stock.length; i++) {

let se = stock[i].split(’|’);

cartTotalNum += parseInt(se[1])

}

}

if (carts.vendor) {

let vendor = carts.vendor.split(’,’);

for (var i = 0; i < vendor.length; i++) {

let se = vendor[i].split(’|’);

cartTotalNum += parseInt(se[1])

}

}

if (carts.clear) {

let clear = carts.clear.split(’,’);

for (var i = 0; i < clear.length; i++) {

let se = clear[i].split(’|’);

cartTotalNum += parseInt(se[1])

}

}

if (carts.second) {

let second = carts.second.split(’,’);

for (var i = 0; i < second.length; i++) {

let se = second[i].split(’|’);

cartTotalNum += parseInt(se[1])

}

}

if (cartTotalNum > 0) {   //判断购物车的数量个数,购物车如果为空就走else

cartTotalNum = cartTotalNum > 99 ? ‘99+’ : cartTotalNum;

wx.setTabBarBadge({ //购物车不为空 ,给购物车的tabar右上角添加购物车数量标志

index: 2,           //标志添加位置

text: “” + cartTotalNum + “”        //通过编译,将购物车总数量放到这里

})

} else {//购物车为空

wx.removeTabBarBadge({    //移除指定位置的tabbar右上角的标志

index: 2,

})

}

},

B页面赋值

getCartList:function(){

var that = this;

var ids = wx.getStorageSync(‘goods_cart’);

if(!ids){

ids = app.globalData.cartType

}

let param = {};

param.openid = app.globalData.openId;

param.cart_ids = JSON.stringify(ids);

app.httpClient.post(‘api_v2/cart/all’,param)

.then(res=>{

wx.hideLoading();

let list = res.data.msg;

that.setData({

cartList: list,       //将取反后的集合setData到我们的Data

isCarts: !list.goods.length && !list.vendor.length && !list.second.length && !list.clear.length?true:false,

selectAllStatus: true, totalMoney: 0.00,totalNum: 0,

selectGoodsStatus: true, selectVendorStatus: true, selectSecondStatus: true, selectClearStatus: true, isEdit: true, editText: ‘编辑商品’,

});

let carts = app.globalData.cartType;

if(list.goods.length>0){

let st = ‘’;

for(var i = 0 ;i<list.goods.length;i++){

st += list.goods[i].spec_id + ‘|’ + list.goods[i].goods_num + ‘’ + ‘,’

}

st = st.substr(0,st.length-1);

carts.stock = st;

}

if (list.vendor.length > 0) {

let ve = ‘’;

for (var i = 0; i < list.vendor.length; i++) {

ve += list.vendor[i].spec_id + ‘|’ + list.vendor[i].goods_num + ‘’ + ‘,’

}

ve = ve.substr(0, ve.length - 1);

carts.vendor = ve;

}

if (list.clear.length > 0) {

let cl = ‘’;

for (var i = 0; i < list.clear.length; i++) {

cl += list.clear[i].spec_id + ‘|’ + list.clear[i].goods_num + ‘’ + ‘,’

}

cl = cl.substr(0, cl.length - 1);

carts.clear = cl;

}

if (list.second.length > 0) {

let se = ‘’;

for (var i = 0; i < list.second.length; i++) {

se += list.second[i].spec_id + ‘|’ + list.second[i].goods_num + ‘’ + ‘,’

}

se = se.substr(0, se.length - 1);

carts.second = se;

}

wx.setStorageSync(‘goods_cart’, carts);

that.getTotalMoney();

})

.catch(res=>{})

}




1 回复

是需要反馈什么问题?

回到顶部