import { message } from 'antd'; import { query, remove, update, create, getSignature } from '../services/resource'; import modelExtend from 'dva-model-extend'; import queryString from 'query-string'; import { pageModel } from './common'; import { pageSize, Codes } from '../utils/config'; import { checkSearchParams } from '../utils/utils'; export default modelExtend(pageModel, { namespace: 'resource', state: { isPaused: false, currentItem: {}, itemLoading: false, listLoading: false, modalVisible: false, modalType: 'create', }, subscriptions: { setup({ dispatch, history }) { history.listen((location) => { if (location.pathname === '/resource/gallery') { const payload = checkSearchParams(queryString.parse(location.search)); payload.type = Codes.CODE_IMAGE, // 只查询图片类型 dispatch({ type: 'query', payload, }); } if (location.pathname === '/resource/video') { const payload = checkSearchParams(queryString.parse(location.search)); payload.type = Codes.CODE_VIDEO, // 只查询视频类型 dispatch({ type: 'query', payload, }); } }); } }, effects: { * query ({ payload = {} }, { call, put }) { yield put({ type: 'changeLoading', payload: { listLoading: true }}); const { data, success } = yield call(query, { ...payload, pageSize }); if (success) { yield put({ type: 'querySuccess', payload: { list: data.list, pagination: { current: Number(payload.pageNo) || 1, pageSize: Number(payload.pageSize) || pageSize, total: data.totalSize, } } }); } yield put({ type: 'changeLoading', payload: { listLoading: false }}); }, * create ({ payload, callback }, { call, put }) { const { data, success } = yield call(create, payload); if (success) { message.success('创建成功!'); if (callback) callback(); yield put({ type: 'hideModal' }); } }, * update ({ payload, callback }, { call, put }) { const { data, success } = yield call(update, payload); if (success) { message.success('修改成功!'); if (callback) callback(); } yield put({ type: 'hideModal' }); }, * delete ({ payload, callback }, { call, put }) { const { data, success } = yield call(remove, payload); if (success) { message.success('删除成功!'); if (callback) callback(); } }, }, reducers: { changeLoading(state, { payload }) { return { ...state, ...payload }; }, showModal(state, { payload }) { return { ...state, ...payload, modalVisible: true }; }, hideModal(state) { return { ...state, modalVisible: false, currentItem: {} }; }, updateItem(state, action) { return { ...state, currentItem: { ...state.currentItem, ...action.payload }, }; }, closeVideo(state) { return { ...state, isPaused: true, modalVisible: false }; }, } })