SystemRole.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\model\system;
  4. use library\traits\JwtAuthModelTrait;
  5. use library\traits\ModelTrait;
  6. use think\Model;
  7. /**
  8. * @mixin \think\Model
  9. */
  10. class SystemRole extends Model
  11. {
  12. use ModelTrait;
  13. use JwtAuthModelTrait;
  14. private $roleData;
  15. /**
  16. * 获取角色数据
  17. * @param $id
  18. * @param string $field
  19. * @return mixed
  20. */
  21. public function getRoleId($id,$field = '*') {
  22. $this->getRoleData();
  23. foreach ($this->roleData as $v) {
  24. if($v['id'] == $id) {
  25. return $field == '*' ? $v : $v[$field];
  26. }
  27. }
  28. }
  29. /**
  30. * @param $post
  31. */
  32. public function saveRole($post) {
  33. $save['name'] = $post['name'];
  34. $save['status'] = $post['status'];
  35. if($post['id']) {
  36. $info = $this->where("name",$post['name'])->find();
  37. if(!empty($info) && $info['id'] != $post['id'] ) {
  38. return self::setErrorInfo('角色名重复了');
  39. }
  40. $this->where('id',$post['id'])->save($save);
  41. return true;
  42. } else {
  43. $this->insert([
  44. 'name' => $post['name'],
  45. 'status' => $post['status']
  46. ]);
  47. }
  48. return true;
  49. }
  50. public function getRoleData(){
  51. if(empty($this->roleData))
  52. $this->roleData = $this->where('status',1)->select();
  53. return $this->roleData;
  54. }
  55. }