Use fetch api

This commit is contained in:
Mimi 2019-12-04 18:30:44 +08:00
parent 619b7e4ace
commit cb77fe55e8

View File

@ -119,7 +119,9 @@ function loadWidget(waifuPath, apiPath) {
function showHitokoto() { function showHitokoto() {
//增加 hitokoto.cn 的 API //增加 hitokoto.cn 的 API
if (Math.random() < 0.6 && messageArray.length > 0) showMessage(messageArray[Math.floor(Math.random() * messageArray.length)], 6000, 9); if (Math.random() < 0.6 && messageArray.length > 0) showMessage(messageArray[Math.floor(Math.random() * messageArray.length)], 6000, 9);
else $.getJSON("https://v1.hitokoto.cn", function(result) { else fetch("https://v1.hitokoto.cn")
.then(response => response.json())
.then(result => {
var text = `这句一言来自 <span style="color:#0099cc;">『${result.from}』</span>,是 <span style="color:#0099cc;">${result.creator}</span> 在 hitokoto.cn 投稿的。`; var text = `这句一言来自 <span style="color:#0099cc;">『${result.from}』</span>,是 <span style="color:#0099cc;">${result.creator}</span> 在 hitokoto.cn 投稿的。`;
showMessage(result.hitokoto, 6000, 9); showMessage(result.hitokoto, 6000, 9);
setTimeout(() => { setTimeout(() => {
@ -154,7 +156,9 @@ function loadWidget(waifuPath, apiPath) {
modelTexturesId = 53; //材质 ID modelTexturesId = 53; //材质 ID
} }
loadModel(modelId, modelTexturesId); loadModel(modelId, modelTexturesId);
$.getJSON(waifuPath, function(result) { fetch(waifuPath)
.then(response => response.json())
.then(result => {
result.mouseover.forEach(tips => { result.mouseover.forEach(tips => {
$(document).on("mouseover", tips.selector, function() { $(document).on("mouseover", tips.selector, function() {
var text = Array.isArray(tips.text) ? tips.text[Math.floor(Math.random() * tips.text.length)] : tips.text; var text = Array.isArray(tips.text) ? tips.text[Math.floor(Math.random() * tips.text.length)] : tips.text;
@ -195,28 +199,22 @@ function loadWidget(waifuPath, apiPath) {
var modelId = localStorage.getItem("modelId"), var modelId = localStorage.getItem("modelId"),
modelTexturesId = localStorage.getItem("modelTexturesId"); modelTexturesId = localStorage.getItem("modelTexturesId");
//可选 "rand"(随机), "switch"(顺序) //可选 "rand"(随机), "switch"(顺序)
$.ajax({ fetch(`${apiPath}/rand_textures/?id=${modelId}-${modelTexturesId}`)
cache: false, .then(response => response.json())
url: `${apiPath}/rand_textures/?id=${modelId}-${modelTexturesId}`, .then(result => {
dataType: "json",
success: function(result) {
if (result.textures["id"] == 1 && (modelTexturesId == 1 || modelTexturesId == 0)) showMessage("我还没有其他衣服呢!", 4000, 10); if (result.textures["id"] == 1 && (modelTexturesId == 1 || modelTexturesId == 0)) showMessage("我还没有其他衣服呢!", 4000, 10);
else showMessage("我的新衣服好看嘛?", 4000, 10); else showMessage("我的新衣服好看嘛?", 4000, 10);
loadModel(modelId, result.textures["id"]); loadModel(modelId, result.textures["id"]);
}
}); });
} }
function loadOtherModel() { function loadOtherModel() {
var modelId = localStorage.getItem("modelId"); var modelId = localStorage.getItem("modelId");
$.ajax({ fetch(`${apiPath}/switch/?id=${modelId}`)
cache: false, .then(response => response.json())
url: `${apiPath}/switch/?id=${modelId}`, .then(result => {
dataType: "json",
success: function(result) {
loadModel(result.model["id"]); loadModel(result.model["id"]);
showMessage(result.model["message"], 4000, 10); showMessage(result.model["message"], 4000, 10);
}
}); });
} }
} }