Conf.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------------------------------
  4. * GouGuOPEN [ 左手研发,右手开源,未来可期!]
  5. +-----------------------------------------------------------------------------------------------
  6. * @Copyright (c) 2021~2024 http://www.gouguoa.com All rights reserved.
  7. +-----------------------------------------------------------------------------------------------
  8. * @Licensed 勾股OA,开源且可免费使用,但并不是自由软件,未经授权许可不能去除勾股OA的相关版权信息
  9. +-----------------------------------------------------------------------------------------------
  10. * @Author 勾股工作室 <hdm58@qq.com>
  11. +-----------------------------------------------------------------------------------------------
  12. */
  13. declare (strict_types = 1);
  14. namespace app\home\controller;
  15. use app\base\BaseController;
  16. use app\home\validate\ConfCheck;
  17. use think\exception\ValidateException;
  18. use think\facade\Db;
  19. use think\facade\View;
  20. class Conf extends BaseController
  21. {
  22. public function index()
  23. {
  24. if (request()->isAjax()) {
  25. $param = get_params();
  26. $where = array();
  27. $where[] = ['status', '>=', 0];
  28. $list = Db::name('Config')->where($where)->select();
  29. return to_assign(0, '', $list);
  30. } else {
  31. return view();
  32. }
  33. }
  34. //添加/编辑配置项
  35. public function add()
  36. {
  37. $param = get_params();
  38. if (request()->isAjax()) {
  39. try {
  40. validate(ConfCheck::class)->check($param);
  41. } catch (ValidateException $e) {
  42. // 验证失败 输出错误信息
  43. return to_assign(1, $e->getError());
  44. }
  45. if (!empty($param['id']) && $param['id'] > 0) {
  46. $param['update_time'] = time();
  47. $res = Db::name('config')->strict(false)->field(true)->update($param);
  48. if ($res) {
  49. add_log('edit', $param['id'], $param);
  50. }
  51. return to_assign();
  52. } else {
  53. $param['create_time'] = time();
  54. $insertId = Db::name('Config')->strict(false)->field(true)->insertGetId($param);
  55. if ($insertId) {
  56. add_log('add', $insertId, $param);
  57. }
  58. return to_assign();
  59. }
  60. } else {
  61. $id = isset($param['id']) ? $param['id'] : 0;
  62. if ($id > 0) {
  63. $config = Db::name('Config')->where(['id' => $id])->find();
  64. View::assign('config', $config);
  65. }
  66. View::assign('id', $id);
  67. return view();
  68. }
  69. }
  70. //删除配置项
  71. public function delete()
  72. {
  73. $id = get_params("id");
  74. $data['status'] = '-1';
  75. $data['id'] = $id;
  76. $data['update_time'] = time();
  77. if (Db::name('Config')->update($data) !== false) {
  78. add_log('delete', $id, $data);
  79. return to_assign(0, "删除成功");
  80. } else {
  81. return to_assign(1, "删除失败");
  82. }
  83. }
  84. //编辑配置信息
  85. public function edit()
  86. {
  87. $param = get_params();
  88. if (request()->isAjax()) {
  89. $data['content'] = serialize($param);
  90. $data['update_time'] = time();
  91. $data['id'] = $param['id'];
  92. $res = Db::name('Config')->strict(false)->field(true)->update($data);
  93. $conf = Db::name('Config')->where('id', $param['id'])->find();
  94. clear_cache('system_config' . $conf['name']);
  95. if ($res) {
  96. add_log('edit', $param['id'], $param);
  97. }
  98. return to_assign();
  99. } else {
  100. $id = isset($param['id']) ? $param['id'] : 0;
  101. $conf = Db::name('Config')->where('id', $id)->find();
  102. $module = strtolower(app('http')->getName());
  103. $class = strtolower(app('request')->controller());
  104. $action = strtolower(app('request')->action());
  105. $template = $module . '/view/' . $class . '/' . $conf['name'] . '.html';
  106. $config = [];
  107. if ($conf['content']) {
  108. $config = unserialize($conf['content']);
  109. }
  110. View::assign('id', $id);
  111. View::assign('config', $config);
  112. if (isTemplate($template)) {
  113. return view($conf['name']);
  114. } else {
  115. return view('../../base/view/common/errortemplate', ['file' => $template]);
  116. }
  117. }
  118. }
  119. }