فهرست منبع

增加统计业务到消息队列中

stanley-king 8 سال پیش
والد
کامیت
89265639e7
8فایلهای تغییر یافته به همراه91 افزوده شده و 563 حذف شده
  1. 1 2
      crontab/control/queue.php
  2. 14 8
      data/logic/queue.logic.php
  3. 76 10
      helper/statistics_helper.php
  4. 0 33
      research/db.php
  5. 0 201
      research/function_test.php
  6. 0 252
      research/research_run.php
  7. 0 16
      research/sms_test.php
  8. 0 41
      research/wxpay.php

+ 1 - 2
crontab/control/queue.php

@@ -30,7 +30,7 @@ class queueControl extends BaseCronControl
             if ($this->_stop) {
                 exit;
             }
-            $content = $worker->pop($queues,1);
+            $content = $worker->pop($queues,60);
 
             if (is_array($content)) {
                 $method = key($content);
@@ -40,7 +40,6 @@ class queueControl extends BaseCronControl
                 if (!$result['state']) {
                     $this->log($result['msg'],false);
                 }
-
             } else {
                 $model = Model();
                 $model->checkActive();

+ 14 - 8
data/logic/queue.logic.php

@@ -13,10 +13,10 @@ defined('InShopNC') or exit('Access Invalid!');
 require_once(BASE_ROOT_PATH . '/helper/push_app.php');
 require_once(BASE_ROOT_PATH . '/helper/sms_helper.php');
 require_once(BASE_ROOT_PATH . '/helper/kdn_helper.php');
+require_once(BASE_ROOT_PATH . '/helper/statistics_helper.php');
 
 class queueLogic
 {
-
     /**
      * push app内容跳转类型
      * @var array
@@ -397,7 +397,7 @@ class queueLogic
     {
         if (empty($param) || empty($param['member_id']) || empty($param['text'])) {
             Log::record("push info: 数据有误! param:" . json_encode($param) . "\t session:" . json_encode($_SESSION));
-            return false;
+            return callback(false);
         }
 
         if (!isset($param['go_type'])) {
@@ -406,7 +406,7 @@ class queueLogic
 
         if (!in_array($param['go_type'], $this->go_type)) {
             Log::record("push info: go_type参数有误! param:" . json_encode($param) . "\t session:" . json_encode($_SESSION));
-            return false;
+            return callback(false);
         }
 
         $push = new push_app();
@@ -425,7 +425,7 @@ class queueLogic
             Log::record("push info: ios fall error\treturn:" . $e->getMessage() . "\t param:" . json_encode($param),Log::ERR);
         }
 
-        return true;
+        return callback(true);
     }
 
     /**
@@ -438,7 +438,7 @@ class queueLogic
     {
         if (empty($param) || empty($param['mobile']) || empty($param['type'])) {
             Log::record("sms info: 数据有误! param:" . json_encode($param),Log::ERR);
-            return false;
+            return callback(false);
         }
 
         try {
@@ -449,7 +449,7 @@ class queueLogic
             Log::record("sms info: fall error\treturn:" . $e->getMessage() . "\t param:" . json_encode($param),Log::ERR);
         }
 
-        return true;
+        return callback(true);
     }
 
     /**
@@ -462,7 +462,7 @@ class queueLogic
     {
         if (empty($param) || empty($param['order_sn'])) {
             Log::record("kdn_helper: 数据有误! param:" . json_encode($param));
-            return false;
+            return callback(false);
         }
 
         try {
@@ -471,7 +471,7 @@ class queueLogic
             Log::record("kdn_helper: fall error\treturn:" . $e->getMessage() . "\t param:" . json_encode($param));
         }
 
-        return true;
+        return callback(true);
     }
 
     /**
@@ -491,4 +491,10 @@ class queueLogic
 
         return true;
     }
+
+    public function savelog($param)
+    {
+        statistics_helper::instance()->add_logs($param);
+        return callback(true);
+    }
 }

+ 76 - 10
helper/statistics_helper.php

@@ -12,8 +12,11 @@ class statistics_helper
     private static $stInstance;
     private $mItems;
     private $mOther;
-
     private $mRecordTime;
+    private $mSaveTime;
+
+    const interval_time = 1800;
+
 
     public static function instance()
     {
@@ -27,26 +30,80 @@ class statistics_helper
     {
         $this->mItems = [];
         $this->mOther = [];
-        $this->mRecordTime = time();
+        $this->reset_time();
     }
+
     public function __destroy()
     {
         $this->save();
     }
 
+    public function add_logs($params)
+    {
+        $this->add_items($params);
+        $this->add_others($params);
+
+        if($this->expired()) {
+            $this->save();
+        }
+    }
+
+    private function add_others($params)
+    {
+        $others = $params['other'];
+        foreach ($others as $key => $count) {
+            $this->add_other($key,$count);
+        }
+    }
+
+    private function add_items($params)
+    {
+        $acts = $params['function'];
+        foreach ($acts as $act => $ops)
+        {
+            if($this->act($act) == false) continue;
+
+            foreach ($ops as $opx => $data)
+            {
+                if($this->op($act,$opx) == false) continue;
+                $oper = &$this->mItems[$act][$opx];
+
+                foreach ($data as $key => $value)
+                {
+                    if($key = 'count')
+                    {
+                        if(empty($oper['count'])) {
+                            $oper['count'] = 0;
+                        }
+
+                        $oper['count'] += intval($value);
+                    }
+                }
+            }
+        }
+    }
+
     private function expired()
     {
+        return (time() >= $this->mSaveTime);
+    }
+
+    private function reset_time()
+    {
+        $this->mRecordTime = time();
         $day = new DateTime();
         $day->setTimestamp($this->mRecordTime);
         $day->setTime(0,0,0);
         $morning = $day->getTimestamp();
 
-        $time_stamp = $morning + 12 * 60 * 60;
-        if(time() > $time_stamp) {
-            $time_stamp = $day->getTimestamp() + 24 * 60 * 60;
-        }
+        $noon = $morning + 12 * 60 * 60;
+        $midnight = $day->getTimestamp() + 24 * 60 * 60;
 
-        return (time() >= $time_stamp);
+        if(time() < $noon) {
+            $this->mSaveTime = $noon;
+        } else {
+            $this->mSaveTime = $midnight;
+        }
     }
 
     private function save()
@@ -58,6 +115,15 @@ class statistics_helper
         $data = json_encode($data);
         file_put_contents($file,$data);
 
+        $this->mOther = [];
+        $this->mItems = [];
+        $this->reset_time();
+    }
+
+    public function send_queue()
+    {
+        $data = array('star_time'=> $this->mRecordTime,'end_time' => time(),'function' => $this->mItems,'other' => $this->mOther);
+        QueueClient::push("savelog",$data);
         $this->mRecordTime = time();
         $this->mOther = [];
         $this->mItems = [];
@@ -65,8 +131,8 @@ class statistics_helper
 
     public function add_call($param)
     {
-        if($this->expired()) {
-            $this->save();
+        if(time() >= $this->mRecordTime + self::interval_time) {
+            $this->send_queue();
         }
 
         $act = $param['act'];
@@ -158,7 +224,7 @@ class statistics_helper
         if(empty($op)) return false;
 
         if(array_key_exists($op,$this->mItems[$act]) == false) {
-            $act[$op] = [];
+            $this->mItems[$act][$op] = [];
         }
         return true;
     }

+ 0 - 33
research/db.php

@@ -1,33 +0,0 @@
-<?php
-
-require_once (BASE_CORE_PATH . '/framework/db/mysqli.php');
-require_once (BASE_DATA_PATH . '/model/member.model.php');
-
-
-
-//Db::connect('master');
-
-$cur = new DateTime();
-$start = $cur->getTimestamp();
-
-echo $start; 
-echo "   ";
-for($i = 100; $i > 0; --$i) {
-    $openid = uniqid();
-    $model_member = Model('member');   
-        
-    $info = $model_member->getMemberInfo(array('member_id' => $openid),'member_mobile');
-
-    if(!empty($info) && count($info) > 0) {
-        echo ($info[0]['member_mobile'] . '\r\n');
-    }
-}
-
-//$data = $model->table('goods')->find(38);
-
-$e = new DateTime();
-$end = $e->getTimestamp();
-
-echo $end;
-
-//echo $end - $start;

تفاوت فایلی نمایش داده نمی شود زیرا این فایل بسیار بزرگ است
+ 0 - 201
research/function_test.php


+ 0 - 252
research/research_run.php

@@ -1,252 +0,0 @@
-<?php
-
-
-$url = 'http://192.168.1.110/mobile/index.php?act=bonusex&op=open&client_type=wap&type_sn=150518546433385490';
-$x = serialize($url);
-$y = unserialize($x);
-
-$cur_time = date("Y-m-d H:i:s");
-
-$tm = strtotime('2016-06-04 10:00:00');
-
-
-function parase_wxinfo($wxinfo)
-{
-    $user = urldecode($wxinfo);
-    $user = json_decode($user,true);
-
-    $info = array();
-    $info['member_nickname'] = $user['nickname'];
-    $info['member_wxopenid'] = $user['openid'];
-    $info['member_avatar'] = $user['headimgurl'];
-    $info['member_sex'] = intval($user['sex']);
-
-    return $info;
-}
-
-$str = <<< EOT
-{%22city%22:%22West%22,%22country%22:%22CN%22,%22headimgurl%22:%22http://wx.qlogo.cn/mmopen/PiajxSqBRaEK3HJTr0VPI0NwVZ6FpsNiaqZPpYGpmRoN1Amn2Y12ansyKkNZ3SwMtUokFfrcic0Vcu5rxNBRVVyjA/0%22,%22nickname%22:%22%E6%B1%9F%E6%B5%B7%E8%8B%97@%E4%B8%BD%E4%BA%BA%E4%B8%BD%E5%A6%86%22,%22openid%22:%22o-E2Rw6Ph4tFPCmbICFumslzUzc8%22,%22privilege%22:[],%22province%22:%22Beijing%22,%22sex%22:1,%22unionid%22:%22ogAyDv5P8mRT_dcd7N9FsBYLUrFQ%22,%22additionalProperties%22:{},%22code%22:0}
-EOT;
-
-$str = urldecode($str);
-
-$x = $str;
-$user = parase_wxinfo($str);
-
-
-
-//include BASE_ROOT_PATH . "/research/db.php";
-//include BASE_ROOT_PATH . "/research/function_test.php";
-//include BASE_ROOT_PATH . "/research/sms_test.php";
-//include BASE_ROOT_PATH . "/research/wxpay.php";
-
-//define('BASE_CORE_PATH',BASE_ROOT_PATH.'/core');
-//define('BASE_DATA_PATH',BASE_ROOT_PATH.'/data');
-//define('BASE_MOBILE_PATH',BASE_ROOT_PATH.'/mobile');
-//define('BASE_CRONTAB_PATH',BASE_ROOT_PATH.'/crontab');
-//define('BASE_UPLOAD_PATH',BASE_DATA_PATH.'/upload');
-//define('BASE_UTIL_PATH',BASE_ROOT_PATH.'/util');
-//
-//require_once (BASE_ROOT_PATH . '/helper/text_filter.php');
-//require_once (BASE_ROOT_PATH . '/helper/category_helper.php');
-//
-//
-//define('InShopNC',true);
-//
-//
-//$str = <<< EOT
-//<script type="text/javascript">
-//    function app(){
-//        var app=navigator.userAgent.toLowerCase(),//判断设备
-//            app_nav=navigator.appVersion.toLowerCase(),
-//            href='';
-//        if(app_nav.indexOf('iphone')>-1&&app.indexOf('iphone')){
-//            href='https://www.pgyer.com/pfcB';
-//        }else if(app.indexOf('micromessenger')>-1&&app_nav.indexOf('android')>-1){
-//            href="javascript:void(0)";
-//        }
-//        else {
-//            href='http://a.lrlz.com/data/upgrade/lrlz_release_1.0.8.apk';
-//        }
-//        return href;
-//
-//    }
-//    function href(){
-//        $('#href_link').attr('href',app());
-//    }
-//    href();
-//</script>
-//
-//    <div class="top">
-//        <p class="title">红包详情</p>
-//        <div class="logo"></div>
-//    </div>
-//EOT;
-//
-//
-//$str = text_filter::filter_html($str);
-//
-//category_helper::instance()->
-//
-//
-//
-//
-//
-//$x = time();
-//
-//$member_birthday = strftime ("%Y-%m-%d %H:%M:%S",time());
-//
-//function is_date($time)
-//{
-//    $pattern = '/[\d]{4}-[\d]{1,2}-[\d]{1,2}/';
-//
-//    return (preg_match($pattern, $time) == 1);
-//}
-//
-//$x = '';
-//$y = isset($x);
-//
-//if('safdasefasefasf'==0){
-//    echo "该字符串转换为数字  等于 0 <br/>";
-//}//output:该字符串转换为数字 等于零。
-//
-//$a='0';
-//if($a==''){
-//    echo "0  等于  '' <br/>";
-//}    //output:0 等于''
-//if(trim($a)==''){
-//    echo "trim(0)等于'' <br/>";
-//}    //no output
-//
-//if($a===''){
-//    echo "0==='' <br/>";
-//}    //no output
-//if(empty($a)){
-//    echo "'' is empty <br/>";
-//}   //output:'' is empty
-//if(is_null($a)){
-//    echo "0 is null <br/>";
-//}   //no output
-//if(is_numeric($a)){
-//    echo "0 is numeric <br/>";
-//}   //output:0 is numeric
-//if(is_string($a)){
-//    echo "0 is string <br/>";
-//}   //no output
-//if(strval($a)==''){
-//    echo "转换成字符串的0 is '' <br/>";
-//}   //no output
-////========= 判断 0 和 '' 以及 empty null false的关系 end =========//
-////========= 判断 '' 和 0 以及 empty null false的关系 start =========//
-//$b = '';
-//if($b==0){
-//    echo "''  等于  0 <br/>";
-//}    //output:''  等于  0
-//if(!''){
-//    echo "'' 就是false <br/>";
-//}    //output:'' 就是false
-//if(!0){
-//    echo "0 就是false <br/>";
-//}    //output:0 就是false
-////========= 判断 '' 和 0 以及 empty null false的关系 end =========//
-//echo "在判断空('')的时候一定要小心,0 也相当于'' ,0  和 '' 都相当于空字符和false,判断为空最好用===";
-//
-//$x = '1978-02-05';
-//$y = is_date($x);
-//
-//$x = time();
-//
-//$member_birthday = strftime ("%Y-%m-%d","{$x}");
-//
-//$time = strtotime($member_birthday);
-//
-//
-//$str = NULL;
-//$y = '';
-//if(isset($str)) {
-//    $x = 0;
-//}
-//
-//if(empty($str)) {
-//    $x = 1;
-//}
-//
-//if($str == NULL) {
-//    $x = 2;
-//}
-//
-//if(isset($y)) {
-//    $x = 0;
-//}
-//
-//if(empty($y)) {
-//    $x = 1;
-//}
-//
-//if($y == NULL) {
-//    $x = 2;
-//}
-//
-//
-//$file = "/mobile/index.php";
-//$ops = explode("?",$file);
-//
-//$squery = $ops[1];
-//$params = preg_split('/&|=/', $squery);
-//
-//for ($i = 0; $i < count($params); ++$i) {
-//    $key = $params[$i];
-//    $val = $params[++$i];
-//    $_GET[$key] = $val;
-//    $_POST[$key] = $val;
-//}
-//
-//
-//
-//
-//$ret = array('jsObj' => NULL,'jsArray' => array());
-//unset($ret['xxx']);
-//$ret = json_encode($ret);
-//
-//define('BASE_ROOT_PATH',str_replace('\\','/',dirname(__FILE__)));
-//
-//echo BASE_ROOT_PATH."\n";
-//
-//require_once (BASE_ROOT_PATH . '/fooder.php');
-//
-//echo $argv;
-//
-//function help_out()
-//{
-//    echo "fetch_goods:start fetch goods.\r\n";
-//    echo "fillgcid:start fill all goods class id.\r\n";
-//    echo "checkgoods:check all goods class id.\r\n";
-//
-//}
-//
-//
-//$time = -microtime();
-////$hash = 0;
-////for ($i=0; $i < rand(1000,4000); ++$i) {
-////    $hash ^= md5(substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, rand(1,10)));
-////}
-//$time += microtime();
-//$y = sprintf('%f', $time);
-//
-//
-//$y = strtotime('2016-12-12 01:01:01');
-//
-//$y = urldecode('%E4%B8%AD%E9%80%9A%E5%BF%AB%E9%80%92');
-//
-//$x=new date();
-//
-//define('StartTime', microtime(true));
-//
-//if(count($argv) == 1) {
-//
-//    return;
-//}
-
-
-
-

+ 0 - 16
research/sms_test.php

@@ -1,16 +0,0 @@
-<?php
-
-require_once (BASE_CORE_PATH . '/framework/libraries/sms.php');
-
-
-function getcode()
-{
-    $code = sprintf("%04d",rand(0, 9999));
-    return $code;
-}
-
-$code = getcode();
-$sms = new Sms();
-$sms->send('13917248980', array('code' =>"9867",'time' => "5", "type" => Sms::register_code));
-
-

+ 0 - 41
research/wxpay.php

@@ -1,41 +0,0 @@
-<?php
-/**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 15/12/8
- * Time: 下午10:53
- */
-
-define(WXPAY_PATH,BASE_DATA_PATH.'/api/wxpay');
-require_once (WXPAY_PATH . '/lib/WxPay.Api.php');
-require_once (WXPAY_PATH . '/lib/WxPay.Config.php');
-require_once (WXPAY_PATH . '/lib/WxPay.Data.php');
-require_once (WXPAY_PATH . '/lib/WxPay.Exception.php');
-require_once (WXPAY_PATH . '/lib/WxPay.Notify.php');
-
-//①、获取用户openid
-//$tools = new JsApiPay();
-//$openId = $tools->GetOpenid();
-
-//②、统一下单
-$input = new WxPayUnifiedOrder();
-$input->SetBody("test"); //商品或支付单简要描述
-$input->SetAttach("test"); //附加数据,在查询API和支付通知中原样返回,该字段主要用于商户携带订单的自定义数据
-$input->SetOut_trade_no(WxPayConfig::MCHID.date("YmdHis"));//商户系统内部的订单号,32个字符内、可包含字母, 其他说明见商户订单号
-$input->SetTotal_fee("1");//订单总金额,单位为分,详见支付金额
-$input->SetTime_start(date("YmdHis")); //订单生成时间,格式为yyyyMMddHHmmss,如2009年12月25日9点10分10秒表示为20091225091010。其他详见时间规则
-$input->SetTime_expire(date("YmdHis", time() + 600));//订单失效时间,格式为yyyyMMddHHmmss,如2009年12月27日9点10分10秒表示为20091227091010。其他详见时间规则
-$input->SetGoods_tag("test");
-$input->SetNotify_url("http://paysdk.weixin.qq.com/example/notify.php");
-$input->SetTrade_type("APP");//JSAPI--公众号支付、NATIVE--原生扫码支付、APP--app支付,统一下单接口trade_type的传参可参考这里,MICROPAY--刷卡支付,刷卡支付有单独的支付接口,不调用统一下单接口
-//$input->SetOpenid($openId);
-$order = WxPayApi::unifiedOrder($input);
-
-echo $order;
-
-//echo '<font color="#f00"><b>统一下单支付单信息</b></font><br/>';
-//printf_info($order);
-//$jsApiParameters = $tools->GetJsApiParameters($order);
-//
-////获取共享收货地址js函数参数
-//$editAddress = $tools->GetEditAddressParameters();