common.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. use think\admin\model\SystemConfig;
  3. function systemConfig($name,$value=null){
  4. $nameArr=array_filter(explode('.',$name));
  5. if(is_null($value)){
  6. if(count($nameArr)==1) {
  7. $source = SystemConfig::where('type', $nameArr[0])->select();
  8. $data=[];
  9. foreach ($source as $model){
  10. $value=$model['value'];
  11. if($decode=json_decode($value,true)){
  12. $value=$decode;
  13. }
  14. $data[$model['name']]=$value;
  15. }
  16. return $data?:null;
  17. }else{
  18. $model = SystemConfig::where('type', $nameArr[0])->where('name',$nameArr[1])->find();
  19. $value=$model['value'];
  20. if($decode=json_decode($value,true)){
  21. $value=$decode;
  22. }
  23. return $value;
  24. }
  25. }else{
  26. SystemConfig::where('type', $nameArr[0])->delete();
  27. if(count($nameArr)==1) {
  28. if(is_array($value)){
  29. foreach ($value as $key=>$val){
  30. SystemConfig::insert([
  31. 'type'=>$nameArr[0],
  32. 'name'=>$key,
  33. 'value'=>is_array($val)?json_encode($val,256):$val,
  34. ]);
  35. }
  36. }
  37. }else{
  38. SystemConfig::insert([
  39. 'type'=>$nameArr[0],
  40. 'name'=>$nameArr[1],
  41. 'value'=>$value,
  42. ]);
  43. }
  44. }
  45. }
  46. /**
  47. * 生成??位随机数
  48. */
  49. function get32Str($length='32'){
  50. // $str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  51. $str = 'abcdefghijklmnopqrstuvwxyz0123456789';
  52. $len = strlen($str)-1;
  53. $randstr = '';
  54. for ($i=0;$i<$length;$i++) {
  55. $num=mt_rand(0,$len);
  56. $randstr .= $str[$num];
  57. }
  58. return $randstr;
  59. }