From 77ebcfaa600d6cba4af1c4caff3c9cd410e471d9 Mon Sep 17 00:00:00 2001 From: zhl Date: Mon, 26 Apr 2021 15:33:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/api.md | 77 ++++++++++++++++++++++++ src/api.server.ts | 10 +-- src/api/controllers/puzzle.controller.ts | 4 +- 3 files changed, 84 insertions(+), 7 deletions(-) create mode 100644 doc/api.md diff --git a/doc/api.md b/doc/api.md new file mode 100644 index 0000000..bf23934 --- /dev/null +++ b/doc/api.md @@ -0,0 +1,77 @@ +# 答题游戏接口说明 +## 一. 说明 + +所有接口均需上传sessionid + +通用返回JSON结构, 接口Response的数据结构说明只包含data部分 +``` JSON +{ + "errcode": 0, //0:成功 2: 缺少必要参数(accountid, sessionid) 4: 帐号被封, 5: 帐号未找到 100: 所有未定义的错误 + "errmsg": "", //错误描述 + "data": {}, // 数据 +} +``` + + +## 二. 客户端接口列表 + +### 1. 获取关卡题目列表 + +1. Method: POST +2. URI: /api/:accountid/puzzle/list + +| 字段 | 说明 | +| -------- | -------------------------------------- | +| accountid | 帐号id | + +> POST参数 + + +| 字段 | 说明 | +| -------- | -------------------------------------- | +| shop | 店铺id | +| level | 关卡id | + +3. Response: JSON + +```js +{ + "id": "6080f330b9655b5c0467ee5e", // 题目id + "title": "“大丈夫为国捐躯,死而无憾!”这话是谁说的?", // 问题 + "answers": [ // 可选答案 + "刘铭传", + "徐骧", + "刘步蟾", + "刘永福" + ], + "type": 1 // 题目类型 1: 普通的文字选择题, 2: 图形 +} +``` + +### 1. 获取关卡题目列表 + +1. Method: POST +2. URI: /api/:accountid/puzzle/answer + +| 字段 | 说明 | +| -------- | -------------------------------------- | +| accountid | 帐号id | + +> POST参数 + + +| 字段 | 说明 | +| -------- | -------------------------------------- | +| id | 题目id | +| level | 关卡id | +| answer | 回答的选项 | + +3. Response: JSON + +```js +{ + result: 1 //答题结果 1: 正确, 0 : 错误 +} +``` + + diff --git a/src/api.server.ts b/src/api.server.ts index 5b02566..8a03e68 100644 --- a/src/api.server.ts +++ b/src/api.server.ts @@ -92,8 +92,8 @@ export class ApiServer { } } private setErrHandler() { - this.server.setNotFoundHandler(function(request: any, reply: { send: (arg0: { code: number; msg: string; }) => void; }){ - reply.send({code: 404, msg: 'page not found'}) + this.server.setNotFoundHandler(function(request: any, reply: { send: (arg0: { errcode: number; errmsg: string; }) => void; }){ + reply.send({errcode: 404, errmsg: 'page not found'}) }); this.server.setErrorHandler(function (error: FastifyError, request: FastifyRequest, reply: FastifyReply) { let statusCode = error && error.statusCode || 100; @@ -104,7 +104,7 @@ export class ApiServer { } else { logger.error(error) } - reply.code(200).send({code: statusCode, msg: error ? error.message : 'unknown error'}) + reply.code(200).send({errcode: statusCode, errmsg: error ? error.message : 'unknown error'}) }) } /** @@ -122,7 +122,7 @@ export class ApiServer { // @ts-ignore if (!payload.code) { payload = { - code: 0, + errcode: 0, data: payload } } @@ -136,7 +136,7 @@ export class ApiServer { self.initControllers(); self.registerRouter(); self.setErrHandler(); - // self.setFormatSend(); + self.setFormatSend(); this.server.listen({port: config.api.port}, (err: any, address: any) => { if (err) { logger.log(err) diff --git a/src/api/controllers/puzzle.controller.ts b/src/api/controllers/puzzle.controller.ts index 2bcff03..f34707d 100644 --- a/src/api/controllers/puzzle.controller.ts +++ b/src/api/controllers/puzzle.controller.ts @@ -5,7 +5,7 @@ import { ZError } from '../../common/ZError' class PuzzleController extends BaseController { @role('anon') - @router('post /puzzle/list') + @router('post /api/:accountid/puzzle/list') async list(req, res) { let { shop, level } = req.params let count = 10 @@ -31,7 +31,7 @@ class PuzzleController extends BaseController { } @role('anon') - @router('post /puzzle/answer') + @router('post /api/:accountid/puzzle/answer') async report(req, res) { let { id, answer } = req.params if (!id || !answer) {