Skip to content

Commit 7f70213

Browse files
author
zhaoxiang
committed
added 加入mock组件,支持接口mock数据输出
1 parent f43c521 commit 7f70213

3 files changed

Lines changed: 169 additions & 4 deletions

File tree

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<?php
2+
/**
3+
* 自动构建随机的Mock数据,参考mockjs的配置
4+
* @since 2018-08-23
5+
* @author zhaoxiang <zhaoxiang051405@gmail.com>
6+
*/
7+
8+
namespace app\admin\behavior;
9+
10+
11+
use app\util\ReturnCode;
12+
use app\util\StrRandom;
13+
use app\util\Strs;
14+
use think\Config;
15+
16+
class Mock {
17+
18+
/**
19+
* 拦截并且返回Mock数据
20+
* @return \think\response\Json
21+
* @author zhaoxiang <zhaoxiang051405@gmail.com>
22+
*/
23+
public function run() {
24+
$header = Config::get('apiAdmin.CROSS_DOMAIN');
25+
26+
$config = [
27+
"myField|1-10" => "1",
28+
"myNum|1-100" => 1,
29+
"myFloat|1-100.1-10" => 1,
30+
"myFa|123.1-10" => 1,
31+
"myFb|123.3" => 1
32+
];
33+
$data = $this->buildData($config);
34+
35+
$return = ['code' => ReturnCode::SUCCESS, 'msg' => '操作成功', 'data' => $data];
36+
37+
return json($return, 200, $header);
38+
}
39+
40+
/**
41+
* 构建随机数据
42+
* @param $config
43+
* @return array
44+
* @author zhaoxiang <zhaoxiang051405@gmail.com>
45+
*/
46+
private function buildData($config) {
47+
$data = [];
48+
49+
foreach ($config as $key => $value) {
50+
$vType = gettype($value);
51+
list($name, $rule) = explode('|', $key);
52+
switch ($vType) {
53+
case 'integer':
54+
$data[$name] = $this->buildInt($rule);
55+
break;
56+
case 'array':
57+
break;
58+
case 'string':
59+
$data[$name] = $this->buildString($rule);
60+
break;
61+
case 'double':
62+
$data[$name] = $this->buildFloat($rule);
63+
break;
64+
case 'boolean':
65+
break;
66+
}
67+
}
68+
69+
return $data;
70+
}
71+
72+
/**
73+
* 构建随机浮点数
74+
* @param string $rule
75+
* @return float|int
76+
* @author zhaoxiang <zhaoxiang051405@gmail.com>
77+
*/
78+
private function buildFloat($rule = '') {
79+
$hasDot = strstr($rule, '.');
80+
if (!$hasDot) {
81+
return $this->buildInt($rule);
82+
}
83+
list($intPart, $floatPart) = explode('.', $rule);
84+
85+
$intVertical = strstr($intPart, '-');
86+
if ($intVertical) {
87+
list($intMin, $intMax) = explode('-', $intPart);
88+
} else {
89+
$intMin = $intMax = $intPart;
90+
}
91+
92+
$floatVertical = strstr($floatPart, '-');
93+
if ($floatVertical) {
94+
list($floatMin, $floatMax) = explode('-', $floatPart);
95+
} else {
96+
$floatMin = $floatMax = $floatPart;
97+
}
98+
99+
return StrRandom::randomFloat($intMin, $intMax, $floatMin, $floatMax);
100+
}
101+
102+
/**
103+
* 构建随机的整型数据
104+
* @param string $rule
105+
* @return float|integer
106+
* @author zhaoxiang <zhaoxiang051405@gmail.com>
107+
*/
108+
private function buildInt($rule = '') {
109+
$hasDot = strstr($rule, '.');
110+
if ($hasDot) {
111+
return $this->buildFloat($rule);
112+
}
113+
$hasVertical = strstr($rule, '-');
114+
if ($hasVertical) {
115+
list($min, $max) = explode('-', $rule);
116+
return mt_rand($min, $max);
117+
} else {
118+
return intval($rule);
119+
}
120+
}
121+
122+
/**
123+
* 构建随机字符串
124+
* @param string $rule
125+
* @return string
126+
* @author zhaoxiang <zhaoxiang051405@gmail.com>
127+
*/
128+
private function buildString($rule = '') {
129+
$hasVertical = strstr($rule, '-');
130+
if ($hasVertical) {
131+
list($minLen, $maxLen) = explode('-', $rule);
132+
$len = mt_rand($minLen, $maxLen);
133+
} else {
134+
$len = $rule;
135+
}
136+
137+
return Strs::randString($len);
138+
}
139+
140+
private function buildArray($rule = '') {
141+
142+
}
143+
144+
private function buildObject($rule = '') {
145+
146+
}
147+
148+
}

application/util/MockConf.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* mock配置和ApiAdmin内置的接口返回数据配置相互转化的类
4+
* @since 2018-08-23
5+
* @author zhaoxiang <zhaoxiang051405@gmail.com>
6+
*/
7+
8+
namespace app\util;
9+
10+
11+
class MockConf {
12+
13+
public function mockToApiAdmin() {
14+
15+
}
16+
17+
public function apiAdminToMock() {
18+
19+
}
20+
21+
}

application/util/StrRandom.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ class StrRandom {
2020
* @author zhaoxiang <zhaoxiang051405@gmail.com>
2121
*/
2222
public static function randomFloat($min = -999999999, $max = 999999999, $dmin = 0, $dmax = 8) {
23-
if ($max <= $min || $dmax <= $dmin) {
24-
return 0.0;
25-
}
26-
2723
$rand = '';
2824
$intNum = mt_rand($min, $max);
2925
$floatLength = mt_rand($dmin, $dmax);

0 commit comments

Comments
 (0)