active.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. //统计用户经验值
  3. require_once '../config.php';
  4. require_once "../public/function.php";
  5. require_once "../public/php/define.php";
  6. function add_edit_event($type = 0, $data = null)
  7. {
  8. $active_type[10] = "channel_update";
  9. $active_type[11] = "channel_create";
  10. $active_type[20] = "article_update";
  11. $active_type[21] = "article_create";
  12. $active_type[30] = "dict_lookup";
  13. $active_type[40] = "term_update";
  14. $active_type[42] = "term_create";
  15. $active_type[41] = "term_lookup";
  16. $active_type[60] = "wbw_update";
  17. $active_type[61] = "wbw_create";
  18. $active_type[70] = "sent_update";
  19. $active_type[71] = "sent_create";
  20. $active_type[80] = "collection_update";
  21. $active_type[81] = "collection_create";
  22. $active_type[90] = "nissaya_open";
  23. date_default_timezone_set("UTC");
  24. define("MAX_INTERVAL", 600000);
  25. define("MIN_INTERVAL", 60000);
  26. if (isset($_COOKIE["user_id"])) {
  27. $dns = _FILE_DB_USER_ACTIVE_;
  28. $dbh = new PDO($dns, _DB_USERNAME_, _DB_PASSWORD_, array(PDO::ATTR_PERSISTENT => true));
  29. $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  30. // 查询上次编辑活跃结束时间
  31. $query = "SELECT id, op_start,op_end, hit FROM "._TABLE_USER_OPERATION_FRAME_." WHERE user_id = ? order by op_end DESC";
  32. $stmt = $dbh->prepare($query);
  33. $stmt->execute(array($_COOKIE["user_id"]));
  34. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  35. $new_record = false;
  36. $currTime = mTime();
  37. if ($row) {
  38. //找到,判断是否超时,超时新建,未超时修改
  39. $id = (int) $row["id"];
  40. $start_time = (int) $row["op_start"];
  41. $endtime = (int) $row["op_end"];
  42. $hit = (int) $row["hit"];
  43. if ($currTime - $endtime > MAX_INTERVAL) {
  44. //超时新建
  45. $new_record = true;
  46. } else {
  47. //未超时修改
  48. $new_record = false;
  49. }
  50. } else {
  51. //没找到,新建
  52. $new_record = true;
  53. }
  54. #获取客户端时区偏移 beijing = +8
  55. if (isset($_COOKIE["timezone"])) {
  56. $client_timezone = (0 - (int) $_COOKIE["timezone"]) * 60 * 1000;
  57. } else {
  58. $client_timezone = 0;
  59. }
  60. $this_active_time = 0; //时间增量
  61. if ($new_record) {
  62. #新建
  63. $query = "INSERT INTO "._TABLE_USER_OPERATION_FRAME_." ( user_id, op_start , op_end , duration , hit , timezone,created_at ) VALUES ( ? , ? , ? , ? , ? ,?,to_timestamp(?)) ";
  64. $sth = $dbh->prepare($query);
  65. #最小思考时间
  66. $sth->execute(array($_COOKIE["user_id"], ($currTime - MIN_INTERVAL), $currTime, MIN_INTERVAL, 1, $client_timezone,($currTime - MIN_INTERVAL)/1000));
  67. if (!$sth || ($sth && $sth->errorCode() != 0)) {
  68. $error = $dbh->errorInfo();
  69. }
  70. $this_active_time = MIN_INTERVAL;
  71. } else {
  72. #修改
  73. $this_active_time = $currTime - $endtime;
  74. $query = "UPDATE "._TABLE_USER_OPERATION_FRAME_." SET op_end = ? , duration = ? , hit = ? , updated_at=now() WHERE id = ? ";
  75. $sth = $dbh->prepare($query);
  76. $sth->execute(array($currTime, ($currTime - $start_time), ($hit + 1), $id));
  77. if (!$sth || ($sth && $sth->errorCode() != 0)) {
  78. $error = $dbh->errorInfo();
  79. }
  80. }
  81. #更新经验总量表
  82. #计算客户端日期 unix时间戳 以毫秒计
  83. $client_currtime = $currTime + $client_timezone;
  84. $client_date = strtotime(gmdate("Y-m-d", $client_currtime / 1000)) * 1000;
  85. #查询是否存在
  86. $query = "SELECT id,duration,hit FROM "._TABLE_USER_OPERATION_DAILY_." WHERE user_id = ? and date_int = ?";
  87. $sth = $dbh->prepare($query);
  88. $sth->execute(array($_COOKIE["user_id"], $client_date));
  89. $row = $sth->fetch(PDO::FETCH_ASSOC);
  90. if ($row) {
  91. #更新
  92. $id = (int) $row["id"];
  93. $duration = (int) $row["duration"];
  94. $hit = (int) $row["hit"];
  95. #修改
  96. $query = "UPDATE "._TABLE_USER_OPERATION_DAILY_." SET duration = ? , hit = ? , updated_at = now() WHERE id = ? ";
  97. $sth = $dbh->prepare($query);
  98. $sth->execute(array(($duration + $this_active_time), ($hit + 1), $id));
  99. if (!$sth || ($sth && $sth->errorCode() != 0)) {
  100. $error = $dbh->errorInfo();
  101. }
  102. } else {
  103. #新建
  104. $query = "INSERT INTO "._TABLE_USER_OPERATION_DAILY_." ( user_id, date_int , date_at, duration , hit ) VALUES ( ? , ? ,to_timestamp(?), ? , ? ) ";
  105. $sth = $dbh->prepare($query);
  106. #最小思考时间
  107. $sth->execute(array($_COOKIE["user_id"], $client_date, $client_date/1000, MIN_INTERVAL, 1));
  108. if (!$sth || ($sth && $sth->errorCode() != 0)) {
  109. $error = $dbh->errorInfo();
  110. }
  111. }
  112. #更新经验总量表结束
  113. #更新log
  114. if ($type > 0) {
  115. $dns = _FILE_DB_USER_ACTIVE_LOG_;
  116. $dbh_log = new PDO($dns, _DB_USERNAME_, _DB_PASSWORD_, array(PDO::ATTR_PERSISTENT => true));
  117. $dbh_log->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  118. $query = "INSERT INTO "._TABLE_USER_OPERATION_LOG_." ( user_id, op_type_id ,op_type , content , create_time , timezone ) VALUES ( ? , ? , ? , ? , ? ,? ) ";
  119. $sth = $dbh_log->prepare($query);
  120. $sth->execute(array($_COOKIE["user_id"], $type,$active_type[$type], $data, $currTime, $client_timezone));
  121. if (!$sth || ($sth && $sth->errorCode() != 0)) {
  122. $error = $dbh->errorInfo();
  123. }
  124. }
  125. #更新log结束
  126. }
  127. }