我使用Yii2-advanced-app(2.0.15),并且需要使用OTP进行登录操作。
使用单元号和密码,一切都正确并且cookie是正确的,但是当我使用opt时,不会为cookie创建任何值。
我的Ajax代码:
$("#m-sendCode__form-submit").click(function() {
$(this).attr('disabled','true');
let mobile = $('#mobile').val();
let csrfToken = $('meta[name="csrf-token"]').attr("content");
let rememberMe = $("#remember2").prop('checked');
// console.log(rememberMe);
$.ajax({
url: '/loginbysms',
method: 'POST',
data: {
_csrfFrontend: csrfToken,
phone: phone,
rememberMe: rememberMe
},
timeout: 6000
})
.done(function(data) {
let response = JSON.parse(data);
// console.log(data);
if (response.sent === 1){
$.ajax({
url: '/loginbysms',
method: 'POST',
data: {
_csrfFrontend: csrfToken,
verify: verify,
// rememberMe: rememberMe
},
})
.done(function(data) {
let s = JSON.parse(data);
if (s.status === 1){
window.location.href = '/';
}
});
}
})
.fail(function(error)){
console.log(error);
});
});
我的控制器是:
public function actionLoginbysms()
{
$dataAjax = Yii::$app->request->post();
$session = Yii::$app->session;
if(isset($dataAjax)) {
if (isset($dataAjax['phone']) && !empty($dataAjax['phone'])) {
$phone = $dataAjax['phone'];
$user = User::findByPhone2($phone);
$sendSMS = new SendSMS();
if ($sendSMS->SendSMS($user->user_otp, $phone)) {
echo json_encode(['sent' => 1]);
exit;
} else {
echo json_encode(['sent' => 0]);
exit;
}
}
if(isset($dataAjax['verify]) && !empty($dataAjax['verfy'])){
$authorizedUser = User::findByOtp($session-
>get('user_phone'), $dataAjax['verify']);
if (isset($authorizedUser) && !empty($authorizedUser)) {
Yii::$app->user->login($authorizedUser, 3600 * 24 *
30)
echo json_encode(['status' => 1]);
exit;
}
}
}
}
当所有内容都为真且用户正确发送了代码时,用户将正确进入主页,但不会保存cookie的值。
请告诉我错误。
答案 0 :(得分:0)
控制器应作如下更改:
public function actionLoginbysms()
{
$dataAjax = Yii::$app->request->post();
$session = Yii::$app->session;
if(Yii::$app->request->isAjax) {
Yii::$app->response->format = Response::FORMAT_JSON;
if (isset($dataAjax['phone']) && !empty($dataAjax['phone'])) {
$mobile = $dataAjax['phone'];
$user = User::findByPhone2($phone);
if ($user) {
unset($dataAjax['phone']);
$numbers = range(10000, 99999);
shuffle($numbers);
$session->set('user_phone', $phone);
if (isset($dataAjax['rememberMe']) && !empty($dataAjax['rememberMe'])
&& ($dataAjax['rememberMe'] == true)) {
$session->set('rememberMe', 'yes');
unset($dataAjax['rememberMe']);
}
$user->user_otp = $numbers[0];
$user->save();
try {
$sendSMS = new SendSMS();
$sendSMS->SendSMS($user->user_otp, $phone);
} catch (\Throwable $e) {
return [
'sent' => 0
];
}
return [
'sent' => 1
];
} else {
return ['user_not_found' => 1];
}
}
else if (isset($dataAjax['verify']) && !empty($dataAjax['verify'])) {
if ($session->isActive && $session->has('user_phone')) {
$authorizedUser = User::findByOtp($session->get('user_phone'), $dataAjax['verify']);
if (isset($authorizedUser) && !empty($authorizedUser)) {
unset($dataAjax['verify']);
$session->remove('user_phone');
$authorizedUser->user_otp = '';
$authorizedUser->save();
if(Yii::$app->user->login($authorizedUser, 3600 * 24 * 30)) {
return ['authenticationSuccess' => 1];
}
}
}
return ['authenticationSuccess' => 0];
}
}
}
正如@ rob006所说,我们不应使用退出。
借助 Yii :: @ app-> response-> format = Response :: FORMAT_JSON ,一切正常