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()); } } }