菜谱项目

User.php 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace App;
  3. use Illuminate\Notifications\Notifiable;
  4. use Illuminate\Foundation\Auth\User as Authenticatable;
  5. use Tymon\JWTAuth\Facades\JWTAuth;
  6. use Illuminate\Support\Facades\DB;
  7. class User extends Authenticatable
  8. {
  9. use Notifiable;
  10. public $timestamps = false;
  11. /**
  12. * The attributes that are mass assignable.
  13. *
  14. * @var array
  15. */
  16. protected $fillable = [
  17. 'name', 'phone', 'password',
  18. ];
  19. /**
  20. * The attributes that should be hidden for arrays.
  21. *
  22. * @var array
  23. */
  24. protected $hidden = [
  25. 'password', 'remember_token',
  26. ];
  27. /**
  28. * Get the identifier that will be stored in the subject claim of the JWT.
  29. *
  30. * @return mixed
  31. */
  32. public function getJWTIdentifier()
  33. {
  34. return $this->getKey();
  35. }
  36. /**
  37. * Return a key value array, containing any custom claims to be added to the JWT.
  38. *
  39. * @return array
  40. */
  41. public function getJWTCustomClaims()
  42. {
  43. return [];
  44. }
  45. public static function updateUserLoginInfo($userid, $data) {
  46. $status = DB::table('users')->where('id', $userid)->update(['token' => $data['token'],'phone_verified'=>1,'last_login_time'=>$data['last_login_time'],'login_num'=>$data['login_num']+1]);
  47. return $status;
  48. }
  49. public static function updateToken($userid, $token) {
  50. $status = DB::table('users')->where('id', $userid)->update(['token' => $token]);
  51. return $status;
  52. }
  53. public static function getCurrentUser() {
  54. $token = getenv('HTTP_TOKEN') ? getenv('HTTP_TOKEN') : (isset($_GET['token']) ? $_GET['token'] : '');
  55. if($token) {
  56. return self::getUserInfoByToken($token);
  57. } else {
  58. return array();
  59. }
  60. }
  61. public static function getUserInfoByToken($token) {
  62. $user = User::where('token', $token)->first();
  63. return !empty($user) ? $user : null;
  64. }
  65. public static function parseToken($token) {
  66. $user = DB::table('users')->where('token', $token)->first();
  67. return !empty($user) ? $user : null;
  68. }
  69. public function getAccounts()
  70. {
  71. return $this->belongsToMany('App\Models\Account', 'user_account', 'user_id', 'account_id')->withPivot('is_master','tid');
  72. }
  73. public static function updatePhoneVerified($mobile,$type) //检测账户注册到哪个步骤
  74. {
  75. $user_info = self::where('mobile', $mobile)->first();
  76. if (!$user_info) {
  77. $user_info = new self();
  78. $user_info->mobile = $mobile;
  79. }
  80. if ($type) $user_info->phone_verified = $type;
  81. $user_info->save();
  82. return $user_info;
  83. }
  84. }