UserDetail.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 getUserLaborCompanyAttribute()
  27. {
  28. $laborCompanyId=$this['userLabor']['labor_company_id']??0;
  29. $laborCompany=LaborCompany::find($laborCompanyId);
  30. return $this['user_labor_company']=$laborCompany['name'];
  31. }
  32. public function getDutyCheckToken($expireTime)
  33. {
  34. if(!$expireTime){
  35. $expireTime=config('users.token_check_in_expire_minutes');
  36. }
  37. $token=Cache::get('dutyCheckTokenUser_'.$this['user_id']);
  38. if($token){
  39. Cache::put('dutyCheckTokenUser_'.$this['user_id'],$token,$expireTime);
  40. Cache::put('dutyCheckTokenStr_'.$token,$this['user_id'],$expireTime);
  41. return $token;
  42. }
  43. $token=md5(rand(1,intval(microtime(true)*10000)).'baoshi');
  44. Cache::put('dutyCheckTokenUser_'.$this['user_id'],$token,$expireTime);
  45. Cache::put('dutyCheckTokenStr_'.$token,$this['user_id'],$expireTime);
  46. return $token;
  47. }
  48. public function hasDutyCheckToken()
  49. {
  50. return Cache::has('dutyCheckTokenUser_'.$this['user_id']);
  51. }
  52. public function markDutyCheckToken()
  53. {
  54. }
  55. }