UserDetail.php 2.1 KB

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