From ded33b257299985937202de6effc8b6d7cae4759 Mon Sep 17 00:00:00 2001 From: adiao <1819192616@qq.com> Date: Mon, 8 May 2023 01:34:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86API=E5=92=8CStore?= =?UTF-8?q?=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/axios/interceptors/request.js | 13 +++++++ src/utils/axios/interceptors/response.js | 24 +++++++++++++ src/utils/axios/util/api.js | 2 ++ src/utils/axios/util/http.js | 43 ++++++++++++++++++++++++ src/utils/pasretTokenUtil.js | 0 src/utils/vuex/index.js | 5 +++ src/utils/vuex/modules/user.js | 35 +++++++++++++++++++ src/views/Login2.vue | 13 +++++++ 8 files changed, 135 insertions(+) create mode 100644 src/utils/axios/interceptors/request.js create mode 100644 src/utils/axios/interceptors/response.js create mode 100644 src/utils/axios/util/api.js create mode 100644 src/utils/axios/util/http.js create mode 100644 src/utils/pasretTokenUtil.js create mode 100644 src/utils/vuex/index.js create mode 100644 src/utils/vuex/modules/user.js create mode 100644 src/views/Login2.vue diff --git a/src/utils/axios/interceptors/request.js b/src/utils/axios/interceptors/request.js new file mode 100644 index 0000000..42b85c1 --- /dev/null +++ b/src/utils/axios/interceptors/request.js @@ -0,0 +1,13 @@ +import axios from "axios" +// 请求拦截器 +axios.interceptors.request.use( + config => { + // 每次发送请求之前判断是否存在token + // 如果存在,则统一在http请求的header都加上token,这样后台根据token判断你的登录情况,此处token一般是用户完成登录后储存到localstorage里的 + token && (config.headers.Authorization = token) + return config + }, + error => { + return Promise.error(error) + } +) \ No newline at end of file diff --git a/src/utils/axios/interceptors/response.js b/src/utils/axios/interceptors/response.js new file mode 100644 index 0000000..fc32e48 --- /dev/null +++ b/src/utils/axios/interceptors/response.js @@ -0,0 +1,24 @@ +import axios from "axios" +// 响应拦截器 +axios.interceptors.response.use(response => { + // 如果返回的状态码为200,说明接口请求成功,可以正常拿到数据 + // 否则的话抛出错误 + if (response.status === 200) { + if (response.data.code === 511) { + // 未授权调取授权接口 + } else if (response.data.code === 510) { + // 未登录跳转登录页 + } else { + return Promise.resolve(response) + } + } else { + return Promise.reject(response) + } +}, error => { + // 我们可以在这里对异常状态作统一处理 + if (error.response.status) { + // 处理请求失败的情况 + // 对不同返回码对相应处理 + return Promise.reject(error.response) + } +}) \ No newline at end of file diff --git a/src/utils/axios/util/api.js b/src/utils/axios/util/api.js new file mode 100644 index 0000000..9b391a5 --- /dev/null +++ b/src/utils/axios/util/api.js @@ -0,0 +1,2 @@ +import { httpGet, httpPost } from './http' +export const getorglist = (params = {}) => httpGet({ url: 'apps/api/org/list', params }) \ No newline at end of file diff --git a/src/utils/axios/util/http.js b/src/utils/axios/util/http.js new file mode 100644 index 0000000..4f21b5c --- /dev/null +++ b/src/utils/axios/util/http.js @@ -0,0 +1,43 @@ +export function httpGet({ + url, + params = {} + }) { + return new Promise((resolve, reject) => { + axios.get(url, { + params + }).then((res) => { + resolve(res.data) + }).catch(err => { + reject(err) + }) + }) + } + + // post + // post请求 + export function httpPost({ + url, + data = {}, + params = {} + }) { + return new Promise((resolve, reject) => { + axios({ + url, + method: 'post', + transformRequest: [function (data) { + let ret = '' + for (let it in data) { + ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&' + } + return ret + }], + // 发送的数据 + data, + // url参数 + params + + }).then(res => { + resolve(res.data) + }) + }) + } \ No newline at end of file diff --git a/src/utils/pasretTokenUtil.js b/src/utils/pasretTokenUtil.js new file mode 100644 index 0000000..e69de29 diff --git a/src/utils/vuex/index.js b/src/utils/vuex/index.js new file mode 100644 index 0000000..89498ad --- /dev/null +++ b/src/utils/vuex/index.js @@ -0,0 +1,5 @@ +import { createStore } from 'vuex' + +export default createStore({ + modules +}) \ No newline at end of file diff --git a/src/utils/vuex/modules/user.js b/src/utils/vuex/modules/user.js new file mode 100644 index 0000000..241a6ac --- /dev/null +++ b/src/utils/vuex/modules/user.js @@ -0,0 +1,35 @@ +export default { + store: { + username: '', + token: '', + image: '', + path: '' + }, + mutations: { + + }, + actions: { + + }, + getName: { + get(state) { + return username = store.username + } + }, + getToken: { + get(state) { + return token = store.token + } + }, + getPath: { + get(state) { + return path = store.path + } + + }, + getImage: { + get(state) { + return image = store.image + } + } +} \ No newline at end of file diff --git a/src/views/Login2.vue b/src/views/Login2.vue new file mode 100644 index 0000000..f3583a4 --- /dev/null +++ b/src/views/Login2.vue @@ -0,0 +1,13 @@ + + + \ No newline at end of file