提交 112efffb 编写于 作者: zhujiamin's avatar zhujiamin

登录设置cookie

上级 594b4509
......@@ -37,8 +37,9 @@
import { Vue, Component } from "vue-property-decorator";
import { Loading } from '@/ibiz-core/utils';
import { Environment } from '@/environments/environment';
import { ThirdPartyService } from '@ibiz-core'
import { ThirdPartyService } from '@ibiz-core';
import { DingTalkService } from '../../ibiz-core/third-party-service/DingTalkService';
import { Util } from '@/ibiz-core/utils';
@Component({
components: {},
i18n: {
......@@ -183,8 +184,19 @@ export default class Login extends Vue {
if (response && response.status === 200) {
this.isLoadding = false;
const data = response.data;
localStorage.setItem("token", data.token);
localStorage.setItem("user", JSON.stringify(data.user));
if (data && data.token) {
Util.setCookie('ibzuaa-token', data.token, 7, true);
localStorage.setItem("token", data.token);
}
if (data && data.user) {
localStorage.setItem('user', JSON.stringify(data.user));
}
if (data && data.user && data.user.orgid) {
Util.setCookie('srforgid', data.user.orgid, 7, true);
sessionStorage.setItem('tempOrgId', data.user.orgid);
}
Util.setCookie('srfsystemid', 'iBizPortal', 7, true);
// 跳转首页
const url: any = this.$route.query.redirect
? this.$route.query.redirect
: "*";
......
......@@ -91,6 +91,7 @@
import { Vue, Component, Prop, Provide, Emit, Watch, } from "vue-property-decorator";
import { settingConfig } from "./config";
import i18n from '@/locale'
import { Util } from '@/ibiz-core/utils';
@Component({
components: {},
})
......@@ -239,6 +240,10 @@ export default class AppSetting extends Vue {
if (localStorage.getItem("token")) {
localStorage.removeItem("token");
}
// 清除cookie
Util.clearCookie('ibzuaa-token',true);
Util.clearCookie('srforgid',true);
Util.clearCookie('srfsystemid',true);
this.$router.push({ name: "login" });
}
......
......@@ -358,4 +358,76 @@ export class Util {
return fmt;
}
/**
* 设置cookie
*
* @static
* @param {*} name 名称
* @param {*} value 值
* @param {*} day 过期天数
* @param {boolean} [isDomain=false] 是否设置在主域下
* @param {string} [path='/'] 默认归属路径
* @memberof Util
*/
public static setCookie(
name: string,
value: string,
day: number = 0,
isDomain: boolean = false,
path: string = '/'
): void {
let domain = '';
// 设置cookie到主域下
if (isDomain) {
// 是否为ip正则
const regExpr = /^(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)$/;
// 为ip时忽略
if (!regExpr.test(location.hostname)) {
const host = location.hostname;
if (host.indexOf('.') !== host.lastIndexOf('.')) {
domain = ';domain=' + host.substring(host.indexOf('.'), host.length);
}
}
}
if (day !== 0) {
//当设置的时间等于0时,不设置expires属性,cookie在浏览器关闭后删除
const expires = day * 24 * 60 * 60 * 1000;
const date = new Date(new Date().getTime() + expires);
document.cookie = `${name}=${escape(value)};path=${path};expires=${date.toUTCString()}${domain}`;
} else {
document.cookie = `${name}=${escape(value)};path=${path}${domain}`;
}
}
/**
* 清除cookie
*
* @static
* @param {string} cookieName
* @param {boolean} [isDomain] 是否在主域下
* @memberof Util
*/
public static clearCookie(cookieName: string, isDomain?: boolean) {
this.setCookie(cookieName, '', -1, isDomain);
}
/**
* 获取cookie
*
* @static
* @param {string} name
* @return {*} {*}
* @memberof Util
*/
public static getCookie(name: string): string {
const reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)');
const arr = document.cookie.match(reg);
if (arr && arr.length > 1) {
return unescape(arr[2]);
} else {
return '';
}
}
}
\ No newline at end of file
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册