ShoppingCart.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import { createAction, NavigationActions } from '../../utils'
  2. import * as authService from '../../services/auth'
  3. import deviceStorage from '../../utils/storage'
  4. import { accMul } from '../../utils/utils'
  5. export default {
  6. namespace: 'shoppingcart',
  7. state: {
  8. ShopList: [],
  9. isDelete: false,
  10. TotalNum: 0,
  11. addShopFlag: false,
  12. isRefreshing: false,
  13. shopListIds: [],
  14. },
  15. reducers: {
  16. getShopListReducer(state, { payload }) {
  17. return { ...state, ...payload }
  18. },
  19. getShopTotalNum(state, { payload }) {
  20. return { ...state, ...payload }
  21. },
  22. shopIdsReducer(state, { payload }) {
  23. return { ...state, ...payload }
  24. },
  25. addShopReducer(state, { payload }) {
  26. return { ...state, ...payload }
  27. },
  28. deleteShopReducer(state, { payload }) {
  29. return { ...state, ...payload }
  30. },
  31. },
  32. effects: {
  33. // 查看购物车
  34. *getShoppingList(action, { call, put }) {
  35. // console.log(action, 112938);
  36. try {
  37. yield put(
  38. createAction('getShopListReducer')({
  39. isRefreshing: true,
  40. })
  41. )
  42. let newShopdata = [],
  43. TotalNum = 0,
  44. shopListIds = []
  45. const shopList = yield call(() =>
  46. authService.getShopList(action.payload)
  47. )
  48. // console.log(shopList, 19999999);
  49. if (shopList && shopList.data) {
  50. if (
  51. Object.keys(shopList.data).indexOf('code') &&
  52. Object.keys(shopList.data)[
  53. Object.keys(shopList.data).indexOf('code') == 'timeOut'
  54. ]
  55. ) {
  56. } else {
  57. if (shopList.data.length > 0) {
  58. shopList.data.map(item => {
  59. newShopdata.push({
  60. isPrimaryChannel: item.isPrimaryChannel,
  61. saleOrgCode: item.isPrimaryChannel,
  62. saleOrgId: item.saleOrgId,
  63. saleOrgName: item.saleOrgName,
  64. supplierCode: item.supplierCode,
  65. supplierId: item.supplierId,
  66. supplierName: item.supplierName,
  67. totalAmount: item.totalAmount,
  68. data: item.cartlist,
  69. })
  70. TotalNum = TotalNum + item.cartlist.length
  71. })
  72. } else {
  73. newShopdata = []
  74. }
  75. newShopdata.map(item =>
  76. item.data.map(data => shopListIds.push(data.goodsId))
  77. )
  78. }
  79. yield put(
  80. createAction('getShopListReducer')({
  81. ShopList: newShopdata,
  82. TotalNum: TotalNum,
  83. isRefreshing: false,
  84. })
  85. )
  86. yield put(
  87. createAction('shopIdsReducer')({ shopListIds: shopListIds })
  88. )
  89. }
  90. } catch (error) {
  91. yield put(
  92. createAction('getShopListReducer')({
  93. isRefreshing: false,
  94. })
  95. )
  96. console.log(
  97. error,
  98. '-----error的完整信息' + '\n' + error.response.data.message,
  99. '-----相关错误信息'
  100. )
  101. }
  102. },
  103. // 添加购物车
  104. *addShop(action, { call, put }) {
  105. try {
  106. const shopAddVal = yield call(() =>
  107. authService.addShopCart(action.payload)
  108. )
  109. let shopFlag = false
  110. if (shopAddVal) {
  111. shopFlag = true
  112. yield put(
  113. createAction('addShopReducer')({
  114. addShopFlag: shopFlag,
  115. })
  116. )
  117. yield put(
  118. createAction('getShoppingList')({
  119. params: {
  120. customer: CUSTOMERINFO.id,
  121. },
  122. })
  123. )
  124. }
  125. } catch (error) {
  126. console.log(
  127. error,
  128. '-----error的完整信息' + '\n' + error.response.data.message,
  129. '-----相关错误信息'
  130. )
  131. }
  132. },
  133. // 删除购物车商品行
  134. *deleteShop(action, { call, put }) {
  135. // console.log(action, 112938);
  136. try {
  137. let newShopdata = [],
  138. TotalNum = 0
  139. const shopList = yield call(() =>
  140. authService.delShopCart(action.payload)
  141. )
  142. if (shopList && shopList.data) {
  143. // shopList.data.map(item => {
  144. // newShopdata.push({
  145. // isPrimaryChannel: item.isPrimaryChannel,
  146. // saleOrgCode: item.isPrimaryChannel,
  147. // saleOrgId: item.saleOrgId,
  148. // saleOrgName: item.saleOrgName,
  149. // supplierCode: item.supplierCode,
  150. // supplierId: item.supplierId,
  151. // supplierName: item.supplierName,
  152. // totalAmount: item.totalAmount,
  153. // data: item.cartlist
  154. // });
  155. // TotalNum = TotalNum + item.cartlist.length;
  156. // });
  157. yield put(
  158. createAction('getShopListReducer')({
  159. isDelete: true,
  160. // ShopList: newShopdata,
  161. // TotalNum: TotalNum
  162. })
  163. )
  164. }
  165. } catch (error) {
  166. console.log(
  167. error,
  168. '-----error的完整信息' + '\n' + error.response.data.message,
  169. '-----相关错误信息'
  170. )
  171. }
  172. },
  173. // 修改购物车数量
  174. *editShop(action, { call, put }) {
  175. try {
  176. let newItem = {}
  177. action.payload.item.orderNum = action.payload.cbNumber
  178. newItem = action.payload.item
  179. yield call(() => authService.editShopCart(newItem))
  180. // const shopList = yield call(() => authService.editShopCart(newItem));
  181. } catch (error) {
  182. console.log(
  183. error,
  184. '-----error的完整信息' + '\n' + error.response.data.message,
  185. '-----相关错误信息'
  186. )
  187. }
  188. },
  189. // 获取购物车数量
  190. *getCartNum(action, { call, put }) {
  191. try {
  192. const cartNum = yield call(() =>
  193. authService.getCartCount(action.payload)
  194. )
  195. if (cartNum && cartNum.data && cartNum.data >= 0) {
  196. yield put(
  197. createAction('getShopTotalNum')({
  198. TotalNum: cartNum.data,
  199. })
  200. )
  201. }
  202. } catch (error) {
  203. console.log(
  204. error,
  205. '-----error的完整信息' + '\n' + error.response.data.message,
  206. '-----相关错误信息'
  207. )
  208. }
  209. },
  210. },
  211. // subscriptions: {
  212. // setup({ dispatch }) {
  213. // dispatch({
  214. // type: "getCartNum",
  215. // payload: {
  216. // params: {
  217. // customer: "d4b4677f-93ec-49b5-bcaa-c134df0f7295"
  218. // }
  219. // }
  220. // });
  221. // }
  222. // }
  223. }