cashier_app/commons/utils/storageManage.js

291 lines
7.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 存储管理对象
* 目标:将现有系统的所有需要存储的数据,统一管理
*
* @author terrfly
* @site https://www.jeequan.com
* @date 2022/04/13 07:18
*/
import appConfig from "@/config/appConfig.js"
// 应用级vue级别缓存 当存在时则读取此值, 否则读取storage中的值。 vue线程内的缓存不必要每次读取应用数据影响性能
const appCache = {
tokenVal: null, // token取值
currentUser: null, // 当前商户信息
}
const model = {
setLogin(res){
const user=res.user.user
uni.setStorageSync('logoutHandle',false)
uni.setStorageSync('shopId', res.shopId)
uni.setStorageSync('shopName',res.shopName)
uni.setStorageSync('logo',res.logo)
uni.setStorageSync('loginType',res.loginType)
uni.setStorageSync('shopUserId',user.id)
if(res.loginType=='staff'){
uni.setStorageSync('merchantName',user.createBy||user.updateBy)
}
},
// 退出清空所有的缓存数据。 (不包含 环境相关)
cleanByLogout: () => {
// 1. 清空app级别缓存。
Object.keys(appCache).forEach(k => appCache[k] = null)
const merchantName=uni.getStorageSync('merchantName')
const MerchantId=uni.getStorageSync('MerchantId')
let envName = model.env() // 获取到当前的环境变量
uni.clearStorageSync() // 清除所有的缓存信息
uni.setStorageSync('merchantName',merchantName)
uni.setStorageSync('MerchantId',MerchantId)
model.env(envName) // 重置env
},
// 获取和放置token
token: (val, isDelete = false) => {
if (isDelete) {
appCache.tokenVal = ""
return uni.removeStorageSync(appConfig.tokenKey)
}
if (val) {
// 有值,为放置
appCache.tokenVal = val
uni.setStorageSync(appConfig.tokenKey, val)
} else {
// 否则为获取
if (!appCache.tokenVal) {
//缓存取不到,获取应用本地信息
appCache.tokenVal = uni.getStorageSync(appConfig.tokenKey)
}
return appCache.tokenVal
}
},
// 获取和放置shopId
shopId: (val, isDelete = false) => {
if (isDelete) {
appCache.shopId = ""
return uni.removeStorageSync('shopId')
}
if (val) {
// 有值,为放置
appCache.shopId = val
uni.setStorageSync('shopId', val)
} else {
// 否则为获取
if (!appCache.shopId) {
//缓存取不到,获取应用本地信息
appCache.shopId = uni.getStorageSync('shopId')
}
return appCache.shopId
}
},
// 获取和放置店铺员工id
shopUserId: (val, isDelete = false) => {
if (isDelete) {
appCache.shopUserId = ""
return uni.removeStorageSync('shopUserId')
}
if (val) {
// 有值,为放置
appCache.shopUserId = val
uni.setStorageSync('shopUserId', val)
} else {
// 否则为获取
if (!appCache.shopUserId) {
//缓存取不到,获取应用本地信息
appCache.shopUserId = uni.getStorageSync('shopUserId')
}
return appCache.shopUserId
}
},
// 获取和放置useType就餐类型
useType: (val, isDelete = false) => {
if (isDelete) {
appCache.useType = ""
return uni.removeStorageSync('useType')
}
if (val) {
// 有值,为放置
appCache.useType = val
uni.setStorageSync('useType', val)
} else {
// 否则为获取
if (!appCache.useType) {
//缓存取不到,获取应用本地信息
appCache.useType = uni.getStorageSync('useType')
}
return appCache.useType
}
},
// 缓存代客下单商品
cacheGoods: (val, isDelete = false) => {
if (isDelete) {
appCache.cacheGoods = ""
return uni.removeStorageSync('cacheGoods')
}
if (val) {
// 有值,为放置
appCache.cacheGoods = val
uni.setStorageSync('cacheGoods', val)
} else {
// 否则为获取
if (!appCache.cacheGoods) {
//缓存取不到,获取应用本地信息
appCache.cacheGoods = uni.getStorageSync('cacheGoods')
}
return appCache.cacheGoods
}
},
// 缓存代客下单商品节点信息缓存
cacheGoodsNode: (val, isDelete = false) => {
if (isDelete) {
appCache.cacheGoodsNode = ""
return uni.removeStorageSync('cacheGoodsNode')
}
if (val) {
// 有值,为放置
appCache.cacheGoodsNode = val
uni.setStorageSync('cacheGoodsNode', val)
} else {
// 否则为获取
if (!appCache.cacheGoodsNode) {
//缓存取不到,获取应用本地信息
appCache.cacheGoodsNode = uni.getStorageSync('cacheGoodsNode')
}
return appCache.cacheGoodsNode
}
},
// 已经登录的用户记录
loggedInUser: (addUserName = null, removeUserName = null) => {
let key = "loggedInUserList"
// 删除
if (removeUserName) {
let nameList = uni.getStorageSync(key) || []
if (nameList.length <= 0) {
//不存在数据
return false
}
let hasUserIndex = nameList.indexOf(removeUserName)
if (hasUserIndex >= 0) {
nameList.splice(hasUserIndex, 1) //删除
uni.setStorageSync(key, nameList)
}
return false
}
// 有新插入的记录
if (addUserName) {
let nameList = uni.getStorageSync(key) || []
let hasUser = false
for (let i = 0; i < nameList.length; i++) {
if (nameList[i] == addUserName) {
hasUser = true
}
}
// 包含记录
if (hasUser) {
return false
}
// 最多存储 5 个
if (nameList.length >= 5) {
nameList.splice(0, 1) //删除第一个
}
nameList.push(addUserName)
uni.setStorageSync(key, nameList)
//获取
} else {
return uni.getStorageSync(key) || [] //默认空数组
}
},
// 用户信息
userInfo: (currentUserInfo) => {
if (currentUserInfo) {
// 仅保存基础数据
let saveUser = {
sysUserId: currentUserInfo.sysUserId, // 用户ID
realname: currentUserInfo.realname, // 用户姓名
avatarUrl: currentUserInfo.avatarUrl, // 头像
telphone: currentUserInfo.telphone, // 手机号
userType: currentUserInfo.userType, // 用户类型
mchNo: currentUserInfo.userNo, // 商户No
mchShortName: currentUserInfo.shortName, // 商户简称
mchType: currentUserInfo.mchType, // 商户类型
mchLevel: currentUserInfo.mchLevel, // 商户级别
isHasMemberEnt:currentUserInfo.isHasMemberEnt,// 是否购买会员模块
entIdList: currentUserInfo.entIdList, // 权限集合List
}
uni.setStorageSync("currentUserInfo", saveUser) // 改变存储
appCache.currentUser = null
}
if(!appCache.currentUser){ // 获取缓存数据
appCache.currentUser = uni.getStorageSync("currentUserInfo")
}
return appCache.currentUser
},
// 项目环境变量:(测试、 生产的切换)
env: (envMode) => {
if (envMode) {
uni.setStorageSync(appConfig.storeEnvEnumKey, envMode) // 改变存储
}
return uni.getStorageSync(appConfig.storeEnvEnumKey)
},
// push 状态是否开启
pushIsOpen: (pushFlag) => {
if (pushFlag) {
uni.setStorageSync('pushFlag', pushFlag) // 改变存储
}
return uni.getStorageSync('pushFlag')
},
// 网站信息
siteInfos: (siteInfos) => {
if (siteInfos) {
uni.setStorageSync("siteInfos", siteInfos) // 改变存储
}
return uni.getStorageSync("siteInfos")
},
// unipush2 cid
uniPush2Cid: (uniPush2Cid) => {
if (uniPush2Cid) {
uni.setStorageSync("uniPush2Cid", uniPush2Cid) // 改变存储
}
return uni.getStorageSync("uniPush2Cid")
},
uploadImgSize: (uploadImgSize) => {
if (uploadImgSize) {
uni.setStorageSync("uploadImgSize", uploadImgSize) // 存储 上传 图片大小限制
}
return uni.getStorageSync("uploadImgSize")
},
}
export default model