ArticleController.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace App\Http\Controllers\Api\v1;
  3. use App\Http\Controllers\Api\webApp;
  4. use App\Http\Validator\ConsultValidator;
  5. use App\Services\Api\ArticleService;
  6. /**
  7. * 文章管理
  8. * Class ArticleController
  9. * @package App\Http\Controllers\Api
  10. */
  11. class ArticleController extends webApp
  12. {
  13. /**
  14. * 列表
  15. * @return array
  16. */
  17. public function index()
  18. {
  19. $params =request()->post();
  20. $pageSize = request()->post('pageSize', 15);
  21. $datas = ArticleService::make()->getDataList($params, $pageSize);
  22. return message(1010, true, $datas);
  23. }
  24. /**
  25. * 详情
  26. */
  27. public function info()
  28. {
  29. $params = request()->all();
  30. $id = isset($params['id'])? intval($params['id']) : 0;
  31. if(empty($id)){
  32. return message(1036, false);
  33. }
  34. if($info = ArticleService::make()->getInfo($id)){
  35. return message(1010, true, $info);
  36. }else{
  37. return message(1009, false);
  38. }
  39. }
  40. /**
  41. * 分类
  42. * @return array
  43. */
  44. public function categorys()
  45. {
  46. $type = request()->post('type', 1);
  47. $datas = ArticleService::make()->getCateList($type);
  48. return message(1010, true, $datas);
  49. }
  50. /**
  51. * 单页数据
  52. */
  53. public function page()
  54. {
  55. $params = request()->all();
  56. $type = isset($params['type'])? intval($params['type']) : 0;
  57. if(empty($type)){
  58. return message(1031, false);
  59. }
  60. if($info = ArticleService::make()->getInfoByType($type)){
  61. return message(1010, true, $info);
  62. }else{
  63. return message(1009, false);
  64. }
  65. }
  66. /**
  67. * 咨询提交
  68. */
  69. public function consult(ConsultValidator $validator)
  70. {
  71. $params = request()->post();
  72. $params = $validator->check($params, 'submit');
  73. if (!is_array($params)) {
  74. return showJson($params, false);
  75. }
  76. try {
  77. if (ArticleService::make()->consultSubmit($this->userId, $params)) {
  78. return showJson(ArticleService::make()->getError(), true);
  79. } else {
  80. return showJson(ArticleService::make()->getError(), false);
  81. }
  82. } catch (\Exception $exception) {
  83. $error = ['data' => $exception->getTrace(), 'err' => $exception->getMessage()];
  84. return showJson(1046, false, $error);
  85. }
  86. }
  87. }