ConfigService.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. // 静态对象
  23. protected static $instance = null;
  24. /**
  25. * 构造函数
  26. * @author laravel开发员
  27. * @since 2020/11/11
  28. * ConfigService constructor.
  29. */
  30. public function __construct()
  31. {
  32. $this->model = new ConfigModel();
  33. }
  34. /**
  35. * 静态入口
  36. * @return ConfigService|null
  37. */
  38. public static function make(){
  39. if(!self::$instance){
  40. self::$instance = new ConfigService();
  41. }
  42. return self::$instance;
  43. }
  44. /**
  45. * 获取配置列表
  46. * @return array
  47. * @since 2020/11/11
  48. * @author laravel开发员
  49. */
  50. public function getList()
  51. {
  52. $param = request()->all();
  53. // 查询条件
  54. $map = [];
  55. // 配置分组ID
  56. $configgroupId = getter($param, "configgroupId", 0);
  57. if ($configgroupId) {
  58. $map[] = ['config_group_id', '=', $configgroupId];
  59. }
  60. // 配置标题
  61. $title = getter($param, "title");
  62. if ($title) {
  63. $map[] = ['name', 'title', "%{$title}%"];
  64. }
  65. $list = $this->model->getList($map, [['sort', 'asc']]);
  66. return message("操作成功", true, $list);
  67. }
  68. /**
  69. * 获取分组配置
  70. * @param $groupId
  71. * @return mixed
  72. */
  73. public function getConfigByGroup($groupId){
  74. $cacheKey = "caches:config:groups:{$groupId}";
  75. $datas = RedisService::get($cacheKey);
  76. if($datas){
  77. return $datas;
  78. }
  79. $datas = $this->model::where(['config_group_id'=> $groupId, 'status'=> 1])
  80. ->select('title','code','value')
  81. ->orderBy('sort','asc')
  82. ->get()
  83. ->keyBy('code');
  84. $datas = $datas? $datas->toArray() : [];
  85. if($datas){
  86. RedisService::set($cacheKey, $datas, 30);
  87. }
  88. return $datas;
  89. }
  90. /**
  91. * 获取配置选项值
  92. * @param $groupId
  93. * @return array|mixed
  94. */
  95. public function getConfigOptionByGroup($groupId){
  96. $cacheKey = "caches:config:groups:value_{$groupId}";
  97. $datas = RedisService::get($cacheKey);
  98. if($datas){
  99. return $datas;
  100. }
  101. $datas = $this->model::where(['config_group_id'=> $groupId, 'status'=> 1])
  102. ->select('title','code','type','value')
  103. ->orderBy('sort','asc')
  104. ->get()
  105. ->keyBy('code');
  106. $datas = $datas? $datas->toArray() : [];
  107. $result = [];
  108. if($datas){
  109. foreach ($datas as $k => $v){
  110. $result[$k] = $v['type'] == 'image' && $v['value']? get_image_url($v['value']) : $v['value'];
  111. }
  112. RedisService::set($cacheKey, $result, 30);
  113. }
  114. return $result;
  115. }
  116. /**
  117. * 获取配置选项值
  118. * @param $groupId
  119. * @return array|mixed
  120. */
  121. public function getConfigOptionTextByGroup($groupId){
  122. $cacheKey = "caches:config:groups:text_{$groupId}";
  123. $datas = RedisService::get($cacheKey);
  124. if($datas){
  125. return $datas;
  126. }
  127. $datas = $this->model::where(['config_group_id'=> $groupId, 'status'=> 1])
  128. ->select('title','code','options')
  129. ->orderBy('sort','asc')
  130. ->get()
  131. ->keyBy('code');
  132. $datas = $datas? $datas->toArray() : [];
  133. $result = [];
  134. if($datas){
  135. foreach ($datas as $k => $v){
  136. $result[$k] = str_replace("\n",'<br/>', $v['options']);
  137. }
  138. RedisService::set($cacheKey, $result, 30);
  139. }
  140. return $result;
  141. }
  142. /**
  143. * 获取单个配置
  144. * @param $code
  145. * @param string $default 默认值
  146. * @return array|mixed|string
  147. */
  148. public function getConfigByCode($code, $default=''){
  149. $cacheKey = "caches:config:code:{$code}";
  150. $datas = RedisService::get($cacheKey);
  151. if($datas){
  152. return $datas;
  153. }
  154. $datas = $this->model::where(['code'=> $code, 'status'=> 1])
  155. ->orderBy('sort','asc')
  156. ->value('value');
  157. if($datas){
  158. RedisService::set($cacheKey, $datas, 30);
  159. }
  160. return $datas? $datas : $default;
  161. }
  162. }