// 该文件用来存储localStorage 本地缓存的方法 /** * 操作用户token */ import CONFIG from '@/config' import CryptoJS from './crypto-js.min.js' const tName = 'ht-h5_1_test' const isScrect = CONFIG.isPro const Screct = { getData(key) { const text = uni.getStorageSync(key) if (text) { if (isScrect) { const localJosn = CryptoJS.AES.decrypt(text, key).toString(CryptoJS.enc.Utf8); return JSON.parse(localJosn).val } return text } return null }, setData(key, val) { let v = val if (isScrect) { const info = { val, ts: tName } v = CryptoJS.AES.encrypt(JSON.stringify(info), key).toString() } uni.setStorageSync(key, v) return v } } export function setToken(value) { return Screct.setData('token', value) } export function getToken() { return Screct.getData('token') } export function removeToken() { uni.removeStorageSync('token'); } /** * 操作用户信息 */ export function setUserInfo(value) { console.log('存储用户信息成功'); return Screct.setData('userinfo', value) } export function getUserInfo() { return Screct.getData('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) console.log('新增浏览历史'); return Screct.setData('goodsHistory', goodsdata) } } else { console.log('新增浏览历史'); const newValue = [item]; return Screct.setData('goodsHistory', newValue) } } catch (e) { return; } } export function getGoodsHistory(value) { return Screct.getData('goodsHistory') || [] } /** * 存储商品信息 */ export function setGoodsData(value) { return Screct.setData('goodsdata', value) } export function getGoodsData() { return Screct.getData('goodsdata') } // 存储用户地址 export function setAddress(value) { return Screct.setData('address', value) } export function getAddress() { return Screct.getData('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 { const data = getCart() || [] data.push(value) return Screct.setData('cart', data) } catch (e) { return; } } // 有问题 只是一个获取cart而已 当一个商品多个数量时 有问题进行修改 yang export function getCart() { //模拟获取购物车数据 return Screct.getData('cart') || [] } // 获取购物车数量 yang export function getCartNumber() { const cart = getCart() if(cart.length > 0){ var num = 0; for(var i=0;i