ShoppingCart.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. *emptyCart(action, { call, put }) {
  135. try {
  136. yield call(() => {
  137. authService.clearShopCart(action.payload)
  138. }
  139. )
  140. yield put(
  141. createAction('getShopListReducer')({
  142. isDelete: true,
  143. edited: false,
  144. ShopList: [],
  145. TotalNum: 0,
  146. Amount: 0
  147. })
  148. )
  149. } catch (error) {
  150. console.log(
  151. error,
  152. '-----error的完整信息' + '\n' + error.response.data.message,
  153. '-----相关错误信息'
  154. )
  155. }
  156. },
  157. // 删除购物车商品行
  158. *deleteShop(action, { call, put }) {
  159. // console.log(action, 112938);
  160. try {
  161. let newShopdata = [],
  162. TotalNum = 0
  163. const shopList = yield call(() =>
  164. authService.delShopCart(action.payload)
  165. )
  166. if (shopList && shopList.data) {
  167. // shopList.data.map(item => {
  168. // newShopdata.push({
  169. // isPrimaryChannel: item.isPrimaryChannel,
  170. // saleOrgCode: item.isPrimaryChannel,
  171. // saleOrgId: item.saleOrgId,
  172. // saleOrgName: item.saleOrgName,
  173. // supplierCode: item.supplierCode,
  174. // supplierId: item.supplierId,
  175. // supplierName: item.supplierName,
  176. // totalAmount: item.totalAmount,
  177. // data: item.cartlist
  178. // });
  179. // TotalNum = TotalNum + item.cartlist.length;
  180. // });
  181. yield put(
  182. createAction('getShopListReducer')({
  183. isDelete: true,
  184. // ShopList: newShopdata,
  185. // TotalNum: TotalNum
  186. })
  187. )
  188. }
  189. } catch (error) {
  190. console.log(
  191. error,
  192. '-----error的完整信息' + '\n' + error.response.data.message,
  193. '-----相关错误信息'
  194. )
  195. }
  196. },
  197. // 修改购物车数量
  198. *editShop(action, { call, put }) {
  199. try {
  200. let newItem = {}
  201. action.payload.item.orderNum = action.payload.cbNumber
  202. newItem = action.payload.item
  203. yield call(() => authService.editShopCart(newItem))
  204. // const shopList = yield call(() => authService.editShopCart(newItem));
  205. } catch (error) {
  206. console.log(
  207. error,
  208. '-----error的完整信息' + '\n' + error.response.data.message,
  209. '-----相关错误信息'
  210. )
  211. }
  212. },
  213. // 获取购物车数量
  214. *getCartNum(action, { call, put }) {
  215. try {
  216. const cartNum = yield call(() =>
  217. authService.getCartCount(action.payload)
  218. )
  219. if (cartNum && cartNum.data && cartNum.data >= 0) {
  220. yield put(
  221. createAction('getShopTotalNum')({
  222. TotalNum: cartNum.data,
  223. })
  224. )
  225. }
  226. } catch (error) {
  227. console.log(
  228. error,
  229. '-----error的完整信息' + '\n' + error.response.data.message,
  230. '-----相关错误信息'
  231. )
  232. }
  233. },
  234. },
  235. // subscriptions: {
  236. // setup({ dispatch }) {
  237. // dispatch({
  238. // type: "getCartNum",
  239. // payload: {
  240. // params: {
  241. // customer: "d4b4677f-93ec-49b5-bcaa-c134df0f7295"
  242. // }
  243. // }
  244. // });
  245. // }
  246. // }
  247. }