登录
This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
<?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) {
|
||||
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('无效的登录方式');
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user