UserController.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. <?php
  2. namespace Home\Controller;
  3. /**
  4. * 用户
  5. * @author Devil
  6. * @blog http://gong.gg/
  7. * @version 0.0.1
  8. * @datetime 2017-03-02T22:48:35+0800
  9. */
  10. class UserController extends CommonController
  11. {
  12. /**
  13. * [_initialize 前置操作-继承公共前置方法]
  14. * @author Devil
  15. * @blog http://gong.gg/
  16. * @version 0.0.1
  17. * @datetime 2017-03-02T22:48:35+0800
  18. */
  19. public function _initialize()
  20. {
  21. // 调用父类前置方法
  22. parent::_initialize();
  23. }
  24. /**
  25. * [GetrefererUrl 获取上一个页面地址]
  26. * @author Devil
  27. * @blog http://gong.gg/
  28. * @version 0.0.1
  29. * @datetime 2017-03-09T15:46:16+0800
  30. */
  31. private function GetrefererUrl()
  32. {
  33. // 上一个页面, 空则用户中心
  34. if(empty($_SERVER['HTTP_REFERER']))
  35. {
  36. $referer_url = U('Home/Bubble/Index');
  37. } else {
  38. if(strpos($_SERVER['HTTP_REFERER'], 'RegInfo') !== false || strpos($_SERVER['HTTP_REFERER'], 'LoginInfo') !== false || strpos($_SERVER['HTTP_REFERER'], 'ForgetPwdInfo') !== false)
  39. {
  40. $referer_url = U('Home/Bubble/Index');
  41. } else {
  42. $referer_url = $_SERVER['HTTP_REFERER'];
  43. }
  44. }
  45. return $referer_url;
  46. }
  47. /**
  48. * [Index 用户中心]
  49. * @author Devil
  50. * @blog http://gong.gg/
  51. * @version 0.0.1
  52. * @datetime 2017-03-02T22:48:35+0800
  53. */
  54. public function Index()
  55. {
  56. $this->display('Index');
  57. }
  58. /**
  59. * [ForgetPwdInfo 密码找回]
  60. * @author Devil
  61. * @blog http://gong.gg/
  62. * @version 0.0.1
  63. * @datetime 2017-03-10T17:06:47+0800
  64. */
  65. public function ForgetPwdInfo()
  66. {
  67. if(empty($this->user))
  68. {
  69. $this->display('ForgetPwdInfo');
  70. } else {
  71. $this->assign('msg', L('common_forget_already_had_tips'));
  72. $this->display('/Public/TipsError');
  73. }
  74. }
  75. /**
  76. * [EmailRegInfo 用户注册页面-邮箱]
  77. * @author Devil
  78. * @blog http://gong.gg/
  79. * @version 0.0.1
  80. * @datetime 2017-03-10T12:18:17+0800
  81. */
  82. public function EmailRegInfo()
  83. {
  84. if(in_array('email', MyC('home_user_reg_state')))
  85. {
  86. if(empty($this->user))
  87. {
  88. $this->assign('referer_url', $this->GetrefererUrl());
  89. $this->display('EmailRegInfo');
  90. } else {
  91. $this->assign('msg', L('common_reg_already_had_tips'));
  92. $this->display('/Public/TipsError');
  93. }
  94. } else {
  95. $this->assign('msg', L('common_close_email_user_reg_tips'));
  96. $this->display('/Public/TipsError');
  97. }
  98. }
  99. /**
  100. * [RegInfo 用户注册页面-短信]
  101. * @author Devil
  102. * @blog http://gong.gg/
  103. * @version 0.0.1
  104. * @datetime 2017-03-02T22:48:35+0800
  105. */
  106. public function RegInfo()
  107. {
  108. if(in_array('sms', MyC('home_user_reg_state')))
  109. {
  110. if(empty($this->user))
  111. {
  112. $this->assign('referer_url', $this->GetrefererUrl());
  113. $this->display('RegInfo');
  114. } else {
  115. $this->assign('msg', L('common_reg_already_had_tips'));
  116. $this->display('/Public/TipsError');
  117. }
  118. } else {
  119. $this->assign('msg', L('common_close_sms_user_reg_tips'));
  120. $this->display('/Public/TipsError');
  121. }
  122. }
  123. /**
  124. * [LoginInfo 用户登录页面]
  125. * @author Devil
  126. * @blog http://gong.gg/
  127. * @version 0.0.1
  128. * @datetime 2017-03-02T22:48:35+0800
  129. */
  130. public function LoginInfo()
  131. {
  132. if(MyC('home_user_login_state') == 1)
  133. {
  134. if(empty($this->user))
  135. {
  136. $this->assign('referer_url', $this->GetrefererUrl());
  137. $this->display('LoginInfo');
  138. } else {
  139. $this->assign('msg', L('common_login_already_had_tips'));
  140. $this->display('/Public/TipsError');
  141. }
  142. } else {
  143. $this->assign('msg', L('common_close_user_login_tips'));
  144. $this->display('/Public/TipsError');
  145. }
  146. }
  147. /**
  148. * [Reg 用户注册-数据添加]
  149. * @author Devil
  150. * @blog http://gong.gg/
  151. * @version 0.0.1
  152. * @datetime 2017-03-07T00:08:36+0800
  153. */
  154. public function Reg()
  155. {
  156. // 是否开启用户注册
  157. if(!in_array(I('type'), MyC('home_user_reg_state')))
  158. {
  159. $this->error(L('common_close_user_reg_tips'));
  160. }
  161. // 是否ajax请求
  162. if(!IS_AJAX)
  163. {
  164. $this->error(L('common_unauthorized_access'));
  165. }
  166. // 账户校验
  167. $this->UserRegAccountsCheck();
  168. // 验证码校验
  169. $verify_param = array(
  170. 'key_prefix' => 'reg',
  171. 'expire_time' => MyC('common_verify_expire_time')
  172. );
  173. if(I('type') == 'sms')
  174. {
  175. $obj = new \My\Sms($verify_param);
  176. } else {
  177. $obj = new \My\Email($verify_param);
  178. }
  179. // 是否已过期
  180. if(!$obj->CheckExpire())
  181. {
  182. $this->ajaxReturn(L('common_verify_expire'), -10);
  183. }
  184. // 是否正确
  185. if(!$obj->CheckCorrect(I('verify')))
  186. {
  187. $this->ajaxReturn(L('common_verify_error'), -11);
  188. }
  189. // 模型
  190. $m = D('User');
  191. // 数据自动校验
  192. if($m->create($_POST, 1))
  193. {
  194. // 额外数据处理
  195. if(I('type') == 'sms')
  196. {
  197. $m->mobile = I('accounts');
  198. } else {
  199. $m->email = I('accounts');
  200. }
  201. $m->add_time = time();
  202. $m->upd_time = time();
  203. $m->salt = GetNumberCode(6);
  204. $m->pwd = LoginPwdEncryption(I('pwd'), $m->salt);
  205. // 数据添加
  206. $user_id = $m->add();
  207. if($user_id > 0)
  208. {
  209. // 清除验证码
  210. $obj->Remove();
  211. if($this->UserLoginRecord($user_id))
  212. {
  213. $this->ajaxReturn(L('common_reg_success'));
  214. }
  215. $this->ajaxReturn(L('common_reg_success_login_tips'));
  216. } else {
  217. $this->ajaxReturn(L('common_reg_error'), -100);
  218. }
  219. } else {
  220. $this->ajaxReturn($m->getError(), -1);
  221. }
  222. }
  223. /**
  224. * [Login 用户登录]
  225. * @author Devil
  226. * @blog http://gong.gg/
  227. * @version 0.0.1
  228. * @datetime 2017-03-09T10:57:31+0800
  229. */
  230. public function Login()
  231. {
  232. // 是否开启用户登录
  233. if(MyC('home_user_login_state') != 1)
  234. {
  235. $this->error(L('common_close_user_login_tips'));
  236. }
  237. // 是否ajax请求
  238. if(!IS_AJAX)
  239. {
  240. $this->error(L('common_unauthorized_access'));
  241. }
  242. // 登录帐号格式校验
  243. $accounts = I('accounts');
  244. if(!CheckMobile($accounts) && !CheckEmail($accounts))
  245. {
  246. $this->ajaxReturn(L('user_login_accounts_format'), -1);
  247. }
  248. // 密码
  249. $pwd = trim(I('pwd'));
  250. if(!CheckLoginPwd($pwd))
  251. {
  252. $this->ajaxReturn(L('user_reg_pwd_format'), -2);
  253. }
  254. // 获取用户账户信息
  255. $where = array('mobile' => $accounts, 'email' => $accounts, '_logic' => 'OR');
  256. $user = M('User')->field(array('id', 'pwd', 'salt', 'state'))->where($where)->find();
  257. if(empty($user))
  258. {
  259. $this->ajaxReturn(L('user_login_accounts_on_exist_error'), -3);
  260. }
  261. // 用户状态
  262. if($user['state'] == 2)
  263. {
  264. $this->ajaxReturn(L('common_user_state_list')[$user['state']]['tips'], -10);
  265. }
  266. // 密码校验
  267. if(LoginPwdEncryption($pwd, $user['salt']) != $user['pwd'])
  268. {
  269. $this->ajaxReturn(L('user_common_pwd_error'), -4);
  270. }
  271. // 更新用户密码
  272. $salt = GetNumberCode(6);
  273. $data = array(
  274. 'pwd' => LoginPwdEncryption($pwd, $salt),
  275. 'salt' => $salt,
  276. 'upd_time' => time(),
  277. );
  278. if(M('User')->where(array('id'=>$user['id']))->save($data) !== false)
  279. {
  280. // 登录记录
  281. if($this->UserLoginRecord($user['id']))
  282. {
  283. $this->ajaxReturn(L('common_login_success'));
  284. }
  285. }
  286. $this->ajaxReturn(L('common_login_invalid'), -100);
  287. }
  288. /**
  289. * [UserVerifyEntry 用户-验证码显示]
  290. * @author Devil
  291. * @blog http://gong.gg/
  292. * @version 0.0.1
  293. * @datetime 2017-03-05T15:10:21+0800
  294. */
  295. public function UserVerifyEntry()
  296. {
  297. $this->CommonVerifyEntry(I('type', 'reg'));
  298. }
  299. /**
  300. * [RegVerifySend 用户注册-验证码发送]
  301. * @author Devil
  302. * @blog http://gong.gg/
  303. * @version 0.0.1
  304. * @datetime 2017-03-05T19:17:10+0800
  305. */
  306. public function RegVerifySend()
  307. {
  308. // 是否开启用户注册
  309. if(!in_array(I('type'), MyC('home_user_reg_state')))
  310. {
  311. $this->error(L('common_close_user_reg_tips'));
  312. }
  313. // 是否ajax请求
  314. if(!IS_AJAX)
  315. {
  316. $this->error(L('common_unauthorized_access'));
  317. }
  318. // 账户校验
  319. $this->UserRegAccountsCheck();
  320. // 验证码公共基础参数
  321. $verify_param = array(
  322. 'key_prefix' => 'reg',
  323. 'expire_time' => MyC('common_verify_expire_time'),
  324. 'time_interval' => MyC('common_verify_time_interval'),
  325. );
  326. // 是否开启图片验证码
  327. $verify = $this->CommonIsImaVerify($verify_param);
  328. // 发送验证码
  329. $code = GetNumberCode(6);
  330. if(I('type') == 'sms')
  331. {
  332. $obj = new \My\Sms($verify_param);
  333. $state = $obj->SendText(I('accounts'), MyC('home_sms_user_reg'), $code);
  334. } else {
  335. $obj = new \My\Email($verify_param);
  336. $email_param = array(
  337. 'email' => I('accounts'),
  338. 'content' => MyC('home_email_user_reg'),
  339. 'title' => MyC('home_site_name').' - '.L('common_email_send_user_reg_title'),
  340. 'code' => $code,
  341. );
  342. $state = $obj->SendHtml($email_param);
  343. }
  344. // 状态
  345. if($state)
  346. {
  347. // 清除验证码
  348. if(isset($verify) && is_object($verify))
  349. {
  350. $verify->Remove();
  351. }
  352. $this->ajaxReturn(L('common_send_success'));
  353. } else {
  354. $this->ajaxReturn(L('common_send_error').'['.$obj->error.']', -100);
  355. }
  356. }
  357. /**
  358. * [UserRegAccountsCheck 用户注册账户校验]
  359. * @author Devil
  360. * @blog http://gong.gg/
  361. * @version 0.0.1
  362. * @datetime 2017-03-10T10:06:29+0800
  363. */
  364. private function UserRegAccountsCheck()
  365. {
  366. // 参数
  367. $type = I('type');
  368. $accounts = I('accounts');
  369. if(empty($accounts) || empty($type) || !in_array($type, array('sms', 'email')))
  370. {
  371. $this->ajaxReturn(L('common_param_error'), -1);
  372. }
  373. // 手机号码
  374. if($type == 'sms')
  375. {
  376. // 手机号码格式
  377. if(!CheckMobile($accounts))
  378. {
  379. $this->ajaxReturn(L('common_mobile_format_error'), -2);
  380. }
  381. // 手机号码是否已存在
  382. if($this->IsExistAccounts($accounts, 'mobile'))
  383. {
  384. $this->ajaxReturn(L('common_mobile_exist_error'), -3);
  385. }
  386. // 电子邮箱
  387. } else {
  388. // 电子邮箱格式
  389. if(!CheckEmail($accounts))
  390. {
  391. $this->ajaxReturn(L('common_email_format_error'), -2);
  392. }
  393. // 电子邮箱是否已存在
  394. if($this->IsExistAccounts($accounts, 'email'))
  395. {
  396. $this->ajaxReturn(L('common_email_exist_error'), -3);
  397. }
  398. }
  399. }
  400. /**
  401. * [ForgetPwdVerifySend 密码找回验证码发送]
  402. * @author Devil
  403. * @blog http://gong.gg/
  404. * @version 0.0.1
  405. * @datetime 2017-03-10T17:35:03+0800
  406. */
  407. public function ForgetPwdVerifySend()
  408. {
  409. // 是否ajax请求
  410. if(!IS_AJAX)
  411. {
  412. $this->error(L('common_unauthorized_access'));
  413. }
  414. // 参数
  415. $accounts = I('accounts');
  416. if(empty($accounts))
  417. {
  418. $this->ajaxReturn(L('common_param_error'), -10);
  419. }
  420. // 账户是否存在
  421. $type = $this->UserForgetAccountsCheck($accounts);
  422. // 验证码公共基础参数
  423. $verify_param = array(
  424. 'key_prefix' => 'forget',
  425. 'expire_time' => MyC('common_verify_expire_time'),
  426. 'time_interval' => MyC('common_verify_time_interval'),
  427. );
  428. // 是否开启图片验证码
  429. $verify = $this->CommonIsImaVerify($verify_param);
  430. // 验证码
  431. $code = GetNumberCode(6);
  432. // 手机
  433. if($type == 'mobile')
  434. {
  435. $obj = new \My\Sms($verify_param);
  436. $state = $obj->SendText($accounts, MyC('home_sms_user_forget_pwd'), $code);
  437. // 邮箱
  438. } else if($type == 'email')
  439. {
  440. $obj = new \My\Email($verify_param);
  441. $email_param = array(
  442. 'email' => $accounts,
  443. 'content' => MyC('home_email_user_forget_pwd'),
  444. 'title' => MyC('home_site_name').' - '.L('common_email_send_user_reg_title'),
  445. 'code' => $code,
  446. );
  447. $state = $obj->SendHtml($email_param);
  448. } else {
  449. $this->ajaxReturn(L('user_login_accounts_format'), -1);
  450. }
  451. // 状态
  452. if($state)
  453. {
  454. // 清除验证码
  455. if(isset($verify) && is_object($verify))
  456. {
  457. $verify->Remove();
  458. }
  459. $this->ajaxReturn(L('common_send_success'));
  460. } else {
  461. $this->ajaxReturn(L('common_send_error').'['.$obj->error.']', -100);
  462. }
  463. }
  464. /**
  465. * [ForgetPwd 密码找回]
  466. * @author Devil
  467. * @blog http://gong.gg/
  468. * @version 0.0.1
  469. * @datetime 2017-03-10T17:55:42+0800
  470. */
  471. public function ForgetPwd()
  472. {
  473. // 是否ajax请求
  474. if(!IS_AJAX)
  475. {
  476. $this->error(L('common_unauthorized_access'));
  477. }
  478. // 参数
  479. $accounts = I('accounts');
  480. $verify = I('verify');
  481. $pwd = trim(I('pwd'));
  482. if(empty($accounts) || empty($verify) || empty($pwd))
  483. {
  484. $this->ajaxReturn(L('common_param_error'), -1);
  485. }
  486. // 账户是否存在
  487. $field = $this->UserForgetAccountsCheck($accounts);
  488. // 验证码校验
  489. $verify_param = array(
  490. 'key_prefix' => 'forget',
  491. 'expire_time' => MyC('common_verify_expire_time'),
  492. 'time_interval' => MyC('common_verify_time_interval'),
  493. );
  494. if($field == 'mobile')
  495. {
  496. $obj = new \My\Sms($verify_param);
  497. } else if($field == 'email')
  498. {
  499. $obj = new \My\Email($verify_param);
  500. }
  501. // 是否已过期
  502. if(!$obj->CheckExpire())
  503. {
  504. $this->ajaxReturn(L('common_verify_expire'), -10);
  505. }
  506. // 是否正确
  507. if(!$obj->CheckCorrect($verify))
  508. {
  509. $this->ajaxReturn(L('common_verify_error'), -11);
  510. }
  511. // 更新用户密码
  512. $salt = GetNumberCode(6);
  513. $data = array(
  514. 'pwd' => LoginPwdEncryption($pwd, $salt),
  515. 'salt' => $salt,
  516. 'upd_time' => time(),
  517. );
  518. if(M('User')->where(array($field=>$accounts))->save($data) !== false)
  519. {
  520. $this->ajaxReturn(L('common_operation_success'));
  521. }
  522. $this->ajaxReturn(L('common_operation_error'), -100);
  523. }
  524. /**
  525. * [UserForgetAccountsCheck 帐号校验]
  526. * @author Devil
  527. * @blog http://gong.gg/
  528. * @version 0.0.1
  529. * @datetime 2017-03-10T17:59:53+0800
  530. * @param [string] $accounts [账户名称]
  531. * @return [string] [账户字段 mobile, email]
  532. */
  533. private function UserForgetAccountsCheck($accounts)
  534. {
  535. if(CheckMobile($accounts))
  536. {
  537. if(!$this->IsExistAccounts($accounts, 'mobile'))
  538. {
  539. $this->ajaxReturn(L('common_mobile_no_exist_error'), -3);
  540. }
  541. return 'mobile';
  542. } else if(CheckEmail($accounts))
  543. {
  544. if(!$this->IsExistAccounts($accounts, 'email'))
  545. {
  546. $this->ajaxReturn(L('common_email_no_exist_error'), -3);
  547. }
  548. return 'email';
  549. }
  550. $this->ajaxReturn(L('common_accounts_format_error'), -4);
  551. }
  552. /**
  553. * [IsExistAccounts 账户是否存在]
  554. * @author Devil
  555. * @blog http://gong.gg/
  556. * @version 0.0.1
  557. * @datetime 2017-03-08T10:27:14+0800
  558. * @param [string] $accounts [账户名称]
  559. * @param [string] $field [字段名称]
  560. * @return [boolean] [存在true, 不存在false]
  561. */
  562. private function IsExistAccounts($accounts, $field = 'mobile')
  563. {
  564. $id = M('User')->where(array($field=>$accounts))->getField('id');
  565. return !empty($id);
  566. }
  567. /**
  568. * [Logout 退出]
  569. * @author Devil
  570. * @blog http://gong.gg/
  571. * @version 0.0.1
  572. * @datetime 2016-12-05T14:31:23+0800
  573. */
  574. public function Logout()
  575. {
  576. if(isset($_SESSION['user']))
  577. {
  578. unset($_SESSION['user']);
  579. }
  580. redirect(__MY_URL__);
  581. }
  582. }
  583. ?>