UserController.class.php 14.2 KB
Newer Older
D
devil_gong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
<?php

namespace Home\Controller;

/**
 * 用户
 * @author   Devil
 * @blog     http://gong.gg/
 * @version  0.0.1
 * @datetime 2017-03-02T22:48:35+0800
 */
class UserController extends CommonController
{
	/**
	 * [_initialize 前置操作-继承公共前置方法]
	 * @author   Devil
	 * @blog     http://gong.gg/
	 * @version  0.0.1
	 * @datetime 2017-03-02T22:48:35+0800
	 */
	public function _initialize()
	{
		// 调用父类前置方法
		parent::_initialize();
	}

	/**
	 * [GetrefererUrl 获取上一个页面地址]
	 * @author   Devil
	 * @blog     http://gong.gg/
	 * @version  0.0.1
	 * @datetime 2017-03-09T15:46:16+0800
	 */
	private function GetrefererUrl()
	{
		// 上一个页面, 空则用户中心
		if(empty($_SERVER['HTTP_REFERER']))
		{
			$referer_url = U('Home/Bubble/Index');
		} else {
			if(strpos($_SERVER['HTTP_REFERER'], 'RegInfo') !== false || strpos($_SERVER['HTTP_REFERER'], 'LoginInfo') !== false || strpos($_SERVER['HTTP_REFERER'], 'ForgetPwdInfo') !== false)
			{
				$referer_url = U('Home/Bubble/Index');
			} else {
				$referer_url = $_SERVER['HTTP_REFERER'];
			}
		}
		return $referer_url;
	}

	/**
	 * [Index 用户中心]
	 * @author   Devil
	 * @blog     http://gong.gg/
	 * @version  0.0.1
	 * @datetime 2017-03-02T22:48:35+0800
	 */
	public function Index()
	{
		$this->display('Index');
	}

	/**
	 * [ForgetPwdInfo 密码找回]
	 * @author   Devil
	 * @blog     http://gong.gg/
	 * @version  0.0.1
	 * @datetime 2017-03-10T17:06:47+0800
	 */
	public function ForgetPwdInfo()
	{
		if(empty($this->user))
		{
			$this->display('ForgetPwdInfo');
		} else {
			$this->assign('msg', L('common_forget_already_had_tips'));
			$this->display('/Public/TipsError');
		}
	}

	/**
	 * [EmailRegInfo 用户注册页面-邮箱]
	 * @author   Devil
	 * @blog     http://gong.gg/
	 * @version  0.0.1
	 * @datetime 2017-03-10T12:18:17+0800
	 */
	public function EmailRegInfo()
	{
		if(in_array('email', MyC('home_user_reg_state')))
		{
			if(empty($this->user))
			{
				$this->assign('referer_url', $this->GetrefererUrl());
				$this->display('EmailRegInfo');
			} else {
				$this->assign('msg', L('common_reg_already_had_tips'));
				$this->display('/Public/TipsError');
			}
		} else {
			$this->assign('msg', L('common_close_email_user_reg_tips'));
			$this->display('/Public/TipsError');
		}
	}

	/**
	 * [RegInfo 用户注册页面-短信]
	 * @author   Devil
	 * @blog     http://gong.gg/
	 * @version  0.0.1
	 * @datetime 2017-03-02T22:48:35+0800
	 */
	public function RegInfo()
	{
		if(in_array('sms', MyC('home_user_reg_state')))
		{
			if(empty($this->user))
			{
				$this->assign('referer_url', $this->GetrefererUrl());
				$this->display('RegInfo');
			} else {
				$this->assign('msg', L('common_reg_already_had_tips'));
				$this->display('/Public/TipsError');
			}
		} else {
			$this->assign('msg', L('common_close_sms_user_reg_tips'));
			$this->display('/Public/TipsError');
		}
	}

	/**
	 * [LoginInfo 用户登录页面]
	 * @author   Devil
	 * @blog     http://gong.gg/
	 * @version  0.0.1
	 * @datetime 2017-03-02T22:48:35+0800
	 */
	public function LoginInfo()
	{
		if(MyC('home_user_login_state') == 1)
		{
			if(empty($this->user))
			{
				$this->assign('referer_url', $this->GetrefererUrl());
				$this->display('LoginInfo');
			} else {
				$this->assign('msg', L('common_login_already_had_tips'));
				$this->display('/Public/TipsError');
			}
		} else {
			$this->assign('msg', L('common_close_user_login_tips'));
			$this->display('/Public/TipsError');
		}
	}

