Просмотр исходного кода

功能增加:更新经验值索引表

visuddhinanda 5 лет назад
Родитель
Сommit
40e5d0fd3d

+ 55 - 0
app/admin/update_user_active_time.php

@@ -0,0 +1,55 @@
+<?php
+require_once '../path.php';
+
+date_default_timezone_set("UTC");
+$logfile = "update_user_active_time_last.txt";
+$last = file_get_contents($logfile);
+$start = strtotime($last." +1 day");
+$end = strtotime($last." +2 day");
+$today = strtotime("today");
+
+$dns = "sqlite:"._FILE_DB_USER_ACTIVE_;
+$dbh = new PDO($dns, "", "",array(PDO::ATTR_PERSISTENT=>true));
+$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);  
+
+
+$dns = "sqlite:"._FILE_DB_USER_ACTIVE_INDEX_;
+$dbh_index = new PDO($dns, "", "",array(PDO::ATTR_PERSISTENT=>true));
+$dbh_index->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);  
+/* 开始一个事务,关闭自动提交 */
+$dbh_index->beginTransaction();
+$query = "INSERT INTO active_index (user_id, date , duration , hit) VALUES (?, ?, ? , ?)";
+$sth_index = $dbh_index->prepare($query);
+
+$runing=$last;
+while ($end <= $today) {
+	$runing=gmdate("Y-m-d",$start);
+	echo "runing:".$runing."\n";
+
+	# do one day
+	$query = "SELECT user_id, sum(duration) as time , sum(hit) as hit  FROM edit WHERE end between ? and ?  group by user_id ";
+	$stmt = $dbh->prepare($query);
+	$stmt->execute(array($start*1000,$end*1000));
+	while($result = $stmt->fetch(PDO::FETCH_ASSOC))
+	{
+		$user_id = $result["user_id"];
+		$time = $result["time"];
+		$hit = $result["hit"];
+		$sth_index->execute(array($user_id,$start*1000,$time,$hit));
+		echo "$user_id - $time - $hit \n";
+	}
+	$start = $end;
+	$end = strtotime( gmdate("Y-m-d",$end)." +1 day");
+}
+$dbh_index->commit();
+if (!$sth_index || ($sth_index && $sth_index->errorCode() != 0)) {
+	/*  识别错误且回滚更改  */
+	$sth_index->rollBack();
+	$error = $dbh_index->errorInfo();
+	echo "error:".$error[2]."\n";
+}
+
+echo "run until:".$runing."\n";
+echo file_put_contents($logfile,$runing);
+
+?>

+ 1 - 0
app/admin/update_user_active_time_last.txt

@@ -0,0 +1 @@
+2021-02-02

+ 1 - 0
app/path.php

@@ -85,5 +85,6 @@ define("_FILE_DB_USER_ARTICLE_"  , __DIR__."/../tmp/user/article.db3");
 define("_FILE_DB_HOSTSETTING_"  , __DIR__."/../tmp/user/hostsetting.db3");
 define("_FILE_DB_USER_SENTENCE_HISTORAY_"  , __DIR__."/../tmp/user/usent_historay.db3");
 define("_FILE_DB_USER_ACTIVE_"  , __DIR__."/../tmp/user/user_active.db3");
+define("_FILE_DB_USER_ACTIVE_INDEX_"  , __DIR__."/../tmp/user/user_active_index.db3");
 
 ?>

+ 122 - 116
app/public/js/comm.js

