2023-06-30 18:37:17 +08:00
|
|
|
pragma Singleton
|
|
|
|
|
|
|
|
import QtQuick
|
|
|
|
|
|
|
|
QtObject {
|
|
|
|
id: request
|
|
|
|
|
2023-07-05 21:14:33 +08:00
|
|
|
property string baseUrl: "https://api.hammer-hfut.tk:233/aics/main/"
|
2023-07-07 04:48:06 +08:00
|
|
|
property string searchUrl: "https://api.hammer-hfut.tk:233/aics/query/"
|
2023-07-01 15:01:36 +08:00
|
|
|
//property string baseUrl: "http://192.168.156.74:8080/"
|
2023-06-30 18:37:17 +08:00
|
|
|
|
|
|
|
// GET
|
|
|
|
function get(url, success, failure) {
|
|
|
|
var xhr = new XMLHttpRequest
|
|
|
|
xhr.open("GET", baseUrl + url)
|
|
|
|
xhr.onreadystatechange = function () {
|
|
|
|
handleResponse(xhr, success, failure)
|
|
|
|
}
|
|
|
|
xhr.send()
|
|
|
|
}
|
2023-07-07 01:22:06 +08:00
|
|
|
// GET in searchUrl
|
|
|
|
function getSearch(url, success, failure) {
|
|
|
|
var xhr = new XMLHttpRequest
|
|
|
|
xhr.open("GET", searchUrl + url)
|
|
|
|
xhr.onreadystatechange = function () {
|
|
|
|
handleResponse(xhr, success, failure)
|
|
|
|
}
|
|
|
|
xhr.send()
|
|
|
|
}
|
|
|
|
// PUT in searchUrl
|
|
|
|
function putSearch(url, arg, success, failure) {
|
|
|
|
var xhr = new XMLHttpRequest
|
|
|
|
xhr.open("PUT", searchUrl + url)
|
|
|
|
xhr.setRequestHeader('Content-Type', 'application/json')
|
|
|
|
xhr.withCredentials = true
|
|
|
|
xhr.onreadystatechange = function () {
|
|
|
|
handleResponse(xhr, success, failure)
|
|
|
|
}
|
|
|
|
xhr.send(arg)
|
|
|
|
}
|
2023-07-06 21:50:19 +08:00
|
|
|
function getAwait(url) {
|
|
|
|
var xhr = new XMLHttpRequest
|
|
|
|
xhr.open("GET", baseUrl + url, false)
|
|
|
|
xhr.send(null)
|
|
|
|
// wait for response then return response data
|
|
|
|
return xhr.responseText
|
|
|
|
}
|
|
|
|
|
2023-06-30 18:37:17 +08:00
|
|
|
// POST
|
|
|
|
function post(url, arg, success, failure) {
|
2023-07-01 15:01:36 +08:00
|
|
|
var xhr = new XMLHttpRequest
|
|
|
|
xhr.open("POST", baseUrl + url, true)
|
|
|
|
xhr.setRequestHeader('Content-Type', 'application/json')
|
|
|
|
xhr.withCredentials = true
|
|
|
|
xhr.onreadystatechange = function () {
|
|
|
|
handleResponse(xhr, success, failure)
|
|
|
|
}
|
|
|
|
xhr.send(arg)
|
|
|
|
}
|
|
|
|
|
2023-07-06 21:50:19 +08:00
|
|
|
// PUT
|
|
|
|
function put(url, arg, success, failure) {
|
|
|
|
var xhr = new XMLHttpRequest
|
|
|
|
xhr.open("PUT", baseUrl + url, true)
|
|
|
|
xhr.setRequestHeader('Content-Type', 'application/json')
|
|
|
|
xhr.withCredentials = true
|
|
|
|
xhr.onreadystatechange = function () {
|
|
|
|
handleResponse(xhr, success, failure)
|
|
|
|
}
|
|
|
|
xhr.send(arg)
|
|
|
|
}
|
|
|
|
|
2023-07-07 05:22:31 +08:00
|
|
|
function putForm(url, arg, success, failure) {
|
|
|
|
var xhr = new XMLHttpRequest
|
|
|
|
xhr.open("POST", baseUrl + url)
|
|
|
|
xhr.setRequestHeader("Content-Length", arg.length)
|
|
|
|
xhr.setRequestHeader("Content-Type",
|
|
|
|
"application/x-www-form-urlencoded;")
|
|
|
|
xhr.onreadystatechange = function () {
|
|
|
|
handleResponse(xhr, success, failure)
|
|
|
|
}
|
|
|
|
xhr.send(arg)
|
|
|
|
}
|
|
|
|
|
2023-07-01 15:01:36 +08:00
|
|
|
|
|
|
|
/* function post(url, arg, success, failure) {
|
2023-06-30 18:37:17 +08:00
|
|
|
var xhr = new XMLHttpRequest
|
|
|
|
xhr.open("POST", baseUrl + url)
|
|
|
|
xhr.setRequestHeader("Content-Length", arg.length)
|
|
|
|
xhr.setRequestHeader(
|
|
|
|
"Content-Type",
|
|
|
|
"application/x-www-form-urlencoded;") //用POST的时候一定要有这句
|
2023-07-01 15:01:36 +08:00
|
|
|
|
2023-06-30 18:37:17 +08:00
|
|
|
xhr.onreadystatechange = function () {
|
|
|
|
handleResponse(xhr, success, failure)
|
|
|
|
}
|
|
|
|
xhr.send(arg)
|
2023-07-01 15:01:36 +08:00
|
|
|
}*/
|
2023-06-30 18:37:17 +08:00
|
|
|
|
|
|
|
// 处理返回值
|
|
|
|
function handleResponse(xhr, success, failure) {
|
|
|
|
if (xhr.readyState === XMLHttpRequest.DONE) {
|
|
|
|
if (xhr.status === 200) {
|
|
|
|
if (success !== null && success !== undefined) {
|
|
|
|
var result = xhr.responseText
|
|
|
|
try {
|
|
|
|
success(result, JSON.parse(result))
|
|
|
|
} catch (e) {
|
|
|
|
success(result, {})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (failure !== null && failure !== undefined)
|
|
|
|
failure(xhr.responseText, xhr.status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|