.roadhogrc.mock.js 4.11 KB
Newer Older
1 2
import mockjs from 'mockjs';
import { getRule, postRule } from './mock/rule';
valleykid's avatar
valleykid committed
3
import { getActivities, getNotice, getFakeList, postFakeList } from './mock/api';
4 5
import { getFakeChartData } from './mock/chart';
import { imgMap } from './mock/utils';
ddcat1115's avatar
ddcat1115 committed
6 7
import { getProfileBasicData } from './mock/profile';
import { getProfileAdvancedData } from './mock/profile';
8 9
import { getNotices } from './mock/notices';
import { format, delay } from 'roadhog-api-doc';
陈帅's avatar
陈帅 committed
10
import { getProvince, getCity, getArea } from './mock/geographic/geographic';
11

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

nikogu's avatar
nikogu committed
15
// 代码中会兼容本地 service mock 以及部署站点的静态数据
16 17 18
const proxy = {
  // 支持值为 Object 和 Array
  'GET /api/currentUser': {
陈帅's avatar
陈帅 committed
19
    $desc: '获取当前用户接口',
20 21 22 23 24 25 26
    $params: {
      pageSize: {
        desc: '分页',
        exp: 2,
      },
    },
    $body: {
afc163's avatar
afc163 committed
27
      name: 'Serati Ma',
陈帅's avatar
陈帅 committed
28 29
      avatar:
        'https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png',
30
      userid: '00000001',
陈帅's avatar
陈帅 committed
31 32
      email: 'antdesign@alipay.com',
      profile: '简单的介绍下自己',
33
      notifyCount: 12,
陈帅's avatar
陈帅 committed
34 35 36 37 38 39 40 41 42 43 44 45 46
      country: 'China',
      geographic: {
        province: {
          label: '浙江省',
          key: '330000',
        },
        city: {
          label: '杭州市',
          key: '330100',
        },
      },
      address: '西湖区工专路 77 号',
      phone: '0752-268888888',
47 48 49
    },
  },
  // GET POST 可省略
陈帅's avatar
陈帅 committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
  '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',
    },
  ],
70 71 72 73 74 75 76 77 78 79 80 81 82
  '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
83
    res.send({ message: 'Ok' });
84 85
  },
  'GET /api/tags': mockjs.mock({
陈帅's avatar
陈帅 committed
86
    'list|100': [{ name: '@city', 'value|1-100': 150, 'type|0-2': 1 }],
87 88
  }),
  'GET /api/fake_list': getFakeList,
valleykid's avatar
valleykid committed
89
  'POST /api/fake_list': postFakeList,
90
  'GET /api/fake_chart_data': getFakeChartData,
ddcat1115's avatar
ddcat1115 committed
91 92
  'GET /api/profile/basic': getProfileBasicData,
  'GET /api/profile/advanced': getProfileAdvancedData,
93
  'POST /api/login/account': (req, res) => {
afc163's avatar
afc163 committed
94
    const { password, userName, type } = req.body;
陈帅's avatar
陈帅 committed
95
    if (password === '888888' && userName === 'admin') {
ddcat1115's avatar
ddcat1115 committed
96 97 98
      res.send({
        status: 'ok',
        type,
陈帅's avatar
陈帅 committed
99
        currentAuthority: 'admin',
ddcat1115's avatar
ddcat1115 committed
100
      });
陈帅's avatar
陈帅 committed
101
      return;
ddcat1115's avatar
ddcat1115 committed
102
    }
陈帅's avatar
陈帅 committed
103
    if (password === '123456' && userName === 'user') {
ddcat1115's avatar
ddcat1115 committed
104 105 106
      res.send({
        status: 'ok',
        type,
陈帅's avatar
陈帅 committed
107
        currentAuthority: 'user',
ddcat1115's avatar
ddcat1115 committed
108
      });
陈帅's avatar
陈帅 committed
109
      return;
ddcat1115's avatar
ddcat1115 committed
110
    }
afc163's avatar
afc163 committed
111
    res.send({
ddcat1115's avatar
ddcat1115 committed
112
      status: 'error',
afc163's avatar
afc163 committed
113
      type,
陈帅's avatar
陈帅 committed
114
      currentAuthority: 'guest',
afc163's avatar
afc163 committed
115
    });
116 117
  },
  'POST /api/register': (req, res) => {
ddcat1115's avatar
ddcat1115 committed
118
    res.send({ status: 'ok', currentAuthority: 'user' });
119 120
  },
  'GET /api/notices': getNotices,
陈帅's avatar
陈帅 committed
121 122
  'GET /api/500': (req, res) => {
    res.status(500).send({
陈帅's avatar
陈帅 committed
123 124 125 126 127
      timestamp: 1513932555104,
      status: 500,
      error: 'error',
      message: 'error',
      path: '/base/category/list',
陈帅's avatar
陈帅 committed
128 129 130 131
    });
  },
  'GET /api/404': (req, res) => {
    res.status(404).send({
陈帅's avatar
陈帅 committed
132 133 134 135 136
      timestamp: 1513932643431,
      status: 404,
      error: 'Not Found',
      message: 'No message available',
      path: '/base/category/list/2121212',
陈帅's avatar
陈帅 committed
137 138 139 140
    });
  },
  'GET /api/403': (req, res) => {
    res.status(403).send({
陈帅's avatar
陈帅 committed
141 142 143 144 145
      timestamp: 1513932555104,
      status: 403,
      error: 'Unauthorized',
      message: 'Unauthorized',
      path: '/base/category/list',
陈帅's avatar
陈帅 committed
146 147
    });
  },
148 149
  'GET /api/401': (req, res) => {
    res.status(401).send({
陈帅's avatar
陈帅 committed
150 151 152 153 154
      timestamp: 1513932555104,
      status: 401,
      error: 'Unauthorized',
      message: 'Unauthorized',
      path: '/base/category/list',
155 156
    });
  },
陈帅's avatar
陈帅 committed
157 158
  'GET /api/geographic/province': getProvince,
  'GET /api/geographic/city/:province': getCity,
159 160
};

陈帅's avatar
陈帅 committed
161
export default (noProxy ? {} : delay(proxy, 1000));