ConfigService.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. protected static $instance = null;
  23. /**
  24. * 构造函数
  25. * @author wesmiler
  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 ConfigService();
  40. }
  41. return self::$instance;
  42. }
  43. /**
  44. * 获取配置列表
  45. * @return array
  46. * @since 2020/11/11
  47. * @author wesmiler
  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 getConfigByCode($code){
  95. $cacheKey = "caches:config:code:{$code}";
  96. $datas = RedisService::get($cacheKey);
  97. if($datas){
  98. return $datas;
  99. }
  100. $datas = $this->model::where(['code'=> $code, 'status'=> 1])
  101. ->orderBy('sort','asc')
  102. ->value('value');
  103. if($datas){
  104. RedisService::set($cacheKey, $datas, 30);
  105. }
  106. return $datas;
  107. }
  108. }