User.php 6.3 KB

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