refactor: refactor api usage

This commit is contained in:
张传龙 2022-07-25 18:36:22 +08:00
parent e5768fa1e3
commit 7762e02b31
11 changed files with 34 additions and 101 deletions

View File

@ -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',
})
}

6
src/api/index.js Normal file
View File

@ -0,0 +1,6 @@
import request from '@/utils/http'
export default {
getUser: () => request.get('/user'),
refreshToken: () => request.post('/auth/refreshToken'),
}

View File

@ -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',
})
}

View File

@ -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,
})
}

View File

@ -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 }

View File

@ -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,
})

View File

@ -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)
}

View File

@ -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}`),
}

View File

@ -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
}

5
src/views/login/api.js Normal file
View File

@ -0,0 +1,5 @@
import request from '@/utils/http'
export default {
login: (data) => request.post('/auth/login', data),
}

View File

@ -43,11 +43,11 @@
</template>
<script setup>
import { login } from '@/api/auth'
import { lStorage } from '@/utils/cache'
import { setToken } from '@/utils/token'
import { useStorage } from '@vueuse/core'
import bgImg from '@/assets/images/login_bg.jpg'
import api from './api'
const title = import.meta.env.VITE_APP_TITLE
@ -80,7 +80,7 @@ async function handleLogin() {
try {
$message.loading('正在验证...')
loging.value = true
const res = await login({ name, password: password.toString() })
const res = await api.login({ name, password: password.toString() })
if (res.code === 0) {
$message.success('登录成功')
setToken(res.data.token)