UserRule.php 1.4 KB

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