You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

63 lines
1.8 KiB

  1. <?php
  2. namespace app\admin\model;
  3. use fast\Tree;
  4. use think\Model;
  5. class UserRule extends Model
  6. {
  7. // 表名
  8. protected $name = 'user_rule';
  9. // 自动写入时间戳字段
  10. protected $autoWriteTimestamp = 'int';
  11. // 定义时间戳字段名
  12. protected $createTime = 'createtime';
  13. protected $updateTime = 'updatetime';
  14. // 追加属性
  15. protected $append = [
  16. 'status_text'
  17. ];
  18. protected static function init()
  19. {
  20. self::afterInsert(function ($row) {
  21. $pk = $row->getPk();
  22. $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
  23. });
  24. }
  25. public function getStatusList()
  26. {
  27. return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
  28. }
  29. public function getStatusTextAttr($value, $data)
  30. {
  31. $value = $value ? $value : $data['status'];
  32. $list = $this->getStatusList();
  33. return isset($list[$value]) ? $list[$value] : '';
  34. }
  35. public static function getTreeList($selected = [])
  36. {
  37. $ruleList = collection(self::where('status', 'normal')->order('weigh desc,id desc')->select())->toArray();
  38. $nodeList = [];
  39. Tree::instance()->init($ruleList);
  40. $ruleList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'name');
  41. $hasChildrens = [];
  42. foreach ($ruleList as $k => $v)
  43. {
  44. if ($v['haschild'])
  45. $hasChildrens[] = $v['id'];
  46. }
  47. foreach ($ruleList as $k => $v) {
  48. $state = array('selected' => in_array($v['id'], $selected) && !in_array($v['id'], $hasChildrens));
  49. $nodeList[] = array('id' => $v['id'], 'parent' => $v['pid'] ? $v['pid'] : '#', 'text' => __($v['title']), 'type' => 'menu', 'state' => $state);
  50. }
  51. return $nodeList;
  52. }
  53. }