business.js 2.23 KB
Newer Older
duanledexianxianxian's avatar
duanledexianxianxian committed
1 2 3 4 5 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 39 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
import * as service from '../services/business';

export default {
  namespace: 'paramBusiness',
  state: {
    list: [],
    pageData: {
      pageSize: 10,
      pageNum: 1,
      total: 0,
      current: 1,
    },
  },
  reducers: {
    save(
      state,
      {
        payload: { records: list, pageSize, pageNum, total, current },
      },
    ) {
      list.forEach(element => {
        element.key = element.paramId;
      });
      return {
        ...state,
        list: [...list],
        pageData: { pageSize, pageNum, total, current },
      };
    },
    changeList(
      state,
      {
        payload: { list },
      },
    ) {
      return { ...state, list: [...list] };
    },
  },
  effects: {
    // 获取列表数据
    *getList({ payload }, { call, put }) {
      const { data } = yield call(service.getList, payload);
      yield put({
        type: 'save',
        payload: { ...data },
      });
    },
    // 增加
    *add({ payload }, { call }) {
      const { code } = yield call(service.add, payload);
      return code === 'sys.success';
    },
    // 更改
    *update({ payload }, { call, put, select }) {
      const { code } = yield call(service.update, payload);
      const result = code === 'sys.success';
      if (result) {
        const list = yield select(({ paramBusiness }) => paramBusiness.list);
        const index = list.findIndex(element => payload.key === element.key);
        if (index > -1) {
          list[index] = { ...list[index], ...payload };
        }
        yield put({
          type: 'changeList',
          payload: { list },
        });
      }
      return result;
    },
    // 删除
    *remove({ payload }, { call }) {
      const { code } = yield call(service.remove, payload);
      return code === 'sys.success';
    },
    *exportParam({ params }, { call }) {
      yield call(service.exportParam, { ...params });
    },
  },
  subscriptions: {
    // setup({ dispatch, history }) {
    //   return history.listen(({ pathname, query, hash }) => {
    //     // 初始化数据加载 也可以在componentDidMount中
    //     if (pathname === "/parameter" && (hash === "#business" || !hash)) {
    //       dispatch({ type: "getList", payload: query });
    //     }
    //   });
    // },
  },
};