Ejemplo n.º 1
0
 /**
  * 找回密码 第二步
  * 如果是手机 跳转进入发短信验证码界面
  * 如果是邮箱,发邮件进入邮箱提醒页面
  * @param checkkey   str    密钥
  * @param password  str     密码
  * @param repassword  str   重复密码
  * @param sendcode   str    验证码
  * @param username  str     用户名
  *
  * @return json or 跳转
  */
 public function actionSetPassword()
 {
     //判断登陆,成功跳转到用户中心,
     PublicLibrary::is_login() ? $this->redirect(['/']) : '';
     $session = Yii::$app->session;
     $session->isActive ? '' : $session->open();
     if (Yii::$app->request->isPost) {
         if (PublicLibrary::is_ajax()) {
             $setarr = Yii::$app->request->post();
             if (!PublicLibrary::contestKey($setarr['checkkey']) && !empty($setarr['checkkey'])) {
                 //echo "<script>alert('请正确提交!'),window.location.href='index.php?r=user/find-password';</script>";
                 echo PublicLibrary::format_res_encode('1001', '请正确提交!', '');
                 exit;
             }
             if ($setarr['from'] == 'mobile') {
                 if (empty($setarr['sendcode']) || $session->get('sendcode') != $setarr['sendcode']) {
                     //echo "<script>alert('验证码不正确!'),window.location.href='index.php?r=user/find-password';</script>";
                     echo PublicLibrary::format_res_encode('1002', '验证码不正确!', '');
                     exit;
                 }
             }
             if ($setarr['password'] != $setarr['repassword'] && !empty($setarr['repassword'])) {
                 //echo "<script>alert('两次密码不同!'),window.location.href='index.php?r=user/find-password';</script>";
                 echo PublicLibrary::format_res_encode('1003', '两次密码不同!', '');
                 exit;
             }
             if (strlen($setarr['password']) <= self::PASSWORD_MAXLENTH && strlen($setarr['password']) > self::PASSWORD_MINLENTH) {
                 echo PublicLibrary::format_res_encode('1004', '密码长度不能小于六位!', '');
                 exit;
             }
         } else {
             $setarr = Yii::$app->request->post();
             if (!PublicLibrary::contestKey($setarr['checkkey']) && !empty($setarr['checkkey'])) {
                 echo "<script>alert('请正确提交!'),window.location.href='" . Url::toRoute('user/find-password') . "';</script>";
                 exit;
             }
             if ($setarr['from'] == 'mobile') {
                 if (empty($setarr['sendcode']) || $session->get('sendcode') != $setarr['sendcode']) {
                     echo "<script>alert('验证码不正确!'),window.location.href='" . Url::toRoute('user/find-password') . "';</script>";
                     exit;
                 }
             }
             if ($setarr['password'] != $setarr['repassword'] && !empty($setarr['repassword'])) {
                 echo "<script>alert('两次密码不同!'),window.location.href='" . Url::toRoute('user/find-password') . "';</script>";
                 exit;
             }
             if (strlen($setarr['password']) <= self::PASSWORD_MAXLENTH && strlen($setarr['password']) > self::PASSWORD_MINLENTH) {
                 echo "<script>alert('密码长度不能小于六位!'),window.location.href='" . Url::toRoute('user/find-password') . "';</script>";
                 exit;
             }
         }
         $Member_model = new Member();
         $userone = $Member_model->getByUserName($setarr['username']);
         if (empty($userone)) {
             echo "<script>alert('该用户不存在!'),window.location.href='" . Url::toRoute('user/find-password') . "';</script>";
             exit;
         }
         $newpass = PublicLibrary::password_encode($setarr['password'], $userone['random']);
         $Member = Member::findOne($userone['uid']);
         $Member->password = $newpass;
         $return = $Member->save();
         if ($return) {
             // echo "<script>alert('找回密码成功!'),window.location.href='index.php?r=user/login';</script>";
             // exit;
             //echo "string";
             if (PublicLibrary::is_ajax()) {
                 $this->layout = false;
                 $data['html'] = $this->render('ajax_fpwsuccess', ['username' => $setarr['username']]);
                 echo PublicLibrary::format_res_encode('0', '密码修改成功', $data);
                 //echo $this->render('ajax_fpwsuccess',['username' => $setarr['username']]);
                 exit;
             } else {
                 return $this->render('fpw_success', ['username' => $setarr['username']]);
                 exit;
             }
             //$this->redirect(['login','msg'=>'找回密码成功!']);//跳转
         } else {
             if (PublicLibrary::is_ajax()) {
                 echo PublicLibrary::format_res_encode_check('1', '找回密码失败', '');
                 exit;
             } else {
                 echo "<script>alert('找回密码失败!'),window.location.href='" . Url::toRoute('user/find-password') . "';</script>";
                 exit;
             }
             //$this->redirect(['find-password','msg'=>'数据更新失败!']);//跳转
         }
     }
 }