import pathToRegexp from 'path-to-regexp'; import { message } from 'antd'; import { queryOne, querySubOrder, create, confirmPay, confirmSend, confirmReceive } from '../../services/order'; export default { namespace: 'orderDetail', state: { filters: {}, operType: 'create', currentItem: {}, itemLoading: false, terminalModalShow: false, productModalShow: false, deliveryModalShow: false, }, subscriptions: { setup({ dispatch, history }) { history.listen(({ pathname, state }) => { const match = pathToRegexp('/trade/order/profile/:id').exec(pathname); const sub = pathToRegexp('/trade/order/sub/profile/:id').exec(pathname); if (match) { dispatch({ type: 'query', payload: { id: match[1] } }); dispatch({ type: 'saveFilters', payload: state }); dispatch({ type: 'saveOperType', payload: { operType: 'view' } }); } else if (sub) { dispatch({ type: 'querySubOrder', payload: { id: sub[1] } }); dispatch({ type: 'saveFilters', payload: state }); dispatch({ type: 'saveOperType', payload: { operType: 'view' } }); } }); }, }, effects: { * query({ payload }, { call, put }) { yield put({ type: 'changeLoading', payload: { itemLoading: true } }); const { data, success } = yield call(queryOne, payload); if (success) { yield put({ type: 'querySuccess', payload: { ...data } }); } yield put({ type: 'changeLoading', payload: { itemLoading: false } }); }, * querySubOrder({ payload }, { call, put }) { yield put({ type: 'changeLoading', payload: { itemLoading: true } }); const { data, success } = yield call(querySubOrder, payload); if (success) { yield put({ type: 'querySuccess', payload: { ...data } }); } yield put({ type: 'changeLoading', payload: { itemLoading: false } }); }, * create({ payload, callback }, { call, put }) { const { success } = yield call(create, { ...payload }); if (success) { message.success('提交成功!'); yield put({ type: 'initState' }); if (callback) callback(); } }, * orderPay({ payload, callback }, { call }) { const { success } = yield call(confirmPay, payload); if (success) { message.success('支付成功!'); if (callback) callback(); } }, * orderSend({ payload, callback }, { call, put }) { const { success } = yield call(confirmSend, payload); if (success) { message.success('发货成功!'); yield put({ type: 'hideDeliveryModal' }); if (callback) callback(); } }, * orderReceive({ payload, callback }, { call }) { const { success } = yield call(confirmReceive, payload); if (success) { message.success('收货成功!'); if (callback) callback(); } }, }, reducers: { changeLoading(state, { payload }) { return { ...state, ...payload }; }, querySuccess(state, { payload }) { return { ...state, currentItem: payload }; }, saveFilters(state, { payload: filters }) { return { ...state, filters }; }, saveOuterParams(state, action) { return { ...state, ...action.payload }; }, showTerminalModal(state, action) { return { ...state, ...action.payload, terminalModalShow: true }; }, hideTerminalModal(state, action) { return { ...state, ...action.payload, terminalModalShow: false }; }, showProductModal(state, action) { return { ...state, ...action.payload, productModalShow: true }; }, hideProductModal(state, action) { return { ...state, ...action.payload, productModalShow: false }; }, showDeliveryModal(state, action) { return { ...state, ...action.payload, deliveryModalShow: true }; }, hideDeliveryModal(state, action) { return { ...state, ...action.payload, deliveryModalShow: false }; }, initState(state) { return { ...state, currentItem: {}, itemLoading: false }; }, }, };