SystemConfig.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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\order\StoreOrderServices;
  15. use app\services\system\config\SystemConfigServices;
  16. use app\services\system\config\SystemConfigTabServices;
  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. *
  38. * @return \think\Response
  39. */
  40. public function index()
  41. {
  42. $where = $this->request->getMore([
  43. ['tab_id', 0],
  44. ['status', -1]
  45. ]);
  46. if (!$where['tab_id']) {
  47. return app('json')->fail(100100);
  48. }
  49. if ($where['status'] == -1) {
  50. unset($where['status']);
  51. }
  52. return app('json')->success($this->services->getConfigList($where));
  53. }
  54. /**
  55. * 显示创建资源表单页.
  56. * @param $type
  57. * @return \think\Response
  58. */
  59. public function create()
  60. {
  61. [$type, $tabId] = $this->request->getMore([
  62. [['type', 'd'], ''],
  63. [['tab_id', 'd'], 1]
  64. ], true);
  65. return app('json')->success($this->services->createFormRule($type, $tabId));
  66. }
  67. /**
  68. * 保存新建的资源
  69. *
  70. * @return \think\Response
  71. */
  72. public function save()
  73. {
  74. $data = $this->request->postMore([
  75. ['menu_name', ''],
  76. ['type', ''],
  77. ['input_type', 'input'],
  78. ['config_tab_id', 0],
  79. ['parameter', ''],
  80. ['upload_type', 1],
  81. ['required', ''],
  82. ['width', 0],
  83. ['high', 0],
  84. ['value', ''],
  85. ['info', ''],
  86. ['desc', ''],
  87. ['sort', 0],
  88. ['status', 0]
  89. ]);
  90. if (!$data['info']) return app('json')->fail(400274);
  91. if (!$data['menu_name']) return app('json')->fail(400275);
  92. if (!$data['desc']) return app('json')->fail(400276);
  93. if ($data['sort'] < 0) {
  94. $data['sort'] = 0;
  95. }
  96. if ($data['type'] == 'text') {
  97. if (!$data['width']) return app('json')->fail(400277);
  98. if ($data['width'] <= 0) return app('json')->fail(400278);
  99. }
  100. if ($data['type'] == 'textarea') {
  101. if (!$data['width']) return app('json')->fail(400279);
  102. if (!$data['high']) return app('json')->fail(400280);
  103. if ($data['width'] < 0) return app('json')->fail(400281);
  104. if ($data['high'] < 0) return app('json')->fail(400282);
  105. }
  106. if ($data['type'] == 'radio' || $data['type'] == 'checkbox') {
  107. if (!$data['parameter']) return app('json')->fail(400283);
  108. $this->services->valiDateRadioAndCheckbox($data);
  109. }
  110. $data['value'] = json_encode($data['value']);
  111. $config = $this->services->getOne(['menu_name' => $data['menu_name']]);
  112. if ($config) {
  113. $this->services->update($config['id'], $data, 'id');
  114. } else {
  115. $this->services->save($data);
  116. }
  117. \crmeb\services\CacheService::clear();
  118. return app('json')->success(400284);
  119. }
  120. /**
  121. * 显示指定的资源
  122. *
  123. * @param int $id
  124. * @return \think\Response
  125. */
  126. public function read($id)
  127. {
  128. if (!$id) {
  129. return app('json')->fail(100100);
  130. }
  131. $info = $this->services->getReadList((int)$id);
  132. return app('json')->success(compact('info'));
  133. }
  134. /**
  135. * 显示编辑资源表单页.
  136. *
  137. * @param int $id
  138. * @return \think\Response
  139. */
  140. public function edit($id)
  141. {
  142. return app('json')->success($this->services->editConfigForm((int)$id));
  143. }
  144. /**
  145. * 保存更新的资源
  146. *
  147. * @param int $id
  148. * @return \think\Response
  149. */
  150. public function update($id)
  151. {
  152. $type = request()->post('type');
  153. if ($type == 'text' || $type == 'textarea' || $type == 'radio' || ($type == 'upload' && (request()->post('upload_type') == 1 || request()->post('upload_type') == 3))) {
  154. $value = request()->post('value');
  155. } else {
  156. $value = request()->post('value/a');
  157. }
  158. if (!$value) $value = request()->post(request()->post('menu_name'));
  159. $data = $this->request->postMore([
  160. ['menu_name', ''],
  161. ['type', ''],
  162. ['input_type', 'input'],
  163. ['config_tab_id', 0],
  164. ['parameter', ''],
  165. ['upload_type', 1],
  166. ['required', ''],
  167. ['width', 0],
  168. ['high', 0],
  169. ['value', $value],
  170. ['info', ''],
  171. ['desc', ''],
  172. ['sort', 0],
  173. ['status', 0]
  174. ]);
  175. if (!$this->services->get($id)) {
  176. return app('json')->fail(100026);
  177. }
  178. $data['value'] = json_encode($data['value']);
  179. $this->services->update($id, $data);
  180. \crmeb\services\CacheService::clear();
  181. return app('json')->success(100001);
  182. }
  183. /**
  184. * 删除指定资源
  185. *
  186. * @param int $id
  187. * @return \think\Response
  188. */
  189. public function delete($id)
  190. {
  191. if (!$this->services->delete($id))
  192. return app('json')->fail(100008);
  193. else {
  194. \crmeb\services\CacheService::clear();
  195. return app('json')->success(100002);
  196. }
  197. }
  198. /**
  199. * 修改状态
  200. * @param $id
  201. * @param $status
  202. * @return mixed
  203. */
  204. public function set_status($id, $status)
  205. {
  206. if ($status == '' || $id == 0) {
  207. return app('json')->fail(100100);
  208. }
  209. $this->services->update($id, ['status' => $status]);
  210. \crmeb\services\CacheService::clear();
  211. return app('json')->success(100014);
  212. }
  213. /**
  214. * 基础配置
  215. * */
  216. public function edit_basics(Request $request)
  217. {
  218. $tabId = $this->request->param('tab_id', 1);
  219. if (!$tabId) {
  220. return app('json')->fail(100100);
  221. }
  222. $url = $request->baseUrl();
  223. return app('json')->success($this->services->getConfigForm($url, $tabId));
  224. }
  225. /**
  226. * 保存数据 true
  227. * */
  228. public function save_basics(Request $request)
  229. {
  230. $post = $this->request->post();
  231. foreach ($post as $k => $v) {
  232. if (is_array($v)) {
  233. $res = $this->services->getUploadTypeList($k);
  234. foreach ($res as $kk => $vv) {
  235. if ($kk == 'upload') {
  236. if ($vv == 1 || $vv == 3) {
  237. $post[$k] = $v[0];
  238. }
  239. }
  240. }
  241. }
  242. }
  243. $this->validate($post, \app\adminapi\validate\setting\SystemConfigValidata::class);
  244. if (isset($post['upload_type'])) {
  245. $this->services->checkThumbParam($post);
  246. }
  247. if (isset($post['extract_type']) && !count($post['extract_type'])) {
  248. return app('json')->fail(400753);
  249. }
  250. if (isset($post['store_brokerage_binding_status'])) {
  251. $this->services->checkBrokerageBinding($post);
  252. }
  253. if (isset($post['store_brokerage_ratio']) && isset($post['store_brokerage_two'])) {
  254. $num = $post['store_brokerage_ratio'] + $post['store_brokerage_two'];
  255. if ($num > 100) {
  256. return app('json')->fail(400285);
  257. }
  258. }
  259. if (isset($post['spread_banner'])) {
  260. $num = count($post['spread_banner']);
  261. if ($num > 5) {
  262. return app('json')->fail(400286);
  263. }
  264. }
  265. if (isset($post['user_extract_min_price'])) {
  266. if (!preg_match('/[0-9]$/', $post['user_extract_min_price'])) {
  267. return app('json')->fail(400287);
  268. }
  269. }
  270. if (isset($post['wss_open'])) {
  271. $this->services->saveSslFilePath((int)$post['wss_open'], $post['wss_local_pk'] ?? '', $post['wss_local_cert'] ?? '');
  272. }
  273. if (isset($post['store_brokerage_price']) && $post['store_brokerage_statu'] == 3) {
  274. if ($post['store_brokerage_price'] === '') {
  275. return app('json')->fail(400288);
  276. }
  277. if ($post['store_brokerage_price'] < 0) {
  278. return app('json')->fail(400289);
  279. }
  280. }
  281. if (isset($post['store_brokerage_binding_time']) && $post['store_brokerage_binding_status'] == 2) {
  282. if (!preg_match("/^[0-9][0-9]*$/", $post['store_brokerage_binding_time'])) {
  283. return app('json')->fail(400290);
  284. }
  285. }
  286. if (isset($post['uni_brokerage_price']) && $post['uni_brokerage_price'] < 0) {
  287. return app('json')->fail(400756);
  288. }
  289. if (isset($post['day_brokerage_price_upper']) && $post['day_brokerage_price_upper'] < -1) {
  290. return app('json')->fail(400757);
  291. }
  292. if (isset($post['pay_new_weixin_open']) && (bool)$post['pay_new_weixin_open']) {
  293. if (empty($post['pay_new_weixin_mchid'])) {
  294. return app('json')->fail(400763);
  295. }
  296. }
  297. //支付接口类型选择,如果有订单就不能再进行切换
  298. if (isset($post['pay_wechat_type'])) {
  299. /** @var StoreOrderServices $orderServices */
  300. $orderServices = app()->make(StoreOrderServices::class);
  301. if ($post['pay_wechat_type'] != -1 && $orderServices->count()) {
  302. return app('json')->fail('支付接口类型已经选择,不能再次进行切换,切换后会导致无法退款等问题。');
  303. }
  304. }
  305. if (isset($post['weixin_ckeck_file'])) {
  306. $from = public_path() . $post['weixin_ckeck_file'];
  307. $to = public_path() . array_reverse(explode('/', $post['weixin_ckeck_file']))[0];
  308. @copy($from, $to);
  309. }
  310. if (isset($post['ico_path'])) {
  311. $from = public_path() . $post['ico_path'];
  312. $toAdmin = public_path('admin') . 'favicon.ico';
  313. $toHome = public_path('home') . 'favicon.ico';
  314. $toPublic = public_path() . 'favicon.ico';
  315. @copy($from, $toAdmin);
  316. @copy($from, $toHome);
  317. @copy($from, $toPublic);
  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. \crmeb\services\CacheService::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. }