Merge branch 'dev' into pi

# Conflicts:
#	dc3/nginx/conf.d/default.conf
pull/1/head
pnoker 4 years ago
commit f28a0f6e99

@ -3,50 +3,47 @@ import router from '@/router/router'
import store from '@/store/store'; import store from '@/store/store';
import NProgress from 'nprogress' import NProgress from 'nprogress'
import 'nprogress/nprogress.css' import 'nprogress/nprogress.css'
import {getTokenCookies, getUserCookies} from '@/util/auth' import {failMessage} from '@/util/util'
import {showError} from '@/util/util' import {getStore} from "@/util/store";
import {getTokenCookies} from '@/util/auth'
//返回其他状态码
axios.defaults.validateStatus = function (status) {
return status >= 200 && status <= 500; // 默认的
};
//跨域请求允许保存cookie
axios.defaults.withCredentials = true;
//NProgress Configuration
NProgress.configure({ NProgress.configure({
showSpinner: false showSpinner: false
}); });
//跨域请求允许保存cookie
axios.defaults.withCredentials = true;
//返回其他状态码
axios.defaults.validateStatus = function (status) {
return status >= 200 && status <= 500;
};
//HTTP Request拦截 //HTTP Request拦截
axios.interceptors.request.use(config => { axios.interceptors.request.use(config => {
NProgress.start(); NProgress.start();
config.headers['Content-Type'] = 'application/json'; config.headers['Content-Type'] = 'application/json';
config.headers['Auth-User'] = getUserCookies(); config.headers['Auth-User'] = getStore({name: 'user'}) || '';
config.headers['Auth-Token'] = getTokenCookies(); config.headers['Auth-Token'] = getTokenCookies() || '';
return config; return config;
}, error => { }, error => {
console.error('Request interceptors:', error);
return Promise.reject(error) return Promise.reject(error)
}); });
//HTTP Response拦截 //HTTP Response拦截
axios.interceptors.response.use(res => { axios.interceptors.response.use(res => {
NProgress.done(); NProgress.done();
const ok = res.data.ok || false; const ok = res.data.ok || false, status = res.data.code || 200, message = res.data.message || '数据请求失败!';
const status = res.data.code || 200; if (status === 401) store.dispatch('ClearToken').then(() => router.push({path: '/login'}));
const message = res.data.message || '未知错误';
//如果是401则跳转到登录页面
if (status === 401) store.dispatch('FedLogOut').then(() => router.push({path: '/login'}));
// 如果请求为 !ok 默认统一处理
if (!ok) { if (!ok) {
showError(message); failMessage(message);
return Promise.reject(new Error(message)); return Promise.reject(new Error(message));
} }
return res.data; return res.data;
}, error => { }, error => {
NProgress.done(); console.error('Response interceptors:', error);
showError('未知错误'); return Promise.reject(error);
return Promise.reject(new Error(error));
}); });
export default axios export default axios

@ -14,4 +14,4 @@ new Vue({
router, router,
store, store,
render: h => h(App) render: h => h(App)
}).$mount('#app') }).$mount('#app');

@ -17,3 +17,15 @@ Vue.use(GeminiScrollbar);
Vue.component('base-card', baseCard); Vue.component('base-card', baseCard);
Vue.component('title-card', titleCard); Vue.component('title-card', titleCard);
console.log(
'.__ __ .___ ________\n' +
'|__| _____/ |_ __| _/____ \\_____ \\\n' +
'| |/ _ \\ __\\ / __ |/ ___\\ _(__ <\n' +
'| ( <_> ) | / /_/ \\ \\___ / \\\n' +
'|__|\\____/|__| \\____ |\\___ >______ /\n' +
' \\/ \\/ \\/\n' +
' https://gitee.com/pnoker/iot-dc3\n' +
' https://github.com/pnoker/iot-dc3\n' +
' DC3 IOT Platform V3.0 Pnoker Authors'
);

