Count.class.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. }else{
  51. $objDLotteryDeliveryChannels ->addDefaultEventOnePV(Request::schemeDomain(),$event_id,$platform_id);
  52. }
  53. $info = array();
  54. $info['ip'] = Request::ip();
  55. $info['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
  56. $info['event_id'] = $event_id;
  57. $info['time'] = time();
  58. $logStr = serialize($info);
  59. $this->writePassLog($logStr,'pass_log');
  60. }
  61. /**
  62. * 渠道计数 操作
  63. * PV IP 记录
  64. * 所有页面请求此方法
  65. */
  66. public function DeliveryChannelsCount() {
  67. // 1.判断当前url中是否有from参数
  68. $id = Request::g ( 'from' );
  69. if (! $id) {
  70. // 2. 判断是否有渠道Cookie
  71. $id = Cookie::get ( $this->keyNam );
  72. }
  73. if ($id) {
  74. // 3. 更具渠道ID增加PV
  75. $this->objDLotteryDeliveryChannels->addOnePV ( $id );
  76. Cookie::set ( $this->keyName, $id, 60 * 60 * 24 * 10 ,'.'.$_SERVER['HTTP_HOST']);
  77. }
  78. }
  79. /**
  80. * 写日志
  81. *
  82. * @param string $log_message
  83. * 日志信息
  84. * @param string $log_type
  85. * 日志类型
  86. */
  87. public static function writePassLog($log_message, $log_type = 'log') {
  88. $log_size = 1024 * 1024 * 5; // 1024 * 1024 * 5 = 5MB
  89. $dir =Config::getInstance ()->get ( 'Log_Path' ).DIRECTORY_SEPARATOR.date('Y',time()).DIRECTORY_SEPARATOR;
  90. if (! is_dir ( $dir )) {
  91. if (! mkdir ( $dir )) {
  92. return false;
  93. }
  94. }
  95. if (! is_writable ( $dir )){
  96. exit ( 'LOG_PATH is not writeable !' );
  97. }
  98. $dir .= date('m',time()).DIRECTORY_SEPARATOR;
  99. if (! is_dir ( $dir )) {
  100. if (! mkdir ( $dir )) {
  101. return false;
  102. }
  103. }
  104. if (! is_writable ( $dir )){
  105. exit ( 'LOG_PATH is not writeable !' );
  106. }
  107. $s_now_time = date ( '[Y-m-d H:i:s]' );
  108. $log_now_day = date ( 'Y_m_d' );
  109. // 根据类型设置日志目标位置
  110. $log_path = $dir;
  111. switch ($log_type) {
  112. case 'debug' :
  113. $log_path .= 'Out_' . $log_now_day . '.log';
  114. break;
  115. case 'error' :
  116. $log_path .= 'Err_' . $log_now_day . '.log';
  117. break;
  118. case 'pass_log' :
  119. $log_path .= 'Pass_log_' . $log_now_day . '.log';
  120. break;
  121. default :
  122. $log_path .= 'Log_' . $log_now_day . '.log';
  123. break;
  124. }
  125. // 检测日志文件大小, 超过配置大小则重命名
  126. if (file_exists ( $log_path ) && $log_size <= filesize ( $log_path )) {
  127. $s_file_name = substr ( basename ( $log_path ), 0, strrpos ( basename ( $log_path ), '.log' ) ) . '_' . time () . '.log';
  128. rename ( $log_path, dirname ( $log_path ) . DS . $s_file_name );
  129. }
  130. // 写日志, 返回成功与否
  131. return error_log ( "$log_message\n", 3, $log_path );
  132. }
  133. }