AuthRule.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: EDZ
  5. * Date: 2018/12/13
  6. * Time: 11:19
  7. */
  8. namespace app\common\model;
  9. use think\facade\Env;
  10. class AuthRule extends Base
  11. {
  12. protected $autoWriteTimestamp = false;
  13. public $filter_method = array('__construct', 'execute', 'login', 'sqlSplit', 'isMobile', 'is_wechat', '_initialize');
  14. public $keyList = array(
  15. array('name'=>'module','title'=>'所属模块','type'=>'hidden'),
  16. array('name'=>'title','title'=>'节点名称','type'=>'text','help'=>''),
  17. array('name'=>'name','title'=>'节点标识','type'=>'text','help'=>''),
  18. array('name'=>'group','title'=>'功能组','type'=>'text','help'=>'功能分组'),
  19. array('name'=>'status','title'=>'状态','type'=>'radio','option'=>array('1'=>'启用','0'=>'禁用'),'help'=>''),
  20. array('name'=>'condition','title'=>'条件','type'=>'text','help'=>'')
  21. );
  22. public function saveNode($data){
  23. if (isset($data['id']) && $data['id']) {
  24. $this->save($data, array('id'=>$data['id']));
  25. }else{
  26. $this->save($data);
  27. }
  28. }
  29. public function updataNode($type){
  30. $data = $this->updateRule($type);
  31. foreach ($data as $value) {
  32. $id = $this->where('name', $value['name'])->value('id',false);
  33. if ($id) {
  34. $value['id'] = $id;
  35. }
  36. $list[] = $value;
  37. }
  38. $this->saveAll($list);
  39. }
  40. public function updateRule($type){
  41. $path = Env::get('app_path') . $type . '/controller';
  42. //控制器类文件
  43. $classname = $this->scanFile($path);
  44. foreach ($classname as $value) {
  45. $class = "\\app\\" . $type . "\\controller\\" . $value;
  46. if(class_exists($class)){
  47. $reflection = new \ReflectionClass($class);
  48. $group_doc = $this->parserDoc($reflection->getDocComment());
  49. $method = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC);
  50. foreach ($method as $key => $v) {
  51. if (!in_array($v->name, $this->filter_method)) {
  52. $title_doc = $this->parserDoc($v->getDocComment());
  53. if (isset($title_doc['title']) && $title_doc['title']) {
  54. $list[] = array(
  55. 'module' => $type,
  56. 'type' => 2,
  57. 'name' => $type . '/' . strtolower($value) . '/' . strtolower($v->name),
  58. 'title' => trim($title_doc['title']),
  59. 'group' => (isset($group_doc['title']) && $group_doc['title']) ? trim($group_doc['title']) : '',
  60. 'status' => 1
  61. );
  62. }
  63. }
  64. }
  65. }
  66. }
  67. return $list;
  68. }
  69. protected function scanFile($path){
  70. $result = array();
  71. $files = scandir($path);
  72. foreach ($files as $file) {
  73. if ($file != '.' && $file != '..') {
  74. if (is_dir($path . '/' . $file)) {
  75. $this->scanFile($path . '/' . $file);
  76. } else {
  77. $result[] = substr(basename($file), 0 , -4);
  78. }
  79. }
  80. }
  81. return $result;
  82. }
  83. protected function parserDoc($text){
  84. $doc = new \com\Doc();
  85. return $doc->parse($text);
  86. }
  87. }