@ -3,9 +3,10 @@ import VueRouter from 'vue-router'
import CommonRouter from './common/index' import CommonRouter from './common/index'
import ViewsRouter from './views/index' import ViewsRouter from './views/index'
import NProgress from "nprogress"; import NProgress from "nprogress";
import {getTokenCookies, getUserCookies} from "@/util/auth";
import store from "@/store/store";
import {checkTokenValid} from "@/api/user"; import {checkTokenValid} from "@/api/user";
import store from "@/store/store";
import {getStore} from '@/util/store'
import {getTokenCookies} from "@/util/auth";
Vue.use(VueRouter); Vue.use(VueRouter);
@ -32,10 +33,10 @@ router.beforeEach((to, from, next) => {
if (meta.title) { if (meta.title) {
document.title = to.meta.title document.title = to.meta.title
} }
if (meta.isAuth !== true) { if (meta.isAuth !== true || from.name === 'login') {
next(); next();
} else { } else {
const token = getTokenCookies(), user = getUserCookies(); const token = getTokenCookies(), user = getStore({name: 'user'});
if ('' === token || undefined === token || '' === user || undefined === user) { if ('' === token || undefined === token || '' === user || undefined === user) {
next({path: '/login'}); next({path: '/login'});
return; return;

@ -1,22 +1,23 @@
import md5 from 'js-md5'; import md5 from 'js-md5';
import {getStore, removeStore, setStore} from '@/util/store' import {getStore, removeStore, setStore} from '@/util/store'
import {removeTokenCookies, removeUserCookies, setTokenCookies, setUserCookies} from '@/util/auth' import {getTokenCookies, removeTokenCookies, setTokenCookies} from '@/util/auth'
import {cancelToken, generateSalt, generateToken} from '@/api/user' import {cancelToken, generateSalt, generateToken} from '@/api/user'
const user = { const user = {
state: { state: {
user: getStore({name: 'user'}) || [], user: getStore({name: 'user'}) || '',
token: getStore({name: 'token'}) || '', token: getTokenCookies() || '',
}, },
actions: { actions: {
GenerateSalt(_, name) { GenerateSalt({commit}, name) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
generateSalt(name).then(res => { generateSalt(name).then(res => {
commit('SET_USER', name);
resolve(res.data); resolve(res.data);
}).catch(error => { }).catch(error => {
reject(error); reject(error);
}) })
}) });
}, },
GenerateToken({commit}, form) { GenerateToken({commit}, form) {
let user = { let user = {
@ -26,46 +27,41 @@ const user = {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
generateToken(user).then(res => { generateToken(user).then(res => {
const data = res.data; const data = res.data;
commit('SET_USER', user.name);
commit('SET_TOKEN', data); commit('SET_TOKEN', data);
resolve(); resolve();
}).catch(error => { }).catch(error => {
reject(error); reject(error);
}); });
}) });
}, },
ClearToken({commit}, name) { ClearToken({commit}) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
cancelToken(name).then((res) => { cancelToken(getStore({name: 'user'})).then((res) => {
commit('REMOVE_TOKEN');
commit('REMOVE_USER'); commit('REMOVE_USER');
commit('REMOVE_TOKEN');
resolve(res); resolve(res);
}).catch(error => { }).catch(error => {
reject(error); reject(error);
}); });
}) });
} }
}, },
mutations: { mutations: {
SET_USER: (state, user) => { SET_USER: (state, user) => {
setUserCookies(user);
state.user = user; state.user = user;
setStore({name: 'user', content: state.user, type: 'session'}) setStore({name: 'user', content: state.user});
}, },
REMOVE_USER: (state) => { REMOVE_USER: (state) => {
removeUserCookies();
state.user = ''; state.user = '';
removeStore({name: 'user', type: 'session'}); removeStore({name: 'user'});
}, },
SET_TOKEN: (state, token) => { SET_TOKEN: (state, token) => {
setTokenCookies(token);
state.token = token; state.token = token;
setStore({name: 'token', content: state.token, type: 'session'}) setTokenCookies(token);
}, },
REMOVE_TOKEN: (state) => { REMOVE_TOKEN: (state) => {
removeTokenCookies();
state.token = ''; state.token = '';
removeStore({name: 'token', type: 'session'}); removeTokenCookies();
} }
} }
}; };

