一、安裝
1.安裝node.js,下載鏈接:https://nodejs.org/zh-cn/
2.安裝apiDoc,命令行:npm install apidoc -g
二、配置
在項目中配置apidoc.json文件(我的配置在接口文件夾下),該文件描述了項目對外提供接口的概要信息如名稱、版本、描述、文檔打開時瀏覽器顯示標題和接口缺省訪問地址。
{ "name": "ServiceEbikeAPIs", "version": "3.1.0", "description": "項目接口文檔", "title": "接口測試文檔", "url" : "http://xxxx.com/api"
}
使用樣例:
class Home { /** * 定義一個變量 用於apiGroup 因為不支持直接輸入中文 * @apiDefine test 測試 */ /** * @api {post} /Index/getVip 獲取vip列表 頁面加載時自動獲取 * @apiName GetUser * @apiGroup test * * @apiParam {string} req1 請求值 * * @apiSuccess {String} res1 返回值1 * @apiSuccessExample Success-Response: * { * res1:"test" * } */ public function test(){ } }
樣例2:
/**
* @api {post} json/friendCircleList 朋友圈動態列表
* @apiSampleRequest json/friendCircleList
*
* @apiGroup 資訊接口/朋友圈動態接口
*
* @apiParam {int32} page 頁數
* @apiParam {int32} pageSize 每頁條數
* @apiParam {int32} type 動態發送者類型(1商家 2用戶 可選)
* @apiParam {int32} type_id 動態發送者ID(商家或者用戶ID,與type對應,可選)
* @apiParam {string} orderField 排序字段
* @apiParam {number} order_sort 排序升降(-1代表降序 1代表升序)
*
* @apiSuccess {Sting[]} data 數據
* @apiSuccess {String} data.code 返回數據結果碼
* @apiSuccess {String} data.message 返回數據信息
*
* @apiSuccessExample Success-Response:
* "code": 0,
* "message": "succ",
* "data":[{
* "_id": {
* "$oid": "5dc618f1ed37514960002395"
* },
* "address": "江蘇省蘇州市",
* "content": "朋友圈動態內容",
* "id": "5dc618f1ed37514960002395",
* "zan_num": 0,
* "evaluate_num": 0,
* "create_time": "2019-10-28 12:42:44",
* "pics": [
* "http://bbt3.ff.5kb.pw/uploads/20191122/2de13ebac0e6e66bdcd11eb3c28d0d25.jpg"
* "http://bbt3.ff.5kb.pw/uploads/20191122/5b6158382ba17ed8a5a72fab3ca4f984.jpg"
* ],
* "type": 2,
* "type_id": 1,
* "is_del": 0,
* "author_info": {
* head_photo: ""
* nick_name: "散場的擁抱^_^"
* user_id: 1
* }
* }....
* ]
*/
參數具體說明參考:http://apidocjs.com
三、生成文檔
cd到項目目錄執行(我的是tp項目,文檔路徑可以自己定義),例子如下:
apidoc -i application/api/controller -o public/docs
備住:其中"
application/api/controller"為需要生成接口的項目目錄,就是根目錄下application/api文件夾下面的controller控制器,
"public/docs"為接口生成目錄,也就是項目跟目錄下的public/docs目錄下
最后會在"public/docs"下面生成接口目錄,點擊index.html就可以查看接口文檔
參考鏈接:https://www.jianshu.com/p/d324810d694d;https://www.jianshu.com/p/34eac66b47e3(感謝分享)