From 002c90356d156831940a959c67aa983563a2ba47 Mon Sep 17 00:00:00 2001 From: bigrocs Date: Sat, 6 Jan 2018 08:38:33 +0800 Subject: [PATCH 01/11] =?UTF-8?q?=E6=B3=A8=E5=86=8C=E5=85=A8=E5=B1=80?= =?UTF-8?q?=E5=92=8C=E9=85=8D=E7=BD=AE=E8=BF=81=E7=A7=BB=E5=88=B0build-vue?= =?UTF-8?q?=E9=87=8C=E9=9D=A2=20=E5=8F=96=E6=B6=88main=20.js=E7=9A=84?= =?UTF-8?q?=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/app/app.vue | 2 -- src/index.js | 11 ++++++++++- src/store/index.js | 2 +- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/components/app/app.vue b/src/components/app/app.vue index c0dae1c..b2086b3 100644 --- a/src/components/app/app.vue +++ b/src/components/app/app.vue @@ -12,7 +12,6 @@ export default { /* [initMainData 通过apiUrl通信获取数据] */ initMainData () { let _this = this - let thenFunction = function(Response) { let mainData = Response.data.main /* 设置主通信数据 */ @@ -20,7 +19,6 @@ export default { /* 初始化路由 */ _this.$router.addRoutes(mainData.routes) } - let catchFunction = function(error) { console.log('%c 获取主配置信息失败! ', 'background: #222; color: #bada55') console.log('%c 请检查模板文件window.config.apiUrl配置参数是否正确,或者服务端是否通信正常。 ', 'background: #222; color: #bada55') diff --git a/src/index.js b/src/index.js index 61d10f4..9e4903d 100644 --- a/src/index.js +++ b/src/index.js @@ -1,9 +1,18 @@ +import Vue from 'vue' import App from './components/app' import router from './router' import store from './store' import 'font-awesome/css/font-awesome.css' - +//注册全局 +window.axios = require('axios') +window.Vue = Vue window.router = router +//配置axios请求头部 +window.axios.defaults.headers.common = { + 'X-CSRF-TOKEN': window.config.csrfToken, + 'X-Requested-With': 'XMLHttpRequest' +} + export { App, router, diff --git a/src/store/index.js b/src/store/index.js index c00f4f0..e35442e 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -8,7 +8,7 @@ Vue.use(Vuex) const state = { /* 设置api通信url */ - apiUrl: '', + apiUrl: window.config.apiUrl, /* 后端主要配置数据 */ mainData:{ routes: '', From a3391abdf6b60d1eeb3d9b3eec91777bad4678a0 Mon Sep 17 00:00:00 2001 From: bigrocs Date: Sat, 6 Jan 2018 09:00:19 +0800 Subject: [PATCH 02/11] =?UTF-8?q?vue=20=E5=90=AF=E5=8A=A8=E6=94=BE?= =?UTF-8?q?=E8=BF=9B=E6=9D=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 9e4903d..a415a6f 100644 --- a/src/index.js +++ b/src/index.js @@ -3,6 +3,8 @@ import App from './components/app' import router from './router' import store from './store' import 'font-awesome/css/font-awesome.css' + +Vue.config.productionTip = false //注册全局 window.axios = require('axios') window.Vue = Vue @@ -13,8 +15,17 @@ window.axios.defaults.headers.common = { 'X-Requested-With': 'XMLHttpRequest' } -export { +export default { + start (){ + new Vue({ + el: '#app', + router, + store, + render: h => h(App) + }) + }, + Vue, App, router, - store + store, } From de3b60ec519fb291f5830eabde2aaa668f68b961 Mon Sep 17 00:00:00 2001 From: bigrocs Date: Tue, 23 Jan 2018 09:05:51 +0800 Subject: [PATCH 03/11] add lodash --- src/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/index.js b/src/index.js index a415a6f..699275f 100644 --- a/src/index.js +++ b/src/index.js @@ -7,6 +7,7 @@ import 'font-awesome/css/font-awesome.css' Vue.config.productionTip = false //注册全局 window.axios = require('axios') +window._ = require('lodash') window.Vue = Vue window.router = router //配置axios请求头部 From 7e93228655f2a1132bb4fafe045e3871df485109 Mon Sep 17 00:00:00 2001 From: bigrocs Date: Fri, 26 Jan 2018 15:13:58 +0800 Subject: [PATCH 04/11] add dialogWidth --- src/store/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/store/index.js b/src/store/index.js index e35442e..4910bac 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -20,7 +20,8 @@ const state = { api:{ sidebar: '' }, - dialog: {} + dialog: {}, + dialogWidth: '61.8%' } export default new Vuex.Store({ state, From 7abbb0e8ae860ab3d2dd7e0dba1af3cb4c8faf6c Mon Sep 17 00:00:00 2001 From: bigrocs Date: Fri, 26 Jan 2018 15:37:23 +0800 Subject: [PATCH 05/11] =?UTF-8?q?=E5=A2=9E=E5=8A=A0dialogWidth=20=E8=AE=BE?= =?UTF-8?q?=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/store/actions.js | 3 +++ src/store/index.js | 2 +- src/store/mutation-types.js | 1 + src/store/mutations.js | 6 ++++++ 4 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/store/actions.js b/src/store/actions.js index 4b1bf77..6772bac 100644 --- a/src/store/actions.js +++ b/src/store/actions.js @@ -47,6 +47,9 @@ export const callbackData = ({ commit,state }, data) => { export const dialog = ({ commit,state }, {apiUrl, postData}) => { commit(types.DIALOG, {apiUrl, postData}) } +export const dialogWidth = ({ commit,state }, width) => { + commit(types.DIALOGWIDTH, width) +} export const deleteDialog = ({ commit,state }, apiUrl) => { commit(types.DELETE_DIALOG, apiUrl) } diff --git a/src/store/index.js b/src/store/index.js index 4910bac..e5fb113 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -21,7 +21,7 @@ const state = { sidebar: '' }, dialog: {}, - dialogWidth: '61.8%' + dialogWidth: '618px' } export default new Vuex.Store({ state, diff --git a/src/store/mutation-types.js b/src/store/mutation-types.js index b8710bd..9e5b940 100644 --- a/src/store/mutation-types.js +++ b/src/store/mutation-types.js @@ -2,5 +2,6 @@ export const SET_MAIN_DATA = 'SET_MAIN_DATA' export const SET_SIDEBAR = 'SET_SIDEBAR' export const CALLBACK_DATA = 'CALLBACK_DATA' export const DIALOG = 'DIALOG' +export const DIALOGWIDTH = 'DIALOGWIDTH' export const DELETE_DIALOG = 'DELETE_DIALOG' export const CALLBACK_ERROR = 'CALLBACK_ERROR' diff --git a/src/store/mutations.js b/src/store/mutations.js index 7b077a3..5a85afd 100644 --- a/src/store/mutations.js +++ b/src/store/mutations.js @@ -12,6 +12,12 @@ export default { [types.DIALOG] (state, {apiUrl, postData}){ Vue.set(state.dialog,apiUrl,postData) }, + + [types.DIALOGWIDTH] (state, width){ + if (!_.isEmpty(width)) { + state.dialogWidth = width + } + }, [types.DELETE_DIALOG] (state, apiUrl){ Vue.delete(state.dialog,apiUrl) }, From 776599d7ae76ec69a0fb385154ae0a8f2c1b6496 Mon Sep 17 00:00:00 2001 From: bigrocs Date: Thu, 1 Mar 2018 16:46:06 +0800 Subject: [PATCH 06/11] =?UTF-8?q?=E5=85=81=E8=AE=B8=E5=9B=9E=E6=89=A7cooki?= =?UTF-8?q?es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/index.js b/src/index.js index 699275f..6a011a7 100644 --- a/src/index.js +++ b/src/index.js @@ -15,6 +15,7 @@ window.axios.defaults.headers.common = { 'X-CSRF-TOKEN': window.config.csrfToken, 'X-Requested-With': 'XMLHttpRequest' } +window.axios.defaults.withCredentials=true export default { start (){ From 60e66943f608853cffed90e63a4eae8529a7b570 Mon Sep 17 00:00:00 2001 From: bigrocs Date: Fri, 9 Mar 2018 17:03:28 +0800 Subject: [PATCH 07/11] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E5=A4=B4=E9=83=A8?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.js | 6 ------ src/store/actions.js | 11 ++++++++++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/index.js b/src/index.js index 6a011a7..8e87715 100644 --- a/src/index.js +++ b/src/index.js @@ -10,12 +10,6 @@ window.axios = require('axios') window._ = require('lodash') window.Vue = Vue window.router = router -//配置axios请求头部 -window.axios.defaults.headers.common = { - 'X-CSRF-TOKEN': window.config.csrfToken, - 'X-Requested-With': 'XMLHttpRequest' -} -window.axios.defaults.withCredentials=true export default { start (){ diff --git a/src/store/actions.js b/src/store/actions.js index 6772bac..285bcd9 100644 --- a/src/store/actions.js +++ b/src/store/actions.js @@ -15,7 +15,16 @@ export const MessageInfo = (message, data) => { } /* 公共post请求方法 */ export const getData = ({ commit,state },{apiUrl = state.apiUrl, postData = null, thenFunction = null, catchFunction = null, message = null}) => { - window.axios.post(apiUrl,postData,{withCredentials:true}) + let token_type = localStorage.getItem('token_type') + let access_token = localStorage.getItem('access_token') + let refresh_token = localStorage.getItem('refresh_token') + window.axios.post(apiUrl,postData,{ + withCredentials:true, + headers: { + 'Accept': 'application/json', + 'Authorization': token_type + ' ' + access_token + }, + }) .then((Response) => { if (thenFunction) { thenFunction(Response) From 47b522bb798cf19d7348811fb9d6b6201c088a6b Mon Sep 17 00:00:00 2001 From: bigrocs Date: Sun, 11 Mar 2018 15:20:24 +0800 Subject: [PATCH 08/11] delete withCredentials:true, --- src/store/actions.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/store/actions.js b/src/store/actions.js index 285bcd9..ed204c6 100644 --- a/src/store/actions.js +++ b/src/store/actions.js @@ -19,7 +19,6 @@ export const getData = ({ commit,state },{apiUrl = state.apiUrl, postData = null let access_token = localStorage.getItem('access_token') let refresh_token = localStorage.getItem('refresh_token') window.axios.post(apiUrl,postData,{ - withCredentials:true, headers: { 'Accept': 'application/json', 'Authorization': token_type + ' ' + access_token From c7207570f21b4a8aac68dd601f34caacca603736 Mon Sep 17 00:00:00 2001 From: bigrocs Date: Fri, 16 Mar 2018 14:50:32 +0800 Subject: [PATCH 09/11] =?UTF-8?q?=E5=88=A0=E9=99=A4message?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/store/actions.js | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/store/actions.js b/src/store/actions.js index ed204c6..72e7822 100644 --- a/src/store/actions.js +++ b/src/store/actions.js @@ -1,4 +1,5 @@ import * as types from './mutation-types' +import { Message } from 'element-ui' /** * [setMainData 设置主通信数据] @@ -7,14 +8,8 @@ import * as types from './mutation-types' export const setMainData = ({ commit,state },mainData) => { commit(types.SET_MAIN_DATA,mainData) } -export const MessageInfo = (message, data) => { - message({ - message: data.message, - type: data.type, - }) -} /* 公共post请求方法 */ -export const getData = ({ commit,state },{apiUrl = state.apiUrl, postData = null, thenFunction = null, catchFunction = null, message = null}) => { +export const getData = ({ commit,state },{apiUrl = state.apiUrl, postData = null, thenFunction = null, catchFunction = null}) => { let token_type = localStorage.getItem('token_type') let access_token = localStorage.getItem('access_token') let refresh_token = localStorage.getItem('refresh_token') @@ -28,21 +23,25 @@ export const getData = ({ commit,state },{apiUrl = state.apiUrl, postData = null if (thenFunction) { thenFunction(Response) } - if (message && Response.data.message) { - MessageInfo(message, Response.data.message) + let message = Response.data.message + if (message) { + Message({ + message: message.message, + type: message.type, + }) } }) - .catch(function (error) { + .catch((error) => { if (catchFunction) { catchFunction(error) }else{ commit(types.CALLBACK_ERROR, error) - } - if (message) { - MessageInfo(message, { - message: '操作失败请联系管理员!', - type: 'error', - }) + // console.log(error) + // console.log(error.message) + // console.log(error.config) + // console.log(error.code) + // console.log(error.request) + console.log(error.response) } }) } From 62460e19507567b1f7df49301b697df193c0dc71 Mon Sep 17 00:00:00 2001 From: bigrocs Date: Fri, 16 Mar 2018 15:00:34 +0800 Subject: [PATCH 10/11] 'url': delete --- src/components/app/app.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/app/app.vue b/src/components/app/app.vue index b2086b3..11a5b60 100644 --- a/src/components/app/app.vue +++ b/src/components/app/app.vue @@ -26,7 +26,7 @@ export default { console.log(error) } let apiUrl = this.$store.state.apiUrl - this.$store.dispatch('getData',{ 'url':apiUrl, thenFunction, catchFunction }) + this.$store.dispatch('getData',{ apiUrl, thenFunction, catchFunction }) } } } From cbeef51917c8d9d21e8ffc5d8bf73bd9cac1bdd4 Mon Sep 17 00:00:00 2001 From: bigrocs Date: Mon, 26 Mar 2018 15:35:06 +0800 Subject: [PATCH 11/11] init --- src/components/app/app.vue | 9 ++-- src/index.js | 8 ++-- src/store/actions.js | 86 ++++++++++++++++++++++---------------- 3 files changed, 59 insertions(+), 44 deletions(-) diff --git a/src/components/app/app.vue b/src/components/app/app.vue index 11a5b60..504b4c0 100644 --- a/src/components/app/app.vue +++ b/src/components/app/app.vue @@ -11,13 +11,12 @@ export default { methods: { /* [initMainData 通过apiUrl通信获取数据] */ initMainData () { - let _this = this - let thenFunction = function(Response) { - let mainData = Response.data.main + let thenFunction = data => { + let mainData = data.main /* 设置主通信数据 */ - _this.$store.dispatch('setMainData',mainData) + this.$store.dispatch('setMainData',mainData) /* 初始化路由 */ - _this.$router.addRoutes(mainData.routes) + this.$router.addRoutes(mainData.routes) } let catchFunction = function(error) { console.log('%c 获取主配置信息失败! ', 'background: #222; color: #bada55') diff --git a/src/index.js b/src/index.js index 8e87715..0fadce7 100644 --- a/src/index.js +++ b/src/index.js @@ -6,10 +6,10 @@ import 'font-awesome/css/font-awesome.css' Vue.config.productionTip = false //注册全局 -window.axios = require('axios') -window._ = require('lodash') -window.Vue = Vue -window.router = router +// window.axios = require('axios') +// window._ = require('lodash') +// window.Vue = Vue +// window.router = router export default { start (){ diff --git a/src/store/actions.js b/src/store/actions.js index 72e7822..b129553 100644 --- a/src/store/actions.js +++ b/src/store/actions.js @@ -1,5 +1,8 @@ import * as types from './mutation-types' import { Message } from 'element-ui' +import localForage from 'localforage' +import axios from 'axios' + /** * [setMainData 设置主通信数据] @@ -9,41 +12,54 @@ export const setMainData = ({ commit,state },mainData) => { commit(types.SET_MAIN_DATA,mainData) } /* 公共post请求方法 */ -export const getData = ({ commit,state },{apiUrl = state.apiUrl, postData = null, thenFunction = null, catchFunction = null}) => { - let token_type = localStorage.getItem('token_type') - let access_token = localStorage.getItem('access_token') - let refresh_token = localStorage.getItem('refresh_token') - window.axios.post(apiUrl,postData,{ - headers: { - 'Accept': 'application/json', - 'Authorization': token_type + ' ' + access_token - }, - }) - .then((Response) => { - if (thenFunction) { - thenFunction(Response) - } - let message = Response.data.message - if (message) { - Message({ - message: message.message, - type: message.type, - }) - } - }) - .catch((error) => { - if (catchFunction) { - catchFunction(error) - }else{ - commit(types.CALLBACK_ERROR, error) - // console.log(error) - // console.log(error.message) - // console.log(error.config) - // console.log(error.code) - // console.log(error.request) - console.log(error.response) - } - }) +export const getData = async ({ commit,state },{apiUrl = state.apiUrl, postData = null, thenFunction = null, catchFunction = null, cache = true}) => { + let key = apiUrl+JSON.stringify(postData) + let cacheData = await localForage.getItem(key) + let thenFun = (data) => { + if (thenFunction) { + thenFunction(data) + } + let message = data.message + if (message) { + Message({ + message: message.message, + type: message.type, + }) + } + } + let catchFun = (error) => { + if (catchFunction) { + catchFunction(error) + }else{ + commit(types.CALLBACK_ERROR, error) + console.log(error) + // console.log(error.message) + // console.log(error.config) + // console.log(error.code) + // console.log(error.request) + // console.log(error.response) + } + } + if (cacheData && cache) { + thenFun(cacheData) + }else{ + axios.post(apiUrl,postData,{ + headers: { + 'Accept': 'application/json', + 'Authorization': localStorage.getItem('token_type') + ' ' + localStorage.getItem('access_token') + }, + }) + .then((Response) => { + let response = async () =>{ + await localForage.setItem(key, Response.data) + thenFun(Response.data) + } + response() + }) + .catch((error) => { + catchFun(error) + }) + } } export const setSidebar = ({ commit,state }, apiUrl) => { commit(types.SET_SIDEBAR, apiUrl)