Link.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace app\api\controller;
  3. use think\Db;
  4. use think\Request;
  5. class Link 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['id'])) {
  38. $where['link_id'] = (int)$param['id'];
  39. }
  40. if (isset($param['type'])) {
  41. $where['link_type'] = (int)$param['type'];
  42. }
  43. if (isset($param['sort'])) {
  44. $where['link_sort'] = (int)$param['sort'];
  45. }
  46. if (isset($param['name']) && strlen($param['name']) > 0) {
  47. $where['link_name'] = ['like', '%' . $this->format_sql_string($param['name']) . '%'];
  48. }
  49. if (isset($param['time_end']) && isset($param['time_start'])) {
  50. $where['link_time'] = ['between', [(int)$param['time_start'], (int)$param['time_end']]];
  51. }elseif (isset($param['time_end'])) {
  52. $where['link_time'] = ['<=', (int)$param['time_end']];
  53. }elseif (isset($param['time_start'])) {
  54. $where['link_time'] = ['>=', (int)$param['time_start']];
  55. }
  56. // 数据获取
  57. $total = model('Link')->getCountByCond($where);
  58. $list = [];
  59. if ($total > 0) {
  60. // 排序
  61. $order = "link_time DESC";
  62. $field = '*';
  63. if (strlen($param['orderby']) > 0) {
  64. $order = 'link_' . $param['orderby'] . " DESC";
  65. }
  66. $list = model('Link')->getListByCond($offset, $limit, $where, $order, $field, []);
  67. foreach ($list as &$item) {
  68. $item['link_name'] = htmlspecialchars($item['link_name'], ENT_QUOTES, 'UTF-8');
  69. $item['link_logo'] = htmlspecialchars($item['link_logo'], ENT_QUOTES, 'UTF-8');
  70. $item['link_url'] = htmlspecialchars($item['link_url'], ENT_QUOTES, 'UTF-8');
  71. }
  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. }