【前端】云工厂的纸掌柜app
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.

46 lines
1.2 KiB

// 自定义一个简单的状态管理工具,不允许直接调用storage
// 后续可以换vuex
import {reactive,readonly} from 'vue'
let userInfoString = uni.getStorageSync('userInfo') || '{}'
let userInfoStore = JSON.parse(userInfoString)
const userInfo = reactive({
userId:userInfoStore.userId,
name:userInfoStore.name,
userType:userInfoStore.userType
})
let globalInfoString = uni.getStorageSync('userInfo') || '{}'
let globalInfoStore = JSON.parse(globalInfoString)
const globalInfo = reactive({
token:globalInfoStore['Qn_token'],
loginToken:globalInfoStore['loginToken']
})
export const userAction = {
getUserInfo:() => {
return readonly(userInfo)
},
setUserInfo:(newValue) =>{
let keys = Object.keys(newValue)
keys.forEach(key => {
if(userInfo.hasOwnProperty(key)) {
userInfo[key] = newValue[key]
}
})
uni.setStorageSync('userInfo',JSON.stringify(userInfo))
}
}
export const globalAction = {
getGlobalInfo:() => {
return readonly(globalInfo)
},
setGlobalInfo:(newValue) =>{
let keys = Object.keys(newValue)
keys.forEach(key => {
if(globalInfo.hasOwnProperty(key)) {
globalInfo[key] = newValue[key]
}
})
uni.setStorageSync('globalInfo',JSON.stringify(globalInfo))
}
}