'datetime', ]; // function hasRole($roles){ // return !!$roles->intersect($this->roles()->get())->count(); // } function isSuperAdmin(): bool { $superAdmins=config("users.superAdmin"); foreach ($superAdmins as $superAdmin){ if($this['name']==$superAdmin){ return true; } } return false; } /** * @param null $expireTime 手动设置过期时间则覆盖其中默认时间,分钟为单位 * @return mixed|string */ public function token($expireTime=null){ if(!$expireTime){ $expireTime=config('users.token_expire_minutes'); } $token=Cache::get('tokenUser_'.$this['id']); if($token){ Cache::put('tokenUser_'.$this['id'],$token,$expireTime); Cache::put('tokenStr_'.$token,$this['id'],$expireTime); return $token; } $token=md5(rand(1,intval(microtime(true)*10000)).'baoshi'); Cache::put('tokenUser_'.$this['id'],$token,$expireTime); Cache::put('tokenStr_'.$token,$this['id'],$expireTime); return $token; } public function touchToken(){ return $this->token(); } public function owners(): BelongsToMany { return $this->belongsToMany('App\Owner','owner_user','user_id','owner_id') ->whereNull("deleted_at"); } function roles(){ return $this->belongsToMany('App\Role','user_role','id_user','id_role'); } function userDetail(){ return $this->hasOne('App\UserDetail','user_id','id'); } function logistics(){ return $this->belongsToMany('App\Logistic','logistic_user','user_id','logistic_id'); } function userWorkgroups(){ return $this->belongsToMany('App\UserWorkgroup','user_workgroup_user','user_id','user_workgroup_id'); } function suppliers(){ return $this->belongsToMany('App\Supplier','supplier_user','user_id','supplier_id'); } function authorities(){ $authorities = new Collection([]); $this->roles->each(function ($role)use(&$authorities){ if($role->authorities){ if(!$authorities){ $authorities=$role->authorities; }else{ $authorities=$authorities->merge($role->authorities); } } }); return $authorities; } function getPermittingOwnerIdsAttribute(): array { $ownerIds=[]; if($this->isSuperAdmin()||Gate::allows('货主-可见全部')){ $owners=Owner::query()->whereNull('deleted_at')->get(); $owners->each(function(Owner $owner)use(&$ownerIds){ array_push($ownerIds,$owner['id']); }); return $ownerIds; } //原查询 $old_owner= array_column(DB::table("owner_role")->whereIn("role_id", array_column($this->roles->toArray(),"id"))->get()->toArray(),"owner_id"); //兼容 TODO $new_owner = $this->ownerGroups()->with(['owners:id,user_owner_group_id'])->get(); $new_work= $this->workGroups()->with(['owners:id,user_workgroup_id'])->get(); foreach ($new_owner as $v){ $ownerIds = array_merge($ownerIds, array_column($v->owners->toArray(),'id')); } foreach ($new_work as $v){ $ownerIds = array_merge($ownerIds, array_column($v->owners->toArray(),'id')); } return array_unique(array_merge($ownerIds, $old_owner)); } function getPermittingWorkgroupIds($allowAll=false): array { $workgroupIds=[]; if ($this->isSuperAdmin()||$allowAll){ $workgroups=UserWorkgroup::all(); $workgroups->each(function (UserWorkgroup $workgroup)use(&$workgroupIds){ array_push($workgroupIds,$workgroup['id']); }); }else{ $workgroupIds = array_column(DB::table("role_user_work_group") ->whereIn("role_id",array_column($this->roles->toArray(),"id"))->get()->toArray(),"user_work_group_id"); } return $workgroupIds; } function getPermittingLaborCompanyIdsAttribute(): array { $labor_company_ids=array(); if($this->isSuperAdmin()||Gate::allows('劳务所-可见全部')){ $laborCompanies=LaborCompany::all(); }else{ $userId=$this['id']; $laborCompanies=LaborCompany::query()->whereIn('id',function ($query)use($userId){ $query->from('role_labor_company')->selectRaw('labor_company_id')->whereIn('role_id',function ($builder)use ($userId){ $builder->from('user_role')->selectRaw('id_role')->where('id_user',$userId); }); })->get(); } $laborCompanies->each(function (LaborCompany $laborCompany) use (&$labor_company_ids) { array_push($labor_company_ids, $laborCompany['id']); }); return array_unique($labor_company_ids); } // 用户可见承运商 public function getPermittingLogisticIdsAttribute(): array { if ($this->isSuperAdmin()){ return Logistic::query()->get()->map(function($logistic){return $logistic->id;})->toArray(); } return $this->logistics()->get()->map(function($logistic){return $logistic->id;})->toArray(); } public function workGroups() { return $this->morphedByMany(UserWorkgroup::class, 'user_authable'); } public function ownerGroups() { return $this->morphedByMany(UserOwnerGroup::class, 'user_authable'); } public function seeLogs():BelongsToMany { return $this->belongsToMany(SeeLog::class); } }