first commit
This commit is contained in:
140
app/Models/User.php
Normal file
140
app/Models/User.php
Normal file
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\Traits\HasAccount;
|
||||
use App\Models\Traits\HasArea;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Illuminate\Support\Str;
|
||||
use RuLong\Identity\Traits\UserHasIdentity;
|
||||
|
||||
class User extends Authenticatable
|
||||
{
|
||||
|
||||
use Notifiable, HasAccount, UserHasIdentity, HasArea;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $guarded = [];
|
||||
|
||||
/**
|
||||
* The attributes that should be hidden for arrays.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $hidden = [
|
||||
'password', 'remember_token',
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $appends = [
|
||||
'nickname',
|
||||
];
|
||||
|
||||
/**
|
||||
* 默认加载的关联
|
||||
* @var array
|
||||
*/
|
||||
protected $with = ['info', 'identity', 'code'];
|
||||
|
||||
protected static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
self::created(function ($model) {
|
||||
if ($model->parent_id) {
|
||||
$parent = self::find($model->parent_id);
|
||||
$model->server_id = $parent->server_id;
|
||||
$model->outlet_id = date('ymdHi') . mt_rand(100, 999);
|
||||
$model->IdentityUpdate(2);
|
||||
} else {
|
||||
$model->server_id = date('Ymd') . mt_rand(100000, 999999);
|
||||
$model->server_key = Str::random(32);
|
||||
$model->des3key = Str::random(24);
|
||||
$model->IdentityUpdate(1);
|
||||
}
|
||||
|
||||
$model->info()->create([
|
||||
'nickname' => $model->username,
|
||||
]);
|
||||
$model->save();
|
||||
});
|
||||
}
|
||||
|
||||
protected function getNicknameAttribute()
|
||||
{
|
||||
return $this->info ? $this->info->nickname : '';
|
||||
}
|
||||
|
||||
public function info()
|
||||
{
|
||||
return $this->hasOne(UserInfo::class);
|
||||
}
|
||||
|
||||
//渠道商配置
|
||||
public function code()
|
||||
{
|
||||
return $this->hasMany(UserCode::class);
|
||||
}
|
||||
|
||||
public function parent()
|
||||
{
|
||||
return $this->hasOne(User::class, 'id', 'parent_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 密码加密
|
||||
* @Author: <C.Jason>
|
||||
* @Date: 2019/9/6 11:37
|
||||
* @param $password
|
||||
*/
|
||||
protected function setPasswordAttribute($password)
|
||||
{
|
||||
if (!empty($password)) {
|
||||
$this->attributes['password'] = bcrypt($password);
|
||||
}
|
||||
}
|
||||
|
||||
public function getTypeTextAttribute()
|
||||
{
|
||||
switch ($this->type) {
|
||||
case 'pingan':
|
||||
return '平安券';
|
||||
break;
|
||||
case 'wo':
|
||||
return '沃支付';
|
||||
break;
|
||||
default:
|
||||
return '未知';
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 关联平安卡券核销
|
||||
* @author 玄尘 2020-04-03
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function coupons()
|
||||
{
|
||||
return $this->hasMany(Coupon::class);
|
||||
}
|
||||
|
||||
public function getCouponCount($type, $date = '')
|
||||
{
|
||||
return $this->coupons()
|
||||
->whereIn('status', [2])
|
||||
->where('thirdPartyGoodsId', $type)
|
||||
->when($date, function ($q) {
|
||||
$q->whereDate('created_at', now()->format('Y-m-d'));
|
||||
})
|
||||
->count();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user