Website.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace app\api\controller;
  3. use think\Db;
  4. use think\Request;
  5. class Website 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['type_id'])) {
  38. $where['type_id'] = (int)$param['type_id'];
  39. }
  40. if (isset($param['status'])) {
  41. $where['website_status'] = (int)$param['status'];
  42. }
  43. if (isset($param['level'])) {
  44. $where['website_level'] = (int)$param['level'];
  45. }
  46. if (isset($param['name']) && strlen($param['name']) > 0) {
  47. $where['website_name'] = ['like', '%' . $this->format_sql_string($param['name']) . '%'];
  48. }
  49. if (isset($param['sub']) && strlen($param['sub']) > 0) {
  50. $where['website_sub'] = ['like', '%' . $this->format_sql_string($param['sub']) . '%'];
  51. }
  52. if (isset($param['en']) && strlen($param['en']) > 0) {
  53. $where['website_en'] = ['like', '%' . $this->format_sql_string($param['en']) . '%'];
  54. }
  55. if (isset($param['letter']) && strlen($param['letter']) == 1) {
  56. $where['website_letter'] = $param['letter'];
  57. }
  58. if (isset($param['area']) && strlen($param['area']) > 0) {
  59. $where['website_area'] = ['like', '%' . $this->format_sql_string($param['area']) . '%'];
  60. }
  61. if (isset($param['lang']) && strlen($param['lang']) > 0) {
  62. $where['website_lang'] = ['like', '%' . $this->format_sql_string($param['lang']) . '%'];
  63. }
  64. if (isset($param['tag']) && strlen($param['tag']) > 0) {
  65. $where['website_tag'] = ['like', '%' . $this->format_sql_string($param['tag']) . '%'];
  66. }
  67. if (isset($param['time_end']) && isset($param['time_start'])) {
  68. $where['website_time'] = ['between', [(int)$param['time_start'], (int)$param['time_end']]];
  69. }elseif (isset($param['time_end'])) {
  70. $where['website_time'] = ['<', (int)$param['time_end']];
  71. }elseif (isset($param['time_start'])) {
  72. $where['website_time'] = ['>', (int)$param['time_start']];
  73. }
  74. // 数据获取
  75. $total = model('Website')->getCountByCond($where);
  76. $list = [];
  77. if ($total > 0) {
  78. // 排序
  79. $order = "website_time DESC";
  80. if (strlen($param['orderby']) > 0) {
  81. $order = 'website_' . $param['orderby'] . " DESC";
  82. }
  83. $field = '*';
  84. $list = model('Website')->getListByCond($offset, $limit, $where, $order, $field, []);
  85. }
  86. // 返回
  87. return json([
  88. 'code' => 1,
  89. 'msg' => '获取成功',
  90. 'info' => [
  91. 'offset' => $offset,
  92. 'limit' => $limit,
  93. 'total' => $total,
  94. 'rows' => $list,
  95. ],
  96. ]);
  97. }
  98. /**
  99. * 查询详情
  100. *
  101. * @return \think\response\Json
  102. */
  103. public function get_detail(Request $request)
  104. {
  105. $param = $request->param();
  106. $validate = validate($request->controller());
  107. if (!$validate->scene($request->action())->check($param)) {
  108. return json([
  109. 'code' => 1001,
  110. 'msg' => '参数错误: ' . $validate->getError(),
  111. ]);
  112. }
  113. $res = Db::table('mac_website')->where(['website_id' => $param['website_id']])->select();
  114. // 返回
  115. return json([
  116. 'code' => 1,
  117. 'msg' => '获取成功',
  118. 'info' => $res
  119. ]);
  120. }
  121. }