313 lines
11 KiB
PHP
313 lines
11 KiB
PHP
<?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\PetBattleService;
|
||
use common\services\StaminaService;
|
||
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 $stamina 体力
|
||
* @property integer $stamina_max 体力上限
|
||
* @property integer $stamina_recover_time 体力最后恢复计算时间
|
||
* @property integer $pet_weak_until 宠物虚弱结束时间
|
||
* @property integer $pet_weak_pet_id 虚弱宠物ID
|
||
* @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],
|
||
|
||
['stamina', 'default', 'value' => StaminaService::MAX_STAMINA],
|
||
['stamina', 'number', 'integerOnly' => true, 'min' => 0],
|
||
|
||
['stamina_max', 'default', 'value' => StaminaService::MAX_STAMINA],
|
||
['stamina_max', 'number', 'integerOnly' => true, 'min' => 1],
|
||
|
||
['stamina_recover_time', 'default', 'value' => 0],
|
||
['stamina_recover_time', 'number', 'integerOnly' => true, 'min' => 0],
|
||
|
||
['pet_weak_until', 'default', 'value' => 0],
|
||
['pet_weak_until', 'number', 'integerOnly' => true, 'min' => 0],
|
||
|
||
['pet_weak_pet_id', 'default', 'value' => 0],
|
||
['pet_weak_pet_id', '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)
|
||
{
|
||
PetBattleService::recoverWeakIfExpired($this);
|
||
$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);
|
||
$clientData['pet_weak_until'] = isset($clientData['pet_weak_until']) ? (int)$clientData['pet_weak_until'] : 0;
|
||
$clientData['pet_weak_pet_id'] = isset($clientData['pet_weak_pet_id']) ? (int)$clientData['pet_weak_pet_id'] : 0;
|
||
$clientData['pet_weak'] = $clientData['pet_weak_until'] > time() ? 1 : 0;
|
||
foreach (StaminaService::getClientData($this->id) as $key => $value) {
|
||
$clientData[$key] = $value;
|
||
}
|
||
|
||
//抽奖次数
|
||
$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());
|
||
}
|
||
}
|