Browse Source

过期时清空所以cookie数据

1178232204@qq.com 3 years ago
parent
commit
31179ffb31
3 changed files with 79 additions and 68 deletions
  1. 1 0
      src/js/index.js
  2. 2 1
      src/js/login.js
  3. 76 67
      src/js/utils.js

+ 1 - 0
src/js/index.js

@@ -635,6 +635,7 @@ window.addEventListener('message', e => {
       sureBtn: false,
       message: data.data || '登录过期',
       callback: function () {
+        clearAllCookie()
         window.location.href = 'login.html'
       }
     });

+ 2 - 1
src/js/login.js

@@ -5,7 +5,7 @@ require("./modal.js")
 
 const { api } = require('./api.js')
 
-const { post, setCookie, delCookie, getUrlArgObject, toast } = require('../js/utils.js');
+const { post, setCookie, delCookie, getUrlArgObject, toast,clearAllCookie } = require('../js/utils.js');
 const hideLogo = getUrlArgObject("hideLg");
 const otherLogo = getUrlArgObject("adLg");
 let type;
@@ -14,6 +14,7 @@ $(function () {
   //清除需要跳转到登录页的标记
   localStorage.removeItem('toLogin');
   localStorage.removeItem('accessToken')
+  clearAllCookie()
   if (hideLogo || otherLogo) {
     $(".copy-right").text("");
   }

+ 76 - 67
src/js/utils.js

@@ -146,9 +146,9 @@ window.abortAjax = '';
 
 const post = function (url, data, opts) {
   const token = localStorage.getItem('accessToken');
-  if(localStorage.getItem('toLogin')==='1'&&token){
+  if (localStorage.getItem('toLogin') === '1' && token) {
     //如果前面已经被标记为登录失效,接下来的请求都不发送
-    return new Promise(()=>{});
+    return new Promise(() => { });
   }
   let isPlacefile = getCookie('isPlacefile') || 1;
   if (!token && window.location.href.indexOf('login') == -1) {
@@ -179,64 +179,64 @@ const post = function (url, data, opts) {
       cache: false,
       processData: false,
       data: (opts && opts.needqs) ? qs.stringify(data) : JSON.stringify(Object.assign({}, data, {
-        isPlacefile: (opts && opts.noPlacefile?undefined:isPlacefile)
+        isPlacefile: (opts && opts.noPlacefile ? undefined : isPlacefile)
       })),
       contentType: 'application/json; charset=UTF-8',
       beforeSend: function (xmlHttp) {
-        if(url.indexOf("sys/user/getHospitalMark")===-1){
-		  xmlHttp.setRequestHeader('If-Modified-Since', '0');
-		  xmlHttp.setRequestHeader('Cache-Control', 'no-cache');
-		  xmlHttp.setRequestHeader('Authorization', token ? 'Bearer ' + token : '');
+        if (url.indexOf("sys/user/getHospitalMark") === -1) {
+          xmlHttp.setRequestHeader('If-Modified-Since', '0');
+          xmlHttp.setRequestHeader('Cache-Control', 'no-cache');
+          xmlHttp.setRequestHeader('Authorization', token ? 'Bearer ' + token : '');
         }
       },
       success: function (resq) {
-        const doc = parent?parent.document:document;
+        const doc = parent ? parent.document : document;
         //console.log($(doc).find(".alertModal:visible"))
-		//失效后不点知道了直接刷新处理:跳回登录页
-		if(localStorage.getItem('toLogin')==='1'&&$(doc).find(".alertModal:visible").length===0){
-		  (parent||window).location.href='login.html';
-		}
-        let res=resq;
-        if(typeof resq ==='string'){
+        //失效后不点知道了直接刷新处理:跳回登录页
+        if (localStorage.getItem('toLogin') === '1' && $(doc).find(".alertModal:visible").length === 0) {
+          (parent || window).location.href = 'login.html';
+        }
+        let res = resq;
+        if (typeof resq === 'string') {
           res = JSON.parse(resq);
         }
         const code = res.code;
-		const isOut = code === '10020011' || code === '10020012' || code === '10020013';
-        if(isOut){
+        const isOut = code === '10020011' || code === '10020012' || code === '10020013';
+        if (isOut) {
           //登录信息失效处理
-		  unloginPop(res.msg);
-        }else{
-		  resolve({
-			data: res
-		  });
+          unloginPop(res.msg);
+        } else {
+          resolve({
+            data: res
+          });
         }
       },
       error: function (error) {
-          if (error.status === 403 || error.status === 401) {
-            setCookie('behosDateStart', '');
-            setCookie('behosDateEnd', '');
-            if (!token && window.location.href.indexOf('login') == -1) {
-            } else {
-              if($(".alertModal:visible").length){
-                return;
-              }
-              $.alerModal({
-                "message": "没有该权限"+error.status,
-                type: "tip",
-                time: '1000',
-                isFather: true,
-                fatherWrapper: $("#mainBox", parent.document)
-              });
+        if (error.status === 403 || error.status === 401) {
+          setCookie('behosDateStart', '');
+          setCookie('behosDateEnd', '');
+          if (!token && window.location.href.indexOf('login') == -1) {
+          } else {
+            if ($(".alertModal:visible").length) {
+              return;
             }
-          } else if (error.status === 500 || error.status === 501 || error.status === 404) {
             $.alerModal({
-              "message": '接口错误',
+              "message": "没有该权限" + error.status,
               type: "tip",
               time: '1000',
               isFather: true,
               fatherWrapper: $("#mainBox", parent.document)
             });
           }
+        } else if (error.status === 500 || error.status === 501 || error.status === 404) {
+          $.alerModal({
+            "message": '接口错误',
+            type: "tip",
+            time: '1000',
+            isFather: true,
+            fatherWrapper: $("#mainBox", parent.document)
+          });
+        }
         reject(error);
       },
     });
@@ -244,39 +244,40 @@ const post = function (url, data, opts) {
 };
 
 //登录失效弹窗处理
-function unloginPop(msg){
+function unloginPop(msg) {
   /*const isOut = code === '10020011' || code === '10020012' || code === '10020013';
   if(isOut){*/
-  const isQcScore = parent&&parent.location.href.indexOf("qcScore.html")===-1;
-    if(self != top&&isQcScore){//子窗口并且不是病例列表
-	  const dialogmessage = {
-		iframemessage: 1,
-		data: msg
-	  }
-	  console.log('子窗口')
-	  window.parent.postMessage(JSON.stringify(dialogmessage), '*')
-    }else{
-	  console.log('父窗口')
-	  $.alerModal({
-		gobackBtn: true,
-		title: "提示",
-		type: "modal",
-		width: '470',
-		time: 1000,
-		cancalText: '我知道了',
-		isFather: true,
-		fatherWrapper: $("#mainBox", parent.document),
-		sureBtn: false,
-		message: msg || '登录过期',
-		callback: function () {
-          (parent||window).location.href = 'login.html'
-		}
-	  });
-	  //失效后不点知道了直接刷新处理标记
-      localStorage.setItem('toLogin',1);
+  const isQcScore = parent && parent.location.href.indexOf("qcScore.html") === -1;
+  if (self != top && isQcScore) {//子窗口并且不是病例列表
+    const dialogmessage = {
+      iframemessage: 1,
+      data: msg
     }
+    console.log('子窗口')
+    window.parent.postMessage(JSON.stringify(dialogmessage), '*')
+  } else {
+    console.log('父窗口')
+    $.alerModal({
+      gobackBtn: true,
+      title: "提示",
+      type: "modal",
+      width: '470',
+      time: 1000,
+      cancalText: '我知道了',
+      isFather: true,
+      fatherWrapper: $("#mainBox", parent.document),
+      sureBtn: false,
+      message: msg || '登录过期',
+      callback: function () {
+        clearAllCookie()
+          (parent || window).location.href = 'login.html'
+      }
+    });
+    //失效后不点知道了直接刷新处理标记
+    localStorage.setItem('toLogin', 1);
+  }
   /*}else{
-	callback()
+  callback()
   }*/
 
 }
@@ -359,6 +360,13 @@ const delCookie = function (name) {
   let cval = getCookie(name);
   if (cval != null) document.cookie = name + '=' + cval + ';expires=' + exp.toGMTString();
 };
+function clearAllCookie() {
+  var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
+  if (keys) {
+    for (var i = keys.length; i--;)
+      document.cookie = keys[i] + '=0;expires=' + new Date(0).toUTCString()
+  }
+}
 
 //弹窗关闭事件
 $('.modal .close').click(function () {
@@ -723,6 +731,7 @@ module.exports = {
   setCookie,
   getCookie,
   delCookie,
+  clearAllCookie,
   getLocal,
   setLocal,
   setBoxHeight,