22301080 | a93bebb | 2025-05-27 19:48:11 +0800 | [diff] [blame^] | 1 | import axios from 'axios'; |
| 2 | |
| 3 | const API_BASE_URL = 'http://localhost:8088'; // 替换为你的后端API基础URL |
| 4 | |
| 5 | export const getAllUsers = async () => { |
| 6 | try { |
| 7 | const response = await axios.get(`${API_BASE_URL}/user/allUser`, { |
| 8 | headers: { |
| 9 | Authorization: localStorage.getItem('token') |
| 10 | } |
| 11 | }); |
| 12 | |
| 13 | console.log("API Response:", response.data); // 打印完整响应 |
| 14 | |
| 15 | if (response.data && response.data.code === 200) { |
| 16 | // 修正这里:response.data.data.data 才是用户数组 |
| 17 | const data = response.data.data.data; |
| 18 | return Array.isArray(data) ? data : [data]; |
| 19 | } else { |
| 20 | throw new Error(response.data?.message || "未知错误"); |
| 21 | } |
| 22 | } catch (error) { |
| 23 | console.error('获取用户列表失败:', error); |
| 24 | throw error; |
| 25 | } |
| 26 | }; |
| 27 | |
| 28 | export const searchUsers = async (key) => { |
| 29 | try { |
| 30 | const response = await axios.get(`${API_BASE_URL}/user/searchUser`, { |
| 31 | params: { key }, |
| 32 | headers: { |
| 33 | Authorization: localStorage.getItem('token') |
| 34 | } |
| 35 | }); |
| 36 | |
| 37 | if (response.data?.code === 200) { |
| 38 | // 提取正确的用户数组:response.data.data.data |
| 39 | const users = response.data.data.data; |
| 40 | return Array.isArray(users) ? users : [users]; |
| 41 | } else { |
| 42 | throw new Error(response.data?.message || "搜索失败"); |
| 43 | } |
| 44 | } catch (error) { |
| 45 | console.error('搜索用户失败:', error); |
| 46 | throw error; |
| 47 | } |
| 48 | }; |
| 49 | |
| 50 | // 修改用户权限 |
| 51 | export const updateUserAuthority = async (username, authority) => { |
| 52 | try { |
| 53 | const response = await axios.put(`${API_BASE_URL}/user/changeAuthority`, |
| 54 | { |
| 55 | changeUsername: username, |
| 56 | authority: authority |
| 57 | }, |
| 58 | { |
| 59 | headers: { |
| 60 | Authorization: localStorage.getItem('token') |
| 61 | } |
| 62 | } |
| 63 | ); |
| 64 | return response.data; |
| 65 | } catch (error) { |
| 66 | console.error('修改用户权限失败:', error); |
| 67 | throw error; |
| 68 | } |
| 69 | }; |
| 70 | |
| 71 | |
| 72 | // // 获取所有折扣 |
| 73 | // export const getAllDiscounts = async () => { |
| 74 | // try { |
| 75 | // const response = await axios.get(`${API_BASE_URL}/discount/all`, { |
| 76 | // headers: { |
| 77 | // Authorization: localStorage.getItem('token') |
| 78 | // } |
| 79 | // }); |
| 80 | |
| 81 | // if (response.data && response.data.code === 200) { |
| 82 | // // 确保返回的是数组格式 |
| 83 | // const data = response.data.data.data || response.data.data; |
| 84 | // return Array.isArray(data) ? data : [data]; |
| 85 | // } else { |
| 86 | // throw new Error(response.data?.message || "获取折扣信息失败"); |
| 87 | // } |
| 88 | // } catch (error) { |
| 89 | // console.error('获取折扣列表失败:', error); |
| 90 | // throw error; |
| 91 | // } |
| 92 | // }; |
| 93 | |
| 94 | // // 获取当前折扣 |
| 95 | // export const getCurrentDiscount = async () => { |
| 96 | // try { |
| 97 | // const response = await axios.get(`${API_BASE_URL}/discount/current`, { |
| 98 | // headers: { |
| 99 | // Authorization: localStorage.getItem('token') |
| 100 | // } |
| 101 | // }); |
| 102 | |
| 103 | // if (response.data && response.data.code === 200) { |
| 104 | // return response.data.data.data || response.data.data; |
| 105 | // } else { |
| 106 | // throw new Error(response.data?.message || "获取当前折扣失败"); |
| 107 | // } |
| 108 | // } catch (error) { |
| 109 | // console.error('获取当前折扣失败:', error); |
| 110 | // throw error; |
| 111 | // } |
| 112 | // }; |
| 113 | // 修改 getAllDiscounts 和 getCurrentDiscount 方法 |
| 114 | export const getAllDiscounts = async () => { |
| 115 | try { |
| 116 | const response = await axios.get(`${API_BASE_URL}/discount/all`, { |
| 117 | headers: { |
| 118 | Authorization: localStorage.getItem('token') |
| 119 | } |
| 120 | }); |
| 121 | |
| 122 | if (response.data && response.data.code === 200) { |
| 123 | // 更健壮的数据提取方式 |
| 124 | return response.data.data?.data || response.data.data || []; |
| 125 | } else { |
| 126 | throw new Error(response.data?.message || "获取折扣信息失败"); |
| 127 | } |
| 128 | } catch (error) { |
| 129 | console.error('获取折扣列表失败:', error); |
| 130 | throw error; |
| 131 | } |
| 132 | }; |
| 133 | |
| 134 | export const getCurrentDiscount = async () => { |
| 135 | try { |
| 136 | const response = await axios.get(`${API_BASE_URL}/discount/current`, { |
| 137 | headers: { |
| 138 | Authorization: localStorage.getItem('token') |
| 139 | } |
| 140 | }); |
| 141 | |
| 142 | if (response.data && response.data.code === 200) { |
| 143 | // 更健壮的数据提取方式 |
| 144 | return response.data.data?.data || response.data.data || null; |
| 145 | } else if (response.data?.message === "目前没有进行中的折扣") { |
| 146 | return null; |
| 147 | } else { |
| 148 | throw new Error(response.data?.message || "获取当前折扣失败"); |
| 149 | } |
| 150 | } catch (error) { |
| 151 | console.error('获取当前折扣失败:', error); |
| 152 | throw error; |
| 153 | } |
| 154 | }; |
| 155 | |
| 156 | // 添加折扣 |
| 157 | export const addDiscount = async (discountData) => { |
| 158 | try { |
| 159 | const response = await axios.post(`${API_BASE_URL}/discount/add`, discountData, { |
| 160 | headers: { |
| 161 | Authorization: localStorage.getItem('token') |
| 162 | } |
| 163 | }); |
| 164 | |
| 165 | if (response.data && response.data.code === 200) { |
| 166 | return response.data.data.data || response.data.data; |
| 167 | } else { |
| 168 | throw new Error(response.data?.message || "添加折扣失败"); |
| 169 | } |
| 170 | } catch (error) { |
| 171 | console.error('添加折扣失败:', error); |
| 172 | throw error; |
| 173 | } |
| 174 | }; |
| 175 | |
| 176 | // 删除折扣 |
| 177 | export const deleteDiscount = async (id) => { |
| 178 | try { |
| 179 | const response = await axios.delete(`${API_BASE_URL}/discount/delete/${id}`, { |
| 180 | headers: { |
| 181 | Authorization: localStorage.getItem('token') |
| 182 | } |
| 183 | }); |
| 184 | |
| 185 | if (response.data && response.data.code === 200) { |
| 186 | return true; |
| 187 | } else { |
| 188 | throw new Error(response.data?.message || "删除折扣失败"); |
| 189 | } |
| 190 | } catch (error) { |
| 191 | console.error('删除折扣失败:', error); |
| 192 | throw error; |
| 193 | } |
| 194 | }; |