User.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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(): bool
  47. {
  48. $superAdmins=config("users.superAdmin");
  49. foreach ($superAdmins as $superAdmin){
  50. if($this['name']==$superAdmin){
  51. return true;
  52. }
  53. }
  54. return false;
  55. }
  56. /**
  57. * @param null $expireTime 手动设置过期时间则覆盖其中默认时间,分钟为单位
  58. * @return mixed|string
  59. */
  60. public function token($expireTime=null){
  61. if(!$expireTime){
  62. $expireTime=config('users.token_expire_minutes');
  63. }
  64. $token=Cache::get('tokenUser_'.$this['id']);
  65. if($token){
  66. Cache::put('tokenUser_'.$this['id'],$token,$expireTime);
  67. Cache::put('tokenStr_'.$token,$this['id'],$expireTime);
  68. return $token;
  69. }
  70. $token=md5(rand(1,intval(microtime(true)*10000)).'baoshi');
  71. Cache::put('tokenUser_'.$this['id'],$token,$expireTime);
  72. Cache::put('tokenStr_'.$token,$this['id'],$expireTime);
  73. return $token;
  74. }
  75. public function touchToken(){
  76. return $this->token();
  77. }
  78. function roles(){
  79. return $this->belongsToMany('App\Role','user_role','id_user','id_role');
  80. }
  81. function userDetail(){
  82. return $this->hasOne('App\UserDetail','user_id','id');
  83. }
  84. function logistics(){
  85. return $this->belongsToMany('App\Logistic','logistic_user','user_id','logistic_id');
  86. }
  87. function userWorkgroups(){
  88. return $this->belongsToMany('App\UserWorkgroup','user_workgroup_user','user_id','user_workgroup_id');
  89. }
  90. function suppliers(){
  91. return $this->belongsToMany('App\Supplier','supplier_user','user_id','supplier_id');
  92. }
  93. function authorities(){
  94. $authorities = new Collection([]);
  95. $this->roles->each(function ($role)use(&$authorities){
  96. if($role->authorities){
  97. if(!$authorities){
  98. $authorities=$role->authorities;
  99. }else{
  100. $authorities=$authorities->merge($role->authorities);
  101. }
  102. }
  103. });
  104. return $authorities;
  105. }
  106. function getPermittingOwnerIdsAttribute(): array
  107. {
  108. $ownerIds=[];
  109. if($this->isSuperAdmin()||Gate::allows('货主-可见全部')){
  110. $owners=Owner::query()->whereNull('deleted_at')->get();
  111. $owners->each(function(Owner $owner)use(&$ownerIds){
  112. array_push($ownerIds,$owner['id']);
  113. });
  114. return $ownerIds;
  115. }
  116. //原查询
  117. $old_owner= array_column(DB::table("owner_role")->whereIn("role_id",
  118. array_column($this->roles->toArray(),"id"))->get()->toArray(),"owner_id");
  119. //兼容
  120. $new_owner = $this->ownerGroups()->with(['owners:id,user_owner_group_id'])->get();
  121. $new_work= $this->workGroups()->with(['owners:id,user_workgroup_id'])->get();
  122. foreach ($new_owner as $v){
  123. $ownerIds = array_merge($ownerIds, array_column($v->owners->toArray(),'id'));
  124. }
  125. foreach ($new_work as $v){
  126. $ownerIds = array_merge($ownerIds, array_column($v->owners->toArray(),'id'));
  127. }
  128. return array_unique(array_merge($ownerIds, $old_owner));
  129. }
  130. function getPermittingWorkgroupIds($allowAll=false): array
  131. {
  132. $workgroupIds=[];
  133. if ($this->isSuperAdmin()||$allowAll){
  134. $workgroups=UserWorkgroup::all();
  135. $workgroups->each(function (UserWorkgroup $workgroup)use(&$workgroupIds){
  136. array_push($workgroupIds,$workgroup['id']);
  137. });
  138. }else{
  139. $workgroupIds = array_column(DB::table("role_user_work_group")
  140. ->whereIn("role_id",array_column($this->roles->toArray(),"id"))->get()->toArray(),"user_work_group_id");
  141. /*$this->authorities()->each(function(Authority $authority)use(&$workgroupIds){
  142. if($authority->type=="工作组"){array_push($workgroupIds,$authority->relevance);}
  143. });*/
  144. }
  145. return $workgroupIds;
  146. }
  147. function getPermittingLaborCompanyIdsAttribute(): array
  148. {
  149. $labor_company_ids=array();
  150. if($this->isSuperAdmin()||Gate::allows('劳务所-可见全部')){
  151. $laborCompanies=LaborCompany::all();
  152. }else{
  153. $userId=$this['id'];
  154. $laborCompanies=LaborCompany::query()->whereIn('id',function ($query)use($userId){
  155. $query->from('role_labor_company')->selectRaw('labor_company_id')->whereIn('role_id',function ($builder)use ($userId){
  156. $builder->from('user_role')->selectRaw('id_role')->where('id_user',$userId);
  157. });
  158. })->get();
  159. }
  160. $laborCompanies->each(function (LaborCompany $laborCompany) use (&$labor_company_ids) {
  161. array_push($labor_company_ids, $laborCompany['id']);
  162. });
  163. return array_unique($labor_company_ids);
  164. }
  165. // 用户可见货主
  166. public function getPermittingLogisticIdsAttribute(): array
  167. {
  168. if ($this->isSuperAdmin()){
  169. return Logistic::query()->get()->map(function($logistic){return $logistic->id;})->toArray();
  170. }
  171. return $this->logistics()->get()->map(function($logistic){return $logistic->id;})->toArray();
  172. }
  173. public function workGroups()
  174. {
  175. return $this->morphedByMany(UserWorkgroup::class, 'user_authable');
  176. }
  177. public function ownerGroups()
  178. {
  179. return $this->morphedByMany(UserOwnerGroup::class, 'user_authable');
  180. }
  181. public function seeLogs():BelongsToMany
  182. {
  183. return $this->belongsToMany(SeeLog::class);
  184. }
  185. }