SystemConfig.php 20 KB

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