rabin 2 meses atrás
pai
commit
1bc230f19b

+ 48 - 20
boot.php

@@ -1,11 +1,33 @@
 <?php
 header('Content-Type: text/html; charset=utf-8');date_default_timezone_set("PRC");define('DEVER_TIME', $_SERVER['REQUEST_TIME']);define('DEVER_PATH', dirname(__FILE__) . DIRECTORY_SEPARATOR);
-Dever::run();
+if (defined('DEVER_SERVER')) {
+    Dever::server();
+} else {
+    Dever::run();
+}
 class Dever
 {
     public static $data = array();
-    public static $app = array();
     public static $commit = true;
+    public static function server()
+    {
+        spl_autoload_register(array('Dever', 'autoload'));
+        $http_worker = new \Workerman\Worker("http://0.0.0.0:8085");
+
+        // 启动4个进程对外提供服务
+        $http_worker->count = 4;
+
+        // 接收到浏览器发送的数据时回复hello world给浏览器
+        $http_worker->onMessage = function(\Workerman\Connection\TcpConnection $connection, $data)
+        {
+            $route = Dever\Route::get();
+            print_r($route);
+            // 向浏览器发送hello world
+            $connection->send('hello world');
+        };
+
+        \Workerman\Worker::runAll();
+    }
     public static function run()
     {
         spl_autoload_register(array('Dever', 'autoload'));
@@ -25,9 +47,11 @@ class Dever
                 }
             }
         }
