2017-12-24 14:17:18 +00:00
|
|
|
<?php
|
|
|
|
|
2017-12-30 20:23:09 +00:00
|
|
|
namespace App\Models;
|
2017-12-24 14:17:18 +00:00
|
|
|
|
2018-06-02 15:37:19 +00:00
|
|
|
use App\Traits\ActiveUserHelper;
|
|
|
|
use App\Traits\LastActivedAtHelper;
|
2017-12-24 14:17:18 +00:00
|
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
|
|
use Illuminate\Notifications\Notifiable;
|
2018-01-27 10:35:56 +00:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2018-06-11 15:10:59 +00:00
|
|
|
use Spatie\Permission\Models\Role;
|
2018-01-31 15:53:56 +00:00
|
|
|
use Spatie\Permission\Traits\HasRoles;
|
2018-06-03 15:45:49 +00:00
|
|
|
use Tymon\JWTAuth\Contracts\JWTSubject;
|
2017-12-24 14:17:18 +00:00
|
|
|
|
2018-06-02 15:37:19 +00:00
|
|
|
/**
|
|
|
|
* Class User
|
|
|
|
* @property string avatar
|
2018-06-11 15:10:59 +00:00
|
|
|
* @property string name
|
|
|
|
* @property string email
|
|
|
|
* @property string introduction
|
|
|
|
* @property string phone
|
|
|
|
* @property string weixin_unionid
|
|
|
|
* @property string weixin_openid
|
|
|
|
* @property Role roles
|
2018-06-02 15:37:19 +00:00
|
|
|
* @package App\Models
|
|
|
|
*/
|
2018-06-03 15:45:49 +00:00
|
|
|
class User extends Authenticatable implements JWTSubject
|
2017-12-24 14:17:18 +00:00
|
|
|
{
|
2018-01-27 10:35:56 +00:00
|
|
|
use Notifiable {
|
|
|
|
notify as protected laravelNotify;
|
|
|
|
}
|
2018-01-31 15:53:56 +00:00
|
|
|
use HasRoles;
|
2018-06-02 10:21:53 +00:00
|
|
|
use ActiveUserHelper;
|
2018-06-02 15:37:19 +00:00
|
|
|
use LastActivedAtHelper;
|
2017-12-24 14:17:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $fillable = [
|
2018-06-03 13:09:33 +00:00
|
|
|
'name', 'phone', 'email', 'password', 'introduction', 'avatar ',
|
2018-06-16 02:18:41 +00:00
|
|
|
'weixin_openid', 'weixin_unionid', 'registration_id'
|
2017-12-24 14:17:18 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The attributes that should be hidden for arrays.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $hidden = [
|
|
|
|
'password', 'remember_token',
|
|
|
|
];
|
2017-12-30 20:23:09 +00:00
|
|
|
|
2018-06-03 15:45:49 +00:00
|
|
|
public function getJWTIdentifier()
|
|
|
|
{
|
|
|
|
return $this->getKey();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getJWTCustomClaims()
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2018-06-02 15:37:19 +00:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-12-30 20:23:09 +00:00
|
|
|
public function getHeaderAttribute()
|
|
|
|
{
|
2018-06-02 15:58:22 +00:00
|
|
|
$hash = md5(strtolower(trim($this->attributes['email'])));
|
|
|
|
$header = "http://www.gravatar.com/avatar/$hash";
|
|
|
|
return $this->avatar ? asset($this->avatar) : $header;
|
2017-12-30 20:23:09 +00:00
|
|
|
}
|
2018-01-01 07:32:35 +00:00
|
|
|
|
2018-06-02 15:37:19 +00:00
|
|
|
public function getLastActiveATAttribute($value)
|
|
|
|
{
|
|
|
|
return $this->getLastActivedAt($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $path
|
|
|
|
*/
|
2018-02-12 15:56:05 +00:00
|
|
|
public function setAvatarAttribute($path)
|
|
|
|
{
|
|
|
|
if (!starts_with($path, 'http')) {
|
|
|
|
$path = config('app.url') . "/storage/app/public/avatar/{$path}";
|
|
|
|
}
|
|
|
|
$this->attributes['avatar'] = $path;
|
|
|
|
}
|
|
|
|
|
2018-06-02 15:37:19 +00:00
|
|
|
/**
|
|
|
|
* @param $model
|
|
|
|
* @return bool
|
|
|
|
*/
|
2018-01-01 13:22:16 +00:00
|
|
|
public function isAuthorOf($model)
|
|
|
|
{
|
|
|
|
return $this->id == $model->user_id;
|
|
|
|
}
|
|
|
|
|
2018-06-02 15:37:19 +00:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
2018-01-15 15:24:11 +00:00
|
|
|
public function replies()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Reply::class, 'user_id');
|
|
|
|
}
|
|
|
|
|
2018-06-02 15:37:19 +00:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
2018-01-01 07:32:35 +00:00
|
|
|
public function topics()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Topic::class);
|
|
|
|
}
|
2018-01-27 10:35:56 +00:00
|
|
|
|
2018-06-02 15:37:19 +00:00
|
|
|
/**
|
|
|
|
* @param $value
|
|
|
|
*/
|
2018-02-12 15:56:05 +00:00
|
|
|
public function setPasswordAttribute($value)
|
|
|
|
{
|
|
|
|
if (!isset($value[59])) {
|
|
|
|
$value = bcrypt($value);
|
|
|
|
}
|
|
|
|
$this->attributes['password'] = $value;
|
|
|
|
}
|
|
|
|
|
2018-06-02 15:37:19 +00:00
|
|
|
/**
|
|
|
|
* @param $instance
|
|
|
|
*/
|
2018-01-27 10:35:56 +00:00
|
|
|
public function notify($instance)
|
|
|
|
{
|
|
|
|
if ($this->id == Auth::id()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->increment('notification_count');
|
|
|
|
$this->laravelNotify($instance);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function markAsRead()
|
|
|
|
{
|
|
|
|
$this->notification_count = 0;
|
|
|
|
$this->save();
|
|
|
|
$this->unreadNotifications->markAsRead();
|
|
|
|
}
|
2017-12-24 14:17:18 +00:00
|
|
|
}
|