SystemConfig.php 20 KB

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