You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
155 lines
4.4 KiB
155 lines
4.4 KiB
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | Niucloud-admin 企业快速开发的多应用管理平台
|
|
// +----------------------------------------------------------------------
|
|
// | 官方网址:https://www.niucloud.com
|
|
// +----------------------------------------------------------------------
|
|
// | niucloud团队 版权所有 开源版本可自由商用
|
|
// +----------------------------------------------------------------------
|
|
// | Author: Niucloud Team
|
|
// +----------------------------------------------------------------------
|
|
|
|
namespace app\api\controller\common;
|
|
|
|
use app\service\api\common\DictService;
|
|
use core\base\BaseController;
|
|
|
|
/**
|
|
* 字典批量获取控制器
|
|
* Class Dict
|
|
* @package app\api\controller\common
|
|
*/
|
|
class Dict extends BaseController
|
|
{
|
|
/**
|
|
* 批量获取字典数据
|
|
* @return \think\Response
|
|
*/
|
|
public function getBatchDict()
|
|
{
|
|
$keys = input('keys', []);
|
|
|
|
// 支持字符串格式(逗号分隔)和数组格式
|
|
if (is_string($keys)) {
|
|
$keys = array_filter(explode(',', $keys));
|
|
}
|
|
|
|
if (!is_array($keys) || empty($keys)) {
|
|
return fail('参数错误:keys必须是非空数组或逗号分隔的字符串');
|
|
}
|
|
|
|
// 限制一次最多获取20个字典,防止性能问题
|
|
if (count($keys) > 20) {
|
|
return fail('一次最多获取20个字典');
|
|
}
|
|
|
|
try {
|
|
$dictService = new DictService();
|
|
$result = $dictService->getBatchDict($keys);
|
|
|
|
return success($result);
|
|
} catch (\Exception $e) {
|
|
return fail('获取字典数据失败:' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 根据业务场景获取字典数据
|
|
* @return \think\Response
|
|
*/
|
|
public function getDictByScene()
|
|
{
|
|
$scene = input('scene', '');
|
|
|
|
if (empty($scene)) {
|
|
return fail('参数错误:scene不能为空');
|
|
}
|
|
|
|
try {
|
|
$dictService = new DictService();
|
|
|
|
// 获取场景对应的字典keys
|
|
$keys = $dictService->getDictKeysByScene($scene);
|
|
|
|
if (empty($keys)) {
|
|
return fail('不支持的业务场景:' . $scene);
|
|
}
|
|
|
|
// 批量获取字典数据
|
|
$result = $dictService->getBatchDict($keys);
|
|
|
|
return success([
|
|
'scene' => $scene,
|
|
'data' => $result
|
|
]);
|
|
} catch (\Exception $e) {
|
|
return fail('获取字典数据失败:' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取单个字典数据
|
|
* @return \think\Response
|
|
*/
|
|
public function getDict()
|
|
{
|
|
$key = input('key', '');
|
|
$useCache = input('use_cache', 1);
|
|
|
|
if (empty($key)) {
|
|
return fail('参数错误:key不能为空');
|
|
}
|
|
|
|
try {
|
|
$dictService = new DictService();
|
|
$result = $dictService->getDict($key, (bool)$useCache);
|
|
|
|
return success($result);
|
|
} catch (\Exception $e) {
|
|
return fail('获取字典数据失败:' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 清除字典缓存
|
|
* @return \think\Response
|
|
*/
|
|
public function clearDictCache()
|
|
{
|
|
$keys = input('keys', null);
|
|
|
|
// 支持字符串格式(逗号分隔)和数组格式
|
|
if (is_string($keys)) {
|
|
$keys = array_filter(explode(',', $keys));
|
|
}
|
|
|
|
try {
|
|
$dictService = new DictService();
|
|
$result = $dictService->clearDictCache($keys);
|
|
|
|
if ($result) {
|
|
return success('缓存清除成功');
|
|
} else {
|
|
return fail('缓存清除失败');
|
|
}
|
|
} catch (\Exception $e) {
|
|
return fail('缓存清除失败:' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取常用字典映射关系
|
|
* @return \think\Response
|
|
*/
|
|
public function getDictMapping()
|
|
{
|
|
try {
|
|
$dictService = new DictService();
|
|
$mapping = $dictService->getCommonDictMapping();
|
|
|
|
return success($mapping);
|
|
} catch (\Exception $e) {
|
|
return fail('获取字典映射失败:' . $e->getMessage());
|
|
}
|
|
}
|
|
}
|