Config.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace app\agent\model\system;
  3. use app\common\model\BaseModel;
  4. use think\facade\Cache;
  5. class Config extends BaseModel
  6. {
  7. /**
  8. * @var string
  9. */
  10. protected $name = 'agent_system_config';
  11. /**
  12. * @var array
  13. */
  14. protected $insert = ['created_at','updated_at'];
  15. /**
  16. * @var array
  17. */
  18. protected $update = ['updated_at'];
  19. /**
  20. * 解析配置提示
  21. *
  22. * @author 许祖兴 < zuxing.xu@lettered.cn>
  23. * @date 2020/3/21 14:31
  24. *
  25. * @param string $value
  26. * @return string
  27. */
  28. public function getTipsAttr($value)
  29. {
  30. return htmlspecialchars_decode($value);
  31. }
  32. /**
  33. * 模型事件定义
  34. *
  35. * @author 许祖兴 < zuxing.xu@lettered.cn>
  36. * @date 2020/3/26 20:54
  37. *
  38. * @return void
  39. */
  40. public static function init()
  41. {
  42. self::event('before_insert', function ($before) {
  43. });
  44. }
  45. /**
  46. * 取配置
  47. *
  48. * @author 许祖兴 < zuxing.xu@lettered.cn>
  49. * @date 2020/3/16 10:59
  50. *
  51. * @param string $name
  52. * @param bool $update
  53. * @return array|bool|mixed
  54. */
  55. public function getAppConfig($name = '', $update = false)
  56. {
  57. // 读取缓存
  58. $result = Cache::get('agent_system_config');
  59. if ($result === false || $update == true) {
  60. $configs = self::column('value,type,group', 'name');
  61. // 解析配置
  62. $result = self::parseConfig($configs);
  63. // 写入缓存
  64. Cache::set('agent_system_config', $result);
  65. }
  66. return $name != '' ? $result[$name] : $result;
  67. }
  68. /**
  69. * 解析配置
  70. *
  71. * @author 许祖兴 < zuxing.xu@lettered.cn>
  72. * @date 2020/3/28 12:26
  73. *
  74. * @param $configs
  75. *
  76. * @return mixed
  77. */
  78. public function parseConfig($configs)
  79. {
  80. $result = [];
  81. foreach ($configs as $config) {
  82. $config['value'] = htmlspecialchars_decode($config['value']);
  83. switch ($config['type']) {
  84. case 'array':
  85. case 'checkbox':
  86. if ($config['name'] == 'config_group' || $config['name'] == 'config_type') {
  87. $v = parse_attr($config['value']);
  88. if (!empty($config['value'])) {
  89. $result[$config['group']][$config['name']] = array_merge(config($config['name'] == 'config_type'
  90. ? 'mni.config_type' : 'mni.config_group'), $v);
  91. } else {
  92. $result[$config['group']][$config['name']] = config($config['name'] == 'config_type'
  93. ? 'mni.config_type' : 'mni.config_group');
  94. }
  95. } else {
  96. $result[$config['group']][$config['name']] = parse_attr($config['value']);
  97. }
  98. break;
  99. default:
  100. $result[$config['group']][$config['name']] = $config['value'];
  101. break;
  102. }
  103. }
  104. return $result;
  105. }
  106. }