This commit is contained in:
mzl
2025-07-09 17:58:30 +08:00
parent 4ddf4921b2
commit 396e92ac1b
22 changed files with 1120 additions and 1068 deletions
+91 -59
View File
@@ -9,110 +9,142 @@ use support\exception\BusinessException;
class BasicDataService
{
// 定义Redis键名前缀常量
const LOGIN_TYPE_KEY = 'login_types';
const SYSTEM_CODE_KEY = 'system_codes';
const CORP_ABBR_KEY = 'corp_abbrs';
/**
* 检查并缓存独立的基础参数
*/
public function checkAndCacheBaseParams($loginType, $systemCode, $corpAbbr)
{
// 构造缓存键
$loginTypeKey = 'login_type_' . $loginType;
$systemCodeKey = 'system_code_' . $systemCode;
$corpAbbrKey = 'corp_abbr_' . $corpAbbr;
// 检查缓存中是否有登录类型参数
if (!$this->checkParamInCache($loginTypeKey, $loginType)) {
// 如果 Redis 没有缓存,执行数据库查询并存入缓存
$this->cacheLoginType($loginTypeKey, $loginType);
if (!$loginType && !$systemCode && !$corpAbbr) {
throw new BusinessException(htmlpage());
}
// 检查缓存中是否有系统码参数
if (!$this->checkParamInCache($systemCodeKey, $systemCode)) {
// 如果 Redis 没有缓存,执行数据库查询并存入缓存
$this->cacheSystemCode($systemCodeKey, $systemCode);
// 检查并缓存登录类型
if (!$this->checkParamInCache(self::LOGIN_TYPE_KEY, $loginType)) {
$this->cacheLoginTypes();
if (!$this->checkParamInCache(self::LOGIN_TYPE_KEY, $loginType)) {
throw new BusinessException('无效的登录方式');
}
}
// 检查缓存中是否有公司标识参数
if (!$this->checkParamInCache($corpAbbrKey, $corpAbbr)) {
// 如果 Redis 没有缓存,执行数据库查询并存入缓存
$this->cacheCorpAbbr($corpAbbrKey, $corpAbbr);
// 检查并缓存系统码
if (!$this->checkParamInCache(self::SYSTEM_CODE_KEY, $systemCode)) {
$this->cacheSystemCodes();
if (!$this->checkParamInCache(self::SYSTEM_CODE_KEY, $systemCode)) {
throw new BusinessException('无效的系统码');
}
}
// 检查并缓存公司标识
if (!$this->checkParamInCache(self::CORP_ABBR_KEY, $corpAbbr)) {
$this->cacheCorpAbbrs();
if (!$this->checkParamInCache(self::CORP_ABBR_KEY, $corpAbbr)) {
throw new BusinessException('无效的公司标识');
}
}
return true;
}
/**
* 检查参数是否在 Redis 缓存中
* 检查参数是否在Redis集合中
*/
private function checkParamInCache($cacheKey, $param)
{
return Redis::get($cacheKey) === $param;
return Redis::sIsMember($cacheKey, $param);
}
/**
* 缓存登录类型
* 缓存所有登录类型到集合
*/
private function cacheLoginType($cacheKey, $loginType)
private function cacheLoginTypes()
{
// 假设我们从数据库或其他来源验证 loginType 的合法性
if (!$this->validateLoginType($loginType)) {
throw new BusinessException('无效的登录方式');
$cacheKey = self::LOGIN_TYPE_KEY;
// 如果集合已存在,直接返回
if (Redis::exists($cacheKey)) {
return;
}
// 存入缓存,缓存有效期可以根据需求调整
Redis::setex($cacheKey, 3600, $loginType); // 1小时有效期
}
/**
* 缓存系统码
*/
private function cacheSystemCode($cacheKey, $systemCode)
{
// 假设我们从数据库或其他来源验证 systemCode 的合法性
if (!$this->validateSystemCode($systemCode)) {
throw new BusinessException('无效的系统码');
$loginTypes = $this->getAllLoginTypes();
if (empty($loginTypes)) {
throw new BusinessException('未获取到登录类型数据');
}
// 存入缓存,缓存有效期可以根据需求调整
Redis::setex($cacheKey, 3600, $systemCode); // 1小时有效期
// 批量添加值到集合
Redis::sAdd($cacheKey, ...$loginTypes);
Redis::expire($cacheKey, 3600); // 1小时有效期
}
/**
* 缓存公司标识
* 缓存所有系统码到集合
*/
private function cacheCorpAbbr($cacheKey, $corpAbbr)
private function cacheSystemCodes()
{
// 假设我们从数据库或其他来源验证 corpAbbr 的合法性
if (!$this->validateCorpAbbr($corpAbbr)) {
throw new BusinessException('无效的公司标识');
$cacheKey = self::SYSTEM_CODE_KEY;
// 如果集合已存在,直接返回
if (Redis::exists($cacheKey)) {
return;
}
// 存入缓存,缓存有效期可以根据需求调整
Redis::setex($cacheKey, 3600, $corpAbbr); // 1小时有效期
$systemCodes = $this->getAllSystemCodes();
if (empty($systemCodes)) {
throw new BusinessException('未获取到系统码数据');
}
// 批量添加值到集合
Redis::sAdd($cacheKey, ...$systemCodes);
Redis::expire($cacheKey, 3600); // 1小时有效期
}
/**
* 验证登录方式
* 缓存所有公司标识到集合
*/
private function validateLoginType($loginType)
private function cacheCorpAbbrs()
{
// 假设这里是验证登录方式的具体逻辑
return in_array($loginType, ['upwd', 'feishu', 'qrcode']);
$cacheKey = self::CORP_ABBR_KEY;
// 如果集合已存在,直接返回
if (Redis::exists($cacheKey)) {
return;
}
$corpAbbrs = $this->getAllCorpAbbrs();
if (empty($corpAbbrs)) {
throw new BusinessException('未获取到公司标识数据');
}
// 批量添加值到集合
Redis::sAdd($cacheKey, ...$corpAbbrs);
Redis::expire($cacheKey, 3600); // 1小时有效期
}
/**
* 验证系统码
* 获取所有有效的登录类型
*/
private function validateSystemCode($systemCode)
private function getAllLoginTypes()
{
// 假设这里是验证系统码的具体逻辑
$system = Systems::where('code', $systemCode)->first();
return $system !== null;
return ['upwd', 'feishu', 'qrcode']; // 示例数据
}
/**
* 验证公司标识
* 获取所有有效的系统码
*/
private function validateCorpAbbr($corpAbbr)
private function getAllSystemCodes()
{
// 假设这里是验证公司标识的具体逻辑
$company = Corp::where('abbr', $corpAbbr)->first();
return $company !== null;
return Systems::pluck('code')->toArray();
}
}
/**
* 获取所有有效的公司标识
*/
private function getAllCorpAbbrs()
{
return Corp::pluck('abbr')->toArray();
}
}