From 7762e02b31dae6b42cb77be26c6df7e56c0a4b87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E4=BC=A0=E9=BE=99?= Date: Mon, 25 Jul 2022 18:36:22 +0800 Subject: [PATCH] refactor: refactor api usage --- src/api/auth/index.js | 16 -------- src/api/index.js | 6 +++ src/api/post/index.js | 39 ------------------- src/api/user/index.js | 38 ------------------ src/store/modules/user.js | 4 +- src/utils/http/index.js | 2 + src/utils/token.js | 4 +- src/views/examples/table/post/api.js | 13 +++++++ src/views/examples/table/post/usePostTable.js | 4 +- src/views/login/api.js | 5 +++ src/views/login/index.vue | 4 +- 11 files changed, 34 insertions(+), 101 deletions(-) delete mode 100644 src/api/auth/index.js create mode 100644 src/api/index.js delete mode 100644 src/api/post/index.js delete mode 100644 src/api/user/index.js create mode 100644 src/views/examples/table/post/api.js create mode 100644 src/views/login/api.js diff --git a/src/api/auth/index.js b/src/api/auth/index.js deleted file mode 100644 index 74c6082..0000000 --- a/src/api/auth/index.js +++ /dev/null @@ -1,16 +0,0 @@ -import { defAxios as request } from '@/utils/http' - -export const login = (data) => { - return request({ - url: '/auth/login', - method: 'post', - data, - }) -} - -export const refreshToken = () => { - return request({ - url: '/auth/refreshToken', - method: 'post', - }) -} diff --git a/src/api/index.js b/src/api/index.js new file mode 100644 index 0000000..2e3ac83 --- /dev/null +++ b/src/api/index.js @@ -0,0 +1,6 @@ +import request from '@/utils/http' + +export default { + getUser: () => request.get('/user'), + refreshToken: () => request.post('/auth/refreshToken'), +} diff --git a/src/api/post/index.js b/src/api/post/index.js deleted file mode 100644 index d40a9a7..0000000 --- a/src/api/post/index.js +++ /dev/null @@ -1,39 +0,0 @@ -import { defAxios as request } from '@/utils/http' - -export function getPosts(params = {}) { - return request({ - url: '/posts', - method: 'get', - params, - }) -} - -export function getPostById({ id }) { - return request({ - url: `/post/${id}`, - method: 'get', - }) -} - -export function savePost(id, data = {}) { - if (id) { - return request({ - url: `/post/${id}`, - method: 'put', - data, - }) - } - - return request({ - url: '/post', - method: 'post', - data, - }) -} - -export function deletePost(id) { - return request({ - url: `/post/${id}`, - method: 'delete', - }) -} diff --git a/src/api/user/index.js b/src/api/user/index.js deleted file mode 100644 index b12ed7f..0000000 --- a/src/api/user/index.js +++ /dev/null @@ -1,38 +0,0 @@ -import { defAxios as request } from '@/utils/http' - -export function getUsers(params = {}) { - return request({ - url: '/users', - method: 'get', - params, - }) -} - -export function getUser(id) { - if (id) { - return request({ - url: `/user/${id}`, - method: 'get', - }) - } - return request({ - url: '/user', - method: 'get', - }) -} - -export function saveUser(data = {}, id) { - if (id) { - return request({ - url: '/user', - method: 'put', - data, - }) - } - - return request({ - url: `/user/${id}`, - method: 'put', - data, - }) -} diff --git a/src/store/modules/user.js b/src/store/modules/user.js index 1685030..2d15164 100644 --- a/src/store/modules/user.js +++ b/src/store/modules/user.js @@ -1,7 +1,7 @@ import { defineStore } from 'pinia' -import { getUser } from '@/api/user' import { removeToken } from '@/utils/token' import { toLogin } from '@/utils/auth' +import api from '@/api' export const useUserStore = defineStore('user', { state() { @@ -26,7 +26,7 @@ export const useUserStore = defineStore('user', { actions: { async getUserInfo() { try { - const res = await getUser() + const res = await api.getUser() if (res.code === 0) { const { id, name, avatar, role } = res.data this.userInfo = { id, name, avatar, role } diff --git a/src/utils/http/index.js b/src/utils/http/index.js index 6c9d12b..35a61a3 100644 --- a/src/utils/http/index.js +++ b/src/utils/http/index.js @@ -17,6 +17,8 @@ export function createAxios(options = {}) { export const defAxios = createAxios() +export default createAxios() + export const testAxios = createAxios({ baseURL: import.meta.env.VITE_APP_BASE_API_TEST, }) diff --git a/src/utils/token.js b/src/utils/token.js index 3a26fb8..45bf364 100644 --- a/src/utils/token.js +++ b/src/utils/token.js @@ -1,5 +1,5 @@ import { lStorage } from './cache' -import { refreshToken } from '@/api/auth' +import api from '@/api' const TOKEN_CODE = 'access_token' const DURATION = 6 * 60 * 60 @@ -25,7 +25,7 @@ export async function refreshAccessToken() { // token生成或者刷新后30分钟内不执行刷新 if (new Date().getTime() - time <= 1000 * 60 * 30) return try { - const res = await refreshToken() + const res = await api.refreshToken() if (res.code === 0) { setToken(res.data.token) } diff --git a/src/views/examples/table/post/api.js b/src/views/examples/table/post/api.js new file mode 100644 index 0000000..42fa0b6 --- /dev/null +++ b/src/views/examples/table/post/api.js @@ -0,0 +1,13 @@ +import request from '@/utils/http' + +export default { + getPosts: (params = {}) => request.get('posts', params), + getPostById: (id) => request.get(`/post/${id}`), + savePost: (id, data = {}) => { + if (id) { + return request.put(`/post/${id}`, data) + } + return request.post('/post', data) + }, + deletePost: (id) => request.delete(`/post/${id}`), +} diff --git a/src/views/examples/table/post/usePostTable.js b/src/views/examples/table/post/usePostTable.js index 1377493..b189b30 100644 --- a/src/views/examples/table/post/usePostTable.js +++ b/src/views/examples/table/post/usePostTable.js @@ -1,7 +1,7 @@ import { h } from 'vue' import { NButton, NSwitch } from 'naive-ui' -import { getPosts } from '@/api/post' import { formatDateTime } from '@/utils' +import api from './api' export const usePostTable = () => { // refs @@ -133,7 +133,7 @@ export const usePostTable = () => { async function getTableData() { try { - const res = await getPosts() + const res = await api.getPosts() if (res.code === 0) { return res.data } diff --git a/src/views/login/api.js b/src/views/login/api.js new file mode 100644 index 0000000..bebc2ea --- /dev/null +++ b/src/views/login/api.js @@ -0,0 +1,5 @@ +import request from '@/utils/http' + +export default { + login: (data) => request.post('/auth/login', data), +} diff --git a/src/views/login/index.vue b/src/views/login/index.vue index 14857db..9bc7fef 100644 --- a/src/views/login/index.vue +++ b/src/views/login/index.vue @@ -43,11 +43,11 @@