123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 2018/9/1
- * Time: 下午11:29
- */
- namespace statistics;
- use brand_helper;
- use category_helper;
- class behavior
- {
- private $mod_behavior;
- private $method_dicts;
- public function __construct()
- {
- $this->mod_behavior = Model('behavior');
- $this->method_dicts = [];
- $this->init_methods();
- }
- public function analyse($time)
- {
- $path_file = BASE_DATA_PATH . '/log/' . date('Ymd', $time) . '_path.log';
- $fd = @fopen($path_file, "r");
- if($fd == false) return false;
- while (!feof($fd))
- {
- $line = fgets($fd,1024);
- if ($line !== false && !empty($line)) {
- $this->record($line);
- }
- }
- fclose($fd);
- return true;
- }
- public function analyse_loop($time)
- {
- $path_file = BASE_DATA_PATH . '/log/' . date('Ymd', $time) . '_path.log';
- $fd = @fopen($path_file, "r");
- while (!feof($fd))
- {
- $line = fgets($fd,1024);
- if ($line !== false && !empty($line)) {
- $this->record($line);
- }
- }
- fclose($fd);
- }
- private function record($line)
- {
- $input = json_decode($line,true);
- if($input == false) return;
- $content = $input['content'];
- unset($input['content']);
- $params = $this->parse_act($content);
- $item = array_merge($input,$params);
- $this->mod_behavior->addBehavior($item);
- }
- private function parse_act($content)
- {
- $result = [];
- $params = $this->parse_content($content);
- if(empty($params)) return $result;
- $act = $params['act'];
- $op = $params['op'];
- $result['act'] = $act;
- $result['op'] = $op;
- $type_data = $this->type_data($act,$op,$params);
- foreach ($type_data as $key => $val)
- {
- if($key == 'type') {
- $result['ktype'] = $val;
- }
- if($key == 'data') {
- $result['kdata'] = $val;
- }
- }
- $from = $params['from'];
- if(!empty($from))
- {
- $from = base64_decode($from);
- $fparams = $this->parse_content($from);
- $fact = $fparams['act'];
- $fop = $fparams['op'];
- $result['from_act'] = $fact;
- $result['from_op'] = $fop;
- $ftype_data = $this->type_data($fact,$fop,$fparams);
- foreach ($ftype_data as $key => $val)
- {
- if($key == 'type') {
- $result['from_ktype'] = $val;
- }
- if($key == 'data') {
- $result['from_kdata'] = $val;
- }
- }
- }
- return $result;
- }
- private function init_methods()
- {
- $this->method_dicts['login'] = function ($op, $params)
- {
- if ($op == 'getcodex' || $op == 'getcode') {
- $val = $params['mobile'];
- return ['type' => 'mobile', 'data' => $val];
- } else {
- return [];
- }
- };
- $this->method_dicts['special'] = function ($op, $params) {
- $val = intval($params['special_id']);
- return ['type' => 'special','data' => $val];
- };
- $this->method_dicts['goods_common'] = function ($op, $params)
- {
- if($op == 'index' || $op == 'detail' || $op == 'detail_ajax') {
- $val = intval($params['goods_id']);
- return ['type' => 'goods','data' => $val];
- }
- elseif($op == 'comments') {
- $val = intval($params['goods_commonid']);
- return ['type' => 'goods_common','data' => $val];
- } else {
- return [];
- }
- };
- $this->method_dicts['search'] = function ($op, $params)
- {
- if($op == 'index' || $op == 'search_goods') {
- $keyword = urldecode($params['keyword']);
- $brandid = intval($params['brand_id']);
- $hotid = intval($params['hot_id']);
- $brand = brand_helper::instance()->name($brandid);
- $hot = category_helper::instance()->name($hotid);
- return ['type' => 'keyword','data' => "{$keyword}#{$brand}#{$hot}"];
- }
- elseif($op == 'suggest_words') {
- $keyword = $params['keyword'];
- return ['type' => 'keyword','data' => "{$keyword}"];
- }
- else {
- return [];
- }
- };
- $this->method_dicts['cart'] = function ($op, $params)
- {
- if($op == 'addex') {
- $bl_id = intval($params['bl_id']);
- $goods_id = intval($params['goods_id']);
- if($bl_id > 0) {
- return ['type' => 'bl','data' => $bl_id];
- } else {
- return ['type' => 'goods','data' => $goods_id];
- }
- }
- elseif($op == 'edit') {
- $cart_id = intval($params['cart_id']);
- return ['type' => 'cart','data' => $cart_id];
- }
- else {
- return [];
- }
- };
- $this->method_dicts['member_buy'] = function ($op, $params) {
- if($op == 'step_first') {
- $cart_ids = urldecode($_POST['cart_id']);
- return ['type' => 'cart_ids','data' => $cart_ids];
- }
- elseif($op == 'step_second') {
- $payment = $params['payment'];
- return ['type' => 'payment','data' => $payment];
- }
- else {
- return [];
- }
- };
- $this->method_dicts['member_bonus'] = function ($op, $params) {
- return [];
- };
- }
- private function type_data($act,$op,$params)
- {
- if(empty($act) || empty($op)) return [];
- if(!array_key_exists($act,$this->method_dicts)) return [];
- return $this->method_dicts[$act]($op,$params);
- }
- private function parse_content($content)
- {
- $result = [];
- $params = preg_split('/&|=/', $content);
- for ($i = 0; $i < count($params); ++$i)
- {
- $key = $params[$i];
- $val = $params[++$i];
- $result[$key] = urldecode($val);
- }
- return $result;
- }
- }
|