重构接口请求,统一管理接口请求模块

This commit is contained in:
Hooray Hu 2023-04-22 01:08:02 +08:00
parent f0b1551530
commit 97edf77ab7
6 changed files with 55 additions and 32 deletions

13
src/api/modules/app.ts Normal file
View File

@ -0,0 +1,13 @@
import api from '../index'
export default {
// 后端获取路由数据
routeList: () => api.get('mock/app/route/list', {
baseURL: '/mock/',
}),
// 基于文件系统路由模式下,后端获取导航菜单数据
menuList: () => api.get('mock/app/menu/list', {
baseURL: '/mock/',
}),
}

24
src/api/modules/user.ts Normal file
View File

@ -0,0 +1,24 @@
import api from '../index'
export default {
// 登录
login: (data: {
account: string
password: string
}) => api.post('user/login', data, {
baseURL: '/mock/',
}),
// 获取权限
permission: () => api.get('user/permission', {
baseURL: '/mock/',
}),
// 修改密码
passwordEdit: (data: {
password: string
newpassword: string
}) => api.post('user/password/edit', data, {
baseURL: '/mock/',
}),
}

View File

@ -1,25 +1,25 @@
export default [
{
url: '/mock/member/login',
url: '/mock/user/login',
method: 'post',
response: (option: any) => {
response: ({ body }: any) => {
return {
error: '',
status: 1,
data: {
account: option.body.account,
token: `${option.body.account}_@string`,
account: body.account,
token: `${body.account}_@string`,
failure_time: Math.ceil(new Date().getTime() / 1000) + 24 * 60 * 60,
},
}
},
},
{
url: '/mock/member/permission',
url: '/mock/user/permission',
method: 'get',
response: (option: any) => {
response: ({ headers }: any) => {
let permissions: string[] = []
if (option.headers.token.indexOf('admin') === 0) {
if (headers.token.indexOf('admin') === 0) {
permissions = [
'permission.browse',
'permission.create',
@ -27,7 +27,7 @@ export default [
'permission.remove',
]
}
else if (option.headers.token.indexOf('test') === 0) {
else if (headers.token.indexOf('test') === 0) {
permissions = [
'permission.browse',
]
@ -42,7 +42,7 @@ export default [
},
},
{
url: '/mock/member/edit/password',
url: '/mock/user/password/edit',
method: 'post',
response: {
error: '',

View File

@ -2,11 +2,10 @@ import { cloneDeep } from 'lodash-es'
import useSettingsStore from './settings'
import useUserStore from './user'
import useRouteStore from './route'
import type { Menu } from '#/global'
import { resolveRoutePath } from '@/utils'
import api from '@/api'
import apiApp from '@/api/modules/app'
import menu from '@/menu'
import type { Menu } from '#/global'
const useMenuStore = defineStore(
// 唯一ID
@ -147,9 +146,7 @@ const useMenuStore = defineStore(
}
// 生成导航(后端生成)
async function generateMenusAtBack() {
await api.get('menu/list', {
baseURL: '/mock/',
}).then(async (res) => {
await apiApp.menuList().then(async (res) => {
let accessedMenus: Menu.recordMainRaw[]
// 如果权限功能开启,则需要对导航数据进行筛选过滤
if (settingsStore.settings.app.enablePermission) {

View File

@ -2,9 +2,9 @@ import { cloneDeep } from 'lodash-es'
import type { RouteMeta, RouteRecordRaw } from 'vue-router'
import useSettingsStore from './settings'
import useUserStore from './user'
import api from '@/api'
import { resolveRoutePath } from '@/utils'
import { systemRoutes } from '@/router/routes'
import apiApp from '@/api/modules/app'
import type { Route } from '#/global'
const useRouteStore = defineStore(
@ -196,9 +196,7 @@ const useRouteStore = defineStore(
}
// 根据权限动态生成路由(后端获取)
async function generateRoutesAtBack() {
await api.get('route/list', {
baseURL: '/mock/',
}).then(async (res) => {
await apiApp.routeList().then(async (res) => {
// 设置 routes 数据
routesRaw.value = formatBackRoutes(res.data)
if (settingsStore.settings.app.enablePermission) {

View File

@ -1,6 +1,6 @@
import useRouteStore from './route'
import useMenuStore from './menu'
import api from '@/api'
import apiUser from '@/api/modules/user'
const useUserStore = defineStore(
// 唯一ID
@ -29,9 +29,7 @@ const useUserStore = defineStore(
password: string
}) {
// 通过 mock 进行登录
const res = await api.post('member/login', data, {
baseURL: '/mock/',
})
const res = await apiUser.login(data)
localStorage.setItem('account', res.data.account)
localStorage.setItem('token', res.data.token)
localStorage.setItem('failure_time', res.data.failure_time)
@ -53,9 +51,7 @@ const useUserStore = defineStore(
// 获取我的权限
async function getPermissions() {
// 通过 mock 获取权限
const res = await api.get('member/permission', {
baseURL: '/mock/',
})
const res = await apiUser.permission()
permissions.value = res.data.permissions
return permissions.value
}
@ -64,12 +60,7 @@ const useUserStore = defineStore(
password: string
newpassword: string
}) {
await api.post('member/edit/password', {
password: data.password,
newpassword: data.newpassword,
}, {
baseURL: '/mock/',
})
await apiUser.passwordEdit(data)
}
return {