SystemConfig.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. <?php
  2. /**
  3. * @author: xaboy<365615158@qq.com>
  4. * @day: 2017/11/02
  5. */
  6. namespace app\admin\model\system;
  7. use crmeb\basic\BaseModel;
  8. use crmeb\services\FormBuilder as Form;
  9. use crmeb\traits\ModelTrait;
  10. use think\facade\Route as Url;
  11. class SystemConfig extends BaseModel
  12. {
  13. /**
  14. * 数据表主键
  15. * @var string
  16. */
  17. protected $pk = 'id';
  18. /**
  19. * 模型名称
  20. * @var string
  21. */
  22. protected $name = 'system_config';
  23. use ModelTrait;
  24. /**
  25. * 修改单个配置
  26. * @param $menu
  27. * @param $value
  28. * @return bool
  29. */
  30. public static function setValue($menu, $value)
  31. {
  32. if (empty($menu) || !($config_one = self::get(['menu_name' => $menu]))) return self::setErrorInfo('字段名称错误');
  33. if ($config_one['type'] == 'radio' || $config_one['type'] == 'checkbox') {
  34. $parameter = [];
  35. $option = [];
  36. $parameter = explode(',', $config_one['parameter']);
  37. foreach ($parameter as $k => $v) {
  38. if (isset($v) && !empty($v)) {
  39. $option[$k] = explode('-', $v);
  40. }
  41. }
  42. $value_arr = [];//选项的值
  43. foreach ($option as $k => $v) {
  44. foreach ($v as $kk => $vv)
  45. if (!$kk) {
  46. $value_arr[$k] = $vv;
  47. }
  48. }
  49. $i = 0;//
  50. if (is_array($value)) {
  51. foreach ($value as $value_v) {
  52. if (in_array($value_v, $value_arr)) {
  53. $i++;
  54. }
  55. }
  56. if (count($value) != $i) return self::setErrorInfo('输入的值不属于选项中的参数');
  57. } else {
  58. if (in_array($value, $value_arr)) {
  59. $i++;
  60. }
  61. if (!$i) return self::setErrorInfo('输入的值不属于选项中的参数');
  62. }
  63. if ($config_one['type'] == 'radio' && is_array($value)) return self::setErrorInfo('单选按钮的值是字符串不是数组');
  64. }
  65. $bool = self::edit(['value' => json_encode($value)], $menu, 'menu_name');
  66. return $bool;
  67. }
  68. /**
  69. * 获取单个参数配置
  70. * @param $menu
  71. * @return bool|mixed
  72. * @throws \think\db\exception\DataNotFoundException
  73. * @throws \think\db\exception\ModelNotFoundException
  74. * @throws \think\exception\DbException
  75. */
  76. public static function getConfigValue($menu)
  77. {
  78. if (empty($menu) || !($config_one = self::where('menu_name', $menu)->find())) return false;
  79. return json_decode($config_one['value'], true);
  80. }
  81. /**
  82. * 获得多个参数
  83. * @param $menus
  84. * @return array
  85. */
  86. public static function getMore($menus)
  87. {
  88. $menus = is_array($menus) ? implode(',', $menus) : $menus;
  89. $list = self::where('menu_name', 'IN', $menus)->column('value', 'menu_name') ?: [];
  90. foreach ($list as $menu => $value) {
  91. $list[$menu] = json_decode($value, true);
  92. }
  93. return $list;
  94. }
  95. /**
  96. * @return array
  97. */
  98. public static function getAllConfig()
  99. {
  100. $list = self::column('value', 'menu_name') ?: [];
  101. foreach ($list as $menu => $value) {
  102. $list[$menu] = json_decode($value, true);
  103. }
  104. return $list;
  105. }
  106. /**
  107. * text 判断
  108. * @param $data
  109. * @return bool
  110. */
  111. public static function valiDateTextRole($data)
  112. {
  113. if (!$data['width']) return self::setErrorInfo('请输入文本框的宽度');
  114. if ($data['width'] <= 0) return self::setErrorInfo('请输入正确的文本框的宽度');
  115. return true;
  116. }
  117. /**
  118. * radio 和 checkbox规则的判断
  119. * @param $data
  120. * @return bool
  121. */
  122. public static function valiDateRadioAndCheckbox($data)
  123. {
  124. $parameter = [];
  125. $option = [];
  126. $option_new = [];
  127. $data['parameter'] = str_replace("\r\n", "\n", $data['parameter']);//防止不兼容
  128. $parameter = explode("\n", $data['parameter']);
  129. if (count($parameter) < 2) return self::setErrorInfo('请输入正确格式的配置参数');
  130. foreach ($parameter as $k => $v) {
  131. if (isset($v) && !empty($v)) {
  132. $option[$k] = explode('=>', $v);
  133. }
  134. }
  135. if (count($option) < 2) return self::setErrorInfo('请输入正确格式的配置参数');
  136. $bool = 1;
  137. foreach ($option as $k => $v) {
  138. $option_new[$k] = $option[$k][0];
  139. foreach ($v as $kk => $vv) {
  140. $vv_num = strlen($vv);
  141. if (!$vv_num) {
  142. $bool = 0;
  143. }
  144. }
  145. }
  146. if (!$bool) return self::setErrorInfo('请输入正确格式的配置参数');
  147. $num1 = count($option_new);//提取该数组的数目
  148. $arr2 = array_unique($option_new);//合并相同的元素
  149. $num2 = count($arr2);//提取合并后数组个数
  150. if ($num1 > $num2) return self::setErrorInfo('请输入正确格式的配置参数');
  151. return true;
  152. }
  153. /**
  154. * textarea 判断
  155. * @param $data
  156. * @return bool
  157. */
  158. public static function valiDateTextareaRole($data)
  159. {
  160. if (!$data['width']) return self::setErrorInfo('请输入多行文本框的宽度');
  161. if (!$data['high']) return self::setErrorInfo('请输入多行文本框的高度');
  162. if ($data['width'] < 0) return self::setErrorInfo('请输入正确的多行文本框的宽度');
  163. if ($data['high'] < 0) return self::setErrorInfo('请输入正确的多行文本框的宽度');
  164. return true;
  165. }
  166. /**
  167. * 获取一数据
  168. * @param $filed
  169. * @param $value
  170. * @return array|null|\think\Model
  171. * @throws \think\db\exception\DataNotFoundException
  172. * @throws \think\db\exception\ModelNotFoundException
  173. * @throws \think\exception\DbException
  174. */
  175. public static function getOneConfig($filed, $value)
  176. {
  177. $where[$filed] = $value;
  178. return self::where($where)->find();
  179. }
  180. /**
  181. * 获取配置分类
  182. * @param $id
  183. * @return \think\Collection
  184. * @throws \think\db\exception\DataNotFoundException
  185. * @throws \think\db\exception\ModelNotFoundException
  186. * @throws \think\exception\DbException
  187. */
  188. public static function getAll($id, int $status = 1)
  189. {
  190. $where['config_tab_id'] = $id;
  191. if ($status == 1) $where['status'] = $status;
  192. return self::where($where)->order('sort desc,id asc')->select();
  193. }
  194. /**
  195. * 获取所有配置分类
  196. * @param int $type
  197. * @return array
  198. */
  199. public static function getConfigTabAll($type = 0)
  200. {
  201. $configAll = SystemConfigTab::getAll($type);
  202. $config_tab = [];
  203. foreach ($configAll as $k => $v) {
  204. if (!$v['info']) {
  205. $config_tab[$k]['id'] = $v['id'];
  206. $config_tab[$k]['label'] = $v['title'];
  207. $config_tab[$k]['icon'] = $v['icon'];
  208. $config_tab[$k]['type'] = $v['type'];
  209. $config_tab[$k]['pid'] = $v['pid'];
  210. }
  211. }
  212. return $config_tab;
  213. }
  214. /**
  215. * 获取所有配置分类
  216. * @param int $type
  217. * @return array
  218. */
  219. public static function getConfigChildrenTabAll($pid = 0)
  220. {
  221. $configAll = SystemConfigTab::getChildrenTab($pid);
  222. $config_tab = [];
  223. foreach ($configAll as $k => $v) {
  224. if (!$v['info']) {
  225. $config_tab[$k]['id'] = $v['id'];
  226. $config_tab[$k]['label'] = $v['title'];
  227. $config_tab[$k]['icon'] = $v['icon'];
  228. $config_tab[$k]['type'] = $v['type'];
  229. $config_tab[$k]['pid'] = $v['pid'];
  230. }
  231. }
  232. return $config_tab;
  233. }
  234. /**
  235. * 选择类型
  236. * @param string $type
  237. * @return array
  238. */
  239. public static function radiotype($type = 'text')
  240. {
  241. return [
  242. ['value' => 'text', 'label' => '文本框', 'disabled' => 1]
  243. , ['value' => 'textarea', 'label' => '多行文本框', 'disabled' => 1]
  244. , ['value' => 'radio', 'label' => '单选按钮', 'disabled' => 1]
  245. , ['value' => 'upload', 'label' => '文件上传', 'disabled' => 1]
  246. , ['value' => 'checkbox', 'label' => '多选按钮', 'disabled' => 1]
  247. ];
  248. }
  249. /**
  250. * 选择文本框类型
  251. * @param string $type
  252. * @return array
  253. */
  254. public static function texttype($type = 'text')
  255. {
  256. return [
  257. ['value' => 'input', 'label' => '文本框']
  258. , ['value' => 'dateTime', 'label' => '时间']
  259. , ['value' => 'color', 'label' => '颜色']
  260. , ['value' => 'number', 'label' => '数字']
  261. ];
  262. }
  263. /**
  264. * 选择文文件类型
  265. * @param string $type
  266. * @return array
  267. */
  268. public static function uploadtype($type = 'text')
  269. {
  270. return [['value' => 1, 'label' => '单图']
  271. , ['value' => 2, 'label' => '多图']
  272. , ['value' => 3, 'label' => '文件']];
  273. }
  274. /**
  275. * 字段状态
  276. * @param string $type
  277. * @return array
  278. */
  279. public static function formstatus($type = 'text')
  280. {
  281. return [['value' => 1, 'label' => '显示'], ['value' => 2, 'label' => '隐藏']];
  282. }
  283. /**
  284. * 文本框
  285. * @param $tab_id
  286. * @return array
  287. * @throws \FormBuilder\exception\FormBuilderException
  288. */
  289. public static function createInputRule($tab_id)
  290. {
  291. $formbuider = [];
  292. $formbuider[] = Form::hidden('type', 'text');
  293. // $formbuider[] = Form::select('config_tab_id','分类',$tab_id)->setOptions(SystemConfig::getConfigTabAll(-1));
  294. $formbuider[] = Form::select('input_type', '类型')->setOptions(self::texttype());
  295. // $formbuider[] = Form::input('info','配置名称')->autofocus(1);
  296. // $formbuider[] = Form::input('menu_name','字段变量')->placeholder('例如:site_url');
  297. // $formbuider[] = Form::input('desc','配置简介');
  298. $formbuider[] = Form::input('value', '默认值');
  299. $formbuider[] = Form::number('width', '文本框宽(%)', 100);
  300. $formbuider[] = Form::input('required', '验证规则')->placeholder('多个请用,隔开例如:required:true,url:true');
  301. // $formbuider[] = Form::number('sort','排序');
  302. // $formbuider[] = Form::radio('status','状态',1)->options(self::formstatus());
  303. return $formbuider;
  304. }
  305. /**
  306. * 多行文本框
  307. * @param $tab_id
  308. * @return array
  309. * @throws \FormBuilder\exception\FormBuilderException
  310. */
  311. public static function createTextAreaRule($tab_id)
  312. {
  313. $formbuider = [];
  314. $formbuider[] = Form::hidden('type', 'textarea');
  315. // $formbuider[] = Form::select('config_tab_id','分类',$tab_id)->setOptions(SystemConfig::getConfigTabAll(-1));
  316. // $formbuider[] = Form::input('info','配置名称')->autofocus(1);
  317. // $formbuider[] = Form::input('menu_name','字段变量')->placeholder('例如:site_url');
  318. // $formbuider[] = Form::input('desc','配置简介');
  319. $formbuider[] = Form::textarea('value', '默认值');
  320. $formbuider[] = Form::number('width', '文本框宽(%)', 100);
  321. $formbuider[] = Form::number('high', '多行文本框高(%)', 5);
  322. // $formbuider[] = Form::number('sort','排序');
  323. // $formbuider[] = Form::radio('status','状态',1)->options(self::formstatus());
  324. return $formbuider;
  325. }
  326. /**
  327. * 单选按钮
  328. * @param $tab_id
  329. * @return array
  330. * @throws \FormBuilder\exception\FormBuilderException
  331. */
  332. public static function createRadioRule($tab_id)
  333. {
  334. $formbuider = [];
  335. $formbuider[] = Form::hidden('type', 'radio');
  336. // $formbuider[] = Form::select('config_tab_id','分类',$tab_id)->setOptions(SystemConfig::getConfigTabAll(-1));
  337. // $formbuider[] = Form::input('info','配置名称')->autofocus(1);
  338. // $formbuider[] = Form::input('menu_name','字段变量')->placeholder('例如:site_url');
  339. // $formbuider[] = Form::input('desc','配置简介');
  340. $formbuider[] = Form::textarea('parameter', '配置参数')->placeholder("参数方式例如:\n1=>男\n2=>女\n3=>保密");
  341. $formbuider[] = Form::input('value', '默认值');
  342. // $formbuider[] = Form::number('sort','排序');
  343. // $formbuider[] = Form::radio('status','状态',1)->options(self::formstatus());
  344. return $formbuider;
  345. }
  346. /**
  347. * 文件上传
  348. * @param $tab_id
  349. * @return array
  350. * @throws \FormBuilder\exception\FormBuilderException
  351. */
  352. public static function createUploadRule($tab_id)
  353. {
  354. $formbuider = [];
  355. $formbuider[] = Form::hidden('type', 'upload');
  356. // $formbuider[] = Form::select('config_tab_id','分类',$tab_id)->setOptions(SystemConfig::getConfigTabAll(-1));
  357. // $formbuider[] = Form::input('info','配置名称')->autofocus(1);
  358. // $formbuider[] = Form::input('menu_name','字段变量')->placeholder('例如:site_url');
  359. // $formbuider[] = Form::input('desc','配置简介');
  360. $formbuider[] = Form::radio('upload_type', '上传类型', 1)->options(self::uploadtype());
  361. // $formbuider[] = Form::number('sort','排序');
  362. // $formbuider[] = Form::radio('status','状态',1)->options(self::formstatus());
  363. return $formbuider;
  364. }
  365. /**
  366. * 多选框
  367. * @param $tab_id
  368. * @return array
  369. * @throws \FormBuilder\exception\FormBuilderException
  370. */
  371. public static function createCheckboxRule($tab_id)
  372. {
  373. $formbuider = [];
  374. $formbuider[] = Form::hidden('type', 'checkbox');
  375. // $formbuider[] = Form::select('config_tab_id','分类',$tab_id)->setOptions(SystemConfig::getConfigTabAll(-1));
  376. // $formbuider[] = Form::input('info','配置名称')->autofocus(1);
  377. // $formbuider[] = Form::input('menu_name','字段变量')->placeholder('例如:site_url');
  378. // $formbuider[] = Form::input('desc','配置简介');
  379. $formbuider[] = Form::textarea('parameter', '配置参数')->placeholder("参数方式例如:\n1=>白色\n2=>红色\n3=>黑色");
  380. // $formbuider[] = Form::input('value','默认值');
  381. // $formbuider[] = Form::number('sort','排序');
  382. // $formbuider[] = Form::radio('status','状态',1)->options(self::formstatus());
  383. return $formbuider;
  384. }
  385. /**
  386. * 下拉框
  387. * @param $tab_id
  388. * @return array
  389. * @throws \FormBuilder\exception\FormBuilderException
  390. */
  391. public static function createSelectRule($tab_id)
  392. {
  393. $formbuider = [];
  394. $formbuider[] = Form::hidden('type', 'select');
  395. // $formbuider[] = Form::select('config_tab_id','分类',$tab_id)->setOptions(SystemConfig::getConfigTabAll(-1));
  396. // $formbuider[] = Form::input('info','配置名称')->autofocus(1);
  397. // $formbuider[] = Form::input('menu_name','字段变量')->placeholder('例如:site_url');
  398. // $formbuider[] = Form::input('desc','配置简介');
  399. $formbuider[] = Form::textarea('parameter', '配置参数')->placeholder("参数方式例如:\n1=>白色\n2=>红色\n3=>黑色");
  400. // $formbuider[] = Form::input('value','默认值');
  401. // $formbuider[] = Form::number('sort','排序');
  402. // $formbuider[] = Form::radio('status','状态',1)->options(self::formstatus());
  403. return $formbuider;
  404. }
  405. /** 生成配置表单
  406. * @param $list
  407. * @return $this
  408. */
  409. public static function builder_config_from_data($list)
  410. {
  411. $formbuider = [];
  412. foreach ($list as $data) {
  413. switch ($data['type']) {
  414. case 'text'://文本框
  415. switch ($data['input_type']) {
  416. case 'input':
  417. $data['value'] = json_decode($data['value'], true) ?: '';
  418. $formbuider[] = Form::input($data['menu_name'], $data['info'], $data['value'])->info($data['desc'])->placeholder($data['desc'])->col(13);
  419. break;
  420. case 'number':
  421. $data['value'] = json_decode($data['value'], true) ?: 0;
  422. $formbuider[] = Form::number($data['menu_name'], $data['info'], $data['value'])->info($data['desc']);
  423. break;
  424. case 'dateTime':
  425. $formbuider[] = Form::dateTime($data['menu_name'], $data['info'], $data['value'])->info($data['desc']);
  426. break;
  427. case 'color':
  428. $data['value'] = json_decode($data['value'], true) ?: '';
  429. $formbuider[] = Form::color($data['menu_name'], $data['info'], $data['value'])->info($data['desc']);
  430. break;
  431. default:
  432. $data['value'] = json_decode($data['value'], true) ?: '';
  433. $formbuider[] = Form::input($data['menu_name'], $data['info'], $data['value'])->info($data['desc'])->placeholder($data['desc'])->col(13);
  434. break;
  435. }
  436. break;
  437. case 'textarea'://多行文本框
  438. $data['value'] = json_decode($data['value'], true) ?: '';
  439. $formbuider[] = Form::textarea($data['menu_name'], $data['info'], $data['value'])->placeholder($data['desc'])->info($data['desc'])->rows(6)->col(13);
  440. break;
  441. case 'radio'://单选框
  442. $data['value'] = json_decode($data['value'], true) ?: '0';
  443. $parameter = explode("\n", $data['parameter']);
  444. $options = [];
  445. if ($parameter) {
  446. foreach ($parameter as $v) {
  447. $pdata = explode("=>", $v);
  448. $options[] = ['label' => $pdata[1], 'value' => $pdata[0]];
  449. }
  450. $formbuider[] = Form::radio($data['menu_name'], $data['info'], $data['value'])->options($options)->info($data['desc'])->col(13);
  451. }
  452. break;
  453. case 'upload'://文件上传
  454. switch ($data['upload_type']) {
  455. case 1:
  456. $data['value'] = json_decode($data['value'], true) ?: '';
  457. $formbuider[] = Form::frameImageOne($data['menu_name'], $data['info'], Url::buildUrl('admin/widget.images/index', array('fodder' => $data['menu_name'])), $data['value'])->icon('image')->width('70%')->height('500px')->info($data['desc'])->col(13);
  458. break;
  459. case 2:
  460. $data['value'] = json_decode($data['value'], true) ?: [];
  461. $formbuider[] = Form::frameImages($data['menu_name'], $data['info'], Url::buildUrl('admin/widget.images/index', array('fodder' => $data['menu_name'])), $data['value'])->maxLength(5)->icon('image')->width('70%')->height('500px')->info($data['desc'])->col(13);
  462. break;
  463. case 3:
  464. $data['value'] = json_decode($data['value'], true);
  465. $formbuider[] = Form::uploadFileOne($data['menu_name'], $data['info'], Url::buildUrl('file_upload'), $data['value'])->name('file')->info($data['desc'])->col(13);
  466. break;
  467. }
  468. break;
  469. case 'checkbox'://多选框
  470. $data['value'] = json_decode($data['value'], true) ?: [];
  471. $parameter = explode("\n", $data['parameter']);
  472. $options = [];
  473. if ($parameter) {
  474. foreach ($parameter as $v) {
  475. $pdata = explode("=>", $v);
  476. $options[] = ['label' => $pdata[1], 'value' => $pdata[0]];
  477. }
  478. $formbuider[] = Form::checkbox($data['menu_name'], $data['info'], $data['value'])->options($options)->info($data['desc'])->col(13);
  479. }
  480. break;
  481. case 'select'://多选框
  482. $data['value'] = json_decode($data['value'], true) ?: [];
  483. $parameter = explode("\n", $data['parameter']);
  484. $options = [];
  485. if ($parameter) {
  486. foreach ($parameter as $v) {
  487. $pdata = explode("=>", $v);
  488. $options[] = ['label' => $pdata[1], 'value' => $pdata[0]];
  489. }
  490. $formbuider[] = Form::select($data['menu_name'], $data['info'], $data['value'])->options($options)->info($data['desc'])->col(13);
  491. }
  492. break;
  493. }
  494. }
  495. return $formbuider;
  496. }
  497. /**
  498. * 配置短信信息
  499. * @param $account
  500. * @param $token
  501. * @return bool
  502. */
  503. public static function setConfigSmsInfo($account, $token)
  504. {
  505. self::beginTrans();
  506. $res1 = self::where('menu_name', 'sms_account')->where('value', '"' . $account . '"')->count();
  507. if (!$res1) $res1 = self::where('menu_name', 'sms_account')->update(['value' => '"' . $account . '"']);
  508. $res2 = self::where('menu_name', 'sms_token')->where('value', '"' . $token . '"')->count();
  509. if (!$res2) $res2 = self::where('menu_name', 'sms_token')->update(['value' => '"' . $token . '"']);
  510. $res = $res1 && $res2;
  511. self::checkTrans($res);
  512. return $res;
  513. }
  514. }