Yii 操作cookie 及ajax登录记住账号功能

2017-10-18 Frank PHP

记录一下吧,算是常用的功能了。

<?php
/**
 * Created by PhpStorm.
 * User: Frank
 * Date: 2017/10/18 0018
 * Time: 13:04
 */

namespace app\components;
use Yii ;

class CookiesUtils{
    /**
     * 设置cookie
     * @param $name
     * @param $value
     */
    public static function  setCookie($name,$value) {

        $cookies = Yii::$app->response->cookies;

        $cookies->add(new \yii\web\Cookie([
            'name' => $name,
            'value' => $value,
            'expire' => time() + 86400 * 365,
        ]));
        //echo 'Cookie set!';
    }

    /**
     * 取值
     * @param $name
     * @return mixed|string
     */
    public static function getCookie($name) {

        $cookies1 = Yii::$app->request->cookies;
        $cookieValue = "" ;
        if ($cookies1->has($name))
            $cookieValue = $cookies1->getValue($name);

        return $cookieValue ;
        //echo 'value : '.$cookieValue;
    }

    /**
     * 删除cookie
     * @param $name
     */
    public static function deleteCookie($name){
        Yii::$app->response->cookies->remove($name);
    }
}

2. ajax登录控制器

/**
     * 登录
     *
     * @return string
     */
    public function actionLogin()
    {

        if($post = Yii::$app->request->post()){
            Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

            $mobile = $post["mobile"] ;
            $password = $post["password"] ;
            $user = Users::find()->where(["mobile"=>$mobile])->orWhere(['nickname'=>$mobile])->andWhere(['is_deleted'=>0])->one() ;
            if(empty($user)){
                $response = ["status"=>"n","info"=>"用户不存在"] ;
                return $response ;
            }
            $model = new LoginForm();
            $model->mobile = $mobile ;
            $model->password = $password ;
            $remember = Yii::$app->request->post("remember") ;
            $cookies = Yii::$app->response->cookies;
            if (!empty($remember) && $remember == "1") {
                CookiesUtils::setCookie("mobile",$mobile) ;
                CookiesUtils::setCookie("remember",$remember) ;
            } else {
                CookiesUtils::deleteCookie("mobile") ;
                CookiesUtils::deleteCookie("remember") ;
            }
            if($model->login()){
                $response = ["status"=>"y","info"=>"success"] ;
            }else{
                $response = ["status"=>"n","info"=>"用户名或密码错误","msg"=>$model->errors] ;
            }
            return $response ;
        }
        return $this->renderPartial('login');
    }

$response用return才是关键,如果用echo() 或die() cookie会设置不上。

3. ajax登录view

<form class="formLogin">
    <input type="text" name="mobile" id="mobile" class="form-control" placeholder="手机号码" value="<?=CookiesUtils::getCookie("mobile") ?>" />
    <input type="password" autocomplete="new-password" name="password" id="password" class="form-control" placeholder="密码" />
    <input type="checkbox" name="remember" <?=CookiesUtils::getCookie("remember")=="1"?"checked":"" ?> id="remember">请记住我
    <button type="button" class="loginBtn" id="loginBtn">登  录</button>
</form>
/**
     * 登录
     */
    $("#loginBtn").click(function () {
        var mobile = $("#mobile").val() ;
        var password = $("#password").val() ;
        var remember = 0 ;
        if($("#remember").is(":checked")){
            remember = 1 ;
        }
        var csrf = $("#csrf").val() ;
        if(mobile==""){
            alert("手机号码不能为空") ;
            return ;
        }
        if (password == "") {
            alert( "请输入登录密码");
            return;
        }
        var data = {mobile:mobile,password:password,remember:remember,"_csrf":csrf} ;
        console.log(data) ;
        $.ajax({
            url:"/login",
            type:"POST",
            async:false,
            data:data,
            success:function(data){
                if(data.status=="y"){
                    result = true ;
                    window.location.href="/" ;
                }else{
                    alert(data.info);
                }
            }
        }) ;
    }) ;

突然想能有自己的代码库该多好啊,省时又省力。希望我们都能不断减少一些重复劳动。

参考:

« 上一篇:nodejs webkit | 下一篇:tomcat struts2 404 配置»

发表评论 登录

Top