HomeModels.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import { createAction, NavigationActions } from '../../utils'
  2. import * as authService from '../../services/auth'
  3. import deviceStorage from '../../utils/storage'
  4. import utils from '../../utils/utils'
  5. export default {
  6. namespace: 'home',
  7. state: {
  8. hotSaleDatas: [],
  9. allProDatas: [],
  10. recentDatas: [],
  11. ReqNumData: [],
  12. fetching: false,
  13. },
  14. reducers: {
  15. hotSale(state, { payload }) {
  16. return { ...state, ...payload }
  17. },
  18. allProduct(state, { payload }) {
  19. return { ...state, ...payload }
  20. },
  21. recentView(state, { payload }) {
  22. return { ...state, ...payload }
  23. },
  24. homeReqRenducer(state, { payload }) {
  25. return { ...state, ...payload }
  26. },
  27. },
  28. effects: {
  29. //热卖促销
  30. *getHotSale(action, { call, put }) {
  31. try {
  32. yield put(createAction('hotSale')({ fetching: true }))
  33. const hotdata = yield call(() =>
  34. authService.hotsale({ customerId: CUSTOMERINFO.id })
  35. )
  36. if (hotdata.data) {
  37. yield put(
  38. createAction('hotSale')({
  39. hotSaleDatas: hotdata.data,
  40. fetching: false,
  41. })
  42. )
  43. }
  44. } catch (error) {
  45. yield put(createAction('hotSale')({ fetching: false }))
  46. console.log(
  47. error,
  48. '-----error的完整信息' + '\n' + error.response.data.message,
  49. '-----相关错误信息'
  50. )
  51. }
  52. // yield put(createAction("getAllPro")());
  53. // yield put(createAction("getRecentView")());
  54. yield put(createAction('category/getCategory')())
  55. yield put(createAction('homeOrderNum')())
  56. },
  57. //所有商品
  58. *getAllPro(action, { call, put }) {
  59. try {
  60. yield put(createAction('allProduct')({ fetching: true }))
  61. const alldata = yield call(() =>
  62. authService.searchCommodityList({
  63. search_customerId: CUSTOMERINFO.id,
  64. search_customerRankCode: CUSTOMERINFO.customerRankCode,
  65. search_EQ_isEnable: 1,
  66. // search_EQ_isOptional: 0,
  67. // search_EQ_enableStrucManage: 0
  68. })
  69. )
  70. if (alldata.data) {
  71. yield put(
  72. createAction('allProduct')({
  73. allProDatas: alldata.data,
  74. fetching: false,
  75. })
  76. )
  77. }
  78. } catch (error) {
  79. console.log(
  80. error,
  81. '-----error的完整信息' + '\n' + error.response.data.message,
  82. '-----相关错误信息'
  83. )
  84. yield put(createAction('allProduct')({ fetching: false }))
  85. }
  86. },
  87. //最近浏览(注释的为根据浏览次数排序)
  88. *getRecentView(action, { call, put }) {
  89. yield put(createAction('recentView')({ fetching: true }))
  90. let recentView = []
  91. if (action && action.payload && action.payload.clear) {
  92. deviceStorage.remove('recentView')
  93. }
  94. const storageView = yield call(deviceStorage.get, 'recentView')
  95. if (!storageView && !action.payload) return
  96. if (!storageView) {
  97. recentView.push(action.payload.recentView)
  98. // recentView[0].clickNum = 1;
  99. deviceStorage.set('recentView', recentView)
  100. } else {
  101. recentView = storageView
  102. // if (recentView && action.payload) {
  103. if (
  104. storageView &&
  105. action.payload &&
  106. action.payload.recentView &&
  107. action.payload.recentView.code &&
  108. !utils.comparison(storageView, action.payload.recentView.code)
  109. ) {
  110. // action.payload.recentView.clickNum = 1;
  111. recentView.unshift(action.payload.recentView)
  112. deviceStorage.set('recentView', recentView)
  113. }
  114. // else {
  115. // for (let i = 0; i < recentView.length; i++) {
  116. // if (action.payload.recentView.code == recentView[i].code) {
  117. // recentView[i].clickNum = recentView[i].clickNum + 1;
  118. // deviceStorage.set("recentView", recentView);
  119. // }
  120. // }
  121. // }
  122. // }
  123. }
  124. yield put(
  125. createAction('recentView')({ recentDatas: recentView, fetching: false })
  126. )
  127. },
  128. //最近浏览清除
  129. *recentViewClear(action, { call, put }) {
  130. deviceStorage.remove('recentView')
  131. yield put(createAction('recentView')({ recentDatas: [] }))
  132. },
  133. //首页订单数量
  134. *homeOrderNum(action, { call, put }) {
  135. try {
  136. yield put(createAction('homeReqRenducer')({ fetching: true }))
  137. const orderNum = yield call(() =>
  138. authService.getHomeOrderNum({ customerId: CUSTOMERINFO.id })
  139. )
  140. if (orderNum.data) {
  141. yield put(
  142. createAction('homeReqRenducer')({
  143. ReqNumData: orderNum.data,
  144. fetching: false,
  145. })
  146. )
  147. }
  148. } catch (error) {
  149. console.log(
  150. error,
  151. '-----error的完整信息' + '\n' + error.response.data.message,
  152. '-----相关错误信息'
  153. )
  154. yield put(createAction('homeReqRenducer')({ fetching: false }))
  155. }
  156. },
  157. },
  158. // // 加入登录后删除
  159. // subscriptions: {
  160. // setup({ dispatch }) {
  161. // dispatch({ type: "getHotSale" });
  162. // }
  163. // }
  164. }