From 898d7867b00d852c025ec1bdfb29d66f5029c65d Mon Sep 17 00:00:00 2001 From: Eddie <102564160@qq.com> Date: Sat, 19 Dec 2020 18:48:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0axios=20=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E8=B7=A8=E5=9F=9F=EF=BC=8C=20=E9=85=8D=E7=BD=AE=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E8=AF=B7=E6=B1=82=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .eslintrc.js | 1 + package-lock.json | 11 ++++- package.json | 1 + src/utils/api/login.js | 10 +++++ src/utils/request.js | 94 +++++++++++++++--------------------------- src/views/Register.vue | 21 ++++++---- vue.config.js | 21 +++++++++- 7 files changed, 88 insertions(+), 71 deletions(-) create mode 100644 src/utils/api/login.js diff --git a/.eslintrc.js b/.eslintrc.js index 8daffb4..acd0eba 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -11,6 +11,7 @@ module.exports = { "plugin:prettier/recommended" ], rules: { + "no-unused-vars": 'off', "vue/component-name-in-template-casing": ["error", "PascalCase"], "no-console": process.env.NODE_ENV === "production" ? "error" : "off", "no-debugger": process.env.NODE_ENV === "production" ? "error" : "off", diff --git a/package-lock.json b/package-lock.json index 89df3cf..1c78cc3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3680,6 +3680,14 @@ "integrity": "sha512-xh1Rl34h6Fi1DC2WWKfxUTVqRsNnr6LsKz2+hfwDxQJWmrx8+c7ylaqBMcHfl1U1r2dsifOvKX3LQuLNZ+XSvA==", "dev": true }, + "axios": { + "version": "0.21.0", + "resolved": "https://registry.npm.taobao.org/axios/download/axios-0.21.0.tgz", + "integrity": "sha1-Jt8IiAOiNQ3/LCf5b++Z/klEKso=", + "requires": { + "follow-redirects": "^1.10.0" + } + }, "babel-eslint": { "version": "10.1.0", "resolved": "https://registry.npmjs.org/babel-eslint/-/babel-eslint-10.1.0.tgz", @@ -7057,8 +7065,7 @@ "follow-redirects": { "version": "1.13.1", "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.1.tgz", - "integrity": "sha512-SSG5xmZh1mkPGyKzjZP8zLjltIfpW32Y5QpdNJyjcfGxK3qo3NDDkZOZSFiGn1A6SclQxY9GzEwAHQ3dmYRWpg==", - "dev": true + "integrity": "sha512-SSG5xmZh1mkPGyKzjZP8zLjltIfpW32Y5QpdNJyjcfGxK3qo3NDDkZOZSFiGn1A6SclQxY9GzEwAHQ3dmYRWpg==" }, "for-in": { "version": "1.0.2", diff --git a/package.json b/package.json index 9d26c4b..21f41ff 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "@ckeditor/ckeditor5-theme-lark": "^21.0.0", "@ckeditor/ckeditor5-vue": "^1.0.1", "@popperjs/core": "^2.4.4", + "axios": "^0.21.0", "cash-dom": "^8.0.0", "chart.js": "^2.9.3", "core-js": "^3.6.5", diff --git a/src/utils/api/login.js b/src/utils/api/login.js new file mode 100644 index 0000000..ed65c91 --- /dev/null +++ b/src/utils/api/login.js @@ -0,0 +1,10 @@ + +import request from '@/utils/request' + +export function signUp (data){ + return request({ + url: '123',//自己的接口地址 + method: 'post',//请求方法 + data //需要携带的参数 + }) +} \ No newline at end of file diff --git a/src/utils/request.js b/src/utils/request.js index 436dfb1..b2ad690 100644 --- a/src/utils/request.js +++ b/src/utils/request.js @@ -1,67 +1,41 @@ -import axios from '@/utils/request' - - + +import axios from 'axios' + + +const token = false +// create an axios instance const service = axios.create({ - baseURL: process.env.VUE_APP_BASE_URL, - timeout: 5000 + baseURL: '/api', + timeout: 80000 // request timeout }) - - + +// request interceptor service.interceptors.request.use( - config => { - if (store.getters.token) { - config.headers['X-Token'] = getToken() - } - return config - }, - error => { - return Promise.reject(error) - } + + config => { + // Do something before request is sent + if (token) { + // 让每个请求携带token-- ['X-Token']为自定义key 请根据实际情况自行修改 + config.headers.Authorization = token + + } + return config + }, + error => { + // Do something with request error + // console.log("出错啦",error) // for debug + Promise.reject(error) + } ) - + // response interceptor service.interceptors.response.use( - response => { - const res = response.data - console.log(res.msg, 'msg') - // if the custom code is not 20000, it is judged as an error. - if (res.code !== 0) { - Message({ - message: res.message || 'Error', - type: 'error', - duration: 5 * 1000 - }) - if (res.code === 50008 || res.code === 50012 || res.code === 50014) { - console.log(res.code, 'res code3') - // to re-login - MessageBox.confirm( - 'You have been logged out, you can cancel to stay on this page, or log in again', - 'Confirm logout', - { - confirmButtonText: 'Re-Login', - cancelButtonText: 'Cancel', - type: 'warning' - } - ).then(() => { - store.dispatch('user/resetToken').then(() => { - location.reload() - }) - }) - } - return Promise.reject(new Error(res.message || 'Error')) - } else { - return res - } - }, - error => { - console.log('err' + error) // for debug - Message({ - message: error.message, - type: 'error', - duration: 5 * 1000 - }) - return Promise.reject(error) - } + response => response, + + error => { + console.log('err' + error) // for debug + return Promise.reject(error) + } ) - -export default service + +export default service \ No newline at end of file diff --git a/src/views/Register.vue b/src/views/Register.vue index abfab2a..c205ce7 100644 --- a/src/views/Register.vue +++ b/src/views/Register.vue @@ -127,6 +127,8 @@