SystemConfig.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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\adminapi\controller\v1\setting;
  12. use app\adminapi\controller\AuthController;
  13. use app\Request;
  14. use app\services\system\config\SystemConfigServices;
  15. use app\services\system\config\SystemConfigTabServices;
  16. use think\facade\App;
  17. /**
  18. * 系统配置
  19. * Class SystemConfig
  20. * @package app\adminapi\controller\v1\setting
  21. */
  22. class SystemConfig extends AuthController
  23. {
  24. /**
  25. * SystemConfig constructor.
  26. * @param App $app
  27. * @param SystemConfigServices $services
  28. */
  29. public function __construct(App $app, SystemConfigServices $services)
  30. {
  31. parent::__construct($app);
  32. $this->services = $services;
  33. }
  34. /**
  35. * 显示资源列表
  36. *
  37. * @return \think\Response
  38. */
  39. public function index()
  40. {
  41. $where = $this->request->getMore([
  42. ['tab_id', 0],
  43. ['status', -1]
  44. ]);
  45. if (!$where['tab_id']) {
  46. return app('json')->fail('参数错误');
  47. }
  48. if ($where['status'] == -1) {
  49. unset($where['status']);
  50. }
  51. return app('json')->success($this->services->getConfigList($where));
  52. }
  53. /**
  54. * 显示创建资源表单页.
  55. * @param $type
  56. * @return \think\Response
  57. */
  58. public function create()
  59. {
  60. [$type, $tabId] = $this->request->getMore([
  61. [['type', 'd'], ''],
  62. [['tab_id', 'd'], 1]
  63. ], true);
  64. return app('json')->success($this->services->createFormRule($type, $tabId));
  65. }
  66. /**
  67. * 保存新建的资源
  68. *
  69. * @return \think\Response
  70. */
  71. public function save()
  72. {
  73. $data = $this->request->postMore([
  74. ['menu_name', ''],
  75. ['type', ''],
  76. ['input_type', 'input'],
  77. ['config_tab_id', 0],
  78. ['parameter', ''],
  79. ['upload_type', 1],
  80. ['required', ''],
  81. ['width', 0],
  82. ['high', 0],
  83. ['value', ''],
  84. ['info', ''],
  85. ['desc', ''],
  86. ['sort', 0],
  87. ['status', 0]
  88. ]);
  89. if (!$data['info']) return app('json')->fail('请输入配置名称');
  90. if (!$data['menu_name']) return app('json')->fail('请输入字段名称');
  91. if (!$data['desc']) return app('json')->fail('请输入配置简介');
  92. if ($data['sort'] < 0) {
  93. $data['sort'] = 0;
  94. }
  95. if ($data['type'] == 'text') {
  96. if (!$data['width']) return app('json')->fail('请输入文本框的宽度');
  97. if ($data['width'] <= 0) return app('json')->fail('请输入正确的文本框的宽度');
  98. }
  99. if ($data['type'] == 'textarea') {
  100. if (!$data['width']) return app('json')->fail('请输入多行文本框的宽度');
  101. if (!$data['high']) return app('json')->fail('请输入多行文本框的高度');
  102. if ($data['width'] < 0) return app('json')->fail('请输入正确的多行文本框的宽度');
  103. if ($data['high'] < 0) return app('json')->fail('请输入正确的多行文本框的宽度');
  104. }
  105. if ($data['type'] == 'radio' || $data['type'] == 'checkbox') {
  106. if (!$data['parameter']) return app('json')->fail('请输入配置参数');
  107. $this->services->valiDateRadioAndCheckbox($data);
  108. }
  109. $data['value'] = json_encode($data['value']);
  110. $config = $this->services->getOne(['menu_name' => $data['menu_name']]);
  111. if ($config) {
  112. $this->services->update($config['id'], $data, 'id');
  113. } else {
  114. $this->services->save($data);
  115. }
  116. \crmeb\services\CacheService::clear();
  117. return app('json')->success('添加配置成功!');
  118. }
  119. /**
  120. * 显示指定的资源
  121. *
  122. * @param int $id
  123. * @return \think\Response
  124. */
  125. public function read($id)
  126. {
  127. if (!$id) {
  128. return app('json')->fail('参数错误,请重新打开');
  129. }
  130. $info = $this->services->getReadList((int)$id);
  131. return app('json')->success(compact('info'));
  132. }
  133. /**
  134. * 显示编辑资源表单页.
  135. *
  136. * @param int $id
  137. * @return \think\Response
  138. */
  139. public function edit($id)
  140. {
  141. return app('json')->success($this->services->editConfigForm((int)$id));
  142. }
  143. /**
  144. * 保存更新的资源
  145. *
  146. * @param int $id
  147. * @return \think\Response
  148. */
  149. public function update($id)
  150. {
  151. $type = request()->post('type');
  152. if ($type == 'text' || $type == 'textarea' || $type == 'radio' || ($type == 'upload' && (request()->post('upload_type') == 1 || request()->post('upload_type') == 3))) {
  153. $value = request()->post('value');
  154. } else {
  155. $value = request()->post('value/a');
  156. }
  157. if (!$value) $value = request()->post(request()->post('menu_name'));
  158. $data = $this->request->postMore([
  159. ['menu_name', ''],
  160. ['type', ''],
  161. ['input_type', 'input'],
  162. ['config_tab_id', 0],
  163. ['parameter', ''],
  164. ['upload_type', 1],
  165. ['required', ''],
  166. ['width', 0],
  167. ['high', 0],
  168. ['value', $value],
  169. ['info', ''],
  170. ['desc', ''],
  171. ['sort', 0],
  172. ['status', 0]
  173. ]);
  174. if (!$this->services->get($id)) {
  175. return app('json')->fail('编辑的记录不存在!');
  176. }
  177. $data['value'] = json_encode($data['value']);
  178. $this->services->update($id, $data);
  179. \crmeb\services\CacheService::clear();
  180. return app('json')->success('修改成功!');
  181. }
  182. /**
  183. * 删除指定资源
  184. *
  185. * @param int $id
  186. * @return \think\Response
  187. */
  188. public function delete($id)
  189. {
  190. if (!$this->services->delete($id))
  191. return app('json')->fail('删除失败,请稍候再试!');
  192. else {
  193. \crmeb\services\CacheService::clear();
  194. return app('json')->success('删除成功!');
  195. }
  196. }
  197. /**
  198. * 修改状态
  199. * @param $id
  200. * @param $status
  201. * @return mixed
  202. */
  203. public function set_status($id, $status)
  204. {
  205. if ($status == '' || $id == 0) {
  206. return app('json')->fail('参数错误');
  207. }
  208. $this->services->update($id, ['status' => $status]);
  209. \crmeb\services\CacheService::clear();
  210. return app('json')->success($status == 0 ? '隐藏成功' : '显示成功');
  211. }
  212. /**
  213. * 基础配置
  214. * */
  215. public function edit_basics(Request $request)
  216. {
  217. $tabId = $this->request->param('tab_id', 1);
  218. if (!$tabId) {
  219. return app('json')->fail('参数错误');
  220. }
  221. $url = $request->baseUrl();
  222. return app('json')->success($this->services->getConfigForm($url, $tabId));
  223. }
  224. /**
  225. * 保存数据 true
  226. * */
  227. public function save_basics(Request $request)
  228. {
  229. $post = $this->request->post();
  230. foreach ($post as $k => $v) {
  231. if (is_array($v)) {
  232. $res = $this->services->getUploadTypeList($k);
  233. foreach ($res as $kk => $vv) {
  234. if ($kk == 'upload') {
  235. if ($vv == 1 || $vv == 3) {
  236. $post[$k] = $v[0];
  237. }
  238. }
  239. }
  240. }
  241. }
  242. $this->validate($post, \app\adminapi\validate\setting\SystemConfigValidata::class);
  243. if (isset($post['upload_type'])) {
  244. $this->services->checkThumbParam($post);
  245. }
  246. if (isset($post['store_brokerage_binding_status'])) {
  247. $this->services->checkBrokerageBinding($post);
  248. }
  249. if (isset($post['store_brokerage_ratio']) && isset($post['store_brokerage_two'])) {
  250. $num = $post['store_brokerage_ratio'] + $post['store_brokerage_two'];
  251. if ($num > 100) {
  252. return app('json')->fail('一二级返佣比例不能大于100%');
  253. }
  254. }
  255. if (isset($post['spread_banner'])) {
  256. $num = count($post['spread_banner']);
  257. if ($num > 5) {
  258. return app('json')->fail('分销海报不能多于5张');
  259. }
  260. }
  261. if (isset($post['user_extract_min_price'])) {
  262. if (!preg_match('/[0-9]$/', $post['user_extract_min_price'])) {
  263. return app('json')->fail('提现最低金额只能为数字!');
  264. }
  265. }
  266. foreach ($post as $k => $v) {
  267. $config_one = $this->services->getOne(['menu_name' => $k]);
  268. if ($config_one) {
  269. $config_one['value'] = $v;
  270. $this->services->valiDateValue($config_one);
  271. $this->services->update($k, ['value' => json_encode($v)], 'menu_name');
  272. }
  273. }
  274. if (isset($post['wss_open'])) {
  275. $this->services->saveSslFilePath((int)$post['wss_open'], $post['wss_local_pk'] ?? '', $post['wss_local_cert'] ?? '');
  276. }
  277. if (isset($post['store_brokerage_price']) && $post['store_brokerage_statu'] == 3) {
  278. if ($post['store_brokerage_price'] === '') {
  279. return app('json')->fail('满额分销最低金额不能为空!');
  280. }
  281. if ($post['store_brokerage_price'] < 0) {
  282. return app('json')->fail('满额分销最低金额不能小于0!');
  283. }
  284. }
  285. if (isset($post['store_brokerage_binding_time']) && $post['store_brokerage_binding_status'] == 2) {
  286. if (!preg_match("/^[0-9][0-9]*$/", $post['store_brokerage_binding_time'])) {
  287. return app('json')->fail('绑定有效期请填写正整数!');
  288. }
  289. }
  290. if (isset($post['weixin_ckeck_file'])) {
  291. $from = public_path() . $post['weixin_ckeck_file'];
  292. $to = public_path() . array_reverse(explode('/', $post['weixin_ckeck_file']))[0];
  293. @copy($from, $to);
  294. }
  295. if (isset($post['ico_path'])) {
  296. $from = public_path() . $post['ico_path'];
  297. $toAdmin = public_path('admin') . 'favicon.ico';
  298. $toHome = public_path('home') . 'favicon.ico';
  299. $toPublic = public_path() . 'favicon.ico';
  300. @copy($from, $toAdmin);
  301. @copy($from, $toHome);
  302. @copy($from, $toPublic);
  303. }
  304. \crmeb\services\CacheService::clear();
  305. return app('json')->success('修改成功');
  306. }
  307. /**
  308. * 获取系统设置头部分类
  309. * @param SystemConfigTabServices $services
  310. * @return mixed
  311. * @throws \think\db\exception\DataNotFoundException
  312. * @throws \think\db\exception\DbException
  313. * @throws \think\db\exception\ModelNotFoundException
  314. */
  315. public function header_basics(SystemConfigTabServices $services)
  316. {
  317. [$type, $pid] = $this->request->getMore([
  318. [['type', 'd'], 0],
  319. [['pid', 'd'], 0]
  320. ], true);
  321. if ($type == 3) {//其它分类
  322. $config_tab = [];
  323. } else {
  324. $config_tab = $services->getConfigTab($pid);
  325. }
  326. return app('json')->success(compact('config_tab'));
  327. }
  328. /**
  329. * 获取单个配置的值
  330. * @param $name
  331. * @return mixed
  332. */
  333. public function get_system($name)
  334. {
  335. $value = sys_config($name);
  336. return app('json')->success(compact('value'));
  337. }
  338. /**
  339. * 获取某个分类下的所有配置
  340. * @param $tabId
  341. * @return mixed
  342. */
  343. public function get_config_list($tabId)
  344. {
  345. $list = $this->services->getConfigTabAllList($tabId);
  346. $data = [];
  347. foreach ($list as $item) {
  348. $data[$item['menu_name']] = json_decode($item['value']);
  349. }
  350. return app('json')->success($data);
  351. }
  352. }