User.php 6.7 KB

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