UserController.class.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace Api\Controller;
  3. use Think\Controller;
  4. class UserController extends BaseController {
  5. //注册
  6. public function register(){
  7. $username = I("username");
  8. $password = I("password");
  9. $confirm_password = I("confirm_password");
  10. $v_code = I("v_code");
  11. if (C('CloseVerify') || $v_code && $v_code == session('v_code') ) {
  12. if ( $password != '' && $password == $confirm_password) {
  13. if ( ! D("User")->isExist($username) ) {
  14. $new_uid = D("User")->register($username,$password);
  15. if ($new_uid) {
  16. //设置自动登录
  17. $ret = D("User")->where("uid = '$new_uid' ")->find() ;
  18. unset($ret['password']);
  19. session("login_user" , $ret );
  20. $token = D("UserToken")->createToken($ret['uid']);
  21. cookie('cookie_token',$token,60*60*24*90);//此处由服务端控制token是否过期,所以cookies过期时间设置多久都无所谓
  22. $this->sendResult(array());
  23. }else{
  24. $this->sendError(10101,L('username_or_password_incorrect'));
  25. }
  26. }else{
  27. $this->sendError(10101,L('username_exists'));
  28. }
  29. }else{
  30. $this->sendError(10101,L('code_much_the_same'));
  31. }
  32. }else{
  33. $this->sendError(10206,L('verification_code_are_incorrect'));
  34. }
  35. }
  36. //登录
  37. public function login(){
  38. $username = I("username");
  39. $password = I("password");
  40. $v_code = I("v_code");
  41. //检查用户输错密码的次数。如果超过一定次数,则需要验证 验证码
  42. $key= 'login_fail_times_'.$username;
  43. if(!D("VerifyCode")->_check_times($key)){
  44. if (!$v_code || $v_code != session('v_code')) {
  45. $this->sendError(10206,L('verification_code_are_incorrect'));
  46. return;
  47. }
  48. }
  49. $ret = D("User")->checkLogin($username,$password);
  50. if ($ret) {
  51. unset($ret['password']);
  52. session("login_user" , $ret );
  53. D("User")->setLastTime($ret['uid']);
  54. $token = D("UserToken")->createToken($ret['uid']);
  55. cookie('cookie_token',$token,60*60*24*90);//此处由服务端控制token是否过期,所以cookies过期时间设置多久都无所谓
  56. $this->sendResult(array());
  57. }else{
  58. D("VerifyCode")->_ins_times($key);//输错密码则设置输错次数
  59. if(D("VerifyCode")->_check_times($key)){
  60. $error_code = 10204 ;
  61. }else{
  62. $error_code = 10210 ;
  63. }
  64. $this->sendError($error_code,L('username_or_password_incorrect'));
  65. return;
  66. }
  67. }
  68. }