SystemConfig.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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. * @return \think\Response
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\DbException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. */
  41. public function index()
  42. {
  43. $where = $this->request->getMore([
  44. ['tab_id', 0],
  45. ['status', -1]
  46. ]);
  47. if (!$where['tab_id']) {
  48. return app('json')->fail(100100);
  49. }
  50. if ($where['status'] == -1) {
  51. unset($where['status']);
  52. }
  53. return app('json')->success($this->services->getConfigList($where));
  54. }
  55. /**
  56. * 显示创建资源表单页.
  57. * @return \think\Response
  58. * @throws \FormBuilder\Exception\FormBuilderException
  59. * @throws \think\db\exception\DataNotFoundException
  60. * @throws \think\db\exception\DbException
  61. * @throws \think\db\exception\ModelNotFoundException
  62. */
  63. public function create()
  64. {
  65. [$type, $tabId] = $this->request->getMore([
  66. [['type', 'd'], ''],
  67. [['tab_id', 'd'], 1]
  68. ], true);
  69. return app('json')->success($this->services->createFormRule($type, $tabId));
  70. }
  71. /**
  72. * 保存新建的资源
  73. * @return \think\Response
  74. */
  75. public function save()
  76. {
  77. $data = $this->request->postMore([
  78. ['menu_name', ''],
  79. ['type', ''],
  80. ['input_type', 'input'],
  81. ['config_tab_id', 0],
  82. ['parameter', ''],
  83. ['upload_type', 1],
  84. ['required', ''],
  85. ['width', 0],
  86. ['high', 0],
  87. ['value', ''],
  88. ['info', ''],
  89. ['desc', ''],
  90. ['sort', 0],
  91. ['status', 0]
  92. ]);
  93. if (!$data['info']) return app('json')->fail(400274);
  94. if (!$data['menu_name']) return app('json')->fail(400275);
  95. if (!$data['desc']) return app('json')->fail(400276);
  96. if ($data['sort'] < 0) {
  97. $data['sort'] = 0;
  98. }
  99. if ($data['type'] == 'text') {
  100. if (!$data['width']) return app('json')->fail(400277);
  101. if ($data['width'] <= 0) return app('json')->fail(400278);
  102. }
  103. if ($data['type'] == 'textarea') {
  104. if (!$data['width']) return app('json')->fail(400279);
  105. if (!$data['high']) return app('json')->fail(400280);
  106. if ($data['width'] < 0) return app('json')->fail(400281);
  107. if ($data['high'] < 0) return app('json')->fail(400282);
  108. }
  109. if ($data['type'] == 'radio' || $data['type'] == 'checkbox') {
  110. if (!$data['parameter']) return app('json')->fail(400283);
  111. $this->services->valiDateRadioAndCheckbox($data);
  112. }
  113. $data['value'] = json_encode($data['value']);
  114. $config = $this->services->getOne(['menu_name' => $data['menu_name']]);
  115. if ($config) {
  116. $this->services->update($config['id'], $data, 'id');
  117. } else {
  118. $this->services->save($data);
  119. }
  120. $this->services->cacheDriver()->clear();
  121. return app('json')->success(400284);
  122. }
  123. /**
  124. * 显示指定的资源
  125. *
  126. * @param int $id
  127. * @return \think\Response
  128. */
  129. public function read($id)
  130. {
  131. if (!$id) {
  132. return app('json')->fail(100100);
  133. }
  134. $info = $this->services->getReadList((int)$id);
  135. return app('json')->success(compact('info'));
  136. }
  137. /**
  138. * 显示编辑资源表单页.
  139. *
  140. * @param int $id
  141. * @return \think\Response
  142. */
  143. public function edit($id)
  144. {
  145. return app('json')->success($this->services->editConfigForm((int)$id));
  146. }
  147. /**
  148. * 保存更新的资源
  149. *
  150. * @param int $id
  151. * @return \think\Response
  152. */
  153. public function update($id)
  154. {
  155. $type = request()->post('type');
  156. if ($type == 'text' || $type == 'textarea' || $type == 'radio' || ($type == 'upload' && (request()->post('upload_type') == 1 || request()->post('upload_type') == 3))) {
  157. $value = request()->post('value');
  158. } else {
  159. $value = request()->post('value/a');
  160. }
  161. if (!$value) $value = request()->post(request()->post('menu_name'));
  162. $data = $this->request->postMore([
  163. ['menu_name', ''],
  164. ['type', ''],
  165. ['input_type', 'input'],
  166. ['config_tab_id', 0],
  167. ['parameter', ''],
  168. ['upload_type', 1],
  169. ['required', ''],
  170. ['width', 0],
  171. ['high', 0],
  172. ['value', $value],
  173. ['info', ''],
  174. ['desc', ''],
  175. ['sort', 0],
  176. ['status', 0]
  177. ]);
  178. if (!$this->services->get($id)) {
  179. return app('json')->fail(100026);
  180. }
  181. $data['value'] = json_encode($data['value']);
  182. $this->services->update($id, $data);
  183. $this->services->cacheDriver()->clear();
  184. return app('json')->success(100001);
  185. }
  186. /**
  187. * 删除指定资源
  188. * @param int $id
  189. * @return \think\Response
  190. */
  191. public function delete($id)
  192. {
  193. if (!$this->services->delete($id))
  194. return app('json')->fail(100008);
  195. else {
  196. $this->services->cacheDriver()->clear();
  197. return app('json')->success(100002);
  198. }
  199. }
  200. /**
  201. * 修改状态
  202. * @param $id
  203. * @param $status
  204. * @return mixed
  205. */
  206. public function set_status($id, $status)
  207. {
  208. if ($status == '' || $id == 0) {
  209. return app('json')->fail(100100);
  210. }
  211. $this->services->update($id, ['status' => $status]);
  212. $this->services->cacheDriver()->clear();
  213. return app('json')->success(100014);
  214. }
  215. /**
  216. * 基础配置
  217. * */
  218. public function edit_basics(Request $request)
  219. {
  220. $tabId = $this->request->param('tab_id', 1);
  221. if (!$tabId) {
  222. return app('json')->fail(100100);
  223. }
  224. $url = $request->baseUrl();
  225. return app('json')->success($this->services->getConfigForm($url, $tabId));
  226. }
  227. /**
  228. * 保存数据 true
  229. * */
  230. public function save_basics(Request $request)
  231. {
  232. $post = $this->request->post();
  233. foreach ($post as $k => $v) {
  234. if (is_array($v)) {
  235. $res = $this->services->getUploadTypeList($k);
  236. foreach ($res as $kk => $vv) {
  237. if ($kk == 'upload') {
  238. if ($vv == 1 || $vv == 3) {
  239. $post[$k] = $v[0];
  240. }
  241. }
  242. }
  243. }
  244. }
  245. $this->validate($post, \app\adminapi\validate\setting\SystemConfigValidata::class);
  246. if (isset($post['upload_type'])) {
  247. $this->services->checkThumbParam($post);
  248. }
  249. if (isset($post['extract_type']) && !count($post['extract_type'])) {
  250. return app('json')->fail(400753);
  251. }
  252. if (isset($post['store_brokerage_binding_status'])) {
  253. $this->services->checkBrokerageBinding($post);
  254. }
  255. if (isset($post['store_brokerage_ratio']) && isset($post['store_brokerage_two'])) {
  256. $num = $post['store_brokerage_ratio'] + $post['store_brokerage_two'];
  257. if ($num > 100) {
  258. return app('json')->fail(400285);
  259. }
  260. }
  261. if (isset($post['spread_banner'])) {
  262. $num = count($post['spread_banner']);
  263. if ($num > 5) {
  264. return app('json')->fail(400286);
  265. }
  266. }
  267. if (isset($post['user_extract_min_price'])) {
  268. if (!preg_match('/[0-9]$/', $post['user_extract_min_price'])) {
  269. return app('json')->fail(400287);
  270. }
  271. }
  272. if (isset($post['wss_open'])) {
  273. $this->services->saveSslFilePath((int)$post['wss_open'], $post['wss_local_pk'] ?? '', $post['wss_local_cert'] ?? '');
  274. }
  275. if (isset($post['store_brokerage_price']) && $post['store_brokerage_statu'] == 3) {
  276. if ($post['store_brokerage_price'] === '') {
  277. return app('json')->fail(400288);
  278. }
  279. if ($post['store_brokerage_price'] < 0) {
  280. return app('json')->fail(400289);
  281. }
  282. }
  283. if (isset($post['store_brokerage_binding_time']) && $post['store_brokerage_binding_status'] == 2) {
  284. if (!preg_match("/^[0-9][0-9]*$/", $post['store_brokerage_binding_time'])) {
  285. return app('json')->fail(400290);
  286. }
  287. }
  288. if (isset($post['uni_brokerage_price']) && $post['uni_brokerage_price'] < 0) {
  289. return app('json')->fail(400756);
  290. }
  291. if (isset($post['day_brokerage_price_upper']) && $post['day_brokerage_price_upper'] < -1) {
  292. return app('json')->fail(400757);
  293. }
  294. if (isset($post['pay_new_weixin_open']) && (bool)$post['pay_new_weixin_open']) {
  295. if (empty($post['pay_new_weixin_mchid'])) {
  296. return app('json')->fail(400763);
  297. }
  298. }
  299. if (isset($post['uni_brokerage_price']) && preg_match('/\.[0-9]{2,}[1-9][0-9]*$/', (string)$post['uni_brokerage_price']) > 0) {
  300. return app('json')->fail(500029);
  301. }
  302. if (isset($post['weixin_ckeck_file'])) {
  303. $from = public_path() . $post['weixin_ckeck_file'];
  304. $to = public_path() . array_reverse(explode('/', $post['weixin_ckeck_file']))[0];
  305. @copy($from, $to);
  306. }
  307. if (isset($post['ico_path'])) {
  308. $from = public_path() . $post['ico_path'];
  309. $toAdmin = public_path('admin') . 'favicon.ico';
  310. $toHome = public_path('home') . 'favicon.ico';
  311. $toPublic = public_path() . 'favicon.ico';
  312. @copy($from, $toAdmin);
  313. @copy($from, $toHome);
  314. @copy($from, $toPublic);
  315. }
  316. if(isset($post['reward_integral']) || isset($post['reward_money'])) {
  317. if($post['reward_integral'] < 0 || $post['reward_money'] < 0) return app('json')->fail(400558);
  318. }
  319. foreach ($post as $k => $v) {
  320. $config_one = $this->services->getOne(['menu_name' => $k]);
  321. if ($config_one) {
  322. $config_one['value'] = $v;
  323. $this->services->valiDateValue($config_one);
  324. $this->services->update($k, ['value' => json_encode($v)], 'menu_name');
  325. }
  326. }
  327. $this->services->cacheDriver()->clear();
  328. return app('json')->success(100001);
  329. }
  330. /**
  331. * 获取系统设置头部分类
  332. * @param SystemConfigTabServices $services
  333. * @return mixed
  334. * @throws \think\db\exception\DataNotFoundException
  335. * @throws \think\db\exception\DbException
  336. * @throws \think\db\exception\ModelNotFoundException
  337. */
  338. public function header_basics(SystemConfigTabServices $services)
  339. {
  340. [$type, $pid] = $this->request->getMore([
  341. [['type', 'd'], 0],
  342. [['pid', 'd'], 0]
  343. ], true);
  344. if ($type == 3) {//其它分类
  345. $config_tab = [];
  346. } else {
  347. $config_tab = $services->getConfigTab($pid);
  348. }
  349. return app('json')->success(compact('config_tab'));
  350. }
  351. /**
  352. * 获取单个配置的值
  353. * @param $name
  354. * @return mixed
  355. */
  356. public function get_system($name)
  357. {
  358. $value = sys_config($name);
  359. return app('json')->success(compact('value'));
  360. }
  361. /**
  362. * 获取某个分类下的所有配置
  363. * @param $tabId
  364. * @return mixed
  365. */
  366. public function get_config_list($tabId)
  367. {
  368. $list = $this->services->getConfigTabAllList($tabId);
  369. $data = [];
  370. foreach ($list as $item) {
  371. $data[$item['menu_name']] = json_decode($item['value']);
  372. }
  373. return app('json')->success($data);
  374. }
  375. /**
  376. * 获取版本号信息
  377. * @return mixed
  378. */
  379. public function getVersion()
  380. {
  381. $version = get_crmeb_version();
  382. return app('json')->success([
  383. 'version' => $version,
  384. 'label' => 19
  385. ]);
  386. }
  387. }