visuddhinanda преди 4 години
родител
ревизия
94df23ae2f
променени са 6 файла, в които са добавени 106 реда и са изтрити 26 реда
  1. 10 11
      app/article/function.php
  2. 44 4
      app/collect/function.php
  3. 4 5
      app/sync/function.php
  4. 3 3
      app/sync/sync.js
  5. 1 1
      app/sync/table_article.php
  6. 44 2
      app/sync/table_article_collect.php

+ 10 - 11
app/article/function.php

@@ -141,33 +141,32 @@ 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 = ? ";
 		$stmt = $this->dbh->prepare($query);
 		if($stmt){
-			$stmt->execute($collectionId);
+			$stmt->execute(array($collectionId));
 		}
-        
+        $table->beginTransaction($query)
+			  ->set($date)
+			  ->commit();
 		if(count($articleList)>0){
 			/* 开始一个事务,关闭自动提交 */
 			$this->dbh->beginTransaction();
 			$query = "INSERT INTO article_list (collect_id, article_id,level,title) VALUES ( ?, ?, ? , ? )";
 			$sth = $this->dbh->prepare($query);
 			foreach ($articleList as $row) {
-				$sth->execute(array($_POST["id"],$row->article,$row->level,$row->title));
-				if($redis){
+				$sth->execute(array($collectionId,$row["article"],$row["level"],$row["title"]));
+				if($this->redis){
 					#删除article权限缓存
-					$redis->del("power://article/".$row->article);
+					$this->redis->del("power://article/".$row["article"]);
 				}
 			}
-			$PDO->commit();
+			$this->dbh->commit();
 			if (!$sth || ($sth && $sth->errorCode() != 0)) {
 				/*  识别错误且回滚更改  */
-				$PDO->rollBack();
-				$error = PDO_ErrorInfo();
+				$this->dbh->rollBack();
+				$error = $this->dbh->errorInfo();;
 				$respond['status']=1;
 				$respond['message']=$error[2];
 			}

+ 44 - 4
app/collect/function.php

@@ -18,6 +18,10 @@ class CollectInfo
         $buffer = array();
     }
 
+	public function getError(){
+		return $this->errorMsg;
+	}
+
     public function get($id){
         if(empty($id)){
             return array("title"=>"","id"=>"");
@@ -122,10 +126,28 @@ class CollectInfo
 	
 		if (count($arrData) > 0) {
 			$this->dbh->beginTransaction();
-			$query="UPDATE collect SET title = ? , subtitle = ? , summary = ?, article_list = ?  ,  status = ? , lang = ? , receive_time= ?  , modify_time= ?   where  id = ?  ";
+			$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"]));
+				$sth->execute(array(
+									$data["title"] , 
+									$data["subtitle"] ,
+									$data["summary"], 
+									$data["article_list"] , 
+									$data["status"] , 
+									$data["lang"] ,  
+									$data["receive_time"] , 
+									$data["modify_time"] , 
+									$data["id"])
+								);
 			}
 			$this->dbh->commit();
 		
@@ -188,7 +210,13 @@ class CollectInfo
 					}
 					else{
 						$newData["article_list"]="";
-					}		
+					}	
+					if(isset($data["owner"])){
+						$newData["owner"]=$data["owner"];
+					}
+					else{
+						$newData["owner"]=$_COOKIE["userid"];
+					}	
 					if(isset($data["lang"])){
 						$newData["lang"]=$data["lang"];
 					}
@@ -201,8 +229,20 @@ class CollectInfo
 					else{
 						$newData["status"]="1";
 					}
+					if(isset($data["create_time"])){
+						$newData["create_time"]=$data["create_time"];
+					}
+					else{
+						$newData["create_time"]=mTime();
+					}
+					if(isset($data["modify_time"])){
+						$newData["modify_time"]=$data["modify_time"];
+					}
+					else{
+						$newData["modify_time"]=mTime();
+					}
 					$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() ));				
