index.js 3.9 KB
Newer Older
vipcxj's avatar
vipcxj committed
1 2
/* eslint-disable no-shadow */
import moment from 'moment';
3 4 5
import { genModules } from '../src/mock/modules';
import { getTasks } from '../src/mock/tasks';
import toFilters from '../src/mock/filter';
vipcxj's avatar
vipcxj committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

const modules = genModules();
const tasks = getTasks();

const domains = [
  {
    id: 1,
    name: '虚拟基地01',
  },
  {
    id: 2,
    name: '虚拟基地02',
  },
  {
    id: 3,
    name: '虚拟基地03',
  },
  {
    id: 4,
    name: '虚拟基地04',
  },
  {
    id: 5,
    name: '虚拟基地05',
  },
];

const getDomain = (id) => {
  const domain = domains.filter(domain => domain.id === id);
  return domain ? domain[0] : null;
};

const dealWithData = (req) => {
vipcxj's avatar
vipcxj committed
39
  const { sort, order } = req.query;
vipcxj's avatar
vipcxj committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  const filters = [];
  for (const queryKey in req.query) {
    if (queryKey.indexOf('f-') === 0) {
      filters.push(toFilters(queryKey, req.query[queryKey]));
    }
  }
  let data = tasks.filter((value) => {
    return filters.map(filter => filter(value)).reduce((ret, cur) => ret && cur, true);
  });
  if (sort) {
    data = data.sort((a, b) => {
      const va = a[sort];
      const vb = b[sort];
      if (order === 'desc') {
        if (moment.isMoment(va) || moment.isDate(va) || moment.isMoment(vb) || moment.isDate(vb)) {
          if (moment(va).isAfter(moment(vb))) {
            return 1;
          } else if (moment(va).isSame(moment(vb))) {
            return 0;
          } else {
            return -1;
          }
        } else if (va > vb) {
          return 1;
        } else if (va === vb) {
          return 0;
        } else {
          return -1;
        }
      } else if (moment.isMoment(va) || moment.isDate(va) || moment.isMoment(vb) || moment.isDate(vb)) {
        if (moment(va).isBefore(moment(vb))) {
          return 1;
        } else if (moment(va).isSame(moment(vb))) {
          return 0;
        } else {
          return -1;
        }
      } else if (va < vb) {
        return 1;
      } else if (va === vb) {
        return 0;
      } else {
        return -1;
      }
    });
  }
  return data;
};

let currentDomainId = null;

91 92 93 94 95 96 97
const wrapResponse = (response) => {
  return {
    errorCode: 0,
    data: response,
  };
};

vipcxj's avatar
vipcxj committed
98 99 100 101 102
module.exports = {

  '/api/user/logout': (req, res) => {
    res.status(204).end();
  },
103
  '/api/domain/all': wrapResponse(domains),
vipcxj's avatar
vipcxj committed
104
  '/api/domain/switch': (req, res) => {
vipcxj's avatar
vipcxj committed
105
    const { domainId } = req.query;
vipcxj's avatar
vipcxj committed
106 107 108 109
    const intDomainId = parseInt(domainId, 10);
    const domainIds = domains.map(domain => domain.id);
    if (domainIds.indexOf(intDomainId) !== -1) {
      if (currentDomainId) {
110
        res.send(wrapResponse(getDomain(currentDomainId)));
vipcxj's avatar
vipcxj committed
111 112 113 114 115 116 117 118 119 120 121 122
      } else {
        res.status(204).end();
      }
      currentDomainId = intDomainId;
    } else {
      res.status(500).send({
        errorCode: 0x00010010,
        message: '无效的项目ID。',
      });
    }
  },
  '/api/domain/current': (req, res) => {
123
    res.send(wrapResponse(getDomain(currentDomainId)));
vipcxj's avatar
vipcxj committed
124 125 126
  },
  '/api/module/all/info': (req, res) => {
    console.log('/api/module/all/info');
vipcxj's avatar
vipcxj committed
127
    const { all } = modules;
vipcxj's avatar
vipcxj committed
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    const publics = modules.public;
    const findModule = id => all.filter(m => m.id === id).pop();
    const fetchParent = (module) => {
      if (module.parent) {
        const parent = findModule(module.parent);
        return parent ? [parent, ...fetchParent(parent)] : [];
      } else {
        return [];
      }
    };
    const parents = new Set();
    publics.forEach((m) => {
      const p0 = fetchParent(m);
      p0.forEach((p) => {
        parents.add(p);
      });
    });
145
    res.send(wrapResponse([
vipcxj's avatar
vipcxj committed
146 147
      ...publics,
      ...parents,
148
    ]));
vipcxj's avatar
vipcxj committed
149 150
  },
  '/api/bpm/task/all/count': (req, res) => {
151
    res.send(wrapResponse(dealWithData(req).length));
vipcxj's avatar
vipcxj committed
152 153 154 155
  },
  '/api/bpm/task/all/info': (req, res) => {
    const pst = Number.parseInt(req.query.pst, 10);
    const psz = Number.parseInt(req.query.psz, 10);
156
    res.send(wrapResponse(dealWithData(req).slice(pst, pst + psz)));
vipcxj's avatar
vipcxj committed
157 158
  },
};