.roadhogrc.mock.js 3.47 KB
Newer Older
1 2 3 4
import mockjs from 'mockjs';
import { getRule, postRule } from './mock/rule';
import { getActivities, getNotice, getFakeList } from './mock/api';
import { getFakeChartData } from './mock/chart';
ddcat1115's avatar
ddcat1115 committed
5 6
import { getProfileBasicData } from './mock/profile';
import { getProfileAdvancedData } from './mock/profile';
7 8 9
import { getNotices } from './mock/notices';
import { format, delay } from 'roadhog-api-doc';

nikogu's avatar
nikogu committed
10 11
// 是否禁用代理
const noProxy = process.env.NO_PROXY === 'true';
12

nikogu's avatar
nikogu committed
13
// 代码中会兼容本地 service mock 以及部署站点的静态数据
14 15 16
const proxy = {
  // 支持值为 Object 和 Array
  'GET /api/currentUser': {
jim's avatar
jim committed
17
    $desc: '获取当前用户接口',
18 19 20 21 22 23 24
    $params: {
      pageSize: {
        desc: '分页',
        exp: 2,
      },
    },
    $body: {
afc163's avatar
afc163 committed
25
      name: 'Serati Ma',
afc163's avatar
afc163 committed
26
      avatar: 'https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png',
27 28 29 30 31
      userid: '00000001',
      notifyCount: 12,
    },
  },
  // GET POST 可省略
jim's avatar
jim committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
  'GET /api/users': [
    {
      key: '1',
      name: 'John Brown',
      age: 32,
      address: 'New York No. 1 Lake Park',
    },
    {
      key: '2',
      name: 'Jim Green',
      age: 42,
      address: 'London No. 1 Lake Park',
    },
    {
      key: '3',
      name: 'Joe Black',
      age: 32,
      address: 'Sidney No. 1 Lake Park',
    },
  ],
52 53 54 55 56 57 58 59 60 61 62 63 64
  '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/forms': (req, res) => {
afc163's avatar
afc163 committed
65
    res.send({ message: 'Ok' });
66 67
  },
  'GET /api/tags': mockjs.mock({
jim's avatar
jim committed
68
    'list|100': [{ name: '@city', 'value|1-100': 150, 'type|0-2': 1 }],
69 70 71
  }),
  'GET /api/fake_list': getFakeList,
  'GET /api/fake_chart_data': getFakeChartData,
ddcat1115's avatar
ddcat1115 committed
72 73
  'GET /api/profile/basic': getProfileBasicData,
  'GET /api/profile/advanced': getProfileAdvancedData,
74
  'POST /api/login/account': (req, res) => {
afc163's avatar
afc163 committed
75
    const { password, userName, type } = req.body;
jim's avatar
jim committed
76
    if (password === '888888' && userName === 'admin') {
ddcat1115's avatar
ddcat1115 committed
77 78 79
      res.send({
        status: 'ok',
        type,
jim's avatar
jim committed
80
        currentAuthority: 'admin',
ddcat1115's avatar
ddcat1115 committed
81
      });
jim's avatar
jim committed
82
      return;
ddcat1115's avatar
ddcat1115 committed
83
    }
jim's avatar
jim committed
84
    if (password === '123456' && userName === 'user') {
ddcat1115's avatar
ddcat1115 committed
85 86 87
      res.send({
        status: 'ok',
        type,
jim's avatar
jim committed
88
        currentAuthority: 'user',
ddcat1115's avatar
ddcat1115 committed
89
      });
jim's avatar
jim committed
90
      return;
ddcat1115's avatar
ddcat1115 committed
91
    }
afc163's avatar
afc163 committed
92
    res.send({
ddcat1115's avatar
ddcat1115 committed
93
      status: 'error',
afc163's avatar
afc163 committed
94
      type,
jim's avatar
jim committed
95
      currentAuthority: 'guest',
afc163's avatar
afc163 committed
96
    });
97 98
  },
  'POST /api/register': (req, res) => {
ddcat1115's avatar
ddcat1115 committed
99
    res.send({ status: 'ok', currentAuthority: 'user' });
100 101
  },
  'GET /api/notices': getNotices,
陈帅's avatar
陈帅 committed
102 103
  'GET /api/500': (req, res) => {
    res.status(500).send({
jim's avatar
jim committed
104 105 106 107 108
      timestamp: 1513932555104,
      status: 500,
      error: 'error',
      message: 'error',
      path: '/base/category/list',
陈帅's avatar
陈帅 committed
109 110 111 112
    });
  },
  'GET /api/404': (req, res) => {
    res.status(404).send({
jim's avatar
jim committed
113 114 115 116 117
      timestamp: 1513932643431,
      status: 404,
      error: 'Not Found',
      message: 'No message available',
      path: '/base/category/list/2121212',
陈帅's avatar
陈帅 committed
118 119 120 121
    });
  },
  'GET /api/403': (req, res) => {
    res.status(403).send({
jim's avatar
jim committed
122 123 124 125 126
      timestamp: 1513932555104,
      status: 403,
      error: 'Unauthorized',
      message: 'Unauthorized',
      path: '/base/category/list',
陈帅's avatar
陈帅 committed
127 128
    });
  },
129 130
  'GET /api/401': (req, res) => {
    res.status(401).send({
jim's avatar
jim committed
131 132 133 134 135
      timestamp: 1513932555104,
      status: 401,
      error: 'Unauthorized',
      message: 'Unauthorized',
      path: '/base/category/list',
136 137
    });
  },
138 139
};

jim's avatar
jim committed
140
export default (noProxy ? {} : delay(proxy, 1000));