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