Conf.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021 勾股工作室
  4. * @license https://opensource.org/licenses/Apache-2.0
  5. * @link https://www.gougucms.com
  6. */
  7. declare (strict_types = 1);
  8. namespace app\admin\controller;
  9. use app\admin\BaseController;
  10. use app\admin\validate\ConfCheck;
  11. use think\exception\ValidateException;
  12. use think\facade\Db;
  13. use think\facade\View;
  14. class Conf extends BaseController
  15. {
  16. public function index()
  17. {
  18. if (request()->isAjax()) {
  19. $param = get_params();
  20. $where = array();
  21. $where[] = ['status', '>=', 0];
  22. $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
  23. $content = Db::name('Config')
  24. ->where($where)
  25. ->paginate($rows, false, ['query' => $param]);
  26. return table_assign(0, '', $content);
  27. } else {
  28. return view();
  29. }
  30. }
  31. //添加/编辑配置项
  32. public function add()
  33. {
  34. $param = get_params();
  35. if (request()->isAjax()) {
  36. try {
  37. validate(ConfCheck::class)->check($param);
  38. } catch (ValidateException $e) {
  39. // 验证失败 输出错误信息
  40. return to_assign(1, $e->getError());
  41. }
  42. if (!empty($param['id']) && $param['id'] > 0) {
  43. $param['update_time'] = time();
  44. $res = Db::name('config')->strict(false)->field(true)->update($param);
  45. if ($res) {
  46. add_log('edit', $param['id'], $param);
  47. }
  48. return to_assign();
  49. } else {
  50. $param['create_time'] = time();
  51. $insertId = Db::name('Config')->strict(false)->field(true)->insertGetId($param);
  52. if ($insertId) {
  53. add_log('add', $insertId, $param);
  54. }
  55. return to_assign();
  56. }
  57. } else {
  58. $id = isset($param['id']) ? $param['id'] : 0;
  59. if ($id > 0) {
  60. $config = Db::name('Config')->where(['id' => $id])->find();
  61. View::assign('config', $config);
  62. }
  63. View::assign('id', $id);
  64. return view();
  65. }
  66. }
  67. //删除配置项
  68. public function delete()
  69. {
  70. $id = get_params("id");
  71. $data['status'] = '-1';
  72. $data['id'] = $id;
  73. $data['update_time'] = time();
  74. if (Db::name('Config')->update($data) !== false) {
  75. add_log('delete', $id, $data);
  76. return to_assign(0, "删除成功");
  77. } else {
  78. return to_assign(1, "删除失败");
  79. }
  80. }
  81. //编辑配置信息
  82. public function edit()
  83. {
  84. $param = get_params();
  85. if (request()->isAjax()) {
  86. $data['content'] = serialize($param);
  87. $data['update_time'] = time();
  88. $data['id'] = $param['id'];
  89. $res = Db::name('Config')->strict(false)->field(true)->update($data);
  90. $conf = Db::name('Config')->where('id', $param['id'])->find();
  91. clear_cache('system_config' . $conf['name']);
  92. if ($res) {
  93. add_log('edit', $param['id'], $param);
  94. }
  95. return to_assign();
  96. } else {
  97. $id = isset($param['id']) ? $param['id'] : 0;
  98. $conf = Db::name('Config')->where('id', $id)->find();
  99. $module = strtolower(app('http')->getName());
  100. $class = strtolower(app('request')->controller());
  101. $action = strtolower(app('request')->action());
  102. $template = $module . '/view/'. $class .'/'.$conf['name'].'.html';
  103. $config = [];
  104. if ($conf['content']) {
  105. $config = unserialize($conf['content']);
  106. }
  107. View::assign('id', $id);
  108. View::assign('config', $config);
  109. if(isTemplate($template)){
  110. return view($conf['name']);
  111. }else{
  112. return view('common/errortemplate',['file' =>$template]);
  113. }
  114. }
  115. }
  116. }