| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- <?php
- namespace App;
- use App\Traits\ModelLogChanging;
- use Illuminate\Database\Eloquent\Relations\BelongsToMany;
- use Illuminate\Database\Eloquent\SoftDeletes;
- use Illuminate\Notifications\Notifiable;
- use Illuminate\Foundation\Auth\User as Authenticatable;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Gate;
- use App\Traits\ModelTimeFormat;
- class User extends Authenticatable
- {
- use ModelLogChanging;
- use ModelTimeFormat;
- use Notifiable;
- use SoftDeletes;
- /**
- * 安卓单点登录记录标识
- */
- const ANDROID_SINGLE_TAG = "ANDROID_SINGLE";
- /**
- * The attributes that are mass assignable.
- *
- * @var array
- */
- protected $fillable = [
- 'name', 'email', 'password'
- ];
- /**
- * The attributes that should be hidden for arrays.
- *
- * @var array
- */
- protected $hidden = [
- 'password', 'remember_token',
- ];
- /**
- * The attributes that should be cast to native types.
- *
- * @var array
- */
- protected $casts = [
- 'email_verified_at' => '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);
- }
- }
|