Links.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021 勾股工作室
  4. * @license https://opensource.org/licenses/Apache-2.0
  5. * @link https://blog.gougucms.com
  6. */
  7. declare (strict_types = 1);
  8. namespace app\admin\controller;
  9. use app\admin\BaseController;
  10. use app\admin\model\Links as linksList;
  11. use app\admin\validate\linksCheck;
  12. use think\exception\ValidateException;
  13. use think\facade\Db;
  14. use think\facade\View;
  15. class links extends BaseController
  16. {
  17. public function index()
  18. {
  19. if (request()->isAjax()) {
  20. $param = get_params();
  21. $where = array();
  22. if (!empty($param['keywords'])) {
  23. $where[] = ['id|name', 'like', '%' . $param['keywords'] . '%'];
  24. }
  25. $where[] = ['status', '=', 1];
  26. $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
  27. $links = linksList::where($where)
  28. ->order('sort desc, id desc')
  29. ->paginate($rows, false, ['query' => $param]);
  30. return table_assign(0, '', $links);
  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(linksCheck::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 = linksList::where('id', $param['id'])->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(linksCheck::class)->scene('add')->check($param);
  56. } catch (ValidateException $e) {
  57. // 验证失败 输出错误信息
  58. return to_assign(1, $e->getError());
  59. }
  60. $param['create_time'] = time();
  61. $sid = linksList::strict(false)->field(true)->insertGetId($param);
  62. if ($sid) {
  63. add_log('add', $sid, $param);
  64. }
  65. return to_assign();
  66. }
  67. }
  68. else{
  69. $id = empty($param['id']) ? 0 : $param['id'];
  70. if ($id > 0) {
  71. $links = Db::name('Links')->where(['id' => $id])->find();
  72. View::assign('links', $links);
  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('Links')->update($data) !== false) {
  86. add_log('delete', $id);
  87. return to_assign(0, "删除成功");
  88. } else {
  89. return to_assign(1, "删除失败");
  90. }
  91. }
  92. }