在每次发送http请求时,headers 的 Authorization 字段都需要携带 jwt 信息,格式为 `bearer ${jwt} `

    1. import { createSlice, PayloadAction, createAsyncThunk } from "@reduxjs/toolkit";
    2. import axios from "axios";
    3. interface ShoppingCarttate {
    4. loading: boolean;
    5. error: string | null;
    6. items: any[];
    7. }
    8. const initialState: ShoppingCarttate = {
    9. loading: true,
    10. error: null,
    11. items: [],
    12. };
    13. export const getShoppingCart = createAsyncThunk(
    14. "shoppingCart/getShoppingCart",
    15. async (jwt: string, thunkAPI) => {
    16. const { data } = await axios.get(
    17. `/api/shoppingCart`,
    18. {
    19. headers: {
    20. Authorization: `bearer ${jwt}`,
    21. },
    22. }
    23. );
    24. return data.shoppingCartItems;
    25. }
    26. );
    27. export const addShoppingCartItem = createAsyncThunk(
    28. "shoppingCart/addShoppingCartItem",
    29. async (parameters: { jwt: string; touristRouteId: string }, thunkAPI) => {
    30. const { data } = await axios.post(
    31. `/api/shoppingCart/items`,
    32. {
    33. touristRouteId: parameters.touristRouteId,
    34. },
    35. {
    36. headers: {
    37. Authorization: `bearer ${parameters.jwt}`,
    38. },
    39. }
    40. );
    41. return data.shoppingCartItems;
    42. }
    43. );
    44. export const checkout = createAsyncThunk(
    45. "shoppingCart/checkout",
    46. async (jwt: string, thunkAPI) => {
    47. const { data } = await axios.post(
    48. `/api/shoppingCart/checkout`,
    49. null,
    50. {
    51. headers: {
    52. Authorization: `bearer ${jwt}`,
    53. },
    54. }
    55. );
    56. return data;
    57. }
    58. );
    59. export const clearShoppingCartItem = createAsyncThunk(
    60. "shoppingCart/clearShoppingCartItem",
    61. async (parameters: { jwt: string; itemIds: number[] }, thunkAPI) => {
    62. return await axios.delete(
    63. `/api/shoppingCart/items/(${parameters.itemIds.join(
    64. ","
    65. )})`,
    66. {
    67. headers: {
    68. Authorization: `bearer ${parameters.jwt}`,
    69. },
    70. }
    71. );
    72. }
    73. );
    74. export const shoppingCartSlice = createSlice({
    75. name: "shoppingCart",
    76. initialState,
    77. reducers: {},
    78. extraReducers: {
    79. [getShoppingCart.pending.type]: (state) => {
    80. state.loading = true;
    81. },
    82. [getShoppingCart.fulfilled.type]: (state, action) => {
    83. state.items = action.payload;
    84. state.loading = false;
    85. state.error = null;
    86. },
    87. [getShoppingCart.rejected.type]: (
    88. state,
    89. action: PayloadAction<string | null>
    90. ) => {
    91. state.loading = false;
    92. state.error = action.payload;
    93. },
    94. [addShoppingCartItem.pending.type]: (state) => {
    95. state.loading = true;
    96. },
    97. [addShoppingCartItem.fulfilled.type]: (state, action) => {
    98. state.items = action.payload;
    99. state.loading = false;
    100. state.error = null;
    101. },
    102. [addShoppingCartItem.rejected.type]: (
    103. state,
    104. action: PayloadAction<string | null>
    105. ) => {
    106. state.loading = false;
    107. state.error = action.payload;
    108. },
    109. [clearShoppingCartItem.pending.type]: (state) => {
    110. state.loading = true;
    111. },
    112. [clearShoppingCartItem.fulfilled.type]: (state) => {
    113. state.items = [];
    114. state.loading = false;
    115. state.error = null;
    116. },
    117. [clearShoppingCartItem.rejected.type]: (
    118. state,
    119. action: PayloadAction<string | null>
    120. ) => {
    121. state.loading = false;
    122. state.error = action.payload;
    123. },
    124. [checkout.pending.type]: (state) => {
    125. state.loading = true;
    126. },
    127. [checkout.fulfilled.type]: (state, action) => {
    128. state.items = [];
    129. state.loading = false;
    130. state.error = null;
    131. },
    132. [checkout.rejected.type]: (
    133. state,
    134. action: PayloadAction<string | null>
    135. ) => {
    136. state.loading = false;
    137. state.error = action.payload;
    138. },
    139. },
    140. });