@@ -2,150 +2,156 @@ var VisibleMenu = ""; // 記錄目前顯示的子選單的 ID
 
 // 顯示或隱藏子選單
 function switchMenu(theMainMenu, theSubMenu, theEvent) {
-  var SubMenu = document.getElementById(theSubMenu);
-  if (SubMenu.style.display == "none") {
-    // 顯示子選單
-    SubMenu.style.display = "block";
-    hideMenu(); // 隱藏子選單
-    VisibleMenu = theSubMenu;
-  } else {
-    // 隱藏子選單
-    if (theEvent != "MouseOver" || VisibleMenu != theSubMenu) {
-      SubMenu.style.display = "none";
-      VisibleMenu = "";
-    }
-  }
+	var SubMenu = document.getElementById(theSubMenu);
+	if (SubMenu.style.display == "none") {
+		// 顯示子選單
+		SubMenu.style.display = "block";
+		hideMenu(); // 隱藏子選單
+		VisibleMenu = theSubMenu;
+	} else {
+		// 隱藏子選單
+		if (theEvent != "MouseOver" || VisibleMenu != theSubMenu) {
+			SubMenu.style.display = "none";
+			VisibleMenu = "";
+		}
+	}
 }
 
 // 隱藏子選單
 function hideMenu() {
-  if (VisibleMenu != "") {
-    document.getElementById(VisibleMenu).style.display = "none";
-  }
-  VisibleMenu = "";
+	if (VisibleMenu != "") {
+		document.getElementById(VisibleMenu).style.display = "none";
+	}
+	VisibleMenu = "";
 }
 function com_show_sub_tree(obj) {
-  eParent = obj.parentNode;
-  var x = eParent.getElementsByTagName("ul");
-  if (x[0].style.display == "none") {
-    x[0].style.display = "block";
-    obj.getElementsByTagName("span")[0].innerHTML = "-";
-  } else {
-    x[0].style.display = "none";
-    obj.getElementsByTagName("span")[0].innerHTML = "+";
-  }
+	eParent = obj.parentNode;
+	var x = eParent.getElementsByTagName("ul");
+	if (x[0].style.display == "none") {
+		x[0].style.display = "block";
+		obj.getElementsByTagName("span")[0].innerHTML = "-";
+	} else {
+		x[0].style.display = "none";
+		obj.getElementsByTagName("span")[0].innerHTML = "+";
+	}
 }
 
 //check if the next sibling node is an element node
 function com_get_nextsibling(n) {
-  let x = n.nextSibling;
-  if (x != null) {
-    while (x.nodeType != 1) {
-      x = x.nextSibling;
-      if (x == null) {
-        return null;
-      }
-    }
-  }
-  return x;
+	let x = n.nextSibling;
+	if (x != null) {
+		while (x.nodeType != 1) {
+			x = x.nextSibling;
+			if (x == null) {
+				return null;
+			}
+		}
+	}
+	return x;
 }
 
 function com_guid(trim = true, hyphen = false) {
-  //guid生成器
-  if (trim) {
-    if (hyphen) {
-      var tmp = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
-    } else {
-      var tmp = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
-    }
-  } else {
-    if (hyphen) {
-      var tmp = "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}";
-    } else {
-      var tmp = "{xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}";
-    }
-  }
+	//guid生成器
+	if (trim) {
+		if (hyphen) {
+			var tmp = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
+		} else {
+			var tmp = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
+		}
+	} else {
+		if (hyphen) {
+			var tmp = "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}";
+		} else {
+			var tmp = "{xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}";
+		}
+	}
 
-  var guid = tmp.replace(/[xy]/g, function (c) {
-    var r = (Math.random() * 16) | 0,
-      v = c == "x" ? r : (r & 0x3) | 0x8;
-    return v.toString(16);
-  });
-  return guid.toUpperCase();
+	var guid = tmp.replace(/[xy]/g, function (c) {
+		var r = (Math.random() * 16) | 0,
+			v = c == "x" ? r : (r & 0x3) | 0x8;
+		return v.toString(16);
+	});
+	return guid.toUpperCase();
 }
 function com_uuid() {
-  //guid生成器
-  let tmp = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
-  let uuid = tmp.replace(/[xy]/g, function (c) {
-    var r = (Math.random() * 16) | 0,
-      v = c == "x" ? r : (r & 0x3) | 0x8;
-    return v.toString(16);
-  });
-  return uuid.toLowerCase();
+	//guid生成器
+	let tmp = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
+	let uuid = tmp.replace(/[xy]/g, function (c) {
+		var r = (Math.random() * 16) | 0,
+			v = c == "x" ? r : (r & 0x3) | 0x8;
+		return v.toString(16);
+	});
+	return uuid.toLowerCase();
 }
 function com_xmlToString(elem) {
-  var serialized;
-  try {
-    serializer = new XMLSerializer();
-    serialized = serializer.serializeToString(elem);
-  } catch (e) {
-    serialized = elem.xml;
-  }
-  return serialized;
+	var serialized;
+	try {
+		serializer = new XMLSerializer();
+		serialized = serializer.serializeToString(elem);
+	} catch (e) {
+		serialized = elem.xml;
+	}
+	return serialized;
 }
 
 function com_getPaliReal(inStr) {
-  var paliletter = "abcdefghijklmnoprstuvyāīūṅñṭḍṇḷṃ";
-  var output = "";
-  inStr = inStr.toLowerCase();
-  inStr = inStr.replace(/ṁ/g, "ṃ");
-  inStr = inStr.replace(/ŋ/g, "ṃ");
-  for (x in inStr) {
-    if (paliletter.indexOf(inStr[x]) != -1) {
-      output += inStr[x];
-    }
-  }
-  return output;
+	var paliletter = "abcdefghijklmnoprstuvyāīūṅñṭḍṇḷṃ";
+	var output = "";
+	inStr = inStr.toLowerCase();
+	inStr = inStr.replace(/ṁ/g, "ṃ");
+	inStr = inStr.replace(/ŋ/g, "ṃ");
+	for (x in inStr) {
+		if (paliletter.indexOf(inStr[x]) != -1) {
+			output += inStr[x];
+		}
+	}
+	return output;
 }
 
 function getCookie(c_name) {
-  if (document.cookie.length > 0) {
-    c_start = document.cookie.indexOf(c_name + "=");
-    if (c_start != -1) {
-      c_start = c_start + c_name.length + 1;
-      c_end = document.cookie.indexOf(";", c_start);
-      if (c_end == -1) c_end = document.cookie.length;
-      return unescape(document.cookie.substring(c_start, c_end));
-    } else {
-      return "";
-    }
-  } else {
-    return "";
-  }
+	if (document.cookie.length > 0) {
+		c_start = document.cookie.indexOf(c_name + "=");
+		if (c_start != -1) {
+			c_start = c_start + c_name.length + 1;
+			c_end = document.cookie.indexOf(";", c_start);
+			if (c_end == -1) c_end = document.cookie.length;
+			return unescape(document.cookie.substring(c_start, c_end));
+		} else {
+			return "";
+		}
+	} else {
+		return "";
+	}
+}
+
+function setTimeZone() {
+	const date = new Date();
+	const timezone = date.getTimezoneOffset();
+	setCookie("timezone", timezone, 10);
 }
 
 function setCookie(c_name, value, expiredays) {
-  var exdate = new Date();
-  exdate.setDate(exdate.getDate() + expiredays);
-  document.cookie =
-    c_name +
-    "=" +
-    escape(value) +
-    (expiredays == null ? "" : "; expires=" + exdate.toGMTString() + ";path=/");
+	var exdate = new Date();
+	exdate.setDate(exdate.getDate() + expiredays);
+	document.cookie =
+		c_name + "=" + escape(value) + (expiredays == null ? "" : "; expires=" + exdate.toGMTString() + ";path=/");
 }
 
 function copy_to_clipboard(strInput) {
-  const input = document.createElement("input");
-  input.setAttribute("readonly", "readonly");
-  input.setAttribute("value", strInput);
-  document.body.appendChild(input);
-  //	input.setSelectionRange(0, strInput.length);
-  //	input.focus();
-  input.select();
-  if (document.execCommand("copy")) {
-    document.execCommand("copy");
-    console.log("复制成功");
-    ntf_show("“" + strInput + "”" + gLocal.gui.copied_to_clipboard);
-  }
-  document.body.removeChild(input);
+	const input = document.createElement("input");
+	input.setAttribute("readonly", "readonly");
+	input.setAttribute("value", strInput);
+	document.body.appendChild(input);
+	//	input.setSelectionRange(0, strInput.length);
+	//	input.focus();
+	input.select();
+	if (document.execCommand("copy")) {
+		document.execCommand("copy");
+		console.log("复制成功");
+		ntf_show("“" + strInput + "”" + gLocal.gui.copied_to_clipboard);
+	}
+	document.body.removeChild(input);
 }
