SystemConfig.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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\controller\admin\system\config;
  12. use app\common\AdminBaseController;
  13. use app\Request;
  14. use app\services\system\config\SystemConfigServices;
  15. use app\services\system\config\SystemConfigTabServices;
  16. use app\validate\admin\SystemConfigValidate;
  17. use think\db\exception\DataNotFoundException;
  18. use think\db\exception\DbException;
  19. use think\db\exception\ModelNotFoundException;
  20. use think\facade\App;
  21. use think\Response;
  22. /**
  23. * 系统配置
  24. * Class SystemConfig
  25. * @package app\controller\admin\v1\setting
  26. */
  27. class SystemConfig extends AdminBaseController
  28. {
  29. /**
  30. * SystemConfig constructor.
  31. * @param Request $request
  32. * @param SystemConfigServices $service
  33. */
  34. public function __construct(Request $request, SystemConfigServices $service)
  35. {
  36. parent::__construct($request);
  37. $this->service = $service;
  38. $this->searchable = [
  39. ['tab_id', 0],
  40. ['status', -1]
  41. ];
  42. $this->createParams = [
  43. ['menu_name', ''],
  44. ['type', ''],
  45. ['input_type', ''],
  46. ['config_tab_id', []],
  47. ['parameter', ''],
  48. ['value', ''],
  49. ['info', ''],
  50. ['desc', ''],
  51. ['sort', 0],
  52. ['status', 0],
  53. ['configuration', []],
  54. ['is_store', 0],
  55. ];
  56. }
  57. /**
  58. * 显示资源列表
  59. *
  60. * @return Response
  61. * @throws DataNotFoundException
  62. * @throws DbException
  63. * @throws ModelNotFoundException
  64. */
  65. public function index()
  66. {
  67. $where = $this->request->getMore($this->searchable);
  68. if (!$where['tab_id']) {
  69. return $this->error('参数错误');
  70. }
  71. if ($where['status'] == -1) {
  72. unset($where['status']);
  73. }
  74. $where['is_store'] = 0;
  75. return $this->success($this->service->getConfigList($where));
  76. }
  77. /**
  78. * 保存新建的资源
  79. *
  80. * @return Response
  81. * @throws DataNotFoundException
  82. * @throws DbException
  83. * @throws ModelNotFoundException
  84. */
  85. public function save()
  86. {
  87. $data = $this->request->postMore($this->createParams);
  88. if ($data['config_tab_id']) $data['config_tab_id'] = end($data['config_tab_id']);
  89. if (!$data['info']) return $this->error('请输入配置名称');
  90. if (!$data['menu_name']) return $this->error('请输入字段名称');
  91. if ($this->service->be(['menu_name' => $data['menu_name']])) return $this->error('字段名已存在');
  92. if (!$data['desc']) return $this->error('请输入配置简介');
  93. if ($data['sort'] < 0) {
  94. $data['sort'] = 0;
  95. }
  96. $data['value'] = json_encode($data['value']);
  97. // $config = $this->service->getOne(['menu_name' => $data['menu_name']]);
  98. // if ($config) {
  99. // $this->service->update($config['id'], $data, 'id');
  100. // } else {
  101. $this->service->create($data);
  102. // }
  103. event('config.create', [$data]);
  104. return $this->success('添加配置成功!');
  105. }
  106. /**
  107. * 保存更新的资源
  108. *
  109. * @param int $id
  110. * @return Response
  111. * @throws DataNotFoundException
  112. * @throws DbException
  113. * @throws ModelNotFoundException
  114. */
  115. public function update($id)
  116. {
  117. $type = request()->post('type');
  118. $configuration = request()->post('configuration/a');
  119. if ($type == 'Input' || $type == 'RadioGroup' || $type == 'Select' || $type == 'Datepicker' || ($type == 'Upload' && (($configuration['upload_type'] ?? 1) == 1 || ($configuration['upload_type'] ?? 1) == 3))) {
  120. $value = request()->post('value');
  121. } else {
  122. $value = request()->post('value/a');
  123. }
  124. if (!$value) $value = request()->post(request()->post('menu_name'));
  125. $data = $this->request->postMore(array_merge($this->createParams, [['value', $value]]));
  126. $data['config_tab_id'] = end($data['config_tab_id']);
  127. if (!$this->service->get($id)) {
  128. return $this->error('编辑的记录不存在!');
  129. }
  130. $data['value'] = json_encode($data['value']);
  131. $this->service->update($id, $data);
  132. event('config.update');
  133. return $this->success('修改成功!');
  134. }
  135. /**
  136. * 删除指定资源
  137. *
  138. * @param int $id
  139. * @return Response
  140. */
  141. public function delete($id)
  142. {
  143. if (!$this->service->delete($id))
  144. return $this->error('删除失败,请稍候再试!');
  145. else {
  146. event('config.delete', [$id]);
  147. return $this->success('删除成功!');
  148. }
  149. }
  150. /**
  151. * 修改状态
  152. * @param $id
  153. * @param $status
  154. * @return mixed
  155. */
  156. public function setStatus($id, $status)
  157. {
  158. if ($status == '' || $id == 0) {
  159. return $this->error('参数错误');
  160. }
  161. $this->service->update($id, ['status' => $status]);
  162. event('config.update');
  163. return $this->success($status == 0 ? '隐藏成功' : '显示成功');
  164. }
  165. /**
  166. * 显示指定的资源
  167. *
  168. * @param int $id
  169. * @return Response
  170. */
  171. public function readConfig($id)
  172. {
  173. if (!$id) {
  174. return $this->error('参数错误,请重新打开');
  175. }
  176. $info = $this->service->getReadList((int)$id);
  177. return $this->success(compact('info'));
  178. }
  179. /**
  180. * 保存数据 true
  181. * */
  182. public function saveConfig(Request $request)
  183. {
  184. $post = $this->request->post();
  185. foreach ($post as $k => $v) {
  186. if (is_array($v)) {
  187. $res = $this->service->getUploadTypeList($k);
  188. foreach ($res as $kk => $vv) {
  189. if ($kk == 'Upload') {
  190. if (($vv['upload_type'] ?? 1) == 1 || ($vv['upload_type'] ?? 1) == 3) {
  191. $post[$k] = $v[0];
  192. }
  193. }
  194. }
  195. }
  196. }
  197. $this->validate($post, SystemConfigValidate::class);
  198. if (isset($post['upload_type'])) {
  199. $this->service->checkThumbParam($post);
  200. }
  201. if (isset($post['spread_banner'])) {
  202. $num = count($post['spread_banner']);
  203. if ($num > 5) {
  204. return $this->error('分销海报不能多于5张');
  205. }
  206. }
  207. if (isset($post['user_extract_min_price'])) {
  208. if (!preg_match('/[0-9]$/', $post['user_extract_min_price'])) {
  209. return $this->error('提现最低金额只能为数字!');
  210. }
  211. }
  212. if (isset($post['store_extract_max_price']) && isset($post['store_extract_min_price'])) {
  213. if ($post['store_extract_max_price'] < $post['store_extract_min_price']) {
  214. return $this->error('门店提现最低金额不能大于最高金额');
  215. }
  216. }
  217. if (isset($post['default_send_day'])) {
  218. $post['default_send_day_start_time'] = time();
  219. }
  220. foreach ($post as $k => $v) {
  221. $config_one = $this->service->getOne(['menu_name' => $k]);
  222. if ($config_one) {
  223. $config_one['value'] = $v;
  224. $this->service->update($k, ['value' => json_encode($v)], 'menu_name');
  225. }
  226. }
  227. event('config.update');
  228. return $this->success('修改成功');
  229. }
  230. /**
  231. * 获取系统设置头部分类
  232. * @param SystemConfigTabServices $services
  233. * @return mixed
  234. * @throws DataNotFoundException
  235. * @throws DbException
  236. * @throws ModelNotFoundException
  237. */
  238. public function header_basics(SystemConfigTabServices $services)
  239. {
  240. [$pid] = $this->request->getMore([
  241. [['pid', 'd'], 0]
  242. ], true);
  243. $config_tab = $services->getConfigTab($pid);
  244. return $this->success(compact('config_tab'));
  245. }
  246. /**
  247. * 获取单个配置的值
  248. * @param $name
  249. * @return mixed
  250. */
  251. public function get_system($name)
  252. {
  253. $value = sys_config($name);
  254. return $this->success(compact('value'));
  255. }
  256. }