Count.class.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace Cas\Module;
  3. use KIF\Data\Convert;
  4. use KIF\Data\ResultWrapper;
  5. use KIF\Db\Transaction;
  6. use KIF\Cache\Memcached;
  7. use Cas\Dao\LotteryEventsLog;
  8. use Cas\Dao\LotteryUserParticipateLog;
  9. use KIF\Url;
  10. use KIF\Cookie;
  11. use Cas\Dao\LotteryDeliveryChannels;
  12. use KIF\Core\Request;
  13. use KIF\Core\Config;
  14. /**
  15. * 计数类
  16. *
  17. * @author lihuanchun
  18. *
  19. */
  20. class Count {
  21. private $keyName;
  22. private $objDLotteryDeliveryChannels;
  23. /**
  24. * 初始化
  25. */
  26. public function __construct() {
  27. $this->keyName = 'FromKey';
  28. $this->objDLotteryDeliveryChannels = new LotteryDeliveryChannels ();
  29. }
  30. /**
  31. * 用文件记录用户访问活动日志 & 来源
  32. * 后期用脚本跑出当天结果 PV UV
  33. *
  34. * @param int $event_id
  35. * @param int $platform_id
  36. */
  37. public function setPassLog( $event_id,$platform_id) {
  38. $objDLotteryDeliveryChannels = new LotteryDeliveryChannels();
  39. $from_id = Request::g('from');
  40. if($from_id){
  41. Cookie::set('cas_from', $from_id,60*60*24*29,'.'.$_SERVER['HTTP_HOST']);
  42. }else{
  43. $from_id = Cookie::get('cas_from');
  44. }
  45. if($from_id){
  46. $data = $objDLotteryDeliveryChannels -> get($from_id);
  47. if($data['events_id'] == $event_id ){
  48. //$objDLotteryDeliveryChannels -> addOnePV($from_id);
  49. }
  50. $objDLotteryDeliveryChannels -> addOnePV($from_id);
  51. }else{
  52. $objDLotteryDeliveryChannels ->addDefaultEventOnePV(Request::schemeDomain(),$event_id,$platform_id);
  53. }
  54. $info = array();
  55. $info['ip'] = Request::ip();
  56. $info['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
  57. $info['event_id'] = $event_id;
  58. $info['time'] = time();
  59. $logStr = serialize($info);
  60. $this->writePassLog($logStr,'pass_log');
  61. }
  62. /**
  63. * 渠道计数 操作
  64. * PV IP 记录
  65. * 所有页面请求此方法
  66. */
  67. public function DeliveryChannelsCount() {
  68. // 1.判断当前url中是否有from参数
  69. $id = Request::g ( 'from' );
  70. if (! $id) {
  71. // 2. 判断是否有渠道Cookie
  72. $id = Cookie::get ( $this->keyNam );
  73. }
  74. if ($id) {
  75. // 3. 更具渠道ID增加PV
  76. $this->objDLotteryDeliveryChannels->addOnePV ( $id );
  77. Cookie::set ( $this->keyName, $id, 60 * 60 * 24 * 10 ,'.'.$_SERVER['HTTP_HOST']);
  78. }
  79. }
  80. /**
  81. * 写日志
  82. *
  83. * @param string $log_message
  84. * 日志信息
  85. * @param string $log_type
  86. * 日志类型
  87. */
  88. public static function writePassLog($log_message, $log_type = 'log') {
  89. $log_size = 1024 * 1024 * 5; // 1024 * 1024 * 5 = 5MB
  90. $dir =Config::getInstance ()->get ( 'Log_Path' ).DIRECTORY_SEPARATOR.date('Y',time()).DIRECTORY_SEPARATOR;
  91. if (! is_dir ( $dir )) {
  92. if (! mkdir ( $dir )) {
  93. return false;
  94. }
  95. }
  96. if (! is_writable ( $dir )){
  97. exit ( 'LOG_PATH is not writeable !' );
  98. }
  99. $dir .= date('m',time()).DIRECTORY_SEPARATOR;
  100. if (! is_dir ( $dir )) {
  101. if (! mkdir ( $dir )) {
  102. return false;
  103. }
  104. }
  105. if (! is_writable ( $dir )){
  106. exit ( 'LOG_PATH is not writeable !' );
  107. }
  108. $s_now_time = date ( '[Y-m-d H:i:s]' );
  109. $log_now_day = date ( 'Y_m_d' );
  110. // 根据类型设置日志目标位置
  111. $log_path = $dir;
  112. switch ($log_type) {
  113. case 'debug' :
  114. $log_path .= 'Out_' . $log_now_day . '.log';
  115. break;
  116. case 'error' :
  117. $log_path .= 'Err_' . $log_now_day . '.log';
  118. break;
  119. case 'pass_log' :
  120. $log_path .= 'Pass_log_' . $log_now_day . '.log';
  121. break;
  122. default :
  123. $log_path .= 'Log_' . $log_now_day . '.log';
  124. break;
  125. }
  126. // 检测日志文件大小, 超过配置大小则重命名
  127. if (file_exists ( $log_path ) && $log_size <= filesize ( $log_path )) {
  128. $s_file_name = substr ( basename ( $log_path ), 0, strrpos ( basename ( $log_path ), '.log' ) ) . '_' . time () . '.log';
  129. rename ( $log_path, dirname ( $log_path ) . DS . $s_file_name );
  130. }
  131. // 写日志, 返回成功与否
  132. return error_log ( "$log_message\n", 3, $log_path );
  133. }
  134. }