Type.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace app\api\controller;
  3. use think\Db;
  4. use think\Request;
  5. class Type 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. // 查询第一级
  36. $where['type_pid'] = 0;
  37. if (isset($param['type_id'])) {
  38. $where['type_id'] = (int)$param['type_id'];
  39. }
  40. // 数据获取
  41. $total = model('Type')->getCountByCond($where);
  42. $list = [];
  43. if ($total > 0) {
  44. // 排序
  45. $order = "type_sort DESC";
  46. $field = '*';
  47. $list = model('Type')->getListByCond(0, PHP_INT_MAX, $where, $order, $field, []);
  48. foreach ($list as $index => $item) {
  49. $child_total = Db::table('mac_type')->where(['type_pid' => $item['type_id']])->count();
  50. if ($child_total > 0) {
  51. $child = Db::table('mac_type')->where(['type_pid' => $item['type_id']])->select();
  52. $list[$index]['child'] = $child;
  53. }
  54. }
  55. }
  56. // 返回
  57. return json([
  58. 'code' => 1,
  59. 'msg' => '获取成功',
  60. 'info' => [
  61. 'total' => $total,
  62. 'rows' => $list,
  63. ],
  64. ]);
  65. }
  66. /**
  67. * 查询首页分类顶部导航栏
  68. *
  69. * @return \think\response\Json
  70. */
  71. public function get_all_list()
  72. {
  73. $list = Db::table('mac_type')->where(['type_pid'=> 0])->column('type_id,type_name,type_en');
  74. // 返回
  75. return json([
  76. 'code' => 1,
  77. 'msg' => '获取成功',
  78. 'info' => [
  79. 'total' => count($list),
  80. 'rows' => $list,
  81. ],
  82. ]);
  83. }
  84. }