	/**
	 * [Reg 用户注册-数据添加]
	 * @author   Devil
	 * @blog     http://gong.gg/
	 * @version  0.0.1
	 * @datetime 2017-03-07T00:08:36+0800
	 */
	public function Reg()
	{
		// 是否开启用户注册
		if(!in_array(I('type'), MyC('home_user_reg_state')))
		{
			$this->error(L('common_close_user_reg_tips'));
		}

		// 是否ajax请求
		if(!IS_AJAX)
		{
			$this->error(L('common_unauthorized_access'));
		}

		// 账户校验
		$this->UserRegAccountsCheck();

		// 验证码校验
		$verify_param = array(
				'key_prefix' => 'reg',
				'expire_time' => MyC('common_verify_expire_time')
			);
		if(I('type') == 'sms')
		{
			$obj = new \My\Sms($verify_param);
		} else {
			$obj = new \My\Email($verify_param);
		}
		// 是否已过期
		if(!$obj->CheckExpire())
		{
			$this->ajaxReturn(L('common_verify_expire'), -10);
		}
		// 是否正确
		if(!$obj->CheckCorrect(I('verify')))
		{
			$this->ajaxReturn(L('common_verify_error'), -11);
		}

		// 模型
		$m = D('User');

		// 数据自动校验
		if($m->create($_POST, 1))
		{
			// 额外数据处理
			if(I('type') == 'sms')
			{
				$m->mobile = I('accounts');
			} else {
				$m->email = I('accounts');
			}
			$m->add_time	=	time();
			$m->upd_time	=	time();
			$m->salt 		=	GetNumberCode(6);
			$m->pwd 		=	LoginPwdEncryption(I('pwd'), $m->salt);

			// 数据添加
			$user_id = $m->add();
			if($user_id > 0)
			{
				// 清除验证码
				$obj->Remove();

				if($this->UserLoginRecord($user_id))
				{
					$this->ajaxReturn(L('common_reg_success'));
				}
				$this->ajaxReturn(L('common_reg_success_login_tips'));
			} else {
				$this->ajaxReturn(L('common_reg_error'), -100);
			}
		} else {
			$this->ajaxReturn($m->getError(), -1);
		}
	}

	/**
	 * [Login 用户登录]
	 * @author   Devil
	 * @blog     http://gong.gg/
	 * @version  0.0.1
	 * @datetime 2017-03-09T10:57:31+0800
	 */
	public function Login()
	{
		// 是否开启用户登录
		if(MyC('home_user_login_state') != 1)
		{
			$this->error(L('common_close_user_login_tips'));
		}

		// 是否ajax请求
		if(!IS_AJAX)
		{
			$this->error(L('common_unauthorized_access'));
		}

		// 登录帐号格式校验
		$accounts = I('accounts');
		if(!CheckMobile($accounts) && !CheckEmail($accounts))
		{
			$this->ajaxReturn(L('user_login_accounts_format'), -1);
		}

		// 密码
		$pwd = trim(I('pwd'));
		if(!CheckLoginPwd($pwd))
		{
			$this->ajaxReturn(L('user_reg_pwd_format'), -2);
		}

		// 获取用户账户信息
		$where = array('mobile' => $accounts, 'email' => $accounts, '_logic' => 'OR');
		$user = M('User')->field(array('id', 'pwd', 'salt', 'state'))->where($where)->find();
		if(empty($user))
		{
			$this->ajaxReturn(L('user_login_accounts_on_exist_error'), -3);
		}
		// 用户状态
		if($user['state'] == 2)
		{
			$this->ajaxReturn(L('common_user_state_list')[$user['state']]['tips'], -10);
		}

		// 密码校验
		if(LoginPwdEncryption($pwd, $user['salt']) != $user['pwd'])
		{
			$this->ajaxReturn(L('user_common_pwd_error'), -4);
		}

		// 更新用户密码
		$salt = GetNumberCode(6);
		$data = array(
				'pwd'		=>	LoginPwdEncryption($pwd, $salt),
				'salt'		=>	$salt,
				'upd_time'	=>	time(),
			);
		if(M('User')->where(array('id'=>$user['id']))->save($data) !== false)
		{
			// 登录记录
			if($this->UserLoginRecord($user['id']))
			{
				$this->ajaxReturn(L('common_login_success'));
			}
		}
		$this->ajaxReturn(L('common_login_invalid'), -100);
	}

	/**
	 * [UserVerifyEntry 用户-验证码显示]
	 * @author   Devil
	 * @blog     http://gong.gg/
	 * @version  0.0.1
	 * @datetime 2017-03-05T15:10:21+0800
	 */
	public function UserVerifyEntry()
	{
		$this->CommonVerifyEntry(I('type', 'reg'));
	}

