diff --git a/App/Controllers/Doc.php b/App/Controllers/Doc.php new file mode 100644 index 0000000..da0b8c2 --- /dev/null +++ b/App/Controllers/Doc.php @@ -0,0 +1,14 @@ +getTypes(); + $type = json_encode($types,JSON_FORCE_OBJECT); + $this->JsonResp(200,$type,"ok"); + } + +} \ No newline at end of file diff --git a/App/Controllers/Home.php b/App/Controllers/Home.php index c670073..b8ad65a 100644 --- a/App/Controllers/Home.php +++ b/App/Controllers/Home.php @@ -35,7 +35,8 @@ class Home extends \Core\Controller } public function docsAction(){ $docs = new Models\Doc(); - //$this->JsonResp(200,$docs,"OK"); + $ret = $docs->getAll(); + $this->JsonResp(200,$ret,"OK"); } } diff --git a/App/Controllers/User.php b/App/Controllers/User.php new file mode 100644 index 0000000..0851b4e --- /dev/null +++ b/App/Controllers/User.php @@ -0,0 +1,39 @@ +getAll(); + $this->JsonResp(200,$all,"OK"); + } + public function insertAction(){ + $user = new Models\User(); + date_default_timezone_set("Asia/Shanghai"); + //var_dump($this->input); + + $userdate = date("Y-m-d H:i:s",time()); + $this->input->created_date = $userdate; + $this->input->update_date = $userdate; + if( strlen($this->input->user_pwd ) < 6){ + $this->JsonResp(201,null,"password empty"); + return; + } + if(!$this->is_password($this->input->user_pwd)){ + $this->JsonResp(201,null,"password illegal"); + return; + } + $all = $user->InsertObject($this->input); + $this->JsonResp(200,$all,"OK"); + } +} \ No newline at end of file diff --git a/App/Models/Doc.php b/App/Models/Doc.php index 5be490d..b8a21f4 100644 --- a/App/Models/Doc.php +++ b/App/Models/Doc.php @@ -21,6 +21,16 @@ class Doc extends Model { $db = static::getDB(); $stmt = $db->query('SELECT * FROM doc'); - return $stmt->fectchAll; + return $stmt->fetchAll(); + } + public function Columns(){ + return $this->columns; + } + public function getTypes(){ + $db = static::getDB(); + + $stmt = $db->query('select * from doc_type'); + $result = $stmt->fetchAll(PDO::FETCH_KEY_PAIR); + return $result; } } \ No newline at end of file diff --git a/App/Models/User.php b/App/Models/User.php index 7d403fd..0b0ccbc 100644 --- a/App/Models/User.php +++ b/App/Models/User.php @@ -11,19 +11,19 @@ use PDO; */ class User extends \Core\Model { + public $tableName = 'users'; /** * Get all the users as an associative array * * @return array */ - public static function getAll() { + public function getAll() { $db = static::getDB(); $stmt = $db->query('SELECT * FROM users'); - return $stmt->fetchAll(PDO::FETCH_ASSOC); } - public static function Insert($user){ + public function Insert($user){ $db = static::getDB(); $stmt = $db->query(`insert into users('user_name','user_pwd','create_date','update_date','display_name' ,'email_address','tel','avatar') values (` + $user["user_name"] + `,` + diff --git a/Core/Controller.php b/Core/Controller.php index 17449fd..760ef5a 100644 --- a/Core/Controller.php +++ b/Core/Controller.php @@ -11,13 +11,15 @@ use mysql_xdevapi\Exception; */ abstract class Controller { - /** * Parameters from the matched route * @var array */ protected $route_params = []; protected $querys = []; + public $body ; + protected $input; + protected $debug = false; /** * Class constructor * @@ -27,9 +29,14 @@ abstract class Controller */ public function __construct($route_params) { + switch($_SERVER['CONTENT_TYPE']){ + case 'application/json': + $this->body = file_get_contents('php://input'); + $this->input = json_decode($this->body); + break; + } $this->route_params = $route_params; } - /** * Magic method called when a non-existent or inaccessible method is * called on an object of this class. Used to execute before and after @@ -47,7 +54,8 @@ abstract class Controller if (method_exists($this, $method)) { if ($this->before() !== false) { - call_user_func_array([$this, $method], $args); + call_user_func_array([$this, $method], $args); + $this->after(); } } else { @@ -72,6 +80,11 @@ abstract class Controller } public function JsonResp($code,$data,$msg){ + header('Access-Control-Allow-Origin:*'); + header('Access-Control-Allow-Methods:OPTIONS, GET, POST'); // 允许option,get,post请求 + header('Access-Control-Allow-Headers:x-requested-with'); // 允许x-requested-with请求头 + header('Access-Control-Allow-Headers:x-ijt'); // 允许x-requested-with请求头 + $ret["code"] = $code; $ret["msg"] = $msg; $ret["data"] = $data; diff --git a/Core/Model.php b/Core/Model.php index 5d4f094..837229f 100644 --- a/Core/Model.php +++ b/Core/Model.php @@ -14,26 +14,28 @@ abstract class Model { public $tableName; public $columns = array(); + public $columnsType = array(); //获得一个表的所有列名 function __construct(){ $this->getColumns($this->tableName); } public function getColumns($tableName) { $db = static::getDB(); - $sql = "show columns from ".$tableName; $stmt = $db->query($sql); $res = $stmt->fetchAll(); if(static::getDB()!=null){ try{ foreach ($res as $key => $value){ - array_push($res,$value[0]); + array_push($this->columns,$value[0]); + array_push($this->columnsType,$value[1]); } }catch (\PDOException $e){ echo $e->getMessage(); } } } + /** * Get the PDO database connection * @@ -63,4 +65,39 @@ abstract class Model } return $db; } + public function InsertObject($obj){ + $db = static::getDB(); + $query = "insert into ".$this->tableName."("; + $val = ""; + $i = 0; + foreach ($obj as $Key => $value){ + // 如果key里面有包含在columns里面 + if(in_array($Key,$this->columns)){ + $query = $query.$Key.","; + $keyIndex = array_search($Key,$this->columns); + //print_r($this->columnsType[$keyIndex]." "); + if((strstr($this->columnsType[$keyIndex],"int(")) != false){ + if(is_integer($value)) + $val = $val.$value.","; + else + $val = $val."0".","; + }else{ + $val = $val."'".$value."',"; + } + } + $i++; + } + $val = rtrim($val,","); + $query = rtrim($query,","); + + $val = $val.");"; + $query = $query.") values("; + $query = $query.$val; + + $stmt = $db->query($query); + + $res = $stmt->fectchAll(); + //$result = $stmt->fetchAll(); + return $res; + } } diff --git a/deploy.bat b/deploy.bat index 33198ca..df9d035 100644 --- a/deploy.bat +++ b/deploy.bat @@ -1,4 +1,5 @@ -tar -czf code.tar.gz small ./* +del code.tar.gz +C:\\cygwin64\\bin\\tar.exe -czf code.tar.gz ./* scp -i ./id_rsa code.tar.gz ubuntu@118.24.238.198:/var/www/html/api/ ssh -t -i ./id_rsa ubuntu@118.24.238.198 tar -zxvf /var/www/html/api/code.tar.gz -C /var/www/html/api/ ssh -t -i ./id_rsa ubuntu@118.24.238.198 rm code.tar.gz