ConfigService.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. use wxkxklmyt\Scws;
  14. /**
  15. * 配置管理-服务类
  16. * @author laravel开发员
  17. * @since 2020/11/11
  18. * Class ConfigService
  19. * @package App\Services
  20. */
  21. class ConfigService extends BaseService
  22. {
  23. // 静态对象
  24. protected static $instance = null;
  25. /**
  26. * 构造函数
  27. * @author laravel开发员
  28. * @since 2020/11/11
  29. * ConfigService constructor.
  30. */
  31. public function __construct()
  32. {
  33. $this->model = new ConfigModel();
  34. }
  35. /**
  36. * 静态入口
  37. * @return ConfigService|null
  38. */
  39. public static function make()
  40. {
  41. if (!self::$instance) {
  42. self::$instance = new ConfigService();
  43. }
  44. return self::$instance;
  45. }
  46. /**
  47. * 获取配置列表
  48. * @return array
  49. * @since 2020/11/11
  50. * @author laravel开发员
  51. */
  52. public function getList()
  53. {
  54. $param = request()->all();
  55. // 查询条件
  56. $map = [];
  57. // 配置分组ID
  58. $configgroupId = getter($param, "configgroupId", 0);
  59. if ($configgroupId) {
  60. $map[] = ['config_group_id', '=', $configgroupId];
  61. }
  62. // 配置标题
  63. $title = getter($param, "title");
  64. if ($title) {
  65. $map[] = ['name', 'title', "%{$title}%"];
  66. }
  67. $list = $this->model->getList($map, [['sort', 'asc']]);
  68. return message("操作成功", true, $list);
  69. }
  70. /**
  71. * 获取分组配置
  72. * @param $groupId
  73. * @return mixed
  74. */
  75. public function getConfigByGroup($groupId)
  76. {
  77. $cacheKey = "caches:config:groups:{$groupId}";
  78. $datas = RedisService::get($cacheKey);
  79. if ($datas) {
  80. return $datas;
  81. }
  82. $datas = $this->model::where(['config_group_id' => $groupId, 'status' => 1])
  83. ->select('title', 'code', 'value')
  84. ->orderBy('sort', 'asc')
  85. ->get()
  86. ->keyBy('code');
  87. $datas = $datas ? $datas->toArray() : [];
  88. if ($datas) {
  89. RedisService::set($cacheKey, $datas, 30);
  90. }
  91. return $datas;
  92. }
  93. /**
  94. * 获取配置选项值
  95. * @param $groupId
  96. * @return array|mixed
  97. */
  98. public function getConfigOptionByGroup($groupId)
  99. {
  100. $cacheKey = "caches:config:groups:value_{$groupId}";
  101. $datas = RedisService::get($cacheKey);
  102. if ($datas) {
  103. return $datas;
  104. }
  105. $datas = $this->model::where(['config_group_id' => $groupId, 'status' => 1])
  106. ->select('title', 'code', 'type', 'value')
  107. ->orderBy('sort', 'asc')
  108. ->get()
  109. ->keyBy('code');
  110. $datas = $datas ? $datas->toArray() : [];
  111. $result = [];
  112. if ($datas) {
  113. foreach ($datas as $k => $v) {
  114. $result[$k] = $v['type'] == 'image' && $v['value'] ? get_image_url($v['value']) : $v['value'];
  115. }
  116. RedisService::set($cacheKey, $result, 30);
  117. }
  118. return $result;
  119. }
  120. /**
  121. * 获取配置选项值
  122. * @param $groupId
  123. * @return array|mixed
  124. */
  125. public function getConfigOptionTextByGroup($groupId)
  126. {
  127. $cacheKey = "caches:config:groups:text_{$groupId}";
  128. $datas = RedisService::get($cacheKey);
  129. if ($datas) {
  130. return $datas;
  131. }
  132. $datas = $this->model::where(['config_group_id' => $groupId, 'status' => 1])
  133. ->select('title', 'code', 'options')
  134. ->orderBy('sort', 'asc')
  135. ->get()
  136. ->keyBy('code');
  137. $datas = $datas ? $datas->toArray() : [];
  138. $result = [];
  139. if ($datas) {
  140. foreach ($datas as $k => $v) {
  141. $result[$k] = str_replace("\n", '<br/>', $v['options']);
  142. }
  143. RedisService::set($cacheKey, $result, 30);
  144. }
  145. return $result;
  146. }
  147. /**
  148. * 获取单个配置
  149. * @param $groupId
  150. * @return mixed
  151. */
  152. public function getConfigByCode($code, $default = 0)
  153. {
  154. $cacheKey = "caches:config:code:{$code}";
  155. $datas = RedisService::get($cacheKey);
  156. if ($datas) {
  157. return $datas ? $datas : $default;
  158. }
  159. $datas = $this->model::where(['code' => $code, 'status' => 1])
  160. ->orderBy('sort', 'asc')
  161. ->value('value');
  162. if ($datas) {
  163. RedisService::set($cacheKey, $datas, 30);
  164. }
  165. return $datas ? $datas : $default;
  166. }
  167. /**
  168. * 获取客服回复内容
  169. * @param $keyword
  170. * @return array|mixed
  171. */
  172. public function getContentByKw($keyword)
  173. {
  174. $cacheKey = "caches:config:info:kw_" . md5($keyword);
  175. $data = RedisService::get($cacheKey);
  176. if ($data) {
  177. return $data;
  178. }
  179. $scws = new Scws();
  180. $kws = $scws->scws($keyword, 3, false);
  181. $data = $this->model->where(['config_group_id' => 9, 'status' => 1, 'mark' => 1])->where(function ($query) use ($kws, $keyword) {
  182. if (count($kws) > 1) {
  183. foreach ($kws as $kw) {
  184. $kw = trim($kw);
  185. $query->where('title', 'like', "%{$kw}%")
  186. ->orWhere('options', 'like', "%{$kw}%");
  187. }
  188. } else {
  189. $query->where('title', 'like', "%{$keyword}%")
  190. ->orWhere('options', 'like', "%{$keyword}%");
  191. }
  192. })->whereNotNull('value')->orderByRaw("RAND()")->value('value');
  193. if($data){
  194. RedisService::set($cacheKey, $data, rand(3,5));
  195. }
  196. return $data;
  197. }
  198. }