diff --git a/application/admin/behavior/Mock.php b/application/admin/behavior/Mock.php new file mode 100644 index 0000000..fab78d4 --- /dev/null +++ b/application/admin/behavior/Mock.php @@ -0,0 +1,148 @@ + + */ + +namespace app\admin\behavior; + + +use app\util\ReturnCode; +use app\util\StrRandom; +use app\util\Strs; +use think\Config; + +class Mock { + + /** + * 拦截并且返回Mock数据 + * @return \think\response\Json + * @author zhaoxiang + */ + public function run() { + $header = Config::get('apiAdmin.CROSS_DOMAIN'); + + $config = [ + "myField|1-10" => "1", + "myNum|1-100" => 1, + "myFloat|1-100.1-10" => 1, + "myFa|123.1-10" => 1, + "myFb|123.3" => 1 + ]; + $data = $this->buildData($config); + + $return = ['code' => ReturnCode::SUCCESS, 'msg' => '操作成功', 'data' => $data]; + + return json($return, 200, $header); + } + + /** + * 构建随机数据 + * @param $config + * @return array + * @author zhaoxiang + */ + private function buildData($config) { + $data = []; + + foreach ($config as $key => $value) { + $vType = gettype($value); + list($name, $rule) = explode('|', $key); + switch ($vType) { + case 'integer': + $data[$name] = $this->buildInt($rule); + break; + case 'array': + break; + case 'string': + $data[$name] = $this->buildString($rule); + break; + case 'double': + $data[$name] = $this->buildFloat($rule); + break; + case 'boolean': + break; + } + } + + return $data; + } + + /** + * 构建随机浮点数 + * @param string $rule + * @return float|int + * @author zhaoxiang + */ + private function buildFloat($rule = '') { + $hasDot = strstr($rule, '.'); + if (!$hasDot) { + return $this->buildInt($rule); + } + list($intPart, $floatPart) = explode('.', $rule); + + $intVertical = strstr($intPart, '-'); + if ($intVertical) { + list($intMin, $intMax) = explode('-', $intPart); + } else { + $intMin = $intMax = $intPart; + } + + $floatVertical = strstr($floatPart, '-'); + if ($floatVertical) { + list($floatMin, $floatMax) = explode('-', $floatPart); + } else { + $floatMin = $floatMax = $floatPart; + } + + return StrRandom::randomFloat($intMin, $intMax, $floatMin, $floatMax); + } + + /** + * 构建随机的整型数据 + * @param string $rule + * @return float|integer + * @author zhaoxiang + */ + private function buildInt($rule = '') { + $hasDot = strstr($rule, '.'); + if ($hasDot) { + return $this->buildFloat($rule); + } + $hasVertical = strstr($rule, '-'); + if ($hasVertical) { + list($min, $max) = explode('-', $rule); + return mt_rand($min, $max); + } else { + return intval($rule); + } + } + + /** + * 构建随机字符串 + * @param string $rule + * @return string + * @author zhaoxiang + */ + private function buildString($rule = '') { + $hasVertical = strstr($rule, '-'); + if ($hasVertical) { + list($minLen, $maxLen) = explode('-', $rule); + $len = mt_rand($minLen, $maxLen); + } else { + $len = $rule; + } + + return Strs::randString($len); + } + + private function buildArray($rule = '') { + + } + + private function buildObject($rule = '') { + + } + +} diff --git a/application/util/MockConf.php b/application/util/MockConf.php new file mode 100644 index 0000000..d2364bc --- /dev/null +++ b/application/util/MockConf.php @@ -0,0 +1,21 @@ + + */ + +namespace app\util; + + +class MockConf { + + public function mockToApiAdmin() { + + } + + public function apiAdminToMock() { + + } + +} diff --git a/application/util/StrRandom.php b/application/util/StrRandom.php index c61fd93..97c1d71 100644 --- a/application/util/StrRandom.php +++ b/application/util/StrRandom.php @@ -20,10 +20,6 @@ class StrRandom { * @author zhaoxiang */ public static function randomFloat($min = -999999999, $max = 999999999, $dmin = 0, $dmax = 8) { - if ($max <= $min || $dmax <= $dmin) { - return 0.0; - } - $rand = ''; $intNum = mt_rand($min, $max); $floatLength = mt_rand($dmin, $dmax);