2026-05-29 19:54:56 +08:00

284 lines
9.0 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
/**
* Created by PhpStorm.
* User: zn
* Date: 2017/5/16
* Time: 14:15
*/
namespace common\models;
use common\modules\pet\vo\Pet;
use common\modules\qixunpay\vo\Pay;
use common\modules\systemlog\SystemLog;
use common\modules\vip\vo\Vip;
use common\services\GameConfigServices;
use common\services\UserService;
use common\utils\EnumUtil;
use common\utils\RedisKeyUtil;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
/**
* 用户表
* @property integer $id
* @property string $username
* @property string $password 密码
* @property string $pay_password 支付密码
* @property string $nickname 昵称
* @property string $wx_name 微信号
* @property string $realname 真实姓名
* @property string $avatar 头像地址
* @property integer $gold 金币
* @property integer $gem 宝石
* @property integer $level 角色等级
* @property integer $exp 角色经验
* @property integer $dog_hunger_end_time 小狗什么时候开始饥饿
* @property integer $use_dog_item_id 当前显示的是哪个狗狗的ItemID
* @property string $introduce_code 推荐码,别人注册时输入
* @property int $factory_level
* @property int $factory_exp
* @property int $compose_level
* @property int $compose_exp
* @property int $online_gift_last_get_time //在线礼包最后获取时间
* @property int $online_gift_get_count //在线礼包获取几次了
* @property int $get_level_gift //是否领取等级礼包
* @property int $fighting_capacity //战斗力
* @property int $guid_target //新手指引
* @property int $guid_step //新手指引
* @property integer $active 是否激活
* @property integer $created_at
* @property integer $updated_at
*/
class User extends ActiveRecord
{
public $token = null; //用户登录后用于识别用户
public function __construct(array $config = [])
{
parent::__construct($config);
$this->token = md5(uniqid('', true));
}
public function behaviors()
{
return [
TimestampBehavior::className(),
];
}
public function rules()
{
return [
['id', 'default', 'value' => 0],
['username', 'string', 'min' => 2, 'max' => 255],
['password', 'trim'],
['password', 'required'],
['password', 'string', 'min' => 2, 'max' => 255],
['pay_password', 'trim'],
['pay_password', 'default', 'value' => ''],
['pay_password', 'string', 'min' => 2, 'max' => 255],
['nickname', 'default', 'value' => ''],
['wx_name', 'default', 'value' => ''],
['realname', 'default', 'value' => ''],
['avatar', 'default', 'value' => ''],
['gold', 'default', 'value' => 0],
['gold', 'number', 'integerOnly' => true, 'min' => 0],
['gem', 'default', 'value' => 0],
['gem', 'number', 'integerOnly' => true, 'min' => 0],
['level', 'default', 'value' => 1],
['level', 'number', 'integerOnly' => true, 'min' => 0, 'max' => GameConfigServices::getMaxPlayerLevel()],
['exp', 'default', 'value' => 0],
['exp', 'number', 'integerOnly' => true, 'min' => 0],
['dog_hunger_end_time', 'default', 'value' => 0],
['use_dog_item_id', 'default', 'value' => 0],
['introduce_code', 'default', 'value' => 0],
['factory_level', 'default', 'value' => 0],
['factory_level', 'number', 'integerOnly' => true, 'min' => 0, 'max' => GameConfigServices::getMaxFactoryLevel()],
['factory_exp', 'number', 'integerOnly' => true, 'min' => 0],
['compose_level', 'default', 'value' => 0],
['compose_level', 'number', 'integerOnly' => true, 'min' => 0, 'max' => GameConfigServices::getMaxComposeLevel()],
['compose_exp', 'number', 'integerOnly' => true, 'min' => 0],
['online_gift_last_get_time', 'default', 'value' => 0],
['online_gift_get_count', 'default', 'value' => 0],
['get_level_gift', 'default', 'value' => EnumUtil::PLAYER_LEVEL_GIFT_NOT_GET],
['get_level_gift', 'in', 'range' => EnumUtil::PLAYER_LEVEL_GIFT_LIST],
['fighting_capacity', 'default', 'value' => 0],
['guid_target', 'default', 'value' => 0],
['guid_step', 'default', 'value' => 0],
['active', 'default', 'value' => EnumUtil::ACTIVE],
['active', 'in', 'range' => EnumUtil::ACTIVE_LIST],
['created_at', 'default', 'value' => 0],
['updated_at', 'default', 'value' => 0],
];
}
public static function findById($id, $excludeDel = true)
{
if ($excludeDel)
return static::findOne(['id' => $id, 'active' => EnumUtil::ACTIVE]);
else
return static::findOne(['id' => $id]);
}
public static function findByUsername($username)
{
return static::findOne(['username' => $username, 'active' => EnumUtil::ACTIVE]);
}
public static function findIsNotActiveByUsername($username)
{
return static::findOne(['username' => $username, 'active' => EnumUtil:: ACTIVE_NOT]);
}
public static function findByNickname($nickname)
{
return static::findOne(['nickname' => $nickname, 'active' => EnumUtil::ACTIVE]);
}
/**
* 根据推荐码查找用户
* @param $code
* @return static
*/
public static function findByIntroduceCode($code)
{
return static::findOne(['introduce_code' => $code, 'active' => EnumUtil::ACTIVE]);
}
/**验证密码是否匹配
* @param $password
* @return bool
*/
public function validatePassword($password)
{
return Yii::$app->security->validatePassword($password, $this->password);
}
/**验证支付密码
* @param $password
* @return bool
*/
public function validatePayPassword($password)
{
return Yii::$app->security->validatePassword($password, $this->pay_password);
}
/**
* 设置密码
* @param $password
*/
public function setPassword($password, $payPassword)
{
$password = trim($password);
$payPassword = trim($payPassword);
if (!empty($password))
$this->password = Yii::$app->security->generatePasswordHash($password);
if (!empty($payPassword))
$this->pay_password = Yii::$app->security->generatePasswordHash($payPassword);
}
/*
* 登录
*/
public function login($password)
{
//验证密码
if (!Yii::$app->security->validatePassword($password, $this->password))
return false;
//缓存登录信息
UserService::saveUserInfo($this);
return true;
}
/**
* 生成要发送给客户的数据
* @param bool $all
* @return array
*/
public function genClientData($all = true)
{
$clientData = $this->toArray();
unset($clientData['password']);
$clientData['user_id'] = $this->id;
$clientData['token'] = $this->token;
$clientData['stone'] = isset($clientData['stone']) ? (int)$clientData['stone'] : (int)GameConfigServices::getParamByPath('init.1.stone', 0);
$clientData['steel'] = isset($clientData['steel']) ? (int)$clientData['steel'] : (int)GameConfigServices::getParamByPath('init.1.steel', 0);
//抽奖次数
$lastLotteryData = json_decode(Yii::$app->redis->get(RedisKeyUtil::userLotteryData($this->id)), true);
$lotteryCount = 0;
if (!empty($lastLotteryData) && $lastLotteryData['date'] == date('Y-m-d', time()))
$lotteryCount = $lastLotteryData['count'];
$clientData['lottery_count'] = $lotteryCount;
if (!$all)
return $clientData;
$farm = Farm::findOne(['user_id' => $this->id]);
//$vip = User::findById($this->id);
$clientData['vip'] = $clientData['level'];
$warPet = Pet::findEnterWarPetByUserID($this->id);
if (!empty($warPet)) {
$clientData['fighting_capacity'] = $warPet->fighting_capacity;
$clientData['pet'] = $warPet->toArray();
} else {
$clientData['fighting_capacity'] = 0;
$clientData['pet'] = null;
}
$clientData['buy_pet_egg_list'] = SystemLog::findBuyPetEggList($this->id);
$clientData['user_total'] = self::find()->count();
$onlineInfo = UserService::updateOnlineInfo($this->id);
$clientData['online_start_time'] = $onlineInfo['start_time'];
$clientData['online_last_time'] = $onlineInfo['last_time'];
$clientData['online_total_time'] = $onlineInfo['total_time'];
$clientData['online_get_count'] = $onlineInfo['get_count'];
if(strlen($clientData['avatar'])>5){
$clientData['avatar']= 'http://'.$_SERVER['HTTP_HOST'].$clientData['avatar'];
}
// Yii::warning($this->id);
// Yii::warning($onlineInfo);
//支付信息
$clientData['pay_list'] = Pay::getPayGroupBy($this->id);
return array_merge($clientData, $farm->genClientData());
}
}