代码:
const request = require('request');
const jimp = require('jimp');
function createShop() {
Promise.all([
request({
uri:"https://fnbr.co/api/shop",
headers: {
"x-api-key":"api-key"
}
}),
jimp.read("./Images/shopBack.jpg"),
jimp.read("./Images/Rarities/uncommon.png"),
jimp.read("./Images/Rarities/rare.png"),
jimp.read("./Images/Rarities/epic.png"),
jimp.read("./Images/Rarities/legendary.png"),
jimp.read("./Images/backdrop.png"),
jimp.loadFont("./Fonts/Burbank32.fnt"),
jimp.loadFont("./Fonts/Burbank28.fnt"),
jimp.loadFont("./Fonts/Burbank60.fnt"),
jimp.loadFont("./Fonts/Burbank96.fnt"),
])
.then(values => {
let [, shopImage, uncommonImage, rareImage, epicImage, legendaryImage, backDrop, font32, font28, titleFont, mainTitle] = values;
let imageMap = {uncommon: uncommonImage, rare: rareImage, epic: epicImage, legendary: legendaryImage};
let shop = JSON.parse(values[0]);
let daily = shop.data.daily;
let featured = shop.data.featured;
let dailyImgs = Promise.all(daily.map(obj => jimp.read(obj.images.icon)));
let featImgs = Promise.all(featured.map(obj => jimp.read(obj.images.icon)));
return Promise.all([
dailyImgs,
featImgs,
values,
daily,
featured,
imageMap
]);
})
.then(shopImages => {
let [dailyImgs, featImgs, [, shopImage,,,,, backDrop, font32, font28, titleFont, mainTitle], daily, featured, imageMap] = shopImages;
let i = 0;
let iconSize = 225;
let rarityDiff = 25;
let xPad = 100;
let yPad = 250;
let columns = 3;
let infoBoxHeight = 80;
let width = shopImage.bitmap.width, height = shopImage.bitmap.height;
let Title = "Fortnite Battle Royale Item Shop";
let titleWidth = measureText(mainTitle, Title);
let dailyWidth = measureText(titleFont, "Daily Items");
let featuredWidth = measureText(titleFont, "Featured Items");
shopImage.print(mainTitle, Math.floor(width / 2 - titleWidth / 2), 50, Title)
.print(titleFont, Math.floor((iconSize*columns + rarityDiff*3 + xPad) / 2 - dailyWidth / 2), yPad-65, "Daily Items")
.print(titleFont, Math.floor(width-(iconSize*columns + rarityDiff*3 + xPad) / 2 - featuredWidth / 2), yPad-65, "Featured Items");
dailyImgs.map(x => {
let itemNameWidth = measureText(font32, daily[i].name);
let itemPriceWidth = measureText(font28, daily[i].price + " V-Bucks");
x.resize(iconSize, iconSize);
let infoBox = backDrop.clone()
.resize(iconSize, infoBoxHeight)
.print(font32, Math.floor(iconSize / 2 - itemNameWidth / 2), 5, daily[i].name)
.print(font28, Math.floor(iconSize / 2 - itemPriceWidth / 2), 40, daily[i].price + " V-Bucks");
let rarityImage = imageMap[daily[i].rarity]
.clone()
.resize(iconSize, iconSize)
.composite(x, 0, 0)
.composite(infoBox, 0, iconSize-infoBoxHeight);
shopImage.composite(rarityImage, (i%columns)*(iconSize+rarityDiff)+xPad, Math.floor(i/columns)*(iconSize+rarityDiff)+yPad);
i++;
});
i = 0;
featImgs.map(x => {
let itemNameWidth = measureText(font32, featured[i].name);
let itemPriceWidth = measureText(font28, featured[i].price + " V-Bucks");
x.resize(iconSize, iconSize);
let infoBox = backDrop.clone()
.resize(iconSize, infoBoxHeight)
.print(font32, Math.floor(iconSize / 2 - itemNameWidth / 2), 5, featured[i].name)
.print(font28, Math.floor(iconSize / 2 - itemPriceWidth / 2), 40, featured[i].price + " V-Bucks");
let rarityImage = imageMap[featured[i].rarity]
.clone()
.resize(iconSize, iconSize)
.composite(x, 0, 0)
.composite(infoBox, 0, iconSize-infoBoxHeight);
shopImage.composite(rarityImage, width-((i%columns)*(iconSize+rarityDiff)+xPad*2+(iconSize+rarityDiff)/2), Math.floor(i/columns)*(iconSize+rarityDiff)+yPad);
i++;
});
shopImage.quality(100)
.write("./Shop.jpg");
console.log(chalk.blue("Successfully created and saved shop image to ./Shop.jpg"));
});
}
这就是我应该得到的:
{"status":200,"data":{"date":"2018-06-03T00:00:00.526Z","featured":[{"id":"5b0e947cdb94f1f04cc0a8e8","name":"Rapscallion","price":"1,500","priceIcon":"vbucks","priceIconLink":"https://image.fnbr.co/price/icon_vbucks.png","images":{"icon":"https://image.fnbr.co/outfit/5b0e947cdb94f1f04cc0a8e8/icon.png","png":"https://image.fnbr.co/outfit/5b0e947cdb94f1f04cc0a8e8/png.png","gallery":"https://image.fnbr.co/outfit/5b0e947cdb94f1f04cc0a8e8/gallery.jpg","featured":"https://image.fnbr.co/outfit/5b0e947cdb94f1f04cc0a8e8/featured.png"},"rarity":"epic","type":"outfit","readableType":"Outfit"},{"id":"5b0e9467db94f14a2ac0a8e7","name":"Scoundrel","price":"1,500","priceIcon":"vbucks","priceIconLink":"https://image.fnbr.co/price/icon_vbucks.png","images":{"icon":"https://image.fnbr.co/outfit/5b0e9467db94f14a2ac0a8e7/icon.png","png":"https://image.fnbr.co/outfit/5b0e9467db94f14a2ac0a8e7/png.png","gallery":"https://image.fnbr.co/outfit/5b0e9467db94f14a2ac0a8e7/gallery.jpg","featured":"https://image.fnbr.co/outfit/5b0e9467db94f14a2ac0a8e7/featured.png"},"rarity":"epic","type":"outfit","readableType":"Outfit"},{"id":"5b0e93d4db94f173b8c0a8de","name":"Starry Flight","price":"800","priceIcon":"vbucks","priceIconLink":"https://image.fnbr.co/price/icon_vbucks.png","images":{"icon":"https://image.fnbr.co/glider/5b0e93d4db94f173b8c0a8de/icon.png","png":"https://image.fnbr.co/glider/5b0e93d4db94f173b8c0a8de/png.png","gallery":"https://image.fnbr.co/glider/5b0e93d4db94f173b8c0a8de/gallery.jpg","featured":"https://image.fnbr.co/glider/5b0e93d4db94f173b8c0a8de/featured.png"},"rarity":"rare","type":"glider","readableType":"Glider"},{"id":"5b0e934bdb94f1dc5dc0a8d9","name":"Nite Owl","price":"800","priceIcon":"vbucks","priceIconLink":"https://image.fnbr.co/price/icon_vbucks.png","images":{"icon":"https://image.fnbr.co/pickaxe/5b0e934bdb94f1dc5dc0a8d9/icon.png","png":"https://image.fnbr.co/pickaxe/5b0e934bdb94f1dc5dc0a8d9/png.png","gallery":"https://image.fnbr.co/pickaxe/5b0e934bdb94f1dc5dc0a8d9/gallery.jpg","featured":"https://image.fnbr.co/pickaxe/5b0e934bdb94f1dc5dc0a8d9/featured.png"},"rarity":"rare","type":"pickaxe","readableType":"Pickaxe"}],"daily":[{"id":"5ab164dcc3a23b4380359484","name":"Breakin'","price":"800","priceIcon":"vbucks","priceIconLink":"https://image.fnbr.co/price/icon_vbucks.png","images":{"icon":"https://image.fnbr.co/emote/5ab164dcc3a23b4380359484/icon.png","png":"https://image.fnbr.co/emote/5ab164dcc3a23b4380359484/png.png","gallery":"https://image.fnbr.co/emote/5ab164dcc3a23b4380359484/gallery.jpg","featured":false},"rarity":"epic","type":"emote","readableType":"Emote"},{"id":"5ae65cf81b8f51dde9a4a2d2","name":"Tidy","price":"500","priceIcon":"vbucks","priceIconLink":"https://image.fnbr.co/price/icon_vbucks.png","images":{"icon":"https://image.fnbr.co/emote/5ae65cf81b8f51dde9a4a2d2/icon.png","png":"https://image.fnbr.co/emote/5ae65cf81b8f51dde9a4a2d2/png.png","gallery":"https://image.fnbr.co/emote/5ae65cf81b8f51dde9a4a2d2/gallery.jpg","featured":false},"rarity":"rare","type":"emote","readableType":"Emote"},{"id":"5ab175b95f957f27504aa516","name":"Brilliant Striker","price":"1,200","priceIcon":"vbucks","priceIconLink":"https://image.fnbr.co/price/icon_vbucks.png","images":{"icon":"https://image.fnbr.co/outfit/5ab175b95f957f27504aa516/icon.png","png":"https://image.fnbr.co/outfit/5ab175b95f957f27504aa516/png.png","gallery":"https://image.fnbr.co/outfit/5ab175b95f957f27504aa516/gallery.jpg","featured":"https://image.fnbr.co/outfit/5ab175b95f957f27504aa516/featured.png"},"rarity":"rare","type":"outfit","readableType":"Outfit"},{"id":"5abcf2199542fb574bda12a4","name":"Midnight Ops","price":"1,200","priceIcon":"vbucks","priceIconLink":"https://image.fnbr.co/price/icon_vbucks.png","images":{"icon":"https://image.fnbr.co/outfit/5abcf2199542fb574bda12a4/icon.png","png":"https://image.fnbr.co/outfit/5abcf2199542fb574bda12a4/png.png","gallery":"https://image.fnbr.co/outfit/5abcf2199542fb574bda12a4/gallery.jpg","featured":"https://image.fnbr.co/outfit/5abcf2199542fb574bda12a4/featured.png"},"rarity":"rare","type":"outfit","readableType":"Outfit"},{"id":"5ab17bef5f957f27504aa530","name":"Plunja","price":"800","priceIcon":"vbucks","priceIconLink":"https://image.fnbr.co/price/icon_vbucks.png","images":{"icon":"https://image.fnbr.co/pickaxe/5ab17bef5f957f27504aa530/icon.png","png":"https://image.fnbr.co/pickaxe/5ab17bef5f957f27504aa530/png.png","gallery":"https://image.fnbr.co/pickaxe/5ab17bef5f957f27504aa530/gallery.jpg","featured":"https://image.fnbr.co/pickaxe/5ab17bef5f957f27504aa530/featured.png"},"rarity":"rare","type":"pickaxe","readableType":"Pickaxe"},{"id":"5ab1bbde9116ac5688c6d7ee","name":"Snow Squall","price":"500","priceIcon":"vbucks","priceIconLink":"https://image.fnbr.co/price/icon_vbucks.png","images":{"icon":"https://image.fnbr.co/glider/5ab1bbde9116ac5688c6d7ee/icon.png","png":"https://image.fnbr.co/glider/5ab1bbde9116ac5688c6d7ee/png.png","gallery":"https://image.fnbr.co/glider/5ab1bbde9116ac5688c6d7ee/gallery.jpg","featured":"https://image.fnbr.co/glider/5ab1bbde9116ac5688c6d7ee/featured.png"},"rarity":"uncommon","type":"glider","readableType":"Glider"}]}}
错误:
UnhandledPromiseRejectionWarning: SyntaxError: Unexpected token o in JSON at position 1
at JSON.parse (<anonymous>)
at Promise.all.then.values (C:\Users\\Documents\project\test.js:84:24)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:160:7)
答案 0 :(得分:1)
请求方法使用回调方法。我想,这就是你无法打印数据的原因。
请使用模块请求 - 承诺而不是请求。我认为应该解决你的问题。
var rp = require('request-promise');
rp({
uri:"https://fnbr.co/api/shop",
headers: {
"x-api-key":"api-key"
}
}),
jimp.read("./Images/shopBack.jpg"),
jimp.read("./Images/Rarities/uncommon.png"),
jimp.read("./Images/Rarities/rare.png"),
jimp.read("./Images/Rarities/epic.png"),
jimp.read("./Images/Rarities/legendary.png"),
jimp.read("./Images/backdrop.png"),
jimp.loadFont("./Fonts/Burbank32.fnt"),
jimp.loadFont("./Fonts/Burbank28.fnt"),
jimp.loadFont("./Fonts/Burbank60.fnt"),
jimp.loadFont("./Fonts/Burbank96.fnt"),
])
.then(values => {
console.log('values of zero is', values[0]);
let [, shopImage, uncommonImage, rareImage, epicImage, legendaryImage, backDrop, font32, font28, titleFont, mainTitle] = values;
let imageMap = {uncommon: uncommonImage, rare: rareImage, epic: epicImage, legendary: legendaryImage};
let shop = JSON.parse(values[0]);
let daily = shop.data.daily;
.....
答案 1 :(得分:0)
要调试它,请从console.log开始,使用values数组来查找它是什么,而不是像你期望的那样找到JSON。