Files
fdesk/app/common/service/BasicDataService.php
T
2025-07-21 15:09:06 +08:00

150 lines
4.0 KiB
PHP

<?php
namespace app\common\service;
use app\common\model\Corp;
use app\common\model\Systems;
use support\Redis;
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)
{
if (!$loginType && !$systemCode && !$corpAbbr) {
throw new BusinessException('header缺少必要参数');
}
// 检查并缓存登录类型
if (!$this->checkParamInCache(self::LOGIN_TYPE_KEY, $loginType)) {
$this->cacheLoginTypes();
if (!$this->checkParamInCache(self::LOGIN_TYPE_KEY, $loginType)) {
throw new BusinessException('无效的登录方式');
}
}
// 检查并缓存系统码
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集合中
*/
private function checkParamInCache($cacheKey, $param)
{
return Redis::sIsMember($cacheKey, $param);
}
/**
* 缓存所有登录类型到集合
*/
private function cacheLoginTypes()
{
$cacheKey = self::LOGIN_TYPE_KEY;
// 如果集合已存在,直接返回
if (Redis::exists($cacheKey)) {
return;
}
$loginTypes = $this->getAllLoginTypes();
if (empty($loginTypes)) {
throw new BusinessException('未获取到登录类型数据');
}
// 批量添加值到集合
Redis::sAdd($cacheKey, ...$loginTypes);
Redis::expire($cacheKey, 3600); // 1小时有效期
}
/**
* 缓存所有系统码到集合
*/
private function cacheSystemCodes()
{
$cacheKey = self::SYSTEM_CODE_KEY;
// 如果集合已存在,直接返回
if (Redis::exists($cacheKey)) {
return;
}
$systemCodes = $this->getAllSystemCodes();
if (empty($systemCodes)) {
throw new BusinessException('未获取到系统码数据');
}
// 批量添加值到集合
Redis::sAdd($cacheKey, ...$systemCodes);
Redis::expire($cacheKey, 3600); // 1小时有效期
}
/**
* 缓存所有公司标识到集合
*/
private function cacheCorpAbbrs()
{
$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 getAllLoginTypes()
{
return ['upwd', 'feishu', 'qrcode']; // 示例数据
}
/**
* 获取所有有效的系统码
*/
private function getAllSystemCodes()
{
return Systems::pluck('code')->toArray();
}
/**
* 获取所有有效的公司标识
*/
private function getAllCorpAbbrs()
{
return Corp::pluck('abbr')->toArray();
}
}