Art.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace app\api\controller;
  3. use think\Db;
  4. use think\Request;
  5. class Art extends Base
  6. {
  7. use PublicApi;
  8. public function __construct()
  9. {
  10. parent::__construct();
  11. $this->check_config();
  12. }
  13. public function index()
  14. {
  15. }
  16. /**
  17. * 获取列表
  18. *
  19. * @param Request $request
  20. * @return \think\response\Json
  21. */
  22. public function get_list(Request $request)
  23. {
  24. // 参数校验
  25. $param = $request->param();
  26. $validate = validate($request->controller());
  27. if (!$validate->scene($request->action())->check($param)) {
  28. return json([
  29. 'code' => 1001,
  30. 'msg' => '参数错误: ' . $validate->getError(),
  31. ]);
  32. }
  33. // 查询条件组装
  34. $where = [];
  35. $offset = isset($param['offset']) ? (int)$param['offset'] : 0;
  36. $limit = isset($param['limit']) ? (int)$param['limit'] : 20;
  37. if (isset($param['time_end']) && isset($param['time_start'])) {
  38. $where['art_time'] = ['between', [(int)$param['time_start'], (int)$param['time_end']]];
  39. }elseif (isset($param['time_end'])) {
  40. $where['art_time'] = ['<=', (int)$param['time_end']];
  41. }elseif (isset($param['time_start'])) {
  42. $where['art_time'] = ['>=', (int)$param['time_start']];
  43. }
  44. if (isset($param['letter'])) {
  45. $where['art_letter'] = $param['letter'];
  46. }
  47. if (isset($param['status'])) {
  48. $where['art_status'] = (int)$param['status'];
  49. }
  50. if (isset($param['name']) && strlen($param['name']) > 0) {
  51. $where['art_name'] = ['like', '%' . $this->format_sql_string($param['name']) . '%'];
  52. }
  53. if (isset($param['sub']) && strlen($param['sub']) > 0) {
  54. $where['art_sub'] = ['like', '%' . $this->format_sql_string($param['sub']) . '%'];
  55. }
  56. if (isset($param['blurb']) && strlen($param['blurb']) > 0) {
  57. $where['art_blurb'] = ['like', '%' . $this->format_sql_string($param['blurb']) . '%'];
  58. }
  59. if (isset($param['title']) && strlen($param['title']) > 0) {
  60. $where['art_title'] = ['like', '%' . $this->format_sql_string($param['title']) . '%'];
  61. }
  62. if (isset($param['content']) && strlen($param['content']) > 0) {
  63. $where['art_content'] = ['like', '%' . $this->format_sql_string($param['content']) . '%'];
  64. }
  65. // 数据获取
  66. $total = model('Art')->getCountByCond($where);
  67. $list = [];
  68. if ($total > 0) {
  69. // 排序
  70. $order = "art_time DESC";
  71. $field = 'art_id,art_name,art_sub,art_en,art_blurb,art_time,art_time_add';
  72. if (strlen($param['orderby']) > 0) {
  73. $order = 'art_' . $param['orderby'] . " DESC";
  74. }
  75. $list = model('Art')->getListByCond($offset, $limit, $where, $order, $field, []);
  76. }
  77. // 返回
  78. return json([
  79. 'code' => 1,
  80. 'msg' => '获取成功',
  81. 'info' => [
  82. 'offset' => $offset,
  83. 'limit' => $limit,
  84. 'total' => $total,
  85. 'rows' => $list,
  86. ],
  87. ]);
  88. }
  89. /**
  90. * 视频文章详情
  91. *
  92. * @param Request $request
  93. * @return \think\response\Json
  94. * @throws \think\db\exception\DataNotFoundException
  95. * @throws \think\db\exception\ModelNotFoundException
  96. * @throws \think\exception\DbException
  97. */
  98. public function get_detail(Request $request)
  99. {
  100. $param = $request->param();
  101. $validate = validate($request->controller());
  102. if (!$validate->scene($request->action())->check($param)) {
  103. return json([
  104. 'code' => 1001,
  105. 'msg' => '参数错误: ' . $validate->getError(),
  106. ]);
  107. }
  108. $res = Db::table('mac_art')->where(['art_id' => $param['art_id']])->select();
  109. // 返回
  110. return json([
  111. 'code' => 1,
  112. 'msg' => '获取成功',
  113. 'info' => $res
  114. ]);
  115. }
  116. }