cookie:

引入js文件:jquery.cookie.zip //下面的bootbox非重点,就不引入js了

  1. <script src="js/jquery.cookie.js"></script>

示例:

6%FMZ(75KZN6DQ8EUJPN4}A.png

  1. <div class="signin-rit">
  2. <span>
  3. <label class="checkbox pull-left" ><input type="checkbox" style="margin-left: 0px"v-model="remember" name="checkbox" checked="">记住密码</label>
  4. </span>
  5. <a class="forgot" href="resetPsw.html" title="忘记密码?">忘记密码?</a>
  6. <div class="clear"> </div>
  7. </div>
<script>
  var app = new Vue({
    el: '#app',
    data: {
      remember:true,//默认记住密码
    },
    methods:{
     login:function () {
         var that=this;
         //登陆验证
         var data={
           userName: that.userName,
           password: that.password
         }
         //server.ip="http://localhost:8080"
           axios.post(server.ip+'/user/login.json', data)
             .then(function (r) {
               var flag=r.data.e.code
               if(flag!=0){//登陆失败
                 var dialog = bootbox.dialog({
                   title: '正在登陆...',
                   message: '<p><i class="fa fa-spin fa-spinner"></i> Loading...</p>',
                   size: 'small',
                 })
                   dialog.init(function(){
                   setTimeout(function(){
                     dialog.find('.bootbox-body').html(r.data.e.desc);
                   }, 1000);
                 });
              }else{
                 setCurrentUser(r.data.data.sendData[0]);
                 bootbox.alert({
                   message: "登陆成功 !",
                   size: 'small'
                 });
                 if(that.remember){
                   $.cookie("userName", that.userName, { expires: 7});
                   $.cookie("password", that.password, { expires: 7 }); //7天
                 }
                 setTimeout(function () {
                   window.location.href="index.html";
                 }, 1000);
               }
             })
             .catch(function (error) {
               console.log(error);
             });
         },
    },
    mounted:function () {
      var that = this;
      if($.cookie('userName')){that.userName=$.cookie('userName')}
      if($.cookie('password')){that.password=$.cookie('password')}
      }
  })

</script>

localStorage:

定义一个js文件:

var currentUser = null;
var storage = window.localStorage;

function setCurrentUser(user) {
  if (window.localStorage) {
    var userJsonStr = JSON.stringify(user)
    storage.setItem('user', userJsonStr)
  } else {
    alert('当前浏览器不支持localStorage,请升级')
  }
};

function getCurrentUser() {
  if (currentUser !== null) {
    return currentUser
  } else {
    var userJsonStr = storage.getItem('user');
    currentUser = JSON.parse(userJsonStr);
    if (currentUser === null) {
      return null;
    } else {
      return currentUser;
    }
  }
}

使用方法 引入该js文件:

<script src="../../dist/js/main.js"></script>
登陆成功时回调方法中:
setCurrentUser(r.data.data.sendData[0]);//将返回的登陆userJson对象存到localStorage

别的页面使用当前登陆的user对象信息:
data:{
      currentUser:getCurrentUser(),
      txUrl:getCurrentUser().fileUrl?getCurrentUser().fileUrl:'../images/index1.jpg',
      userName:getCurrentUser().userName,
      email:getCurrentUser().email,
      introduce:getCurrentUser().introduce,
    },