visuddhinanda 4 лет назад
Родитель
Сommit
c26e39ed3c
7 измененных файлов с 158 добавлено и 54 удалено
  1. 20 1
      app/sync/function.php
  2. 4 3
      app/sync/index.php
  3. 110 41
      app/sync/sync.js
  4. 10 3
      app/sync/sync.php
  5. 4 0
      app/sync/table_sentence.php
  6. 1 1
      app/term/related_para.js
  7. 9 5
      app/usent/function.php

+ 20 - 1
app/sync/function.php

@@ -45,6 +45,25 @@ function do_sync($param)
     $PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
 
     switch ($op) {
+        case "sync_count":
+			if(isset($_POST["time"])){
+				$time = $_POST["time"];
+				$query = "SELECT  count(*) as co from {$param->table} where {$param->receive_time} > ? {$param->where} ";
+				$stmt = $PDO->prepare($query);
+				$stmt->execute(array($time));
+				$Fetch = $stmt->fetch(PDO::FETCH_ASSOC);
+				if($Fetch){
+					$output["data"]=(int)$Fetch["co"];
+				}
+				else{
+					$output["data"]=0;
+				}
+			}
+			else{
+				$output["data"]=0;
+			}
+			return $output;	
+			break;
         case "sync":
             {
 				if(isset($_POST["size"])){
@@ -66,7 +85,7 @@ function do_sync($param)
 					if(count($Fetch)>0){
 						$newTime = $Fetch[count($Fetch)-1]["receive_time"];
 						$syncId = implode(",",$param->sync_id);
-						$query = "SELECT {$param->uuid} as guid, $syncId , {$param->modify_time} as modify_time from {$param->table}  where {$param->receive_time} > ? and {$param->receive_time} <= ? {$param->where}  order by {$param->receive_time} ";
+						$query = "SELECT {$param->uuid} as guid, $syncId , {$param->modify_time} as modify_time , {$param->receive_time} as receive_time from {$param->table}  where {$param->receive_time} > ? and {$param->receive_time} <= ? {$param->where}  order by {$param->receive_time} ";
 						$stmt = $PDO->prepare($query);
 						$stmt->execute(array($time,$newTime));
 						$Fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);

+ 4 - 3
app/sync/index.php

@@ -32,12 +32,13 @@ require_once "../pcdl/html_head.php";
 				
 				<button onclick="sync_pull()">pull</button>
 				<button onclick="sync_push()">push</button>
+				<button onclick="sync_stop()">stop</button>
 			</div>
 		</div>
 		<div>上次更新时间:</div>
-		<div id="sync_result">
-
-		</div>
+		
+		<div id="sync_result"></div>
+		<div id="sync_log"></div>
 	</div>
 		
 	<script>

+ 110 - 41
app/sync/sync.js

@@ -1,63 +1,132 @@
 var sync_db_list = [
-	/*"sync/table_article.php",*/
-	/*"sync/table_term.php",*/
-	/*"sync/table_article_collect.php",*/
-	"sync/table_sentence.php",
+	{ script: "sync/table_article.php", count: -1, finished: 0, enable: false },
+	{ script: "sync/table_term.php", count: -1, finished: 0, enable: false },
+	{ script: "sync/table_article_collect.php", count: -1, finished: 0, enable: false },
+	{ script: "sync/table_sentence.php", count: -1, finished: 0, enable: true },
 ];
+var isStop = false;
 var sync_curr_do_db = 0;
-function sync_index_init() {}
+function sync_index_init() {
+	render_progress();
+}
 
 function sync_pull() {
 	sync_curr_do_db = 0;
-	$("#sync_result").html("working"); //
+	isStop = false;
+	$("#sync_log").html("working"); //
 	sync_do_db($("#sync_server_address").val(), $("#sync_local_address").val(), 1);
 }
 function sync_push() {
+	isStop = false;
 	sync_curr_do_db = 0;
-	$("#sync_result").html("working"); //
+	$("#sync_log").html("working"); //
 	sync_do_db($("#sync_local_address").val(), $("#sync_server_address").val(), 1);
 }
+function sync_stop() {
+	isStop = true;
+}
 function sync_do_db(src, dest, time = 1) {
 	let size = 500;
-	$.get(
-		"sync.php",
-		{
-			server: src,
-			localhost: dest,
-			path: sync_db_list[sync_curr_do_db],
-			time: time,
-			size: size,
-		},
-		function (data) {
-			let result;
-			try {
-				result = JSON.parse(data);
-			} catch (error) {
-				console.error(error + " data:" + data);
-				return;
+	while (sync_db_list[sync_curr_do_db].enable == false) {
+		sync_curr_do_db++;
+		if (sync_curr_do_db >= sync_db_list.length) {
+			$("#sync_log").html($("#sync_log").html() + "<br>All Done"); //
+			return;
+		}
+	}
+	if (sync_db_list[sync_curr_do_db].count < 0) {
+		$.get(
+			"sync.php",
+			{
+				server: src,
+				localhost: dest,
+				path: sync_db_list[sync_curr_do_db].script,
+				time: time,
+				size: -1,
+			},
+			function (data) {
+				let result;
+				try {
+					result = JSON.parse(data);
+					sync_db_list[sync_curr_do_db].count = parseInt(result.data);
+					sync_do_db(src, dest, time);
+				} catch (error) {
+					console.error(error + " data:" + data);
+					return;
+				}
 			}
-			$("#sync_result").html(
-				$("#sync_result").html() +
-					"<div><h2>" +
-					sync_db_list[sync_curr_do_db] +
-					"</h2>" +
-					result.message +
-					"</div>"
-			); //
-			if (result.src_row >= size) {
-				sync_do_db(src, dest, result.time);
-			} else {
-				sync_curr_do_db++;
-				if (sync_curr_do_db < sync_db_list.length) {
-					sync_do_db(src, dest, 1);
+		);
+	} else {
+		$.get(
+			"sync.php",
+			{
+				server: src,
+				localhost: dest,
+				path: sync_db_list[sync_curr_do_db].script,
+				time: time,
+				size: size,
+			},
+			function (data) {
+				let result;
+				try {
+					result = JSON.parse(data);
+				} catch (error) {
+					console.error(error + " data:" + data);
+					return;
+				}
+				$("#sync_log").html(
+					$("#sync_log").html() +
+						"<div><h2>" +
+						sync_db_list[sync_curr_do_db].script +
+						"</h2>" +
+						result.message +
+						"</div>"
+				); //
+				render_progress();
+				if (isStop) {
+					return;
+				}
+				sync_db_list[sync_curr_do_db].finished += parseInt(result.src_row);
+				if (result.src_row >= size) {
+					//没弄完,接着弄
+					sync_do_db(src, dest, result.time);
 				} else {
-					$("#sync_result").html($("#sync_result").html() + "<br>All Done"); //
+					sync_curr_do_db++;
+
+					if (sync_curr_do_db < sync_db_list.length) {
+						while (sync_db_list[sync_curr_do_db].enable == false) {
+							sync_curr_do_db++;
+							if (sync_curr_do_db >= sync_db_list.length) {
+								$("#sync_log").html($("#sync_log").html() + "<br>All Done"); //
+								return;
+							}
+						}
+						sync_do_db(src, dest, 1);
+					} else {
+						$("#sync_log").html($("#sync_log").html() + "<br>All Done"); //
+					}
 				}
 			}
-		}
-	);
+		);
+	}
+}
+function render_progress() {
+	let html = "";
+	for (const iterator of sync_db_list) {
+		let spanWidth = parseInt((500 * iterator.finished) / iterator.count);
+		html +=
+			"<div style='width:500px;background-color:white;color:black;'><span style='background-color:green;display:inline-block;width:" +
+			spanWidth +
+			"px;'>" +
+			iterator.script +
+			"|" +
+			iterator.finished +
+			"/" +
+			iterator.count +
+			"<span></div>";
+	}
+	$("#sync_result").html(html);
 }
-
 function login() {
 	$("#server_msg").html("正在登录<br>");
 	$.post(

+ 10 - 3
app/sync/sync.php

@@ -30,6 +30,13 @@ else{
 }
 
 $client = new \GuzzleHttp\Client();
+if($size<0){
+	$response = $client->request('POST', $server.'/app/'.$path,['verify' => false,'form_params'=>['op'=>'sync_count','time'=>$time,'size'=>$size,"key"=>$sync_key,"userid"=>$_COOKIE["userid"]]]);
+	$serverJson=(string)$response->getBody();
+	$serverData = json_decode($serverJson,true);
+	echo json_encode($serverData, JSON_UNESCAPED_UNICODE);
+	exit;	
+}
 $response = $client->request('POST', $server.'/app/'.$path,['verify' => false,'form_params'=>['op'=>'sync','time'=>$time,'size'=>$size,"key"=>$sync_key,"userid"=>$_COOKIE["userid"]]]);
 $serverJson=(string)$response->getBody();
 $serverData = json_decode($serverJson,true);
@@ -51,7 +58,7 @@ $output["src_row"]=count($serverDBData);
 $message.= "输入时间:".$time." | ";
 $message.= "src_row:".$output["src_row"]." | ";
 if($output["src_row"]>0){
-	$output["time"]=$serverDBData[$output["src_row"]-1]["modify_time"];
+	$output["time"]=$serverDBData[$output["src_row"]-1]["receive_time"];
 	$message.= "最新时间:".$output["time"]." | ";
 }
 else{
@@ -153,7 +160,7 @@ else{
 	$message .= "<div>";
 	if(count($update_to_local)>0){
 		$message .=  "需要更新到本地".count($update_to_local)."条记录 | ";
-		
+		/*
 		$idInServer = json_encode($update_to_local, JSON_UNESCAPED_UNICODE);
 		$response = $client->request('POST', $server.'/app/'.$path,['verify' => false,'form_params'=>['op'=>'get','id'=>"{$idInServer}","key"=>$sync_key,"userid"=>$_COOKIE["userid"]]]);
 		$serverData=(string)$response->getBody();
@@ -169,7 +176,7 @@ else{
 		else{
 			$message .= "数据提取错误 错误信息:{$arrData["message"]} ";
 		}
-		
+		*/
 	}
 	$message .= "</div>";
 

+ 4 - 0
app/sync/table_sentence.php

@@ -69,6 +69,10 @@ if (isset($_GET["op"])) {
 }
 
 switch ($op) {
+	case "sync_count":
+		$result = do_sync($input);
+		echo json_encode($result, JSON_UNESCAPED_UNICODE);
+	break;		
 	case "sync":
 		$result = do_sync($input);
 		echo json_encode($result, JSON_UNESCAPED_UNICODE);

+ 1 - 1
app/term/related_para.js

@@ -47,7 +47,7 @@ function related_para_dlg_render(para) {
 				}
 			}
 			output +=
-				"<a href='../reader/?view=para&book=" +
+				"<a href='../reader/?view=chapter&book=" +
 				paraList[0].book +
 				"&para=" +
 				paraList[0].para +

+ 9 - 5
app/usent/function.php

@@ -181,13 +181,14 @@ class Sent_DB
 			//add_edit_event(_SENT_EDIT_, "{$oldList[0]["book"]}-{$oldList[0]["paragraph"]}-{$oldList[0]["begin"]}-{$oldList[0]["end"]}@{$oldList[0]["channal"]}");
 	
 			$this->dbh_sent->beginTransaction();
-			if(isset($data["book"]) && isset($data["paragraph"]) && isset($data["begin"]) && isset($data["end"]) && isset($data["channal"])){
-				if(!isset($data["modify_time"])){
-					$data["modify_time"] = mTime();
-				}
+
+			if(isset($arrData[0]["book"]) && isset($arrData[0]["paragraph"]) && isset($arrData[0]["begin"]) && isset($arrData[0]["end"]) && isset($arrData[0]["channal"])){
 				$query = "UPDATE sentence SET text = ? , strlen = ? , editor=?, modify_time= ? , receive_time = ?  where  book = ? and paragraph=? and [begin]=? and [end]=? and channal=?  ";
 				$sth = $this->dbh_sent->prepare($query);
 				foreach ($arrData as $data) {
+					if(!isset($data["modify_time"])){
+						$data["modify_time"] = mTime();
+					}
 					$sth->execute(array($data["text"],mb_strlen($data["text"],"UTF-8"),$data["editor"],$data["modify_time"],mTime(),$data["book"],$data["paragraph"],$data["begin"],$data["end"],$data["channal"]));
 				}
 			}
@@ -195,7 +196,10 @@ class Sent_DB
 				$query = "UPDATE sentence SET text = ? , strlen = ? , editor=?, modify_time= ? ,receive_time = ?   where  id= ?  ";
 				$sth = $this->dbh_sent->prepare($query);
 				foreach ($arrData as $data) {
-					$sth->execute(array($data["text"],mb_strlen($data["text"],"UTF-8"),$data["editor"],mTime(),mTime(),$data["id"]));
+					if(!isset($data["modify_time"])){
+						$data["modify_time"]=mTime();
+					}
+					$sth->execute(array($data["text"],mb_strlen($data["text"],"UTF-8"),$data["editor"],$data["modify_time"],mTime(),$data["id"]));
 				}				
 			}