JhHardwareWRS/utils/storage.js
2026-06-24 15:19:14 +08:00

54 lines
1.1 KiB
JavaScript

const mockData = require('./mockData')
const STORE_KEY = 'jh_wrs_store_v1'
const clone = value => JSON.parse(JSON.stringify(value))
const baseStore = () => ({
currentUserPhone: '',
people: clone(mockData.people),
products: clone(mockData.products),
reports: [],
sessions: [],
})
const getStore = () => {
const store = wx.getStorageSync(STORE_KEY)
if (store && store.people && store.products) {
return store
}
const nextStore = baseStore()
wx.setStorageSync(STORE_KEY, nextStore)
return nextStore
}
const saveStore = store => {
wx.setStorageSync(STORE_KEY, store)
return store
}
const resetStore = () => saveStore(baseStore())
const getCurrentUser = () => {
const store = getStore()
return store.people.find(person => person.phone === store.currentUserPhone) || null
}
const setCurrentUser = phone => {
const store = getStore()
const user = store.people.find(person => person.phone === phone)
store.currentUserPhone = user ? phone : ''
saveStore(store)
return user || null
}
module.exports = {
clone,
getCurrentUser,
getStore,
resetStore,
saveStore,
setCurrentUser,
}