+					$sth->execute(array($newData["id"] , $newData["title"] , $newData["subtitle"] ,$newData["summary"], $newData["article_list"] , $newData["owner"] , $newData["lang"] , $newData["status"] , $newData["create_time"] ,  $newData["modify_time"] , mTime() ));				
 				}
 				else{
 					$this->errorMsg="标题不能为空";

+ 4 - 5
app/sync/function.php

@@ -58,14 +58,14 @@ function do_sync($param)
 				}
 				if(isset($_POST["time"])){
 					$time = $_POST["time"];
-					$query = "SELECT {$param->uuid} as guid, {$param->modify_time} as modify_time from {$param->table}  where {$param->modify_time} > ? order by {$param->modify_time} ASC  limit 0,".$size;
+					$query = "SELECT {$param->uuid} as guid, {$param->receive_time} as receive_time from {$param->table}  where {$param->receive_time} > ? order by {$param->receive_time}  limit 0,".$size;
 					$stmt = $PDO->prepare($query);
 					$stmt->execute(array($time));
 					$Fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
 
 					if(count($Fetch)>0){
-						$newTime = $Fetch[count($Fetch)-1]["modify_time"];
-						$query = "SELECT {$param->uuid} as guid, {$param->modify_time} as modify_time from {$param->table}  where {$param->modify_time} > ? and {$param->modify_time} <= ?  order by {$param->modify_time} ASC ";
+						$newTime = $Fetch[count($Fetch)-1]["receive_time"];
+						$query = "SELECT {$param->uuid} as guid, {$param->modify_time} as modify_time from {$param->table}  where {$param->receive_time} > ? and {$param->receive_time} <= ?  order by {$param->receive_time} ";
 						$stmt = $PDO->prepare($query);
 						$stmt->execute(array($time,$newTime));
 						$Fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
@@ -79,7 +79,7 @@ function do_sync($param)
 					$count =count($params);
 					/*  创建一个填充了和params相同数量占位符的字符串 */
 					$place_holders = implode(',', array_fill(0, count($params), '?'));
-					$query = "SELECT {$param->uuid} as guid, {$param->modify_time} from {$param->table}  where {$param->uuid} in ($place_holders)  limit 0,".$size;
+					$query = "SELECT {$param->uuid} as guid, {$param->modify_time} as modify_time from {$param->table}  where {$param->uuid} in ($place_holders) order by {$param->receive_time} ASC  limit 0,".$size;
 					$stmt = $PDO->prepare($query);
 					$stmt->execute($params);
 					$Fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
@@ -183,7 +183,6 @@ function do_sync($param)
                 try {
                     $PDO->beginTransaction();
                     foreach ($arrData as $one) {
-
                         $newRow = array();
                         foreach ($param->update as $row) {
                             $newRow[] = $one["{$row}"];

+ 3 - 3
app/sync/sync.js

@@ -1,8 +1,8 @@
 var sync_db_list = [
-	"sync/table_article.php",
-	/*"sync/table_term.php",
+	/*"sync/table_article.php",*/
+	/*"sync/table_term.php",*/
 	"sync/table_article_collect.php",
-	"sync/table_sentence.php",*/
+	/*"sync/table_sentence.php",*/
 ];
 var sync_curr_do_db = 0;
 function sync_index_init() {}

+ 1 - 1
app/sync/table_article.php

@@ -9,7 +9,7 @@ $input = (object) [
     "table" =>  "article",
     "uuid" =>  "id",
     "modify_time" =>  "modify_time",
-    "receive_time" =>  "modify_time",
+    "receive_time" =>  "receive_time",
     "insert" => [
         'id',
 		'title',

+ 44 - 2
app/sync/table_article_collect.php

@@ -2,14 +2,17 @@
 //header('Content-type: application/json; charset=utf8');
 
 require_once "../path.php";
+require_once "../public/function.php";
 require_once "../sync/function.php";
+require_once "../collect/function.php";
+require_once "../redis/function.php";
 
 $input = (object) [
     "database" =>  _FILE_DB_USER_ARTICLE_,
     "table" =>  "collect",
     "uuid" =>  "id",
     "modify_time" =>  "modify_time",
-    "receive_time" =>  "modify_time",
+    "receive_time" =>  "receive_time",
     "insert" => [
         'id',
 		'title',
@@ -38,6 +41,9 @@ $input = (object) [
 ];
 
 
+$output=array();
+$output["error"]=0;
+$output["message"]="";
 
 if (isset($_GET["op"])) {
 	$op = $_GET["op"];
@@ -60,7 +66,43 @@ switch ($op) {
 		echo json_encode($result, JSON_UNESCAPED_UNICODE);
 	break;
 	case "insert":
-		
+		if (isset($_POST["data"])) {
+			$data = $_POST["data"];
+			$arrData = json_decode($data, true);
+			foreach ($arrData as $key => $value) {
+				# 设置数据同步时间
+				$arrData[$key]["receive_time"]=mTime();
+			}
+			$collection=new CollectInfo(redis_connect());
+			$result = $collection->insert($arrData);
+			if($result==false){
+				$output["error"]=1;
+				$output["message"]="没有提交数据";				
+			}
+		} else {
+			$output["error"]=1;
+			$output["message"]=$collection->getError();
+		}
+		echo json_encode($result, JSON_UNESCAPED_UNICODE);
+	break;	
+	case "update":
+		if (isset($_POST["data"])) {
+			$data = $_POST["data"];
+			$arrData = json_decode($data, true);
+			foreach ($arrData as $key => $value) {
+				# 设置数据同步时间
+				$arrData[$key]["receive_time"]=mTime();
+			}
+			$collection=new CollectInfo(redis_connect());
+			$result = $collection->update($arrData);
+			if($result==false){
+				$output["error"]=1;
+				$output["message"]="没有提交数据";				
+			}
+		} else {
+			$output["error"]=1;
+			$output["message"]=$collection->getError();
+		}
 		echo json_encode($result, JSON_UNESCAPED_UNICODE);
 	break;	
 }