Keywords.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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\KeywordsCheck;
  11. use think\exception\ValidateException;
  12. use think\facade\Db;
  13. use think\facade\View;
  14. class Keywords extends BaseController
  15. {
  16. public function index()
  17. {
  18. if (request()->isAjax()) {
  19. $param = get_params();
  20. $where = array();
  21. if (!empty($param['keywords'])) {
  22. $where[] = ['title', 'like', '%' . $param['keywords'] . '%'];
  23. }
  24. $where[] = ['status', '>=', 0];
  25. $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
  26. $content = Db::name('Keywords')
  27. ->order('create_time desc')
  28. ->where($where)
  29. ->paginate($rows, false, ['query' => $param]);
  30. return table_assign(0, '', $content);
  31. } else {
  32. return view();
  33. }
  34. }
  35. //添加
  36. public function add()
  37. {
  38. $param = get_params();
  39. if (request()->isAjax()) {
  40. if (!empty($param['id']) && $param['id'] > 0) {
  41. try {
  42. validate(KeywordsCheck::class)->scene('edit')->check($param);
  43. } catch (ValidateException $e) {
  44. // 验证失败 输出错误信息
  45. return to_assign(1, $e->getError());
  46. }
  47. $param['update_time'] = time();
  48. $res = Db::name('Keywords')->strict(false)->field(true)->update($param);
  49. if ($res) {
  50. add_log('edit', $param['id'], $param);
  51. }
  52. return to_assign();
  53. } else {
  54. try {
  55. validate(KeywordsCheck::class)->scene('add')->check($param);
  56. } catch (ValidateException $e) {
  57. // 验证失败 输出错误信息
  58. return to_assign(1, $e->getError());
  59. }
  60. $param['create_time'] = time();
  61. $insertId = Db::name('Keywords')->strict(false)->field(true)->insertGetId($param);
  62. if ($insertId) {
  63. add_log('add', $insertId, $param);
  64. }
  65. return to_assign();
  66. }
  67. }
  68. else{
  69. $id = isset($param['id']) ? $param['id'] : 0;
  70. if ($id > 0) {
  71. $keywords = Db::name('Keywords')->where(['id' => $id])->find();
  72. View::assign('keywords', $keywords);
  73. }
  74. View::assign('id', $id);
  75. return view();
  76. }
  77. }
  78. //删除
  79. public function delete()
  80. {
  81. $id = get_params("id");
  82. $data['status'] = '-1';
  83. $data['id'] = $id;
  84. $data['update_time'] = time();
  85. if (Db::name('Keywords')->update($data) !== false) {
  86. add_log('delete', $id, $data);
  87. return to_assign(0, "删除成功");
  88. } else {
  89. return to_assign(1, "删除失败");
  90. }
  91. }
  92. }