SystemConfig.php 16 KB

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