You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
350 lines
7.9 KiB
350 lines
7.9 KiB
// 该文件用来存储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<cart.length;i++){
|
|
|
|
// console.log(cart[i])
|
|
num = cart[i].number +num
|
|
|
|
}
|
|
return num;
|
|
}
|
|
}else{
|
|
return 0;
|
|
}
|
|
|
|
}
|
|
export function removeCart() { //模拟删除购物车数据
|
|
uni.removeStorageSync('cart')
|
|
}
|
|
// 重置购物车数据 yang
|
|
export function resetCart(cart) { //
|
|
let newValue = JSON.stringify(cart)
|
|
uni.setStorageSync('cart',newValue);
|
|
}
|
|
|
|
export function setPickaddress(value){ //存储收件地址
|
|
try {
|
|
let newValue = JSON.stringify(value);
|
|
uni.setStorageSync('pickaddress', newValue);
|
|
console.log('存储收件地址信息成功', newValue);
|
|
} catch (e) {
|
|
return;
|
|
}
|
|
}
|
|
export function getPickaddress() { //获取收件地址
|
|
let location = uni.getStorageSync('pickaddress');
|
|
if (location) {
|
|
return JSON.parse(location);
|
|
}
|
|
}
|
|
export function removePickaddress() { //删除收件地址
|
|
uni.removeStorageSync('pickaddress')
|
|
}
|
|
export function setToaddress(value){ //存储取件地址
|
|
try {
|
|
let newValue = JSON.stringify(value);
|
|
uni.setStorageSync('toaddress', newValue);
|
|
console.log('存储取件地址信息成功', newValue);
|
|
} catch (e) {
|
|
return;
|
|
}
|
|
}
|
|
export function getToaddress() { //获取取件地址
|
|
let location = uni.getStorageSync('toaddress');
|
|
if (location) {
|
|
return JSON.parse(location);
|
|
}
|
|
}
|
|
export function removeToaddress() { //删除取件地址
|
|
uni.removeStorageSync('toaddress')
|
|
}
|
|
|
|
|
|
export function setSalesGoods(value) {
|
|
try {
|
|
|
|
let newValue = JSON.stringify(value);
|
|
uni.setStorageSync('SalesGoods', newValue);
|
|
} catch (e) {
|
|
return;
|
|
}
|
|
}
|
|
export function getSalesGoods() {
|
|
let GoodsDetails = uni.getStorageSync('SalesGoods');
|
|
|
|
if (GoodsDetails) {
|
|
return JSON.parse(GoodsDetails);
|
|
}
|
|
}
|
|
|
|
export function setSalesGoodsItem(value) {
|
|
try {
|
|
|
|
let newValue = JSON.stringify(value);
|
|
uni.setStorageSync('SalesGoodsItem', newValue);
|
|
} catch (e) {
|
|
return;
|
|
}
|
|
}
|
|
export function getSalesGoodsItem() {
|
|
let GoodsDetails = uni.getStorageSync('SalesGoodsItem');
|
|
if (GoodsDetails) {
|
|
return JSON.parse(GoodsDetails);
|
|
}
|
|
}
|
|
export function rmSalesGoodsItem() {
|
|
uni.removeStorageSync('SalesGoodsItem')
|
|
}
|
|
|
|
|
|
|
|
// 由于无法调取接口 点击商品进入详情只能把数据暂存在缓存内 然后详情页获取该数据渲染
|
|
export function setGoodsDetails(value) {
|
|
try {
|
|
|
|
let newValue = JSON.stringify(value);
|
|
uni.setStorageSync('GoodsDetails', newValue);
|
|
console.log('存储商品信息成功');
|
|
} catch (e) {
|
|
return;
|
|
}
|
|
}
|
|
export function getGoodsDetails() {
|
|
let GoodsDetails = uni.getStorageSync('GoodsDetails');
|
|
|
|
if (GoodsDetails) {
|
|
return JSON.parse(GoodsDetails);
|
|
}
|
|
}
|
|
|
|
|
|
export function setCartDetails(value){ //模拟存储购物车数据(为了跳转详情渲染 只能再存一个cart数据)
|
|
try {
|
|
let data = getCartDetails() || []
|
|
data.push(value)
|
|
let newValue = JSON.stringify(data);
|
|
uni.setStorageSync('CartDetails', newValue);
|
|
} catch (e) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
export function getCartDetails() {
|
|
let CartDetails = uni.getStorageSync('CartDetails');
|
|
|
|
if (CartDetails) {
|
|
return JSON.parse(CartDetails);
|
|
}
|
|
}
|
|
|
|
// 获取对应的商品详情
|
|
export function getCartDetails_goods_id(goods_id) {
|
|
let CartDetails = uni.getStorageSync('CartDetails');
|
|
|
|
|
|
if (CartDetails) {
|
|
CartDetails = JSON.parse(CartDetails)
|
|
for(let i=0;i<CartDetails.length;i++){
|
|
|
|
if(goods_id == CartDetails[i].skuId){
|
|
|
|
return CartDetails[i];
|
|
}
|
|
}
|
|
}
|
|
}
|