// 该文件用来存储localStorage 本地缓存的方法 /** * 操作用户token */ export function setToken(value) { uni.setStorageSync('token', value); console.log('存储用户信息成功'); } export function getToken() { let token = uni.getStorageSync('token'); return token; } export function removeToken() { uni.removeStorageSync('token'); } /** * 操作用户信息 */ export function setUserInfo(value) { try { let newValue = JSON.stringify(value); uni.setStorageSync('userinfo', newValue); console.log('存储用户信息成功'); } catch (e) { return; } } export function getUserInfo() { let userinfo = uni.getStorageSync('userinfo'); if (userinfo) { return JSON.parse(userinfo); } } export function removeUserInfo(){ uni.removeStorageSync('userinfo'); } /** * 项目主题颜色 */ export function setConfig(value) { try { let config = JSON.stringify(value); uni.setStorageSync('config', config); console.log('存储主题成功'); } catch (e) { return; } } export function getConfig() { let config = uni.getStorageSync('config'); if (config) { return JSON.parse(config); } return null } /** * 获取推荐人id */ export function setRecommend(value) { uni.setStorageSync('recommend', value); console.log('recommend', value); } export function getRecommend() { let recommend = uni.getStorageSync('recommend'); return recommend; } /** * 浏览历史 */ export function setGoodsHistory(item) { try { let goodsdata = getGoodsHistory() const goodsLength = goodsdata.length if (goodsLength) { const hasGoods = goodsdata.some(v => v.id === item.id) if (!hasGoods) { goodsdata = goodsLength >= 20 ? goodsdata.slice(0, goodsLength - 1) : goodsdata goodsdata.unshift(item) const newValue = JSON.stringify(goodsdata); uni.setStorageSync('goodsHistory', newValue); console.log('新增浏览历史'); } } else { const newValue = JSON.stringify([item]); uni.setStorageSync('goodsHistory', newValue); console.log('新增浏览历史'); } } catch (e) { return; } } export function getGoodsHistory(value) { let goodsdata = uni.getStorageSync('goodsHistory'); if (goodsdata) { return JSON.parse(goodsdata); } return [] } /** * 存储商品信息 */ export function setGoodsData(value) { try { let newValue = JSON.stringify(value); uni.setStorageSync('goodsdata', newValue); console.log('存储商品信息成功'); } catch (e) { return; } } export function getGoodsData() { let goodsdata = uni.getStorageSync('goodsdata'); if (goodsdata) { return JSON.parse(goodsdata); } } // 存储用户地址 export function setAddress(value) { try { let newValue = JSON.stringify(value); uni.setStorageSync('address', newValue); console.log('存储地址信息成功', newValue); } catch (e) { return; } } export function getAddress() { let address = uni.getStorageSync('address'); if (address) { return JSON.parse(address); } } export function removeAddress() { uni.removeStorageSync('address'); } // 设置分类跳转 export function setTbIndex(value) { //设置菜单栏选项 uni.setStorageSync('tabIndex', value); console.log('tabIndex', value); } export function getTbIndex() { //获取菜单栏选项 let tabIndex = uni.getStorageSync('tabIndex'); return tabIndex; } export function removeTbIndex() { //移除菜单栏选项 uni.removeStorageSync('tabIndex'); } export function setlocation(value){ //存储位置信息 try { let newValue = JSON.stringify(value); uni.setStorageSync('location', newValue); console.log('存储地址信息成功', newValue); } catch (e) { return; } } export function getlocation() { //获取位置信息 let location = uni.getStorageSync('location'); if (location) { return JSON.parse(location); } } export function setCart(value){ //模拟存储购物车数据 try { let data = getCart() || [] data.push(value) let newValue = JSON.stringify(data); uni.setStorageSync('cart', newValue); // console.log('存储购物车数据成功', newValue); } catch (e) { return; } } // 有问题 只是一个获取cart而已 当一个商品多个数量时 有问题进行修改 yang export function getCart() { //模拟获取购物车数据 let cart = uni.getStorageSync('cart'); if (cart) { return JSON.parse(cart); } return [] } // 获取购物车数量 yang export function getCartNumber() { if(uni.getStorageSync('cart')){ let cart = JSON.parse(uni.getStorageSync('cart')); if (cart) { var num = 0; for(var i=0;i