43 lines
968 B
TypeScript
43 lines
968 B
TypeScript
import appInfo from "../appInfo"
|
|
import pbClient from "../pbClient"
|
|
|
|
const tokenCache = {} as Record<string, string>
|
|
|
|
/**
|
|
* 更新租户的token
|
|
* @param {string} id 记录id
|
|
* @param {string} appName 租户名称
|
|
* @param {string} value 新的token
|
|
*/
|
|
const update = async (id: string, appName: string, value: string) => {
|
|
try {
|
|
await pbClient
|
|
.collection("app_info")
|
|
.update(id, { tenant_access_token: value })
|
|
} catch {
|
|
/* empty */
|
|
}
|
|
|
|
tokenCache[appName] = value
|
|
console.log(`reset ${appName} access token success`, value)
|
|
}
|
|
|
|
/**
|
|
* 获取租户的token
|
|
* @param {string} appName 租户名称
|
|
* @returns {string} 租户的token
|
|
*/
|
|
const get = async (appName: string) => {
|
|
if (tokenCache[appName]) return tokenCache[appName]
|
|
const config = await appInfo.getVal(appName, "tenant_access_token")
|
|
tokenCache[appName] = config
|
|
return config
|
|
}
|
|
|
|
const tenantAccessToken = {
|
|
get,
|
|
update,
|
|
}
|
|
|
|
export default tenantAccessToken
|