Article.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. ['is_show', 0]
  38. ]);
  39. $data['add_time'] = time();
  40. if ($data['id']) {
  41. $id = $data['id'];
  42. unset($data['id']);
  43. $res = ArticleModel::edit($data, $id, 'id');
  44. if($res)
  45. return $this->success('修改成功!', ['id' => $id]);
  46. else
  47. return $this->fail('修改失败!');
  48. } else {
  49. $res = ArticleModel::create($data);
  50. return $this->success('添加成功!', ['id' => $res->id]);
  51. }
  52. }
  53. /**
  54. * 显示指定的资源
  55. *
  56. * @param int $id
  57. * @return \think\Response
  58. */
  59. public function read($id)
  60. {
  61. $info = ArticleModel::getOne($id);
  62. return $this->success(compact('info'));
  63. }
  64. /**
  65. * 删除指定资源
  66. *
  67. * @param int $id
  68. * @return \think\Response
  69. */
  70. public function delete($id)
  71. {
  72. if (!$id) return $this->fail('数据不存在');
  73. $res = ArticleModel::get($id);
  74. if (!$res) return $this->fail('数据不存在!');
  75. if ($res['is_del']) return $this->fail('已删除!');
  76. $data['is_del'] = 1;
  77. if (ArticleModel::edit($data, $id))
  78. return $this->success('删除成功!');
  79. else
  80. return $this->fail('删除失败,请稍候再试!');
  81. }
  82. }