refactor: refactor api usage
This commit is contained in:
13
src/views/examples/table/post/api.js
Normal file
13
src/views/examples/table/post/api.js
Normal 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}`),
|
||||
}
|
@@ -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
5
src/views/login/api.js
Normal file
@@ -0,0 +1,5 @@
|
||||
import request from '@/utils/http'
|
||||
|
||||
export default {
|
||||
login: (data) => request.post('/auth/login', data),
|
||||
}
|
@@ -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)
|
||||
|
Reference in New Issue
Block a user