119 lines
3.1 KiB
PHP
119 lines
3.1 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 = 'bsh_info_login_types';
|
||
|
|
const SYSTEM_CODE_KEY = 'bsh_info_system_codes';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 检查并缓存独立的基础参数
|
||
|
|
*/
|
||
|
|
public function checkAndCacheBaseParams($loginType, $systemCode)
|
||
|
|
{
|
||
|
|
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('无效的登录方式');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 检查并缓存系统码
|
||
|
|
if (!$this->checkParamInCache(self::SYSTEM_CODE_KEY, $systemCode)) {
|
||
|
|
$this->cacheSystemCodes();
|
||
|
|
if (!$this->checkParamInCache(self::SYSTEM_CODE_KEY, $systemCode)) {
|
||
|
|
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 getAllLoginTypes()
|
||
|
|
{
|
||
|
|
return ['upwd', 'feishu', 'wechat', 'qrcode']; // 示例数据
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 获取所有有效的系统码
|
||
|
|
*/
|
||
|
|
private function getAllSystemCodes()
|
||
|
|
{
|
||
|
|
return Systems::pluck('code')->toArray();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 获取所有有效的公司标识
|
||
|
|
*/
|
||
|
|
private function getAllCorpAbbrs()
|
||
|
|
{
|
||
|
|
return Corp::pluck('abbr')->toArray();
|
||
|
|
}
|
||
|
|
}
|