behavior.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2018/9/1
  6. * Time: 下午11:29
  7. */
  8. namespace statistics;
  9. use brand_helper;
  10. use category_helper;
  11. class behavior
  12. {
  13. private $mod_behavior;
  14. private $method_dicts;
  15. public function __construct()
  16. {
  17. $this->mod_behavior = Model('behavior');
  18. $this->method_dicts = [];
  19. $this->init_methods();
  20. }
  21. public function analyse($time)
  22. {
  23. $path_file = BASE_DATA_PATH . '/log/' . date('Ymd', $time) . '_path.log';
  24. $fd = @fopen($path_file, "r");
  25. if($fd == false) return false;
  26. while (!feof($fd))
  27. {
  28. $line = fgets($fd,1024);
  29. if ($line !== false && !empty($line)) {
  30. $this->record($line);
  31. }
  32. }
  33. fclose($fd);
  34. return true;
  35. }
  36. public function analyse_loop($time)
  37. {
  38. $path_file = BASE_DATA_PATH . '/log/' . date('Ymd', $time) . '_path.log';
  39. $fd = @fopen($path_file, "r");
  40. while (!feof($fd))
  41. {
  42. $line = fgets($fd,1024);
  43. if ($line !== false && !empty($line)) {
  44. $this->record($line);
  45. }
  46. }
  47. fclose($fd);
  48. }
  49. private function record($line)
  50. {
  51. $input = json_decode($line,true);
  52. if($input == false) return;
  53. $content = $input['content'];
  54. unset($input['content']);
  55. $params = $this->parse_act($content);
  56. $item = array_merge($input,$params);
  57. $this->mod_behavior->addBehavior($item);
  58. }
  59. private function parse_act($content)
  60. {
  61. $result = [];
  62. $params = $this->parse_content($content);
  63. if(empty($params)) return $result;
  64. $act = $params['act'];
  65. $op = $params['op'];
  66. $result['act'] = $act;
  67. $result['op'] = $op;
  68. $type_data = $this->type_data($act,$op,$params);
  69. foreach ($type_data as $key => $val)
  70. {
  71. if($key == 'type') {
  72. $result['ktype'] = $val;
  73. }
  74. if($key == 'data') {
  75. $result['kdata'] = $val;
  76. }
  77. }
  78. $from = $params['from'];
  79. if(!empty($from))
  80. {
  81. $from = base64_decode($from);
  82. $fparams = $this->parse_content($from);
  83. $fact = $fparams['act'];
  84. $fop = $fparams['op'];
  85. $result['from_act'] = $fact;
  86. $result['from_op'] = $fop;
  87. $ftype_data = $this->type_data($fact,$fop,$fparams);
  88. foreach ($ftype_data as $key => $val)
  89. {
  90. if($key == 'type') {
  91. $result['from_ktype'] = $val;
  92. }
  93. if($key == 'data') {
  94. $result['from_kdata'] = $val;
  95. }
  96. }
  97. }
  98. return $result;
  99. }
  100. private function init_methods()
  101. {
  102. $this->method_dicts['login'] = function ($op, $params)
  103. {
  104. if ($op == 'getcodex' || $op == 'getcode') {
  105. $val = $params['mobile'];
  106. return ['type' => 'mobile', 'data' => $val];
  107. } else {
  108. return [];
  109. }
  110. };
  111. $this->method_dicts['special'] = function ($op, $params) {
  112. $val = intval($params['special_id']);
  113. return ['type' => 'special','data' => $val];
  114. };
  115. $this->method_dicts['goods_common'] = function ($op, $params)
  116. {
  117. if($op == 'index' || $op == 'detail' || $op == 'detail_ajax') {
  118. $val = intval($params['goods_id']);
  119. return ['type' => 'goods','data' => $val];
  120. }
  121. elseif($op == 'comments') {
  122. $val = intval($params['goods_commonid']);
  123. return ['type' => 'goods_common','data' => $val];
  124. } else {
  125. return [];
  126. }
  127. };
  128. $this->method_dicts['search'] = function ($op, $params)
  129. {
  130. if($op == 'index' || $op == 'search_goods') {
  131. $keyword = urldecode($params['keyword']);
  132. $brandid = intval($params['brand_id']);
  133. $hotid = intval($params['hot_id']);
  134. $brand = brand_helper::instance()->name($brandid);
  135. $hot = category_helper::instance()->name($hotid);
  136. return ['type' => 'keyword','data' => "{$keyword}#{$brand}#{$hot}"];
  137. }
  138. elseif($op == 'suggest_words') {
  139. $keyword = $params['keyword'];
  140. return ['type' => 'keyword','data' => "{$keyword}"];
  141. }
  142. else {
  143. return [];
  144. }
  145. };
  146. $this->method_dicts['cart'] = function ($op, $params)
  147. {
  148. if($op == 'addex') {
  149. $bl_id = intval($params['bl_id']);
  150. $goods_id = intval($params['goods_id']);
  151. if($bl_id > 0) {
  152. return ['type' => 'bl','data' => $bl_id];
  153. } else {
  154. return ['type' => 'goods','data' => $goods_id];
  155. }
  156. }
  157. elseif($op == 'edit') {
  158. $cart_id = intval($params['cart_id']);
  159. return ['type' => 'cart','data' => $cart_id];
  160. }
  161. else {
  162. return [];
  163. }
  164. };
  165. $this->method_dicts['member_buy'] = function ($op, $params) {
  166. if($op == 'step_first') {
  167. $cart_ids = urldecode($_POST['cart_id']);
  168. return ['type' => 'cart_ids','data' => $cart_ids];
  169. }
  170. elseif($op == 'step_second') {
  171. $payment = $params['payment'];
  172. return ['type' => 'payment','data' => $payment];
  173. }
  174. else {
  175. return [];
  176. }
  177. };
  178. $this->method_dicts['member_bonus'] = function ($op, $params) {
  179. return [];
  180. };
  181. }
  182. private function type_data($act,$op,$params)
  183. {
  184. if(empty($act) || empty($op)) return [];
  185. if(!array_key_exists($act,$this->method_dicts)) return [];
  186. return $this->method_dicts[$act]($op,$params);
  187. }
  188. private function parse_content($content)
  189. {
  190. $result = [];
  191. $params = preg_split('/&|=/', $content);
  192. for ($i = 0; $i < count($params); ++$i)
  193. {
  194. $key = $params[$i];
  195. $val = $params[++$i];
  196. $result[$key] = urldecode($val);
  197. }
  198. return $result;
  199. }
  200. }