基於vue 作了關於token驗證的實例,和移動端下fixed失效的解決方案

vue-koa2-token

基於vue的 作了token驗證

前端部分(對axios設置Authorization)

import axios from 'axios'
import store from '../store'
import router from '../router'
 
//設置全局axios默認值
axios.defaults.timeout = 6000; //6000的超時驗證
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';
 
//建立一個axios實例
const instance = axios.create();
instance.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';
 
axios.interceptors.request.use = instance.interceptors.request.use;
 
//request攔截器
instance.interceptors.request.use(
    config => {
        //每次發送請求以前檢測都vuex存有token,那麼都要放在請求頭髮送給服務器
        if(store.state.token){
            config.headers.Authorization = `token ${store.state.token}`;
        }
        return config;
    },
    err => {
        return Promise.reject(err);
    }
);
//respone攔截器
instance.interceptors.response.use(
    response => {
        return response;
    },
    error => { //默認除了2XX以外的都是錯誤的,就會走這裏
        if(error.response){
            switch(error.response.status){
                case 401:
                    store.dispatch('UserLogout'); //多是token過時,清除它
                    router.replace({ //跳轉到登陸頁面
                        path: 'login',
                        query: { redirect: router.currentRoute.fullPath } // 將跳轉的路由path做爲參數,登陸成功後跳轉到該路由
                    });
            }
        }
        return Promise.reject(error.response);
    }
);
 
export default instance;
 而後在路由文件中
 //註冊全局鉤子用來攔截導航
router.beforeEach((to, from, next) => {
  //獲取store裏面的token
  let token = store.state.token;
  //判斷要去的路由有沒有requiresAuth
  if(to.meta.requiresAuth){
 
    if(token){
      next();
    }else{
      next({
        path: '/login',
        query: { redirect: to.fullPath }  // 將剛剛要去的路由path(卻無權限)做爲參數,方便登陸成功後直接跳轉到該路由
      });
    }
 
  }else{
    next();//若是無需token,那麼隨它去吧
  }
});

後端(node) 咱們封裝了一箇中間件 在須要驗證token的路由,加上這個中間件

router.get('/dosh',checkToken,User.dosh)
 
const jwt = require('jsonwebtoken');
 
一、使用jsonwebtoken 建立token
const jwt = require('jsonwebtoken');
 
//登陸時:覈對用戶名和密碼成功後,應用將用戶的id(圖中的user_id)做爲JWT Payload的一個屬性
module.exports = function(user_id){
    const token = jwt.sign({
        user_id: user_id
    }, 'sinner77', {
        expiresIn: '3600s' //過時時間設置爲60妙。那麼decode這個token的時候獲得的過時時間爲 : 建立token的時間 + 設置的值
    });
    return token;
};
二、對於前端的請求,校驗接口
//檢查token是否過時
module.exports = async ( ctx, next ) => {
    if(ctx.request.header['authorization']){
        let token = ctx.request.header['authorization'].split(' ')[1];
        //解碼token
        let decoded = jwt.decode(token, 'sinner77');
        //console.log(decoded);的輸出 :{ user_id: '123123123', iat: 1494405235, exp: 1494405235 }
        if(token && decoded.exp <= new Date()/1000){
            ctx.status = 401;
            ctx.body = {
                message: 'token過時'
            };
        }else{
            //若是權限沒問題,那麼交個下一個控制器處理
            return next();
        }
    }else{
        ctx.status = 401;
        ctx.body = {
            message: '沒有token'
        }
    }
};

移動端開發過程當中,相信不少人對於定位fixed都會遇到不少問題而頭疼,底部fixed輸入框被彈起,左邊slide有輸入框,fixed定位失效
,還有動態添加fixed定位,會有卡頓現象。因此,移動端下開發,儘可能少用fixed佈局。html

之前左邊是fixed 獲取焦點時,手機鍵盤會把fix失效
下面介紹解決這種現象方法
一、佈局
大概的佈局以下前端

<html>
    <head></head>
    <body>
          <div>須要定位的內容</div>
         <div class=".body">
                  這裏是主體內容
        </div>
        <div>須要定位的內容</div>
   </body>

</html>
//主要的樣式以下,其餘須要定位的內容能夠放到.body div外面vue

html {
 height: 100%;
  }

body {node

width: 100%;
height: 100%;
position: relative;
overflow: hidden;

}
.body{ios

width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
z-index: 0;
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
overflow-x: hidden;
overflow-y: scroll;
-webkit-overflow-scrolling: touch;

}
2.isscroll 的使用
這個滾動插件,官網有詳細介紹git

原文地址:blog
代碼託管github 歡迎star
https://github.com/yxl720/vue...github

相關文章
相關標籤/搜索