Commit 3133e0a0 authored by 水落(YangLei)'s avatar 水落(YangLei)

feat: 请求处理

parent fdf67e83
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
<slot name="search" :query="queryForm" /> <slot name="search" :query="queryForm" />
</a-form-model> </a-form-model>
<div class="tw-text-right"> <div class="tw-text-right tw-mt-2">
<a-space> <a-space>
<a-button @click="queryForm = {}">重置</a-button> <a-button @click="queryForm = {}">重置</a-button>
<a-button type="primary" @click="getData">查询</a-button> <a-button type="primary" @click="getData">查询</a-button>
...@@ -44,7 +44,6 @@ ...@@ -44,7 +44,6 @@
<script> <script>
import { request, METHOD } from '@/utils/requestUtil'; import { request, METHOD } from '@/utils/requestUtil';
import BASE_URL from '@/utils/baseUrlUtil';
const data = [ const data = [
{ {
...@@ -101,8 +100,8 @@ export default { ...@@ -101,8 +100,8 @@ export default {
this.data = data; this.data = data;
this.loading = false; this.loading = false;
}, 1000); }, 1000);
// const res = await request(`${BASE_URL}/${this.url}`, METHOD.GET); const res = await request(this.url, METHOD.GET);
// console.log(res); console.log(res);
}, },
add() { add() {
this.addVisible = true; this.addVisible = true;
...@@ -116,6 +115,6 @@ export default { ...@@ -116,6 +115,6 @@ export default {
<style module> <style module>
.card { .card {
@apply tw-bg-white tw-rounded-md tw-p-2.5; @apply tw-bg-white tw-rounded-md tw-p-4;
} }
</style> </style>
...@@ -48,8 +48,6 @@ const i18n = new VueI18n({ ...@@ -48,8 +48,6 @@ const i18n = new VueI18n({
silentFallbackWarn: true, silentFallbackWarn: true,
}); });
//装载antd控件 //装载antd控件
Vue.use(Antd); Vue.use(Antd);
//装载Viser; //装载Viser;
...@@ -75,7 +73,7 @@ loadGuards(guards, { router, store, i18n, message: Vue.prototype.$message }); ...@@ -75,7 +73,7 @@ loadGuards(guards, { router, store, i18n, message: Vue.prototype.$message });
// }, // },
// }); // });
loadResponseInterceptor(); loadResponseInterceptor({ router });
new Vue({ new Vue({
router, router,
......
...@@ -78,7 +78,7 @@ ...@@ -78,7 +78,7 @@
<script> <script>
import CommonLayout from '@/pages/frame/layouts/CommonLayout'; import CommonLayout from '@/pages/frame/layouts/CommonLayout';
import { login } from '@/pages/frame/services/accountService'; import { login } from '@/pages/frame/services/accountService';
import { setToken } from '@/utils/requestUtil'; import { setToken, clearToken } from '@/utils/requestUtil';
import { loadRoutes } from '@/utils/routerUtil'; import { loadRoutes } from '@/utils/routerUtil';
import { mapMutations } from 'vuex'; import { mapMutations } from 'vuex';
import loginI18n from './i18n'; import loginI18n from './i18n';
...@@ -93,6 +93,9 @@ export default { ...@@ -93,6 +93,9 @@ export default {
form: this.$form.createForm(this), form: this.$form.createForm(this),
}; };
}, },
created() {
clearToken();
},
computed: { computed: {
applicationName() { applicationName() {
return this.$store.state.settingModule.systemName; return this.$store.state.settingModule.systemName;
...@@ -117,6 +120,7 @@ export default { ...@@ -117,6 +120,7 @@ export default {
afterLogin(result) { afterLogin(result) {
this.logging = false; this.logging = false;
const { token, userId } = result; const { token, userId } = result;
console.log(token);
setToken(token); setToken(token);
this.$router.replace('/dashboard/workbench'); this.$router.replace('/dashboard/workbench');
......
<template> <template>
<h1>Menu Management</h1> <my-table url="/api/v1/menus" addBtn="addBtn">
<template #search="{query}">
<a-form-model-item label="菜单、目录名称">
<a-input v-model="query.name" />
</a-form-model-item>
</template>
<template #add>
<a-input v-model="add.name" />
</template>
<a-table-column title="姓名" data-index="name" />
<a-table-column title="操作">
<template #default="row">
<span>
<a> {{ row }}</a>
<a-divider type="vertical" />
<a>Delete</a>
</span>
</template>
</a-table-column>
</my-table>
</template> </template>
\ No newline at end of file <script>
export default {
data: () => ({
add: {},
addBtn: { text: '新建', title: '菜单配置' },
}),
};
</script>
import Cookie from 'js-cookie'
// 401拦截 // 401拦截
const resp401 = { const resp401 = {
/** /**
* 响应数据之前做点什么 * 响应数据之前做点什么
* @param response 响应对象 * @param response 响应对象
* @param options 应用配置 包含: {router, i18n, store, message} * @param options 应用配置 包含: {router, i18n, store, message}
* @returns {*} * @returns {*}
*/ */
onFulfilled(response, options) { onFulfilled(response, options) {
const {message} = options const { message } = options;
if (response.code === 401) { if (response.code === 401) {
message.error('无此权限') message.error('无此权限');
} }
return response return response;
}, },
/** /**
* 响应出错时执行 * 响应出错时执行
* @param error 错误对象 * @param error 错误对象
* @param options 应用配置 包含: {router, i18n, store, message} * @param options 应用配置 包含: {router, i18n, store, message}
* @returns {Promise<never>} * @returns {Promise<never>}
*/ */
onRejected(error, options) { onRejected(error, options) {
const {message} = options const { message } = options;
const {response} = error const { response } = error;
if (response.status === 401) { if (response.status === 401) {
message.error('无此权限') message.error('无此权限');
} }
return Promise.reject(error) return Promise.reject(error);
} },
} };
const resp403 = { const resp403 = {
onFulfilled(response, options) { onFulfilled(response, options) {
const {message} = options const { message } = options;
if (response.code === 403) { if (response.code === 403) {
message.error('请求被拒绝') message.error('请求被拒绝');
} }
return response return response;
}, },
onRejected(error, options) { onRejected(error, options) {
const {message} = options const { message } = options;
const {response} = error const { response } = error;
if (response.status === 403) { if (response.status === 403) {
message.error('请求被拒绝') message.error('请求被拒绝');
} }
return Promise.reject(error) return Promise.reject(error);
} },
} };
const reqCommon = { const reqCommon = {
/** /**
* 发送请求之前做些什么 * 发送请求之前做些什么
* @param config axios config * @param config axios config
* @param options 应用配置 包含: {router, i18n, store, message} * @param options 应用配置 包含: {router, i18n, store, message}
* @returns {*} * @returns {*}
*/ */
onFulfilled(config, options) { onFulfilled(config, options) {
const {message} = options const { message } = options;
const {url, xsrfCookieName} = config const { url, xsrfCookieName } = config;
if (url.indexOf('login') === -1 && xsrfCookieName && !Cookie.get(xsrfCookieName)) { if (url.indexOf('login') === -1 && xsrfCookieName) {
message.warning('认证 token 已过期,请重新登录') message.warning('认证 token 已过期,请重新登录');
} }
return config return config;
}, },
/** /**
* 请求出错时做点什么 * 请求出错时做点什么
* @param error 错误对象 * @param error 错误对象
* @param options 应用配置 包含: {router, i18n, store, message} * @param options 应用配置 包含: {router, i18n, store, message}
* @returns {Promise<never>} * @returns {Promise<never>}
*/ */
onRejected(error, options) { onRejected(error, options) {
const {message} = options const { message } = options;
message.error(error.message) message.error(error.message);
return Promise.reject(error) return Promise.reject(error);
} },
} };
export default { export default {
request: [reqCommon], // 请求拦截 request: [reqCommon], // 请求拦截
response: [resp401, resp403] // 响应拦截 response: [resp401, resp403], // 响应拦截
} };
...@@ -14,9 +14,10 @@ axios.defaults.xsrfCookieName = xsrfHeaderName; ...@@ -14,9 +14,10 @@ axios.defaults.xsrfCookieName = xsrfHeaderName;
/** /**
* @param {*} info 提示函数 * @param {*} info 提示函数
*/ */
function loadResponseInterceptor() { function loadResponseInterceptor({ router }) {
axios.interceptors.request.use( axios.interceptors.request.use(
function(config) { function(config) {
console.log(getToken(), config);
// 在发送请求之前做些什么 // 在发送请求之前做些什么
config.headers = { config.headers = {
...config.headers, ...config.headers,
...@@ -40,10 +41,15 @@ function loadResponseInterceptor() { ...@@ -40,10 +41,15 @@ function loadResponseInterceptor() {
if (data.code === 'sys.success') { if (data.code === 'sys.success') {
return data.data; return data.data;
} }
if (data.code === 'error.system.authc') {
router.push('/login');
}
notification.error({ notification.error({
message: data.code, message: data.code,
description: h => h('pre', data.message), description: h => h('pre', data.message),
}); });
return Promise.reject(data.message); return Promise.reject(data.message);
}, },
function(error) { function(error) {
...@@ -58,14 +64,6 @@ function loadResponseInterceptor() { ...@@ -58,14 +64,6 @@ function loadResponseInterceptor() {
); );
} }
// 认证类型
const AUTH_TYPE = {
BEARER: 'Bearer',
BASIC: 'basic',
AUTH1: 'auth1',
AUTH2: 'auth2',
};
// http method // http method
const METHOD = { const METHOD = {
GET: 'get', GET: 'get',
...@@ -136,13 +134,4 @@ function checkAuthorization() { ...@@ -136,13 +134,4 @@ function checkAuthorization() {
return !!getToken(); return !!getToken();
} }
export { export { METHOD, request, parseUrlParams, loadResponseInterceptor, setToken, checkAuthorization, clearToken };
METHOD,
AUTH_TYPE,
request,
parseUrlParams,
loadResponseInterceptor,
setToken,
checkAuthorization,
clearToken,
};
...@@ -177,7 +177,6 @@ const assetsCDN = { ...@@ -177,7 +177,6 @@ const assetsCDN = {
nprogress: 'NProgress', nprogress: 'NProgress',
clipboard: 'ClipboardJS', clipboard: 'ClipboardJS',
'@antv/data-set': 'DataSet', '@antv/data-set': 'DataSet',
'js-cookie': 'Cookies',
}, },
css: [], css: [],
js: [ js: [
...@@ -188,7 +187,6 @@ const assetsCDN = { ...@@ -188,7 +187,6 @@ const assetsCDN = {
'//cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.js', '//cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.js',
'//cdn.jsdelivr.net/npm/clipboard@2.0.6/dist/clipboard.min.js', '//cdn.jsdelivr.net/npm/clipboard@2.0.6/dist/clipboard.min.js',
'//cdn.jsdelivr.net/npm/@antv/data-set@0.11.4/build/data-set.min.js', '//cdn.jsdelivr.net/npm/@antv/data-set@0.11.4/build/data-set.min.js',
'//cdn.jsdelivr.net/npm/js-cookie@2.2.1/src/js.cookie.min.js',
], ],
}; };
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment