SystemConfig.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\admin\v1\system\config;
  12. use app\controller\admin\AuthController;
  13. use app\Request;
  14. use app\services\system\config\SystemConfigServices;
  15. use app\services\system\config\SystemConfigTabServices;
  16. use think\facade\App;
  17. /**
  18. * 系统配置
  19. * Class SystemConfig
  20. * @package app\controller\admin\v1\setting
  21. */
  22. class SystemConfig extends AuthController
  23. {
  24. /**
  25. * SystemConfig constructor.
  26. * @param App $app
  27. * @param SystemConfigServices $services
  28. */
  29. public function __construct(App $app, SystemConfigServices $services)
  30. {
  31. parent::__construct($app);
  32. $this->services = $services;
  33. }
  34. /**
  35. * 显示资源列表
  36. *
  37. * @return \think\Response
  38. */
  39. public function index()
  40. {
  41. $where = $this->request->getMore([
  42. ['tab_id', 0],
  43. ['status', -1]
  44. ]);
  45. if (!$where['tab_id']) {
  46. return $this->fail('参数错误');
  47. }
  48. if ($where['status'] == -1) {
  49. unset($where['status']);
  50. }
  51. $where['is_store'] = 0;
  52. return $this->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 $this->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', ''],
  78. ['config_tab_id', []],
  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. ['is_store', 0],
  90. ]);
  91. if ($data['config_tab_id']) $data['config_tab_id'] = end($data['config_tab_id']);
  92. if (!$data['info']) return $this->fail('请输入配置名称');
  93. if (!$data['menu_name']) return $this->fail('请输入字段名称');
  94. if (!$data['desc']) return $this->fail('请输入配置简介');
  95. if ($data['sort'] < 0) {
  96. $data['sort'] = 0;
  97. }
  98. if ($data['type'] == 'text') {
  99. if (!$data['width']) return $this->fail('请输入文本框的宽度');
  100. if ($data['width'] <= 0) return $this->fail('请输入正确的文本框的宽度');
  101. }
  102. if ($data['type'] == 'textarea') {
  103. if (!$data['width']) return $this->fail('请输入多行文本框的宽度');
  104. if (!$data['high']) return $this->fail('请输入多行文本框的高度');
  105. if ($data['width'] < 0) return $this->fail('请输入正确的多行文本框的宽度');
  106. if ($data['high'] < 0) return $this->fail('请输入正确的多行文本框的宽度');
  107. }
  108. if ($data['type'] == 'radio' || $data['type'] == 'checkbox') {
  109. if (!$data['parameter']) return $this->fail('请输入配置参数');
  110. $this->services->valiDateRadioAndCheckbox($data);
  111. }
  112. $data['value'] = json_encode($data['value']);
  113. $config = $this->services->getOne(['menu_name' => $data['menu_name']]);
  114. if ($config) {
  115. $this->services->update($config['id'], $data, 'id');
  116. } else {
  117. $this->services->save($data);
  118. }
  119. event('config.create', [$data]);
  120. return $this->success('添加配置成功!');
  121. }
  122. /**
  123. * 显示指定的资源
  124. *
  125. * @param int $id
  126. * @return \think\Response
  127. */
  128. public function read($id)
  129. {
  130. if (!$id) {
  131. return $this->fail('参数错误,请重新打开');
  132. }
  133. $info = $this->services->getReadList((int)$id);
  134. return $this->success(compact('info'));
  135. }
  136. /**
  137. * 显示编辑资源表单页.
  138. *
  139. * @param int $id
  140. * @return \think\Response
  141. */
  142. public function edit($id)
  143. {
  144. return $this->success($this->services->editConfigForm((int)$id));
  145. }
  146. /**
  147. * 保存更新的资源
  148. *
  149. * @param int $id
  150. * @return \think\Response
  151. */
  152. public function update($id)
  153. {
  154. $type = request()->post('type');
  155. if ($type == 'text' || $type == 'textarea' || $type == 'radio' || ($type == 'upload' && (request()->post('upload_type') == 1 || request()->post('upload_type') == 3))) {
  156. $value = request()->post('value');
  157. } else {
  158. $value = request()->post('value/a');
  159. }
  160. if (!$value) $value = request()->post(request()->post('menu_name'));
  161. $data = $this->request->postMore([['is_store', 0], 'status', 'info', 'desc', 'sort', 'config_tab_id', 'required', 'parameter', ['value', $value], 'upload_type', 'input_type']);
  162. $data['config_tab_id'] = end($data['config_tab_id']);
  163. if (!$this->services->get($id)) {
  164. return $this->fail('编辑的记录不存在!');
  165. }
  166. $data['value'] = json_encode($data['value']);
  167. $this->services->update($id, $data);
  168. \crmeb\services\SystemConfigService::clear();
  169. return $this->success('修改成功!');
  170. }
  171. /**
  172. * 删除指定资源
  173. *
  174. * @param int $id
  175. * @return \think\Response
  176. */
  177. public function delete($id)
  178. {
  179. if (!$this->services->delete($id))
  180. return $this->fail('删除失败,请稍候再试!');
  181. else {
  182. event('config.delete', [$id]);
  183. return $this->success('删除成功!');
  184. }
  185. }
  186. /**
  187. * 修改状态
  188. * @param $id
  189. * @param $status
  190. * @return mixed
  191. */
  192. public function set_status($id, $status)
  193. {
  194. if ($status == '' || $id == 0) {
  195. return $this->fail('参数错误');
  196. }
  197. $this->services->update($id, ['status' => $status]);
  198. event('config.status', [$id, $status]);
  199. return $this->success($status == 0 ? '隐藏成功' : '显示成功');
  200. }
  201. /**
  202. * 基础配置
  203. * */
  204. public function edit_basics(Request $request)
  205. {
  206. $tabId = $this->request->param('tab_id', 1);
  207. if (!$tabId) {
  208. return $this->fail('参数错误');
  209. }
  210. $url = $request->baseUrl();
  211. return $this->success($this->services->getConfigForm($url, $tabId));
  212. }
  213. /**
  214. * @param string $type
  215. * @return mixed
  216. */
  217. public function getNewFormBuild(string $type)
  218. {
  219. return $this->success($this->services->getNewFormBuildRule($type));
  220. }
  221. /**
  222. * 获取用户配置
  223. * @return mixed
  224. */
  225. public function getUserConfig($type)
  226. {
  227. return $this->success($this->services->getUserConfig($type));
  228. }
  229. /**
  230. * 保存用户配置
  231. * @param $type
  232. * @return mixed
  233. */
  234. public function saveUserConfig($type)
  235. {
  236. $post = $this->request->post();
  237. $this->validate($post, \app\validate\admin\setting\SystemConfigValidate::class);
  238. $this->services->saveUserConfig($type, $post);
  239. return $this->success('保存成功');
  240. }
  241. /**
  242. * 获取缩略图配置
  243. * @return mixed
  244. */
  245. public function getImageConfig()
  246. {
  247. return $this->success($this->services->getImageConfig());
  248. }
  249. /**
  250. * 保存数据 true
  251. * */
  252. public function save_basics(Request $request)
  253. {
  254. $post = $this->request->post();
  255. foreach ($post as $k => $v) {
  256. if (is_array($v)) {
  257. $res = $this->services->getUploadTypeList($k);
  258. foreach ($res as $kk => $vv) {
  259. if ($kk == 'upload') {
  260. if ($vv == 1 || $vv == 3) {
  261. $post[$k] = $v[0];
  262. }
  263. }
  264. }
  265. }
  266. }
  267. //打开友情链接 && 友情链接有配置内容
  268. if (isset($post['links_list']) && !empty($post['links_list']) && isset($post['links_open']) && $post['links_open']) {
  269. foreach ($post['links_list'] as $item) {
  270. foreach ($item as $v => $k) {
  271. if ('' === $item[$v]) {
  272. return $this->fail('友情链接内容不能为空');
  273. }
  274. }
  275. }
  276. }
  277. if (isset($post['filing_list']) && !empty($post['filing_list'])) {
  278. foreach ($post['filing_list'] as $item) {
  279. foreach ($item as $v => $k) {
  280. if ('' === $item[$v]) {
  281. return $this->fail('PC底部自定义展示内容不能为空');
  282. }
  283. }
  284. }
  285. }
  286. $this->validate($post, \app\validate\admin\setting\SystemConfigValidate::class);
  287. if (isset($post['upload_type'])) {
  288. $this->services->checkThumbParam($post);
  289. }
  290. if (isset($post['store_brokerage_binding_status'])) {
  291. $this->services->checkBrokerageBinding($post);
  292. }
  293. if (isset($post['store_brokerage_ratio']) && isset($post['store_brokerage_two'])) {
  294. $num = $post['store_brokerage_ratio'] + $post['store_brokerage_two'];
  295. if ($num > 100) {
  296. return $this->fail('一二级返佣比例不能大于100%');
  297. }
  298. }
  299. if (isset($post['spread_banner'])) {
  300. $num = count($post['spread_banner']);
  301. if ($num > 5) {
  302. return $this->fail('分销海报不能多于5张');
  303. }
  304. }
  305. if (isset($post['user_extract_min_price'])) {
  306. if (!preg_match('/[0-9]$/', $post['user_extract_min_price'])) {
  307. return $this->fail('提现最低金额只能为数字!');
  308. }
  309. }
  310. if (isset($post['store_extract_max_price']) && isset($post['store_extract_min_price'])) {
  311. if ($post['store_extract_max_price'] < $post['store_extract_min_price']) {
  312. return $this->fail('门店提现最低金额不能大于最高金额');
  313. }
  314. }
  315. //小程序支付
  316. if (isset($post['pay_routine_open']) && $post['pay_routine_open']) {
  317. if (empty($post['pay_routine_mchid'])) {
  318. return $this->fail('小程序商户号不能为空');
  319. }
  320. }
  321. if (isset($post['routine_auth_type'])) {
  322. if (empty($post['routine_auth_type']) || count($post['routine_auth_type']) == 0) {
  323. return app('json')->fail('手机号获取方式至少选择一个');
  324. }
  325. }
  326. //全场包邮开关
  327. if (isset($post['whole_free_shipping'])) {
  328. $wholeFreeShipping = (int)$post['whole_free_shipping'];
  329. if (!$wholeFreeShipping) {
  330. $post['store_free_postage'] = 0;
  331. }
  332. unset($post['whole_free_shipping']);
  333. }
  334. if (isset($post['default_send_day'])) {
  335. $post['default_send_day_start_time'] = time();
  336. }
  337. foreach ($post as $k => $v) {
  338. $config_one = $this->services->getOne(['menu_name' => $k]);
  339. if ($config_one) {
  340. $config_one['value'] = $v;
  341. $this->services->valiDateValue($config_one);
  342. $this->services->update($k, ['value' => json_encode($v)], 'menu_name');
  343. }
  344. }
  345. if (isset($post['wss_open'])) {
  346. $this->services->saveSslFilePath((int)$post['wss_open'], $post['wss_local_pk'] ?? '', $post['wss_local_cert'] ?? '');
  347. }
  348. \crmeb\services\SystemConfigService::clear();
  349. return $this->success('修改成功');
  350. }
  351. /**
  352. * 获取系统设置头部分类
  353. * @param SystemConfigTabServices $services
  354. * @return mixed
  355. * @throws \think\db\exception\DataNotFoundException
  356. * @throws \think\db\exception\DbException
  357. * @throws \think\db\exception\ModelNotFoundException
  358. */
  359. public function header_basics(SystemConfigTabServices $services)
  360. {
  361. [$type, $pid] = $this->request->getMore([
  362. [['type', 'd'], 0],
  363. [['pid', 'd'], 0]
  364. ], true);
  365. if ($type == 3) {//其它分类
  366. $config_tab = [];
  367. } else {
  368. $config_tab = $services->getConfigTab($pid);
  369. }
  370. return $this->success(compact('config_tab'));
  371. }
  372. /**
  373. * 获取单个配置的值
  374. * @param $name
  375. * @return mixed
  376. */
  377. public function get_system($name)
  378. {
  379. $value = sys_config($name);
  380. return $this->success(compact('value'));
  381. }
  382. /**
  383. * @return mixed
  384. */
  385. public function getVersion()
  386. {
  387. $version = get_crmeb_version();
  388. return $this->success([
  389. 'version' => $version,
  390. 'label' => strripos($version, 'min') === false ? 3 : 2
  391. ]);
  392. }
  393. }