active.php 5.8 KB

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