SystemConfig.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace App\Model;
  3. use Illuminate\Database\Eloquent\Model;
  4. /**
  5. * Class System_Config
  6. * @package App
  7. */
  8. class SystemConfig extends Model
  9. {
  10. //
  11. protected $table = "system_config";
  12. protected $guarded = [];
  13. static $groups = [
  14. 'ali_config'=>'阿里云',
  15. 'lock_config'=>'锁配置',
  16. ];
  17. const Field_textarea = 0,Filed_richText = 1,Field_Json = 2,
  18. Field_Switch = 3,Field_Time = 4,Field_File = 5,
  19. Field_Checkbox = 6,Field_Json_Array = 7,Field_Image = 8;
  20. private static $_fieldType = [
  21. self::Field_textarea =>'纯文本',
  22. self::Filed_richText =>'富文本',
  23. self::Field_Json =>'JSON',
  24. self::Field_Switch =>'开关',
  25. self::Field_Time =>'时间',
  26. self::Field_File =>'文件',
  27. self::Field_Checkbox =>'选择框',
  28. self::Field_Json_Array =>'数组JSON',
  29. self::Field_Image =>'图片',
  30. ];
  31. protected static function getType(){
  32. return self::$_fieldType;
  33. }
  34. /**
  35. * @param String $group
  36. * @param String $key
  37. * @param String $default
  38. * @return array|String
  39. */
  40. public static function get(string $group, string $key = '', string $default = "")
  41. {
  42. $query = self::where('group', $group);
  43. if ($key) {
  44. $res = $query->where('key', $key)->first(['value'])->value ?? $default;
  45. return $res;
  46. } else {
  47. $res = $query->get(['key', 'value']);
  48. $arr = [];
  49. foreach ($res as $index => $item) {
  50. $arr[$item['key']] = $item['value'];
  51. }
  52. return $arr;
  53. }
  54. }
  55. /**
  56. * @param String $group
  57. * @param String $key
  58. * @param String $value
  59. * @return int
  60. */
  61. public static function set(string $group, string $key, string $value)
  62. {
  63. return self::where([['group', $group], ['key', $key]])->update(['value' => $value]);
  64. }
  65. }