Browse Source

同步改结构,刚刚开始,测试一下

visuddhinanda 4 years ago
parent
commit
6dc961e9ec

+ 2 - 1
.gitignore

@@ -5,4 +5,5 @@
 .DS_Store
 /.debug
 .favorites.json
-/vendor
+/vendor
+/node_modules

+ 3 - 3
app/article/function.php

@@ -142,7 +142,7 @@ class ArticleList extends Table
 
 	function upgrade($collectionId,$articleList=array()){
 		if(count($articleList)==0){
-
+			return false;
 		}
 		# 更新 article_list 表
 		$query = "DELETE FROM article_list WHERE collect_id = ? ";
@@ -153,9 +153,9 @@ class ArticleList extends Table
         
 		if(count($articleList)>0){
 			/* 开始一个事务,关闭自动提交 */
-			$PDO->beginTransaction();
+			$this->dbh->beginTransaction();
 			$query = "INSERT INTO article_list (collect_id, article_id,level,title) VALUES ( ?, ?, ? , ? )";
-			$sth = $PDO->prepare($query);
+			$sth = $this->dbh->prepare($query);
 			foreach ($articleList as $row) {
 				$sth->execute(array($_POST["id"],$row->article,$row->level,$row->title));
 				if($redis){

+ 2 - 2
app/article/my_collect_put.php

@@ -20,12 +20,12 @@ if(!isset($_POST["title"])){
 	echo json_encode($respond, JSON_UNESCAPED_UNICODE);
 	exit;
 }
-PDO_Connect(""._FILE_DB_USER_ARTICLE_);
+add_edit_event(_COLLECTION_NEW_,$uuid);
 
+PDO_Connect(""._FILE_DB_USER_ARTICLE_);
 $query="INSERT INTO collect ( id,  title  , subtitle  , summary , article_list   , owner, lang  , status  , create_time , modify_time , receive_time   )  VALUES  ( ? , ? , ? , ?  , ? , ? , ? , ? , ? , ? , ? ) ";
 $sth = $PDO->prepare($query);
 $uuid = UUID::v4();
-add_edit_event(_COLLECTION_NEW_,$uuid);
 $sth->execute(array($uuid , $_POST["title"] , "" ,"", "[]" ,  $_COOKIE["userid"] , "" , 1 , mTime() ,  mTime() , mTime() ));
 $respond=array("status"=>0,"message"=>"");
 if (!$sth || ($sth && $sth->errorCode() != 0)) {

+ 121 - 4
app/collect/function.php

@@ -1,14 +1,15 @@
 <?php
 require_once '../path.php';
 require_once "../share/function.php";
-
+require_once "../db/table.php";
+require_once "../article/function.php";
 
 class CollectInfo
 {
     private $dbh;
     private $buffer;
 	private $_redis;
-
+	private $errorMsg;
     public function __construct($redis=false) {
         $dns = ""._FILE_DB_USER_ARTICLE_;
         $this->dbh = new PDO($dns, "", "",array(PDO::ATTR_PERSISTENT=>true));
@@ -25,7 +26,7 @@ class CollectInfo
             return $buffer[$id];
         }
         if($this->dbh){
-            $query = "SELECT id,title,owner,status,lang FROM collect WHERE id= ?";
+            $query = "SELECT id,title,owner,status,lang, FROM collect WHERE id= ?";
             $stmt = $this->dbh->prepare($query);
             $stmt->execute(array($id));
 			$collect = $stmt->fetch(PDO::FETCH_ASSOC);
@@ -36,7 +37,7 @@ class CollectInfo
             else{
                 $buffer[$id] =false;
                 return $buffer[$id];
-            }            
+            }
         }
         else{
             $buffer[$id] = false;
@@ -115,5 +116,121 @@ class CollectInfo
 		}
 		return $iPower;
 	}
+
+	public function update($arrData){
+		/* 修改现有数据 */
+	
+		if (count($arrData) > 0) {
+			$this->dbh->beginTransaction();
+			$query="UPDATE collect SET title = ? , subtitle = ? , summary = ?, article_list = ?  ,  status = ? , lang = ? , receive_time= ?  , modify_time= ?   where  id = ?  ";
+			$sth = $this->dbh->prepare($query);
+			foreach ($arrData as $data) {
+				$sth->execute(array($data["title"] , $data["subtitle"] ,$data["summary"], $data["article_list"] , $data["status"] , $data["lang"] ,  mTime() , mTime() , $data["id"]));
+			}
+			$this->dbh->commit();
+		
+			if (!$sth || ($sth && $sth->errorCode() != 0)) {
+				/*  识别错误且回滚更改  */
+				$this->dbh->rollBack();
+				$error = $this->dbh->errorInfo();
+				$this->errorMsg = $error[2];
+				return false;
+			} else {
+				#没错误 添加log 
+				$this->errorMsg = "";
+				//更新列表
+				$ArticleList = new ArticleList($this->_redis);
+				foreach ($arrData as $data) {
+					$arrList = json_decode($data["article_list"],true);
+					$ArticleList->upgrade($data["id"],$arrList);
+				}
+				return true;
+			}
+		}
+		else{
+			$this->errorMsg = "";
+			return true;
+		}
+	}
+
+	public function insert($arrData){
+		/* 插入新数据 */
+		$respond=array("status"=>0,"message"=>"");
+		if (count($arrData) > 0) {
+			$this->dbh->beginTransaction();
+			$query="INSERT INTO collect ( id,  title  , subtitle  , summary , article_list , owner, lang  , status  , create_time , modify_time , receive_time   )  VALUES  ( ? , ? , ? , ?  , ? , ? , ? , ? , ? , ? , ? ) ";
+			$sth = $this->dbh->prepare($query);
+			$newDataList=array();
+			foreach ($arrData as $data) {
+				$newData = array();
+				if(isset($data["title"]) && !empty($data["title"])){
+					$newData["title"]=$data["title"];
+					if(isset($data["id"])){
+						$newData["id"]=$data["id"];
+					}
+					else{
+						$newData["id"]=UUID::v4();
+					}
+					if(isset($data["subtitle"])){
+						$newData["subtitle"]=$data["subtitle"];
+					}
+					else{
+						$newData["subtitle"]="";
+					}
+					if(isset($data["summary"])){
+						$newData["summary"]=$data["summary"];
+					}
+					else{
+						$newData["summary"]="";
+					}	
+					if(isset($data["article_list"])){
+						$newData["article_list"]=$data["article_list"];
+					}
+					else{
+						$newData["article_list"]="";
+					}		
+					if(isset($data["lang"])){
+						$newData["lang"]=$data["lang"];
+					}
+					else{
+						$newData["lang"]="";
+					}
+					if(isset($data["status"])){
+						$newData["status"]=$data["status"];
+					}
+					else{
+						$newData["status"]="1";
+					}
+					$newDataList[]=$newData;
+					$sth->execute(array($newData["id"] , $newData["title"] , $newData["subtitle"] ,$newData["summary"], $newData["article_list"] ,  $_COOKIE["userid"] , $newData["lang"] , $newData["status"] , mTime() ,  mTime() , mTime() ));				
+				}
+				else{
+					$this->errorMsg="标题不能为空";
+				}
+			}
+			$this->dbh->commit();
+	
+			if (!$sth || ($sth && $sth->errorCode() != 0)) {
+				/*  识别错误且回滚更改  */
+				$this->dbh->rollBack();
+				$error = $this->dbh->errorInfo();
+				$this->errorMsg = $error[2];
+				return false;
+			} else {
+				$this->errorMsg = "";
+				//更新列表
+				$ArticleList = new ArticleList($this->_redis);
+				foreach ($newDataList as $data) {
+					$arrList = json_decode($data["article_list"],true);
+					$ArticleList->upgrade($data["id"],$arrList);
+				}
+				return true;
+			}
+		}
+		else{
+			$this->errorMsg = "";
+			return true;
+		}
+	}
 }
 ?>

+ 19 - 29
app/sync/function.php

@@ -15,23 +15,20 @@ function do_sync($param)
 		if($key===FALSE){
 			$output["error"]=1;
 			$output["message"]="无法提取key userid:".$_POST["userid"];
-			echo json_encode($output, JSON_UNESCAPED_UNICODE);	
-			return false;
+			return $output;	
 		}
 		else{
 			if($key!=$_POST["key"]){
 				$output["error"]=1;
 				$output["message"]="key验证失败 {$key}- {$_POST["key"]} ";
-				echo json_encode($output, JSON_UNESCAPED_UNICODE);	
-				return false;
+				return $output;	
 			}
 		}
 	}
 	else{
 		$output["error"]=1;
 		$output["message"]="redis初始化失败";
-		echo json_encode($output, JSON_UNESCAPED_UNICODE);	
-		return false;
+		return $output;	
 	}
 
     if (isset($_GET["op"])) {
@@ -41,11 +38,10 @@ function do_sync($param)
     } else {
 		$output["error"]=1;
 		$output["message"]="无操作码";
-		echo json_encode($output, JSON_UNESCAPED_UNICODE);	
-		return false;
+		return $output;	
     }
 
-    $PDO = new PDO("" . $param->database, "", "", array(PDO::ATTR_PERSISTENT => true));
+    $PDO = new PDO($param->database, "", "", array(PDO::ATTR_PERSISTENT => true));
     $PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
 
     switch ($op) {
@@ -75,8 +71,8 @@ function do_sync($param)
 						$Fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
 					}
 					$output["data"]=$Fetch;
-					echo json_encode($output, JSON_UNESCAPED_UNICODE);	
-					return;			
+					return $output;
+			
 				}
 				else if(isset($_POST["id"])){
 					$params = json_decode($_POST["id"],true);
@@ -89,7 +85,8 @@ function do_sync($param)
 					$Fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
 					$iFetch = count($Fetch);
 					$output["data"]=$Fetch;
-					echo json_encode($output, JSON_UNESCAPED_UNICODE);					
+					return $output;
+					
 				}
 
                 break;
@@ -112,8 +109,7 @@ function do_sync($param)
                 $Fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
 				$output["message"]="提取数据".count($Fetch);
 				$output["data"]=$Fetch;
-				echo json_encode($output, JSON_UNESCAPED_UNICODE);
-                return true;
+				return $output;
                 break;
             }
         case "insert":
