From dc2c4fb06fad1177fe724775c35cdf4577b57311 Mon Sep 17 00:00:00 2001 From: filesite Date: Wed, 10 Apr 2024 10:11:45 +0800 Subject: [PATCH] add function getHeros to get hero list --- heroUnion.mjs | 31 +++++++++++++++++++++++++++++++ router_api.mjs | 14 ++++++++++++++ test/heroUnion.test.mjs | 17 +++++++++++++++++ 3 files changed, 62 insertions(+) diff --git a/heroUnion.mjs b/heroUnion.mjs index 8bb9dbf..2feeeda 100644 --- a/heroUnion.mjs +++ b/heroUnion.mjs @@ -398,6 +398,37 @@ class HeroUnion { return this.stats; } + //获取爬虫列表 + getHeros(page, limit) { + if (typeof(page) == 'undefined') { + page = 1; + } + + if (typeof(limit) == 'undefined') { + limit = 20; + } + + if (page < 1) { + page = 1; + } + if (limit > 100) { + limit = 100; + } + + let start = (page - 1)*limit, + end = start + limit; + + if (start >= this.heros.length) { + return []; + } + + if (end > this.heros.length) { + end = this.heros.length; + } + + return this.heros.slice(start, end); + } + //初始化 async init() { await this.getConfig(); diff --git a/router_api.mjs b/router_api.mjs index c921eaa..b2b2d0e 100644 --- a/router_api.mjs +++ b/router_api.mjs @@ -194,6 +194,20 @@ router.post('/onboard/', async (req, res) => { return res.status(200).json(data); }); +router.get('/heros/', async (req, res) => { + let page = req.query.page, + limit = req.query.limit; + + if (!page || typeof(page) != 'number') { + page = 1; + } + if (!limit || typeof(limit) != 'number') { + limit = 20; + } + + return res.status(200).json(heroUnion.getHeros(page, limit)); +}); + router.get('/stats/', async (req, res) => { return res.status(200).json(heroUnion.getStats()); }); diff --git a/test/heroUnion.test.mjs b/test/heroUnion.test.mjs index 238249f..38a197a 100644 --- a/test/heroUnion.test.mjs +++ b/test/heroUnion.test.mjs @@ -43,6 +43,14 @@ test('Hero onboard test', async (t) => { assert.equal(response.status, 200); assert.equal(response.data.code, 1); + + + params.name = 'test_hero_2'; + const response2 = await axios.post(api, params, axiosConfig); + console.log(response2.data); + + assert.equal(response2.status, 200); + assert.equal(response2.data.code, 1); }); test('HeroUnion stats test', async (t) => { @@ -51,5 +59,14 @@ test('HeroUnion stats test', async (t) => { const response = await axios.get(api, axiosConfig); console.log(response.data); + assert.equal(response.status, 200); +}); + +test('HeroUnion get heros test', async (t) => { + let api = 'http://127.0.0.1:8080/api/heros/'; + + const response = await axios.get(api, axiosConfig); + console.log(response.data); + assert.equal(response.status, 200); }); \ No newline at end of file