Article.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace app\adminapi\controller\v1\article;
  3. use app\adminapi\controller\AuthController;
  4. use app\models\article\ArticleModel;
  5. use crmeb\services\{UtilService as Util};
  6. use think\Request;
  7. class Article extends AuthController
  8. {
  9. /**
  10. * 显示资源列表
  11. *
  12. * @return \think\Response
  13. */
  14. public function index()
  15. {
  16. $where = Util::getMore([
  17. ['page', 1],
  18. ['limit', 10],
  19. ['title', '']
  20. ], $this->request);
  21. $list = ArticleModel::systemPage($where);
  22. return $this->success($list);
  23. }
  24. /**
  25. * 保存新建的资源
  26. *
  27. * @param \think\Request $request
  28. * @return \think\Response
  29. */
  30. public function save(Request $request)
  31. {
  32. $data = Util::postMore([
  33. ['id', 0],
  34. ['title', ''],
  35. ['image', ''],
  36. ['content', '']
  37. ]);
  38. $data['add_time'] = time();
  39. if ($data['id']) {
  40. $id = $data['id'];
  41. unset($data['id']);
  42. $res = ArticleModel::edit($data, $id, 'id');
  43. if($res)
  44. return $this->success('修改成功!', ['id' => $id]);
  45. else
  46. return $this->fail('修改失败!');
  47. } else {
  48. $res = ArticleModel::create($data);
  49. return $this->success('添加成功!', ['id' => $res->id]);
  50. }
  51. }
  52. /**
  53. * 显示指定的资源
  54. *
  55. * @param int $id
  56. * @return \think\Response
  57. */
  58. public function read($id)
  59. {
  60. $info = ArticleModel::getOne($id);
  61. return $this->success(compact('info'));
  62. }
  63. /**
  64. * 删除指定资源
  65. *
  66. * @param int $id
  67. * @return \think\Response
  68. */
  69. public function delete($id)
  70. {
  71. if (!$id) return $this->fail('数据不存在');
  72. $res = ArticleModel::get($id);
  73. if (!$res) return $this->fail('数据不存在!');
  74. if ($res['is_del']) return $this->fail('已删除!');
  75. $data['is_del'] = 1;
  76. if (ArticleModel::edit($data, $id))
  77. return $this->success('删除成功!');
  78. else
  79. return $this->fail('删除失败,请稍候再试!');
  80. }
  81. }