心理咨询网
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ListController.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * @copyright (C)2016-2099 Hnaoyun Inc.
  4. * @author XingMeng
  5. * @email hnxsh@foxmail.com
  6. * @date 2018年4月20日
  7. * 内容列表接口控制器
  8. */
  9. namespace app\api\controller;
  10. use core\basic\Controller;
  11. use app\api\model\CmsModel;
  12. use core\basic\Url;
  13. use app\home\controller\ParserController;
  14. class ListController extends Controller
  15. {
  16. protected $model;
  17. public function __construct()
  18. {
  19. $this->model = new CmsModel();
  20. }
  21. public function index()
  22. {
  23. // 获取参数
  24. $acode = request('acode', 'var') ?: get_default_lg();
  25. $scode = request('scode', 'var') ?: '';
  26. $num = request('num', 'int') ?: $this->config('pagesize');
  27. $rorder = request('order');
  28. if (! preg_match('/^[\w\-,\s]+$/', $rorder)) {
  29. $order = 'a.istop DESC,a.isrecommend DESC,a.isheadline DESC,a.sorting ASC,a.date DESC,a.id DESC';
  30. } else {
  31. switch ($rorder) {
  32. case 'id':
  33. $order = 'a.id DESC,a.istop DESC,a.isrecommend DESC,a.isheadline DESC,a.sorting ASC,a.date DESC';
  34. break;
  35. case 'date':
  36. $order = 'a.date DESC,a.istop DESC,a.isrecommend DESC,a.isheadline DESC,a.sorting ASC,a.id DESC';
  37. break;
  38. case 'sorting':
  39. $order = 'a.sorting ASC,a.istop DESC,a.isrecommend DESC,a.isheadline DESC,a.date DESC,a.id DESC';
  40. break;
  41. case 'istop':
  42. $order = 'a.istop DESC,a.isrecommend DESC,a.isheadline DESC,a.sorting ASC,a.date DESC,a.id DESC';
  43. break;
  44. case 'isrecommend':
  45. $order = 'a.isrecommend DESC,a.istop DESC,a.isheadline DESC,a.sorting ASC,a.date DESC,a.id DESC';
  46. break;
  47. case 'isheadline':
  48. $order = 'a.isrecommend DESC,a.istop DESC,a.isheadline DESC,a.sorting ASC,a.date DESC,a.id DESC';
  49. break;
  50. case 'visits':
  51. case 'likes':
  52. case 'oppose':
  53. $order = $rorder . ' DESC,a.istop DESC,a.isrecommend DESC,a.isheadline DESC,a.sorting ASC,a.date DESC,a.id DESC';
  54. break;
  55. case 'random': // 随机取数
  56. $db_type = get_db_type();
  57. if ($db_type == 'mysql') {
  58. $order = "RAND()";
  59. } elseif ($db_type == 'sqlite') {
  60. $order = "RANDOM()";
  61. }
  62. break;
  63. default:
  64. if ($rorder) {
  65. $orders = explode(',', $rorder);
  66. foreach ($orders as $k => $v) {
  67. if (strpos($v, 'ext_') === 0) {
  68. $orders[$k] = 'e.' . $v;
  69. } else {
  70. $orders[$k] = 'a.' . $v;
  71. }
  72. }
  73. $order = implode(',', $orders);
  74. $order .= ',a.istop DESC,a.isrecommend DESC,a.isheadline DESC,a.sorting ASC,a.date DESC,a.id DESC';
  75. }
  76. }
  77. }
  78. // 读取数据
  79. $data = $this->model->getLists($acode, $scode, $num, $order);
  80. $Parser = new ParserController();
  81. foreach ($data as $key => $value) {
  82. if ($value->outlink) {
  83. $data[$key]->apilink = $value->outlink;
  84. } else {
  85. $data[$key]->apilink = url('/api/content/index/id/' . $value->id, false);
  86. }
  87. $data[$key]->likeslink = url('/home/Do/likes/id/' . $value->id, false);
  88. $data[$key]->opposelink = url('/home/Do/oppose/id/' . $value->id, false);
  89. $data[$key]->content = str_replace(STATIC_DIR . '/upload/', get_http_url() . STATIC_DIR . '/upload/', $value->content);
  90. // 返回网页链接地址,便于AJAX调用内容
  91. $data[$key]->contentlink = $Parser->parserLink(2, $value->urlname, 'content', $value->scode, $value->sortfilename, $value->id, $value->filename);
  92. }
  93. // 输出数据
  94. if (request('page') <= PAGECOUNT) {
  95. json(1, $data);
  96. } else {
  97. return json(0, '已经到底了!');
  98. }
  99. }
  100. }