Getpassword.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace app\index\controller;
  3. use app\common\model\User AS UserModel;
  4. use app\common\controller\IndexBase;
  5. use think\Controller;
  6. class Getpassword extends IndexBase
  7. {
  8. /**
  9. * 获取邮箱或手机注册码
  10. * @param string $type
  11. */
  12. public function getnum($type='',$to=''){
  13. //邮箱注册码与手机注册码,不建议同时启用,所以这里没分开处理
  14. if( time()-get_cookie('send_num') <120 ){
  15. return $this->err_js('2分钟后,才能再次获取验证码!');
  16. }
  17. $to = mymd5($to,'DE');
  18. $num = cache(get_cookie('user_sid').'_reg') ?: rand(1000,9999);
  19. $send_num = get_md5_num($to.$num,6);
  20. $title = '来自《'.config('webdb.webname').'》的验证码,请注册查收';
  21. $content = '你的验证码是:'.$send_num;
  22. cache(get_cookie('user_sid').'_reg',$num,600);
  23. if($type=='mobphone'){
  24. $result = send_sms($to,$send_num);
  25. }elseif($type=='email'){
  26. if( UserModel::get_info($to,'email') ){
  27. $result = send_mail($to,$title,$content);
  28. }else{
  29. $result = '当前邮箱不存在!';
  30. }
  31. }else{
  32. $result = '请选择类型!';
  33. }
  34. if($result===true){
  35. set_cookie('send_num', time());
  36. return $this->ok_js();
  37. }else{
  38. return $this->err_js($result.$send_num);
  39. }
  40. }
  41. /**
  42. * 核对手机或邮箱注册码
  43. * @param string $num
  44. * @return void|\think\response\Json
  45. */
  46. public function check_num($num='',$field=''){
  47. //邮箱注册码与手机注册码,不建议同时启用,所以这里没分开处理
  48. $field = mymd5($field,'DE');
  49. $_num = cache(get_cookie('user_sid').'_reg');
  50. $send_num = get_md5_num($field.$_num,6);
  51. if( $num == $send_num){
  52. return $this->ok_js();
  53. }
  54. return $this->err_js('验证码不正确');
  55. }
  56. /**
  57. * 校验用户名与密码
  58. */
  59. public function check($username='',$password='',$captcha=''){
  60. if($username!=''){
  61. $info = UserModel::get_info($username,'username');
  62. if($info){
  63. $array = [
  64. 'uid'=>$info['uid'],
  65. 'email'=>mymd5($info['email']),
  66. 'mobphone'=>$info['mobphone']?mymd5($info['mobphone']):'',
  67. ];
  68. return $this->ok_js($array);
  69. }else{
  70. return $this->err_js('用户不存在!');
  71. }
  72. }else{
  73. $data = get_post('get');
  74. if(isset($data['captcha'])&&$data['captcha']==''){
  75. $data['captcha'] = 'test';
  76. }
  77. foreach($data AS $key=>$value){
  78. $name = $key;
  79. break;
  80. }
  81. $result = $this->validate($data, 'Reg.'.$name);
  82. if( $result!==true ){
  83. return $this->err_js($result);
  84. }else{
  85. return $this->ok_js();
  86. }
  87. }
  88. }
  89. /**
  90. * 取回密码
  91. * @return mixed|string
  92. */
  93. public function index()
  94. {
  95. if ($this->user) {
  96. $this->error('你已经登录了!');
  97. }
  98. $data = get_post('post');
  99. hook_listen('getpassword_begin',$data);
  100. if(IS_POST){
  101. $info = UserModel::get_info($data['username'],'username');
  102. if(!$info){
  103. $this->error('帐号不存在');
  104. }
  105. if($data['captcha']==''){
  106. $data['captcha'] = 'test';
  107. }
  108. $result = $this->validate($data, 'Reg.captcha');
  109. if(true !== $result) $this->error($result);
  110. //邮箱注册码与手机注册码,这里只判断只中一种
  111. $num = cache(get_cookie('user_sid').'_reg');
  112. $send_num = get_md5_num((($this->webdb['getpassword_by_phone']&&$info['mobphone'])?$info['mobphone']:$info['email']).$num,6);
  113. if( ($data['email_code']!=$send_num&&$data['phone_code']!=$send_num) || empty($num)) {
  114. $this->error('验证码不对');
  115. }
  116. cache(get_cookie('user_sid').'_reg',null);
  117. $array = [
  118. 'uid'=>$info['uid'],
  119. 'password'=>$data['password'],
  120. ];
  121. $result = UserModel::edit_user($array);
  122. hook_listen('getpassword_end',$info,$data);
  123. if($result){
  124. $this->success('密码设置成功','index/index');
  125. }else{
  126. $this->error('密码设置失败!');
  127. }
  128. }
  129. return $this->fetch();
  130. }
  131. }