更新代码
This commit is contained in:
4
packages/identity/README.md
Normal file
4
packages/identity/README.md
Normal file
@@ -0,0 +1,4 @@
|
||||
# Identity 身份系统
|
||||
# trait User 模型 涉及函数 RuLong\Identity\Traits\UserHasIdentity
|
||||
# 身份变更 $User->identityUpdate(Identity_id,Channel) // (身份ID,变更渠道)
|
||||
# 身份加权点数 $User->addPoint(Identity_id,Point) // (身份ID,加权数额:默认1)
|
||||
27
packages/identity/composer.json
Normal file
27
packages/identity/composer.json
Normal file
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"name": "rulong/identity",
|
||||
"description": "",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "C.Jason",
|
||||
"email": "chenjxlg@163.com"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=7.1.3",
|
||||
"laravel/framework": "*"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-0": {
|
||||
"RuLong\\Identity": "src/"
|
||||
}
|
||||
},
|
||||
"extra": {
|
||||
"laravel": {
|
||||
"providers": [
|
||||
"RuLong\\Identity\\ServiceProvider"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
8
packages/identity/config/rulong_identity.php
Normal file
8
packages/identity/config/rulong_identity.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
return [
|
||||
'user_model' => \App\User::class,
|
||||
'channel' => [
|
||||
'AutoUp' => '自动升级',
|
||||
'AdminUp' => '后台升级',
|
||||
],
|
||||
];
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
|
||||
class CreateIdentitiesTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('identities', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('identities');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
|
||||
class CreateIdentityLogsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('identity_logs', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('identity_logs');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
|
||||
class CreateUserIdentitiesTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('user_identities', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('user_identities');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
|
||||
class CreateIdentityPointsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('identity_points', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('identity_points');
|
||||
}
|
||||
}
|
||||
15
packages/identity/src/Contracts/IdentityContracts.php
Normal file
15
packages/identity/src/Contracts/IdentityContracts.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace RuLong\Identity\Contracts;
|
||||
|
||||
interface IdentityContracts
|
||||
{
|
||||
|
||||
/**
|
||||
* 子规则的实际计算过程
|
||||
* @Author:<C.Jason>
|
||||
* @Date:2018-10-31T14:22:48+0800
|
||||
*/
|
||||
function fire();
|
||||
|
||||
}
|
||||
39
packages/identity/src/Events/IdentityPoint.php
Normal file
39
packages/identity/src/Events/IdentityPoint.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace RuLong\Identity\Events;
|
||||
|
||||
use RuLong\Identity\Contracts\IdentityContracts;
|
||||
use RuLong\Identity\Models\IdentityPoint as IdentityPointModel;
|
||||
use RuLong\Identity\Traits\Identityable;
|
||||
|
||||
class IdentityPoint implements IdentityContracts
|
||||
{
|
||||
use Identityable;
|
||||
|
||||
public $user;
|
||||
public $identity_id;
|
||||
public $point;
|
||||
|
||||
public function __construct($user, array $other = null)
|
||||
{
|
||||
$this->user = $user;
|
||||
$this->identity_id = $other['identity_id'];
|
||||
if (isset($other['point']) && is_numeric($other['point'])) {
|
||||
$this->point = $other['point'];
|
||||
} else {
|
||||
$this->point = 1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建及修改身份
|
||||
* @Author:<Leady>
|
||||
* @Date:2018-11-05T13:21:47+0800
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function fire()
|
||||
{
|
||||
IdentityPointModel::create(['user_id' => $this->user->id, 'identity_id' => $this->identity_id, 'point' => $this->point]);
|
||||
}
|
||||
|
||||
}
|
||||
61
packages/identity/src/Events/IdentityUpdate.php
Normal file
61
packages/identity/src/Events/IdentityUpdate.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace RuLong\Identity\Events;
|
||||
|
||||
use RuLong\Identity\Contracts\IdentityContracts;
|
||||
use RuLong\Identity\Models\Identity as IdentityModel;
|
||||
use RuLong\Identity\Models\IdentityLog as IdentityLogModel;
|
||||
use RuLong\Identity\Models\UserIdentity as UserIdentityModel;
|
||||
use RuLong\Identity\Traits\Identityable;
|
||||
|
||||
class IdentityUpdate implements IdentityContracts
|
||||
{
|
||||
use Identityable;
|
||||
|
||||
public $user;
|
||||
public $identity_id;
|
||||
public $channel;
|
||||
public $other;
|
||||
public $maxKey;
|
||||
public function __construct($user, array $other = null)
|
||||
{
|
||||
$this->user = $user;
|
||||
$this->identity_id = $other['identity_id'];
|
||||
$this->channel = $other['channel'] ?? '';
|
||||
$this->maxKey = $other['max_key'] ?? 0;
|
||||
if ($this->channel == 'AdminUp') {
|
||||
$info = IdentityModel::find($this->identity_id);
|
||||
if ($other['pay'] == 1) {
|
||||
$this->other = [
|
||||
'full' => $info->configs['full'] ?? '',
|
||||
'cost' => $info->configs['cost'] ?? '',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建及修改身份
|
||||
* @Author:<Leady>
|
||||
* @Date:2018-11-05T13:21:47+0800
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function fire()
|
||||
{
|
||||
$before = 0;
|
||||
if ($this->user->identity->identity_id === 0) {
|
||||
$data = [
|
||||
'user_id' => $this->user->id,
|
||||
'identity_id' => $this->identity_id,
|
||||
'max_key' => $this->maxKey,
|
||||
];
|
||||
UserIdentityModel::create($data);
|
||||
} else {
|
||||
$before = $this->user->identity->identity_id;
|
||||
UserIdentityModel::where('user_id', $this->user->id)->update(['identity_id' => $this->identity_id, 'max_key' => $this->maxKey]);
|
||||
}
|
||||
IdentityLogModel::create(['user_id' => $this->user->id, 'before' => $before, 'after' => $this->identity_id, 'channel' => $this->channel, 'other' => $this->other]);
|
||||
}
|
||||
|
||||
}
|
||||
20
packages/identity/src/Identity.php
Normal file
20
packages/identity/src/Identity.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace RuLong\Identity;
|
||||
|
||||
use RuLong\Identity\Contracts\IdentityContracts;
|
||||
|
||||
class Identity
|
||||
{
|
||||
protected $contracts;
|
||||
|
||||
public function __construct(IdentityContracts $contracts)
|
||||
{
|
||||
$this->contracts = $contracts;
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
$this->contracts->fire();
|
||||
}
|
||||
}
|
||||
13
packages/identity/src/Models/Identity.php
Normal file
13
packages/identity/src/Models/Identity.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace RuLong\Identity\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Identity extends Model
|
||||
{
|
||||
//
|
||||
protected $casts = [
|
||||
'configs' => 'array',
|
||||
];
|
||||
}
|
||||
35
packages/identity/src/Models/IdentityLog.php
Normal file
35
packages/identity/src/Models/IdentityLog.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace RuLong\Identity\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class IdentityLog extends Model
|
||||
{
|
||||
protected $guarded = [];
|
||||
|
||||
protected $casts = [
|
||||
'other' => 'array',
|
||||
];
|
||||
|
||||
//
|
||||
protected function user()
|
||||
{
|
||||
return $this->belongsTo(config('user_account.user_model'))->withDefault();
|
||||
}
|
||||
|
||||
protected function getIdentityTitleAttribute()
|
||||
{
|
||||
return Identity::where('id', $this->after)->value('title') ?: '';
|
||||
}
|
||||
|
||||
public function getChannelTextAttribute()
|
||||
{
|
||||
return config('rulong_identity.channel.' . $this->channel) ?? '无';
|
||||
}
|
||||
|
||||
public function user_obj()
|
||||
{
|
||||
return $this->belongsTo(config('rulong_bonus.user_model'),'user_id','id');
|
||||
}
|
||||
}
|
||||
15
packages/identity/src/Models/IdentityPoint.php
Normal file
15
packages/identity/src/Models/IdentityPoint.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace RuLong\Identity\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class IdentityPoint extends Model
|
||||
{
|
||||
protected $guarded = [];
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(config('user_account.user_model'), 'user_id', 'id');
|
||||
}
|
||||
}
|
||||
81
packages/identity/src/Models/UserIdentity.php
Normal file
81
packages/identity/src/Models/UserIdentity.php
Normal file
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace RuLong\Identity\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use RuLong\UserRelation\Models\UserRelation;
|
||||
|
||||
class UserIdentity extends Model
|
||||
{
|
||||
protected $primaryKey = 'user_id';
|
||||
public $incrementing = false;
|
||||
protected $guarded = [];
|
||||
|
||||
public function info()
|
||||
{
|
||||
return $this->belongsTo(Identity::class, 'identity_id');
|
||||
}
|
||||
|
||||
public function logs()
|
||||
{
|
||||
return $this->hasMany(IdentityLog::class, 'user_id', 'user_id');
|
||||
}
|
||||
|
||||
public function getActivationChildsAttribute()
|
||||
{
|
||||
$childrenIds = $this->user->children()->pluck('id'); //拉入直推会员ID
|
||||
$direct = self::when($childrenIds, function ($query) use ($childrenIds) {
|
||||
$query->whereIn('user_id', $childrenIds);
|
||||
})->count() ?: 0;
|
||||
return $direct;
|
||||
}
|
||||
|
||||
public function childrentime($timeBetween = '')
|
||||
{
|
||||
$childrenIds = $this->user->children()->pluck('id'); //拉入直推会员ID
|
||||
return self::when($childrenIds, function ($query) use ($childrenIds) {
|
||||
$query->whereIn('user_id', $childrenIds);
|
||||
})->when($timeBetween, function ($query) use ($timeBetween) {
|
||||
$query->whereBetween('created_at', $timeBetween);
|
||||
})->count() ?: 0;
|
||||
}
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(config('user_account.user_model'))->withDefault();
|
||||
}
|
||||
|
||||
/**
|
||||
* 按指定层数内获取推荐人ID
|
||||
* @Author:<Leady>
|
||||
* @Date:2018-12-06T11:45:44+0800
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function relationid($layer = 6)
|
||||
{
|
||||
$users = UserRelation::where('bloodline', 'like', "%," . $this->user_id . ",%")->where('layer', '<=', $this->user->relation->layer + $layer)->pluck('user_id');
|
||||
return $users; //拉入9层
|
||||
}
|
||||
|
||||
/**
|
||||
* 按指定层数获取推荐人ID
|
||||
* @Author:<Leady>
|
||||
* @Date:2018-12-06T11:45:44+0800
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function realrelationid($layer = 6)
|
||||
{
|
||||
$users = UserRelation::where('bloodline', 'like', "%," . $this->user_id . ",%")->where('layer', $this->user->relation->layer + $layer)->pluck('user_id');
|
||||
return $users; //拉入9层
|
||||
}
|
||||
|
||||
public function getRelationCountAttribute()
|
||||
{
|
||||
$childrenIds = self::relationid(1); //拉入直推会员ID
|
||||
$direct = self::when($childrenIds, function ($query) use ($childrenIds) {
|
||||
$query->whereIn('user_id', $childrenIds);
|
||||
})->count() ?: 0;
|
||||
return $direct;
|
||||
}
|
||||
|
||||
}
|
||||
34
packages/identity/src/ServiceProvider.php
Normal file
34
packages/identity/src/ServiceProvider.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace RuLong\Identity;
|
||||
|
||||
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
|
||||
|
||||
class ServiceProvider extends LaravelServiceProvider
|
||||
{
|
||||
|
||||
/**
|
||||
* 部署时加载
|
||||
* @Author:<C.Jason>
|
||||
* @Date:2018-06-22T16:01:20+0800
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
if ($this->app->runningInConsole()) {
|
||||
$this->publishes([__DIR__ . '/../config/rulong_identity.php' => config_path('rulong_identity.php')]);
|
||||
$this->loadMigrationsFrom(__DIR__ . '/../database/migrations/');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册服务提供者
|
||||
* @Author:<C.Jason>
|
||||
* @Date:2018-06-22T16:01:12+0800
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->mergeConfigFrom(__DIR__ . '/../config/rulong_identity.php', 'rulong_identity');
|
||||
}
|
||||
}
|
||||
13
packages/identity/src/Traits/Identityable.php
Normal file
13
packages/identity/src/Traits/Identityable.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace RuLong\Identity\Traits;
|
||||
|
||||
use RuLong\Identity\Identity;
|
||||
|
||||
trait Identityable
|
||||
{
|
||||
public static function start()
|
||||
{
|
||||
return new Identity(new static(...func_get_args()));
|
||||
}
|
||||
}
|
||||
59
packages/identity/src/Traits/UserHasIdentity.php
Normal file
59
packages/identity/src/Traits/UserHasIdentity.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace RuLong\Identity\Traits;
|
||||
|
||||
use RuLong\Identity\Models\Identity;
|
||||
use RuLong\Identity\Models\UserIdentity;
|
||||
use \RuLong\Identity\Events\IdentityPoint;
|
||||
use \RuLong\Identity\Events\IdentityUpdate;
|
||||
use \RuLong\Identity\Models\IdentityPoint as IdentityPointModel;
|
||||
|
||||
trait UserHasIdentity
|
||||
{
|
||||
|
||||
/**
|
||||
* 获取身份标题
|
||||
* @Author:<C.Jason>
|
||||
* @Date:2018-11-09T17:00:39+0800
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function getIdentityTextAttribute()
|
||||
{
|
||||
return Identity::where('id', $this->identity->identity_id)->value('title') ?: '注册用户';
|
||||
}
|
||||
|
||||
public function pointlogs()
|
||||
{
|
||||
return $this->hasMany(IdentityPointModel::class);
|
||||
}
|
||||
|
||||
public function identity()
|
||||
{
|
||||
return $this->hasOne(UserIdentity::class)->withDefault(['identity_id' => 0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* User模型涉及函数,辅助快速执行修改身份,
|
||||
* [identityUpdate description]
|
||||
* @Author:<Leady>
|
||||
* @Date:2018-11-07T16:24:44+0800
|
||||
* @param [type] $id [身份ID]
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function identityUpdate($id, $channel = 'AutoUp', $pay = 0, $max_key = 0)
|
||||
{
|
||||
return IdentityUpdate::start($this, ['identity_id' => $id, 'channel' => $channel, 'pay' => $pay, 'max_key' => $max_key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* User模型设计函数,辅助快速增加身份积分。
|
||||
* @Author:<Leady>
|
||||
* @Date:2018-11-07T16:25:45+0800
|
||||
* @param [type] $id [description]
|
||||
* @param integer $point [description]
|
||||
*/
|
||||
public function addPoint($id, $point = 1)
|
||||
{
|
||||
return IdentityPoint::start($this, ['identity_id' => $id, 'point' => $point]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user