86 lines
2.1 KiB
TypeScript
86 lines
2.1 KiB
TypeScript
import { ipcMain } from 'electron'
|
|||
|
|
import axios from 'axios'
|
||
|
|
import { DEFINE_STRING } from '../../define/define_string'
|
||
|
|
|
||
|
|
function AxiosIpc() {
|
||
|
|
// 通用 GET 请求
|
||
|
|
ipcMain.handle(DEFINE_STRING.AXIOS.HTTP_GET, async (_, url, config = {}) => {
|
||
|
|
try {
|
||
|
|
const response = await axios.get(url, config)
|
||
|
|
return {
|
||
|
|
success: true,
|
||
|
|
data: response.data,
|
||
|
|
status: response.status
|
||
|
|
}
|
||
|
|
} catch (error: any) {
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
error: error.message,
|
||
|
|
message: error.message,
|
||
|
|
status: error.response?.status,
|
||
|
|
data: error.response?.data
|
||
|
|
}
|
||
|
|
}
|
||
|
|
})
|
||
|
|
|
||
|
|
// 通用 POST 请求
|
||
|
|
ipcMain.handle(DEFINE_STRING.AXIOS.HTTP_POST, async (_, url, data = {}, config = {}) => {
|
||
|
|
try {
|
||
|
|
const response = await axios.post(url, data, config)
|
||
|
|
return {
|
||
|
|
success: true,
|
||
|
|
data: response.data,
|
||
|
|
status: response.status
|
||
|
|
}
|
||
|
|
} catch (error: any) {
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
error: error.message,
|
||
|
|
message: error.message,
|
||
|
|
status: error.response?.status,
|
||
|
|
data: error.response?.data
|
||
|
|
}
|
||
|
|
}
|
||
|
|
})
|
||
|
|
|
||
|
|
// 通用 PUT 请求
|
||
|
|
ipcMain.handle(DEFINE_STRING.AXIOS.HTTP_PUT, async (_, url, data = {}, config = {}) => {
|
||
|
|
try {
|
||
|
|
const response = await axios.put(url, data, config)
|
||
|
|
return {
|
||
|
|
success: true,
|
||
|
|
data: response.data,
|
||
|
|
status: response.status
|
||
|
|
}
|
||
|
|
} catch (error: any) {
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
error: error.message,
|
||
|
|
status: error.response?.status,
|
||
|
|
data: error.response?.data
|
||
|
|
}
|
||
|
|
}
|
||
|
|
})
|
||
|
|
|
||
|
|
// 通用 DELETE 请求
|
||
|
|
ipcMain.handle(DEFINE_STRING.AXIOS.HTTP_DELETE, async (_, url, config = {}) => {
|
||
|
|
try {
|
||
|
|
const response = await axios.delete(url, config)
|
||
|
|
return {
|
||
|
|
success: true,
|
||
|
|
data: response.data,
|
||
|
|
status: response.status
|
||
|
|
}
|
||
|
|
} catch (error: any) {
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
error: error.message,
|
||
|
|
status: error.response?.status,
|
||
|
|
data: error.response?.data
|
||
|
|
}
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
export default AxiosIpc
|