User.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace App;
  3. use App\Traits\LogModelChanging;
  4. use Illuminate\Notifications\Notifiable;
  5. use Illuminate\Foundation\Auth\User as Authenticatable;
  6. use Illuminate\Support\Collection;
  7. use Illuminate\Support\Facades\Cache;
  8. use Illuminate\Support\Facades\Gate;
  9. use App\Traits\ModelTimeFormat;
  10. class User extends Authenticatable
  11. {
  12. use LogModelChanging;
  13. use ModelTimeFormat;
  14. use Notifiable;
  15. /**
  16. * The attributes that are mass assignable.
  17. *
  18. * @var array
  19. */
  20. protected $fillable = [
  21. 'name', 'email', 'password'
  22. ];
  23. /**
  24. * The attributes that should be hidden for arrays.
  25. *
  26. * @var array
  27. */
  28. protected $hidden = [
  29. 'password', 'remember_token',
  30. ];
  31. /**
  32. * The attributes that should be cast to native types.
  33. *
  34. * @var array
  35. */
  36. protected $casts = [
  37. 'email_verified_at' => 'datetime',
  38. ];
  39. // function hasRole($roles){
  40. // return !!$roles->intersect($this->roles()->get())->count();
  41. // }
  42. function isSuperAdmin(){
  43. $superAdmins=config("users.superAdmin");
  44. foreach ($superAdmins as $superAdmin){
  45. if($this['name']==$superAdmin){
  46. return true;
  47. }
  48. }
  49. return false;
  50. }
  51. /**
  52. * @param null $expireTime 手动设置过期时间则覆盖其中默认时间,分钟为单位
  53. * @return mixed|string
  54. */
  55. public function token($expireTime=null){
  56. if(!$expireTime){
  57. $expireTime=config('users.token_expire_minutes');
  58. }
  59. $token=Cache::get('tokenUser_'.$this['id']);
  60. if($token){
  61. Cache::put('tokenUser_'.$this['id'],$token,$expireTime);
  62. Cache::put('tokenStr_'.$token,$this['id'],$expireTime);
  63. return $token;
  64. }
  65. $token=md5(rand(1,intval(microtime(true)*10000)).'baoshi');
  66. Cache::put('tokenUser_'.$this['id'],$token,$expireTime);
  67. Cache::put('tokenStr_'.$token,$this['id'],$expireTime);
  68. return $token;
  69. }
  70. public function touchToken(){
  71. return $this->token();
  72. }
  73. function roles(){
  74. return $this->belongsToMany('App\Role','user_role','id_user','id_role');
  75. }
  76. function logistics(){
  77. return $this->belongsToMany('App\Logistic','logistic_user','user_id','logistic_id');
  78. }
  79. function userWorkgroups(){
  80. return $this->belongsToMany('App\UserWorkgroup','user_workgroup_user','user_id','user_workgroup_id');
  81. }
  82. function authorities(){
  83. $authorities = new Collection([]);
  84. $user=User::query()->where('id',$this['id'])->with('roles.authorities')->first();
  85. $user->roles->each(function ($role)use(&$authorities){
  86. if($role->authorities){
  87. if(!$authorities){
  88. $authorities=$role->authorities;
  89. }else{
  90. $authorities=$authorities->merge($role->authorities);
  91. }
  92. }
  93. });
  94. return $authorities;
  95. }
  96. function getPermittingOwnerIdsAttribute(){
  97. $ownerIds=[];
  98. if($this->isSuperAdmin()||Gate::allows('货主-可见全部')){
  99. $owners=Owner::query()->whereNull('deleted_at')->get();
  100. $owners->each(function(Owner $owner)use(&$ownerIds){
  101. array_push($ownerIds,$owner['id']);
  102. });
  103. return $ownerIds;
  104. }
  105. $this->authorities()->each(function(Authority $authority)use(&$ownerIds){
  106. $ownerId=$authority->getOwnerIdAttribute();
  107. if($ownerId){array_push($ownerIds,$ownerId);}
  108. });
  109. return array_unique($ownerIds);
  110. }
  111. function getPermittingWorkgroupIds($allowAll=false){
  112. $workgroupIds=[];
  113. if ($this->isSuperAdmin()||$allowAll){
  114. $workgroups=UserWorkgroup::all();
  115. $workgroups->each(function (UserWorkgroup $workgroup)use(&$workgroupIds){
  116. array_push($workgroupIds,$workgroup['id']);
  117. });
  118. }else{
  119. $this->authorities()->each(function(Authority $authority)use(&$workgroupIds){
  120. if($authority->type=="工作组"){array_push($workgroupIds,$authority->relevance);}
  121. });
  122. }
  123. return $workgroupIds;
  124. }
  125. }