From 08f4b538a9611c565d152f716f1d7723c11187b6 Mon Sep 17 00:00:00 2001 From: RogerWork Date: Tue, 24 Oct 2023 18:31:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E8=AF=AD=E8=A8=80=E5=88=87?= =?UTF-8?q?=E6=8D=A2=E7=9A=84=E5=AD=A6=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 + src/request/config.js | 10 ++++ src/request/index.js | 109 ++++++++++++++++++++++++++++++++++++++ src/views/login/index.vue | 16 ++++-- yarn.lock | 57 ++++++++++++++++++++ 5 files changed, 189 insertions(+), 4 deletions(-) create mode 100644 src/request/config.js create mode 100644 src/request/index.js diff --git a/package.json b/package.json index ac8749f..abdbabe 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "dependencies": { "@element-plus/icons-vue": "^2.1.0", "@types/node": "^20.8.7", + "axios": "^1.5.1", "element-plus": "^2.4.1", "normalize.css": "^8.0.1", "vue": "^3.3.4", diff --git a/src/request/config.js b/src/request/config.js new file mode 100644 index 0000000..c8e3826 --- /dev/null +++ b/src/request/config.js @@ -0,0 +1,10 @@ +export default { + method: 'get', + baseURL: 'http://localhost:8001', + header: { + 'Content-Type': 'application/json;charset=UTF-8' + }, + timeout: 10000, + withCredentials: true, + responseType: 'json', +} \ No newline at end of file diff --git a/src/request/index.js b/src/request/index.js new file mode 100644 index 0000000..75b5ade --- /dev/null +++ b/src/request/index.js @@ -0,0 +1,109 @@ +import axios from "axios"; +import config from "@/request/config.js"; +import router from "@/router/index.js"; +import ElMessage from 'element-plus'; + +export default function request(options) { + return new Promise((resolve, reject) => { + const instance = axios.create({...config}); + // 请求拦截器 + instance.interceptors.request.use( + (config) => { + let token = localStorage.getItem('pm_token'); + if (token) { + config.headers.token = token; + } else { + router.push('/login') + } + }, + (error) => { + console.log("request:", error); + if (error.code === "ECONNABORTED" && error.message.indexOf("timeout") !== -1) { + ElMessage({message: "请求超时", type: "error", showClose: true}); + } + return Promise.reject(error) + } + ); + // 响应拦截器 + instance.interceptors.response.use( + (response) => { + return response.data; + }, + (err) => { + if (err && err.response) { + switch (err.response.status) { + case 400: + err.message = "请求错误"; + break + case 401: + err.message = "未授权"; + break + case 403: + err.message = "拒绝访问"; + break + case 404: + err.message = `请求地址出错:${err.response.config.url}`; + break + case 408: + err.message = "请求超时"; + break; + case 500: + err.message = "服务器内部错误"; + break; + case 501: + err.message = "服务未实现"; + break; + case 502: + err.message = "网关错误"; + break; + case 503: + err.message = "服务不可用"; + break; + case 504: + err.message = "网关超时"; + break; + case 505: + err.message = "HTTP版本不受支持"; + break; + default: + } + } + console.log(err); + if (err.message) { + ElMessage({message: err.message, type: 'error', showClose: true}); + } + return Promise.reject(err); + } + ); + //请求处理 + instance(options).then( + /** + * response 统一格式 + * { + * code: 200, + * msg: '消息[String]', + * data: '返回数据[Any]' + * } + * code说明: + * 200 成功 + * -1 失败,可能网络不通,可能后台服务异常或其它异常 + * -2 登录失效跳回登录 + */ + (res) => { + if (res.code === 200) { + resolve(res); + }else{ + if (res.code === -2) { + router.push("/login"); + } + ElMessage({message: res.msg || "操作失败", type: "error", showClose: true}); + reject(res); + } + } + ).catch( + (error) => { + reject(error); + } + ) + }) +} \ No newline at end of file diff --git a/src/views/login/index.vue b/src/views/login/index.vue index 69ed15c..caaefb8 100644 --- a/src/views/login/index.vue +++ b/src/views/login/index.vue @@ -1,11 +1,19 @@ - - + + diff --git a/yarn.lock b/yarn.lock index 4a083d9..68400a1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -382,6 +382,20 @@ async-validator@^4.2.5: resolved "https://registry.yarnpkg.com/async-validator/-/async-validator-4.2.5.tgz#c96ea3332a521699d0afaaceed510a54656c6339" integrity sha512-7HhHjtERjqlNbZtqNqy2rckN/SpOOlmDliet+lP7k+eKZEjPk3DgyeU9lIXLdeLz0uBbbVp+9Qdow9wJWgwwfg== +asynckit@^0.4.0: + version "0.4.0" + resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79" + integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q== + +axios@^1.5.1: + version "1.5.1" + resolved "https://registry.yarnpkg.com/axios/-/axios-1.5.1.tgz#11fbaa11fc35f431193a9564109c88c1f27b585f" + integrity sha512-Q28iYCWzNHjAm+yEAot5QaAMxhMghWLFVf7rRdwhUI+c2jix2DUXjAHXVi+s1ibs3mjPO/cCgbA++3BjD0vP/A== + dependencies: + follow-redirects "^1.15.0" + form-data "^4.0.0" + proxy-from-env "^1.1.0" + balanced-match@^1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee" @@ -421,6 +435,13 @@ braces@^3.0.2, braces@~3.0.2: optionalDependencies: fsevents "~2.3.2" +combined-stream@^1.0.8: + version "1.0.8" + resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f" + integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg== + dependencies: + delayed-stream "~1.0.0" + csstype@^3.1.1: version "3.1.2" resolved "https://registry.yarnpkg.com/csstype/-/csstype-3.1.2.tgz#1d4bf9d572f11c14031f0436e1c10bc1f571f50b" @@ -438,6 +459,11 @@ debug@^4.3.4: dependencies: ms "2.1.2" +delayed-stream@~1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619" + integrity sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ== + element-plus@^2.4.1: version "2.4.1" resolved "https://registry.yarnpkg.com/element-plus/-/element-plus-2.4.1.tgz#8a5faa69e856d82494b94d77fb485d9e727c8bc1" @@ -527,6 +553,20 @@ fill-range@^7.0.1: dependencies: to-regex-range "^5.0.1" +follow-redirects@^1.15.0: + version "1.15.3" + resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.3.tgz#fe2f3ef2690afce7e82ed0b44db08165b207123a" + integrity sha512-1VzOtuEM8pC9SFU1E+8KfTjZyMztRsgEfwQl44z8A25uy13jSzTj6dyK2Df52iV0vgHCfBwLhDWevLn95w5v6Q== + +form-data@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452" + integrity sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww== + dependencies: + asynckit "^0.4.0" + combined-stream "^1.0.8" + mime-types "^2.1.12" + fsevents@~2.3.2: version "2.3.3" resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-2.3.3.tgz#cac6407785d03675a2a5e1a5305c697b347d90d6" @@ -630,6 +670,18 @@ micromatch@^4.0.4: braces "^3.0.2" picomatch "^2.3.1" +mime-db@1.52.0: + version "1.52.0" + resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.52.0.tgz#bbabcdc02859f4987301c856e3387ce5ec43bf70" + integrity sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg== + +mime-types@^2.1.12: + version "2.1.35" + resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.35.tgz#381a871b62a734450660ae3deee44813f70d959a" + integrity sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw== + dependencies: + mime-db "1.52.0" + minimatch@^9.0.2, minimatch@^9.0.3: version "9.0.3" resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-9.0.3.tgz#a6e00c3de44c3a542bfaae70abfc22420a6da825" @@ -710,6 +762,11 @@ postcss@^8.1.10, postcss@^8.4.27: picocolors "^1.0.0" source-map-js "^1.0.2" +proxy-from-env@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz#e102f16ca355424865755d2c9e8ea4f24d58c3e2" + integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg== + queue-microtask@^1.2.2: version "1.2.3" resolved "https://registry.yarnpkg.com/queue-microtask/-/queue-microtask-1.2.3.tgz#4929228bbc724dfac43e0efb058caf7b6cfb6243"