提交 d7cfa4ac 编写于 作者: laizhilong's avatar laizhilong

修改登出后自动填充表单的逻辑

上级 9f9af46b
...@@ -40,7 +40,8 @@ ...@@ -40,7 +40,8 @@
localStorage.setItem('user', JSON.stringify(data.user)); localStorage.setItem('user', JSON.stringify(data.user));
} }
if (data.ibzuser) { if (data.ibzuser) {
var ibzuser = JSON.stringify(data.ibzuser); var ibzuser = data.ibzuser;
localStorage.setItem('ibzuser',JSON.stringify(ibzuser));
// 设置cookie,保存账号密码7天 // 设置cookie,保存账号密码7天
setCookie(ibzuser.loginname, ibzuser.password, 7); setCookie(ibzuser.loginname, ibzuser.password, 7);
// 跳转首页 // 跳转首页
...@@ -58,7 +59,6 @@ ...@@ -58,7 +59,6 @@
} }
}); });
} else { } else {
alert("钉钉授权登录失败!");
// 回到登录页 // 回到登录页
window.location.href = "../"; window.location.href = "../";
} }
...@@ -76,7 +76,7 @@ ...@@ -76,7 +76,7 @@
// 设置cookie // 设置cookie
function setCookie(loginname, password, exdays) { function setCookie(loginname, password, exdays) {
// 获取时间 // 获取时间
let exdate = new Date(); var exdate = new Date();
// 保存的天数 // 保存的天数
exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays); exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays);
// 字符串拼接cookie // 字符串拼接cookie
......
...@@ -40,7 +40,8 @@ ...@@ -40,7 +40,8 @@
localStorage.setItem('user', JSON.stringify(data.user)); localStorage.setItem('user', JSON.stringify(data.user));
} }
if (data.ibzuser) { if (data.ibzuser) {
var ibzuser = JSON.stringify(data.ibzuser); var ibzuser = data.ibzuser;
localStorage.setItem('ibzuser',JSON.stringify(ibzuser));
// 设置cookie,保存账号密码7天 // 设置cookie,保存账号密码7天
setCookie(ibzuser.loginname, ibzuser.password, 7); setCookie(ibzuser.loginname, ibzuser.password, 7);
// 跳转首页 // 跳转首页
...@@ -58,7 +59,6 @@ ...@@ -58,7 +59,6 @@
} }
}); });
} else { } else {
alert("微信授权登录失败!");
// 回到登录页 // 回到登录页
window.location.href = "../"; window.location.href = "../";
} }
...@@ -76,7 +76,7 @@ ...@@ -76,7 +76,7 @@
// 设置cookie // 设置cookie
function setCookie(loginname, password, exdays) { function setCookie(loginname, password, exdays) {
// 获取时间 // 获取时间
let exdate = new Date(); var exdate = new Date();
// 保存的天数 // 保存的天数
exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays); exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays);
// 字符串拼接cookie // 字符串拼接cookie
......
...@@ -25,7 +25,6 @@ ...@@ -25,7 +25,6 @@
data: JSON.stringify(opt), data: JSON.stringify(opt),
contentType: 'application/json', contentType: 'application/json',
success: function (data) { success: function (data) {
// console.log(JSON.stringify(data));
if (data) { if (data) {
if (data.openid) { if (data.openid) {
localStorage.setItem('openid', data.openid); localStorage.setItem('openid', data.openid);
...@@ -40,7 +39,8 @@ ...@@ -40,7 +39,8 @@
localStorage.setItem('user', JSON.stringify(data.user)); localStorage.setItem('user', JSON.stringify(data.user));
} }
if (data.ibzuser) { if (data.ibzuser) {
var ibzuser = JSON.stringify(data.ibzuser); var ibzuser = data.ibzuser;
localStorage.setItem('ibzuser',JSON.stringify(ibzuser));
// 设置cookie,保存账号密码7天 // 设置cookie,保存账号密码7天
setCookie(ibzuser.loginname, ibzuser.password, 7); setCookie(ibzuser.loginname, ibzuser.password, 7);
// 跳转首页 // 跳转首页
...@@ -58,7 +58,6 @@ ...@@ -58,7 +58,6 @@
} }
}); });
} else { } else {
alert("微信授权登录失败!");
// 回到登录页 // 回到登录页
window.location.href = "../"; window.location.href = "../";
} }
...@@ -76,7 +75,7 @@ ...@@ -76,7 +75,7 @@
// 设置cookie // 设置cookie
function setCookie(loginname, password, exdays) { function setCookie(loginname, password, exdays) {
// 获取时间 // 获取时间
let exdate = new Date(); var exdate = new Date();
// 保存的天数 // 保存的天数
exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays); exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays);
// 字符串拼接cookie // 字符串拼接cookie
......
...@@ -149,7 +149,6 @@ ...@@ -149,7 +149,6 @@
if (!this.state) { if (!this.state) {
this.state = this.getUrlParam('state'); this.state = this.getUrlParam('state');
} }
// alert("code:" + this.code + "\nstate:" + this.state);
// 获取失败,回到登录页 // 获取失败,回到登录页
if (!this.code || !this.state) { if (!this.code || !this.state) {
this.$message.error("钉钉授权,获取code失败"); this.$message.error("钉钉授权,获取code失败");
...@@ -201,7 +200,6 @@ ...@@ -201,7 +200,6 @@
/** /**
* 注册处理 * 注册处理
*/ */
...@@ -244,7 +242,8 @@ ...@@ -244,7 +242,8 @@
localStorage.setItem('user', JSON.stringify(data.user)); localStorage.setItem('user', JSON.stringify(data.user));
} }
if (data.ibzuser) { if (data.ibzuser) {
let ibzuser: any = JSON.stringify(data.ibzuser); let ibzuser: any = data.ibzuser;
localStorage.setItem('ibzuser',JSON.stringify(ibzuser));
// 设置cookie,保存账号密码7天 // 设置cookie,保存账号密码7天
this.setCookie(ibzuser.loginname, ibzuser.password, 7); this.setCookie(ibzuser.loginname, ibzuser.password, 7);
// 跳转首页 // 跳转首页
...@@ -268,24 +267,29 @@ ...@@ -268,24 +267,29 @@
closable: true closable: true
}); });
} }
// 返回登录页
this.goLogin();
}); });
} }
/** /**
* 设置cookie,保存账号密码 * 设置cookie
* @param loginname
* @param password
*/ */
public setCookie(loginname: any, password: any, exdays: any) { public setCookie(name: any, value: any, day: any) {
// 获取时间 if (day !== 0) { //当设置的时间等于0时,不设置expires属性,cookie在浏览器关闭后删除
let exdate = new Date(); var curDate = new Date();
// 保存的天数 var curTamp = curDate.getTime();
exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays); var curWeeHours = new Date(curDate.toLocaleDateString()).getTime() - 1;
// 字符串拼接cookie var passedTamp = curTamp - curWeeHours;
window.document.cookie = "loginname" + "=" + loginname + ";path=/;expires=" + exdate.toUTCString(); var leftTamp = 24 * 60 * 60 * 1000 - passedTamp;
window.document.cookie = "password" + "=" + password + ";path=/;expires=" + exdate.toUTCString(); var leftTime = new Date();
leftTime.setTime(leftTamp + curTamp);
document.cookie = name + "=" + escape(value) + ";expires=" + leftTime.toUTCString();
} else {
document.cookie = name + "=" + escape(value);
}
} }
......
<template> <template>
<div class='login'> <div class='login'>
<img src="/assets/img/background.png"/> <img src="/assets/img/background.png" draggable="false"/>
<div class='login-con'> <div class='login-con'>
<card :bordered="false"> <card :bordered="false">
...@@ -40,7 +40,6 @@ ...@@ -40,7 +40,6 @@
class="login_reset">重置 class="login_reset">重置
</i-button> </i-button>
</form-item> </form-item>
<form-item> <form-item>
<a @click="goRegister" class="goRegister" draggable="false">没有账号,现在注册一个</a> <a @click="goRegister" class="goRegister" draggable="false">没有账号,现在注册一个</a>
</form-item> </form-item>
...@@ -50,7 +49,7 @@ ...@@ -50,7 +49,7 @@
<span class="form_tipinfo">其他登录方式</span> <span class="form_tipinfo">其他登录方式</span>
</div> </div>
<div style="text-align: center"> <div style="text-align: center">
<div class="sign-btn" @click="tencentHandleClick('tencent')"> <div class="sign-btn" @click="qqHandleClick('qq')">
<img src="/assets/img/QQ.svg" class="qq-svg-container" draggable="false"> <img src="/assets/img/QQ.svg" class="qq-svg-container" draggable="false">
</div> </div>
<div class="sign-btn" @click="wechatHandleClick('wechat')"> <div class="sign-btn" @click="wechatHandleClick('wechat')">
...@@ -63,15 +62,12 @@ ...@@ -63,15 +62,12 @@
</form-item> </form-item>
</i-form> </i-form>
<p class='login-tip'>
{{this.loginTip}}
<!--{{this.$t('components.login.tip')}}-->
</p>
</div> </div>
</card> </card>
<div class="log_footer"> <div class="log_footer">
<div class="copyright"> <div class="copyright">
<a href="https://www.ibizlab.cn/" target="_blank">{{appTitle}} is based on ibizlab .</a> <a href="https://www.ibizlab.cn/" target="_blank" draggable="false">{{appTitle}} is based on ibizlab
.</a>
</div> </div>
</div> </div>
</div> </div>
...@@ -79,391 +75,387 @@ ...@@ -79,391 +75,387 @@
</template> </template>
<script lang="ts"> <script lang="ts">
import {Vue, Component, Watch} from 'vue-property-decorator'; import {Vue, Component, Watch} from 'vue-property-decorator';
import {Environment} from '@/environments/environment'; import {Environment} from '@/environments/environment';
//import Divider from "ibiz-vue-lib/lib/ibiz-vue-lib.common";
@Component({
@Component({ components: {}
components: {} })
}) export default class Login extends Vue {
export default class Login extends Vue {
/**
/** * 表单对象
* 表单对象 *
* * @type {*}
* @type {*} * @memberof Login
* @memberof Login */
*/ public form: any = {loginname: 'guest', password: '123456'};
public form: any = {loginname: 'ibzadmin', password: '123456'};
/** /**
* 登录提示语 * 应用名称
*/ *
public loginTip: any = ""; * @type {string}
* @memberof Login
/** */
* 按钮可点击 public appTitle: string = Environment.AppTitle;
*/
public canClick: any = true; /**
* 值规则
/** *
* 应用名称 * @type {*}
* * @memberof Login
* @type {string} */
* @memberof Login public rules = {};
*/
public appTitle: string = Environment.AppTitle; /**
* 设置值规则
/** *
* 值规则 * @memberof Login
* */
* @type {*} public setRules() {
* @memberof Login this.rules = {
*/ loginname: [
public rules = {}; {required: true, message: this.$t('components.login.loginname.message'), trigger: 'change'},
],
/** password: [
* 设置值规则 {required: true, message: this.$t('components.login.password.message'), trigger: 'change'},
* ],
* @memberof Login }
*/ };
public setRules() {
this.rules = { /**
loginname: [ * 生命周期Create
{required: true, message: this.$t('components.login.loginname.message'), trigger: 'change'}, *
], * @memberof Login
password: [ */
{required: true, message: this.$t('components.login.password.message'), trigger: 'change'}, public created() {
], this.setRules();
} }
};
/**
* 生命周期Create
*
* @memberof Login
*/
public created() {
this.setRules();
}
public mounted() { public mounted() {
if (this.getCookie("loginname") && this.getCookie("loginname") !== 'undefined') { if (this.getCookie("loginname") && this.getCookie("loginname") !== 'undefined') {
this.form.loginname = this.getCookie("loginname"); this.form.loginname = this.getCookie("loginname");
} }
if (this.getCookie("password") && this.getCookie("password") !== 'undefined') { if (this.getCookie("password") && this.getCookie("password") !== 'undefined') {
this.form.password = this.getCookie("password"); this.form.password = this.getCookie("password");
}
if (localStorage.getItem("ibzuser")) {
const ibzuser:any = localStorage.getItem("ibzuser");
if (ibzuser) {
const ibzuserObj:any = JSON.parse(ibzuser);
this.form.loginname = ibzuserObj.loginname;
this.form.password = ibzuserObj.password;
}
}
} }
}
/** /**
* 监听语言变化 * 监听语言变化
* *
* @memberof Login * @memberof Login
*/ */
@Watch('$i18n.locale') @Watch('$i18n.locale')
onLocaleChange(newval: any, val: any) { onLocaleChange(newval: any, val: any) {
this.setRules(); this.setRules();
}
/**
* 登陆处理
*
* @memberof Login
*/
public handleSubmit(): void {
// 清除cookie
let leftTime = new Date();
leftTime.setTime(leftTime.getSeconds() - 1000);
document.cookie = "ibzuaa-token=;expires=" + leftTime.toUTCString();
const form: any = this.$refs.loginForm;
let validatestate: boolean = true;
form.validate((valid: boolean) => {
validatestate = valid ? true : false;
});
if (!validatestate) {
return;
} }
const loginname: any = this.form.loginname; /**
const password: any = this.form.password; * 登陆处理
const post: Promise<any> = this.$http.post('v7/login', this.form, true); *
post.then((response: any) => { * @memberof Login
if (response && response.status === 200) { */
const data = response.data; public handleSubmit(): void {
if (data && data.token) { const form: any = this.$refs.loginForm;
localStorage.setItem('token', data.token); let validatestate: boolean = true;
this.setCookie('ibzuaa-token',data.token,0); form.validate((valid: boolean) => {
} validatestate = valid ? true : false;
// 设置cookie,保存账号密码7天 });
this.setCookie("loginname",loginname, 7); if (!validatestate) {
// 跳转首页 return;
const url: any = this.$route.query.redirect ? this.$route.query.redirect : '*';
this.$router.push({path: url});
} }
}).catch((error: any) => {
// 登录提示
const data = error.data;
if (data && data.message) {
this.loginTip = data.message;
this.$Message.error({
content: "登录失败," + data.message,
duration: 5,
closable: true
});
} else {
this.$Message.error({
content: "登录失败",
duration: 5,
closable: true
});
}
});
} const loginname: any = this.form.loginname;
const password: any = this.form.password;
const post: Promise<any> = this.$http.post('v7/login', this.form, true);
post.then((response: any) => {
if (response && response.status === 200) {
const data = response.data;
if (data && data.token) {
localStorage.setItem('token', data.token);
this.setCookie('ibzuaa-token', data.token, 0);
}
if (data && data.user) {
localStorage.setItem('user', JSON.stringify(data.user));
}
// 设置cookie,保存账号密码7天
this.setCookie("loginname", loginname, 7);
// 跳转首页
const url: any = this.$route.query.redirect ? this.$route.query.redirect : '*';
this.$router.push({path: url});
}
}).catch((error: any) => {
// 登录提示
const data = error.data;
if (data && data.detail) {
this.$Message.error({
content: "登录失败," + data.detail,
duration: 5,
closable: true
});
} else {
this.$Message.error({
content: "登录失败",
duration: 5,
closable: true
});
}
});
/** }
* 重置页面
*
* @memberof Login
*/
public goReset(): void {
const _this = this;
_this.form={loginname: 'ibzadmin', password: '123456'}
}
/** /**
* 跳转注册页面 * 重置页面
*/ *
public goRegister(): void { * @memberof Login
const _this = this; */
_this.$router.push('/register'); public goReset(): void {
} const _this = this;
_this.form={loginname: 'ibzadmin', password: '123456'}
}
/**
* 跳转注册页面
*/
public goRegister(): void {
const _this = this;
_this.$router.push('/register');
}
/** /**
* 设置cookie * 设置cookie
* */
* @memberof Login public setCookie(name: any, value: any, day: any) {
*/
public setCookie(name: any, value: any, day: any) {
if (day !== 0) { //当设置的时间等于0时,不设置expires属性,cookie在浏览器关闭后删除 if (day !== 0) { //当设置的时间等于0时,不设置expires属性,cookie在浏览器关闭后删除
let curDate = new Date(); var curDate = new Date();
let curTamp = curDate.getTime(); var curTamp = curDate.getTime();
let curWeeHours = new Date(curDate.toLocaleDateString()).getTime() - 1; var curWeeHours = new Date(curDate.toLocaleDateString()).getTime() - 1;
let passedTamp = curTamp - curWeeHours; var passedTamp = curTamp - curWeeHours;
let leftTamp = 24 * 60 * 60 * 1000 - passedTamp; var leftTamp = 24 * 60 * 60 * 1000 - passedTamp;
let leftTime = new Date(); var leftTime = new Date();
leftTime.setTime(leftTamp + curTamp); leftTime.setTime(leftTamp + curTamp);
document.cookie = name + "=" + escape(value) + ";expires=" + leftTime.toUTCString(); document.cookie = name + "=" + escape(value) + ";expires=" + leftTime.toUTCString();
} else { } else {
document.cookie = name + "=" + escape(value); document.cookie = name + "=" + escape(value);
} }
} }
/** /**
* 获取cookie * 获取cookie
* */
* @memberof Login public getCookie(name: any): any {
*/ var arr;
public getCookie(name: any): any { var reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
let arr;
let reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
if (arr = document.cookie.match(reg)) if (arr = document.cookie.match(reg))
return unescape(arr[2]); return unescape(arr[2]);
else else
return null; return null;
} }
/** /**
* qq授权登录 * qq授权登录
* @param thirdpart * @param thirdpart
*/ */
public tencentHandleClick(thirdpart: any) { public qqHandleClick(thirdpart: any) {
// 截取地址,拼接需要部分组成新地址 // 截取地址,拼接需要部分组成新地址
const baseUrl = this.getNeedLocation(); const baseUrl = this.getNeedLocation();
// 从后台获取qq互联创建的网站应用appid // 从后台获取qq互联创建的网站应用appid
const get: Promise<any> = this.$http.get('/uaa/getQQAppId'); const get: Promise<any> = this.$http.get('/uaa/getQQAppId');
get.then((response: any) => { get.then((response: any) => {
if (response && response.status === 200) { if (response && response.status === 200) {
const data = response.data; const data = response.data;
if (data && data.appid) { if (data && data.appid) {
// 1.qq互联创建的网站应用appid // 1.qq互联创建的网站应用appid
const client_id = data.appid; const client_id = data.appid;
// 2.回调地址,即授权登录成功后跳转的地址,需要UrlEncode转码 // 2.回调地址,即授权登录成功后跳转的地址,需要UrlEncode转码
const redirect_uri = baseUrl + 'assets/qqRedirect.html'; const redirect_uri = baseUrl + 'assets/qqRedirect.html';
const redirect_uri_encode = decodeURIComponent(redirect_uri); const redirect_uri_encode = decodeURIComponent(redirect_uri);
// 3.随机生成一段字符串,防止CSRF攻击的 // 3.随机生成一段字符串,防止CSRF攻击的
const state = Math.random().toString(36).substr(2); const state = Math.random().toString(36).substr(2);
// 4.qq授权登录地址 // 4.qq授权登录地址
const url = 'https://graph.qq.com/oauth2.0/authorize?response_type=code' const url = 'https://graph.qq.com/oauth2.0/authorize?response_type=code'
+ '&client_id=' + client_id + '&client_id=' + client_id
+ '&redirect_uri=' + redirect_uri_encode + '&redirect_uri=' + redirect_uri_encode
+ "&scope=get_user_info" + "&scope=get_user_info"
+ "&state=" + state; + "&state=" + state;
// 5.跳转qq授权 // 5.跳转qq授权
window.location.href = url; window.location.href = url;
}else { }else {
this.$Message.error({
content: "获取网站应用appid失败," + data.detail,
duration: 5,
closable: true
});
}
}
}).catch((error: any) => {
const data = error.data;
if (data && data.detail) {
this.$Message.error({ this.$Message.error({
content: "获取网站应用appid失败," + data.detail, content: "获取网站应用appid失败," + data.detail,
duration: 5, duration: 5,
closable: true closable: true
}); });
} else {
this.$Message.error({
content: "获取网站应用appid失败",
duration: 5,
closable: true
});
} }
} });
}).catch((error: any) => { }
const data = error.data;
if (data && data.detail) {
this.$Message.error({
content: "获取网站应用appid失败," + data.detail,
duration: 5,
closable: true
});
} else {
this.$Message.error({
content: "获取网站应用appid失败",
duration: 5,
closable: true
});
}
});
}
/**
* 微信授权登录 /**
* @param thirddpart * 微信授权登录
*/ * @param thirdpart
public wechatHandleClick(thirddpart: any) { */
// 从后台获取微信开放平台提供的appid public wechatHandleClick(thirdpart: any) {
const get: Promise<any> = this.$http.get('/uaa/getWechatAppId'); // 从后台获取微信开放平台提供的appid
get.then((response: any) => { const get: Promise<any> = this.$http.get('/uaa/getWechatAppId');
if (response && response.status === 200) { get.then((response: any) => {
const data = response.data; if (response && response.status === 200) {
if (data && data.appid) { const data = response.data;
// 截取地址,拼接需要部分组成新地址 if (data && data.appid) {
const baseUrl = this.getNeedLocation(); // 截取地址,拼接需要部分组成新地址
const baseUrl = this.getNeedLocation();
// 1.微信开放平台提供的appId
const appId = data.appid; // 1.微信开放平台提供的appId
// 2.微信扫码后回调地址,需要UrlEncode转码 const appId = data.appid;
const redirect_uri = baseUrl + 'assets/weixinRedirect.html'; // 2.微信扫码后回调地址,需要UrlEncode转码
const redirect_uri_encode = encodeURIComponent(redirect_uri); const redirect_uri = baseUrl + 'assets/weixinRedirect.html';
// 3.微信扫码url const redirect_uri_encode = encodeURIComponent(redirect_uri);
const url = 'https://open.weixin.qq.com/connect/qrconnect?response_type=code' // 3.微信扫码url
+ '&appid=' + appId const url = 'https://open.weixin.qq.com/connect/qrconnect?response_type=code'
+ '&redirect_uri=' + redirect_uri_encode + '&appid=' + appId
+ '&scope=snsapi_login' + '&redirect_uri=' + redirect_uri_encode
+ '&state=STATE'; + '&scope=snsapi_login'
// 4.跳转微信扫码 + '&state=STATE';
window.location.href = url; // 4.跳转微信扫码
}else { window.location.href = url;
}else {
this.$Message.error({
content: "获取网站应用appid失败," + data.detail,
duration: 5,
closable: true
});
}
}
}).catch((error: any) => {
const data = error.data;
if (data && data.detail) {
this.$Message.error({ this.$Message.error({
content: "获取网站应用appid失败," + data.detail, content: "获取网站应用appid失败," + data.detail,
duration: 5, duration: 5,
closable: true closable: true
}); });
} else {
this.$Message.error({
content: "获取网站应用appid失败",
duration: 5,
closable: true
});
} }
} });
}).catch((error: any) => {
const data = error.data;
if (data && data.detail) {
this.$Message.error({
content: "获取网站应用appid失败," + data.detail,
duration: 5,
closable: true
});
} else {
this.$Message.error({
content: "获取网站应用appid失败",
duration: 5,
closable: true
});
}
});
}
/** }
* 钉钉授权登录
* @param thirdpart
*/ /**
public dingtalkHandleClick(thirdpart: any) { * 钉钉授权登录
// 从后台获取钉钉开放平台提供的appid * @param thirdpart
const get: Promise<any> = this.$http.get('/uaa/getDingtalkAppId'); */
get.then((response: any) => { public dingtalkHandleClick(thirdpart: any) {
if (response && response.status === 200) { // 从后台获取钉钉开放平台提供的appid
const data = response.data; const get: Promise<any> = this.$http.get('/uaa/getDingtalkAppId');
if (data && data.appid) { get.then((response: any) => {
// 截取地址,拼接需要部分组成新地址 if (response && response.status === 200) {
const baseUrl = this.getNeedLocation(); const data = response.data;
if (data && data.appid) {
// 1.钉钉开放平台提供的appId // 截取地址,拼接需要部分组成新地址
const appId = data.appid; const baseUrl = this.getNeedLocation();
// 2.钉钉扫码后回调地址,需要UrlEncode转码
const redirect_uri = baseUrl + 'assets/dingdingRedirect.html'; // 1.钉钉开放平台提供的appId
const redirect_uri_encode = encodeURIComponent(redirect_uri); const appId = data.appid;
// 3.钉钉扫码url // 2.钉钉扫码后回调地址,需要UrlEncode转码
const url = 'https://oapi.dingtalk.com/connect/qrconnect?response_type=code' const redirect_uri = baseUrl + 'assets/dingdingRedirect.html';
+ '&appid=' + appId const redirect_uri_encode = encodeURIComponent(redirect_uri);
+ '&redirect_uri=' + redirect_uri_encode // 3.钉钉扫码url
+ '&scope=snsapi_login' const url = 'https://oapi.dingtalk.com/connect/qrconnect?response_type=code'
+ '&state=STATE'; + '&appid=' + appId
+ '&redirect_uri=' + redirect_uri_encode
// 4.跳转钉钉扫码 + '&scope=snsapi_login'
window.location.href = url; + '&state=STATE';
}else {
// 4.跳转钉钉扫码
window.location.href = url;
}else {
this.$Message.error({
content: "获取网站应用appid失败," + data.detail,
duration: 5,
closable: true
});
}
}
}).catch((error: any) => {
const data = error.data;
if (data && data.detail) {
this.$Message.error({ this.$Message.error({
content: "获取网站应用appid失败," + data.detail, content: "获取网站应用appid失败," + data.detail,
duration: 5, duration: 5,
closable: true closable: true
}); });
} else {
this.$Message.error({
content: "获取网站应用appid失败",
duration: 5,
closable: true
});
} }
} });
}).catch((error: any) => {
const data = error.data;
if (data && data.detail) {
this.$Message.error({
content: "获取网站应用appid失败," + data.detail,
duration: 5,
closable: true
});
} else {
this.$Message.error({
content: "获取网站应用appid失败",
duration: 5,
closable: true
});
}
});
} }
/** /**
* 获取需要的location部分 * 获取需要的location部分
*/ */
public getNeedLocation() { public getNeedLocation() {
// 截取地址,拼接需要部分组成新地址 // 截取地址,拼接需要部分组成新地址
const scheme = window.location.protocol; const scheme = window.location.protocol;
const host = window.location.host; const host = window.location.host;
let baseUrl: any; let baseUrl: any;
baseUrl = scheme + "//" + host; baseUrl = scheme + "//" + host;
const port = window.location.port; const port = window.location.port;
if (port) { if (port) {
if (port == '80' || port == '443') { if (port == '80' || port == '443') {
baseUrl += "/"; baseUrl += "/";
} else {
baseUrl += ":" + port + "/";
}
} else { } else {
baseUrl += ":" + port + "/"; baseUrl += "/";
} }
} else { // console.log(baseUrl);
baseUrl += "/";
return baseUrl;
} }
// console.log(baseUrl);
return baseUrl;
}
} }
</script> </script>
<style lang='less'> <style lang='less'>
......
...@@ -144,7 +144,7 @@ ...@@ -144,7 +144,7 @@
* 挂载 * 挂载
*/ */
public mounted() { public mounted() {
let _this = this;
// 从url获取授权code和state // 从url获取授权code和state
this.code = this.$route.query.code; this.code = this.$route.query.code;
if (!this.code) { if (!this.code) {
...@@ -156,7 +156,7 @@ ...@@ -156,7 +156,7 @@
} }
// 获取失败,回到登录页 // 获取失败,回到登录页
if (!this.code || !this.state) { if (!this.code || !this.state) {
this.$message.error("微信授权,获取code失败"); this.$message.error("QQ授权,获取code失败");
this.goLogin(); this.goLogin();
} }
else { else {
...@@ -168,6 +168,7 @@ ...@@ -168,6 +168,7 @@
this.nickname = localStorage.getItem("nickname"); this.nickname = localStorage.getItem("nickname");
} }
} }
} }
...@@ -247,7 +248,8 @@ ...@@ -247,7 +248,8 @@
localStorage.setItem('user', JSON.stringify(data.user)); localStorage.setItem('user', JSON.stringify(data.user));
} }
if (data.ibzuser) { if (data.ibzuser) {
let ibzuser: any = JSON.stringify(data.ibzuser); let ibzuser: any = data.ibzuser;
localStorage.setItem('ibzuser',JSON.stringify(ibzuser));
// 设置cookie,保存账号密码7天 // 设置cookie,保存账号密码7天
this.setCookie(ibzuser.loginname, ibzuser.password, 7); this.setCookie(ibzuser.loginname, ibzuser.password, 7);
// 跳转首页 // 跳转首页
...@@ -271,81 +273,29 @@ ...@@ -271,81 +273,29 @@
closable: true closable: true
}); });
} }
// 返回登录页
this.goLogin();
}); });
} }
/** /**
* 自动登录倒计时 * 设置cookie
*/ */
public countDown(totalTime: any): void { public setCookie(name: any, value: any, day: any) {
if (!this.canClick) return; if (day !== 0) { //当设置的时间等于0时,不设置expires属性,cookie在浏览器关闭后删除
this.canClick = false; var curDate = new Date();
this.BtnContent = totalTime + 's后自动登录'; var curTamp = curDate.getTime();
// 设置定时器 var curWeeHours = new Date(curDate.toLocaleDateString()).getTime() - 1;
let clock = window.setInterval(() => { var passedTamp = curTamp - curWeeHours;
// 秒数-1 var leftTamp = 24 * 60 * 60 * 1000 - passedTamp;
totalTime--; var leftTime = new Date();
this.BtnContent = totalTime + 's后自动登录'; leftTime.setTime(leftTamp + curTamp);
if (totalTime < 0) { document.cookie = name + "=" + escape(value) + ";expires=" + leftTime.toUTCString();
// 清除定时器 } else {
window.clearInterval(clock); document.cookie = name + "=" + escape(value);
// 登录请求 }
const loginname: any = this.form.loginname;
const password: any = this.form.password;
const post: Promise<any> = this.$http.post('v7/login', this.form, true);
post.then((response: any) => {
if (response && response.status === 200) {
const data = response.data;
if (data && data.token) {
localStorage.setItem('token', data.token);
}
if (data && data.user) {
localStorage.setItem('user', JSON.stringify(data.user));
}
// 设置cookie,保存账号密码7天
this.setCookie(loginname, password, 7);
// 跳转首页
const url: any = this.$route.query.redirect ? this.$route.query.redirect : '*';
this.$router.push({path: url});
}
}).catch((error: any) => {
const data = error.data;
if (data && data.detail) {
this.$Message.error({
content: "登录失败," + data.detail,
duration: 3,
closable: true
});
} else {
this.$Message.error({
content: "登录失败",
duration: 3,
closable: true
});
}
});
}
}, 1000)
}
/**
* 设置cookie,保存账号密码
* @param loginname
* @param password
*/
public setCookie(loginname: any, password: any, exdays: any) {
// 获取时间
let exdate = new Date();
// 保存的天数
exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays);
// 字符串拼接cookie
window.document.cookie = "loginname" + "=" + loginname + ";path=/;expires=" + exdate.toUTCString();
window.document.cookie = "password" + "=" + password + ";path=/;expires=" + exdate.toUTCString();
} }
} }
</script> </script>
......
...@@ -87,7 +87,7 @@ ...@@ -87,7 +87,7 @@
public state: any; public state: any;
/** /**
* 钉钉用户身份的唯一标识 * 微信用户身份的唯一标识
*/ */
public openid: any; public openid: any;
...@@ -244,7 +244,8 @@ ...@@ -244,7 +244,8 @@
localStorage.setItem('user', JSON.stringify(data.user)); localStorage.setItem('user', JSON.stringify(data.user));
} }
if (data.ibzuser) { if (data.ibzuser) {
let ibzuser: any = JSON.stringify(data.ibzuser); let ibzuser: any = data.ibzuser;
localStorage.setItem('ibzuser',JSON.stringify(ibzuser));
// 设置cookie,保存账号密码7天 // 设置cookie,保存账号密码7天
this.setCookie(ibzuser.loginname, ibzuser.password, 7); this.setCookie(ibzuser.loginname, ibzuser.password, 7);
// 跳转首页 // 跳转首页
...@@ -268,23 +269,27 @@ ...@@ -268,23 +269,27 @@
closable: true closable: true
}); });
} }
// 返回登录页
this.goLogin();
}); });
} }
/** /**
* 设置cookie,保存账号密码 * 设置cookie
* @param loginname
* @param password
*/ */
public setCookie(loginname: any, password: any, exdays: any) { public setCookie(name: any, value: any, day: any) {
// 获取时间 if (day !== 0) { //当设置的时间等于0时,不设置expires属性,cookie在浏览器关闭后删除
let exdate = new Date(); var curDate = new Date();
// 保存的天数 var curTamp = curDate.getTime();
exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays); var curWeeHours = new Date(curDate.toLocaleDateString()).getTime() - 1;
// 字符串拼接cookie var passedTamp = curTamp - curWeeHours;
window.document.cookie = "loginname" + "=" + loginname + ";path=/;expires=" + exdate.toUTCString(); var leftTamp = 24 * 60 * 60 * 1000 - passedTamp;
window.document.cookie = "password" + "=" + password + ";path=/;expires=" + exdate.toUTCString(); var leftTime = new Date();
leftTime.setTime(leftTamp + curTamp);
document.cookie = name + "=" + escape(value) + ";expires=" + leftTime.toUTCString();
} else {
document.cookie = name + "=" + escape(value);
}
} }
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册