SystemConfig.php 14 KB

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