Category.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\model;
  4. /**
  5. * 栏目模型
  6. */
  7. class Category extends Base
  8. {
  9. protected $pk = 'id';
  10. protected $schema = [
  11. "id" => "int",
  12. "parent_id" => "int",
  13. "name" => "varchar",
  14. "url" => "varchar",
  15. "route" => "varchar",
  16. "type" => "int",
  17. "is_nav" => "int",
  18. "sort" => "int",
  19. "title" => "varchar",
  20. "keywords" => "varchar",
  21. "description" => "varchar",
  22. "is_blank" => "int",
  23. "create_time" => "int",
  24. "update_time" => "int"
  25. ];
  26. // 获取列表
  27. public static function getList(array $param = [])
  28. {
  29. $where = [];
  30. if (isset($param['is_nav']) ) {
  31. $where[] = ['is_nav', '=', (int) $param['is_nav']];
  32. }
  33. if (isset($param['type']) ) {
  34. $where[] = ['type', '=', (int) $param['type']];
  35. }
  36. $order = isset($param['order']) ? (string) $param['order'] : "sort ASC,id DESC";
  37. return self::where($where)->field("id,parent_id,name,url,route,type,is_nav,sort,is_blank,create_time")->order($order)->select();
  38. }
  39. // 导航状态修改 1,显示; 0,不显示
  40. public static function navStaus(int $id)
  41. {
  42. try {
  43. $info = self::find($id);
  44. $info->is_nav = -1 - $info['is_nav'];
  45. $info->save();
  46. return json(['code' => 0, 'msg' => '修改成功!', 'is_nav'=>$info->is_nav]);
  47. } catch (\Exception $e) {
  48. return json(['code' => 1, 'msg' => $e->getMessage()]);
  49. }
  50. }
  51. public static function selectOne()
  52. {
  53. $list = self::where('type', 1)->field("id, parent_id,name")->select();
  54. $top = new static([
  55. "id"=> 0,
  56. "parent_id" => -1,
  57. "name" => "顶级栏目"
  58. ]);
  59. $list->push($top);
  60. return list_tree($list, 'id', 'parent_id', 'child', -1);
  61. }
  62. }