UserRule.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 getTitleAttr($value, $data)
  26. {
  27. return __($value);
  28. }
  29. public function getStatusList()
  30. {
  31. return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
  32. }
  33. public function getStatusTextAttr($value, $data)
  34. {
  35. $value = $value ? $value : $data['status'];
  36. $list = $this->getStatusList();
  37. return isset($list[$value]) ? $list[$value] : '';
  38. }
  39. public static function getTreeList($selected = [])
  40. {
  41. $ruleList = collection(self::where('status', 'normal')->order('weigh desc,id desc')->select())->toArray();
  42. $nodeList = [];
  43. Tree::instance()->init($ruleList);
  44. $ruleList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'name');
  45. $hasChildrens = [];
  46. foreach ($ruleList as $k => $v)
  47. {
  48. if ($v['haschild'])
  49. $hasChildrens[] = $v['id'];
  50. }
  51. foreach ($ruleList as $k => $v) {
  52. $state = array('selected' => in_array($v['id'], $selected) && !in_array($v['id'], $hasChildrens));
  53. $nodeList[] = array('id' => $v['id'], 'parent' => $v['pid'] ? $v['pid'] : '#', 'text' => __($v['title']), 'type' => 'menu', 'state' => $state);
  54. }
  55. return $nodeList;
  56. }
  57. }