Advertisement
Guest User

Untitled

a guest
May 10th, 2018
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import mockjs from 'mockjs';
  2. import { getRule, postRule } from './mock/rule';
  3. import { getActivities, getNotice, getFakeList } from './mock/api';
  4. import { getFakeChartData } from './mock/chart';
  5. import { getProfileBasicData } from './mock/profile';
  6. import { getProfileAdvancedData } from './mock/profile';
  7. import { getNotices } from './mock/notices';
  8. import { format, delay } from 'roadhog-api-doc';
  9.  
  10. // 是否禁用代理
  11. const noProxy = process.env.NO_PROXY === 'true';
  12.  
  13. // 代码中会兼容本地 service mock 以及部署站点的静态数据
  14. const proxy = {
  15.   // 支持值为 Object 和 Array
  16.   'GET /api/currentUser': {
  17.     $desc: '获取当前用户接口',
  18.     $params: {
  19.       pageSize: {
  20.         desc: '分页',
  21.         exp: 2,
  22.       },
  23.     },
  24.     $body: {
  25.       name: 'Serati Ma',
  26.       avatar: 'https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png',
  27.       userid: '00000001',
  28.       notifyCount: 12,
  29.     },
  30.   },
  31.   // GET POST 可省略
  32.   'GET /api/users': [
  33.     {
  34.       key: '1',
  35.       name: 'John Brown',
  36.       age: 32,
  37.       address: 'New York No. 1 Lake Park',
  38.     },
  39.     {
  40.       key: '2',
  41.       name: 'Jim Green',
  42.       age: 42,
  43.       address: 'London No. 1 Lake Park',
  44.     },
  45.     {
  46.       key: '3',
  47.       name: 'Joe Black',
  48.       age: 32,
  49.       address: 'Sidney No. 1 Lake Park',
  50.     },
  51.   ],
  52.   'GET /api/project/notice': getNotice,
  53.   'GET /api/activities': getActivities,
  54.   'GET /api/rule': getRule,
  55.   'POST /api/rule': {
  56.     $params: {
  57.       pageSize: {
  58.         desc: '分页',
  59.         exp: 2,
  60.       },
  61.     },
  62.     $body: postRule,
  63.   },
  64.   'POST /api/forms': (req, res) => {
  65.     res.send({ message: 'Ok' });
  66.   },
  67.   'GET /api/tags': mockjs.mock({
  68.     'list|100': [{ name: '@city', 'value|1-100': 150, 'type|0-2': 1 }],
  69.   }),
  70.   'GET /api/fake_list': getFakeList,
  71.   'GET /api/fake_chart_data': getFakeChartData,
  72.   'GET /api/profile/basic': getProfileBasicData,
  73.   'GET /api/profile/advanced': getProfileAdvancedData,
  74.   'POST /api/login/account': (req, res) => {
  75.     const { password, userName, type } = req.body;
  76.     if (password === '888888' && userName === 'admin') {
  77.       res.send({
  78.         status: 'ok',
  79.         type,
  80.         currentAuthority: 'admin',
  81.       });
  82.       return;
  83.     }
  84.     if (password === '123456' && userName === 'user') {
  85.       res.send({
  86.         status: 'ok',
  87.         type,
  88.         currentAuthority: 'user',
  89.       });
  90.       return;
  91.     }
  92.     res.send({
  93.       status: 'error',
  94.       type,
  95.       currentAuthority: 'guest',
  96.     });
  97.   },
  98.   'POST /api/register': (req, res) => {
  99.     res.send({ status: 'ok', currentAuthority: 'user' });
  100.   },
  101.   'GET /api/notices': getNotices,
  102.   'GET /api/500': (req, res) => {
  103.     res.status(500).send({
  104.       timestamp: 1513932555104,
  105.       status: 500,
  106.       error: 'error',
  107.       message: 'error',
  108.       path: '/base/category/list',
  109.     });
  110.   },
  111.   'GET /api/404': (req, res) => {
  112.     res.status(404).send({
  113.       timestamp: 1513932643431,
  114.       status: 404,
  115.       error: 'Not Found',
  116.       message: 'No message available',
  117.       path: '/base/category/list/2121212',
  118.     });
  119.   },
  120.   'GET /api/403': (req, res) => {
  121.     res.status(403).send({
  122.       timestamp: 1513932555104,
  123.       status: 403,
  124.       error: 'Unauthorized',
  125.       message: 'Unauthorized',
  126.       path: '/base/category/list',
  127.     });
  128.   },
  129.   'GET /api/401': (req, res) => {
  130.     res.status(401).send({
  131.       timestamp: 1513932555104,
  132.       status: 401,
  133.       error: 'Unauthorized',
  134.       message: 'Unauthorized',
  135.       path: '/base/category/list',
  136.     });
  137.   },
  138. };
  139.  
  140. export default (noProxy ? {} : delay(proxy, 1000));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement