mjh 4 years ago
parent
commit
68f2c8c3ba
2 changed files with 41 additions and 2 deletions
  1. 38 0
      src/utils/cookie.js
  2. 3 2
      src/views/AuthPage/components/AuthCard.vue

+ 38 - 0
src/utils/cookie.js

@@ -0,0 +1,38 @@
+/**
+ * @description 保存cookie
+ * @param {Object} json 需要存储cookie的对象
+ * @param {Number} days 默认存储多少天
+ */
+export function setCookie(json, days) {
+  // 设置过期时间
+  let data = new Date(
+    new Date().getTime() + days * 24 * 60 * 60 * 1000
+  ).toUTCString();
+
+  for (const key in json) {
+    document.cookie = key + "=" + json[key] + "; expires=" + data;
+  }
+}
+
+/**
+ * @description 获取cookie
+ * @param {String} name 需要获取cookie的key
+ */
+export function getCookie(name) {
+  const arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
+  if (arr != null) {
+    return unescape(arr[2]);
+  } else {
+    return null;
+  }
+}
+
+/**
+ * @description 删除cookie
+ * @param {String} name 需要删除cookie的key
+ */
+export function clearCookie(name) {
+  let json = {};
+  json[name] = "";
+  setCookie(json, -1);
+}

+ 3 - 2
src/views/AuthPage/components/AuthCard.vue

@@ -113,6 +113,7 @@
 <script>
 import {Avatar, Cherry, Key, Lock, Message, Phone} from "@element-plus/icons-vue";
 import SlideVerifier from "@/views/AuthPage/components/SlideVerifier";
+import {setCookie} from "@/utils/cookie";
 
 export default {
   name: "AuthCard",
@@ -357,8 +358,8 @@ export default {
                         }
                       } else {
                         let p = new Promise(function (resolve, reject) {
-                          document.cookie = "redirect_uri=" + null;
-                          resolve("success");
+                          setCookie({redirect_uri: ""}, -1);
+                          resolve();
                         }).then(r => {
                           window.location.href = res.data.data.redirectUri + "?id_token=" + res.data.data.token;
                         });