SystemConfigServices.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\system\config;
  12. use app\model\system\config\SystemConfig;
  13. use qiniu\basic\BaseServices;
  14. use qiniu\exceptions\AdminException;
  15. use qiniu\services\FileService;
  16. use qiniu\services\wechat\contract\ServeConfigInterface;
  17. use think\db\exception\DataNotFoundException;
  18. use think\db\exception\DbException;
  19. use think\db\exception\ModelNotFoundException;
  20. use think\exception\ValidateException;
  21. /**
  22. * 系统配置
  23. * Class SystemConfigServices
  24. * @package app\services\system\config
  25. * @mixin SystemConfig
  26. */
  27. class SystemConfigServices extends BaseServices implements ServeConfigInterface
  28. {
  29. /**
  30. * SystemConfigServices constructor.
  31. * @param SystemConfig $model
  32. */
  33. public function __construct(SystemConfig $model)
  34. {
  35. $this->model = $model;
  36. }
  37. /**
  38. * 获取单个系统配置
  39. * @param string $configName
  40. * @param int $store_id
  41. * @param null $default
  42. * @return mixed|null
  43. */
  44. public function getConfigValue(string $configName, int $store_id = 0, $default = null)
  45. {
  46. if ($store_id) {//查门店、供应商
  47. /** @var SystemStoreConfigServices $service */
  48. $service = app()->make(SystemStoreConfigServices::class);
  49. return $service->getConfig($configName, $store_id);
  50. } else {//平台
  51. $info = $this->search(['menu_name' => $configName])->find();
  52. $value = $info['value'];
  53. $type = $info['type'];
  54. $detail = $info['configuration'];
  55. $value = is_null($value) ? $default : json_decode($value, true);
  56. if ($type == 'Upload' && !empty($value)) {
  57. $value = set_file_url($value);
  58. if ($detail['upload_type'] != 2) {
  59. $value = $value[0] ?? $default;
  60. }
  61. }
  62. return $value;
  63. }
  64. }
  65. /**
  66. * 获取全部配置
  67. * @param array $configName
  68. * @param int $store_id
  69. * @return array
  70. */
  71. public function getConfigAll(array $configName = [], int $store_id = 0)
  72. {
  73. if ($store_id) {
  74. /** @var SystemStoreConfigServices $service */
  75. $service = app()->make(SystemStoreConfigServices::class);
  76. return $service->getConfigAll($configName, $store_id);
  77. } else {
  78. $get_all = function (array $configName = [], int $storeId = 0) {
  79. if ($configName) {
  80. return $this->search(['menu_name' => $configName])->column('value', 'menu_name');
  81. } else {
  82. return $this->getModel()->column('value', 'menu_name');
  83. }
  84. };
  85. return array_map(function ($item) {
  86. return json_decode($item, true);
  87. }, $get_all($configName));
  88. }
  89. }
  90. /**
  91. * 获取配置并分页
  92. * @param array $where
  93. * @return array
  94. * @throws DataNotFoundException
  95. * @throws DbException
  96. * @throws ModelNotFoundException
  97. */
  98. public function getConfigList(array $where)
  99. {
  100. [$page, $limit] = $this->getPageValue();
  101. $list = $this->search($where)->page($page, $limit)->order('sort desc,id asc')->select()->toArray();
  102. $count = $this->getCount($where);
  103. foreach ($list as &$item) {
  104. $item['value'] = $item['value'] ? json_decode($item['value'], true) ?: '' : '';
  105. if ($item['type'] == 'Upload' && !empty($item['value'])) {
  106. $item['value'] = set_file_url($item['value']);
  107. $tidy_srr = [];
  108. foreach ($item['value'] as $key => $value) {
  109. $tidy_srr[$key]['filepath'] = $value;
  110. $tidy_srr[$key]['filename'] = basename($value);
  111. }
  112. $item['value'] = $tidy_srr;
  113. }
  114. }
  115. return compact('count', 'list');
  116. }
  117. /**
  118. * 获取系统配置信息
  119. * @param int $tabId
  120. * @return array
  121. */
  122. public function getReadList(int $tabId)
  123. {
  124. $info = $this->getConfigTabAllList($tabId);
  125. foreach ($info as &$v) {
  126. $v['value'] = $v['value'] ? json_decode($v['value'], true) ?: '' : '';
  127. if ($v['type'] == 'Upload' && !empty($v['value'])) {
  128. $v['value'] = set_file_url($v['value']);
  129. $tidy_srr = [];
  130. foreach ($v['value'] as $key => $value) {
  131. $tidy_srr[$key]['filepath'] = $value;
  132. $tidy_srr[$key]['filename'] = basename($value);
  133. }
  134. $v['value'] = $tidy_srr;
  135. }
  136. }
  137. return $info;
  138. }
  139. public function getConfigTabAllList(int $tabId, int $status = 1, int $type = 1, int $relation_id = 0)
  140. {
  141. $where['tab_id'] = $tabId;
  142. if ($status == 1) $where['status'] = $status;
  143. if ($relation_id != 0) $where['is_store'] = 1;
  144. return $this->search($where)
  145. ->when(isset($relation_id) && $relation_id != 0, function ($query) use ($type, $relation_id) {
  146. $query->with(['storeConfig' => function ($querys) use ($type, $relation_id) {
  147. $querys->where('type', $type)->where('relation_id', $relation_id);
  148. }]);
  149. })
  150. ->order('sort desc')->select()->toArray();
  151. }
  152. /**
  153. * 获取上传配置中的上传类型
  154. * @param string $configName
  155. * @return array
  156. */
  157. public function getUploadTypeList(string $configName)
  158. {
  159. return $this->search(['menu_name' => $configName])->column('configuration', 'type');
  160. }
  161. /**
  162. * 检测缩略图水印配置是否更改
  163. * @param array $post
  164. * @return bool
  165. */
  166. public function checkThumbParam(array $post)
  167. {
  168. unset($post['upload_type'], $post['image_watermark_status']);
  169. /** @var SystemConfigTabServices $systemConfigTabServices */
  170. $systemConfigTabServices = app()->make(SystemConfigTabServices::class);
  171. //上传配置->基础配置
  172. $tab_id = $systemConfigTabServices->getColumn(['eng_title' => 'upload_base'], 'id');
  173. if ($tab_id) {
  174. $all = $this->getColumn(['config_tab_id' => $tab_id], 'value', 'menu_name');
  175. if (array_intersect(array_keys($all), array_keys($post))) {
  176. foreach ($post as $key => $item) {
  177. //配置更改删除原来生成的缩略图
  178. if (isset($all[$key]) && $item != $all[$key]) {
  179. try {
  180. FileService::delDir(public_path('uploads/thumb_water'));
  181. break;
  182. } catch (\Throwable $e) {
  183. }
  184. }
  185. }
  186. }
  187. }
  188. return true;
  189. }
  190. /**
  191. * 获取全部配置
  192. * @param array $configName
  193. * @param int $storeId
  194. * @param int $type 0 正常结构 1:只返回key value
  195. * @return array
  196. */
  197. public function getConfigAllField(array $configName = [], int $storeId = 0, int $type = 0)
  198. {
  199. $list = $this->withSearchSelect(['menu_name', 'store_id'], ['menu_name' => $configName, 'store_id' => $storeId])->column(implode(',', ['info', 'type', 'value', 'desc', 'parameter']), 'menu_name');
  200. foreach ($list as &$item) {
  201. $item['value'] = json_decode($item['value'], true);
  202. }
  203. $value = [];
  204. foreach ($configName as $key) {
  205. if ($type) {
  206. $value[$key] = $list[$key]['value'] ?? '';
  207. } else {
  208. $value[$key] = $list[$key] ?? ['info' => '', 'type' => 'text', 'value' => '', 'desc' => '', 'parameter' => ''];
  209. }
  210. }
  211. return $value;
  212. }
  213. /**
  214. * 获取配置
  215. * @param string $key
  216. * @param null $default
  217. * @return mixed
  218. */
  219. public function getConfig(string $key, $default = null)
  220. {
  221. return sys_config($key, $default);
  222. }
  223. }