You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
161 lines
4.2 KiB
PHP
161 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
/**
|
|
* User Model
|
|
*/
|
|
|
|
use App\Utils\Hash;
|
|
use App\Models\Configurations;
|
|
use App\Models\InviteCode;
|
|
use App\Services\RedisClient;
|
|
use App\Services\UserFeatureRestrictionService;
|
|
use App\Services\UserDataDeletionService;
|
|
|
|
class User extends Model
|
|
{
|
|
protected $connection = "default";
|
|
protected $table = "user";
|
|
|
|
public $isLogin;
|
|
|
|
public $isAdmin;
|
|
|
|
protected $casts = [
|
|
"enable" => 'int',
|
|
'restrict_recharge' => 'int',
|
|
'restrict_balance_purchase' => 'int',
|
|
'restrict_invite' => 'int',
|
|
'restrict_ticket' => 'int',
|
|
'restrict_transaction_records' => 'int',
|
|
'restriction_updated_at' => 'int',
|
|
'restriction_operator_id' => 'int',
|
|
'login_session_version' => 'int',
|
|
'is_admin' => 'boolean',
|
|
];
|
|
|
|
public function getGravatarAttribute()
|
|
{
|
|
$hash = md5(strtolower(trim($this->attributes['email'])));
|
|
return "https://secure.gravatar.com/avatar/".$hash;
|
|
}
|
|
|
|
public function getAffLink()
|
|
{
|
|
if (!$this->canInvite()) {
|
|
return '';
|
|
}
|
|
|
|
$uid = $this->attributes['id'];
|
|
$redis = new RedisClient();
|
|
$domain = $_SERVER['HTTP_HOST'];
|
|
|
|
$aff_method = $redis->get('aff_method', 60, function () {
|
|
return Configurations::where('name', 'aff_method')->value('value');
|
|
});
|
|
|
|
if ($aff_method === 'affid') {
|
|
return 'https://' . $domain . '/auth/register?aff=' . $uid;
|
|
}
|
|
if ($aff_method === 'code') {
|
|
$code = InviteCode::where('user_id', $uid)->first();
|
|
if ($code === null) {
|
|
do {
|
|
$value = bin2hex(random_bytes(6));
|
|
} while (InviteCode::where('code', $value)->exists());
|
|
$code = new InviteCode();
|
|
$code->code = $value;
|
|
$code->user_id = $uid;
|
|
$code->save();
|
|
}
|
|
|
|
return 'https://' . $domain . '/auth/register?code=' . $code->code;
|
|
}
|
|
|
|
return '';
|
|
}
|
|
|
|
public function canInvite()
|
|
{
|
|
return (int) $this->enable === 1 && !UserFeatureRestrictionService::isInviteRestricted($this);
|
|
}
|
|
|
|
public function isAdmin()
|
|
{
|
|
return $this->attributes['is_admin'];
|
|
}
|
|
public function regDate()
|
|
{
|
|
return $this->attributes['reg_date'];
|
|
}
|
|
|
|
public function updatePassword($pwd)
|
|
{
|
|
$this->pass = Hash::passwordHash($pwd);
|
|
if (!$this->save()) {
|
|
throw new \RuntimeException('密码保存失败');
|
|
}
|
|
\App\Services\LoginSessionRevocationService::revokeUser($this);
|
|
}
|
|
public function inviteCodes()
|
|
{
|
|
$uid = $this->attributes['id'];
|
|
return InviteCode::where('user_id', $uid)->get();
|
|
}
|
|
|
|
public function ref_by_user()
|
|
{
|
|
$uid = $this->attributes['ref_by'];
|
|
return User::where('id', $uid)->first();
|
|
}
|
|
|
|
public function invitedMemberTotal()
|
|
{
|
|
return User::where('ref_by', $this->attributes['id'])->count();
|
|
}
|
|
|
|
public function inviteMoneyTotal()
|
|
{
|
|
$uid = $this->attributes['id'];
|
|
$aff_money = 0;
|
|
$paybacks = Payback::where("ref_by", $uid)->get();
|
|
foreach ($paybacks as $payback) {
|
|
$aff_money += $payback->ref_get;
|
|
}
|
|
|
|
return $aff_money;
|
|
}
|
|
|
|
public function yesterdayIncome()
|
|
{
|
|
return Code::where('usedatetime','like',date('Y-m-d%',strtotime("-1 days")))->sum('number');
|
|
}
|
|
|
|
public function todayIncome()
|
|
{
|
|
return Code::where('usedatetime','like',date('Y-m-d%'))->sum('number');
|
|
}
|
|
|
|
public function thisMonthIncome()
|
|
{
|
|
return Code::where('usedatetime','like',date('Y-m%'))->sum('number');
|
|
}
|
|
|
|
public function lastMonthIncome()
|
|
{
|
|
return Code::where('usedatetime','like',date('Y-m%',strtotime("-1 months")))->sum('number');
|
|
}
|
|
|
|
public function totalIncome()
|
|
{
|
|
return Code::where('usedatetime','like',date('%'))->sum('number');
|
|
}
|
|
|
|
public function kill_user()
|
|
{
|
|
$result = (new UserDataDeletionService())->deleteUsers([(int) $this->attributes['id']]);
|
|
return (int) $result['deleted'] === 1;
|
|
}
|
|
}
|