User.php 6.8 KB

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