Browse Source

add function getHeros to get hero list

master
filesite 8 months ago
parent
commit
dc2c4fb06f
  1. 31
      heroUnion.mjs
  2. 14
      router_api.mjs
  3. 17
      test/heroUnion.test.mjs

31
heroUnion.mjs

@ -398,6 +398,37 @@ class HeroUnion { @@ -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();

14
router_api.mjs

@ -194,6 +194,20 @@ router.post('/onboard/', async (req, res) => { @@ -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());
});

17
test/heroUnion.test.mjs

@ -43,6 +43,14 @@ test('Hero onboard test', async (t) => { @@ -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) => {
@ -53,3 +61,12 @@ test('HeroUnion stats test', async (t) => { @@ -53,3 +61,12 @@ test('HeroUnion stats test', async (t) => {
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);
});
Loading…
Cancel
Save