Menu.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php namespace App\Models\Manager;
  2. use Illuminate\Database\Eloquent\Factories\HasFactory;
  3. use Illuminate\Database\Eloquent\Model;
  4. use App\Models\Manager\CompanyCategoryRule;
  5. /**
  6. * 菜单模型
  7. *
  8. */
  9. class Menu extends Model
  10. {
  11. use HasFactory;
  12. // 与模型关联的表名
  13. protected $table = 'menu';
  14. // 是否主动维护时间戳
  15. public $timestamps = false;
  16. protected $connection = 'mysql';
  17. // 定义时间戳字段名
  18. // const CREATED_AT = 'insert_time';
  19. // const UPDATED_AT = 'update_time';
  20. /**
  21. * 添加数据
  22. *
  23. */
  24. public function add($data)
  25. {
  26. // 时间
  27. $data['insert_time'] = time();
  28. $data['update_time'] = time();
  29. // 写入数据表
  30. $id = $this->query()->insertGetId($data);
  31. // 如果操作失败
  32. if( !$id ) return 0;
  33. // 更新缓存
  34. if( isset($data['module']) ) $this->getList($data['module'],true);
  35. // 返回结果
  36. return $id;
  37. }
  38. /**
  39. * 添加数据
  40. *
  41. */
  42. public function edit($id,$data)
  43. {
  44. // 更新时间
  45. $data['update_time'] = time();
  46. // 写入数据表
  47. $result = $this->query()->where([['id','=',$id]])->update($data);
  48. // 如果操作失败
  49. if( !$result ) return 0;
  50. // 更新缓存
  51. if( isset($data['module']) ) $this->getList($data['module'],true);
  52. // 返回结果
  53. return $id;
  54. }
  55. /**
  56. * 获取列表
  57. * @param string $module 对应模块
  58. * @param bool $force 是否强制更新
  59. *
  60. */
  61. public function getList($module,$force = false)
  62. {
  63. // 结果数据
  64. $list = $force ? [] : cache('admin:menu:list:'.$module);
  65. // 不存在数据
  66. if ( !$list ) {
  67. // 从数据库获取数据
  68. $data = $this->query()->where([['module','=',$module],['status','=',0]])->orderBy('sort_order')->orderBy('id')->get(['id','title','module','url','icon','type','pid'])->toArray();
  69. // 循环处理数据
  70. $list = [];
  71. // 进行更新
  72. foreach ($data as $value) {
  73. // 重组数据
  74. $list[$value['id']] = $value;
  75. }
  76. // 存起来
  77. cache(['admin:menu:list:'.$module=>$list]);
  78. }
  79. // 返回结果
  80. return $list;
  81. }
  82. /**
  83. * 获取配置平台对应的应用数据
  84. *
  85. * @param string $module 对应模块
  86. * @param int ID
  87. * @param string 指定字段
  88. *
  89. */
  90. public function getOne($module,$id,$field='')
  91. {
  92. // 获取列表数据
  93. $list = $this->getList($module);
  94. // 获取数据
  95. $one = isset($list[$id]) ? $list[$id] : [];
  96. // 返回值
  97. return empty($field) ? $one : ( isset($one[$field]) ? $one[$field] : null);
  98. }
  99. }