User.php 5.6 KB

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