Skip to content

3. yapi mock配置

tangcent edited this page Apr 18, 2020 · 3 revisions

可使用两种配置文件

  • [.yapi.config]仅对导出到yapi有效
  • [.easy.api.config](推荐)对所有API导出操作有效

yapi配置

可增加自定义mock规则(beta)

例如:

## 增加自定义mock规则
#mockjs官方示例: http://mockjs.com/examples.html
#定义一些基础的规则
#中国手机号
phone=1@pick(["34","35","36","37","38","39","50","5","52","58","59","57","82","87","88","70","47","30","3","32","55","56","85","86","33","53","80","89"])@string("number", 8)
#毫秒时间戳
mtimestamp=@timestamp@string("number", 3)
#0-9
digit=@natural(0,9)
#小于1000的自然数
natural_lt_1000=@natural(0,1000)
#小数点后两位
float_with_two=@natural(0,10000).@natural(0,100)
#http url
http_url=@pick(["http","https"])://www.@domain()/@string('lower',1,8)?@string('lower',2,4)=@string('number',1,2)
#objectId 只是字符和位数,不满足具体协议
objectId=@string("0123456789abcdef",24,24)

可以使用mockjs提供的规则与自定义的规则来定制最后输出到YAPI的mock信息

例如:

#常见的响应mock
mock.[c|integer]=0
mock.[code|integer]=0
mock.[status|integer]=0
mock.[ok|boolean]=true
mock.[success|boolean]=true
mock.[m|string]=
mock.[msg|string]=
mock.[message|string]=
mock.[errMsg|string]=

#常见的分页mock
mock.[*.p|integer]=1
mock.[*.l|integer]=@pick(["10","15","100"])
mock.[*.t|integer]=@natural(0,1000)
mock.[*.offset|integer]=1
mock.[*.limit|integer]=@pick(["10","15","100"])
mock.[*.total|integer]=@natural(0,1000)

# 整型的xxxTime mock为时间戳
mock.[*Time|integer]=${mtimestamp}

#性别
mock.[*.sex|integer]=@natural(0,2)
mock.[*.sex|string]=@pick(["男","女"])
mock.[*.gender|integer]=@natural(0,2)
mock.[*.gender|string]=@pick(["男","女"])

//用户信息相关
mock.[*.phone|string]=${phone}
mock.[*Phone|string]=${phone}
mock.[*.provinceName|string]=@province
mock.[*ProvinceName|string]=@province
mock.[*.cityName|string]=@city
mock.[*CityName|string]=@city
mock.[*.districtName|string]=@county
mock.[*DistrictName|string]=@county
mock.[*.address]=@cword(2,3)路@natural(1,1500)号

#链接
mock.[*.url|string]=${http_url}
mock.[*.link|string]=${http_url}
mock.[*.linkUrl|string]=${http_url}
mock.[*Link|string]=${http_url}

# integer和number更自然一些
mock.[*Type|integer]=${digit}
mock.[*Status|integer]=${digit}
mock.[*.type|integer]=${digit}
mock.[*.status|integer]=${digit}
mock.[*|integer]=@natural(0,10000)
mock.[*|number]=@float(0,10000)