	/**
	 * [RegVerifySend 用户注册-验证码发送]
	 * @author   Devil
	 * @blog     http://gong.gg/
	 * @version  0.0.1
	 * @datetime 2017-03-05T19:17:10+0800
	 */
	public function RegVerifySend()
	{
		// 是否开启用户注册
		if(!in_array(I('type'), MyC('home_user_reg_state')))
		{
			$this->error(L('common_close_user_reg_tips'));
		}

		// 是否ajax请求
		if(!IS_AJAX)
		{
			$this->error(L('common_unauthorized_access'));
		}

		// 账户校验
		$this->UserRegAccountsCheck();

		// 验证码公共基础参数
		$verify_param = array(
				'key_prefix' => 'reg',
				'expire_time' => MyC('common_verify_expire_time'),
				'time_interval'	=>	MyC('common_verify_time_interval'),
			);

		// 是否开启图片验证码
		$verify = $this->CommonIsImaVerify($verify_param);

		// 发送验证码
		$code = GetNumberCode(6);
		if(I('type') == 'sms')
		{
			$obj = new \My\Sms($verify_param);
			$state = $obj->SendText(I('accounts'), MyC('home_sms_user_reg'), $code);
		} else {
			$obj = new \My\Email($verify_param);
			$email_param = array(
					'email'		=>	I('accounts'),
					'content'	=>	MyC('home_email_user_reg'),
					'title'		=>	MyC('home_site_name').' - '.L('common_email_send_user_reg_title'),
					'code'		=>	$code,
				);
			$state = $obj->SendHtml($email_param);
		}
		
		// 状态
		if($state)
		{
			// 清除验证码
			if(isset($verify) && is_object($verify))
			{
				$verify->Remove();
			}

			$this->ajaxReturn(L('common_send_success'));
		} else {
			$this->ajaxReturn(L('common_send_error').'['.$obj->error.']', -100);
		}
	}

	/**
	 * [UserRegAccountsCheck 用户注册账户校验]
	 * @author   Devil
	 * @blog     http://gong.gg/
	 * @version  0.0.1
	 * @datetime 2017-03-10T10:06:29+0800
	 */
	private function UserRegAccountsCheck()
	{
		// 参数
		$type = I('type');
		$accounts = I('accounts');
		if(empty($accounts) || empty($type) || !in_array($type, array('sms', 'email')))
		{
			$this->ajaxReturn(L('common_param_error'), -1);
		}

		// 手机号码
		if($type == 'sms')
		{
			// 手机号码格式
			if(!CheckMobile($accounts))
			{
				$this->ajaxReturn(L('common_mobile_format_error'), -2);
			}

			// 手机号码是否已存在
			if($this->IsExistAccounts($accounts, 'mobile'))
			{
				$this->ajaxReturn(L('common_mobile_exist_error'), -3);
			}

		// 电子邮箱
		} else {
			// 电子邮箱格式
			if(!CheckEmail($accounts))
			{
				$this->ajaxReturn(L('common_email_format_error'), -2);
			}

			// 电子邮箱是否已存在
			if($this->IsExistAccounts($accounts, 'email'))
			{
				$this->ajaxReturn(L('common_email_exist_error'), -3);
			}
		}
	}

	/**
	 * [ForgetPwdVerifySend 密码找回验证码发送]
	 * @author   Devil
	 * @blog     http://gong.gg/
	 * @version  0.0.1
	 * @datetime 2017-03-10T17:35:03+0800
	 */
	public function ForgetPwdVerifySend()
	{
		// 是否ajax请求
		if(!IS_AJAX)
		{
			$this->error(L('common_unauthorized_access'));
		}

		// 参数
		$accounts = I('accounts');
		if(empty($accounts))
		{
			$this->ajaxReturn(L('common_param_error'), -10);
		}

		// 账户是否存在
		$type = $this->UserForgetAccountsCheck($accounts);

		// 验证码公共基础参数
		$verify_param = array(
				'key_prefix' => 'forget',
				'expire_time' => MyC('common_verify_expire_time'),
				'time_interval'	=>	MyC('common_verify_time_interval'),
			);

		// 是否开启图片验证码
		$verify = $this->CommonIsImaVerify($verify_param);

		// 验证码
		$code = GetNumberCode(6);

		// 手机
		if($type == 'mobile')
		{
			$obj = new \My\Sms($verify_param);
			$state = $obj->SendText($accounts, MyC('home_sms_user_forget_pwd'), $code);

		// 邮箱
		} else if($type == 'email')
		{
			$obj = new \My\Email($verify_param);
			$email_param = array(
					'email'		=>	$accounts,
					'content'	=>	MyC('home_email_user_forget_pwd'),
					'title'		=>	MyC('home_site_name').' - '.L('common_email_send_user_reg_title'),
					'code'		=>	$code,
				);
			$state = $obj->SendHtml($email_param);
		} else {
			$this->ajaxReturn(L('user_login_accounts_format'), -1);
		}

		// 状态
		if($state)
		{
			// 清除验证码
			if(isset($verify) && is_object($verify))
			{
				$verify->Remove();
			}

			$this->ajaxReturn(L('common_send_success'));
		} else {
			$this->ajaxReturn(L('common_send_error').'['.$obj->error.']', -100);
		}
	}

