From 6b7a104e2e37a0ffdce81a890f9b07ad37ff1e25 Mon Sep 17 00:00:00 2001 From: the1812 Date: Mon, 21 Jan 2019 00:07:00 +0800 Subject: [PATCH] Add Medal.active and deactive --- utils/medal-helper/medal-helper.js | 41 +++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/utils/medal-helper/medal-helper.js b/utils/medal-helper/medal-helper.js index d2e1e6644..f495b997b 100644 --- a/utils/medal-helper/medal-helper.js +++ b/utils/medal-helper/medal-helper.js @@ -12,17 +12,46 @@ this.name = medalName; this.upName = uname; } - static async getList() + static parseJson(text, { successAction, errorMessage, errorAction }) { - const json = JSON.parse(await Ajax.getTextWithCredentials("https://api.live.bilibili.com/i/api/medal?page=1&pageSize=256")); + const json = JSON.parse(text); if (json.code !== 0) { - logError("无法获取勋章列表."); - return []; + logError(`${errorMessage} 错误码:${json.code} ${json.message || ""}`); + return errorAction(json); } - return json.data.fansMedalList.map(it => new Medal(it)); + return successAction(json); + } + static async getList() + { + return Medal.parseJson( + await Ajax.getTextWithCredentials("https://api.live.bilibili.com/i/api/medal?page=1&pageSize=256"), + { + successAction: json => json.data.fansMedalList.map(it => new Medal(it)), + errorAction: () => [], + errorMessage: "无法获取勋章列表.", + }); + } + async activate() + { + return Medal.parseJson( + await Ajax.getTextWithCredentials(`https://api.live.bilibili.com/i/ajaxWearFansMedal?medal_id=${this.id}`), + { + successAction: () => true, + errorAction: () => false, + errorMessage: "佩戴勋章失败.", + }); + } + async deactivate() + { + return Medal.parseJson( + await Ajax.getTextWithCredentials(`https://api.live.bilibili.com/i/ajaxCancelWear`), + { + successAction: () => true, + errorAction: () => false, + errorMessage: "卸下勋章失败.", + }); } - } }; })(); \ No newline at end of file