AuthRule.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * ----------------------------------------------------------------------------
  4. * 行到水穷处,坐看云起时
  5. * 开发软件,找贵阳云起信息科技,官网地址:https://www.56q7.com/
  6. * ----------------------------------------------------------------------------
  7. * Author: 老成
  8. * email:85556713@qq.com
  9. */
  10. declare(strict_types=1);
  11. namespace app\common\model;
  12. use app\common\library\Tree;
  13. use think\Model;
  14. class AuthRule extends Model{
  15. // 自动写入时间戳字段
  16. protected $autoWriteTimestamp = true;
  17. // 定义时间戳字段名
  18. protected $createTime = 'createtime';
  19. protected $updateTime = 'updatetime';
  20. protected $type = [
  21. 'updatetime' => 'timestamp:Y-m-d H:i',
  22. 'createtime' => 'timestamp:Y-m-d H:i',
  23. ];
  24. const menutypeList = [
  25. 'tab'=>'选项卡',
  26. 'layer'=>'弹窗',
  27. 'blank'=>'跳转链接',
  28. ];
  29. public static function onAfterInsert($rule)
  30. {
  31. $rule->weigh=1000-$rule->id;
  32. $rule->save();
  33. }
  34. public static function getRuleListTree(mixed $ruleids,bool $ismenu=false):array
  35. {
  36. $where=[];
  37. if($ismenu){
  38. $where[]=['ismenu','=',1];
  39. }
  40. if($ruleids=='*'){
  41. $ruleList = self::where($where)->field('id,pid,title,icon,controller,action,ismenu,isplatform,weigh,status')->order('weigh DESC,id ASC')->select()->toArray();
  42. }else{
  43. $ruleList = self::where($where)->whereIn('id',$ruleids)->field('id,pid,title,icon,controller,action,ismenu,isplatform,weigh,status')->order('weigh DESC,id ASC')->select()->toArray();
  44. }
  45. Tree::instance()->init($ruleList);
  46. $list = Tree::instance()->getTreeArray(0);
  47. return $list;
  48. }
  49. public static function getMenuListTree(mixed $ruleids):array
  50. {
  51. if($ruleids=='*'){
  52. $ruleList = self::where('ismenu',1)->field('id,pid,title,icon,controller,action,ismenu,weigh,status')->order('weigh DESC,id ASC')->select()->toArray();
  53. }else{
  54. $ruleList = self::where('ismenu',1)->whereIn('id',$ruleids)->field('id,pid,title,icon,controller,action,ismenu,weigh,status')->order('weigh DESC,id ASC')->select()->toArray();
  55. }
  56. Tree::instance()->init($ruleList);
  57. $list = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title');
  58. return $list;
  59. }
  60. public static function getRuleList(mixed $ruleids,$pid=0):array
  61. {
  62. if($ruleids=='*'){
  63. $ruleList = self::field('id,pid,title,icon,ismenu,menutype,extend,controller,action,ismenu,weigh,status')->order('weigh DESC,id ASC')->select()->toArray();
  64. }else{
  65. $ruleList = self::whereIn('id',$ruleids)->field('id,pid,title,icon,ismenu,menutype,extend,controller,action,ismenu,weigh,status')->order('weigh DESC,id ASC')->select()->toArray();
  66. }
  67. $tree=new Tree();
  68. $tree->init($ruleList);
  69. $list = $tree->getTreeArray($pid);
  70. return $list;
  71. }
  72. }