import { createAction, NavigationActions } from '../../utils' import * as authService from '../../services/auth' import deviceStorage from '../../utils/storage' import { accMul } from '../../utils/utils' export default { namespace: 'shoppingcart', state: { ShopList: [], isDelete: false, TotalNum: 0, addShopFlag: false, isRefreshing: false, shopListIds: [], }, reducers: { getShopListReducer(state, { payload }) { return { ...state, ...payload } }, getShopTotalNum(state, { payload }) { return { ...state, ...payload } }, shopIdsReducer(state, { payload }) { return { ...state, ...payload } }, addShopReducer(state, { payload }) { return { ...state, ...payload } }, deleteShopReducer(state, { payload }) { return { ...state, ...payload } }, }, effects: { // 查看购物车 *getShoppingList(action, { call, put }) { // console.log(action, 112938); try { yield put( createAction('getShopListReducer')({ isRefreshing: true, }) ) let newShopdata = [], TotalNum = 0, shopListIds = [] const shopList = yield call(() => authService.getShopList(action.payload) ) // console.log(shopList, 19999999); if (shopList && shopList.data) { if ( Object.keys(shopList.data).indexOf('code') && Object.keys(shopList.data)[ Object.keys(shopList.data).indexOf('code') == 'timeOut' ] ) { } else { if (shopList.data.length > 0) { shopList.data.map(item => { newShopdata.push({ isPrimaryChannel: item.isPrimaryChannel, saleOrgCode: item.isPrimaryChannel, saleOrgId: item.saleOrgId, saleOrgName: item.saleOrgName, supplierCode: item.supplierCode, supplierId: item.supplierId, supplierName: item.supplierName, totalAmount: item.totalAmount, data: item.cartlist, }) TotalNum = TotalNum + item.cartlist.length }) } else { newShopdata = [] } newShopdata.map(item => item.data.map(data => shopListIds.push(data.goodsId)) ) } yield put( createAction('getShopListReducer')({ ShopList: newShopdata, TotalNum: TotalNum, isRefreshing: false, }) ) yield put( createAction('shopIdsReducer')({ shopListIds: shopListIds }) ) } } catch (error) { yield put( createAction('getShopListReducer')({ isRefreshing: false, }) ) console.log( error, '-----error的完整信息' + '\n' + error.response.data.message, '-----相关错误信息' ) } }, // 添加购物车 *addShop(action, { call, put }) { try { const shopAddVal = yield call(() => authService.addShopCart(action.payload) ) let shopFlag = false if (shopAddVal) { shopFlag = true yield put( createAction('addShopReducer')({ addShopFlag: shopFlag, }) ) yield put( createAction('getShoppingList')({ params: { customer: CUSTOMERINFO.id, }, }) ) } } catch (error) { console.log( error, '-----error的完整信息' + '\n' + error.response.data.message, '-----相关错误信息' ) } }, //清空购物车 *emptyCart(action, { call, put }) { try { yield call(() => { authService.clearShopCart(action.payload) } ) yield put( createAction('getShopListReducer')({ isDelete: true, edited: false, ShopList: [], TotalNum: 0, Amount: 0 }) ) } catch (error) { console.log( error, '-----error的完整信息' + '\n' + error.response.data.message, '-----相关错误信息' ) } }, // 删除购物车商品行 *deleteShop(action, { call, put }) { // console.log(action, 112938); try { let newShopdata = [], TotalNum = 0 const shopList = yield call(() => authService.delShopCart(action.payload) ) if (shopList && shopList.data) { // shopList.data.map(item => { // newShopdata.push({ // isPrimaryChannel: item.isPrimaryChannel, // saleOrgCode: item.isPrimaryChannel, // saleOrgId: item.saleOrgId, // saleOrgName: item.saleOrgName, // supplierCode: item.supplierCode, // supplierId: item.supplierId, // supplierName: item.supplierName, // totalAmount: item.totalAmount, // data: item.cartlist // }); // TotalNum = TotalNum + item.cartlist.length; // }); yield put( createAction('getShopListReducer')({ isDelete: true, // ShopList: newShopdata, // TotalNum: TotalNum }) ) } } catch (error) { console.log( error, '-----error的完整信息' + '\n' + error.response.data.message, '-----相关错误信息' ) } }, // 修改购物车数量 *editShop(action, { call, put }) { try { let newItem = {} action.payload.item.orderNum = action.payload.cbNumber newItem = action.payload.item yield call(() => authService.editShopCart(newItem)) // const shopList = yield call(() => authService.editShopCart(newItem)); } catch (error) { console.log( error, '-----error的完整信息' + '\n' + error.response.data.message, '-----相关错误信息' ) } }, // 获取购物车数量 *getCartNum(action, { call, put }) { try { const cartNum = yield call(() => authService.getCartCount(action.payload) ) if (cartNum && cartNum.data && cartNum.data >= 0) { yield put( createAction('getShopTotalNum')({ TotalNum: cartNum.data, }) ) } } catch (error) { console.log( error, '-----error的完整信息' + '\n' + error.response.data.message, '-----相关错误信息' ) } }, }, // subscriptions: { // setup({ dispatch }) { // dispatch({ // type: "getCartNum", // payload: { // params: { // customer: "d4b4677f-93ec-49b5-bcaa-c134df0f7295" // } // } // }); // } // } }