SystemConfig.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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\admin\controller\setting;
  12. use think\Url;
  13. use service\FormBuilder as Form;
  14. use think\Request;
  15. use service\JsonService as Json;
  16. use service\UploadService as Upload;
  17. use app\admin\controller\AuthController;
  18. use app\admin\model\system\SystemConfig as ConfigModel;
  19. /**
  20. * 配置列表控制器
  21. * Class SystemConfig
  22. * @package app\admin\controller\system
  23. */
  24. class SystemConfig extends AuthController
  25. {
  26. /**
  27. * 基础配置
  28. * */
  29. public function index()
  30. {
  31. list($type, $pid, $tab_id, $children_tab_id) = parent::getMore([
  32. ['type', $this->request->param('type',0)],//配置类型
  33. ['paid', 0],//父级分类ID
  34. ['tab_id', $this->request->param('tab_id',0)],//当前分类ID
  35. ['children_tab_id', null],//当前子集分类ID
  36. ], null, true);
  37. $config_tab = null;//顶级分类
  38. $children_config_tab = null;//二级分类
  39. if ($type == 3) {//其它分类
  40. $config_tab = null;
  41. } else {
  42. $config_tab = ConfigModel::getConfigTabAll($type);
  43. }
  44. if ($pid) {
  45. $children_config_tab = ConfigModel::getConfigChildrenTabAll($pid);
  46. foreach ($children_config_tab as $kk => $vv) {
  47. $arr = ConfigModel::getAll($vv['id'])->toArray();
  48. if (empty($arr)) {
  49. unset($children_config_tab[$kk]);
  50. }
  51. }
  52. $tab_id = $pid;
  53. //表单字段
  54. $list = ConfigModel::getAll($children_tab_id);
  55. } else {
  56. $children_config_tab = ConfigModel::getConfigChildrenTabAll($tab_id);
  57. foreach ($children_config_tab as $kk => $vv) {
  58. $arr = ConfigModel::getAll($vv['id'])->toArray();
  59. if (empty($arr)) {
  60. unset($children_config_tab[$kk]);
  61. }
  62. }
  63. if (!$children_tab_id && $children_config_tab) $children_tab_id = $children_config_tab[0]['id'];
  64. //表单字段
  65. $list = ConfigModel::getAll($tab_id);
  66. }
  67. $this->assign('pid', $pid);
  68. $this->assign('children_tab_id', $children_tab_id);
  69. $this->assign('tab_id', $tab_id);
  70. $this->assign('config_tab', $config_tab);
  71. $this->assign('children_config_tab', $children_config_tab);
  72. $formbuider = [];
  73. foreach ($list as $data) {
  74. switch ($data['type']) {
  75. case 'text'://文本框
  76. switch ($data['input_type']) {
  77. case 'input':
  78. $data['value'] = json_decode($data['value'], true) ?: '';
  79. $formbuider[] = Form::input($data['menu_name'], $data['info'], $data['value'])->info($data['desc'])->placeholder($data['desc'])->col(13);
  80. break;
  81. case 'number':
  82. $data['value'] = json_decode($data['value'], true) ?: 0;
  83. $formbuider[] = Form::number($data['menu_name'], $data['info'], $data['value'])->info($data['desc']);
  84. break;
  85. case 'dateTime':
  86. $formbuider[] = Form::dateTime($data['menu_name'], $data['info'], $data['value'])->info($data['desc']);
  87. break;
  88. case 'color':
  89. $data['value'] = json_decode($data['value'], true) ?: '';
  90. $formbuider[] = Form::color($data['menu_name'], $data['info'], $data['value'])->info($data['desc']);
  91. break;
  92. default:
  93. $data['value'] = json_decode($data['value'], true) ?: '';
  94. $formbuider[] = Form::input($data['menu_name'], $data['info'], $data['value'])->info($data['desc'])->placeholder($data['desc'])->col(13);
  95. break;
  96. }
  97. break;
  98. case 'textarea'://多行文本框
  99. $data['value'] = json_decode($data['value'], true) ?: '';
  100. $formbuider[] = Form::textarea($data['menu_name'], $data['info'], $data['value'])->placeholder($data['desc'])->info($data['desc'])->rows(6)->col(13);
  101. break;
  102. case 'radio'://单选框
  103. $data['value'] = json_decode($data['value'], true) ?: '0';
  104. $parameter = explode("\n", $data['parameter']);
  105. $options = [];
  106. if ($parameter) {
  107. foreach ($parameter as $v) {
  108. if(strstr($v,'=>') !== false){
  109. $pdata = explode("=>", $v);
  110. }else if(strstr($v,'=') !== false){
  111. $pdata = explode("=", $v);
  112. }
  113. $options[] = ['label' => $pdata[1], 'value' => $pdata[0]];
  114. }
  115. $formbuider[] = Form::radio($data['menu_name'], $data['info'], $data['value'])->options($options)->info($data['desc'])->col(13);
  116. }
  117. break;
  118. case 'upload'://文件上传
  119. switch ($data['upload_type']) {
  120. case 1:
  121. $data['value'] = json_decode($data['value'], true) ?: '';
  122. $formbuider[] = Form::frameImageOne($data['menu_name'], $data['info'], Url::build('admin/widget.images/index', array('fodder' => $data['menu_name'])), $data['value'])->icon('image')->width('70%')->height('500px')->info($data['desc'])->col(13);
  123. break;
  124. case 2:
  125. $data['value'] = json_decode($data['value'], true) ?: [];
  126. $formbuider[] = Form::frameImages($data['menu_name'], $data['info'], Url::build('admin/widget.images/index', array('fodder' => $data['menu_name'])), $data['value'])->maxLength(5)->icon('image')->width('70%')->height('500px')->info($data['desc'])->col(13);
  127. break;
  128. case 3:
  129. $data['value'] = json_decode($data['value'], true);
  130. $formbuider[] = Form::uploadFileOne($data['menu_name'], $data['info'], Url::build('file_upload'), $data['value'])->name('file')->info($data['desc'])->col(13);
  131. break;
  132. }
  133. break;
  134. case 'checkbox'://多选框
  135. $data['value'] = json_decode($data['value'], true) ?: [];
  136. $parameter = explode("\n", $data['parameter']);
  137. $options = [];
  138. if ($parameter) {
  139. foreach ($parameter as $v) {
  140. if(strstr($v,'=>') !== false){
  141. $pdata = explode("=>", $v);
  142. }else if(strstr($v,'=') !== false){
  143. $pdata = explode("=", $v);
  144. }
  145. $options[] = ['label' => $pdata[1], 'value' => $pdata[0]];
  146. }
  147. if (!is_array($data['value'])) {
  148. $value = [$data['value']];
  149. }else{
  150. $value = $data['value'];
  151. }
  152. $formbuider[] = Form::checkbox($data['menu_name'], $data['info'], $value)->options($options)->info($data['desc'])->col(13);
  153. }
  154. break;
  155. case 'select'://多选框
  156. $data['value'] = json_decode($data['value'], true) ?: [];
  157. $parameter = explode("\n", $data['parameter']);
  158. $options = [];
  159. if ($parameter) {
  160. foreach ($parameter as $v) {
  161. if(strstr($v,'=>') !== false){
  162. $pdata = explode("=>", $v);
  163. }else if(strstr($v,'=') !== false){
  164. $pdata = explode("=", $v);
  165. }
  166. $options[] = ['label' => $pdata[1], 'value' => $pdata[0]];
  167. }
  168. $formbuider[] = Form::select($data['menu_name'], $data['info'], $data['value'])->options($options)->info($data['desc'])->col(13);
  169. }
  170. break;
  171. }
  172. }
  173. $form = Form::make_post_form('编辑配置', $formbuider, Url::build('save_basics'));
  174. $this->assign(compact('form'));
  175. $this->assign('list', $list);
  176. return $this->fetch();
  177. }
  178. /**
  179. * 添加字段
  180. * */
  181. public function create(Request $request)
  182. {
  183. $data = parent::getMore(['type'], $request);//接收参数
  184. $tab_id = $request->param('tab_id', 1);
  185. $formbuider = array();
  186. switch ($data['type']) {
  187. case 0://文本框
  188. $formbuider = ConfigModel::createInputRule($tab_id);
  189. break;
  190. case 1://多行文本框
  191. $formbuider = ConfigModel::createTextAreaRule($tab_id);
  192. break;
  193. case 2://单选框
  194. $formbuider = ConfigModel::createRadioRule($tab_id);
  195. break;
  196. case 3://文件上传
  197. $formbuider = ConfigModel::createUploadRule($tab_id);
  198. break;
  199. case 4://多选框
  200. $formbuider = ConfigModel::createCheckboxRule($tab_id);
  201. break;
  202. }
  203. $form = Form::make_post_form('添加字段', $formbuider, Url::build('save'));
  204. $this->assign(compact('form'));
  205. return $this->fetch('public/form-builder');
  206. }
  207. /**
  208. * 保存字段
  209. * */
  210. public function save(Request $request)
  211. {
  212. $data = parent::postMore([
  213. 'menu_name',
  214. 'type',
  215. 'config_tab_id',
  216. 'parameter',
  217. 'upload_type',
  218. 'required',
  219. 'width',
  220. 'high',
  221. 'value',
  222. 'info',
  223. 'desc',
  224. 'sort',
  225. 'status',], $request);
  226. if (!$data['info']) return Json::fail('请输入配置名称');
  227. if (!$data['menu_name']) return Json::fail('请输入字段名称');
  228. if ($data['menu_name']) {
  229. $oneConfig = ConfigModel::getOneConfig('menu_name', $data['menu_name']);
  230. if (!empty($oneConfig)) return Json::fail('请重新输入字段名称,之前的已经使用过了');
  231. }
  232. if (!$data['desc']) return Json::fail('请输入配置简介');
  233. if ($data['sort'] < 0) {
  234. $data['sort'] = 0;
  235. }
  236. if ($data['type'] == 'text') {
  237. if (!ConfigModel::valiDateTextRole($data)) return Json::fail(ConfigModel::getErrorInfo());
  238. }
  239. if ($data['type'] == 'textarea') {
  240. if (!ConfigModel::valiDateTextareaRole($data)) return Json::fail(ConfigModel::getErrorInfo());
  241. }
  242. if ($data['type'] == 'radio' || $data['type'] == 'checkbox') {
  243. if (!$data['parameter']) return Json::fail('请输入配置参数');
  244. if (!ConfigModel::valiDateRadioAndCheckbox($data)) return Json::fail(ConfigModel::getErrorInfo());
  245. $data['value'] = json_encode($data['value']);
  246. }
  247. ConfigModel::set($data);
  248. return Json::successful('添加菜单成功!');
  249. }
  250. /**
  251. * @param Request $request
  252. * @param $id
  253. * @return \think\response\Json
  254. */
  255. public function update_config(Request $request, $id)
  256. {
  257. $data = parent::postMore(['status', 'info', 'desc', 'sort', 'config_tab_id', 'required', 'parameter', 'value', 'upload_type'], $request);
  258. if (!ConfigModel::get($id)) return Json::fail('编辑的记录不存在!');
  259. $data['value'] = rtrim($data['value'], '"');
  260. $data['value'] = ltrim($data['value'], '"');
  261. ConfigModel::edit($data, $id);
  262. return Json::successful('修改成功!');
  263. }
  264. /**
  265. * 修改是否显示子子段
  266. * @param $id
  267. * @return mixed
  268. */
  269. public function edit_cinfig($id)
  270. {
  271. $menu = ConfigModel::get($id)->getData();
  272. if (!$menu) return Json::fail('数据不存在!');
  273. $formbuider = array();
  274. $formbuider[] = Form::input('menu_name', '字段变量', $menu['menu_name'])->disabled(1);
  275. $formbuider[] = Form::select('config_tab_id', '分类', (string)$menu['config_tab_id'])->setOptions(ConfigModel::getConfigTabAll(-1));
  276. $formbuider[] = Form::input('info', '配置名称', $menu['info'])->autofocus(1);
  277. $formbuider[] = Form::input('desc', '配置简介', $menu['desc']);
  278. //输入框验证规则
  279. if (!empty($menu['required'])) {
  280. $formbuider[] = Form::input('value', '默认值', $menu['value']);
  281. $formbuider[] = Form::number('width', '文本框宽(%)', $menu['width']);
  282. $formbuider[] = Form::input('required', '验证规则', $menu['required'])->placeholder('多个请用,隔开例如:required:true,url:true');
  283. }
  284. //多行文本
  285. if (!empty($menu['high'])) {
  286. $formbuider[] = Form::textarea('value', '默认值', $menu['value'])->rows(5);
  287. $formbuider[] = Form::number('width', '文本框宽(%)', $menu['width']);
  288. $formbuider[] = Form::number('high', '多行文本框高(%)', $menu['high']);
  289. } else {
  290. $formbuider[] = Form::input('value', '默认值', $menu['value']);
  291. }
  292. //单选和多选参数配置
  293. if (!empty($menu['parameter'])) {
  294. $formbuider[] = Form::textarea('parameter', '配置参数', $menu['parameter'])->placeholder("参数方式例如:\n1=白色\n2=红色\n3=黑色");
  295. }
  296. //上传类型选择
  297. if (!empty($menu['upload_type'])) {
  298. $formbuider[] = Form::radio('upload_type', '上传类型', $menu['upload_type'])->options([['value' => 1, 'label' => '单图'], ['value' => 2, 'label' => '多图'], ['value' => 3, 'label' => '文件']]);
  299. }
  300. $formbuider[] = Form::number('sort', '排序', $menu['sort']);
  301. $formbuider[] = Form::radio('status', '状态', $menu['status'])->options([['value' => 1, 'label' => '显示'], ['value' => 2, 'label' => '隐藏']]);
  302. $form = Form::make_post_form('编辑字段', $formbuider, Url::build('update_config', array('id' => $id)));
  303. $this->assign(compact('form'));
  304. return $this->fetch('public/form-builder');
  305. }
  306. /**
  307. * 删除子字段
  308. * @return \think\response\Json
  309. */
  310. public function delete_cinfig()
  311. {
  312. $id = input('id');
  313. if (!ConfigModel::del($id))
  314. return Json::fail(ConfigModel::getErrorInfo('删除失败,请稍候再试!'));
  315. else
  316. return Json::successful('删除成功!');
  317. }
  318. /**
  319. * 保存数据 true
  320. * */
  321. public function save_basics()
  322. {
  323. $request = Request::instance();
  324. if ($request->isPost()) {
  325. $post = $request->post();
  326. if(isset($post['tab_id'])) unset($post['tab_id']);
  327. foreach ($post as $k => $v) {
  328. if (is_array($v)) {
  329. $res = ConfigModel::where('menu_name', $k)->column('type,upload_type');
  330. foreach ($res as $kk => $vv) {
  331. if ($kk == 'upload') {
  332. if ($vv == 1 || $vv == 3) {
  333. $post[$k] = $v[0];
  334. }
  335. }
  336. }
  337. }
  338. }
  339. foreach ($post as $k => $v) {
  340. if($k=='site_url'){
  341. $http = "/^(http(s)?:\/\/)([0-9a-z-]{1,}.)?[0-9a-z-]{2,}.([0-9a-z-]{2,}.)?[a-z]{2,}$/i";
  342. if(!preg_match($http,$v) || substr_count($v,'/')>2){
  343. return Json::fail('域名有误!应如:http://crmeb.net');
  344. }
  345. }
  346. if($k=='gold_rate' || $k=='single_gold_coin'|| $k=='extract_min_money'|| $k=='store_brokerage_ratio'|| $k=='store_brokerage_two'|| $k=='barrage_show_time'|| $k=='store_stock'){
  347. if(bcsub($v,0,0)<=0){
  348. switch ($k){
  349. case 'gold_rate':
  350. return Json::fail('人民币与金币换算率不能小于等于0');
  351. break;
  352. case 'single_gold_coin':
  353. return Json::fail('单次签到虚拟币数不能小于等于0');
  354. break;
  355. case 'extract_min_money':
  356. return Json::fail('提现最低金额不能小于等于0');
  357. break;
  358. case 'store_brokerage_ratio':
  359. return Json::fail('一级推广人返佣比例不能小于等于0');
  360. break;
  361. case 'store_brokerage_two':
  362. return Json::fail('二级推广人返佣比例不能小于等于0');
  363. break;
  364. case 'barrage_show_time':
  365. return Json::fail('专题弹幕停留时间不能小于等于0');
  366. break;
  367. case 'store_stock':
  368. return Json::fail('警戒库存不能小于等于0');
  369. break;
  370. }
  371. }
  372. }
  373. ConfigModel::edit(['value' => json_encode($v)], $k, 'menu_name');
  374. }
  375. return Json::successful('修改成功');
  376. }
  377. }
  378. /**
  379. * 模板表单提交
  380. * */
  381. public function view_upload()
  382. {
  383. if ($_POST['type'] == 3) {
  384. $res = Upload::file($_POST['file'], 'config/file');
  385. } else {
  386. $res = Upload::Image($_POST['file'], 'config/image');
  387. }
  388. if (!$res->status) return Json::fail($res->error);
  389. return Json::successful('上传成功!', ['url' => $res->filePath]);
  390. }
  391. /**
  392. * 基础配置 单个
  393. * @return mixed|void
  394. */
  395. public function index_alone()
  396. {
  397. $tab_id = input('tab_id');
  398. if (!$tab_id) return $this->failed('参数错误,请重新打开');
  399. $this->assign('tab_id', $tab_id);
  400. $list = ConfigModel::getAll($tab_id);
  401. $config_tab = ConfigModel::getConfigTabAll();
  402. foreach ($config_tab as $kk => $vv) {
  403. $arr = ConfigModel::getAll($vv['value'])->toArray();
  404. if (empty($arr)) {
  405. unset($config_tab[$kk]);
  406. }
  407. }
  408. $this->assign('config_tab', $config_tab);
  409. $this->assign('list', $list);
  410. return $this->fetch();
  411. }
  412. /**
  413. * 保存数据 单个
  414. * @return mixed
  415. */
  416. public function save_basics_alone()
  417. {
  418. $request = Request::instance();
  419. if ($request->isPost()) {
  420. $post = $request->post();
  421. $tab_id = $post['tab_id'];
  422. unset($post['tab_id']);
  423. foreach ($post as $k => $v) {
  424. ConfigModel::edit(['value' => json_encode($v)], $k, 'menu_name');
  425. }
  426. return $this->successfulNotice('修改成功');
  427. }
  428. }
  429. /**
  430. * 获取文件名
  431. * */
  432. public function getImageName()
  433. {
  434. $request = Request::instance();
  435. $post = $request->post();
  436. $src = $post['src'];
  437. $data['name'] = basename($src);
  438. exit(json_encode($data));
  439. }
  440. /**
  441. * 上传文件
  442. * @return string
  443. */
  444. public function file_upload()
  445. {
  446. $res = Upload::file('file', 'config/file');
  447. if(!$res->status) return Json::fail($res->error);
  448. return Json::successful('上传成功!',['filePath'=>$res->filePath]);
  449. }
  450. }