Setting.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace app\common\models;
  10. class Setting extends Model {
  11. protected $name = "setting";
  12. /**
  13. * 保存数据
  14. * @param $name
  15. * @param null $value
  16. * @return bool
  17. */
  18. public static function saveData($name,$value=null){
  19. if(is_array($name)){
  20. foreach($name as $key=>$item){
  21. $item = self::getFilterSettingData($item);
  22. if(self::where("name",$key)->count()){
  23. self::where("name",$key)->save([ "value"=>$item ]);
  24. }else{
  25. self::create([ "name"=>$key, "value"=>$item ]);
  26. }
  27. }
  28. return true;
  29. }
  30. $value = self::getFilterSettingData($value);
  31. if(self::where("name",$name)->count()){
  32. self::where("name",$name)->save([ "value"=>$value ]);
  33. }else{
  34. self::create([ "name"=>$name, "value"=>$value ]);
  35. }
  36. return true;
  37. }
  38. protected static function getFilterSettingData($data){
  39. $value = self::filterSpace($data);
  40. return is_array($value) ? json_encode($value,JSON_UNESCAPED_UNICODE) : $value;
  41. }
  42. /**
  43. * 过滤空格
  44. * @param $data
  45. * @return array|false|mixed|string
  46. */
  47. protected static function filterSpace($data){
  48. if(is_array($data)){
  49. foreach($data as $k=>$v){
  50. $data[$k] = is_array($v) ? self::filterSpace($v) : trim($v);
  51. }
  52. }else{
  53. $data = trim($data);
  54. }
  55. return $data;
  56. }
  57. /**
  58. * 该方法用于获取字符串配置内容
  59. * @param string|null $name
  60. * @return mixed
  61. * @throws \think\db\exception\DataNotFoundException
  62. * @throws \think\db\exception\DbException
  63. * @throws \think\db\exception\ModelNotFoundException
  64. */
  65. public static function getStringData($name){
  66. $row = self::where("name",$name)->find();
  67. return $row["value"];
  68. }
  69. /**
  70. * 该方法用于获取数组配置内容
  71. * @param $name
  72. * @return array|mixed|null
  73. * @throws \think\db\exception\DataNotFoundException
  74. * @throws \think\db\exception\DbException
  75. * @throws \think\db\exception\ModelNotFoundException
  76. */
  77. public static function getArrayData($name){
  78. $keys = explode(".",$name);
  79. $name = array_shift($keys);
  80. $row = self::where("name",$name)->find();
  81. $array = json_decode($row["value"],true);
  82. if(empty($array) || empty($keys)){
  83. return $array ?? null;
  84. }
  85. $string = $_string = null;
  86. for($i=0; $i<count($keys);$i++){
  87. if(empty($string)){
  88. $string = empty($array[$keys[$i]]) ? null : $array[$keys[$i]];
  89. }else{
  90. $_string = $string[$keys[$i]];
  91. $string = $_string;
  92. }
  93. }
  94. return $string;
  95. }
  96. }