From 7f7021371a9224bb06bb0f10d96222e4acf5ce00 Mon Sep 17 00:00:00 2001
From: zhaoxiang <zhaoxiang051405@gmail.com>
Date: Thu, 23 Aug 2018 19:48:57 +0800
Subject: [PATCH] =?UTF-8?q?added=20=E5=8A=A0=E5=85=A5mock=E7=BB=84?=
 =?UTF-8?q?=E4=BB=B6=EF=BC=8C=E6=94=AF=E6=8C=81=E6=8E=A5=E5=8F=A3mock?=
 =?UTF-8?q?=E6=95=B0=E6=8D=AE=E8=BE=93=E5=87=BA?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 application/admin/behavior/Mock.php | 148 ++++++++++++++++++++++++++++
 application/util/MockConf.php       |  21 ++++
 application/util/StrRandom.php      |   4 -
 3 files changed, 169 insertions(+), 4 deletions(-)
 create mode 100644 application/admin/behavior/Mock.php
 create mode 100644 application/util/MockConf.php

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 @@
+<?php
+/**
+ * 自动构建随机的Mock数据,参考mockjs的配置
+ * @since   2018-08-23
+ * @author  zhaoxiang <zhaoxiang051405@gmail.com>
+ */
+
+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 <zhaoxiang051405@gmail.com>
+     */
+    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 <zhaoxiang051405@gmail.com>
+     */
+    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 <zhaoxiang051405@gmail.com>
+     */
+    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 <zhaoxiang051405@gmail.com>
+     */
+    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 <zhaoxiang051405@gmail.com>
+     */
+    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 @@
+<?php
+/**
+ * mock配置和ApiAdmin内置的接口返回数据配置相互转化的类
+ * @since   2018-08-23
+ * @author  zhaoxiang <zhaoxiang051405@gmail.com>
+ */
+
+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 <zhaoxiang051405@gmail.com>
      */
     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);