UserController.class.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. $this->sendResult(array());
  23. }else{
  24. $this->sendError(10101,'register fail');
  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. //获取用户信息
  69. public function info(){
  70. $login_user = $this->checkLogin();
  71. $uid = $login_user['uid'] ;
  72. $field = "uid,username,email,email_verify,name,avatar,avatar_small" ;
  73. $info = D("User")->where(" uid = '$uid' ")->field($field)->find();
  74. $this->sendResult($info);
  75. }
  76. //通过旧密码验证来更新用户密码
  77. public function resetPassword(){
  78. $login_user = $this->checkLogin();
  79. $username = $login_user['username'];
  80. $password = I("password");
  81. $new_password = I("new_password");
  82. $ret = D("User")->checkLogin($username,$password);
  83. if ($ret) {
  84. $ret = D("User")->updatePwd($login_user['uid'],$new_password);
  85. if ($ret) {
  86. $this->sendResult(array());
  87. }else{
  88. $this->sendError(10101,L('modify_faild'));
  89. }
  90. }else{
  91. $this->sendError(10101,L('old_password_incorrect'));
  92. }
  93. }
  94. }