+
+//所有页面都需要在加载的的时候设置浏览器时区
+setTimeZone();

+ 55 - 4
app/ucenter/active.php

@@ -4,6 +4,7 @@ require_once '../path.php';
 require_once "../public/function.php";
 
 function add_edit_event($type="",$data=null){
+	date_default_timezone_set("UTC");
 	define("MAX_INTERVAL",600000);
 	define("MIN_INTERVAL",10000);
 	
@@ -20,9 +21,9 @@ function add_edit_event($type="",$data=null){
 		$currTime = mTime();
 		if ($row) {
 			//找到,判断是否超时,超时新建,未超时修改
-			$endtime = (int)$row["end"];
 			$id = (int)$row["id"];
 			$start_time = (int)$row["start"];
+			$endtime = (int)$row["end"];
 			$hit = (int)$row["hit"];
 			if($currTime-$endtime>MAX_INTERVAL){
 				//超时新建
@@ -37,23 +38,73 @@ function add_edit_event($type="",$data=null){
 			$new_record = true;
 		}
 	
+		#获取客户端时区偏移 beijing = +8
+		if(isset($_COOKIE["timezone"])){
+			$client_timezone = (0-(int)$_COOKIE["timezone"])*60*1000;
+		}
+		else{
+			$client_timezone = 0;
+		}
+		
+		$this_active_time=0;//时间增量
 		if($new_record){
-			$query="INSERT INTO edit ( user_id, start , end  , duration , hit )  VALUES  ( ? , ? , ? , ? , ? ) ";
+			#新建
+			$query="INSERT INTO edit ( user_id, start , end  , duration , hit , timezone )  VALUES  ( ? , ? , ? , ? , ? ,?) ";
 			$sth = $dbh->prepare($query);
-			$sth->execute(array($_COOKIE["userid"] , $currTime , ($currTime+MIN_INTERVAL) , MIN_INTERVAL,1) );
+			#最小思考时间
+			$sth->execute(array($_COOKIE["userid"] , ($currTime-MIN_INTERVAL) ,$currTime ,  MIN_INTERVAL,1 ,$client_timezone ) );
 			if (!$sth || ($sth && $sth->errorCode() != 0)) {
 				$error = $dbh->errorInfo();
 			}
+			$this_active_time=MIN_INTERVAL;
 		}
 		else{
-	
+			#修改
+			$this_active_time=$currTime-$endtime;
 			$query="UPDATE edit SET end = ? , duration = ? , hit = ? WHERE id = ? ";
 			$sth = $dbh->prepare($query);
 			$sth->execute( array($currTime,($currTime-$start_time), ($hit+1),$id));
 			if (!$sth || ($sth && $sth->errorCode() != 0)) {
 				$error = $dbh->errorInfo();
 			}
+			
 		}
+
+		#更新经验总量表
+		#计算客户端日期 unix时间戳 以毫秒计
+		$client_currtime = $currTime + $client_timezone;
+		$client_date = strtotime(gmdate("Y-m-d",$client_currtime/1000))*1000;
+		
+		#查询是否存在
+		$query = "SELECT id,duration,hit  FROM active_index WHERE user_id = ? and date = ?";
+		$sth = $dbh->prepare($query);
+		$sth->execute(array($_COOKIE["userid"],$client_date));
+		$row = $sth->fetch(PDO::FETCH_ASSOC);
+		if ($row) {
+			#更新
+			$id = (int)$row["id"];
+			$duration = (int)$row["duration"];
+			$hit = (int)$row["hit"];
+			#修改
+			$query="UPDATE active_index SET duration = ? , hit = ? WHERE id = ? ";
+			$sth = $dbh->prepare($query);
+			$sth->execute( array(($duration+$this_active_time), ($hit+1),$id));
+			if (!$sth || ($sth && $sth->errorCode() != 0)) {
+				$error = $dbh->errorInfo();
+			}			
+		}
+		else{
+			#新建
+			$query="INSERT INTO active_index ( user_id, date , duration , hit )  VALUES  ( ? , ? , ? , ?  ) ";
+			$sth = $dbh->prepare($query);
+			#最小思考时间
+			$sth->execute(array($_COOKIE["userid"] , $client_date ,  MIN_INTERVAL,1 ) );
+			if (!$sth || ($sth && $sth->errorCode() != 0)) {
+				$error = $dbh->errorInfo();
+			}
+		}
+		#更新经验总量表结束
+
 	}
 }