1
0

Comment.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace app\api\controller;
  3. use think\Db;
  4. use think\Request;
  5. class Comment 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. $offset = isset($param['offset']) ? (int)$param['offset'] : 0;
  34. $limit = isset($param['limit']) ? (int)$param['limit'] : 20;
  35. // 查询条件组装
  36. $where = [];
  37. if (isset($param['rid'])) {
  38. $where['comment_rid'] = (int)$param['rid'];
  39. }
  40. // 数据获取
  41. $total = model('Comment')->getCountByCond($where);
  42. $list = [];
  43. if ($total > 0) {
  44. // 排序
  45. $order = "comment_time DESC";
  46. if (strlen($param['orderby']) > 0) {
  47. $order = 'comment_' . $param['orderby'] . " DESC";
  48. }
  49. $field = '*';
  50. $list = model('Comment')->getListByCond($offset, $limit, $where, $order, $field, []);
  51. }
  52. // 返回
  53. return json([
  54. 'code' => 1,
  55. 'msg' => '获取成功',
  56. 'info' => [
  57. 'offset' => $offset,
  58. 'limit' => $limit,
  59. 'total' => $total,
  60. 'rows' => $list,
  61. ],
  62. ]);
  63. }
  64. }