From 122381148075a04315ca8b19df0566f228a9391d Mon Sep 17 00:00:00 2001 From: pkkgu <910111100@qq.com> Date: Mon, 14 Dec 2020 13:14:31 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加通过栏目目录获取栏目IDapi−>classid(′news′); 增加通过栏目目录获取表名api->tbname('news'); 增加通过栏目目录读取栏目单条数据 api−>inforOne(′news′,′id,title′,where='id=99'); 增加过滤MYSQL返回数字键名方法 formatData 增加打印设置回调方式$api->printType('json')->infor(.....);$api->printType('echo')->infor(.....); --- e/extend/api/api.class.php | 310 ++++++++++++++++++++++++++++++------- 1 file changed, 252 insertions(+), 58 deletions(-) diff --git a/e/extend/api/api.class.php b/e/extend/api/api.class.php index 51066b3..cb7297b 100644 --- a/e/extend/api/api.class.php +++ b/e/extend/api/api.class.php @@ -1,14 +1,21 @@ empire = $empire; $this->public_r = $public_r; $this->dbtbpre = $dbtbpre; $this->ecms_config = $ecms_config; $this->level_r = $level_r; + $this->class_r = $class_r; } public function __get($name){ @@ -110,7 +117,6 @@ class api { } /* output */ - public function show($str , $type = 'text/html' , $charset='utf-8'){ header('Content-Type: '.$type.'; charset='.$charset); exit($str); @@ -134,16 +140,13 @@ class api { } /* database */ - public function execute($sql = '' , $exit = true){ return $exit ? $this->empire->query($sql) : $this->empire->query1($sql); } public function insert($table = '' , $data = array()){ - if(empty($table) || empty($data) || !is_array($data)){ - return false; - } - $table = $this->dbtbpre . $table; + if(!$this->tbname($table)) return false; + if(empty($data) || !is_array($data)) return $this->ErrorCode(100008); $field = ""; $value = ""; foreach($data as $f=>$v){ @@ -153,32 +156,32 @@ class api { $field = substr($field , 1); $value = substr($value , 1); - $sql = "insert into {$table} ({$field}) values ({$value});"; - $res = $this->execute($sql , false); + $sql = "insert into {$this->table} ({$field}) values ({$value});"; + $res = $this->execute($sql); if(true === $res){ - return $this->empire->lastid(); + return $this->printJson($this->empire->lastid()); }else{ - return false; + return $this->ErrorCode(100009);; } } - public function update($table = '' , $data = '' , $where = '0'){ - if(empty($table) || empty($data) || (!is_string($data) && !is_array($data))){ - return false; - } - $table = $this->dbtbpre . $table; + public function update($table, $data , $where){ + if(!$this->tbname($table)) return false; + if(!$where) return $this->ErrorCode(100004); + if(empty($data) || (!is_string($data) && !is_array($data))) return $this->ErrorCode(100007); + if(is_string($data)){ $setField = $data; }else{ $setField = ""; foreach($data as $f=>$v){ - $v = !is_array($v) ? "'{$v}'" : $v[0]; - $setField .= ",{$f}={$v}"; + $v = is_array($v) ? $v[0] : $v; + $setField .= ",{$f}='{$v}'"; } $setField = substr($setField , 1); } - $sql = "update {$table} set {$setField} where {$where}"; - return $this->execute($sql , false); + $sql = "update {$this->table} set {$setField} where {$where}"; + return $this->printJson($this->execute($sql)); } public function select($table = '' , $field = '*' , $where = '1' , $limit = 20 , $page = 1 , $orderby = ''){ @@ -223,53 +226,43 @@ class api { return $this->query($sql , false); } - public function delete($table = '' , $where = '0'){ - if(empty($table)){ - return false; - } - $table = $this->dbtbpre . $table; - $sql = "delete from {$table} where {$where};"; - return $this->execute($sql , false); + public function delete($table , $where){ + if(!$this->tbname($table)) return false; + if(!$where) return $this->ErrorCode(100004); + + $sql = "delete from {$this->table} where {$where};"; + return $this->printJson($this->execute($sql)); } public function query($sql = '' , $exit = false){ $data = $this->execute($sql , $exit); - if(false === $data){ - return false; - } + if(false === $data) return false; + $this->SQL = $sql; $res = array(); + $bqno = 0; while($r = $this->empire->fetch($data)){ - $arr = array(); - foreach($r as $k=>$v){ - if(is_string($k)){ - $arr[$k] = $v; - } - } - $res[] = $arr; + ++$bqno; + $res[] = $this->formatData($r, $bqno); } - return $res; + return empty($res) ? $this->ErrorCode(100006) : $res; } - public function one($sql = ''){ - $res = $this->empire->fetch1($sql); - if(!empty($res)){ - foreach($res as $k=>$r){ - if(!is_string($k)){ - unset($res[$k]); - } - } - }else{ - $res = false; - } - return $res; + public function one($sql){ + if(!$sql) return $this->ErrorCode(100003); + + $this->SQL = $sql; + $r = $this->empire->fetch1($this->SQL); + return $this->formatData($r); } - public function total($table = '' , $where = '1'){ - $sql = true !== $where ? "select count(*) as total from ".($this->dbtbpre . $table)." where ".$where : $table; - return $this->empire->gettotal($sql); + public function total($table, $where){ + if(!$this->tbname($table)) return false; + if(!$where) return $this->ErrorCode(100004); + $sql = "select count(*) as total from {$this->table} where ".$where; + return $this->printJson($this->empire->gettotal($sql)); } - - function send_http_status($code) { + + private function send_http_status($code) { static $_status = array( // Informational 1xx 100 => 'Continue', @@ -325,6 +318,207 @@ class api { header('Status:'.$code.' '.$_status[$code]); } } -} + + /** + * 设置返回类型 + * @param string $type return=原样返回|json=返回json|echo=输出json + * @return $this + */ + public function printType($type){ + $this->printType = $type; + return $this; + } + + /** + * 打印数据(中文不转义需PHP>=5.4) + * @param arrty $arr + * @return json|bool|echo + */ + public function printJson($arr, $errCode=0, $errMsg='success'){ + if($this->printType=='return') { return $arr; } + else if($this->printType=='print') { echo '
'; print_r($arr); return; }
+ else if($this->printType=='var_dump') { echo ''; var_dump($arr); return; }
+ $arr['errCode'] = $errCode;
+ $arr['errMsg'] = $errMsg;
+ $jsonData = json_encode($arr, JSON_UNESCAPED_UNICODE);
+ if($this->printType=='json') return $jsonData;
+ if($this->printType=='echo') echo $jsonData;
+ }
+ /**
+ * 获取栏目ID
+ * @param string $classid 栏目ID或栏目目录(栏目目录,因某些项目绑定的栏目可能会变更,另外栏目目录易记,不用再去查询栏目ID)
+ * @return bool|int $classid 返回栏目ID
+ */
+ public function classid($classid){
+ // 判断数字栏目ID是否存在
+ if(is_numeric($classid)){
+ $classid = array_key_exists($classid, $this->class_r) ? $classid : 0;
+ }
+ // 根据栏目目录获取栏目ID
+ else if($classid){
+ $classid = array_search($classid, array_column($this->class_r, 'classpath'));
+ }
+ if(!$classid) {
+ $this->classid = 0;
+ return $this->ErrorCode(100002, $classid);
+ }
+ $this->classid = $classid;
+ return $this->classid;
+ }
+
+ /**
+ * 获取表名
+ * @param string $table 表名-->栏目ID-->栏目目录 (优先级同上)
+ * 注:$table为表名时:以enews开头是判断为非栏目表名(例如会员主表:enewsmember)
+ * @return bool|string $tbname 返回表全名
+ */
+ public function tbname($table){
+ $this->classid = 0;
+ // 判断是否是非栏目表名
+ if(substr($table, 0, 5) == 'enews'){
+ $this->classid = 0;
+ $this->tbname = $table;
+ $this->table = $this->dbtbpre.$table;
+ return $this->table;
+ }
+ // 判断是否是栏目表名
+
+ $tbname = in_array($table, array_column($this->class_r, 'tbname'));
+ if($tbname){
+ $this->classid = 0;
+ $this->tbname = $table;
+ $this->table = $this->dbtbpre.'ecms_'.$table;
+ return $this->table;
+ }
+ // 判断是否是栏目ID或栏目目录,再获取表名
+ if(!$this->classid($table)) return false;
+
+ $class_r = $this->class_r;
+ $tbname = $class_r[$this->classid]['tbname'];
+ if(!$tbname) {
+ $this->tbname = '';
+ return $this->ErrorCode(100001, $table);
+ }
+ $this->tbname = $tbname;
+ $this->table = $this->dbtbpre.'ecms_'.$tbname;
+ return $this->table;
+ }
+
+ /**
+ * 读取帝国CMS数据
+ * @param strint $table 表名-->栏目ID-->栏目目录 (优先级同上)
+ * @param strint $field 字段
+ * @param strint $where 查询条件
+ * @param strint $orderby 排序(id DESC)
+ * @param int $limit 1读取单条数据,非1读取多条数据
+ * @return bool|array $r
+ *
+ * 例(单条):
+ * 1. 查询新闻表ID=99的数据
+ * $api->infor('news', 'id,title', 'id=99');
+ *
+ * 2. 查询栏目目录old_news,ID=99的数据
+ * $api->infor('old_news', 'id,title', 'id=99');
+ *
+ * 3. 查询栏目ID=8,ID=99的数据
+ * $api->infor(8, 'id,title', 'id=99');
+ *
+ * 4. 查询会员表,会员ID为66
+ * $api->infor('enewsmember', 'userid,username', 'userid=66');
+ *
+ *
+ * 例(多条):
+ * 1. 查询新闻表最新10条数据
+ * $api->inforOne('news', 'id,title', 'classid=1' ,'id DESC' ,10);
+ *
+ * 2. 查询栏目目录为old_news,的最新10条数据
+ * $api->infor('old_news', 'id,title', '' ,'id DESC' ,10);
+ *
+ * 3. 查询栏目ID=8的最新10条数据
+ * $api->infor(8, 'id,title', '' ,'id DESC' ,10);
+ *
+ * 4. 查询会员表,会员组ID为1的最新10个会员
+ * $api->infor('enewsmember', 'userid,username', 'groupid=66' ,'userid DESC' ,10);
+ *
+ * 例:
+ * 返回json:$r = $api->printType('json')->infor('bid', 'id,title,classid', 'id=1');
+ * 直接输出:$r = $api->printType('echo')->infor('bid', 'id,title,classid', 'id=1');
+ *
+ */
+ public function infor($table='', $field='*', $where='', $order='', $limit='1'){
+ if(!$this->tbname($table)) return false;
+ if(!$limit) return $this->ErrorCode(100005);
+ if(!$where){
+ if(!$this->classid) return $this->ErrorCode(100004);
+ $where = "classid='{$this->classid}'";
+ }
+ //排序
+ $order = $order ? "order by $order" : '';
+ $SQL = "select $field from {$this->table} where {$where} {$order} limit {$limit}";
+
+ if($limit==1){
+ $data = $this->one($SQL); //单查
+ }else{
+ $data = $this->query($SQL); //多查
+ if(!$data) return false;
+ $data = array('data'=>$data, 'count'=>count($data));
+ }
+ return $this->printJson($data);
+ }
+
+ /**
+ * 格式化数据(去除数据键名数据)
+ * @param array $r 要处理的数据
+ * @param int $bqno 序号
+ * @return array
+ */
+ private function formatData($r,$bqno=0){
+ if(!$r) return $this->ErrorCode(100006);;
+ $res = array();
+ foreach($r as $k=>$v){
+ if(is_numeric($k)) continue;
+
+ if($k=='newstime'||$k=='truetime'||$k=='lastdotime'){
+ $res[$k.'YmdHis'] = date("Y-m-d H:i:s",$v);
+ $res[$k.'Ymd'] = date("Y-m-d",$v);
+ }else{
+ $res[$k] = stripslashes($v);
+ }
+ }
+ // 数据序号
+ if($bqno) $res['bqno'] = $bqno;
+ return $res;
+ }
+
+ /**
+ * error code
+ * @param int $errCode
+ * @param strint $errMsg 错误附加说明
+ * @return bool
+ */
+ private function ErrorCode($errCode=0, $errMsg='') {
+ $Msg = array(
+ 0 => 'success',
+ 100001 => '表名不能为空',
+ 100002 => '栏目ID不能为空',
+ 100003 => 'SQL语句不能为空',
+ 100004 => 'where条件不能为空',
+ 100005 => '需要指定查询条数',
+ 100006 => '无数据',
+ 100007 => '更新数据参数不能为空',
+ 100008 => '增加数据参数不能为空',
+ 100009 => '插入数据失败'
+ );
+ $errBool = $errCode ? false : true;
+ $errMsg = $Msg[$errCode].($errMsg ? "($errMsg)" : '');
+ $this->errCode = $errCode;
+ $this->errMsg = $errMsg;
+ return $this->printJson($errBool, $errCode, $errMsg);
+ }
+}
+/**
+ * error code
+ * 仅用作类内部使用,不用于官方API接口的errCode码
+ */
--
Gitee
From eeb867939471c8ef4cd2a498bf1cf3151f48b10b Mon Sep 17 00:00:00 2001
From: pkkgu <910111100@qq.com>
Date: Mon, 14 Dec 2020 17:16:30 +0800
Subject: [PATCH 2/2] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=9B=9E=E8=B0=83?=
=?UTF-8?q?=E6=96=B9=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
e/extend/api/api.class.php | 9 ++-------
1 file changed, 2 insertions(+), 7 deletions(-)
diff --git a/e/extend/api/api.class.php b/e/extend/api/api.class.php
index cb7297b..0b263b3 100644
--- a/e/extend/api/api.class.php
+++ b/e/extend/api/api.class.php
@@ -514,11 +514,6 @@ class api {
$errMsg = $Msg[$errCode].($errMsg ? "($errMsg)" : '');
$this->errCode = $errCode;
$this->errMsg = $errMsg;
- return $this->printJson($errBool, $errCode, $errMsg);
+ return $errBool;
}
-}
-
-/**
- * error code
- * 仅用作类内部使用,不用于官方API接口的errCode码
- */
+}
\ No newline at end of file
--
Gitee