Gbook.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace app\api\controller;
  3. use think\Request;
  4. class Gbook extends Base
  5. {
  6. use PublicApi;
  7. public function __construct()
  8. {
  9. parent::__construct();
  10. $this->check_config();
  11. }
  12. public function index()
  13. {
  14. }
  15. /**
  16. * 获取列表
  17. *
  18. * @param Request $request
  19. * @return \think\response\Json
  20. */
  21. public function get_list(Request $request)
  22. {
  23. // 参数校验
  24. $param = $request->param();
  25. $validate = validate($request->controller());
  26. if (!$validate->scene($request->action())->check($param)) {
  27. return json([
  28. 'code' => 1001,
  29. 'msg' => '参数错误: ' . $validate->getError(),
  30. ]);
  31. }
  32. // 查询条件组装
  33. $where = [];
  34. $offset = isset($param['offset']) ? (int)$param['offset'] : 0;
  35. $limit = isset($param['limit']) ? (int)$param['limit'] : 20;
  36. if (isset($param['id'])) {
  37. $where['gbook_id'] = (int)$param['id'];
  38. }
  39. if (isset($param['rid'])) {
  40. $where['gbook_rid'] = (int)$param['rid'];
  41. }
  42. if (isset($param['user_id'])) {
  43. $where['user_id'] = (int)$param['user_id'];
  44. }
  45. if (isset($param['status'])) {
  46. $where['gbook_status'] = (int)$param['status'];
  47. }
  48. if (isset($param['name']) && strlen($param['name']) > 0) {
  49. $where['gbook_name'] = ['like', '%' . $this->format_sql_string($param['name']) . '%'];
  50. }
  51. if (isset($param['content']) && strlen($param['content']) > 0) {
  52. $where['gbook_content'] = ['like', '%' . $this->format_sql_string($param['content']) . '%'];
  53. }
  54. if (isset($param['time_end']) && isset($param['time_start'])) {
  55. $where['gbook_time'] = ['between', [(int)$param['time_start'], (int)$param['time_end']]];
  56. }elseif (isset($param['time_end'])) {
  57. $where['gbook_time'] = ['<', (int)$param['time_end']];
  58. }elseif (isset($param['time_start'])) {
  59. $where['gbook_time'] = ['>', (int)$param['time_start']];
  60. }
  61. // 数据获取
  62. $total = model('Gbook')->getCountByCond($where);
  63. $list = [];
  64. if ($total > 0) {
  65. // 排序
  66. $order = "gbook_time DESC";
  67. $field = '*';
  68. if (strlen($param['orderby']) > 0) {
  69. $order = 'gbook_' . $param['orderby'] . " DESC";
  70. }
  71. $list = model('Gbook')->getListByCond($offset, $limit, $where, $order, $field, []);
  72. }
  73. // 返回
  74. return json([
  75. 'code' => 1,
  76. 'msg' => '获取成功',
  77. 'info' => [
  78. 'offset' => $offset,
  79. 'limit' => $limit,
  80. 'total' => $total,
  81. 'rows' => $list,
  82. ],
  83. ]);
  84. }
  85. }