Commit 518094b1 authored by 陈帅's avatar 陈帅

Perfect mock demo

parent a8ef08b6
......@@ -26,3 +26,5 @@ jsconfig.json
# visual studio code
.history
*.log
funtction/mock
\ No newline at end of file
......@@ -5,7 +5,6 @@ import { getFakeChartData } from './mock/chart';
import { getProfileBasicData } from './mock/profile';
import { getProfileAdvancedData } from './mock/profile';
import { getNotices } from './mock/notices';
import { format, delay } from 'roadhog-api-doc';
import { getProvince, getCity } from './mock/geographic';
// 是否禁用代理
......@@ -15,14 +14,6 @@ const noProxy = process.env.NO_PROXY === 'true';
const proxy = {
// 支持值为 Object 和 Array
'GET /api/currentUser': {
$desc: '获取当前用户接口',
$params: {
pageSize: {
desc: '分页',
exp: 2,
},
},
$body: {
name: 'Serati Ma',
avatar: 'https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png',
userid: '00000001',
......@@ -71,7 +62,6 @@ const proxy = {
address: '西湖区工专路 77 号',
phone: '0752-268888888',
},
},
// GET POST 可省略
'GET /api/users': [
{
......@@ -96,15 +86,7 @@ const proxy = {
'GET /api/project/notice': getNotice,
'GET /api/activities': getActivities,
'GET /api/rule': getRule,
'POST /api/rule': {
$params: {
pageSize: {
desc: '分页',
exp: 2,
},
},
$body: postRule,
},
'POST /api/rule': postRule,
'POST /api/forms': (req, res) => {
res.send({ message: 'Ok' });
},
......@@ -185,4 +167,4 @@ const proxy = {
'GET /api/captcha': getFakeCaptcha,
};
export default (noProxy ? {} : delay(proxy, 1000));
export default (noProxy ? {} : proxy);
......@@ -4,16 +4,11 @@ const express = require('express');
const mock = require('./mock/index');
const app = express();
const sendData = (body, req, res) => {
if (!body) {
res.send('test');
return '';
}
if ('$body' in body) {
res.send(body.$body);
return;
}
if (typeof body === 'function') {
body(req, res);
}
......@@ -21,10 +16,14 @@ const sendData = (body, req, res) => {
};
app.get('/api', (req, res) => {
const html = Object.keys(mock).map(url => {
return `<li><code>${url}</code></li>`;
const href = url.split(' /')[1];
return `<li><a href="${href}"><code>${url}</code></a></li>`;
});
res.send(`<ul>${html.join('')}</ul>`);
});
app.get('/', (req, res) => {
res.send(`<ul><li><a href="api/api"><code>/api</code></a></li></ul>`);
});
Object.keys(mock).forEach(url => {
const body = mock[url];
......
This diff is collapsed.
function getJson(infoType) {
const json = require(`${__dirname}/${infoType}.json`);
return JSON.parse(json);
}
export function getProvince(req, res) {
res.json(getJson('province'));
}
export function getCity(req, res) {
res.json(getJson('city')[req.params.province]);
}
export default {
getProvince,
getCity,
};
[
{
"name": "北京市",
"id": "110000"
},
{
"name": "天津市",
"id": "120000"
},
{
"name": "河北省",
"id": "130000"
},
{
"name": "山西省",
"id": "140000"
},
{
"name": "内蒙古自治区",
"id": "150000"
},
{
"name": "辽宁省",
"id": "210000"
},
{
"name": "吉林省",
"id": "220000"
},
{
"name": "黑龙江省",
"id": "230000"
},
{
"name": "上海市",
"id": "310000"
},
{
"name": "江苏省",
"id": "320000"
},
{
"name": "浙江省",
"id": "330000"
},
{
"name": "安徽省",
"id": "340000"
},
{
"name": "福建省",
"id": "350000"
},
{
"name": "江西省",
"id": "360000"
},
{
"name": "山东省",
"id": "370000"
},
{
"name": "河南省",
"id": "410000"
},
{
"name": "湖北省",
"id": "420000"
},
{
"name": "湖南省",
"id": "430000"
},
{
"name": "广东省",
"id": "440000"
},
{
"name": "广西壮族自治区",
"id": "450000"
},
{
"name": "海南省",
"id": "460000"
},
{
"name": "重庆市",
"id": "500000"
},
{
"name": "四川省",
"id": "510000"
},
{
"name": "贵州省",
"id": "520000"
},
{
"name": "云南省",
"id": "530000"
},
{
"name": "西藏自治区",
"id": "540000"
},
{
"name": "陕西省",
"id": "610000"
},
{
"name": "甘肃省",
"id": "620000"
},
{
"name": "青海省",
"id": "630000"
},
{
"name": "宁夏回族自治区",
"id": "640000"
},
{
"name": "新疆维吾尔自治区",
"id": "650000"
},
{
"name": "台湾省",
"id": "710000"
},
{
"name": "香港特别行政区",
"id": "810000"
},
{
"name": "澳门特别行政区",
"id": "820000"
}
]
This diff is collapsed.
......@@ -2,17 +2,17 @@
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"serve": "firebase serve --only functions",
"serve": "npm run mock && firebase serve --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
"logs": "firebase functions:log",
"mock":"cd .. && rollup -c"
},
"dependencies": {
"cors": "^2.8.1",
"express": "^4.16.3",
"firebase-admin": "^5.12.1",
"firebase-functions": "^1.0.3",
"firebase-functions": "^1.1.0",
"mockjs": "^1.0.1-beta3",
"moment": "^2.22.2"
},
......
function getJson(infoType) {
const json = require(`${__dirname}/geographic/${infoType}.json`); // eslint-disable-line
return json;
}
import city from './geographic/city.json';
import province from './geographic/province.json';
export function getProvince(req, res) {
res.json(getJson('province'));
res.json(province);
}
export function getCity(req, res) {
res.json(getJson('city')[req.params.province]);
res.json(city[req.params.province]);
}
export default {
......
......@@ -5,10 +5,10 @@
"private": true,
"scripts": {
"precommit": "npm run lint-staged",
"preinstall": "cd functions && npm install",
"start": "cross-env ESLINT=none roadhog dev",
"start:no-proxy": "cross-env NO_PROXY=true ESLINT=none roadhog dev",
"build": "cross-env ESLINT=none roadhog build",
"build-site": "roadhog-api-doc static",
"site": "npm run build && firebase deploy",
"analyze": "cross-env ANALYZE=1 roadhog build",
"lint:style": "stylelint \"src/**/*.less\" --syntax less",
......@@ -27,7 +27,6 @@
"bizcharts": "^3.1.10",
"bizcharts-plugin-slider": "^2.0.3",
"classnames": "^2.2.6",
"cors": "^2.8.4",
"dva": "^2.3.1",
"dva-loading": "^2.0.3",
"enquire-js": "^0.2.1",
......@@ -39,18 +38,20 @@
"path-to-regexp": "^2.1.0",
"prop-types": "^15.5.10",
"qs": "^6.5.0",
"rc-drawer": "^1.3.1",
"rc-drawer": "^1.4.4",
"react": "^16.4.0",
"react-container-query": "^0.11.0",
"react-document-title": "^2.0.3",
"react-dom": "^16.4.0",
"react-fittext": "^1.0.0",
"rollbar": "^2.3.4",
"rollup": "^0.62.0",
"rollup-plugin-json": "^3.0.0",
"url-polyfill": "^1.0.10"
},
"devDependencies": {
"@babel/polyfill": "^7.0.0-beta.36",
"@types/react": "^16.3.17",
"@types/react": "^16.4.4",
"@types/react-dom": "^16.0.6",
"antd-theme-webpack-plugin": "^1.0.8",
"babel-eslint": "^8.1.2",
......@@ -62,7 +63,7 @@
"cross-env": "^5.1.1",
"cross-port-killer": "^1.0.1",
"enzyme": "^3.1.0",
"eslint": "^5.0.0",
"eslint": "^5.0.1",
"eslint-config-airbnb": "^17.0.0",
"eslint-config-prettier": "^2.9.0",
"eslint-plugin-babel": "^5.1.0",
......@@ -75,12 +76,11 @@
"husky": "^0.14.3",
"lint-staged": "^7.2.0",
"mockjs": "^1.0.1-beta3",
"prettier": "1.13.5",
"prettier": "1.13.6",
"pro-download": "^1.0.1",
"redbox-react": "^1.5.0",
"regenerator-runtime": "^0.11.1",
"regenerator-runtime": "^0.12.0",
"roadhog": "^2.4.2",
"roadhog-api-doc": "^1.0.3",
"stylelint": "^9.2.1",
"stylelint-config-prettier": "^3.0.4",
"stylelint-config-standard": "^18.0.0"
......
import json from 'rollup-plugin-json';
// rollup.config.js
export default {
input: './.roadhogrc.mock.js',
output: {
file: './functions/mock/index.js',
format: 'umd',
name: 'mock',
},
plugins: [
json({
preferConst: true, // Default: false
indent: ' ',
}),
],
};
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment