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

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

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

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