119 lines
3.6 KiB
PHP
119 lines
3.6 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
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* 检查并缓存独立的基础参数
|
||
|
|
*/
|
||
|
|
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 (!$this->checkParamInCache($systemCodeKey, $systemCode)) {
|
||
|
|
// 如果 Redis 没有缓存,执行数据库查询并存入缓存
|
||
|
|
$this->cacheSystemCode($systemCodeKey, $systemCode);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 检查缓存中是否有公司标识参数
|
||
|
|
if (!$this->checkParamInCache($corpAbbrKey, $corpAbbr)) {
|
||
|
|
// 如果 Redis 没有缓存,执行数据库查询并存入缓存
|
||
|
|
$this->cacheCorpAbbr($corpAbbrKey, $corpAbbr);
|
||
|
|
}
|
||
|
|
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 检查参数是否在 Redis 缓存中
|
||
|
|
*/
|
||
|
|
private function checkParamInCache($cacheKey, $param)
|
||
|
|
{
|
||
|
|
return Redis::get($cacheKey) === $param;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 缓存登录类型
|
||
|
|
*/
|
||
|
|
private function cacheLoginType($cacheKey, $loginType)
|
||
|
|
{
|
||
|
|
// 假设我们从数据库或其他来源验证 loginType 的合法性
|
||
|
|
if (!$this->validateLoginType($loginType)) {
|
||
|
|
throw new BusinessException('无效的登录方式');
|
||
|
|
}
|
||
|
|
// 存入缓存,缓存有效期可以根据需求调整
|
||
|
|
Redis::setex($cacheKey, 3600, $loginType); // 1小时有效期
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 缓存系统码
|
||
|
|
*/
|
||
|
|
private function cacheSystemCode($cacheKey, $systemCode)
|
||
|
|
{
|
||
|
|
// 假设我们从数据库或其他来源验证 systemCode 的合法性
|
||
|
|
if (!$this->validateSystemCode($systemCode)) {
|
||
|
|
throw new BusinessException('无效的系统码');
|
||
|
|
}
|
||
|
|
// 存入缓存,缓存有效期可以根据需求调整
|
||
|
|
Redis::setex($cacheKey, 3600, $systemCode); // 1小时有效期
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 缓存公司标识
|
||
|
|
*/
|
||
|
|
private function cacheCorpAbbr($cacheKey, $corpAbbr)
|
||
|
|
{
|
||
|
|
// 假设我们从数据库或其他来源验证 corpAbbr 的合法性
|
||
|
|
if (!$this->validateCorpAbbr($corpAbbr)) {
|
||
|
|
throw new BusinessException('无效的公司标识');
|
||
|
|
}
|
||
|
|
// 存入缓存,缓存有效期可以根据需求调整
|
||
|
|
Redis::setex($cacheKey, 3600, $corpAbbr); // 1小时有效期
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 验证登录方式
|
||
|
|
*/
|
||
|
|
private function validateLoginType($loginType)
|
||
|
|
{
|
||
|
|
// 假设这里是验证登录方式的具体逻辑
|
||
|
|
return in_array($loginType, ['upwd', 'feishu', 'qrcode']);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 验证系统码
|
||
|
|
*/
|
||
|
|
private function validateSystemCode($systemCode)
|
||
|
|
{
|
||
|
|
// 假设这里是验证系统码的具体逻辑
|
||
|
|
$system = Systems::where('code', $systemCode)->first();
|
||
|
|
return $system !== null;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 验证公司标识
|
||
|
|
*/
|
||
|
|
private function validateCorpAbbr($corpAbbr)
|
||
|
|
{
|
||
|
|
// 假设这里是验证公司标识的具体逻辑
|
||
|
|
$company = Corp::where('abbr', $corpAbbr)->first();
|
||
|
|
return $company !== null;
|
||
|
|
}
|
||
|
|
}
|