@@ -123,8 +119,7 @@ function do_sync($param)
                 } else {
 					$output["error"]=1;
 					$output["message"]="没有提交数据";
-					echo json_encode($output, JSON_UNESCAPED_UNICODE);	
-                    return false;
+					return $output;
                 }
 
                 // 开始一个事务,关闭自动提交
@@ -157,14 +152,13 @@ function do_sync($param)
                     $error = $PDO->errorInfo();
 					$output["error"]=1;
 					$output["message"]="error - $error[2]";
-					echo json_encode($output, JSON_UNESCAPED_UNICODE);	
-                    return false;
+					return $output;
+
                 } else {
                     $count = count($arrData);
 					$output["error"]=0;
 					$output["message"]="INSERT $count recorders.";
-					echo json_encode($output, JSON_UNESCAPED_UNICODE);						
-                    return (true);
+					return $output;						
                 }
                 break;
             }
@@ -175,8 +169,7 @@ function do_sync($param)
                 } else {
 					$output["error"]=1;
 					$output["message"]="没有输入数据";
-					echo json_encode($output, JSON_UNESCAPED_UNICODE);	
-                    return false;
+					return $output;	
                 }
                 $arrData = json_decode($data, true);
                 $query = "UPDATE {$param->table} SET ";
@@ -204,23 +197,20 @@ function do_sync($param)
                         $error = $PDO->errorInfo();
 						$output["error"]=1;
 						$output["message"]="error - $error[2]";
