biliob/Web/js/httpUtils.js
2024-11-02 18:24:16 +08:00

150 lines
3.9 KiB
JavaScript

function get(url){
return fetch(url)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json();
})
.then(data => {
return data;
})
.catch(error => {
return error;
});
}
function post(url,formData,isJSON) {
return fetch(url, {
method: "POST",
body: formData
})
.then(response => {
if (!response.ok) {
throw new Error("Network response was not ok");
}
if(isJSON){
return response.json();
}
return response.blob();
})
.then(blob => {
if(isJSON){
return blob;
}
const imageUrl = URL.createObjectURL(blob);
return imageUrl;
})
.catch(error => {
console.error("There was a problem with the fetch operation:", error);
throw error; // 重新抛出错误以便外部捕获
});
}
function post2blob(url,formData) {
return fetch(url, {
method: "POST",
body: formData
})
.then(response => {
if (!response.ok) {
throw new Error("Network response was not ok");
}
return response.text();
})
.then(blob => {
return blob;
})
.catch(error => {
console.error("There was a problem with the fetch operation:", error);
throw error; // 重新抛出错误以便外部捕获
});
}
function buildFormData(json){
const formData = new FormData();
Object.keys(json).forEach(key => {
const value = json[key];
if (Array.isArray(value)) {
value.forEach((item, index) => {
formData.append(`${key}[${index}]`, item);
});
} else if (value !== null && typeof value === 'object') {
formData.append(key, value);
} else {
formData.append(key, value);
}
});
return formData;
}
//----------------直播配置相关接口
function getLiveVideoList() {
return get("/live/video/list");
}
function getHttpImage(url) {
const encode = encodeURI(url);
const formData = new FormData();
formData.append("url", encode);
return post("/file/img",formData,false)
.then(blob=>{
return blob;
})
}
function getHttpTmpImage(url) {
const encode = encodeURI(url);
const formData = new FormData();
formData.append("url", encode);
return post2blob("/file/imgTmp",formData)
.then(blob=>{
return blob;
})
}
function getBiliAllUser(){
return get("/user/list")
}
function addRoomConfig(json){
const formData=buildFormData(json)
return post("/live/config/set",formData,true)
}
function getRoomConfig(roomId){
return get("/live/config/get?roomId="+roomId)
}
function deleteRoomConfig(roomId){
return get("/live/config/delete?roomId="+roomId)
}
//----------------直播配置相关接口end
//----------------首页相关接口
function getAllLive(page,limit){
return get("/live/list?page="+page+"&limit="+limit)
}
function getAllConfig(page,limit){
return get("/live/config/all?page="+page+"&limit="+limit)
}
//----------------首页相关接口end
//----------------直播视频相关接口
function startLiveVideo(roomId){
return get("/live/video/start?roomId="+roomId)
}
function stopLiveVideo(roomId){
return get("/live/video/stop?roomId="+roomId)
}
function getVideo(roomId,page,limit){
return get("/file/list?roomId="+roomId+"&page="+page+"&limit="+limit)
}
function getPlayerVideo(roomId,videoId){
return get('/video/play?roomId='+roomId+"&videoId="+videoId)
}
//----------------直播视频相关接口end
//----------------弹幕相关接口
function startLiveDanmu(roomId){
return get("/live/danmu/start?roomId="+roomId)
}
function stopLiveDanmu(roomId){
return get("/live/danmu/stop?roomId="+roomId)
}
//----------------弹幕相关接口end