这篇文章记录了在 vben admin 框架中使用 mock 来模拟请求
提示:以下是本篇文章正文内容,下面案例可供参考
在 mock包中创建文件 \test\mock.ts
// \test\mock.ts
import { MockMethod } from 'vite-plugin-mock';
import { resultSuccess } from '../_util';
// 准备的模拟物联网卡数据
const simApiMockData = {
code: 0,
result: {
list: [
{
id: '1555042387559780352',
deviceId: '2206101',
deviceName: '2222',
iccid: '89860483162090515058',
msisdn: '1440832155058',
imsi: '460080321505058',
state: { text: '停机', value: 'stop' },
fakeState: { text: '待激活', value: 'notActive' },
createTime: 1.659585971141e12,
activeDate: 1.6592832e12,
openDate: 1.6589376e12,
modifyTime: 1.659585971141e12,
},
{
id: '1562280375538700288',
deviceId: '2205909',
deviceName: '2205909',
iccid: '89860483162090515069',
msisdn: '1440832155069',
imsi: '460080321505069',
state: { text: '已激活', value: 'activated' },
fakeState: { text: '待激活', value: 'notActive' },
createTime: 1.661311641949e12,
activeDate: 1.6596288e12,
openDate: 1.6589376e12,
modifyTime: 1.661311641949e12,
},
{
id: '1565246034465939456',
deviceId: '2209002',
deviceName: '2209002',
iccid: '89860483162090515062',
msisdn: '1440832155062',
imsi: '460080321505062',
state: { text: '已激活', value: 'activated' },
fakeState: { text: '待激活', value: 'notActive' },
createTime: 1.662018710137e12,
activeDate: 1.6614432e12,
openDate: 1.6589376e12,
modifyTime: 1.662018710137e12,
},
{
id: '1582305527906983936',
deviceId: '2208101',
deviceName: '2208101',
iccid: 'RS232_NET',
imsi: 'RS232_NET',
state: { text: '待激活', value: 'notActive' },
fakeState: { text: '待激活', value: 'notActive' },
createTime: 1.666086010323e12,
modifyTime: 1.666086010323e12,
},
{
id: '1582546476625059840',
deviceId: '2208102',
deviceName: 'AAA-网线',
iccid: 'RS232_NET',
imsi: 'RS232_NET',
state: { text: '待激活', value: 'notActive' },
fakeState: { text: '待激活', value: 'notActive' },
createTime: 1.666143456976e12,
modifyTime: 1.666143456976e12,
},
],
count: 5,
pageNo: 1,
pageSize: 20,
},
message: 'success',
};
// 准备的模拟数据
const roleManagementData = {
code: 0,
result: {
list: [
{
createTime: 1609837428000,
updateTime: 1645477700000,
creator: 'admin',
updater: '',
deleted: false,
tenantId: '1',
id: '2',
name: '普通角色',
code: 'common',
sort: 2,
status: 0,
type: 1,
remark: '普通角色',
dataScope: 2,
},
{
createTime: 1609912175000,
updateTime: 1653725465000,
creator: '',
updater: '1',
deleted: false,
tenantId: '1',
id: '101',
name: '测试账号',
code: 'test',
sort: 0,
status: 0,
type: 2,
remark: '132',
dataScope: 1,
dataScopeDeptIds: [],
},
{
createTime: 1609837428000,
updateTime: 1645477701000,
creator: 'admin',
updater: '',
deleted: false,
tenantId: '1',
id: '1',
name: '超级管理员',
code: 'super_admin',
sort: 1,
status: 0,
type: 1,
remark: '超级管理员',
dataScope: 1,
},
],
count: 3,
pageNo: 1,
pageSize: 20,
},
message: '操作成功',
};
// 准备接口
export default [
{
url: '/basic-api/v1/device/sim/_query',
timeout: 1000,
method: 'post',
response: () => {
return resultSuccess(simApiMockData);
},
},
{
url: '/basic-api/system/role/page',
timeout: 1000,
method: 'get',
response: () => {
return resultSuccess(roleManagementData);
},
},
] as MockMethod[];
在 src\api 中创建文件 test\mock.ts
import { defHttp } from '/@/utils/http/axios';
import { BasicModel, Page } from '/@/api/model/baseModel';
enum Api {
SIM_DATA_LIST = '/v1/device/sim/_query',
ROLE_MANAGEMENT_DATA_LIST = '/system/role/page',
}
export interface SimCard extends BasicModel<SimCard> {
configName?: string; // 名称
configKey?: string; // 参数键
configValue?: string; // 参数值
isSys?: string; // 系统内置(1是 0否)
}
//获取sim卡信息的接口
export const getSimDataList = (params?: SimCard | any) =>
defHttp.postJson<Page<SimCard>>({
url: Api.SIM_DATA_LIST,
params,
});
//获取角色管理信息的接口
export const getRoleManagementDataList = (params?: SimCard | any) =>
defHttp.get<Page<SimCard>>({
url: Api.ROLE_MANAGEMENT_DATA_LIST,
params,
});
这里做测试,用一个 button 按钮来获取数据好了
<template>
<a-button type="primary" @click="getData1()"> 获取数据 a-button>
<a-button type="primary" @click="getData2()"> 获取数据 a-button>
template>
<script lang="ts">
import { defineComponent } from 'vue';
import { getSimDataList, getRoleManagementDataList } from '/@/api/test/mock';
export default defineComponent({
name: 'MockTest',
});
script>
<script lang="ts" setup>
function getData1() {
getSimDataList().then((res) => {
console.log('get data here');
console.log(res);
});
}
function getData2() {
getRoleManagementDataList().then((res) => {
console.log('get data here');
console.log(res);
});
}
script>
控制台输出
实际项目当中我们可能只需要响应的数据中的一部分
比如
用数据的时候可能只想要数据中的 list 部分,如果后台返回数据的格式一致
这时候可以对返回的数据进行处理
src\utils\http\axios\index.ts 中当请求成功之后,对返回的数据处理后返回处理好的数据,根据自己实际后台返回数据的格式来修改处理规则