biliob/Web/js/httpUtils.js

214 lines
6.2 KiB
JavaScript
Raw Normal View History

2024-11-27 17:39:02 +08:00
function get(url) {
return fetch(url)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
2024-10-30 18:27:37 +08:00
return response.json();
2024-11-27 17:39:02 +08:00
})
.then(data => {
return data;
})
.catch(error => {
return error;
});
}
function post(url, formData, isJSON) {
return sendPost(url,true,formData,isJSON)
}
function sendPost(url, isFormData, formData, isJSON) {
var obj = {};
if (isFormData) {
obj = {
method: "POST",
body: formData
2024-10-30 18:27:37 +08:00
}
2024-11-27 17:39:02 +08:00
} else {
obj = {
method: "POST",
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(formData)
2024-10-30 18:27:37 +08:00
}
2024-11-27 17:39:02 +08:00
}
return fetch(url, obj)
.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; // 重新抛出错误以便外部捕获
});
2024-10-30 18:27:37 +08:00
}
2024-11-01 00:15:12 +08:00
2024-11-27 17:39:02 +08:00
function post2blob(url, formData) {
2024-11-01 00:15:12 +08:00
return fetch(url, {
method: "POST",
body: formData
})
2024-11-27 17:39:02 +08:00
.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; // 重新抛出错误以便外部捕获
});
2024-11-01 00:15:12 +08:00
}
2024-11-27 17:39:02 +08:00
function buildFormData(json) {
2024-10-31 18:23:39 +08:00
const formData = new FormData();
Object.keys(json).forEach(key => {
const value = json[key];
2024-11-27 17:39:02 +08:00
2024-10-31 18:23:39 +08:00
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;
}
//----------------直播配置相关接口
2024-10-30 18:27:37 +08:00
function getLiveVideoList() {
2024-11-27 17:39:02 +08:00
return get("/live/video/list");
2024-10-30 18:27:37 +08:00
}
function getHttpImage(url) {
const encode = encodeURI(url);
const formData = new FormData();
formData.append("url", encode);
2024-11-27 17:39:02 +08:00
return post("/file/img", formData, false)
.then(blob => {
return blob;
})
2024-10-30 18:27:37 +08:00
}
2024-11-01 00:15:12 +08:00
function getHttpTmpImage(url) {
const encode = encodeURI(url);
const formData = new FormData();
formData.append("url", encode);
2024-11-27 17:39:02 +08:00
return post2blob("/file/imgTmp", formData)
.then(blob => {
return blob;
})
}
function addRoomConfig(config) {
const formData = buildFormData(config)
return post("/live/config/set", formData, true)
}
function setArrayRoomConfig(rooms, config) {
var json = {
"config": config,
"array": rooms
}
return sendPost("/live/config/set/array", false, json, true)
}
function deleteArrayRoomConfig(rooms) {
return sendPost("/live/config/delete/array", false,rooms, true)
2024-11-01 00:15:12 +08:00
}
2024-11-27 17:39:02 +08:00
function deleteAllRoomConfig() {
return get("/live/config/delete/all")
2024-10-31 18:23:39 +08:00
}
2024-11-27 17:39:02 +08:00
function getRoomConfig(roomId) {
return get("/live/config/get?roomId=" + roomId)
2024-10-31 18:23:39 +08:00
}
2024-11-27 17:39:02 +08:00
function deleteRoomConfig(roomId) {
return get("/live/config/delete?roomId=" + roomId)
2024-10-31 18:23:39 +08:00
}
2024-11-27 17:39:02 +08:00
function checkFollowStatus(userId) {
return get("/live/config/follow/check?userId=" + userId)
2024-11-21 18:29:19 +08:00
}
2024-11-27 17:39:02 +08:00
function confirmFollowStatus(userId) {
return get("/live/config/follow/confirm?userId=" + userId)
2024-11-21 18:29:19 +08:00
}
2024-11-27 17:39:02 +08:00
function addAllFollow(userId) {
return get("/live/config/follow/all?userId=" + userId)
2024-11-21 18:29:19 +08:00
}
2024-11-27 17:39:02 +08:00
function addFollow(uid, anchorId) {
return get("/live/config/follow/add?anchorId=" + anchorId + "&uid=" + uid)
}
function addFollowList(uid, array) {
const formData = new FormData();
formData.append("array", array);
return post("/live/config/follow/addList?uid=" + uid, formData, true)
2024-11-21 18:29:19 +08:00
}
2024-11-27 17:39:02 +08:00
function addFollowRoomList(array) {
2024-11-21 18:29:19 +08:00
const formData = new FormData();
formData.append("array", array);
2024-11-27 17:39:02 +08:00
return post("/live/config/follow/roomId/addList", formData, true)
2024-11-21 18:29:19 +08:00
}
2024-10-31 18:23:39 +08:00
//----------------直播配置相关接口end
//----------------首页相关接口
2024-11-27 17:39:02 +08:00
function getAllLive(page, limit) {
return get("/live/list?page=" + page + "&limit=" + limit)
2024-10-31 18:23:39 +08:00
}
2024-11-27 17:39:02 +08:00
function getAllConfig(page, limit) {
return get("/live/config/all?page=" + page + "&limit=" + limit)
2024-11-01 18:21:57 +08:00
}
2024-10-31 18:23:39 +08:00
//----------------首页相关接口end
2024-11-01 18:21:57 +08:00
//----------------直播视频相关接口
2024-11-27 17:39:02 +08:00
function startLiveVideo(roomId) {
return get("/live/video/start?roomId=" + roomId)
2024-10-31 18:23:39 +08:00
}
2024-11-27 17:39:02 +08:00
function stopLiveVideo(roomId) {
return get("/live/video/stop?roomId=" + roomId)
2024-10-31 18:23:39 +08:00
}
2024-11-27 17:39:02 +08:00
function getVideo(roomId, page, limit) {
return get("/file/list?roomId=" + roomId + "&page=" + page + "&limit=" + limit)
2024-11-01 18:21:57 +08:00
}
2024-11-27 17:39:02 +08:00
function getPlayerVideo(roomId, videoId) {
return get('/video/play?roomId=' + roomId + "&videoId=" + videoId)
2024-11-02 18:24:16 +08:00
}
2024-11-01 18:21:57 +08:00
//----------------直播视频相关接口end
2024-10-31 18:23:39 +08:00
//----------------弹幕相关接口
2024-11-27 17:39:02 +08:00
function startLiveDanmu(roomId) {
return get("/live/danmu/start?roomId=" + roomId)
2024-10-31 18:23:39 +08:00
}
2024-11-27 17:39:02 +08:00
function stopLiveDanmu(roomId) {
return get("/live/danmu/stop?roomId=" + roomId)
2024-10-31 18:23:39 +08:00
}
2024-11-27 17:39:02 +08:00
function getDanmu(roomId, videoId) {
return get("/live/danmu/get?roomId=" + roomId + "&videoId=" + videoId)
2024-11-18 18:27:49 +08:00
}
2024-10-31 18:23:39 +08:00
//----------------弹幕相关接口end
2024-11-18 18:27:49 +08:00
//----------------用户相关接口
2024-11-27 17:39:02 +08:00
function getBiliAllUser() {
2024-11-18 18:27:49 +08:00
return get("/user/list")
}
2024-11-27 17:39:02 +08:00
function login() {
2024-11-18 18:27:49 +08:00
return get("/user/login")
}
2024-11-27 17:39:02 +08:00
function refreshCookie(userId) {
return get("/user/refreshCookie?uid=" + userId)
2024-11-21 18:29:19 +08:00
}
//----------------用户相关接口end
//----------------礼物相关接口
2024-11-27 17:39:02 +08:00
function getVideoGiftInfo(roomId, videoId) {
return get("/live/gift/info?roomId=" + roomId + "&videoId=" + videoId)
2024-11-21 18:29:19 +08:00
}
//----------------礼物相关接口end