cmsUser.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { query, create, update, remove } from '../services/cmsUser';
  2. import modelExtend from 'dva-model-extend';
  3. import queryString from 'query-string';
  4. import { message } from 'antd';
  5. import { pageModel } from './common';
  6. import { pageSize } from '../utils/config';
  7. import { checkSearchParams } from '../utils/utils';
  8. export default modelExtend(pageModel, {
  9. namespace: 'cmsUser',
  10. state: {
  11. currentItem: {},
  12. itemLoading: false,
  13. listLoading: false,
  14. modalVisible: false,
  15. modalType: 'create',
  16. },
  17. subscriptions: {
  18. setup({ dispatch, history }) {
  19. history.listen((location) => {
  20. if (location.pathname === '/cms/user') {
  21. const payload = checkSearchParams(queryString.parse(location.search));
  22. dispatch({
  23. type: 'query',
  24. payload,
  25. });
  26. }
  27. });
  28. }
  29. },
  30. effects: {
  31. * query ({ payload = {} }, { call, put }) {
  32. yield put({ type: 'changeLoading', payload: { listLoading: true }});
  33. const { data, success } = yield call(query, payload);
  34. if (success) {
  35. yield put({
  36. type: 'querySuccess',
  37. payload: {
  38. list: data.list,
  39. pagination: {
  40. current: Number(payload.pageNo) || 1,
  41. pageSize: Number(payload.pageSize) || pageSize,
  42. total: data.totalSize,
  43. }
  44. }
  45. });
  46. }
  47. yield put({ type: 'changeLoading', payload: { listLoading: false }});
  48. },
  49. * create ({ payload, callback }, { call, put }) {
  50. const { data, success } = yield call(create, payload);
  51. if (success) {
  52. message.success('创建成功!');
  53. if (callback) callback();
  54. }
  55. },
  56. * update ({ payload, callback }, { call, put }) {
  57. const { data, success } = yield call(update, payload);
  58. if (success) {
  59. yield put({ type: 'hideModal' });
  60. message.success('更新成功!');
  61. if (callback) callback();
  62. }
  63. },
  64. * delete ({ payload, callback }, { call, put }) {
  65. const { data, success } = yield call(remove, payload);
  66. if (success) {
  67. message.success('禁用成功!');
  68. if (callback) callback();
  69. }
  70. },
  71. * recover ({ payload, callback }, { call, put }) {
  72. const { data, success } = yield call(update, payload);
  73. if (success) {
  74. message.success('解禁成功!');
  75. if (callback) callback();
  76. }
  77. },
  78. },
  79. reducers: {
  80. changeLoading(state, { payload }) {
  81. return { ...state, ...payload };
  82. },
  83. showModal(state, { payload }) {
  84. return { ...state, ...payload, modalVisible: true };
  85. },
  86. hideModal(state) {
  87. return { ...state, modalVisible: false };
  88. },
  89. }
  90. })