SystemConfig.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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 crmeb\services\CacheService;
  17. use crmeb\services\easywechat\orderShipping\MiniOrderService;
  18. use think\facade\App;
  19. /**
  20. * 系统配置
  21. * Class SystemConfig
  22. * @package app\adminapi\controller\v1\setting
  23. */
  24. class SystemConfig extends AuthController
  25. {
  26. /**
  27. * SystemConfig constructor.
  28. * @param App $app
  29. * @param SystemConfigServices $services
  30. */
  31. public function __construct(App $app, SystemConfigServices $services)
  32. {
  33. parent::__construct($app);
  34. $this->services = $services;
  35. }
  36. /**
  37. * 显示资源列表
  38. * @return \think\Response
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\DbException
  41. * @throws \think\db\exception\ModelNotFoundException
  42. */
  43. public function index()
  44. {
  45. $where = $this->request->getMore([
  46. ['tab_id', 0],
  47. ['config_name', ''],
  48. ['status', -1]
  49. ]);
  50. if (!$where['tab_id'] && $where['config_name'] == '') {
  51. return app('json')->fail(100100);
  52. }
  53. if ($where['status'] == -1) {
  54. unset($where['status']);
  55. }
  56. return app('json')->success($this->services->getConfigList($where));
  57. }
  58. /**
  59. * 显示创建资源表单页.
  60. * @return \think\Response
  61. * @throws \FormBuilder\Exception\FormBuilderException
  62. * @throws \think\db\exception\DataNotFoundException
  63. * @throws \think\db\exception\DbException
  64. * @throws \think\db\exception\ModelNotFoundException
  65. */
  66. public function create()
  67. {
  68. [$type, $tabId] = $this->request->getMore([
  69. [['type', 'd'], ''],
  70. [['tab_id', 'd'], 1]
  71. ], true);
  72. return app('json')->success($this->services->createFormRule($type, $tabId));
  73. }
  74. /**
  75. * 保存新建的资源
  76. * @return \think\Response
  77. */
  78. public function save()
  79. {
  80. $data = $this->request->postMore([
  81. ['menu_name', ''],
  82. ['type', ''],
  83. ['input_type', 'input'],
  84. ['config_tab_id', 0],
  85. ['parameter', ''],
  86. ['upload_type', 1],
  87. ['required', ''],
  88. ['width', 0],
  89. ['high', 0],
  90. ['value', ''],
  91. ['info', ''],
  92. ['desc', ''],
  93. ['sort', 0],
  94. ['level', 0],
  95. ['link_data', []],
  96. ['status', 0]
  97. ]);
  98. if (is_array($data['config_tab_id'])) $data['config_tab_id'] = end($data['config_tab_id']);
  99. if (!$data['info']) return app('json')->fail(400274);
  100. if (!$data['menu_name']) return app('json')->fail(400275);
  101. if (!$data['desc']) return app('json')->fail(400276);
  102. if ($data['sort'] < 0) {
  103. $data['sort'] = 0;
  104. }
  105. if ($data['type'] == 'text') {
  106. if (!$data['width']) return app('json')->fail(400277);
  107. if ($data['width'] <= 0) return app('json')->fail(400278);
  108. }
  109. if ($data['type'] == 'textarea') {
  110. if (!$data['width']) return app('json')->fail(400279);
  111. if (!$data['high']) return app('json')->fail(400280);
  112. if ($data['width'] < 0) return app('json')->fail(400281);
  113. if ($data['high'] < 0) return app('json')->fail(400282);
  114. }
  115. if ($data['type'] == 'radio' || $data['type'] == 'checkbox') {
  116. if (!$data['parameter']) return app('json')->fail(400283);
  117. $this->services->valiDateRadioAndCheckbox($data);
  118. }
  119. if ($data['level'] == 1) {
  120. if (!$data['link_data']) return app('json')->fail('请选择关联顶级选项');
  121. $data['link_id'] = $data['link_data'][0];
  122. $data['link_value'] = $data['link_data'][1];
  123. }
  124. $data['value'] = json_encode($data['value']);
  125. $config = $this->services->getOne(['menu_name' => $data['menu_name']]);
  126. if ($config) {
  127. $this->services->update($config['id'], $data, 'id');
  128. } else {
  129. $this->services->save($data);
  130. }
  131. CacheService::clear();
  132. return app('json')->success(400284);
  133. }
  134. /**
  135. * 显示指定的资源
  136. *
  137. * @param int $id
  138. * @return \think\Response
  139. */
  140. public function read($id)
  141. {
  142. if (!$id) {
  143. return app('json')->fail(100100);
  144. }
  145. $info = $this->services->getReadList((int)$id);
  146. return app('json')->success(compact('info'));
  147. }
  148. /**
  149. * 显示编辑资源表单页.
  150. *
  151. * @param int $id
  152. * @return \think\Response
  153. */
  154. public function edit($id)
  155. {
  156. return app('json')->success($this->services->editConfigForm((int)$id));
  157. }
  158. /**
  159. * 保存更新的资源
  160. *
  161. * @param int $id
  162. * @return \think\Response
  163. */
  164. public function update($id)
  165. {
  166. $type = request()->post('type');
  167. if ($type == 'text' || $type == 'textarea' || $type == 'radio' || ($type == 'upload' && (request()->post('upload_type') == 1 || request()->post('upload_type') == 3))) {
  168. $value = request()->post('value');
  169. } else {
  170. $value = request()->post('value/a');
  171. }
  172. if (!$value) $value = request()->post(request()->post('menu_name'));
  173. $data = $this->request->postMore([
  174. ['menu_name', ''],
  175. ['type', ''],
  176. ['input_type', 'input'],
  177. ['config_tab_id', 0],
  178. ['parameter', ''],
  179. ['upload_type', 1],
  180. ['required', ''],
  181. ['width', 0],
  182. ['high', 0],
  183. ['value', $value],
  184. ['info', ''],
  185. ['desc', ''],
  186. ['sort', 0],
  187. ['level', 0],
  188. ['link_data', []],
  189. ['status', 0]
  190. ]);
  191. if (is_array($data['config_tab_id'])) $data['config_tab_id'] = end($data['config_tab_id']);
  192. if (!$this->services->get($id)) {
  193. return app('json')->fail(100026);
  194. }
  195. if ($data['level'] == 1) {
  196. if (!$data['link_data']) return app('json')->fail('请选择关联顶级选项');
  197. $data['link_id'] = $data['link_data'][0];
  198. $data['link_value'] = $data['link_data'][1];
  199. }
  200. $data['value'] = json_encode($data['value']);
  201. $this->services->update($id, $data);
  202. CacheService::clear();
  203. return app('json')->success(100001);
  204. }
  205. /**
  206. * 删除指定资源
  207. * @param int $id
  208. * @return \think\Response
  209. */
  210. public function delete($id)
  211. {
  212. if (!$this->services->delete($id))
  213. return app('json')->fail(100008);
  214. else {
  215. CacheService::clear();
  216. return app('json')->success(100002);
  217. }
  218. }
  219. /**
  220. * 修改状态
  221. * @param $id
  222. * @param $status
  223. * @return mixed
  224. */
  225. public function set_status($id, $status)
  226. {
  227. if ($status == '' || $id == 0) {
  228. return app('json')->fail(100100);
  229. }
  230. $this->services->update($id, ['status' => $status]);
  231. CacheService::clear();
  232. return app('json')->success(100014);
  233. }
  234. /**
  235. * 基础配置
  236. * */
  237. public function edit_basics(Request $request)
  238. {
  239. $tabId = $this->request->param('tab_id', 1);
  240. if (!$tabId) {
  241. return app('json')->fail(100100);
  242. }
  243. $url = $request->baseUrl();
  244. return app('json')->success($this->services->getConfigForm($url, $tabId));
  245. }
  246. /**
  247. * 保存数据 true
  248. * */
  249. public function save_basics(Request $request)
  250. {
  251. $post = $this->request->post();
  252. foreach ($post as $k => $v) {
  253. if (is_array($v)) {
  254. $res = $this->services->getUploadTypeList($k);
  255. foreach ($res as $kk => $vv) {
  256. if ($kk == 'upload') {
  257. if ($vv == 1 || $vv == 3) {
  258. $post[$k] = $v[0];
  259. }
  260. }
  261. }
  262. }
  263. }
  264. $this->validate($post, \app\adminapi\validate\setting\SystemConfigValidata::class);
  265. if (isset($post['upload_type'])) {
  266. $this->services->checkThumbParam($post);
  267. }
  268. if (isset($post['extract_type']) && !count($post['extract_type'])) {
  269. return app('json')->fail(400753);
  270. }
  271. if (isset($post['store_brokerage_binding_status'])) {
  272. $this->services->checkBrokerageBinding($post);
  273. }
  274. if (isset($post['store_brokerage_ratio']) && isset($post['store_brokerage_two'])) {
  275. $num = $post['store_brokerage_ratio'] + $post['store_brokerage_two'];
  276. if ($num > 100) {
  277. return app('json')->fail(400285);
  278. }
  279. }
  280. if (isset($post['spread_banner'])) {
  281. $num = count($post['spread_banner']);
  282. if ($num > 5) {
  283. return app('json')->fail(400286);
  284. }
  285. }
  286. if (isset($post['user_extract_min_price'])) {
  287. if (!preg_match('/[0-9]$/', $post['user_extract_min_price'])) {
  288. return app('json')->fail(400287);
  289. }
  290. }
  291. if (isset($post['wss_open'])) {
  292. $this->services->saveSslFilePath((int)$post['wss_open'], $post['wss_local_pk'] ?? '', $post['wss_local_cert'] ?? '');
  293. }
  294. if (isset($post['store_brokerage_price']) && $post['store_brokerage_statu'] == 3) {
  295. if ($post['store_brokerage_price'] === '') {
  296. return app('json')->fail(400288);
  297. }
  298. if ($post['store_brokerage_price'] < 0) {
  299. return app('json')->fail(400289);
  300. }
  301. }
  302. if (isset($post['store_brokerage_binding_time']) && $post['store_brokerage_binding_status'] == 2) {
  303. if (!preg_match("/^[0-9][0-9]*$/", $post['store_brokerage_binding_time'])) {
  304. return app('json')->fail(400290);
  305. }
  306. }
  307. if (isset($post['uni_brokerage_price']) && $post['uni_brokerage_price'] < 0) {
  308. return app('json')->fail(400756);
  309. }
  310. if (isset($post['day_brokerage_price_upper']) && $post['day_brokerage_price_upper'] < -1) {
  311. return app('json')->fail(400757);
  312. }
  313. if (isset($post['pay_new_weixin_open']) && (bool)$post['pay_new_weixin_open']) {
  314. if (empty($post['pay_new_weixin_mchid'])) {
  315. return app('json')->fail(400763);
  316. }
  317. }
  318. if (isset($post['uni_brokerage_price']) && preg_match('/\.[0-9]{2,}[1-9][0-9]*$/', (string)$post['uni_brokerage_price']) > 0) {
  319. return app('json')->fail(500029);
  320. }
  321. if (isset($post['weixin_ckeck_file'])) {
  322. $from = public_path() . $post['weixin_ckeck_file'];
  323. $to = public_path() . array_reverse(explode('/', $post['weixin_ckeck_file']))[0];
  324. @copy($from, $to);
  325. }
  326. if (isset($post['ico_path'])) {
  327. $from = public_path() . $post['ico_path'];
  328. $toAdmin = public_path('admin') . 'favicon.ico';
  329. $toHome = public_path('home') . 'favicon.ico';
  330. $toPublic = public_path() . 'favicon.ico';
  331. @copy($from, $toAdmin);
  332. @copy($from, $toHome);
  333. @copy($from, $toPublic);
  334. }
  335. if (isset($post['reward_integral']) || isset($post['reward_money'])) {
  336. if ($post['reward_money'] < 0) return app('json')->fail('赠送余额不能小于0元');
  337. if ($post['reward_integral'] < 0) return app('json')->fail('赠送积分不能小于0');
  338. }
  339. if (isset($post['sign_give_point'])) {
  340. if (!is_int($post['sign_give_point']) || $post['sign_give_point'] < 0) return app('json')->fail('签到赠送积分请填写大于等于0的整数');
  341. }
  342. if (isset($post['sign_give_exp'])) {
  343. if ((int)$post['sign_give_exp'] < 0) return app('json')->fail('签到赠送经验请填写大于等于0的整数');
  344. }
  345. if (isset($post['integral_frozen'])) {
  346. if (!ctype_digit($post['integral_frozen']) || $post['integral_frozen'] < 0) return app('json')->fail('积分冻结天数请填写大于等于0的整数');
  347. }
  348. if (isset($post['store_free_postage'])) {
  349. if (!is_int($post['store_free_postage']) || $post['store_free_postage'] < 0) return app('json')->fail('满额包邮请填写大于等于0的整数');
  350. }
  351. if (isset($post['withdrawal_fee'])) {
  352. if ($post['withdrawal_fee'] < 0 || $post['withdrawal_fee'] > 100) return app('json')->fail('提现手续费范围在0-100之间');
  353. }
  354. if (isset($post['routine_auth_type']) && count($post['routine_auth_type']) == 0) return app('json')->fail('微信和手机号登录开关至少开启一个');
  355. if (isset($post['integral_max_num'])) {
  356. if (!ctype_digit($post['integral_max_num']) || $post['integral_max_num'] < 0) return app('json')->fail('积分抵扣上限请填写大于等于0的整数');
  357. }
  358. if (isset($post['customer_phone'])) {
  359. if (!ctype_digit($post['customer_phone']) || strlen($post['customer_phone']) > 11) return app('json')->fail('客服手机号为11位数字');
  360. }
  361. if (isset($post['refund_time_available'])) {
  362. if (!ctype_digit($post['refund_time_available'])) return app('json')->fail('售后期限必须为大于0的整数');
  363. }
  364. if (isset($post['sms_save_type']) && sys_config('sms_account', '') != '') return app('json')->success(100001);
  365. foreach ($post as $k => $v) {
  366. $config_one = $this->services->getOne(['menu_name' => $k]);
  367. if ($config_one) {
  368. $config_one['value'] = $v;
  369. $this->services->valiDateValue($config_one);
  370. $this->services->update($k, ['value' => json_encode($v)], 'menu_name');
  371. }
  372. }
  373. CacheService::clear();
  374. return app('json')->success(100001);
  375. }
  376. /**
  377. * 获取系统设置头部分类
  378. * @param SystemConfigTabServices $services
  379. * @return mixed
  380. * @throws \think\db\exception\DataNotFoundException
  381. * @throws \think\db\exception\DbException
  382. * @throws \think\db\exception\ModelNotFoundException
  383. */
  384. public function header_basics(SystemConfigTabServices $services)
  385. {
  386. [$type, $pid] = $this->request->getMore([
  387. [['type', 'd'], 0],
  388. [['pid', 'd'], 0]
  389. ], true);
  390. if ($type == 3) {//其它分类
  391. $config_tab = [];
  392. } else {
  393. $config_tab = $services->getConfigTab($pid);
  394. if (empty($config_tab)) $config_tab[] = $services->get($pid, ['id', 'id as value', 'title as label', 'pid', 'icon', 'type']);
  395. }
  396. return app('json')->success(compact('config_tab'));
  397. }
  398. /**
  399. * 获取单个配置的值
  400. * @param $name
  401. * @return mixed
  402. */
  403. public function get_system($name)
  404. {
  405. $value = sys_config($name);
  406. return app('json')->success(compact('value'));
  407. }
  408. /**
  409. * 获取某个分类下的所有配置
  410. * @param $tabId
  411. * @return mixed
  412. */
  413. public function get_config_list($tabId)
  414. {
  415. $list = $this->services->getConfigTabAllList($tabId);
  416. $data = [];
  417. foreach ($list as $item) {
  418. $data[$item['menu_name']] = json_decode($item['value']);
  419. }
  420. return app('json')->success($data);
  421. }
  422. /**
  423. * 获取版本号信息
  424. * @return mixed
  425. */
  426. public function getVersion()
  427. {
  428. $version = get_crmeb_version();
  429. return app('json')->success([
  430. 'version' => $version,
  431. 'label' => 19
  432. ]);
  433. }
  434. }