1、导入mockjs
2、在main.js中引入mock
/**
* mock 测试
*/
const openMock = true
// const openMock = false
if (openMock) {
require(‘@/../test/mock‘)
}
/********************* mock/index.js*******************************/
import Mock from ‘mockjs‘
let baseUrl = window.baseUrl.cmd
let mockUrl = window.baseUrl.mock
// 1. 开启/关闭[所有模块]拦截, 通过调[openMock参数]设置.
// 2. 开启/关闭[业务模块]拦截, 通过调用fnCreate方法[isOpen参数]设置.
// 3. 开启/关闭[业务模块中某个请求]拦截, 通过函数返回对象中的[isOpen属性]设置.
let openMock = true
// // XX管理
// import routeConfig from ‘./modules/dev/routeConfig‘;
// fnCreate(routeConfig, openMock)
/**
* 创建mock模拟数据
* @param {*} mod 模块
* @param {*} isOpen 是否开启?
*/
function fnCreate (mod, isOpen = true) {
if (isOpen) {
for (let key in mod) {
((res) => {
let url = res.url
console.log(‘==========‘, url);
if (url.indexOf(mockUrl) === 0) {
url = url.replace(mockUrl, baseUrl)
}
Mock.mock(new RegExp(url), res.method, (opts) => {
// opts["data"] = opts;
// opts[‘data‘] = opts.body ? JSON.parse(opts.body) : null
// delete opts.body
console.log(‘\n‘)
console.log(‘%cmock拦截, 请求: ‘, ‘color:blue‘, opts)
let data = res.data(opts.body)
console.log(‘%cmock拦截, 响应: ‘, ‘color:blue‘, data)
return data
})
// eslint-disable-next-line func-call-spacing
})
// eslint-disable-next-line no-unexpected-multiline
(mod[key]() || {})
}
}
}
/************************************************************************************/
/**
* api接口的统一出口
*/
import * as apiUrl from "@/js/http/modules/res/apiUrlRes";
let baseUrl = window.baseUrl.mock
/**
* 设备树一级子节点列表
*/
function listDevTree() {
return {
url: baseUrl + apiUrl.devTree.listDevTreeUrl,
method: ‘post‘,
data: params => {
let paramsJson = null;
if (params) {
paramsJson = JSON.parse(params);
}
// console.log("dddddddd", paramsJson.parentId)
return getListDevTree(paramsJson.parentId)
}
}
}
function getListDevTree(parentId) {
const rootMockData = {
"code": 0,
"data": [{
"createTime": 1610001483000,
"id": 1,
"lastUpdateTime": 1610001483000,
"leaf": false,
"nodeName": "ROOT",
"nodeType": 0,
"num": 2,
"parentId": 0,
"icon":"el-icon-info"
}],
"fail": false,
"msg": "SUCCESS"
};
const childMockData = {
"code": 0,
"data": [{
"createTime": 1610001593000,
"id": 2,
"lastUpdateTime": 1610001593000,
"leaf": false,
"nodeName": "Test_0",
"nodeType": 0,
"num": 3,
"parentId": 1,
"icon":"el-icon-info"
},
{
"createTime": 1610001593000,
"id": 3,
"lastUpdateTime": 1610001593000,
"leaf": true,
"nodeName": "Test_333",
"nodeType": 0,
"num": 0,
"parentId": 1,
"icon":"el-icon-info"
}],
"fail": false,
"msg": "SUCCESS"
};
let mockData = null;
if (parentId === 0) {
mockData = rootMockData;
} else {
mockData = childMockData;
}
return mockData;
}
/**
* 增加节点
* @returns
*/
function addNode() {
const mockData = {"code":0,"data":33,"msg":"操作成功"};
return {
url: baseUrl + apiUrl.devTree.addNodeUrl,
method: ‘post‘,
data:()=>{
return mockData;
}
}
}