UserArticlesController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: pl125 <xskjs888@163.com>
  8. // +----------------------------------------------------------------------
  9. namespace api\portal\controller;
  10. use cmf\controller\RestUserBaseController;
  11. use api\portal\logic\PortalPostModel;
  12. class UserArticlesController extends RestUserBaseController
  13. {
  14. /**
  15. * 显示资源列表
  16. */
  17. public function index()
  18. {
  19. $params = $this->request->get();
  20. $userId = $this->getUserId();
  21. $postModel = new PortalPostModel();
  22. $dates = $postModel->getUserArticles($userId, $params);
  23. $this->success('请求成功!', $dates);
  24. }
  25. /**
  26. * 保存新建的资源
  27. */
  28. public function save()
  29. {
  30. $dates = $this->request->post();
  31. $dates['user_id'] = $this->getUserId();
  32. $result = $this->validate($dates, 'Articles.article');
  33. if ($result !== true) {
  34. $this->error($result);
  35. }
  36. if (empty($dates['published_time'])) {
  37. $dates['published_time'] = time();
  38. }
  39. $postModel = new PortalPostModel();
  40. $postModel->addArticle($dates);
  41. $this->success('添加成功!');
  42. }
  43. /**
  44. * 显示指定的资源
  45. * @param $id
  46. */
  47. public function read($id)
  48. {
  49. if (empty($id)) {
  50. $this->error('无效的文章id');
  51. }
  52. $params = $this->request->get();
  53. $params['id'] = $id;
  54. $userId = $this->getUserId();
  55. $postModel = new PortalPostModel();
  56. $dates = $postModel->getUserArticles($userId, $params);
  57. $this->success('请求成功!', $dates);
  58. }
  59. /**
  60. * 保存更新的资源
  61. * @param $id
  62. */
  63. public function update($id)
  64. {
  65. $data = $this->request->put();
  66. $result = $this->validate($data, 'Articles.article');
  67. if ($result !== true) {
  68. $this->error($result);
  69. }
  70. if (empty($id)) {
  71. $this->error('无效的文章id');
  72. }
  73. $postModel = new PortalPostModel();
  74. $result = $postModel->editArticle($data, $id, $this->getUserId());
  75. if ($result === false) {
  76. $this->error('编辑失败!');
  77. } else {
  78. $this->success('编辑成功!');
  79. }
  80. }
  81. /**
  82. * 删除指定资源
  83. * @param $id
  84. */
  85. public function delete($id)
  86. {
  87. if (empty($id)) {
  88. $this->error('无效的文章id');
  89. }
  90. $postModel = new PortalPostModel();
  91. $result = $postModel->deleteArticle($id, $this->getUserId());
  92. if ($result == -1) {
  93. $this->error('文章已删除');
  94. }
  95. if ($result) {
  96. $this->success('删除成功!');
  97. } else {
  98. $this->error('删除失败!');
  99. }
  100. }
  101. /**
  102. * 批量删除文章
  103. */
  104. public function deletes()
  105. {
  106. $ids = $this->request->post('ids/a');
  107. if (empty($ids)) {
  108. $this->error('文章id不能为空');
  109. }
  110. $postModel = new PortalPostModel();
  111. $result = $postModel->deleteArticle($ids, $this->getUserId());
  112. if ($result == -1) {
  113. $this->error('文章已删除');
  114. }
  115. if ($result) {
  116. $this->success('删除成功!');
  117. } else {
  118. $this->error('删除失败!');
  119. }
  120. }
  121. /**
  122. * 我的文章列表
  123. */
  124. public function my()
  125. {
  126. $params = $this->request->get();
  127. $userId = $this->getUserId();
  128. $postModel = new PortalPostModel();
  129. $data = $postModel->getUserArticles($userId, $params);
  130. $this->success('请求成功!', $data);
  131. }
  132. }