feat: 新增退出登录接口,新增全局接口封装,修改登录接口内容

This commit is contained in:
奔跑的面条
2022-05-21 17:31:01 +08:00
parent 92e1ec05d2
commit b6143bc75e
15 changed files with 161 additions and 35 deletions
+10 -4
View File
@@ -1,11 +1,12 @@
import { useRoute } from 'vue-router'
import { ResultEnum } from '@/enums/httpEnum'
import { ResultEnum, RequestHttpHeaderEnum } from '@/enums/httpEnum'
import { ErrorPageNameMap, PageEnum } from '@/enums/pageEnum'
import { docPath, giteeSourceCodePath } from '@/settings/pathConst'
import { SystemStoreEnum, SystemStoreUserInfoEnum } from '@/store/modules/systemStore/systemStore.d'
import { StorageEnum } from '@/enums/storageEnum'
import { clearLocalStorage, getLocalStorage } from './storage'
import { clearLocalStorage, getLocalStorage, clearCookie } from './storage'
import router from '@/router'
import { logoutApi } from '@/api/path/system.api'
/**
* * 根据名字跳转路由
@@ -101,9 +102,14 @@ export const reloadRoutePage = () => {
}
/**
* * 退出
* * 退出登录
*/
export const logout = () => {
export const logout = async () => {
const res:any = await logoutApi()
if(res.code === ResultEnum.SUCCESS) {
window['$message'].success((`${window.$t('global.logout_success')}!`))
}
clearCookie(RequestHttpHeaderEnum.COOKIE)
clearLocalStorage(StorageEnum.GO_SYSTEM_STORE)
routerTurnByName(PageEnum.BASE_LOGIN_NAME)
}
+41 -3
View File
@@ -5,7 +5,7 @@
* @param v 键值(无需stringiiy
* @returns RemovableRef
*/
export const setLocalStorage = <T>(k: string, v: T) => {
export const setLocalStorage = <T>(k: string, v: T) => {
try {
window.localStorage.setItem(k, JSON.stringify(v))
} catch (error) {
@@ -18,7 +18,7 @@
* @param k 键名
* @returns any
*/
export const getLocalStorage = (k: string) => {
export const getLocalStorage = (k: string) => {
const item = window.localStorage.getItem(k)
try {
return item ? JSON.parse(item) : item
@@ -31,7 +31,7 @@
* * 清除本地会话数据
* @param name
*/
export const clearLocalStorage = (name: string) => {
export const clearLocalStorage = (name: string) => {
window.localStorage.removeItem(name)
}
@@ -68,4 +68,42 @@ export const getSessionStorage: (k: string) => any = (k: string) => {
*/
export const clearSessioStorage = (name: string) => {
window.sessionStorage.removeItem(name)
}
/**
* * 设置 cookie
* @param name 键名
* @param cvalue 键值
* @param exdays 过期时间
*/
export const setCookie = (name: string, cvalue: string, exdays: number) => {
const d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
const expires = "expires=" + d.toUTCString();
document.cookie = name + "=" + cvalue + "; " + expires;
}
/**
* * 获取 cookie
* @param cname 键名
* @returns string
*/
export const getCookie = (cname: string) => {
const name = cname + "=";
const ca = document.cookie.split(';');
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1);
if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
}
return "";
}
/**
* * 清除 cookie
* @param name 键名
* @returns string
*/
export const clearCookie = (name: string) => {
setCookie(name, "", -1);
}