功能:收口小程序前后台安全与防重复提交
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
const { STORAGE_KEYS, getStorage, removeStorage, setStorage } = require('./storage')
|
||||
|
||||
function getAccessToken() {
|
||||
return getStorage(STORAGE_KEYS.ACCESS_TOKEN, '')
|
||||
}
|
||||
|
||||
function getRefreshToken() {
|
||||
return getStorage(STORAGE_KEYS.REFRESH_TOKEN, '')
|
||||
}
|
||||
|
||||
function getStoredUserInfo() {
|
||||
return getStorage(STORAGE_KEYS.USER_INFO, null)
|
||||
}
|
||||
|
||||
function getStoredPermissions() {
|
||||
return getStorage(STORAGE_KEYS.PERMISSIONS, [])
|
||||
}
|
||||
|
||||
function getCurrentShop() {
|
||||
return getStorage(STORAGE_KEYS.CURRENT_SHOP, null)
|
||||
}
|
||||
|
||||
function isLoggedIn() {
|
||||
return Boolean(getAccessToken())
|
||||
}
|
||||
|
||||
function setSession(payload) {
|
||||
if (!payload || !payload.accessToken || !payload.refreshToken) {
|
||||
return
|
||||
}
|
||||
|
||||
setStorage(STORAGE_KEYS.ACCESS_TOKEN, payload.accessToken)
|
||||
setStorage(STORAGE_KEYS.REFRESH_TOKEN, payload.refreshToken)
|
||||
|
||||
if (payload.userInfo) {
|
||||
setStorage(STORAGE_KEYS.USER_INFO, payload.userInfo)
|
||||
|
||||
if (payload.userInfo.shopId) {
|
||||
setCurrentShop({
|
||||
id: payload.userInfo.shopId,
|
||||
shopName: payload.userInfo.shopName || ''
|
||||
})
|
||||
} else {
|
||||
removeStorage(STORAGE_KEYS.CURRENT_SHOP)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function updateStoredUserInfo(userInfo) {
|
||||
if (!userInfo) {
|
||||
removeStorage(STORAGE_KEYS.USER_INFO)
|
||||
return
|
||||
}
|
||||
|
||||
setStorage(STORAGE_KEYS.USER_INFO, userInfo)
|
||||
}
|
||||
|
||||
function setPermissions(permissions) {
|
||||
setStorage(STORAGE_KEYS.PERMISSIONS, Array.isArray(permissions) ? permissions : [])
|
||||
}
|
||||
|
||||
function clearPermissions() {
|
||||
removeStorage(STORAGE_KEYS.PERMISSIONS)
|
||||
}
|
||||
|
||||
function setCurrentShop(shop) {
|
||||
if (!shop || !shop.id) {
|
||||
removeStorage(STORAGE_KEYS.CURRENT_SHOP)
|
||||
return
|
||||
}
|
||||
|
||||
setStorage(STORAGE_KEYS.CURRENT_SHOP, {
|
||||
id: shop.id,
|
||||
shopName: shop.shopName || ''
|
||||
})
|
||||
}
|
||||
|
||||
function clearSession() {
|
||||
removeStorage(STORAGE_KEYS.ACCESS_TOKEN)
|
||||
removeStorage(STORAGE_KEYS.REFRESH_TOKEN)
|
||||
removeStorage(STORAGE_KEYS.USER_INFO)
|
||||
removeStorage(STORAGE_KEYS.PERMISSIONS)
|
||||
removeStorage(STORAGE_KEYS.CURRENT_SHOP)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getAccessToken,
|
||||
getRefreshToken,
|
||||
getStoredUserInfo,
|
||||
getStoredPermissions,
|
||||
getCurrentShop,
|
||||
isLoggedIn,
|
||||
setSession,
|
||||
updateStoredUserInfo,
|
||||
setPermissions,
|
||||
clearPermissions,
|
||||
setCurrentShop,
|
||||
clearSession
|
||||
}
|
||||
Reference in New Issue
Block a user