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.

119 lines
3.8 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Service;
use App\Exception\BusinessException;
use App\Helper\Client\Lanhuo;
use App\Model\Player;
use App\Model\PlayerRetention;
use App\Model\User;
use App\Helper\Log;
class UserService extends Service
{
public function afterLogin($params)
{
$gameId = $params['game_id'];
$userId = $params['user_id'];
$loginTime = $params['login_time'];
$sdkVersion = $params['sdk_version'];
$deviceNumber = $params['device_number'];
$clientIp = $params['client_ip'];
if (empty($time)) {
$loginTime = time();
}
Log::info('[USER-LOGIN]用户['. $userId . ']登录游戏[' . $gameId . ']');
$player = Player::where('user_id', $userId)->where('game_id', $gameId)->first(['id', 'create_time']);
if (!$player) {
throw new BusinessException('玩家信息不存在');
}
$playerRetention = PlayerRetention::where('player_id', $player->id)->first();
$retentionDay = $this->getRetentionDay($player->create_time, $loginTime);
if ($retentionDay > 0) {
if (!$playerRetention) {
$playerRetention = new PlayerRetention();
$playerRetention->player_id = $player->id;
$playerRetention->game_id = $gameId;
$playerRetention->user_id = $userId;
}
$playerRetention->{'day' . $retentionDay} = 1;
$playerRetention->save();
}
if (in_array($gameId, [335, 336])) {
$user = User::where('id', $userId)->first(['id', 'account', 'phone']);
Lanhuo::addLoginLog([
'user_id' => $userId,
'account' => $user->account,
'phone' => $user->phone,
'device_type' => $sdkVersion ?? 1, // 设备类型(1-andriod 2-ios)
'device_number' => $deviceNumber,
]);
}
}
private function getRetentionDay($createTime, $loginTime)
{
$dayTime = 24 * 60 * 60;
$retentionDay = 0;
foreach (PlayerRetention::$dayList as $day) {
$date = date('Ymd', $createTime + $day * $dayTime);
if ($date == date('Ymd', $loginTime)) {
$retentionDay = $day;
continue;
}
}
return $retentionDay;
}
public function afterRegister($userId)
{
Log::info('[USER-REGISTER]用户['. $userId . ']注册');
$user = User::where('id', $userId)->first([
'id',
'device_number',
'register_ip',
'fgame_id',
'device_type',
'account',
'phone',
'real_name',
'idcard',
'age_status',
]);
if (!$user) {
throw new BusinessException('用户不存在');
}
$repeatUser = User::where('device_number', $user->device_number)
->where('register_ip', $user->register_ip)
->where('id', '<', $user->id)
->first();
if ($repeatUser) {
$user->is_repeat = 1;
$user->save();
Log::info('[USER-REGISTER]用户['. $userId . ']为重复账号');
}
if (in_array($user->fgame_id, [335, 336])) {
Lanhuo::addUser([
'user_id' => $user->id,
'account' => $user->account,
'phone' => $user->phone,
'device_type' => $user->device_type ?? 1, // 设备类型(1-andriod 2-ios)
'device_number' => $user->device_number,
'name' => $user->real_name,
'id_card' => $user->idcard,
'is_real' => $user->age_status == 2 ? 1 : 0, // 是否实名(1-实名 0-未实名)
]);
}
}
}