Files
JayuCloud/WeChatProgram/admin/utils/auth.js
T

148 lines
5.2 KiB
JavaScript

const { STORAGE_KEYS, getStorage, removeStorage, setStorage } = require('./storage')
/**
* Purpose: getAccessToken supports auth.js by holding or executing the get access token page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function getAccessToken() {
return getStorage(STORAGE_KEYS.ACCESS_TOKEN, '')
}
/**
* Purpose: getRefreshToken supports auth.js by holding or executing the get refresh token page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function getRefreshToken() {
return getStorage(STORAGE_KEYS.REFRESH_TOKEN, '')
}
/**
* Purpose: getStoredUserInfo supports auth.js by holding or executing the get stored user info page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function getStoredUserInfo() {
return getStorage(STORAGE_KEYS.USER_INFO, null)
}
/**
* Purpose: getStoredPermissions supports auth.js by holding or executing the get stored permissions page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function getStoredPermissions() {
return getStorage(STORAGE_KEYS.PERMISSIONS, [])
}
/**
* Purpose: getCurrentShop supports auth.js by holding or executing the get current shop page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function getCurrentShop() {
return getStorage(STORAGE_KEYS.CURRENT_SHOP, null)
}
/**
* Purpose: isLoggedIn supports auth.js by holding or executing the is logged in page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function isLoggedIn() {
return Boolean(getAccessToken())
}
/**
* Purpose: setSession supports auth.js by holding or executing the set session page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
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)
}
}
}
/**
* Purpose: updateStoredUserInfo supports auth.js by holding or executing the update stored user info page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function updateStoredUserInfo(userInfo) {
if (!userInfo) {
removeStorage(STORAGE_KEYS.USER_INFO)
return
}
setStorage(STORAGE_KEYS.USER_INFO, userInfo)
}
/**
* Purpose: setPermissions supports auth.js by holding or executing the set permissions page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function setPermissions(permissions) {
setStorage(STORAGE_KEYS.PERMISSIONS, Array.isArray(permissions) ? permissions : [])
}
/**
* Purpose: clearPermissions supports auth.js by holding or executing the clear permissions page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function clearPermissions() {
removeStorage(STORAGE_KEYS.PERMISSIONS)
}
/**
* Purpose: setCurrentShop supports auth.js by holding or executing the set current shop page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function setCurrentShop(shop) {
if (!shop || !shop.id) {
removeStorage(STORAGE_KEYS.CURRENT_SHOP)
return
}
setStorage(STORAGE_KEYS.CURRENT_SHOP, {
id: shop.id,
shopName: shop.shopName || ''
})
}
/**
* Purpose: clearSession supports auth.js by holding or executing the clear session page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
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
}