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()); } }