完善model 模块功能,controller 添加跨域支持

deploy
a74589669 2019-08-18 23:51:59 +08:00
parent a018e4a13f
commit 0519fbfe9f
8 changed files with 126 additions and 11 deletions

14
App/Controllers/Doc.php Normal file
View File

@ -0,0 +1,14 @@
<?php
namespace App\Controllers;
use App\Models;
class Doc extends \Core\Controller
{
function docTypeAction(){
$docModel = new Models\Doc();
$types = $docModel->getTypes();
$type = json_encode($types,JSON_FORCE_OBJECT);
$this->JsonResp(200,$type,"ok");
}
}

View File

@ -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");
}
}

39
App/Controllers/User.php Normal file
View File

@ -0,0 +1,39 @@
<?php
namespace App\Controllers;
use Core\Model;
use \Core\View;
use App\Models;
class User extends \Core\Controller
{
function is_password($password) {
return preg_match("/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/", $password);
}
public function allAction(){
$user = new Models\User();
$all = $user->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");
}
}

View File

@ -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;
}
}

View File

@ -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"] + `,` +

View File

@ -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
@ -48,6 +55,7 @@ abstract class Controller
if (method_exists($this, $method)) {
if ($this->before() !== false) {
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'); // 允许optiongetpost请求
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;

View File

@ -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;
}
}

View File

@ -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