ConfigService.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. use App\Models\ConfigModel;
  13. /**
  14. * 配置管理-服务类
  15. * @author laravel开发员
  16. * @since 2020/11/11
  17. * Class ConfigService
  18. * @package App\Services
  19. */
  20. class ConfigService extends BaseService
  21. {
  22. public static $instance = null;
  23. /**
  24. * 构造函数
  25. * @author laravel开发员
  26. * @since 2020/11/11
  27. * ConfigService constructor.
  28. */
  29. public function __construct()
  30. {
  31. $this->model = new ConfigModel();
  32. }
  33. /**
  34. * 静态入口
  35. * @return ConfigService|null
  36. */
  37. public static function make(){
  38. if(!self::$instance){
  39. self::$instance = new static();
  40. }
  41. return self::$instance;
  42. }
  43. /**
  44. * 获取配置列表
  45. * @return array
  46. * @since 2020/11/11
  47. * @author laravel开发员
  48. */
  49. public function getList()
  50. {
  51. $param = request()->all();
  52. // 查询条件
  53. $map = [];
  54. // 配置分组ID
  55. $configgroupId = getter($param, "configgroupId", 0);
  56. if ($configgroupId) {
  57. $map[] = ['config_group_id', '=', $configgroupId];
  58. }
  59. // 配置标题
  60. $title = getter($param, "title");
  61. if ($title) {
  62. $map[] = ['name', 'title', "%{$title}%"];
  63. }
  64. $list = $this->model->getList($map, [['sort', 'asc']]);
  65. return message("操作成功", true, $list);
  66. }
  67. /**
  68. * 获取分组配置
  69. * @param $groupId
  70. * @return mixed
  71. */
  72. public function getConfigByGroup($groupId){
  73. $cacheKey = "caches:config:groups:{$groupId}";
  74. $datas = RedisService::get($cacheKey);
  75. if($datas){
  76. return $datas;
  77. }
  78. $datas = $this->model::where(['config_group_id'=> $groupId, 'status'=> 1])
  79. ->select('title','code','value')
  80. ->orderBy('sort','asc')
  81. ->get()
  82. ->keyBy('code');
  83. $datas = $datas? $datas->toArray() : [];
  84. if($datas){
  85. RedisService::set($cacheKey, $datas, 30);
  86. }
  87. return $datas;
  88. }
  89. /**
  90. * 获取分组配置
  91. * @param $groupId
  92. * @return mixed
  93. */
  94. public function getConfigByGroupValue($groupId){
  95. $cacheKey = "caches:config:groups1:{$groupId}";
  96. $datas = RedisService::get($cacheKey);
  97. if($datas){
  98. return $datas;
  99. }
  100. $datas = $this->model::where(['config_group_id'=> $groupId, 'status'=> 1])
  101. ->select('title','code','value')
  102. ->orderBy('sort','asc')
  103. ->get()
  104. ->keyBy('code');
  105. $datas = $datas? $datas->toArray() : [];
  106. if($datas){
  107. $config = [];
  108. foreach ($datas as $v){
  109. $config[$v['code']] = $v['value'];
  110. }
  111. $datas = $config;
  112. RedisService::set($cacheKey, $datas, 30);
  113. }
  114. return $datas;
  115. }
  116. /**
  117. * 获取单个配置
  118. * @param $groupId
  119. * @return mixed
  120. */
  121. public function getConfigByCode($code){
  122. $cacheKey = "caches:config:code:{$code}";
  123. $datas = RedisService::get($cacheKey);
  124. if($datas){
  125. return $datas;
  126. }
  127. $datas = $this->model::where(['code'=> $code, 'status'=> 1])
  128. ->orderBy('sort','asc')
  129. ->value('value');
  130. if($datas){
  131. RedisService::set($cacheKey, $datas, 30);
  132. }
  133. return $datas;
  134. }
  135. }