resource.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { message } from 'antd';
  2. import { query, remove, update, create, getSignature } from '../services/resource';
  3. import modelExtend from 'dva-model-extend';
  4. import queryString from 'query-string';
  5. import { pageModel } from './common';
  6. import { pageSize, Codes } from '../utils/config';
  7. import { checkSearchParams } from '../utils/utils';
  8. export default modelExtend(pageModel, {
  9. namespace: 'resource',
  10. state: {
  11. isPaused: false,
  12. currentItem: {},
  13. itemLoading: false,
  14. listLoading: false,
  15. modalVisible: false,
  16. modalType: 'create',
  17. },
  18. subscriptions: {
  19. setup({ dispatch, history }) {
  20. history.listen((location) => {
  21. if (location.pathname === '/resource/gallery') {
  22. const payload = checkSearchParams(queryString.parse(location.search));
  23. payload.type = Codes.CODE_IMAGE, // 只查询图片类型
  24. dispatch({
  25. type: 'query',
  26. payload,
  27. });
  28. }
  29. if (location.pathname === '/resource/video') {
  30. const payload = checkSearchParams(queryString.parse(location.search));
  31. payload.type = Codes.CODE_VIDEO, // 只查询视频类型
  32. dispatch({
  33. type: 'query',
  34. payload,
  35. });
  36. }
  37. });
  38. }
  39. },
  40. effects: {
  41. * query ({ payload = {} }, { call, put }) {
  42. yield put({ type: 'changeLoading', payload: { listLoading: true }});
  43. const { data, success } = yield call(query, { ...payload, pageSize });
  44. if (success) {
  45. yield put({
  46. type: 'querySuccess',
  47. payload: {
  48. list: data.list,
  49. pagination: {
  50. current: Number(payload.pageNo) || 1,
  51. pageSize: Number(payload.pageSize) || pageSize,
  52. total: data.totalSize,
  53. }
  54. }
  55. });
  56. }
  57. yield put({ type: 'changeLoading', payload: { listLoading: false }});
  58. },
  59. * create ({ payload, callback }, { call, put }) {
  60. const { data, success } = yield call(create, payload);
  61. if (success) {
  62. message.success('创建成功!');
  63. if (callback) callback();
  64. yield put({ type: 'hideModal' });
  65. }
  66. },
  67. * update ({ payload, callback }, { call, put }) {
  68. const { data, success } = yield call(update, payload);
  69. if (success) {
  70. message.success('修改成功!');
  71. if (callback) callback();
  72. }
  73. yield put({ type: 'hideModal' });
  74. },
  75. * delete ({ payload, callback }, { call, put }) {
  76. const { data, success } = yield call(remove, payload);
  77. if (success) {
  78. message.success('删除成功!');
  79. if (callback) callback();
  80. }
  81. },
  82. },
  83. reducers: {
  84. changeLoading(state, { payload }) {
  85. return { ...state, ...payload };
  86. },
  87. showModal(state, { payload }) {
  88. return { ...state, ...payload, modalVisible: true };
  89. },
  90. hideModal(state) {
  91. return { ...state, modalVisible: false, currentItem: {} };
  92. },
  93. updateItem(state, action) {
  94. return {
  95. ...state,
  96. currentItem: { ...state.currentItem, ...action.payload },
  97. };
  98. },
  99. closeVideo(state) {
  100. return { ...state, isPaused: true, modalVisible: false };
  101. },
  102. }
  103. })