	/**
	 * [ForgetPwd 密码找回]
	 * @author   Devil
	 * @blog     http://gong.gg/
	 * @version  0.0.1
	 * @datetime 2017-03-10T17:55:42+0800
	 */
	public function ForgetPwd()
	{
		// 是否ajax请求
		if(!IS_AJAX)
		{
			$this->error(L('common_unauthorized_access'));
		}

		// 参数
		$accounts = I('accounts');
		$verify = I('verify');
		$pwd = trim(I('pwd'));
		if(empty($accounts) || empty($verify) || empty($pwd))
		{
			$this->ajaxReturn(L('common_param_error'), -1);
		}

		// 账户是否存在
		$field = $this->UserForgetAccountsCheck($accounts);

		// 验证码校验
		$verify_param = array(
				'key_prefix' => 'forget',
				'expire_time' => MyC('common_verify_expire_time'),
				'time_interval'	=>	MyC('common_verify_time_interval'),
			);
		if($field == 'mobile')
		{
			$obj = new \My\Sms($verify_param);
		} else if($field == 'email')
		{
			$obj = new \My\Email($verify_param);
		}
		// 是否已过期
		if(!$obj->CheckExpire())
		{
			$this->ajaxReturn(L('common_verify_expire'), -10);
		}
		// 是否正确
		if(!$obj->CheckCorrect($verify))
		{
			$this->ajaxReturn(L('common_verify_error'), -11);
		}

		// 更新用户密码
		$salt = GetNumberCode(6);
		$data = array(
				'pwd'		=>	LoginPwdEncryption($pwd, $salt),
				'salt'		=>	$salt,
				'upd_time'	=>	time(),
			);
		if(M('User')->where(array($field=>$accounts))->save($data) !== false)
		{
			$this->ajaxReturn(L('common_operation_success'));
		}
		$this->ajaxReturn(L('common_operation_error'), -100);
	}

	/**
	 * [UserForgetAccountsCheck 帐号校验]
	 * @author   Devil
	 * @blog     http://gong.gg/
	 * @version  0.0.1
	 * @datetime 2017-03-10T17:59:53+0800
	 * @param    [string]     $accounts [账户名称]
	 * @return   [string]               [账户字段 mobile, email]
	 */
	private function UserForgetAccountsCheck($accounts)
	{
		if(CheckMobile($accounts))
		{
			if(!$this->IsExistAccounts($accounts, 'mobile'))
			{
				$this->ajaxReturn(L('common_mobile_no_exist_error'), -3);
			}
			return 'mobile';
		} else if(CheckEmail($accounts))
		{
			if(!$this->IsExistAccounts($accounts, 'email'))
			{
				$this->ajaxReturn(L('common_email_no_exist_error'), -3);
			}
			return 'email';
		}
		$this->ajaxReturn(L('common_accounts_format_error'), -4);
	}

	/**
	 * [IsExistAccounts 账户是否存在]
	 * @author   Devil
	 * @blog     http://gong.gg/
	 * @version  0.0.1
	 * @datetime 2017-03-08T10:27:14+0800
	 * @param    [string] $accounts 	[账户名称]
	 * @param    [string] $field 		[字段名称]
	 * @return   [boolean] 				[存在true, 不存在false]
	 */
	private function IsExistAccounts($accounts, $field = 'mobile')
	{
		$id = M('User')->where(array($field=>$accounts))->getField('id');
		return !empty($id);
	}

	/**
	 * [Logout 退出]
	 * @author   Devil
	 * @blog     http://gong.gg/
	 * @version  0.0.1
	 * @datetime 2016-12-05T14:31:23+0800
	 */
	public function Logout()
	{
		if(isset($_SESSION['user']))
		{
			unset($_SESSION['user']);
		}
		redirect(__MY_URL__);
	}
}
?>