User.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. //原查询
  114. $old_owner= array_column(DB::table("owner_role")->whereIn("role_id",
  115. array_column($this->roles->toArray(),"id"))->get()->toArray(),"owner_id");
  116. //兼容
  117. $new_owner = $this->ownerGroups()->with(['owners:id,user_owner_group_id'])->get();
  118. $new_work= $this->workGroups()->with(['owners:id,user_workgroup_id'])->get();
  119. foreach ($new_owner as $v){
  120. $ownerIds = array_merge($ownerIds, array_column($v->owners->toArray(),'id'));
  121. }
  122. foreach ($new_work as $v){
  123. $ownerIds = array_merge($ownerIds, array_column($v->owners->toArray(),'id'));
  124. }
  125. return array_unique(array_merge($ownerIds, $old_owner));
  126. }
  127. function getPermittingWorkgroupIds($allowAll=false){
  128. $workgroupIds=[];
  129. if ($this->isSuperAdmin()||$allowAll){
  130. $workgroups=UserWorkgroup::all();
  131. $workgroups->each(function (UserWorkgroup $workgroup)use(&$workgroupIds){
  132. array_push($workgroupIds,$workgroup['id']);
  133. });
  134. }else{
  135. $workgroupIds = array_column(DB::table("role_user_work_group")
  136. ->whereIn("role_id",array_column($this->roles->toArray(),"id"))->get()->toArray(),"user_work_group_id");
  137. /*$this->authorities()->each(function(Authority $authority)use(&$workgroupIds){
  138. if($authority->type=="工作组"){array_push($workgroupIds,$authority->relevance);}
  139. });*/
  140. }
  141. return $workgroupIds;
  142. }
  143. function getPermittingLaborCompanyIdsAttribute(): array
  144. {
  145. $labor_company_ids=array();
  146. if($this->isSuperAdmin()||Gate::allows('劳务所-可见全部')){
  147. $laborCompanies=LaborCompany::all();
  148. }else{
  149. $userId=$this['id'];
  150. $laborCompanies=LaborCompany::query()->whereIn('id',function ($query)use($userId){
  151. $query->from('role_labor_company')->selectRaw('labor_company_id')->whereIn('role_id',function ($builder)use ($userId){
  152. $builder->from('user_role')->selectRaw('id_role')->where('id_user',$userId);
  153. });
  154. })->get();
  155. }
  156. $laborCompanies->each(function (LaborCompany $laborCompany) use (&$labor_company_ids) {
  157. array_push($labor_company_ids, $laborCompany['id']);
  158. });
  159. return array_unique($labor_company_ids);
  160. }
  161. public function workGroups()
  162. {
  163. return $this->morphedByMany(UserWorkgroup::class, 'user_authable');
  164. }
  165. public function ownerGroups()
  166. {
  167. return $this->morphedByMany(UserOwnerGroup::class, 'user_authable');
  168. }
  169. }