181 lines
4.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +----------------------------------------------------------------------
// | 作者:修缘 联系QQ278896498 QQ群1054861244
// | 声明:未经作者许可,禁止倒卖等商业运营,违者必究
// | 另接php业务网站制作、代理后台、gm后台、支付对接等
// +----------------------------------------------------------------------
// | 创建时间: 2022/1/5 19:52
// +----------------------------------------------------------------------
namespace app\admin\model;
use app\admin\model\Level;
use app\common\model\User;
class AgencyUser extends Model
{
protected $name = 'admin_user';
protected $autoWriteTimestamp = true;
//可搜索字段
protected $searchField = [
'username',
'nickname',
'parent_id',
];
//可作为条件的字段
protected $whereField = ['level',];
protected $timeField = [
'create_time'
];
/**
* 初始化
* Author修缘
* 联系QQ278896498
* Date: 2021/12/24 19:55
*/
public static function init()
{
self::event('before_insert', static function ($data) {
//添加自动加密密码
$data->password = base64_encode(password_hash($data->password, 1));
// 自动生成邀请码
// do {
// $invite_code = get_rand_str();
// $old = (new static())::where('role',$invite_code)->find('invite_code');
// } while ($old != null);
// $data->invite_code = $invite_code;
});
//修改密码自动加密
self::event('before_update', function ($data) {
$old = (new static())::get($data->id);
if ($data->password !== $old->password) {
$data->password = base64_encode(password_hash($data->password, 1));
}
});
}
/**
* 关联代理等级
* Author修缘
* 联系QQ278896498
* Date: 2021/12/24 3:43
*/
public function agencyLevel()
{
return $this->belongsTo(Level::class, 'level', 'id');
}
public function playerAccount()
{
return $this->hasMany(User::class,'invite', 'invite');
}
public function parent()
{
return $this->belongsTo(AgencyUser::class, 'parent_id', 'id');
}
public function grandpa()
{
return $this->belongsTo(AgencyUser::class,'grandpa_id', 'id');
}
public static function get_agency_relation(int $id = 0)
{
return self::where(['id' => $id])->with([
'parent' => function ($query) {
$query->field('id, username, parent_id, grandpa_id');
},
'grandpa' => function ($query) {
$query->field('id, username, parent_id, grandpa_id');
},
])->field('id, username, level, parent_id, grandpa_id')->find();
}
/**
* 查询代理用户
* Author修缘
* 联系QQ278896498
* Date: 2021/12/24 19:35
*/
public function scopeRole($query)
{
$query->where('role', 2);
}
/**
* 可用上级代理
* Author修缘
* 联系QQ278896498
* Date: 2021/12/24 19:35
*/
public function scopeParent($query)
{
$query->where(['level' => [1,2]]);
}
/**
* 获取直属下级代理
* Author修缘
* 联系QQ278896498
* Date: 2021/12/24 21:45
*/
public function scopeDirectlySon($query, $user)
{
if (in_array(2, $user->role)) {
$query->where('parent_id', $user->id);
} else {
$query->where(['parent_id' => 0, 'grandpa_id' => 0]);
}
}
// 代理获取所有下级代理 含间接
public function scopeProgeny($query, $user)
{
if (in_array(2, $user->role)) {
$query->whereIn('parent_id|grandpa_id', $user->id);
} else {
$query->whereIn('role', 2);
}
}
public function scopeSon($query, $son_agency)
{
$query->whereIn('username', $son_agency);
}
public static function bank_type()
{
$type = [
1 => lang('alipay_name'),
2 => lang('WeChat_name'),
3 => lang('qq_name'),
4 => lang('boc_name'),
5 => lang('icbc_name'),
6 => lang('ccb_name'),
7 => lang('abchina_name'),
8 => lang('cmbchina_name'),
9 => lang('psbc_name'),
10 => lang('bankcomm_name'),
11 => lang('cash_name'),
12 => lang('else_name'),
];
return $type;
}
}