1、安装
npm install mockjs
2、创建文件
src/mock/api、src/mock/index
import Mock from 'mockjs' import MockApi from "./api" Mock.setup({ timeout: "300", }); const mocks = [...MockApi]; export function mockXHR() { for (let i of mocks) { Mock.mock(new RegExp(i.url), i.type || "get", i.response); } }
import Mock from "mockjs"; //处理路径传参 import { parameteUrl } from "@/utils/query-param" function ArrSet(Arr: any[], id: string): any[] { let obj: any = {} const arrays = Arr.reduce((setArr, item) => { obj[item[id]] ? '' : (obj[item[id]] = true && setArr.push(item)) return setArr }, []) return arrays } /** * @description: min ≤ r ≤ max 随机数 * @param {*} Min * @param {*} Max * @return {*} */ function RandomNumBoth(Min: any, Max: any) { var Range = Max - Min; var Rand = Math.random(); var num = Min + Math.round(Rand * Range); //四舍五入 return num; } //左中 export default [ { url: "/bigscreen/countUserNum", type: "get", response: () => { const a = Mock.mock({ success: true, data: { offlineNum: '@integer(50, 100)', alarmNum: '@integer(20, 100)', lockNum: '@integer(10, 50)', totalNum: 368 } }) a.data.onlineNum = a.data.totalNum - a.data.offlineNum - a.data.lockNum - a.data.alarmNum return a }, }, { url: "/bigscreen/countDeviceNum", type: "get", response: () => { const a = Mock.mock({ success: true, data: { alarmNum: '@integer(100, 1000)', offlineNum: '@integer(0, 50)', totalNum: 698 } }) a.data.onlineNum = a.data.totalNum - a.data.offlineNum return a } }, //左下 { url: "/bigscreen/leftBottom", type: "get", response: () => { const a = Mock.mock({ success: true, data: { "list|20": [ { provinceName: "@province()", cityName: '@city()', countyName: "@county()", createTime: "@datetime('yyyy-MM-dd HH:mm:ss')", deviceId: "6c512d754bbcd6d7cd86abce0e0cac58", "gatewayno|+1": 10000, "onlineState|1": [0, 1], } ] } }) return a } }, //右上 { url: "/bigscreen/alarmNum", type: "get", response: () => { const a = Mock.mock({ success: true, data: { dateList: ['2021-11', '2021-12', '2022-01', '2022-02', '2022-03', "2022-04"], "numList|6": [ '@integer(0, 1000)' ], "numList2|6": [ '@integer(0, 1000)' ] } }) return a } }, //右中 { url: "/bigscreen/ranking", type: "get", response: () => { let num = Mock.mock({ "list|80": [{ value: "@integer(50,1000)", name: "@city()" }] }).list // console.log("ranking",num); let newNum: any = [], numObj: any = {} num.map((item: any) => { if (!numObj[item.name] && newNum.length < 8) { numObj[item.name] = true newNum.push(item) } }) let arr = newNum.sort((a: any, b: any) => { return b.value - a.value }) let a = { success: true, data: arr } return a } }, //右下 { url: "/bigscreen/rightBottom", type: "get", response: () => { const a = Mock.mock({ success: true, data: { "list|40": [{ alertdetail: "@csentence(5,10)", "alertname|1": ["水浸告警", "各种报警"], alertvalue: "@float(60, 200)", createtime: "2022-04-19 08:38:33", deviceid: null, "gatewayno|+1": 10000, phase: "A1", sbInfo: "@csentence(10,18)", "terminalno|+1": 100, provinceName: "@province()", cityName: '@city()', countyName: "@county()", }], } }) return a } }, //安装计划 { url: "/bigscreen/installationPlan", type: "get", response: () => { let num = RandomNumBoth(26, 32); const a = Mock.mock({ ["category|" + num]: ["@city()"], ["barData|" + num]: ["@integer(10, 100)"], }) let lineData = [], rateData = []; for (let index = 0; index < num; index++) { let lineNum = Mock.mock('@integer(0, 100)') + a.barData[index] lineData.push(lineNum) let rate = a.barData[index] / lineNum; rateData.push((rate * 100).toFixed(0)) } a.lineData = lineData a.rateData = rateData return { success: true, data: a } } }, { url: "/bigscreen/centerMap", type: "get", response: (options: any) => { let params = parameteUrl(options.url) //不是中国的时候 if (params.regionCode && !["china"].includes(params.regionCode)) { const a = Mock.mock({ success: true, data: { "dataList|100": [ { name: "@city()", value: '@integer(1, 1000)' } ], regionCode: params.regionCode,//-代表中国 } }) return a } else { const a = Mock.mock({ success: true, data: { "dataList|12": [ { name: "@province()", value: '@integer(1, 1100)' } ], regionCode: 'china', } }) // 去重 a.data.dataList = ArrSet(a.data.dataList, "name") return a } } } ];
/** * @description: 获取路径参数 * @param {*} url * @return {*} */ export function parameteUrl(url: string) { var json: any = {} if (/\?/.test(url)) { var urlString = url.substring(url.indexOf("?") + 1); var urlArray = urlString.split("&"); for (var i = 0; i < urlArray.length; i++) { var urlItem = urlArray[i]; var item = urlItem.split("="); // console.log(item); json[item[0]] = item[1]; } return json; } return {}; } // get请求从config.url获取参数,post从config.body中获取参数 export function paramObj(url:string) { const search = url.split('?')[1] if (!search) { return {} } return JSON.parse( '{"' + decodeURIComponent(search) .replace(/"/g, '\\"') .replace(/&/g, '","') .replace(/=/g, '":"') + '"}' ) }
配置
src/main.js
//不使用mock 请注释掉 import { mockXHR } from "@/mock/index"; mockXHR()
vue.config.js
devServer: { host: '0.0.0.0', port: 8112, open: false, }
axios调用
baseUrl: ""
baseUrl: "/api"
标签:return,接入,url,Mock,vue2,integer,mockJs,data,mock From: https://www.cnblogs.com/Nyan-Workflow-FC/p/17584089.html