News.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace Content\V1;
  3. use Dever;
  4. use Content\Lib\Base;
  5. class News extends Base
  6. {
  7. //*@ apiSampleRequest /v1/news.index 暂时不添加测试接口,这里需要域名和跨域支持
  8. /**
  9. * @api {get} v1/news.index 首页接口
  10. * @apiVersion 1.0.0
  11. * @apiName api.index
  12. * @apiGroup Data
  13. *
  14. * @apiDescription 小程序的首页接口
  15. *
  16. * apiSampleRequest /v1/news.index
  17. *
  18. * @apiParam {Number} appid 小程序id
  19. *
  20. * @apiSuccess {Object[]} info 小程序基本信息
  21. * @apiSuccess {Number} info.id 小程序id
  22. * @apiSuccess {String} info.name 小程序名称
  23. * @apiSuccess {Number} info.cate_id 小程序所属栏目id
  24. * @apiSuccess {String} info.logo 小程序logo
  25. * @apiSuccess {String} info.title 首页标题
  26. * @apiSuccess {String} info.pic 首页头图
  27. *
  28. * @apiSuccess {Object[]} cate 主栏目信息
  29. * @apiSuccess {Number} cate.id 栏目id
  30. * @apiSuccess {String} cate.name 栏目名称
  31. *
  32. * @apiSuccess {Object[]} focus 头部焦点图信息
  33. * @apiSuccess {Number} focus.id 焦点图id
  34. * @apiSuccess {String} focus.name 焦点图名称
  35. * @apiSuccess {String} focus.subname 焦点图子标题
  36. * @apiSuccess {String} focus.pic 焦点图图片
  37. * @apiSuccess {Number} focus.cate_id 栏目id
  38. * @apiSuccess {Number} focus.news_id 资讯id
  39. *
  40. * @apiSuccess {Object[]} cate_child 子栏目信息
  41. * @apiSuccess {Number} cate_child.id 栏目id
  42. * @apiSuccess {String} cate_child.name 栏目名称
  43. * @apiSuccess {Number} cate_child.cate_id 上级栏目id
  44. *
  45. * @apiSuccess {Object[]} news 资讯信息
  46. * @apiSuccess {Number} news.id 资讯id
  47. * @apiSuccess {String} news.name 资讯标题
  48. * @apiSuccess {String} news.content 资讯内容
  49. * @apiSuccess {String} news.num_view 阅读数
  50. * @apiSuccess {String} news.cdate 时间
  51. *
  52. * @apiSuccess {Object[]} news.author 作者信息
  53. * @apiSuccess {Number} news.author.id 作者id
  54. * @apiSuccess {String} news.author.name 作者姓名
  55. * @apiSuccess {String} news.author.pic 作者头像
  56. *
  57. * @apiSuccessExample 操作成功
  58. * {"status":1,"code":1,"msg":"success","data":{"info":{"id":"1","name":"\u7279\u8272\u5c0f\u9547\u600e\u4e48\u641e","cate_id":"1","logo":"http:\/\/192.168.33.10\/plant\/data\/upload\/1\/2018\/03\/24\/1e493a87d4cdddde7fd06ad9762694d2.jpg","title":"test","pic":"http:\/\/192.168.33.10\/plant\/data\/upload\/1\/2018\/03\/24\/da31b5d54d15192fea10406f77f4b0cb.jpg","state":"1","cdate":"1521858466"},"cate":{"id":"1","name":"\u7279\u8272\u5c0f\u9547\u4e0d\u5f97\u4e0d\u5b66\u4e60\u7cfb\u5217","cate_id":"-1","reorder":"1","state":"1","cdate":"1521814022"},"cate_child":[{"id":"3","name":"\u5341\u5927\u6848\u4f8b","cate_id":"1","reorder":"1","state":"1","cdate":"1521858511","news":[{"id":"1","name":"\u4e07\u8fbe\u57ce\u521b\u65b0\u4e1a\u6001\u7814\u7a76\u62a5\u544a","pic":["http:\/\/192.168.33.10\/plant\/data\/upload\/1\/2018\/03\/24\/1e493a87d4cdddde7fd06ad9762694d2.jpg"],"top":"http:\/\/192.168.33.10\/plant\/data\/upload\/1\/2018\/03\/24\/da31b5d54d15192fea10406f77f4b0cb.jpg","cate_id":"3","info_id":"1","author_id":"1","content":"<p>\u5730\u65b9\u5927\u5e45\u5ea6\u53d1<\/p>","num_view":"0","cdate":"9\u5c0f\u65f6\u524d","author":{"id":"1","name":"\u7a0b\u8302","pic":"http:\/\/192.168.33.10\/plant\/data\/upload\/1\/2018\/03\/24\/1e493a87d4cdddde7fd06ad9762694d2.jpg","state":"1","cdate":"1521858481"}}]},{"id":"2","name":"\u5341\u5927\u680f\u76ee","cate_id":"1","reorder":"1","state":"1","cdate":"1521858505"}]},"page":{"total":"1","current_page":1,"total_page":1,"next_page":1,"prev_page":1,"html":"","status":0}}
  59. *
  60. * @apiErrorExample 操作失败
  61. * {"status":2,"code":1,"msg":"\u9519\u8bef\u7684\u5c0f\u7a0b\u5e8fid"}
  62. */
  63. public function index_api()
  64. {
  65. # 获取一条主栏目
  66. $this->data['cate'] = Dever::load('set/cate-one', $this->data['info']['cate_id']);
  67. if ($this->data['cate']) {
  68. # 获取焦点图
  69. $this->focus($this->data['cate']['id']);
  70. $this->data['cate_child'] = Dever::load('set/cate-getAll', array('option_cate_id' => $this->data['cate']['id']));
  71. $this->data['news'] = array();
  72. if ($this->data['cate_child'] && isset($this->data['cate_child'][0])) {
  73. $news = Dever::load('content/news-getAll', array('option_info_id' => $this->data['info']['id'], 'option_cate_id' => $this->data['cate_child'][0]['id']));
  74. if ($news) {
  75. foreach ($news as $k => $v) {
  76. $this->data['news'][$k] = $this->one($v);
  77. }
  78. }
  79. }
  80. }
  81. return $this->data;
  82. }
  83. private function focus($cate)
  84. {
  85. $this->data['focus'] = Dever::load('content/focus-getAll', array('option_cate_id_parent' => $cate));
  86. if ($this->data['focus']) {
  87. foreach ($this->data['focus'] as $k => $v) {
  88. $this->data['focus'][$k]['pic'] = $this->replacePic($v['pic']);
  89. }
  90. }
  91. }
  92. /**
  93. * @api {get} v1/news.list 资讯列表
  94. * @apiVersion 1.0.0
  95. * @apiName news.list
  96. * @apiGroup Data
  97. *
  98. * @apiDescription 小程序的资讯列表接口
  99. *
  100. * @apiParam {Number} appid 小程序id
  101. * @apiParam {Number} cateid 子栏目id
  102. *
  103. * @apiSuccess {Object[]} info 小程序基本信息
  104. * @apiSuccess {Number} info.id 小程序id
  105. * @apiSuccess {String} info.name 小程序名称
  106. * @apiSuccess {Number} info.cate_id 小程序所属栏目id
  107. * @apiSuccess {String} info.logo 小程序logo
  108. * @apiSuccess {String} info.title 首页标题
  109. * @apiSuccess {String} info.pic 首页头图
  110. *
  111. * @apiSuccess {Object[]} news 资讯信息
  112. * @apiSuccess {Number} news.id 资讯id
  113. * @apiSuccess {String} news.name 资讯标题
  114. * @apiSuccess {String} news.name 资讯子标题
  115. * @apiSuccess {String} news.content 资讯内容
  116. * @apiSuccess {String} news.num_view 阅读数
  117. * @apiSuccess {String} news.cdate 时间
  118. *
  119. * @apiSuccess {Object[]} news.author 作者信息
  120. * @apiSuccess {Number} news.author.id 作者id
  121. * @apiSuccess {String} news.author.name 作者姓名
  122. * @apiSuccess {String} news.author.pic 作者头像
  123. *
  124. * @apiSuccessExample 操作成功
  125. * {"status":1,"code":1,"msg":"success","data":{"info":{"id":"1","name":"\u7279\u8272\u5c0f\u9547\u600e\u4e48\u641e","cate_id":"1","logo":"http:\/\/192.168.33.10\/plant\/data\/upload\/1\/2018\/03\/24\/1e493a87d4cdddde7fd06ad9762694d2.jpg","title":"test","pic":"http:\/\/192.168.33.10\/plant\/data\/upload\/1\/2018\/03\/24\/da31b5d54d15192fea10406f77f4b0cb.jpg","state":"1","cdate":"1521858466"},"news":[{"id":"1","name":"\u4e07\u8fbe\u57ce\u521b\u65b0\u4e1a\u6001\u7814\u7a76\u62a5\u544a","pic":["http:\/\/192.168.33.10\/plant\/data\/upload\/1\/2018\/03\/24\/1e493a87d4cdddde7fd06ad9762694d2.jpg"],"top":"http:\/\/192.168.33.10\/plant\/data\/upload\/1\/2018\/03\/24\/da31b5d54d15192fea10406f77f4b0cb.jpg","cate_id":"3","info_id":"1","author_id":"1","content":"<p>\u5730\u65b9\u5927\u5e45\u5ea6\u53d1<\/p>","num_view":"0","cdate":"9\u5c0f\u65f6\u524d","author":{"id":"1","name":"\u7a0b\u8302","pic":"http:\/\/192.168.33.10\/plant\/data\/upload\/1\/2018\/03\/24\/1e493a87d4cdddde7fd06ad9762694d2.jpg","state":"1","cdate":"1521858481"}}]},"page":{"total":"1","current_page":1,"total_page":1,"next_page":1,"prev_page":1,"html":"","status":0}}
  126. *
  127. * @apiErrorExample 操作失败
  128. * {"status":2,"code":1,"msg":"\u9519\u8bef\u7684\u5c0f\u7a0b\u5e8fid"}
  129. */
  130. public function list_api()
  131. {
  132. $cateid = Dever::input('cateid');
  133. if ($cateid > 0) {
  134. $cate = Dever::load('set/cate-one', $cateid);
  135. if ($cate) {
  136. $this->data['news'] = Dever::load('content/news-getAll', array('option_info_id' => $this->data['info']['id'], 'option_cate_id' => $cate['id']));
  137. if ($this->data['news']) {
  138. foreach ($this->data['news'] as $k => $v) {
  139. $this->data['news'][$k] = $this->one($v);
  140. }
  141. }
  142. }
  143. }
  144. return $this->data;
  145. }
  146. /**
  147. * @api {get} v1/news.view 资讯详情
  148. * @apiVersion 1.0.0
  149. * @apiName news.view
  150. * @apiGroup Data
  151. *
  152. * @apiDescription 小程序的资讯详情接口
  153. *
  154. * @apiParam {Number} appid 小程序id
  155. * @apiParam {Number} id 资讯id
  156. *
  157. * @apiSuccess {Object[]} info 小程序基本信息
  158. * @apiSuccess {Number} info.id 小程序id
  159. * @apiSuccess {String} info.name 小程序名称
  160. * @apiSuccess {Number} info.cate_id 小程序所属栏目id
  161. * @apiSuccess {String} info.logo 小程序logo
  162. * @apiSuccess {String} info.title 首页标题
  163. * @apiSuccess {String} info.pic 首页头图
  164. *
  165. * @apiSuccess {Object[]} news 资讯信息
  166. * @apiSuccess {Number} news.id 资讯id
  167. * @apiSuccess {String} news.name 资讯标题
  168. * @apiSuccess {String} news.content 资讯内容
  169. * @apiSuccess {String} news.num_view 阅读数
  170. * @apiSuccess {String} news.cdate 时间
  171. *
  172. * @apiSuccess {Object[]} news.author 作者信息
  173. * @apiSuccess {Number} news.author.id 作者id
  174. * @apiSuccess {String} news.author.name 作者姓名
  175. * @apiSuccess {String} news.author.pic 作者头像
  176. *
  177. * @apiSuccessExample 操作成功
  178. * {"status":1,"code":1,"msg":"success","data":{"info":{"id":"1","name":"\u7279\u8272\u5c0f\u9547\u600e\u4e48\u641e","cate_id":"1","logo":"http:\/\/192.168.33.10\/plant\/data\/upload\/1\/2018\/03\/24\/1e493a87d4cdddde7fd06ad9762694d2.jpg","title":"test","pic":"http:\/\/192.168.33.10\/plant\/data\/upload\/1\/2018\/03\/24\/da31b5d54d15192fea10406f77f4b0cb.jpg","state":"1","cdate":"1521858466"},"news":{"id":"1","name":"\u4e07\u8fbe\u57ce\u521b\u65b0\u4e1a\u6001\u7814\u7a76\u62a5\u544a","info_id":"1","author_id":"1","cate_id_parent":"1","cate_id":"3","num_add_view":"0","num_up":"0","num_view":"0","num_review":"0","pic":["http:\/\/192.168.33.10\/plant\/data\/upload\/1\/2018\/03\/24\/1e493a87d4cdddde7fd06ad9762694d2.jpg"],"top":"http:\/\/192.168.33.10\/plant\/data\/upload\/1\/2018\/03\/24\/da31b5d54d15192fea10406f77f4b0cb.jpg","content":"<p>\u5730\u65b9\u5927\u5e45\u5ea6\u53d1<\/p>","reorder":"1","state":"1","cdate":"9\u5c0f\u65f6\u524d","author":{"id":"1","name":"\u7a0b\u8302","pic":"http:\/\/192.168.33.10\/plant\/data\/upload\/1\/2018\/03\/24\/1e493a87d4cdddde7fd06ad9762694d2.jpg","state":"1","cdate":"1521858481"}}}}
  179. *
  180. * @apiErrorExample 操作失败
  181. * {"status":2,"code":1,"msg":"\u9519\u8bef\u7684\u5c0f\u7a0b\u5e8fid"}
  182. */
  183. public function view_api()
  184. {
  185. $id = Dever::input('id');
  186. if ($id > 0) {
  187. $news = Dever::load('content/news-one', $id);
  188. if ($news) {
  189. $this->data['news'] = $this->one($news);
  190. # 增加阅读量
  191. Dever::load('content/news-addView', $id);
  192. } else {
  193. Dever::alert('错误的资讯id');
  194. }
  195. } else {
  196. Dever::alert('错误的资讯id');
  197. }
  198. return $this->data;
  199. }
  200. }