globalConf
This commit is contained in:
parent
b39117e074
commit
92e756dbea
@ -21,6 +21,14 @@ Vue.component('textarea-input', TextareaInput);
|
|||||||
Vue.component('my-table', MyTable);
|
Vue.component('my-table', MyTable);
|
||||||
Vue.component('scroll-table', ScrollTable);
|
Vue.component('scroll-table', ScrollTable);
|
||||||
|
|
||||||
|
const globalConf = {
|
||||||
|
loginEndpoint: 'login',
|
||||||
|
loginRoute: '/login',
|
||||||
|
authHeader: 'X-Auth-Token',
|
||||||
|
initUrl: 'conf/init.json',
|
||||||
|
el: '#my-app'
|
||||||
|
}
|
||||||
|
|
||||||
const Router = new VueRouter();
|
const Router = new VueRouter();
|
||||||
|
|
||||||
const Store = new Vuex.Store({
|
const Store = new Vuex.Store({
|
||||||
@ -62,15 +70,15 @@ const Store = new Vuex.Store({
|
|||||||
apiRequest(context, payload) {
|
apiRequest(context, payload) {
|
||||||
let doRequest = () => {
|
let doRequest = () => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
const authH = {};
|
||||||
|
authH[globalConf.authHeader] = context.state.persist.authToken;
|
||||||
Axios({
|
Axios({
|
||||||
method: payload.method ? payload.method : 'get',
|
method: payload.method ? payload.method : 'get',
|
||||||
baseURL: context.state.ui.api.baseURL,
|
baseURL: context.state.ui.api.baseURL,
|
||||||
url: payload.endpoint,
|
url: payload.endpoint,
|
||||||
params: payload.params,
|
params: payload.params,
|
||||||
data: payload.data,
|
data: payload.data,
|
||||||
headers: {
|
headers: authH
|
||||||
'X-Auth-Token': context.state.persist.authToken
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.then(response => {
|
.then(response => {
|
||||||
console.log(response);
|
console.log(response);
|
||||||
@ -83,14 +91,14 @@ const Store = new Vuex.Store({
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
if (payload.endpoint != 'login') {
|
if (payload.endpoint != globalConf.loginEndpoint) {
|
||||||
// no jwt check for login call
|
// no jwt check for login call
|
||||||
|
|
||||||
// empty username = not logged in
|
// empty username = not logged in
|
||||||
if (!context.state.persist.credentials.username) {
|
if (!context.state.persist.credentials.username) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
// show login page
|
// show login page
|
||||||
Router.push('/login');
|
Router.push(globalConf.loginRoute);
|
||||||
reject(['not logged in']);
|
reject(['not logged in']);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -100,7 +108,7 @@ const Store = new Vuex.Store({
|
|||||||
// too old jwt, logout
|
// too old jwt, logout
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
context.commit('clearLogin');
|
context.commit('clearLogin');
|
||||||
Router.push('/login');
|
Router.push(globalConf.loginRoute);
|
||||||
reject(['jwt too old, logout']);
|
reject(['jwt too old, logout']);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -109,7 +117,7 @@ const Store = new Vuex.Store({
|
|||||||
// jwt near expire, get new one
|
// jwt near expire, get new one
|
||||||
console.log("getting new jwt");
|
console.log("getting new jwt");
|
||||||
|
|
||||||
return context.dispatch('login', context.state.persist.credentials)
|
return context.dispatch(globalConf.loginEndpoint, context.state.persist.credentials)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
console.log("LOOOGIIIINNN");
|
console.log("LOOOGIIIINNN");
|
||||||
return doRequest();
|
return doRequest();
|
||||||
@ -126,7 +134,7 @@ const Store = new Vuex.Store({
|
|||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
context.dispatch('apiRequest', {
|
context.dispatch('apiRequest', {
|
||||||
method: 'post',
|
method: 'post',
|
||||||
endpoint: 'login',
|
endpoint: globalConf.loginEndpoint,
|
||||||
data: payload
|
data: payload
|
||||||
})
|
})
|
||||||
.then(data => {
|
.then(data => {
|
||||||
@ -180,7 +188,8 @@ export default {
|
|||||||
Store,
|
Store,
|
||||||
Axios,
|
Axios,
|
||||||
InitApp(config) { // config: {initUrl, views, el}
|
InitApp(config) { // config: {initUrl, views, el}
|
||||||
Axios.get(config.initUrl)
|
Object.assign(globalConf, config);
|
||||||
|
Axios.get(globalConf.initUrl)
|
||||||
.then(results => {
|
.then(results => {
|
||||||
// set navigation
|
// set navigation
|
||||||
if (!Array.isArray(results.data.routes)) {
|
if (!Array.isArray(results.data.routes)) {
|
||||||
@ -202,7 +211,7 @@ export default {
|
|||||||
},
|
},
|
||||||
component: {
|
component: {
|
||||||
template: '<div id="' + name + rIdx + '">' + content + '</div>',
|
template: '<div id="' + name + rIdx + '">' + content + '</div>',
|
||||||
components: config.views,
|
components: globalConf.views,
|
||||||
data: function (data) {
|
data: function (data) {
|
||||||
if (typeof data != 'object') {
|
if (typeof data != 'object') {
|
||||||
return () => { return {}; };
|
return () => { return {}; };
|
||||||
@ -219,7 +228,7 @@ export default {
|
|||||||
|
|
||||||
// load app, when init finishs
|
// load app, when init finishs
|
||||||
new Vue({
|
new Vue({
|
||||||
el: config.el,
|
el: globalConf.el,
|
||||||
render: h => h(App),
|
render: h => h(App),
|
||||||
router: Router,
|
router: Router,
|
||||||
store: Store
|
store: Store
|
||||||
|
Loading…
Reference in New Issue
Block a user