UserController.class.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace Api\Controller;
  3. use Think\Controller;
  4. class UserController extends BaseController {
  5. //注册
  6. public function register(){
  7. $username = trim(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. session('v_code',null) ;
  23. $this->sendResult(array());
  24. }else{
  25. $this->sendError(10101,'register fail');
  26. }
  27. }else{
  28. $this->sendError(10101,L('username_exists'));
  29. }
  30. }else{
  31. $this->sendError(10101,L('code_much_the_same'));
  32. }
  33. }else{
  34. $this->sendError(10206,L('verification_code_are_incorrect'));
  35. }
  36. }
  37. //登录
  38. public function login(){
  39. $username = I("username");
  40. $password = I("password");
  41. $v_code = I("v_code");
  42. //检查用户输错密码的次数。如果超过一定次数,则需要验证 验证码
  43. $key= 'login_fail_times_'.$username;
  44. if(!D("VerifyCode")->_check_times($key)){
  45. if (!$v_code || $v_code != session('v_code')) {
  46. $this->sendError(10206,L('verification_code_are_incorrect'));
  47. return;
  48. }
  49. }
  50. $ret = D("User")->checkLogin($username,$password);
  51. if ($ret) {
  52. unset($ret['password']);
  53. session("login_user" , $ret );
  54. D("User")->setLastTime($ret['uid']);
  55. $token = D("UserToken")->createToken($ret['uid']);
  56. cookie('cookie_token',$token,60*60*24*90);//此处由服务端控制token是否过期,所以cookies过期时间设置多久都无所谓
  57. $this->sendResult(array());
  58. }else{
  59. D("VerifyCode")->_ins_times($key);//输错密码则设置输错次数
  60. if(D("VerifyCode")->_check_times($key)){
  61. $error_code = 10204 ;
  62. }else{
  63. $error_code = 10210 ;
  64. }
  65. $this->sendError($error_code,L('username_or_password_incorrect'));
  66. return;
  67. }
  68. }
  69. //获取用户信息
  70. public function info(){
  71. $login_user = $this->checkLogin();
  72. $uid = $login_user['uid'] ;
  73. $field = "uid,username,email,name,avatar,avatar_small" ;
  74. $info = D("User")->where(" uid = '$uid' ")->field($field)->find();
  75. $this->sendResult($info);
  76. }
  77. //通过旧密码验证来更新用户密码
  78. public function resetPassword(){
  79. $login_user = $this->checkLogin();
  80. $username = $login_user['username'];
  81. $password = I("password");
  82. $new_password = I("new_password");
  83. $ret = D("User")->checkLogin($username,$password);
  84. if ($ret) {
  85. $ret = D("User")->updatePwd($login_user['uid'],$new_password);
  86. if ($ret) {
  87. $this->sendResult(array());
  88. }else{
  89. $this->sendError(10101,L('modify_faild'));
  90. }
  91. }else{
  92. $this->sendError(10101,L('old_password_incorrect'));
  93. }
  94. }
  95. //退出登录
  96. public function logout(){
  97. $login_user = $this->checkLogin();
  98. D("UserToken")->where(" uid = '$login_user[uid]' ")->save(array("token_expire"=>0));
  99. session("login_user" , NULL);
  100. cookie('cookie_token',NULL);
  101. session(null);
  102. $this->sendResult(array());
  103. }
  104. }