mirror of
https://github.com/YMFE/yapi.git
synced 2024-12-03 04:50:32 +08:00
373 lines
24 KiB
HTML
373 lines
24 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="zh-CN">
|
||
|
||
<head>
|
||
<title>YApi 接口管理平台 使用手册</title>
|
||
|
||
<meta name="keywords" content="api管理,接口管理,接口文档,api文档">
|
||
|
||
|
||
<meta name="description" content="YApi 是高效、易用、功能强大的 api 管理平台,旨在为开发、产品、测试人员提供更优雅的接口管理服务。可以帮助开发者轻松创建、发布、维护 API,YApi 还为用户提供了优秀的交互体验,开发人员只需利用平台提供的接口数据写入工具以及简单的点击操作就可以实现接口的管理。">
|
||
|
||
|
||
<link rel="shortcut icon" href="./images/favicon.ico" />
|
||
<link rel="stylesheet" href="source/main.css" />
|
||
|
||
|
||
|
||
<link rel="stylesheet" href="styles/theme.css" />
|
||
|
||
|
||
<meta charset="UTF-8">
|
||
<meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
|
||
<meta name="format-detection" content="telephone=no,email=no" />
|
||
<meta http-equiv="X-UA-Compatible" content="ie=edge, chrome=1">
|
||
</head>
|
||
<body>
|
||
<div class="ydoc">
|
||
<header class="ydoc-header">
|
||
<div class="ydoc-header-area">
|
||
|
||
<a href="./index.html" class="navbar-brand"><img src="./images/logo_header@2x.png" /> YAPI</a>
|
||
|
||
<button class="ydocIcon navbar-toggle"></button>
|
||
<nav class="ydoc-nav">
|
||
<ul class="navbar-left">
|
||
|
||
<li class="">
|
||
|
||
<a href="index.html"></a>
|
||
</li>
|
||
|
||
<li class="active">
|
||
|
||
<a href="usage.html">使用手册</a>
|
||
</li>
|
||
|
||
<li class="">
|
||
|
||
<a href="devops.html">内网部署</a>
|
||
</li>
|
||
|
||
<li class="">
|
||
|
||
<a href="plugin.html">插件 Wiki</a>
|
||
</li>
|
||
|
||
<li class="">
|
||
|
||
<a href="qa.html">常见问题</a>
|
||
</li>
|
||
|
||
<li class="">
|
||
|
||
<a href="releases.html">版本记录</a>
|
||
</li>
|
||
|
||
<li class="">
|
||
|
||
<a href="http://yapi.demo.qunar.com/" target="_blank">产品演示</a>
|
||
</li>
|
||
|
||
<li class="">
|
||
|
||
<a href="api.html"></a>
|
||
</li>
|
||
|
||
</ul>
|
||
</nav>
|
||
</div>
|
||
</header>
|
||
|
||
<!-- <header style="height:20px"></header> -->
|
||
|
||
<!-- Docs page layout -->
|
||
|
||
<div class="ydoc-banner-bg">
|
||
<div class="ydoc-banner ">
|
||
<div class="ydoc-banner-area">
|
||
<h1 >YApi</h1>
|
||
<p class="desc ">高效、易用、功能强大的api管理平台,旨在为开发、产品、测试人员提供更优雅的接口管理服务。</p>
|
||
|
||
</div>
|
||
</div>
|
||
|
||
|
||
<div class="ydoc-container">
|
||
|
||
<div class="ydoc-container-content ">
|
||
|
||
|
||
<div class="content-left staticsidenav" role="complementary">
|
||
<nav class="docs-sidebar hidden-print hidden-xs hidden-sm">
|
||
<ul class="nav docs-sidenav">
|
||
|
||
<!-- <li > -->
|
||
<li >
|
||
|
||
<a href="getfamiliar.html">认识 YApi</a>
|
||
</li>
|
||
|
||
<!-- <li > -->
|
||
<li >
|
||
|
||
<a href="quickstart.html">创建第一个API</a>
|
||
</li>
|
||
|
||
<!-- <li > -->
|
||
<li >
|
||
|
||
<a href="manage.html">管理分组与项目</a>
|
||
</li>
|
||
|
||
<!-- <li > -->
|
||
<li >
|
||
|
||
<a href="project.html">项目操作</a>
|
||
</li>
|
||
|
||
<!-- <li > -->
|
||
<li >
|
||
|
||
<a href="interface.html">接口操作</a>
|
||
</li>
|
||
|
||
<!-- <li class="active" class="active" > -->
|
||
<li class="active" >
|
||
|
||
<a href="mock.html">普通 Mock</a>
|
||
</li>
|
||
|
||
<ul class="nav docs-sidenav-extend" >
|
||
|
||
<li >
|
||
<a href="#Mock介绍">Mock介绍</a>
|
||
</li>
|
||
|
||
<li >
|
||
<a href="#定义_mock_数据示例">定义 mock 数据示例</a>
|
||
</li>
|
||
|
||
<li >
|
||
<a href="#原理">原理</a>
|
||
</li>
|
||
|
||
<li >
|
||
<a href="#如何使用_Mock">如何使用 Mock</a>
|
||
</li>
|
||
|
||
<li >
|
||
<a href="#Mock_语法规范">Mock 语法规范</a>
|
||
</li>
|
||
|
||
</ul>
|
||
|
||
<!-- <li > -->
|
||
<li >
|
||
|
||
<a href="adv_mock.html">高级 Mock</a>
|
||
</li>
|
||
|
||
<!-- <li > -->
|
||
<li >
|
||
|
||
<a href="case.html">使用测试集</a>
|
||
</li>
|
||
|
||
<!-- <li > -->
|
||
<li >
|
||
|
||
<a href="data.html">数据导入</a>
|
||
</li>
|
||
|
||
</ul>
|
||
</nav>
|
||
</div>
|
||
|
||
<div class="content-right markdown-body use-sidebar" role="main">
|
||
|
||
<h2 class="subject" id="Mock介绍">Mock介绍 <a class="hashlink" href="#Mock介绍">#</a></h2> <p style='text-indent:2em;line-height:1.8em'>YApi的 Mock 功能可以根据用户的输入接口信息如协议、URL、接口名、请求头、请求参数、返回数据(<a href="#mock">返回数据</a>)生成 Mock 接口,这些接口会自动生成模拟数据,创建者可以自由构造需要的数据。 </p>
|
||
|
||
<p> <strong>mock地址解析</strong>:<code>YApi平台网址 + mock + 您的项目id + 接口实际请求path</code></p>
|
||
<p> 假设你 YApi 的部署地址为:<a href="http://yapi.xxx.com">http://yapi.xxx.com</a> 然后用这个地址作为示例</p>
|
||
<pre><code>mockd 地址: http<span class="token operator">:</span>//yapi.xxx.com/mock/<span class="token number">29</span>/api/hackathon/login
|
||
</code></pre><blockquote>
|
||
<p>注:项目 id 可以在项目设置里查看到</p>
|
||
</blockquote>
|
||
<h2 class="subject" id="定义_mock_数据示例">定义 mock 数据示例 <a class="hashlink" href="#定义_mock_数据示例">#</a></h2><p>项目 -> 接口编辑 -> 返回数据设置</p>
|
||
<p><img src="./images/usage/mock-demo.jpg" /></p>
|
||
<blockquote>
|
||
<p>注:开启 json-schema 功能后,将不再使用 mockjs 解析定义的返回数据,而是根据 json-schema 定义的数据结构,生成随机数据。</p>
|
||
</blockquote>
|
||
<h2 class="subject" id="原理">原理 <a class="hashlink" href="#原理">#</a></h2><p>YApi Mock 功能基于 node 和 <a href="http://mockjs.com">mockjs</a>,跟 Mockjs 区别是 yapi 基于 json 定义 mock ,无法使用 mockjs 原有的函数功能,正则表达式需要基于 rule 书写,示例如下:</p>
|
||
<pre><code><span class="token punctuation">{</span>
|
||
<span class="token property">"name|regexp"</span><span class="token operator">:</span> <span class="token string">"[a-z0-9_]+?"</span><span class="token punctuation">,</span>
|
||
<span class="token property">"type|regexp"</span><span class="token operator">:</span> <span class="token string">"json|text|xml"</span> //枚举数据类型可这样实现
|
||
<span class="token punctuation">}</span>
|
||
</code></pre><p>2 支持替换请求的 query, body 参数</p>
|
||
<pre><code><span class="token punctuation">{</span>
|
||
<span class="token property">"name"</span><span class="token operator">:</span> <span class="token string">"${query.name}"</span><span class="token punctuation">,</span> //请求的url是/path?name=xiaoming<span class="token punctuation">,</span> 返回的name字段是xiaoming
|
||
<span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"${body.type}"</span> //请求的requestBody type=<span class="token number">1</span><span class="token punctuation">,</span>返回的type字段是<span class="token number">1</span>
|
||
<span class="token punctuation">}</span>
|
||
</code></pre><p>其他基本用法请查看:<a href="http://mockjs.com/examples.html">Mockjs 官网</a></p>
|
||
<h2 class="subject" id="如何使用_Mock">如何使用 Mock <a class="hashlink" href="#如何使用_Mock">#</a></h2><h3 class="subject" id="1_在_js_代码直接请求yapi提供的_mock_地址(不用担心跨域问题)">1 在 js 代码直接请求yapi提供的 mock 地址(不用担心跨域问题) <a class="hashlink" href="#1_在_js_代码直接请求yapi提供的_mock_地址(不用担心跨域问题)">#</a></h3><p>在代码直接请求 yapi 提供的 mock 地址,以 jQuery 为例:</p>
|
||
<pre><code class="lang-javascript"><span class="token keyword">let</span> prefix <span class="token operator">=</span> <span class="token string">'http://yapi.xxx.com/mock/2817'</span>
|
||
$<span class="token punctuation">.</span><span class="token function">post</span><span class="token punctuation">(</span>prefix<span class="token operator">+</span><span class="token string">'/baseapi/path'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>username<span class="token punctuation">:</span> <span class="token string">'xxx'</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span><span class="token punctuation">{</span>
|
||
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span> <span class="token comment">//返回上图预览部分的数据</span>
|
||
<span class="token punctuation">}</span><span class="token punctuation">)</span>
|
||
</code></pre>
|
||
<h3 class="subject" id="2_基于本地服务器反向代理">2 基于本地服务器反向代理 <a class="hashlink" href="#2_基于本地服务器反向代理">#</a></h3><p>优点:不用修改项目代码</p>
|
||
<h4 class="subject" id="2.1_基于_nginx_反向代理">2.1 基于 nginx 反向代理 <a class="hashlink" href="#2.1_基于_nginx_反向代理">#</a></h4><pre><code class="lang-nginx"><span class="token keyword">location</span> <span class="token operator">/</span>baseapi
|
||
<span class="token punctuation">{</span>
|
||
<span class="token keyword">proxy_pass</span> <span class="token keyword">http</span><span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>yapi<span class="token punctuation">.</span>xxx<span class="token punctuation">.</span>com<span class="token operator">/</span>mock<span class="token operator">/</span><span class="token number">2817</span><span class="token operator">/</span>baseapi<span class="token punctuation">;</span> <span class="token comment">#baseapi后面没有"/"</span>
|
||
<span class="token punctuation">}</span>
|
||
</code></pre>
|
||
<h4 class="subject" id="2.2_基于_ykit_mock功能">2.2 基于 ykit mock功能 <a class="hashlink" href="#2.2_基于_ykit_mock功能">#</a></h4><pre><code class="lang-javascript"><span class="token punctuation">{</span>
|
||
pattern<span class="token punctuation">:</span> <span class="token regex">/\/api\/(.*)/</span><span class="token punctuation">,</span>
|
||
responder<span class="token punctuation">:</span> <span class="token string">'http://yapi.xxx.com/mock/58/api/$1'</span>
|
||
<span class="token punctuation">}</span>
|
||
</code></pre>
|
||
<p>上面通过正则匹配,将所有接口转到 <a href="http://yapi.xxx.com">http://yapi.xxx.com</a> 上,比如 <code>http://localhost/api/user/status</code> 会成为 <code>http://yapi.xxx.com/mock/58/api/user/status</code></p>
|
||
<p>详细使用指南: <a target="_blank" href="https://ykit.ymfe.org/plugins-mock.html#获取远程数据_Map_Remote_">ykit-config-mock</a></p>
|
||
<h4 class="subject" id="2.3_基于_ykit_Jerry_代理">2.3 基于 ykit Jerry 代理 <a class="hashlink" href="#2.3_基于_ykit_Jerry_代理">#</a></h4><p>假设您本地服务器访问地址是: <a href="http://xxx.com">http://xxx.com</a></p>
|
||
<p><img src="./images/ykit.jpg" /></p>
|
||
<p><span id="mock"></span></p>
|
||
<h4 class="subject" id="2.4_基于_Charles_代理">2.4 基于 Charles 代理 <a class="hashlink" href="#2.4_基于_Charles_代理">#</a></h4><p>点击 Charles 工具栏下的 tools >> Rewrite Settings 填写如下信息:</p>
|
||
<p><img src="./images/charles.png" width="60%" /></p>
|
||
<h2 class="subject" id="Mock_语法规范">Mock 语法规范 <a class="hashlink" href="#Mock_语法规范">#</a></h2><blockquote>
|
||
<p>了解更多Mock详情:<a href="http://mockjs.com/examples.html">Mock.js 官方文档</a></p>
|
||
</blockquote>
|
||
<p>Mock.js 的语法规范包括两部分:</p>
|
||
<p><a href="#DTD">1. 数据模板定义规范(Data Template Definition,DTD)</a></p>
|
||
<p><a href="#DPD">2. 数据占位符定义规范(Data Placeholder Definition,DPD)</a></p>
|
||
<p><span id = "DTD"></span></p>
|
||
<h3 class="subject" id="数据模板定义规范(Data_Template_Definition,DTD)">数据模板定义规范(Data Template Definition,DTD) <a class="hashlink" href="#数据模板定义规范(Data_Template_Definition,DTD)">#</a></h3><p>数据模板中的每个属性由 3 部分构成:属性名、生成规则、属性值:</p>
|
||
<pre><code>// 属性名 name (与生成规则之间用 <span class="token string">"|"</span> 隔开)
|
||
// 生成规则 rule(生成规则有<span class="token number">7</span>种详见下面的生成规则)
|
||
// 属性值 value(可以含有 <span class="token string">"@占位符"</span> 同时也指定了最终值的初始值和类型)
|
||
|
||
'name|rule'<span class="token operator">:</span> value
|
||
|
||
生成规则:
|
||
'name|min-max'<span class="token operator">:</span> value
|
||
'name|count'<span class="token operator">:</span> value
|
||
'name|min-max.dmin-dmax'<span class="token operator">:</span> value
|
||
'name|min-max.dcount'<span class="token operator">:</span> value
|
||
'name|count.dmin-dmax'<span class="token operator">:</span> value
|
||
'name|count.dcount'<span class="token operator">:</span> value
|
||
'name|+step'<span class="token operator">:</span> value
|
||
</code></pre><p>下面提供了6种生成规则以及示例包括 String、Number、Boolean、Object、Array:</p>
|
||
<h3 class="subject" id="1._属性值是字符串_String">1. 属性值是字符串 String <a class="hashlink" href="#1._属性值是字符串_String">#</a></h3><pre><code><span class="token number">1</span>. 'name|min-max'<span class="token operator">:</span> string
|
||
|
||
通过重复 string 生成一个字符串,重复次数大于等于 min,小于等于 max。
|
||
|
||
<span class="token number">2</span>. 'name|count'<span class="token operator">:</span> string
|
||
|
||
通过重复 string 生成一个字符串,重复次数等于 count。
|
||
</code></pre><h3 class="subject" id="2._属性值是数字_Number">2. 属性值是数字 Number <a class="hashlink" href="#2._属性值是数字_Number">#</a></h3><pre><code><span class="token number">1</span>. 'name|+<span class="token number">1</span>'<span class="token operator">:</span> number
|
||
|
||
属性值自动加 <span class="token number">1</span>,初始值为 number。
|
||
|
||
<span class="token number">2</span>. 'name|min-max'<span class="token operator">:</span> number
|
||
|
||
生成一个大于等于 min、小于等于 max 的整数,属性值 number 只是用来确定类型。
|
||
|
||
<span class="token number">3</span>. 'name|min-max.dmin-dmax'<span class="token operator">:</span> number
|
||
|
||
生成一个浮点数,整数部分大于等于 min、小于等于 max,小数部分保留 dmin 到 dmax 位。
|
||
|
||
例如:
|
||
Mock.mock(<span class="token punctuation">{</span>
|
||
'number1|<span class="token number">1</span>-<span class="token number">100.1</span>-<span class="token number">10</span>'<span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
|
||
'number2|<span class="token number">123.1</span>-<span class="token number">10</span>'<span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
|
||
'number3|<span class="token number">123.3</span>'<span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
|
||
'number4|<span class="token number">123.10</span>'<span class="token operator">:</span> <span class="token number">1.123</span>
|
||
<span class="token punctuation">}</span><span class="token punctuation">)</span>
|
||
// =>
|
||
<span class="token punctuation">{</span>
|
||
<span class="token property">"number1"</span><span class="token operator">:</span> <span class="token number">12.92</span><span class="token punctuation">,</span>
|
||
<span class="token property">"number2"</span><span class="token operator">:</span> <span class="token number">123.51</span><span class="token punctuation">,</span>
|
||
<span class="token property">"number3"</span><span class="token operator">:</span> <span class="token number">123.777</span><span class="token punctuation">,</span>
|
||
<span class="token property">"number4"</span><span class="token operator">:</span> <span class="token number">123.1231091814</span>
|
||
<span class="token punctuation">}</span>
|
||
</code></pre><h3 class="subject" id="3._属性值是布尔型_Boolean">3. 属性值是布尔型 Boolean <a class="hashlink" href="#3._属性值是布尔型_Boolean">#</a></h3><pre><code><span class="token number">1</span>. 'name|<span class="token number">1</span>'<span class="token operator">:</span> boolean
|
||
|
||
随机生成一个布尔值,值为 <span class="token boolean">true</span> 的概率是 <span class="token number">1</span>/<span class="token number">2</span>,值为 <span class="token boolean">false</span> 的概率同样是 <span class="token number">1</span>/<span class="token number">2</span>。
|
||
|
||
<span class="token number">2</span>. 'name|min-max'<span class="token operator">:</span> value
|
||
|
||
随机生成一个布尔值,值为 value 的概率是 min / (min + max<span class="token punctuation">)</span>,值为 !value 的概率是 max / (min + max<span class="token punctuation">)</span>。
|
||
</code></pre><h3 class="subject" id="4._属性值是对象_Object">4. 属性值是对象 Object <a class="hashlink" href="#4._属性值是对象_Object">#</a></h3><pre><code><span class="token number">1</span>. 'name|count'<span class="token operator">:</span> object
|
||
|
||
从属性值 object 中随机选取 count 个属性。
|
||
|
||
<span class="token number">2</span>. 'name|min-max'<span class="token operator">:</span> object
|
||
|
||
从属性值 object 中随机选取 min 到 max 个属性。
|
||
</code></pre><h3 class="subject" id="5._属性值是数组_Array">5. 属性值是数组 Array <a class="hashlink" href="#5._属性值是数组_Array">#</a></h3><pre><code><span class="token number">1</span>. 'name|<span class="token number">1</span>'<span class="token operator">:</span> array
|
||
|
||
从属性值 array 中随机选取 <span class="token number">1</span> 个元素,作为最终值。
|
||
|
||
<span class="token number">2</span>. 'name|+<span class="token number">1</span>'<span class="token operator">:</span> array
|
||
|
||
从属性值 array 中顺序选取 <span class="token number">1</span> 个元素,作为最终值。
|
||
|
||
<span class="token number">3</span>. 'name|min-max'<span class="token operator">:</span> array
|
||
|
||
通过重复属性值 array 生成一个新数组,重复次数大于等于 min,小于等于 max。
|
||
|
||
<span class="token number">4</span>. 'name|count'<span class="token operator">:</span> array
|
||
|
||
通过重复属性值 array 生成一个新数组,重复次数为 count。
|
||
</code></pre><p><span id = "DPD"></span></p>
|
||
<h3 class="subject" id="数据占位符定义规范(Data_Placeholder_Definition,DPD)">数据占位符定义规范(Data Placeholder Definition,DPD) <a class="hashlink" href="#数据占位符定义规范(Data_Placeholder_Definition,DPD)">#</a></h3><pre><code>占位符 只是在属性值字符串中占个位置,并不出现在最终的属性值中。
|
||
|
||
占位符 的格式为:
|
||
|
||
@占位符
|
||
|
||
说明:
|
||
<span class="token number">1</span>. 用 @ 来标识其后的字符串是 占位符,在YApi提供的Mock输入框在输入“@”后会自动提示占位符。
|
||
|
||
例如:
|
||
name<span class="token operator">:</span> <span class="token punctuation">{</span>
|
||
first<span class="token operator">:</span> '@FIRST'<span class="token punctuation">,</span>
|
||
middle<span class="token operator">:</span> '@FIRST'<span class="token punctuation">,</span>
|
||
last<span class="token operator">:</span> '@LAST'<span class="token punctuation">,</span>
|
||
full<span class="token operator">:</span> '@first @middle @last'
|
||
<span class="token punctuation">}</span>
|
||
// 上面的示例可以得到如下结果:
|
||
<span class="token property">"name"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
|
||
<span class="token property">"first"</span><span class="token operator">:</span> <span class="token string">"Charles"</span><span class="token punctuation">,</span>
|
||
<span class="token property">"middle"</span><span class="token operator">:</span> <span class="token string">"Brenda"</span><span class="token punctuation">,</span>
|
||
<span class="token property">"last"</span><span class="token operator">:</span> <span class="token string">"Lopez"</span><span class="token punctuation">,</span>
|
||
<span class="token property">"full"</span><span class="token operator">:</span> <span class="token string">"Charles Brenda Lopez"</span>
|
||
<span class="token punctuation">}</span>
|
||
</code></pre>
|
||
</div>
|
||
</div>
|
||
|
||
</div>
|
||
|
||
</div>
|
||
|
||
<footer class="footer">
|
||
<div class="copyright">
|
||
© 2018 <a href="https://ymfe.org">YMFE</a> Team. Build by <a href="http://ued.qunar.com/ydoc/">ydoc</a>
|
||
</div>
|
||
</footer>
|
||
</div>
|
||
|
||
|
||
<div class="open-panel"></div>
|
||
<div class="mask"></div>
|
||
|
||
|
||
<script src="source/main.js"></script>
|
||
<script src="source/app.js"></script>
|
||
|
||
|
||
</body>
|
||
|
||
</html>
|