|
| 1 | +import axios, {AxiosInstance} from "axios"; |
| 2 | +import {sleep} from "./sleep"; |
| 3 | +import {Base64Utils} from "./base64"; |
| 4 | + |
| 5 | +export interface MessageBox { |
| 6 | + success: (msg: string) => void; |
| 7 | + error: (msg: string) => void; |
| 8 | +} |
| 9 | + |
| 10 | +export class HttpClient { |
| 11 | + private readonly api: AxiosInstance; |
| 12 | + private readonly messageBox: MessageBox; |
| 13 | + |
| 14 | + constructor(timeout: number, messageBox: MessageBox) { |
| 15 | + this.messageBox = messageBox; |
| 16 | + this.api = axios.create({ |
| 17 | + timeout: timeout, |
| 18 | + headers: { |
| 19 | + "Content-Type": "application/json", |
| 20 | + }, |
| 21 | + }); |
| 22 | + |
| 23 | + this.addRequestInterceptors(); |
| 24 | + this.addResponseInterceptors(); |
| 25 | + } |
| 26 | + |
| 27 | + private addRequestInterceptors() { |
| 28 | + this.api.interceptors.request.use((config: any) => { |
| 29 | + const token = localStorage.getItem("token"); |
| 30 | + if (token) { |
| 31 | + config.headers = { |
| 32 | + Authorization: `${token}`, |
| 33 | + } as any; |
| 34 | + } |
| 35 | + return config; |
| 36 | + }, (error: any) => { |
| 37 | + return Promise.reject(error); |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + private addResponseInterceptors() { |
| 42 | + this.api.interceptors.response.use(async (response: any) => { |
| 43 | + const headers = response.headers; |
| 44 | + const token = headers['authorization']; |
| 45 | + |
| 46 | + const state = response.status; |
| 47 | + if (state === 200) { |
| 48 | + if (token) { |
| 49 | + console.log('reset token', token); |
| 50 | + localStorage.setItem("token", token) |
| 51 | + } |
| 52 | + |
| 53 | + if (response.data) { |
| 54 | + const success = response.data.success; |
| 55 | + if (!success) { |
| 56 | + const errMessage = response.data.errMessage; |
| 57 | + const errCode = response.data.errCode; |
| 58 | + if ("token.expire" === errCode || "token.error" === errCode) { |
| 59 | + this.messageBox.error('登录已过期,请退出再重新打开'); |
| 60 | + await sleep(1500); |
| 61 | + localStorage.clear(); |
| 62 | + window.location.href = '/#login'; |
| 63 | + } else { |
| 64 | + if ("login.error" === errCode) { |
| 65 | + return response; |
| 66 | + } |
| 67 | + this.messageBox.error(errMessage) |
| 68 | + } |
| 69 | + } |
| 70 | + } else { |
| 71 | + this.messageBox.error('抱歉,该账户无权限访问'); |
| 72 | + } |
| 73 | + } |
| 74 | + return response; |
| 75 | + }, |
| 76 | + (error: any) => { |
| 77 | + const response = error.response; |
| 78 | + const state = response.data.status; |
| 79 | + |
| 80 | + if (state === 403) { |
| 81 | + this.messageBox.error('抱歉,该账户无权限访问'); |
| 82 | + return { |
| 83 | + data: { |
| 84 | + success: false, |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + return Promise.reject(error); |
| 89 | + } |
| 90 | + ) |
| 91 | + } |
| 92 | + |
| 93 | + public get = async (url: string, params?: any) => { |
| 94 | + try { |
| 95 | + const response = await this.api.get(url, { |
| 96 | + params |
| 97 | + }); |
| 98 | + return response.data; |
| 99 | + } catch (e) { |
| 100 | + return { |
| 101 | + success: false, |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + public post = async (url: string, data: any) => { |
| 107 | + try { |
| 108 | + const response = await this.api.post(url, data); |
| 109 | + return response.data; |
| 110 | + } catch (e) { |
| 111 | + return { |
| 112 | + success: false, |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | + public page = async (url: string, params: any, sort: any, filter: any, match: { |
| 119 | + key: string, |
| 120 | + type: string |
| 121 | + }[]) => { |
| 122 | + const base64Match = Base64Utils.stringToBase64(JSON.stringify(match)); |
| 123 | + const base64Sort = Base64Utils.stringToBase64(JSON.stringify(sort)); |
| 124 | + const base64Filter = Base64Utils.stringToBase64(JSON.stringify(filter)); |
| 125 | + |
| 126 | + const response = await this.get(url, { |
| 127 | + ...params, |
| 128 | + sort: base64Sort, |
| 129 | + filter: base64Filter, |
| 130 | + params: base64Match, |
| 131 | + }); |
| 132 | + |
| 133 | + if (response.success) { |
| 134 | + const list = response.data.total > 0 ? response.data.list : []; |
| 135 | + return { |
| 136 | + data: list, |
| 137 | + success: response.success, |
| 138 | + total: response.data.total |
| 139 | + }; |
| 140 | + } else { |
| 141 | + return { |
| 142 | + data: [], |
| 143 | + success: response.success, |
| 144 | + total: 0 |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + |
| 150 | + public download = async (url: string, filename?: string) => { |
| 151 | + try { |
| 152 | + const token = localStorage.getItem("token"); |
| 153 | + const response = await axios.get(url, { |
| 154 | + responseType: 'blob', |
| 155 | + headers: { |
| 156 | + 'Authorization': token, |
| 157 | + } |
| 158 | + }); |
| 159 | + const bytes = await response.data; |
| 160 | + const blob = new Blob([bytes]); |
| 161 | + const downloadUrl = window.URL.createObjectURL(blob); |
| 162 | + const a = document.createElement('a'); |
| 163 | + a.href = downloadUrl; |
| 164 | + a.download = filename || 'result.csv'; |
| 165 | + a.click(); |
| 166 | + } catch (e) { |
| 167 | + console.log(e); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + public postDownload = async (url: string, data: any, filename?: string) => { |
| 172 | + try { |
| 173 | + const token = localStorage.getItem("token"); |
| 174 | + const response = await axios.post(url, data, { |
| 175 | + responseType: 'blob', |
| 176 | + headers: { |
| 177 | + 'Authorization': token, |
| 178 | + } |
| 179 | + }); |
| 180 | + const bytes = await response.data; |
| 181 | + const blob = new Blob([bytes]); |
| 182 | + const downloadUrl = window.URL.createObjectURL(blob); |
| 183 | + const a = document.createElement('a'); |
| 184 | + a.href = downloadUrl; |
| 185 | + a.download = filename || 'result.csv'; |
| 186 | + a.click(); |
| 187 | + } catch (e) { |
| 188 | + console.log(e); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | +} |
0 commit comments