-        self::$app[] = DEVER_APP_NAME;
         if ($route['l'] && strpos($route['l'], '.')) {
             list($class, $method) = explode('.', $route['l']);
+            if (strstr($class, 'manage/') && !strstr($class, 'api')) {
+                Dever\Output::error('route error');
+            }
             $result = Dever\Output::success(self::load($class, DEVER_APP_NAME, 'api')->loadDevelop($method, Dever\Route::input(), true));
         } else {
             $result = Dever\Output::success('ok');
@@ -38,7 +62,7 @@ class Dever
     }
     public static function autoload($class)
     {
-        if (strpos($class, 'Dever') === 0) {
+        if (strpos($class, 'Dever') === 0 || strpos($class, 'Workerman') === 0) {
             include DEVER_PATH . 'src/' . str_replace('\\', '/', $class) . '.php';
         } else {
             Dever\Library::autoload($class);
@@ -68,12 +92,7 @@ class Dever
     public static function load($class, $app = '', $path = 'lib')
     {
         if (strpos($class, '/') && !$app) {
-            list($app, $class) = explode('/', $class);
-        }
-        if ($app) {
-            array_unshift(self::$app, $app);
-        } else {
-            $app = self::$app[0];
+            list($app, $class) = explode('/', $class, 2);
         }
         $index = $app . $path . $class;
         if (empty(self::$data[$index])) {
@@ -86,22 +105,31 @@ class Dever
         if (strpos($table, '/') && !$app) {
             list($app, $table) = explode('/', $table);
         }
-        if ($app) {
-            array_unshift(self::$app, $app);
-        } else {
-            $app = self::$app[0];
-        }
         $index = $app . $path . $table;
         if (empty(self::$data[$index]) || !$cache) {
             self::$data[$index] = new Dever\Model($table, $app, $store, $partition, $path);
         }
         return self::$data[$index];
     }
-    public static function reset()
+    public static function option($table, $type = '', $where = [])
+    {
+        $data = Dever::db($table)->select($where);
+        if ($type) {
+            if (is_bool($type)) {
+                $type = '不选择';
+            }
+            $default = array(0 => array('id' => -1, 'name' => $type));
+            $data = array_merge($default, $data);
+        }
+        return $data;
+    }
+    public static function field($table, $id, $default = '无', $key = 'name')
     {
-        if (count(self::$app) > 1) {
-            array_shift(self::$app);
+        if ($id && $id > 0) {
+            $info = Dever::db($table)->find($id);
+            return $info[$key];
         }
+        return $default;
     }
     public static function config()
     {
@@ -256,9 +284,9 @@ class Dever
             return number_format($number, $num);
         }
         if ($type == 3) {
-            return round($number, $num);
+            return sprintf("%.".$num."f", $number);
         }
-        return sprintf("%.".$num."f", $number);
+        return round($number + 0.000001, $num);
     }
     public static function uuid()
     {

+ 2 - 2
src/Dever/Helper/Str.php

@@ -283,9 +283,9 @@ class Str
         }
         return false;
     }
-    public static function val($show, $data)
+    public static function val($show, $data = array())
     {
-        if (strpos($show, '{') !== false && strpos($show, '{"') === false) {
+        if ($data && strpos($show, '{') !== false && strpos($show, '{"') === false) {
             $func = function ($r) use ($data) {
                 if (isset($data[$r[1]])) {
                     return $data[$r[1]];

+ 0 - 1
src/Dever/Import.php

@@ -86,7 +86,6 @@ class Import
     {
         $data = $this->class->$method(...$param);
         Debug::lib($this->class, $method);
-        Dever::reset();
         return $data;
     }
 }

+ 7 - 2
src/Dever/Library.php

@@ -28,7 +28,7 @@ class Library
             $file = $path . DIRECTORY_SEPARATOR . $class;
             $class = str_replace('/', '\\', $class);
         } else {
-            $class = ucfirst($file);
+            $class = $file = ucfirst($file);
             $file = $path . DIRECTORY_SEPARATOR . $file;
         }
         $class = ucfirst($app) . '\\' . ucfirst($path) . '\\' . $class;
@@ -40,7 +40,12 @@ class Library
     }
     public static function load($class, $app, $path)
     {
-        $class = self::apply(ucfirst($class), $app, $path);
+        if (strstr($class, 'manage/')) {
+            $auth = new \Manage\Lib\Auth(true);
+            $class = str_replace('manage/', 'lib/', $class);
+            $path = 'manage';
+        }
+        $class = self::apply($class, $app, $path);
         if (empty(self::$class[$class])) {
             self::$class[$class] = new $class();
         }

+ 21 - 3
src/Dever/Model.php

@@ -36,7 +36,6 @@ class Model
         if (isset($file)) {
             $this->init($path, $store, $file);
         }
-        Dever::reset();
     }
     public function partition($partition)
     {
@@ -165,6 +164,19 @@ class Model
         }
         return $this->store->find($this->config['table'], $param, $set, $this->config['struct'], $lock);
     }
+    public function sum($param = array(), $field)
+    {
+        return $this->column($param, 'sum(`'.$field.'`)', 0);
+    }
+    public function column($param = array(), $field, $default = '')
+    {
+        $info = $this->find($param, array('col' => $field . ' as value'));
+        return $info ? $info['value'] : $default;
+    }
+    public function columns($param = array(), $field = 'id')
+    {
+        return $this->kv($param, array('col' => $field));
+    }
     public function count($param = array())
     {
         if (isset($this->partition['where']) && $this->partition['where']) {
@@ -184,8 +196,14 @@ class Model
             if (empty($set['kv'])) {
                 $set['kv'] = array('id', 'name');
             }
-            foreach ($data as $k => $v) {
-                $result[$v[$set['kv'][0]]] = $v[$set['kv'][1]];
+            if (isset($set['kv'][1])) {
+                foreach ($data as $k => $v) {
+                    $result[$v[$set['kv'][0]]] = $v[$set['kv'][1]];
+                }
+            } else {
+                foreach ($data as $k => $v) {
+                    $result[] = $v[$set['kv']];
+                }
             }
         }
         return $result;

+ 22 - 4
src/Dever/Output.php

@@ -16,6 +16,7 @@ class Output
         if ($uuid) {
             $result['uuid'] = Dever::uuid();
         }
+        $result = self::setting($result);
         self::handle($result);
         return $data;
     }
@@ -42,20 +43,37 @@ class Output
         $content .= "\n</pre>\n";
         return $content;
     }
-    public static function handle(&$result)
+    public static function setting($result)
     {
-        if (isset(Dever::config('setting')['output'])) {
-            foreach (Dever::config('setting')['output'] as $k => $v) {
+        $setting = Dever::config('setting');
+        if (isset($setting['output_app']) && $setting['output_app'] && !in_array(DEVER_APP_NAME, $setting['output_app'])) {
+            $setting['output'] = array();
+        }
+       
+        if ($setting = Dever::issets($setting, 'output')) {
+            foreach ($setting as $k => $v) {
                 if (isset($result[$k])) {
                     if (is_array($v)) {
-                        $result[$v[0]] = $v[1][$result[$k]] ?? $result[$k];
+                        if (is_array($result[$k])) {
+                            $result[$v[0]] = $result[$k][$v[1]] ?? $result[$k];
+                        } else {
+                            $result[$v[0]] = $v[1][$result[$k]] ?? $result[$k];
+                        }
                     } else {
                         $result[$v] = $result[$k];
                     }
                     unset($result[$k]);
+                } elseif (strstr($v, 'Dever')) {
+                    $result[$k] = \Dever\Helper\Str::val($v);
+                } else {
+                    $result[$k] = Dever::call($v);
                 }
             }
         }
+        return $result;
+    }
+    public static function handle(&$result)
+    {
         Debug::out($result);
         self::json($result);
         self::callback($result);

+ 1 - 1
src/Dever/Project.php

@@ -95,7 +95,7 @@ class Project
         if (isset(self::$content[$app])) {
             return self::$content[$app];
         }
+        Output::error('app not exists:' . $app);
         return false;
-        //Output::error('app not exists:' . $app);
     }
 }

+ 8 - 0
src/Dever/Route.php

@@ -11,6 +11,14 @@ class Route
         if ($condition == 'set') {
             return self::$data[$key] = $lang;
         }
+        if (is_array($key)) {
+            foreach ($key as $v) {
+                $v = self::input($v, $condition, $lang, $value);
+                if ($v) {
+                    return $v;
+                }
+            }
+        }
         if (is_string($key) && isset(self::$data[$key]) && self::$data[$key] && self::$data[$key] != 'false') {
             $value = self::$data[$key];
         }

+ 12 - 7
src/Dever/Sql.php

@@ -158,12 +158,9 @@ class Sql
             $col = $set['col'];
         }
         if (isset($set['join'])) {
-            $temp = explode('_', $table);
-            $prefix = $temp[0] . '_'  . $temp[1] . '_';
-            $temp = implode('_', array_slice($temp, 2));
-            $table .= ' AS ' . $temp;
+            $table .= ' AS main';
             foreach ($set['join'] as $k => $v) {
-                $table .= ' ' . $v['type'] . ' ' . $prefix . $v['table'] . ' AS ' . $v['table'] . ' ON ' . $v['on'];
+                $table .= ' ' . $v['type'] . ' ' . DEVER_PROJECT . '_' . $v['table'] . ' AS t' . $k . ' ON ' . $v['on'];
             }
         }
         if (isset($set['group'])) {
@@ -323,8 +320,16 @@ class Sql
             }
             $a = '';
             if (is_array($v)) {
-                $a = '`' . $k . '`' . $v[0];
-                $v = $v[1];
+                if (isset($v[2])) {
+                    $a = '`' . $v[0] . '`' . $v[1] . '`' . $v[2] . '`';
+                    $sql .= '`' . $k . '`=' . $a . ',';
+                    continue;
+                } elseif (isset($v[1])) {
+                    $a = '`' . $k . '`' . $v[0];
+                    $v = $v[1];
+                } else {
+                    $v = json_encode($v, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
+                }
             }
             $sql .= '`' . $k . '`=' . $a . ':' . $k . ',';
             $bind[':'.$k] = $v;