rabin 1 year ago
parent
commit
34605ae6a8

+ 5 - 6
src/Dever/Debug.php

@@ -10,16 +10,15 @@ class Debug
     private static $start;
     public static function init()
     {
+        ini_set('display_errors', true);
+        self::$start = microtime();
+        set_error_handler(array(__CLASS__, 'error_handler'));
+        set_exception_handler(array(__CLASS__, 'exception_handler'));
         self::$shell = Dever::shell(Config::get('setting')['shell']);
-        if (self::$shell) {
-            self::$start = microtime();
-            ini_set('display_errors', true);
-            set_error_handler(array(__CLASS__, 'error_handler'));
-            set_exception_handler(array(__CLASS__, 'exception_handler'));
-        }
     }
     public static function error_handler($no, $str, $file, $line)
     {
+        self::$shell = true;
         $data['msg'] = $str;
         $data['file'] = $file . ':' . $line;
         $content = file_get_contents($file);

+ 37 - 0
src/Dever/Helper/Code.php

@@ -0,0 +1,37 @@
+<?php namespace Dever\Helper;
+class Code
+{
+    public static function create($width = '120', $height = '40')
+    {
+        header("Content-type: image/png");
+        $image = @imagecreate($width, $height);
+        $back = imagecolorallocate($image, 255, 255, 255);
+        $border = imagecolorallocate($image, 0, 0, 0);
+        imagefilledrectangle($image, 0, 0, $width - 1, $height - 1, $back);
+        imagerectangle($image, 0, 0, $width - 1, $height - 1, $border);
+        for ($i = 0; $i <= 200; $i++) {
+            imagesetpixel($image, rand(2, $width), rand(2, $height), imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255)));
+        }
+        $cal = array
+            (
+            array('+', '+'),
+            array('-', '-'),
+        );
+        $index = array_rand($cal);
+        $m1 = rand(1, 100);
+        $m2 = rand(1, 100);
+        $string = $m1 . $cal[$index][1] . $m2 . '';
+        $code = '$code = ' . $m1 . $cal[$index][0] . $m2 . ';';
+        eval($code);
+        $length = strlen($string);
+        for ($i = 0; $i < $length; $i++) {
+            $bg_color = imagecolorallocate($image, rand(0, 255), rand(0, 128), rand(0, 255));
+            $x = floor($width / $length) * $i;
+            $y = rand(0, $height - 15);
+            imagechar($image, rand(5, 5), $x + 5, $y, $string[$i], $bg_color);
+        }
+        imagepng($image);
+        imagedestroy($image);
+        return $code;
+    }
+}

+ 12 - 5
src/Dever/Helper/Env.php