-						echo json_encode($output, JSON_UNESCAPED_UNICODE);	
-						return false;
+						return $output;
+
                     } else {
                         $count = count($arrData);
 						$output["error"]=0;
 						$output["message"]="Update $count recorders.";
-						echo json_encode($output, JSON_UNESCAPED_UNICODE);	
-                        return true;
+						return $output;
                     }
                 } catch (Exception $e) {
                     $PDO->rollback();
 					$output["error"]=1;
 					$output["message"]="Failed:" . $e->getMessage();
-					echo json_encode($output, JSON_UNESCAPED_UNICODE);						
-                    return false;
+					return $output;						
                 }
-
                 break;
             }
         default:

+ 2 - 1
app/sync/table_article.php

@@ -38,6 +38,7 @@ $input = (object) [
     ]    
 ];
 
-do_sync($input);
+$result = do_sync($input);
+echo json_encode($result, JSON_UNESCAPED_UNICODE);
 
 ?>

+ 28 - 1
app/sync/table_article_collect.php

@@ -37,6 +37,33 @@ $input = (object) [
     ]    
 ];
 
-do_sync($input);
+
+
+if (isset($_GET["op"])) {
+	$op = $_GET["op"];
+} else if (isset($_POST["op"])) {
+	$op = $_POST["op"];
+} else {
+	$output["error"]=1;
+	$output["message"]="无操作码";
+	echo json_encode($output, JSON_UNESCAPED_UNICODE);
+	exit;
+}
+
+switch ($op) {
+	case "sync":
+		$result = do_sync($input);
+		echo json_encode($result, JSON_UNESCAPED_UNICODE);
+	break;
+	case "get":
+		$result = do_sync($input);
+		echo json_encode($result, JSON_UNESCAPED_UNICODE);
+	break;
+	case "insert":
+		
+		echo json_encode($result, JSON_UNESCAPED_UNICODE);
+	break;	
+}
+
 
 ?>

