Article.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\Lang;
  4. /**
  5. * ============================================================================
  6. *
  7. * ============================================================================
  8. *
  9. * ----------------------------------------------------------------------------
  10. *
  11. * ============================================================================
  12. * 文章控制器
  13. */
  14. class Article extends MobileMall
  15. {
  16. public function initialize()
  17. {
  18. parent::initialize();
  19. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/article.lang.php');
  20. }
  21. /**
  22. * @api {POST} api/Article/article_list 文章列表
  23. * @apiVersion 1.0.0
  24. * @apiGroup Article
  25. *
  26. * @apiParam {Int} ac_id 文章分类
  27. *
  28. * @apiSuccess {String} code 返回码,10000为成功
  29. * @apiSuccess {String} message 返回消息
  30. * @apiSuccess {Object} result 返回数据
  31. * @apiSuccess {Object[]} result.article_list 文章列表
  32. * @apiSuccess {Int} result.article_list.ac_id 分类ID
  33. * @apiSuccess {String} result.article_list.article_content 文章内容
  34. * @apiSuccess {Int} result.article_list.article_id 文章ID
  35. * @apiSuccess {String} result.article_list.article_pic 文章图片
  36. * @apiSuccess {Int} result.article_list.article_show 是否显示 0否1是
  37. * @apiSuccess {Int} result.article_list.article_sort 排序
  38. * @apiSuccess {Int} result.article_list.article_time 添加时间(Unix时间戳)
  39. * @apiSuccess {String} result.article_list.article_title 文章标题
  40. * @apiSuccess {String} result.article_list.article_url 文章跳转链接
  41. * @apiSuccess {String} result.article_type_name 分类名称
  42. */
  43. public function article_list()
  44. {
  45. $ac_id = intval(input('param.ac_id'));
  46. if ($ac_id > 0) {
  47. $articleclass_model = model('articleclass');
  48. $article_model = model('article');
  49. $condition = array();
  50. $child_class_list = $articleclass_model->getChildClass($ac_id);
  51. $ac_ids = array();
  52. if (!empty($child_class_list) && is_array($child_class_list)) {
  53. foreach ($child_class_list as $v) {
  54. $ac_ids[] = $v['ac_id'];
  55. }
  56. }
  57. $ac_ids = implode(',', $ac_ids);
  58. $condition[] = array('ac_id', 'in', $ac_ids);
  59. $condition[] = array('article_show', '=', '1');
  60. $result['article_list'] = $article_model->getArticleList($condition, 20);
  61. $result['article_type_name'] = $this->article_type_name($ac_ids);
  62. $result = array_merge(array('article_list' => $result['article_list'], 'article_type_name' => $result['article_type_name']), mobile_page(is_object($article_model->page_info) ? $article_model->page_info : ''));
  63. ds_json_encode(10000, '', $result);
  64. } else {
  65. ds_json_encode(10001, lang('param_error'));
  66. }
  67. }
  68. /**
  69. * 根据类别编号获取文章类别信息
  70. */
  71. private function article_type_name()
  72. {
  73. $ac_id = intval(input('param.ac_id'));
  74. if ($ac_id > 0) {
  75. $articleclass_model = model('articleclass');
  76. $article_class = $articleclass_model->getOneArticleclass($ac_id);
  77. return ($article_class['ac_name']);
  78. } else {
  79. return (lang('param_error'));
  80. }
  81. }
  82. /**
  83. * @api {POST} api/Article/article_show 单篇文章显示
  84. * @apiVersion 1.0.0
  85. * @apiGroup Article
  86. *
  87. * @apiParam {Int} article_id 文章ID
  88. *
  89. * @apiSuccess {String} code 返回码,10000为成功
  90. * @apiSuccess {String} message 返回消息
  91. * @apiSuccess {Object} result 返回数据
  92. * @apiSuccess {Int} result.ac_id 分类ID
  93. * @apiSuccess {String} result.article_content 文章内容
  94. * @apiSuccess {Int} result.article_id 文章ID
  95. * @apiSuccess {String} result.article_pic 文章图片
  96. * @apiSuccess {Int} result.article_show 是否显示 0否1是
  97. * @apiSuccess {Int} result.article_sort 排序
  98. * @apiSuccess {Int} result.article_time 添加时间(Unix时间戳)
  99. * @apiSuccess {String} result.article_title 文章标题
  100. * @apiSuccess {String} result.article_url 文章跳转链接
  101. */
  102. public function article_show()
  103. {
  104. $article_model = model('article');
  105. $article_id = intval(input('param.article_id'));
  106. if ($article_id > 0) {
  107. $prefix = 'api-article-show-';
  108. $article = rcache($article_id, $prefix);
  109. if (empty($article)) {
  110. $condition = array();
  111. $condition[] = array('article_id', '=', $article_id);
  112. $article = $article_model->getOneArticle($condition);
  113. wcache($article_id, $article, $prefix, 3600);
  114. }
  115. if (empty($article)) {
  116. ds_json_encode(10001, lang('article_show_not_exists'));
  117. } else {
  118. $article['article_content'] = htmlspecialchars_decode($article['article_content']);
  119. ds_json_encode(10000, '', $article);
  120. }
  121. } else {
  122. ds_json_encode(10001, lang('param_error'));
  123. }
  124. }
  125. }