UserDetail.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Model;
  4. use App\Traits\ModelTimeFormat;
  5. use Illuminate\Support\Facades\Cache;
  6. class UserDetail extends Model
  7. {
  8. use ModelTimeFormat;
  9. protected $primaryKey='user_id';
  10. public $incrementing=false;
  11. protected $fillable=[
  12. 'user_id','full_name','gender','identity_number','mobile_phone','type'
  13. ];
  14. protected $appends=[
  15. 'user_labor_company'
  16. ];
  17. public function user(){
  18. return $this->belongsTo('App\User','user_id','id');
  19. }
  20. public function userLabor(){
  21. return $this->belongsTo('App\UserLabor','user_id','user_id');
  22. }
  23. public function userDutyChecks(){
  24. return $this->hasMany('App\UserDutyCheck','user_id','user_id');
  25. }
  26. public function updateUserName(){
  27. if ($this['mobile_phone']==$this['user']['name'])
  28. $this['user']->update(['name'=>$this['full_name']]);
  29. }
  30. public function getUserLaborCompanyAttribute()
  31. {
  32. $laborCompanyId=$this['userLabor']['labor_company_id']??0;
  33. $laborCompany=LaborCompany::find($laborCompanyId);
  34. return $this['user_labor_company']=$laborCompany['name'];
  35. }
  36. public function getDutyCheckToken($expireTime)
  37. {
  38. if(!$expireTime){
  39. $expireTime=config('users.token_check_in_expire_minutes');
  40. }
  41. $token=Cache::get('dutyCheckTokenUser_'.$this['user_id']);
  42. if($token){
  43. Cache::put('dutyCheckTokenUser_'.$this['user_id'],$token,$expireTime);
  44. Cache::put('dutyCheckTokenStr_'.$token,$this['user_id'],$expireTime);
  45. return $token;
  46. }
  47. $token=md5(rand(1,intval(microtime(true)*10000)).'baoshi');
  48. Cache::put('dutyCheckTokenUser_'.$this['user_id'],$token,$expireTime);
  49. Cache::put('dutyCheckTokenStr_'.$token,$this['user_id'],$expireTime);
  50. return $token;
  51. }
  52. public function hasDutyCheckToken()
  53. {
  54. return Cache::has('dutyCheckTokenUser_'.$this['user_id']);
  55. }
  56. public function markDutyCheckToken()
  57. {
  58. }
  59. }