+ 0 - 36
app/sync/table_article_list.php

@@ -1,36 +0,0 @@
-<?php
-//header('Content-type: application/json; charset=utf8');
-
-require_once "../path.php";
-require_once "../sync/function.php";
-
-$input = (object) [
-    "database" =>  _FILE_DB_USER_ARTICLE_,
-    "table" =>  "article_list",
-    "uuid" =>  "id",
-    "modify_time" =>  "modify_time",
-    "receive_time" =>  "modify_time",
-    "insert" => [
-        'id',
-		'collect_id',
-		'collect_title',
-		'article_id',
-		'level',
-		'title',
-		'create_time',
-		'modify_time'
-    ],
-    "update" =>  [
-		'collect_id',
-		'collect_title',
-		'article_id',
-		'level',
-		'title',
-		'create_time',
-		'modify_time'
-    ]    
-];
-
-do_sync($input);
-
-?>

+ 2 - 1
app/sync/table_sentence.php

@@ -48,6 +48,7 @@ $input = (object) [
     ]    
 ];
 
-do_sync($input);
+$result = do_sync($input);
+echo json_encode($result, JSON_UNESCAPED_UNICODE);
 
 ?>

+ 2 - 1
app/sync/table_term.php

@@ -41,6 +41,7 @@ $input = (object) [
     ]    
 ];
 
-do_sync($input);
+$result = do_sync($input);
+echo json_encode($result, JSON_UNESCAPED_UNICODE);
 
 ?>

+ 10 - 0
package-lock.json

@@ -2,6 +2,11 @@
   "requires": true,
   "lockfileVersion": 1,
   "dependencies": {
+    "at-js": {
+      "version": "1.2.12",
+      "resolved": "https://registry.npmjs.org/at-js/-/at-js-1.2.12.tgz",
+      "integrity": "sha1-X26O1gXopRmgXn0lMKSgKaxtPCU="
+    },
     "highcharts": {
       "version": "9.0.1",
       "resolved": "https://registry.npmjs.org/highcharts/-/highcharts-9.0.1.tgz",
@@ -11,6 +16,11 @@
       "version": "2.0.3",
       "resolved": "https://registry.npmjs.org/marked/-/marked-2.0.3.tgz",
       "integrity": "sha512-5otztIIcJfPc2qGTN8cVtOJEjNJZ0jwa46INMagrYfk0EvqtRuEHLsEe0LrFS0/q+ZRKT0+kXK7P2T1AN5lWRA=="
+    },
+    "tributejs": {
+      "version": "5.1.3",
+      "resolved": "https://registry.npmjs.org/tributejs/-/tributejs-5.1.3.tgz",
+      "integrity": "sha512-B5CXihaVzXw+1UHhNFyAwUTMDk1EfoLP5Tj1VhD9yybZ1I8DZJEv8tZ1l0RJo0t0tk9ZhR8eG5tEsaCvRigmdQ=="
     }
   }
 }

+ 3 - 1
package.json

@@ -2,7 +2,9 @@
   "requires": true,
   "lockfileVersion": 1,
   "dependencies": {
+    "at-js": "^1.2.12",
     "highcharts": "^9.0.1",
-    "marked": "^2.0.3"
+    "marked": "^2.0.3",
+    "tributejs": "^5.1.3"
   }
 }