User.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. public function owners(): BelongsToMany
  83. {
  84. return $this->belongsToMany('App\Owner','owner_user','user_id','owner_id')
  85. ->whereNull("deleted_at");
  86. }
  87. function roles(){
  88. return $this->belongsToMany('App\Role','user_role','id_user','id_role');
  89. }
  90. function userDetail(){
  91. return $this->hasOne('App\UserDetail','user_id','id');
  92. }
  93. function logistics(){
  94. return $this->belongsToMany('App\Logistic','logistic_user','user_id','logistic_id');
  95. }
  96. function userWorkgroups(){
  97. return $this->belongsToMany('App\UserWorkgroup','user_workgroup_user','user_id','user_workgroup_id');
  98. }
  99. function suppliers(){
  100. return $this->belongsToMany('App\Supplier','supplier_user','user_id','supplier_id');
  101. }
  102. function authorities(){
  103. $authorities = new Collection([]);
  104. $this->roles->each(function ($role)use(&$authorities){
  105. if($role->authorities){
  106. if(!$authorities){
  107. $authorities=$role->authorities;
  108. }else{
  109. $authorities=$authorities->merge($role->authorities);
  110. }
  111. }
  112. });
  113. return $authorities;
  114. }
  115. function getPermittingOwnerIdsAttribute(): array
  116. {
  117. $ownerIds=[];
  118. if($this->isSuperAdmin()||Gate::allows('货主-可见全部')){
  119. $owners=Owner::query()->whereNull('deleted_at')->get();
  120. $owners->each(function(Owner $owner)use(&$ownerIds){
  121. array_push($ownerIds,$owner['id']);
  122. });
  123. return $ownerIds;
  124. }
  125. //原查询
  126. $old_owner= array_column(DB::table("owner_role")->whereIn("role_id",
  127. array_column($this->roles->toArray(),"id"))->get()->toArray(),"owner_id");
  128. //兼容 TODO
  129. $new_owner = $this->ownerGroups()->with(['owners:id,user_owner_group_id'])->get();
  130. $new_work= $this->workGroups()->with(['owners:id,user_workgroup_id'])->get();
  131. foreach ($new_owner as $v){
  132. $ownerIds = array_merge($ownerIds, array_column($v->owners->toArray(),'id'));
  133. }
  134. foreach ($new_work as $v){
  135. $ownerIds = array_merge($ownerIds, array_column($v->owners->toArray(),'id'));
  136. }
  137. return array_unique(array_merge($ownerIds, $old_owner));
  138. }
  139. function getPermittingWorkgroupIds($allowAll=false): array
  140. {
  141. $workgroupIds=[];
  142. if ($this->isSuperAdmin()||$allowAll){
  143. $workgroups=UserWorkgroup::all();
  144. $workgroups->each(function (UserWorkgroup $workgroup)use(&$workgroupIds){
  145. array_push($workgroupIds,$workgroup['id']);
  146. });
  147. }else{
  148. $workgroupIds = array_column(DB::table("role_user_work_group")
  149. ->whereIn("role_id",array_column($this->roles->toArray(),"id"))->get()->toArray(),"user_work_group_id");
  150. }
  151. return $workgroupIds;
  152. }
  153. function getPermittingLaborCompanyIdsAttribute(): array
  154. {
  155. $labor_company_ids=array();
  156. if($this->isSuperAdmin()||Gate::allows('劳务所-可见全部')){
  157. $laborCompanies=LaborCompany::all();
  158. }else{
  159. $userId=$this['id'];
  160. $laborCompanies=LaborCompany::query()->whereIn('id',function ($query)use($userId){
  161. $query->from('role_labor_company')->selectRaw('labor_company_id')->whereIn('role_id',function ($builder)use ($userId){
  162. $builder->from('user_role')->selectRaw('id_role')->where('id_user',$userId);
  163. });
  164. })->get();
  165. }
  166. $laborCompanies->each(function (LaborCompany $laborCompany) use (&$labor_company_ids) {
  167. array_push($labor_company_ids, $laborCompany['id']);
  168. });
  169. return array_unique($labor_company_ids);
  170. }
  171. // 用户可见承运商
  172. public function getPermittingLogisticIdsAttribute(): array
  173. {
  174. if ($this->isSuperAdmin()){
  175. return Logistic::query()->get()->map(function($logistic){return $logistic->id;})->toArray();
  176. }
  177. return $this->logistics()->get()->map(function($logistic){return $logistic->id;})->toArray();
  178. }
  179. public function workGroups()
  180. {
  181. return $this->morphedByMany(UserWorkgroup::class, 'user_authable');
  182. }
  183. public function ownerGroups()
  184. {
  185. return $this->morphedByMany(UserOwnerGroup::class, 'user_authable');
  186. }
  187. public function seeLogs():BelongsToMany
  188. {
  189. return $this->belongsToMany(SeeLog::class);
  190. }
  191. }