AdminRole.php 1.7 KB

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