ConfigService.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Laravel框架 [ Laravel ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 南京Laravel研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: wesmiler <12345678@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. use App\Models\ConfigModel;
  13. /**
  14. * 配置管理-服务类
  15. * @author wesmiler
  16. * @since 2020/11/11
  17. * Class ConfigService
  18. * @package App\Services
  19. */
  20. class ConfigService extends BaseService
  21. {
  22. /**
  23. * 构造函数
  24. * @author wesmiler
  25. * @since 2020/11/11
  26. * ConfigService constructor.
  27. */
  28. public function __construct()
  29. {
  30. $this->model = new ConfigModel();
  31. }
  32. /**
  33. * 获取配置列表
  34. * @return array
  35. * @since 2020/11/11
  36. * @author wesmiler
  37. */
  38. public function getList()
  39. {
  40. $param = request()->all();
  41. // 查询条件
  42. $map = [];
  43. // 配置分组ID
  44. $configgroupId = getter($param, "configgroupId", 0);
  45. if ($configgroupId) {
  46. $map[] = ['config_group_id', '=', $configgroupId];
  47. }
  48. // 配置标题
  49. $title = getter($param, "title");
  50. if ($title) {
  51. $map[] = ['name', 'title', "%{$title}%"];
  52. }
  53. $list = $this->model->getList($map, [['sort', 'asc']]);
  54. return message("操作成功", true, $list);
  55. }
  56. /**
  57. * 获取分组配置
  58. * @param $groupId
  59. * @return mixed
  60. */
  61. public function getConfigByGroup($groupId){
  62. $cacheKey = "caches:config:groups:{$groupId}";
  63. $datas = RedisService::get($cacheKey);
  64. if($datas){
  65. //return $datas;
  66. }
  67. $datas = $this->model::where(['config_group_id'=> $groupId, 'status'=> 1])
  68. ->select('title','code','value')
  69. ->orderBy('sort','asc')
  70. ->get()
  71. ->keyBy('code');
  72. $datas = $datas? $datas->toArray() : [];
  73. if($datas){
  74. RedisService::set($cacheKey, $datas, 30);
  75. }
  76. return $datas;
  77. }
  78. /**
  79. * 获取单个配置
  80. * @param $groupId
  81. * @return mixed
  82. */
  83. public function getConfigByCode($code){
  84. $cacheKey = "caches:config:code:{$code}";
  85. $datas = RedisService::get($cacheKey);
  86. if($datas){
  87. return $datas;
  88. }
  89. $datas = $this->model::where(['code'=> $code, 'status'=> 1])
  90. ->orderBy('sort','asc')
  91. ->value('value');
  92. if($datas){
  93. RedisService::set($cacheKey, $datas, 30);
  94. }
  95. return $datas;
  96. }
  97. }