Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Yzncms [ 御宅男工作室 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018 http://yzncms.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 御宅男 <530765310@qq.com>
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 后台配置模型
  13. // +----------------------------------------------------------------------
  14. namespace app\admin\model;
  15. use think\Model;
  16. class Config extends Model
  17. {
  18. // 追加属性
  19. protected $append = [
  20. 'extend_html',
  21. ];
  22. // 自动写入时间戳
  23. protected $autoWriteTimestamp = true;
  24. public function getExtendHtmlAttr($value, $data)
  25. {
  26. $result = preg_replace_callback("/\{([a-zA-Z]+)\}/", function ($matches) use ($data) {
  27. if (isset($data[$matches[1]])) {
  28. return $data[$matches[1]];
  29. }
  30. }, $data['extend']);
  31. return $result;
  32. }
  33. /**
  34. * 刷新配置文件
  35. */
  36. public static function refreshFile()
  37. {
  38. $configs = self::where('status', 1)->column('name,value,type,options');
  39. $newConfigs = [];
  40. foreach ($configs as $key => $value) {
  41. if ($value['options'] != '') {
  42. $value['options'] = parse_attr($value['options']);
  43. }
  44. switch ($value['type']) {
  45. case 'array':
  46. $newConfigs[$key] = (array) json_decode($value['value'], true);
  47. break;
  48. case 'select':
  49. case 'radio':
  50. $newConfigs[$key] = $value['value'];
  51. if (isset($value['options'][$value['value']])) {
  52. $newConfigs[$key . '_text'] = $value['options'][$value['value']];
  53. } else {
  54. $newConfigs[$key . '_text'] = $value['value'];
  55. }
  56. //$newConfigs[$key] = isset($value['options'][$value['value']]) ? ['key' => $value['value'], 'value' => $value['options'][$value['value']]] : ['key' => $value['value'], 'value' => $value['value']];
  57. break;
  58. case 'selects':
  59. case 'checkbox':
  60. if (empty($value['value'])) {
  61. $newConfigs[$key] = [];
  62. } else {
  63. $valueArr = explode(',', $value['value']);
  64. foreach ($valueArr as $v) {
  65. if (isset($value['options'][$v])) {
  66. $newConfigs[$key][$v] = $value['options'][$v];
  67. } elseif ($v) {
  68. $newConfigs[$key][$v] = $v;
  69. }
  70. }
  71. }
  72. break;
  73. case 'files':
  74. case 'images':
  75. $newConfigs[$key] = empty($value['value']) ? [] : explode(',', $value['value']);
  76. break;
  77. case 'Ueditor':
  78. $newConfigs[$key] = htmlspecialchars_decode($value['value']);
  79. break;
  80. default:
  81. $newConfigs[$key] = $value['value'];
  82. break;
  83. }
  84. }
  85. file_put_contents(
  86. ROOT_PATH . 'config' . DS . 'site.php',
  87. '<?php' . "\n\nreturn " . var_export_short($newConfigs) . ";\n"
  88. );
  89. return true;
  90. }
  91. }