@ -1,29 +1,18 @@
import Cookies from 'js-cookie' import Cookies from 'js-cookie'
import website from "@/config/website";
const UserKey = 'x-access-user'; const keyName = website.key + '-';
const TokenKey = 'x-access-token'; const TokenKey = keyName + 'auth-token';
// 120分钟120 * 60 * 1000 // 360分钟360 * 60 * 1000
const inFifteenMinutes = new Date(new Date().getTime() + 120 * 60 * 1000); const expiration = new Date(new Date().getTime() + 360 * 60 * 1000);
export function getUserCookies() {
return Cookies.get(UserKey);
}
export function setUserCookies(user) {
return Cookies.set(UserKey, user, {expires: inFifteenMinutes});
}
export function removeUserCookies() {
return Cookies.remove(UserKey);
}
export function getTokenCookies() { export function getTokenCookies() {
return Cookies.get(TokenKey); return Cookies.get(TokenKey);
} }
export function setTokenCookies(token) { export function setTokenCookies(token) {
return Cookies.set(TokenKey, token, {expires: inFifteenMinutes}); return Cookies.set(TokenKey, token, {expires: expiration});
} }
export function removeTokenCookies() { export function removeTokenCookies() {

@ -34,8 +34,7 @@ export const getStore = (params = {}) => {
debug debug
} = params; } = params;
name = keyName + name; name = keyName + name;
let obj = {}, let obj = {}, content;
content;
obj = window.sessionStorage.getItem(name); obj = window.sessionStorage.getItem(name);
if (isNull(obj)) obj = window.localStorage.getItem(name); if (isNull(obj)) obj = window.localStorage.getItem(name);
if (isNull(obj)) return; if (isNull(obj)) return;

@ -1,5 +1,7 @@
import {isNull} from './validate' import {isNull} from './validate'
import {Message} from 'element-ui' import {Message, Notification} from 'element-ui'
let failNotify = true, failNotifyText = '';
/** /**
* 表单序列化 * 表单序列化
@ -251,38 +253,6 @@ export const randomLenNum = (len, date) => {
return random; return random;
}; };
/**
* 成功操作
*
* @param message
*/
export const successMessage = (message) => {
if (message === '' || message == null) {
message = '操作成功!';
}
Message({
center: true,
type: 'success',
message: message
});
};
/**
* 失败操作
*
* @param message
*/
export const failMessage = (message) => {
if (message === '' || message == null) {
message = '操作失败!';
}
Message({
center: true,
type: 'error',
message: message
});
};
/** /**
* 打开全屏/关闭全屏 * 打开全屏/关闭全屏
*/ */
@ -377,11 +347,47 @@ export const openWindow = (url, title, w, h) => {
} }
}; };
export const showError = (message) => { /**
* 成功操作
*
* @param message
*/
export const successMessage = (message) => {
if (message === '' || message == null) {
message = '操作成功!';
}
Message({ Message({
center: true, center: true,
type: 'error', type: 'success',
showClose: true, showClose: true,
message: message message: message
}); });
}; };
/**
* 失败操作
*
* @param message
*/
export const failMessage = (message) => {
let show = true;
if (message === '' || message == null) {
message = '操作失败!';
}
if (failNotify && failNotifyText === message) {
show = false;
}
if (show) {
failNotify = true;
failNotifyText = message;
Notification.error({
title: '错误',
dangerouslyUseHTMLString: true,
message: `<p>${message}</p><p>详情: 请查看浏览器 控制台 输出。</p>`,
onClose: () => {
failNotify = false;
failNotifyText = '';
}
});
}
};

@ -71,8 +71,7 @@
}, },
handleCommand(command) { handleCommand(command) {
if (command === 'logout') { if (command === 'logout') {
let name = this.$store.getters.user; this.$store.dispatch('ClearToken').then((res) => {
this.$store.dispatch('ClearToken', name).then((res) => {
if (res.ok) { if (res.ok) {
this.$router.push('/login'); this.$router.push('/login');
} }

@ -12,7 +12,7 @@
<h4 class="login-title"> <h4 class="login-title">
Demo Application Demo Application
</h4> </h4>
<p style="text-align: center;">基于 DC3 接口开发的 demo 应用仅供演示使用<br> 使用 VueElementUiAvue 开发</p> <p style="text-align: center;">基于 DC3 接口开发的 demo 应用仅供演示和测试用途!</p>
<el-form class="login-form" <el-form class="login-form"
status-icon status-icon
:rules="loginRules" :rules="loginRules"

Loading…
Cancel
Save