You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
4.9 KiB

преди 3 години
  1. <?php
  2. namespace Home\Controller;
  3. use Think\Controller;
  4. class UserController extends BaseController {
  5. //注册
  6. public function register(){
  7. //跳转到web目录
  8. header("location:./web/#/user/register");
  9. exit();
  10. if (!IS_POST) {
  11. $this->assign('CloseVerify',C('CloseVerify'));
  12. $this->display ();
  13. }else{
  14. $username = I("username");
  15. $password = I("password");
  16. $confirm_password = I("confirm_password");
  17. $v_code = I("v_code");
  18. if (C('CloseVerify') || $v_code && $v_code == session('v_code') ) {
  19. if ( $password != '' && $password == $confirm_password) {
  20. if ( ! D("User")->isExist($username) ) {
  21. $ret = D("User")->register($username,$password);
  22. if ($ret) {
  23. $this->message(L('register_succeeded'),U('Home/User/login'));
  24. }else{
  25. $this->message('register fail');
  26. }
  27. }else{
  28. $this->message(L('username_exists'));
  29. }
  30. }else{
  31. $this->message(L('code_much_the_same'));
  32. }
  33. }else{
  34. $this->message(L('verification_code_are_incorrect'));
  35. }
  36. }
  37. }
  38. //登录
  39. public function login()
  40. {
  41. //跳转到web目录
  42. header("location:./web/#/user/login");
  43. exit();
  44. if (!IS_POST) {
  45. //如果有cookie记录,则自动登录
  46. $cookie_token = cookie('cookie_token');
  47. if ($cookie_token) {
  48. $ret = D("UserToken")->getToken($cookie_token);
  49. if ($ret && $ret['token_expire'] > time() ) {
  50. D("User")->setLastTime($ret['uid']);
  51. $login_user = D("User")->where(array('uid' => $ret['uid']))->field('password', true)->find();
  52. session("login_user" , $login_user);
  53. $this->message(L('auto_login_succeeded'),U('Home/Item/index'));
  54. exit();
  55. }
  56. }
  57. $this->assign('CloseVerify',C('CloseVerify'));
  58. $this->display ();
  59. }else{
  60. $username = I("username");
  61. $password = I("password");
  62. $v_code = I("v_code");
  63. if (C('CloseVerify')) { //如果关闭验证码
  64. $ret = D("User")->checkLogin($username,$password);
  65. if ($ret) {
  66. session("login_user" , $ret );
  67. D("User")->setLastTime($ret['uid']);
  68. $token = D("UserToken")->createToken($ret['uid']);
  69. cookie('cookie_token',$token,60*60*24*90);//此处由服务端控制token是否过期,所以cookies过期时间设置多久都无所谓
  70. unset($ret['password']);
  71. $this->message(L('login_succeeded'),U('Home/Item/index'));
  72. }else{
  73. $this->message(L('username_or_password_incorrect'));
  74. }
  75. }else{
  76. if ($v_code && $v_code == session('v_code')) {
  77. $ret = D("User")->checkLogin($username,$password);
  78. if ($ret) {
  79. session("login_user" , $ret );
  80. D("User")->setLastTime($ret['uid']);
  81. $token = D("UserToken")->createToken($ret['uid']);
  82. cookie('cookie_token',$token,60*60*24*90);//此处由服务端控制token是否过期,所以cookies过期时间设置多久都无所谓
  83. unset($ret['password']);
  84. $this->message(L('login_succeeded'),U('Home/Item/index'));
  85. }else{
  86. $this->message(L('username_or_password_incorrect'));
  87. }
  88. }else{
  89. $this->message(L('verification_code_are_incorrect'));
  90. }
  91. }
  92. }
  93. }
  94. //生成验证码
  95. public function verify(){
  96. //生成验证码图片
  97. Header("Content-type: image/PNG");
  98. $im = imagecreate(44,18); // 画一张指定宽高的图片
  99. $back = ImageColorAllocate($im, 245,245,245); // 定义背景颜色
  100. imagefill($im,0,0,$back); //把背景颜色填充到刚刚画出来的图片中
  101. $vcodes = "";
  102. srand((double)microtime()*1000000);
  103. //生成4位数字
  104. for($i=0;$i<4;$i++){
  105. $font = ImageColorAllocate($im, rand(100,255),rand(0,100),rand(100,255)); // 生成随机颜色
  106. $authnum=rand(1,9);
  107. $vcodes.=$authnum;
  108. imagestring($im, 5, 2+$i*10, 1, $authnum, $font);
  109. }
  110. $_SESSION['v_code'] = $vcodes;
  111. for($i=0;$i<200;$i++) //加入干扰象素
  112. {
  113. $randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
  114. imagesetpixel($im, rand()%70 , rand()%30 , $randcolor); // 画像素点函数
  115. }
  116. ImagePNG($im);
  117. ImageDestroy($im);
  118. }
  119. public function setting(){
  120. $user = $this->checkLogin();
  121. if (!IS_POST) {
  122. $this->assign("user",$user);
  123. $this->display ();
  124. }else{
  125. $username = $user['username'];
  126. $password = I("password");
  127. $new_password = I("new_password");
  128. $ret = D("User")->checkLogin($username,$password);
  129. if ($ret) {
  130. $ret = D("User")->updatePwd($user['uid'],$new_password);
  131. if ($ret) {
  132. $this->message(L('modify_succeeded'),U("Home/Item/index"));
  133. }else{
  134. $this->message(L('modify_faild'));
  135. }
  136. }else{
  137. $this->message(L('old_password_incorrect'));
  138. }
  139. }
  140. }
  141. //退出登录
  142. public function exist(){
  143. $login_user = $this->checkLogin();
  144. session("login_user" , NULL);
  145. cookie('cookie_token',NULL);
  146. session(null);
  147. $this->message(L('logout_succeeded'),U('Home/index/index'));
  148. }
  149. }