SystemConfig.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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\model\system;
  12. use basic\ModelBasic;
  13. use service\FormBuilder as Form;
  14. use traits\ModelTrait;
  15. class SystemConfig extends ModelBasic
  16. {
  17. use ModelTrait;
  18. /**
  19. * 修改单个配置
  20. * */
  21. public static function setValue($menu,$value){
  22. if(empty($menu) || !($config_one = self::get(['menu_name'=>$menu]))) return self::setErrorInfo('字段名称错误');
  23. if($config_one['type'] == 'radio' || $config_one['type'] == 'checkbox'){
  24. $parameter = array();
  25. $option = array();
  26. $parameter = explode(',',$config_one['parameter']);
  27. foreach ($parameter as $k=>$v){
  28. if(isset($v) && !empty($v)){
  29. $option[$k] = explode('-',$v);
  30. }
  31. }
  32. $value_arr = array();//选项的值
  33. foreach ($option as $k=>$v){
  34. foreach ($v as $kk=>$vv)
  35. if(!$kk){
  36. $value_arr[$k] = $vv;
  37. }
  38. }
  39. $i = 0;
  40. if(is_array($value)){
  41. foreach ($value as $value_v){
  42. if(in_array($value_v,$value_arr)){
  43. $i++;
  44. }
  45. }
  46. if(count($value) != $i) return self::setErrorInfo('输入的值不属于选项中的参数');
  47. }else{
  48. if(in_array($value,$value_arr)){
  49. $i++;
  50. }
  51. if(!$i) return self::setErrorInfo('输入的值不属于选项中的参数');
  52. }
  53. if($config_one['type'] == 'radio' && is_array($value)) return self::setErrorInfo('单选按钮的值是字符串不是数组');
  54. }
  55. $bool = self::edit(['value' => json_encode($value)],$menu,'menu_name');
  56. return $bool;
  57. }
  58. /**
  59. * 获取单个参数配置
  60. * */
  61. public static function getValue($menu){
  62. if(empty($menu) || !($config_one = self::get(['menu_name'=>$menu]))) return false;
  63. return json_decode($config_one['value'],true);
  64. }
  65. /**
  66. * 获取所有配置分类
  67. * @param int $type
  68. * @return array
  69. */
  70. public static function getConfigChildrenTabAll($pid = 0)
  71. {
  72. $configAll = SystemConfigTab::getChildrenTab($pid);
  73. $config_tab = [];
  74. foreach ($configAll as $k => $v) {
  75. if (!$v['info']) {
  76. $config_tab[$k]['id'] = $v['id'];
  77. $config_tab[$k]['label'] = $v['title'];
  78. $config_tab[$k]['icon'] = $v['icon'];
  79. $config_tab[$k]['type'] = $v['type'];
  80. }
  81. }
  82. return $config_tab;
  83. }
  84. /**
  85. * 获得多个参数
  86. * @param $menus
  87. * @return array
  88. */
  89. public static function getMore($menus)
  90. {
  91. $menus = is_array($menus) ? implode(',',$menus) : $menus;
  92. $list = self::where('menu_name','IN',$menus)->column('value','menu_name')?:[];
  93. foreach ($list as $menu => $value){
  94. $value= json_decode($value,true);
  95. $list[$menu]=is_array($value) && $value[0] ? $value[0] : $value;
  96. }
  97. return $list;
  98. }
  99. public static function getAllConfig()
  100. {
  101. $list = self::column('value','menu_name')?:[];
  102. foreach ($list as $menu => $value){
  103. $list[$menu] = json_decode($value,true);
  104. }
  105. return $list;
  106. }
  107. /**
  108. * text 判断
  109. * */
  110. public static function valiDateTextRole($data){
  111. if (!$data['width']) return self::setErrorInfo('请输入文本框的宽度');
  112. if ($data['width'] <= 0) return self::setErrorInfo('请输入正确的文本框的宽度');
  113. return true;
  114. }
  115. /**
  116. * radio 和 checkbox规则的判断
  117. * */
  118. public static function valiDateRadioAndCheckbox($data){
  119. $parameter = array();
  120. $option = array();
  121. $option_new = array();
  122. $data['parameter'] = str_replace("\r\n","\n",$data['parameter']);//防止不兼容
  123. $parameter = explode("\n",$data['parameter']);
  124. if(count($parameter) < 2)return self::setErrorInfo('请输入正确格式的配置参数');
  125. foreach ($parameter as $k=>$v){
  126. if(isset($v) && !empty($v)){
  127. $option[$k] = explode('=',$v);
  128. }
  129. }
  130. if(count($option) < 2)return self::setErrorInfo('请输入正确格式的配置参数');
  131. $bool = 1;
  132. foreach ($option as $k=>$v){
  133. $option_new[$k] = $option[$k][0];
  134. foreach ($v as $kk=>$vv){
  135. $vv_num = strlen($vv);
  136. if(!$vv_num){
  137. $bool = 0;
  138. }
  139. }
  140. }
  141. if(!$bool)return self::setErrorInfo('请输入正确格式的配置参数');
  142. $num1 = count($option_new);//提取该数组的数目
  143. $arr2 = array_unique($option_new);//合并相同的元素
  144. $num2 = count($arr2);//提取合并后数组个数
  145. if($num1>$num2)return self::setErrorInfo('请输入正确格式的配置参数');
  146. return true;
  147. }
  148. /**
  149. * textarea 判断
  150. * */
  151. public static function valiDateTextareaRole($data){
  152. if (!$data['width']) return self::setErrorInfo('请输入多行文本框的宽度');
  153. if (!$data['high']) return self::setErrorInfo('请输入多行文本框的高度');
  154. if ($data['width'] < 0) return self::setErrorInfo('请输入正确的多行文本框的宽度');
  155. if ($data['high'] < 0) return self::setErrorInfo('请输入正确的多行文本框的宽度');
  156. return true;
  157. }
  158. /**
  159. * 获取一数据
  160. * */
  161. public static function getOneConfig($filed,$value){
  162. $where[$filed] = $value;
  163. return self::where($where)->find();
  164. }
  165. /**
  166. * 获取配置分类
  167. * */
  168. public static function getAll($id){
  169. $where['config_tab_id'] = $id;
  170. $where['status'] = 1;
  171. return self::where($where)->order('sort desc,id asc')->select();
  172. }
  173. /**
  174. * 获取所有配置分类
  175. * @param int $type
  176. * @return array
  177. */
  178. public static function getConfigTabAll($type = 0)
  179. {
  180. $configAll = SystemConfigTab::getAll($type);
  181. $config_tab = [];
  182. foreach ($configAll as $k => $v) {
  183. if (!$v['info']) {
  184. $config_tab[$k]['value'] = $v['id'];
  185. $config_tab[$k]['label'] = $v['title'];
  186. $config_tab[$k]['icon'] = $v['icon'];
  187. $config_tab[$k]['type'] = $v['type'];
  188. }
  189. }
  190. return $config_tab;
  191. }
  192. /**
  193. * 选择类型
  194. * */
  195. public static function radiotype($type='text'){
  196. return [
  197. ['value'=>'text','label'=>'文本框','disabled'=>1]
  198. ,['value'=>'textarea','label'=>'多行文本框','disabled'=>1]
  199. ,['value'=>'radio','label'=>'单选按钮','disabled'=>1]
  200. ,['value'=>'upload','label'=>'文件上传','disabled'=>1]
  201. ,['value'=>'checkbox','label'=>'多选按钮','disabled'=>1]
  202. ];
  203. }
  204. /**
  205. * 文本框
  206. * */
  207. public static function createInputRule($tab_id){
  208. $formbuider = array();
  209. $formbuider[] = Form::radio('type','类型','text')->options(self::radiotype());
  210. $formbuider[] = Form::select('config_tab_id','分类',$tab_id)->setOptions(SystemConfig::getConfigTabAll(-1));
  211. $formbuider[] = Form::input('info','配置名称')->autofocus(1);
  212. $formbuider[] = Form::input('menu_name','字段变量')->placeholder('例如:site_url');
  213. $formbuider[] = Form::input('desc','配置简介');
  214. $formbuider[] = Form::input('value','默认值');
  215. $formbuider[] = Form::number('width','文本框宽(%)',100);
  216. $formbuider[] = Form::input('required','验证规则')->placeholder('多个请用,隔开例如:required:true,url:true');
  217. $formbuider[] = Form::number('sort','排序');
  218. $formbuider[] = Form::radio('status','状态',1)->options([['value'=>1,'label'=>'显示'],['value'=>2,'label'=>'隐藏']]);
  219. return $formbuider;
  220. }
  221. /**
  222. * 多行文本框
  223. * */
  224. public static function createTextAreaRule($tab_id){
  225. $formbuider = array();
  226. $formbuider[] = Form::radio('type','类型','textarea')->options(self::radiotype());
  227. $formbuider[] = Form::select('config_tab_id','分类',$tab_id)->setOptions(SystemConfig::getConfigTabAll(-1));
  228. $formbuider[] = Form::input('info','配置名称')->autofocus(1);
  229. $formbuider[] = Form::input('menu_name','字段变量')->placeholder('例如:site_url');
  230. $formbuider[] = Form::input('desc','配置简介');
  231. $formbuider[] = Form::textarea('value','默认值');
  232. $formbuider[] = Form::number('width','文本框宽(%)',100);
  233. $formbuider[] = Form::number('high','多行文本框高(%)',5);
  234. $formbuider[] = Form::number('sort','排序');
  235. $formbuider[] = Form::radio('status','状态',1)->options([['value'=>1,'label'=>'显示'],['value'=>2,'label'=>'隐藏']]);
  236. return $formbuider;
  237. }
  238. /**
  239. * 单选按钮
  240. * */
  241. public static function createRadioRule($tab_id){
  242. $formbuider = array();
  243. $formbuider[] = Form::radio('type','类型','radio')->options(self::radiotype());
  244. $formbuider[] = Form::select('config_tab_id','分类',$tab_id)->setOptions(SystemConfig::getConfigTabAll(-1));
  245. $formbuider[] = Form::input('info','配置名称')->autofocus(1);
  246. $formbuider[] = Form::input('menu_name','字段变量')->placeholder('例如:site_url');
  247. $formbuider[] = Form::input('desc','配置简介');
  248. $formbuider[] = Form::textarea('parameter','配置参数')->placeholder("参数方式例如:\n1=男\n2=女\n3=保密");
  249. $formbuider[] = Form::input('value','默认值');
  250. $formbuider[] = Form::number('sort','排序');
  251. $formbuider[] = Form::radio('status','状态',1)->options([['value'=>1,'label'=>'显示'],['value'=>2,'label'=>'隐藏']]);
  252. return $formbuider;
  253. }
  254. /**
  255. * 文件上传
  256. * */
  257. public static function createUploadRule($tab_id){
  258. $formbuider = array();
  259. $formbuider[] = Form::radio('type','类型','upload')->options(self::radiotype());
  260. $formbuider[] = Form::select('config_tab_id','分类',$tab_id)->setOptions(SystemConfig::getConfigTabAll(-1));
  261. $formbuider[] = Form::input('info','配置名称')->autofocus(1);
  262. $formbuider[] = Form::input('menu_name','字段变量')->placeholder('例如:site_url');
  263. $formbuider[] = Form::input('desc','配置简介');
  264. $formbuider[] = Form::radio('upload_type','上传类型',1)->options([['value'=>1,'label'=>'单图'],['value'=>2,'label'=>'多图'],['value'=>3,'label'=>'文件']]);
  265. $formbuider[] = Form::number('sort','排序');
  266. $formbuider[] = Form::radio('status','状态',1)->options([['value'=>1,'label'=>'显示'],['value'=>2,'label'=>'隐藏']]);
  267. return $formbuider;
  268. }
  269. /**
  270. * 多选框
  271. * */
  272. public static function createCheckboxRule($tab_id){
  273. $formbuider = array();
  274. $formbuider[] = Form::radio('type','类型','checkbox')->options(self::radiotype());
  275. $formbuider[] = Form::select('config_tab_id','分类',$tab_id)->setOptions(SystemConfig::getConfigTabAll(-1));
  276. $formbuider[] = Form::input('info','配置名称')->autofocus(1);
  277. $formbuider[] = Form::input('menu_name','字段变量')->placeholder('例如:site_url');
  278. $formbuider[] = Form::input('desc','配置简介');
  279. $formbuider[] = Form::textarea('parameter','配置参数')->placeholder("参数方式例如:\n1=白色\n2=红色\n3=黑色");
  280. $formbuider[] = Form::input('value','默认值');
  281. $formbuider[] = Form::number('sort','排序');
  282. $formbuider[] = Form::radio('status','状态',1)->options([['value'=>1,'label'=>'显示'],['value'=>2,'label'=>'隐藏']]);
  283. return $formbuider;
  284. }
  285. /**
  286. * 插入数据到数据库
  287. * */
  288. public static function set($data)
  289. {
  290. return self::create($data);
  291. }
  292. }