protectSlice.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import {createSlice} from "@reduxjs/toolkit";
  2. import {REQ_STATUS} from "@api/config";
  3. import {buildReqReducer, reqProtectCheck, reqProtectGet} from "@features/api";
  4. import {getHttpMsg} from "@utils/helpers";
  5. import {toast} from "react-toastify";
  6. const reducerProtectCheck=(state,action,arg)=>{
  7. let payload=action.payload;
  8. if (payload&&payload.code===200){
  9. let data=payload.data;
  10. if (data!=null){
  11. let info=data[0];
  12. if (info!=null)state.protectInfo=info;
  13. }
  14. }
  15. }
  16. const protectCheckStatus=(state,status,arg)=>{
  17. state.status.ProtectCheck=status;
  18. }
  19. const reducerProtectGet=(state,action,arg)=>{
  20. let payload=action.payload;
  21. let msg=getHttpMsg(payload);
  22. if (payload&&payload.code===200){
  23. let CurProtectTimes=state.protectInfo.CurProtectTimes!=null?Number(state.protectInfo.CurProtectTimes):0;
  24. CurProtectTimes=CurProtectTimes+1;
  25. state.protectInfo.CurProtectTimes=CurProtectTimes;
  26. if (msg!=null&&msg!="")toast.success(msg);
  27. }
  28. else
  29. {
  30. if (msg!=null&&msg!="")toast.error(msg);
  31. }
  32. if (arg.callback!=null)arg.callback(payload);
  33. }
  34. const protectGetStatus=(state,status,arg)=>{
  35. state.status.ProtectGet=status;
  36. }
  37. export const ProtectSlice = createSlice({
  38. name: "protect",
  39. initialState: {
  40. protectInfo: {},
  41. status:{
  42. ProtectCheck:REQ_STATUS.NONE,
  43. ProtectGet:REQ_STATUS.NONE,
  44. }
  45. },
  46. reducers: {
  47. },
  48. extraReducers: (builder) => {
  49. buildReqReducer(builder,reqProtectCheck,reducerProtectCheck,protectCheckStatus);
  50. buildReqReducer(builder,reqProtectGet,reducerProtectGet,protectGetStatus);
  51. }
  52. })
  53. export const {} = ProtectSlice.actions;
  54. export const getProtectInfo=(state)=>{
  55. return state.protect.protectInfo;
  56. }
  57. export const getProtectGetStatus=(state)=>{
  58. return state.protect.status.ProtectGet;
  59. }
  60. export default ProtectSlice.reducer;