@@ -192,12 +192,19 @@ class Env
     }
     public static function header($key = null)
     {
-        $headers = [];
-        foreach ($_SERVER as $name => $value) {
-            if (substr($name, 0, 5) == 'HTTP_') {
-                $headers[str_replace(' ', '-', strtolower(str_replace('_', ' ', substr($name, 5))))] = $value;
+        if ($key) {
+            $key = 'HTTP_' . strtoupper($key);
+            if (isset($_SERVER[$key])) {
+                return $_SERVER[$key];
             }
+        } else {
+            $headers = [];
+            foreach ($_SERVER as $name => $value) {
+                if (substr($name, 0, 5) == 'HTTP_') {
+                    $headers[str_replace(' ', '-', strtolower(str_replace('_', ' ', substr($name, 5))))] = $value;
+                }
+            }
+            return $headers;
         }
-        return is_null($key) ? $headers : (isset($headers[$key]) ? $headers[$key] : null);
     }
 }

+ 1 - 1
src/Dever/Helper/Rule.php

@@ -3,7 +3,7 @@ class Rule
 {
 	public static function get($method, $fix = '/', $rule = '')
     {
-        return $fix[0] . self::$method($rule) . $fix;
+        return $fix . self::$method($rule) . $fix;
     }
     protected static function idcard($rule)
     {

+ 27 - 52
src/Dever/Secure.php → src/Dever/Helper/Secure.php

@@ -1,30 +1,26 @@
-<?php namespace Dever;
+<?php namespace Dever\Helper;
 use Dever;
 class Secure
 {
-    const TIME = 300;
-    private static $token = 'dever_api';
     public static function login($uid, $extend = false)
     {
         $auth = '';
         $data = Dever::json_encode(array($uid, time(), $extend));
-        return self::encode($data, self::$token);
+        return self::encode($data);
     }
-    public static function checkLogin($signature)
+    public static function checkLogin($signature, $time = 300)
     {
-        $auth = Dever::json_decode(self::decode($signature, self::$token));
+        self::repeat($signature, $time);
+        $auth = Dever::json_decode(self::decode($signature));
         if (isset($auth[0]) && isset($auth[1]) && $auth[0] && $auth[0] > 0) {
-            if (time() - $auth[1] < self::TIME) {
+            if (time() - $auth[1] < $time) {
                 return array('uid' => $auth[0], 'time' => $auth[1], 'extend' => $auth[2]);
             }
         }
         return false;
     }
-    public static function get($request, $key = '')
+    public static function get($request)
     {
-        if ($key) {
-            self::$token = $key;
-        }
         $time = self::timestamp();
         $nonce = self::nonce();
         $signature = self::signature($time, $nonce, $request);
@@ -35,10 +31,13 @@ class Secure
         );
         return $request;
     }
-    public static function check($request, $param = array(), $key = '')
+    public static function check($request = array(), $time = 300)
     {
+        if (!$request) {
+            $request = Dever::input();
+        }
         if (empty($request['signature']) || empty($request['nonce'])) {
-            Output::error('api signature not exists');
+            Dever::error('api signature not exists');
         }
         if (isset($request['l'])) {
             unset($request['l']);
@@ -46,32 +45,16 @@ class Secure
         if (isset($request['shell'])) {
             unset($request['shell']);
         }
-        if ($key) {
-            self::$token = $key;
-        }
-        self::repeat($request['signature'], self::TIME);
         if (empty($request['time'])) {
             return self::checkLogin($request['signature']);
         }
-        if (time() - $request['time'] > self::TIME) {
-            Output::error('api signature has expired');
-        }
-        $signature_check = $request['signature'];
-        if ($param && isset($param['request'])) {
-            foreach ($param['request'] as $k => $v) {
-                if (isset($request[$k])) {
-                    $param['request'][$k] = $request[$k];
-                }
-            }
-            $temp = $param['request'];
-            $temp['token'] = self::token();
-            $temp['time'] = $request['time'];
-            $temp['nonce'] = $request['nonce'];
-            $request = $temp;
+        self::repeat($request['signature'], $time);
+        if (time() - $request['time'] > $time) {
+            Dever::error('api signature has expired');
         }
         $signature = self::signature($request['time'], $request['nonce'], $request);
-        if ($signature_check != $signature) {
-            Output::error('invalid signature');
+        if ($request['signature'] != $signature) {
+            Dever::error('invalid signature');
         }
         return $signature;
     }
@@ -96,7 +79,7 @@ class Secure
     }
     public static function token()
     {
-        return self::$token;
+        return Dever::config('setting')['token'];
     }
     public static function nonce()
     {
@@ -104,25 +87,21 @@ class Secure
     }
     public static function timestamp()
     {
-        return \Dever\Helper\Date::mtime();
+        return Date::mtime();
     }
     public static function repeat($value, $expire)
     {
-        if (isset(Config::get('setting')['redis']) && !\Dever\Helper\Redis::lock($value, 1, $expire)) {
-            Output::error('api signature repeat');
+        if (isset(Dever::config('setting')['redis']) && !Redis::lock($value, 1, $expire)) {
+            Dever::error('api signature repeat');
         }
     }
-    public static function encode($string, $key = "")
+    public static function encode($string, $key = '')
     {
         $ckey_length = 5;
         if (!$key) {
-            $key = Config::get('base')->token;
-            if ($key) {
-                $key = sha1($key);
-            } else {
-                $key = sha1(self::$key);
-            }
+            $key = self::token();
         }
+        $key = sha1($key);
         $keya = md5(substr($key, 0, 16));
         $keyb = md5(substr($key, 16, 16));
         $keyc = ''; //md5串后4位,每次不一样
@@ -160,13 +139,9 @@ class Secure
     {
         $ckey_length = 5;
         if (!$key) {
-            $key = Config::get('base')->token;
-            if ($key) {
-                $key = sha1($key);
-            } else {
-                $key = sha1(self::$key);
-            }
+            $key = self::token();
         }
+        $key = sha1($key);
         $keya = md5(substr($key, 0, 16));
         $keyb = md5(substr($key, 16, 16));
         $keyc = '';//和encrypt时的$keyc一样
@@ -223,4 +198,4 @@ class Secure
         $decodestr = base64_decode($string);
         return $decodestr;
     }
-}
+}

+ 5 - 1
src/Dever/Helper/Str.php

@@ -1,5 +1,5 @@
 <?php namespace Dever\Helper;
-class String
+class Str
 {
     public static function encode($string, $key = '')
     {
@@ -22,6 +22,10 @@ class String
         }
         return round($value, 2);
     }
+    public static function salt($len)
+    {
+        return bin2hex(random_bytes($len));
+    }
     public static function rand($len, $type = 4)
     {
         $source = array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");

+ 5 - 3
src/Dever/Import.php

@@ -1,4 +1,5 @@
 <?php namespace Dever;
+use Dever;
 class Import
 {
     protected $param;
@@ -17,7 +18,7 @@ class Import
         if (strpos($project['path'], 'http') === 0) {
             $this->class = $project;
         } else {
-            $this->class = Library::get()->load($class, $app, $path);
+            $this->class = Library::load($class, $app, $path);
         }
     }
     public function __call($method, $param)
@@ -40,7 +41,7 @@ class Import
                     if (method_exists($this->class, $token)) {
                         $key = $this->class->{$token}();
                     }
-                    Secure::check($this->param, array(), $key);
+                    \Dever\Helper\Secure::check($this->param, array(), $key);
                 }
             }
             return $this->loadDevelopCommit();
@@ -54,7 +55,7 @@ class Import
     private function loadDevelopCommit()
     {
         if (method_exists($this->class, $this->method . '_commit')) {
-            $db = \Dever::store();
+            $db = Dever::store();
             try {
                 $db->begin();
                 $data = $this->loadDevelopCall();
@@ -75,6 +76,7 @@ class Import
         $param = $this->loadDevelopParam();
         $data = $this->class->{$this->method}(...$param);
         Debug::lib($this->class, $this->method);
+        Dever::reset();
         return $data;
     }
     private function loadDevelopParam()

+ 25 - 28
src/Dever/Library.php

@@ -1,45 +1,42 @@
 <?php namespace Dever;
 class Library
 {
-    protected $file;
-    protected $class;
-    protected static $instance;
-    public static function get()
+    protected static $file;
+    protected static $class;
+    public static function autoload($class)
     {
-        if (empty(self::$instance)) {
-            self::$instance = new self();
-            //spl_autoload_register(array(self::$instance, 'autoload'));
-        }
-
-        return self::$instance;
+        $class = explode('\\', $class);
+        self::require(lcfirst($class[0]), lcfirst($class[1]) . DIRECTORY_SEPARATOR . $class[2]);
     }
-    public function require($file)
+    public static function require($app, $file)
     {
-        $file .= '.php';
-        if (is_file($file) && empty($this->file[$file])) {
-            $this->file[$file] = true;
+        $project = Project::load($app);
+        $file = $project['path'] . $file . '.php';
+        if (is_file($file) && empty(self::$file[$file])) {
+            self::$file[$file] = true;
             require $file;
         }
     }
-    public function apply(&$class, $app, $path)
+    public static function apply($class, $app, $path)
     {
-        $class = ucfirst($class);
-        $file = $path . DIRECTORY_SEPARATOR . $class;
-        if (strpos($path, '/')) {
-            $path = str_replace(' ', '\\', ucwords(str_replace('/', ' ', $path)));
+        if (strpos($class, '/')) {
+            $class = str_replace(' ', '/', ucwords(str_replace('/', ' ', $class)));
+            $file = $path . DIRECTORY_SEPARATOR . $class;
+            $class = str_replace('/', '\\', $class);
         } else {
-            $path = ucfirst($path);
+            $class = ucfirst($class);
+            $file = $path . DIRECTORY_SEPARATOR . $class;
         }
-        $class = ucfirst($app) . '\\' . $path . '\\' . $class;
-        $project = Project::load($app);
-        $this->require($project['path'] . $file);
+        $class = ucfirst($app) . '\\' . ucfirst($path) . '\\' . $class;
+        self::require($app, $file);
+        return $class;
     }
-    public function load($class, $app, $path)
+    public static function load($class, $app, $path)
     {
-        $this->apply($class, $app, $path);
-        if (empty($this->class[$class])) {
-            $this->class[$class] = new $class();
+        $class = self::apply($class, $app, $path);
+        if (empty(self::$class[$class])) {
+            self::$class[$class] = new $class();
         }
-        return $this->class[$class];
+        return self::$class[$class];
     }
 }

+ 74 - 2
src/Dever/Model.php

@@ -17,6 +17,7 @@ class Model
                 if (isset($this->config['partition']) && empty($partition)) {
                     $partition = $this->config['partition'];
                 }
+                $file = $path . DIRECTORY_SEPARATOR . $app . DIRECTORY_SEPARATOR . $table . '.php';
                 $this->config['app'] = $app;
                 $this->config['table'] = DEVER_PROJECT . '_' . $app . '_' . $table;
                 $this->lang();
@@ -32,6 +33,7 @@ class Model
         if (isset($file)) {
             $this->init($file);
         }
+        Dever::reset();
     }
     private function partition($partition)
     {
@@ -66,7 +68,7 @@ class Model
     }
     private function init($file)
     {
-        $data['struct'] = $data['index'] = 0;
+        $data['index'] = $data['struct'] = 0;
         if ($this->partition) {
             if (isset($this->partition['table'])) {
                 $this->config['table'] .= '_' . $this->partition['table'];
@@ -102,6 +104,13 @@ class Model
     }
     public function select($param, $set = array(), $lock = false)
     {
+        if (empty($set['order'])) {
+            if (isset($this->config['order'])) {
+                $set['order'] = $this->config['order'] . ',id desc';
+            } else {
+                $set['order'] = 'id desc';
+            }
+        }
         if (isset($set['num'])) {
             $set['limit'] = Paginator::init($set['num'], $set['page'] ?? 1, function()use($param){return $this->count($param);});
         }
@@ -115,7 +124,7 @@ class Model
         }
         return $result;
     }
-    public function count($param)
+    public function count($param = array())
     {
         return $this->store->count($this->config['table'], $param, $this->config['struct']);
     }
@@ -123,6 +132,33 @@ class Model
     {
         return $this->select($param, $set, $lock)->fetch();
     }
+    public function kv($param, $set = array())
+    {
+        $result = array();
+        $data = $this->select($param, $set)->fetchAll();
+        if ($data) {
+            if (empty($set['kv'])) {
+                $set['kv'] = array('id', 'name');
+            }
+            foreach ($data as $k => $v) {
+                $result[$v[$set['kv'][0]]] = $v[$set['kv'][1]];
+            }
+        }
+        return $result;
+    }
+    public function up($param, $data, $lock = false)
+    {
+        $info = $this->find($param, array(), $lock);
+        if ($info) {
+            $state = Dever::db('menu')->update($info['id'], $data);
+            if ($state) {
+                return $info['id'];
+            }
+            return false;
+        } else {
+            return Dever::db('menu')->insert($data);
+        }
+    }
     public function insert($data)
     {
         if (empty($data['cdate'])) {
@@ -199,6 +235,42 @@ class Model
             return $this->store->query($sql, $bind)->fetchColumn();
         }
     }
+    public function value($key, $value = false, $col = 'id,name')
+    {
+        if (isset($this->config['struct']) && $option = Dever::isset($this->config['struct'][$key], 'value')) {
+            if (isset($this->config['struct'][$key]['option'])) {
+                $option = $this->config['struct'][$key]['option'];
+            } else {
+                if (strpos($option, 'Dever') === 0) {
+                    eval('$option=' . $option . ';');
+                    $value['option'] = $option;
+                } elseif (is_string($option)) {
+                    $option = Dever::db($option)->select([], ['col' => $col])->fetchAll();
+                } elseif (is_array($option) && !isset($option[0])) {
+                    $temp = $option;
+                    $option = array();
+                    $col = explode(',', $col);
+                    foreach ($temp as $k => $v) {
+                        $option[] = array($col[0] => $k, $col[1] => $name);
+                    }
+                }
+                $this->config['struct'][$key]['option'] = $option;
+            }
+            if ($value && $option) {
+                if (strpos($value, ',')) {
+                    $temp = explode(',', $value);
+                    $result = array();
+                    foreach ($temp as $v) {
+                        $result[] = Dever::in_array($option, $v);
+                    }
+                    return implode('、', $result);
+                }
+                return Dever::in_array($option, $value);
+            }
+            return $option;
+        }
+        return false;
+    }
     public function __call($method, $data)
     {
         if (isset($this->config['request'][$method])) {

+ 2 - 2
src/Dever/Output.php

@@ -3,7 +3,7 @@ use Dever;
 class Output
 {
     private static $format = 'json';
-    public static function success($data, $uuid = false, $code = 0)
+    public static function success($data, $uuid = false, $code = 200)
     {
         if (is_object($data)) {
             $result = array();
@@ -26,7 +26,7 @@ class Output
         self::handle($result);
         return $data;
     }
-    public static function error($msg, $code = 1)
+    public static function error($msg, $code = 500)
     {
         $result = array();
         $result['status'] = 2;

+ 3 - 0
src/Dever/Paginator.php

@@ -25,6 +25,9 @@ class Paginator
     public static function get($template = '', $maxpage = 10, $link = '')
     {
         if (self::$rows) {
+            if ($template == 'total') {
+                return (self::$rows)();
+            }
             $page['num'] = self::$num;
             $page['status'] = self::$status;
             $page['current'] = self::$current;

+ 22 - 14
src/Dever/Route.php

@@ -3,7 +3,7 @@ class Route
 {
     protected static $data = array();
     protected static $type = '';
-    public static function input($key = false, $lang = '', $condition = '', $value = '')
+    public static function input($key = false, $condition = '', $lang = '', $value = '')
     {
         if (!$key) {
             return self::$data;
@@ -14,16 +14,21 @@ class Route
         if (is_string($key) && isset(self::$data[$key]) && self::$data[$key]) {
             $value = self::$data[$key];
         }
-        return $value;
         if ($condition) {
+            if (!$lang) {
+                $lang = $key;
+            }
             if (!$value) {
                 Output::error($lang . '不能为空');
             }
-            $state = false;
-            $test = '$state = ' . $value . $condition . ';';
-            eval($test);
+            $state = true;
+            if (strpos($condition, '/') === 0) {
+                $state = preg_match($condition, $value);
+            } elseif (function_exists($condition)) {
+                $state = $condition($value);
+            }
             if (!$state) {
-                Output::error($lang);
+                Output::error($lang . '验证无效');
             }
         }
         return $value;
@@ -90,7 +95,7 @@ class Route
             self::api();
         }
         self::match();
-        self::filter();
+        self::filter(self::$data);
         if (isset(self::$data['uuid']) && isset(Config::get('setting')['redis']) && !\Dever\Helper\Redis::lock(self::$data['uuid'], 1, 10)) {
             Output::error('route repeat');
         }
@@ -121,7 +126,6 @@ class Route
         } else {
             self::$type = '?l=';
         }
-        
         define('DEVER_PROTO', ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https' : 'http');
         define('DEVER_APP_HOST', DEVER_PROTO . '://' . $_SERVER['HTTP_HOST'] . ($_SERVER['SCRIPT_NAME'] ? substr($_SERVER['SCRIPT_NAME'], 0, strpos($_SERVER['SCRIPT_NAME'], DEVER_ENTRY)) : DIRECTORY_SEPARATOR));
     }
@@ -163,14 +167,18 @@ class Route
             }
         }
     }
-    protected static function filter()
+    protected static function filter(&$data)
     {
-        if (self::$data) {
-            foreach (self::$data as $k => $v) {
-                if (self::$data[$k] == 'undefined') {
-                    self::$data[$k] = '';
+        if ($data) {
+            foreach ($data as $k => $v) {
+                if (is_array($v)) {
+                    self::filter($v);
                 } else {
-                    self::$data[$k] = htmlspecialchars($v);
+                    if ($data[$k] == 'undefined') {
+                        $data[$k] = '';
+                    } else {
+                        $data[$k] = htmlspecialchars($v);
+                    }
                 }
             }
         }

+ 7 - 7
src/Dever/Session.php

@@ -1,6 +1,6 @@
 <?php namespace Dever;
 use Dever;
-use Dever\String\Encrypt;
+use Dever\Helper\Secure;
 class Session
 {
     private static $start = false;
@@ -69,7 +69,7 @@ class Session
     {
         $key = DEVER_PROJECT . '_' . $key;
         if ($encode) {
-            $value = Encrypt::encode(base64_encode(serialize($value)), $this->key);
+            $value = Secure::encode(serialize($value), $this->key);
         }
         $method = '_set' . $this->method;
         $this->$method($key, $value, $time);
@@ -81,7 +81,7 @@ class Session
         $method = '_get' . $this->method;
         $value = $this->$method($key);
         if ($encode) {
-            $value = unserialize(base64_decode(Encrypt::decode($value, $this->key)));
+            $value = unserialize(Secure::decode($value, $this->key));
         }
         return $value;
     }
@@ -97,7 +97,7 @@ class Session
     }
     private function _setCookie($key, $value, $time = 3600)
     {
-        return setCookie($this->prefix . $key, $value, time() + $time, "/", Config::get('host')->cookie);
+        return setCookie($this->prefix . $key, $value, time() + $time, '/', '');
     }
     private function _getCookie($key)
     {
@@ -108,11 +108,11 @@ class Session
     }
     private function _unsetCookie($key)
     {
-        return setCookie($this->prefix . $key, false, time() - 3600, "/", Config::get('host')->cookie);
+        return setCookie($this->prefix . $key, false, time() - 3600, '/', '');
     }
     private function _setSession($key, $value, $time = 3600)
     {
-        setCookie(session_name(), session_id(), time() + $time, "/", Config::get('host')->cookie); 
+        setCookie(session_name(), session_id(), time() + $time, '/', ''); 
         return $_SESSION[$this->prefix . $key] = $value;
     }
     private function _getSession($key)
@@ -130,7 +130,7 @@ class Session
     private function _initFile()
     {
         $this->id = md5($this->key);
-        $this->file = Dever::path(Dever::data() . 'session/') . $this->id;
+        $this->file = Dever::path('session') . $this->id;
         if (is_file($this->file)) {
             $this->data = unserialize(file_get_contents($this->file));
             return;

+ 3 - 7
src/Dever/Sql.php

@@ -1,10 +1,6 @@
 <?php namespace Dever;
 class Sql
 {
-    public static function setColOrder($order)
-    {
-        $this->colOrder = $order;
-    }
     public static function desc($table)
     {
         return 'DESC ' . $table;
@@ -52,7 +48,7 @@ class Sql
         } elseif (strpos($set['type'], 'int') !== false || strpos($set['type'], 'float') !== false || strpos($set['type'], 'decimal') !== false || strpos($set['type'], 'double') !== false) {
             $field .= ' NOT NULL DEFAULT 0';
         } else {
-            $field .= ' NULL';
+            $field .= ' NOT NULL DEFAULT ""';
         }
         if (isset($set['name'])) {
             $field .= ' COMMENT \'' . $set['name'] . '\'';
@@ -242,10 +238,10 @@ class Sql
             }
             $a = '';
             if (is_array($v)) {
-                $a = $v[0];
+                $a = '`' . $k . '`' . $v[0];
                 $v = $v[1];
             }
-            $sql .= '`' . $k . '`=`' . $k . '`' . $a . ':' . $k . ',';
+            $sql .= '`' . $k . '`=' . $a . ':' . $k . ',';
             $bind[':'.$k] = $v;
         }
         return rtrim($sql, ',') . self::where($param, $bind, $field);

+ 1 - 1
src/Dever/Store/Base.php

@@ -25,7 +25,7 @@ class Base
     }
     protected function error($msg)
     {
-        Debug::error($msg, $this->type);
+        Debug::out($msg, $this->type);
     }
     protected function log($msg)
     {

+ 6 - 3
src/Dever/Store/Pdo.php

@@ -68,12 +68,15 @@ class Pdo extends Base
     public function struct($config, $state = 0)
     {
         if ($state) {
-            $this->query(Sql::alter($config['table'], $config['struct'], $this->query(Sql::desc($config['table']))));
+            $sql = Sql::alter($config['table'], $config['struct'], $this->query(Sql::desc($config['table'])));
+            if ($sql) {
+                $this->query($sql);
+            }
         } else {
             $this->query(Sql::create($config));
         }
         if (isset($config['default']) && $config['default']) {
-            $count = $this->count($config['table'], array());
+            $count = $this->count($config['table'], array(), $config['struct']);
             if (!$count) {
                 $this->query(Sql::inserts($config['table'], $config['default']));
             }
@@ -113,7 +116,7 @@ class Pdo extends Base
     }
     public function count($table, $param, $field)
     {
-        return $this->select($table, $param, array('col'=>'count(*)'), $field)->fetch(\PDO::FETCH_NUM)[0];
+        return $this->select($table, $param, array('col'=>'count(*)'), $field, false)->fetch(\PDO::FETCH_NUM)[0];
     }
     public function insert($table, $data, $field)
     {