浏览代码

增加搜索借口

stanley-king 8 年之前
父节点
当前提交
76af4aa327

+ 60 - 2
centra_srv.php

@@ -5,6 +5,64 @@ define('BASE_ROOT_PATH',str_replace('\\','/',dirname(__FILE__)));
 require_once (BASE_ROOT_PATH . '/fooder.php');
 require_once (BASE_ROOT_PATH . '/helper/search/server.php');
 require_once (BASE_ROOT_PATH . '/helper/search/processor.php');
+require_once (BASE_ROOT_PATH . '/helper/search/event_handler.php');
+
+function search_work($sockfd)
+{
+    echo "start searcher";
+    Base::run_util();
+    search\searcher::instance();
+    search\CentraHelper::instance()->init(new search\processor());
+    echo "start ending";
+
+    search\CentraHelper::instance()->run_loop($sockfd);
+}
+
+function fork_subprocess($count,$listen_fd)
+{
+    if (($child = pcntl_fork()) === 0)
+    {
+        echo "pid = {$child} count = {$count} " . __LINE__ ."\r\n";
+        search_work($listen_fd);
+        exit();
+    }
+    elseif($child === -1)
+    {
+        echo "pid = {$child} count = {$count} " . __LINE__ ."\r\n";
+        die('could not fork');
+    }
+    else
+    {
+        echo "pid = {$child} count = {$count} " . __LINE__ ."\r\n";
+        $ret = pcntl_waitpid($child,$status,WNOHANG);
+        if($ret == 0) {
+            echo "spawn-fcgi: successful ret == 0 PID: {$child}\n" ;
+        }
+        elseif($ret == -1) {
+            echo "spawn-fcgi: ret == 0  PID: {$child}\n" ;
+        }
+        else {
+            echo "spawn-fcgi: ret == 0 child exited PID: {$child}.\n";
+        }
+    }
+}
+
+function remote_addr()
+{
+    global $config;
+
+    $host = $config['searcher']['host'];
+    $port = $config['searcher']['port'];
+
+    return "{$host}:{$port}";
+}
+
+$listen_fd = stream_socket_server (remote_addr(), $errno, $errstr);
+
+$count = 3;
+while ($count-- > 0) {
+    fork_subprocess($count,$listen_fd);
+}
+
+
 
-search\CentraHelper::instance()->init(new search\processor());
-search\CentraHelper::instance()->run_loop();

+ 1 - 1
helper/buy_first.php

@@ -198,7 +198,7 @@ class buy_first
     private function invoice()
     {
         //发票信息
-        return $this->mLogicOut['inv_info'];;
+        return $this->mLogicOut['inv_info'];
     }
     private function available_pred($goods_amount)
     {

+ 28 - 0
helper/order_helper.php

@@ -690,6 +690,33 @@ class order_helper
 
         return $info;
     }
+    private function invoice_info($order)
+    {
+        $result = [];
+        $result['inv_content'] = "";
+        $result['inv_title'] = "";
+        $result['inv_id'] = 0;
+
+        if(array_key_exists('extend_order_common',$order))
+        {
+            $order_common = $order['extend_order_common'];
+            if(array_key_exists('invoice_info',$order_common))
+            {
+                $invoice_info = $order_common['invoice_info'];
+                foreach ($invoice_info as $key => $value)
+                {
+                    if($key == '内容') {
+                        $result['inv_content'] = $value;
+                    }
+                    elseif($key == '抬头') {
+                        $item['inv_title'] = $value;
+                    }
+                }
+            }
+        }
+
+        return $result;
+    }
 
     public function format()
     {
@@ -700,6 +727,7 @@ class order_helper
             $item['actions'] = $actions;
             $item['order_goods'] = $this->order_goods($order);
             $item['reciver_info'] = $this->reciver_info($order);
+            $item['invoice_info'] = $this->invoice_info($order);
 
             $result[] = $item;
         }

+ 21 - 0
helper/search/event_handler.php

@@ -0,0 +1,21 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: stanley-king
+ * Date: 2016/10/15
+ * Time: 下午4:41
+ */
+
+function ev_accept($socket, $flag, $base)
+{
+    search\CentraHelper::instance()->ev_accept($socket,$flag,$base);
+}
+function ev_read($buffer, $id)
+{
+    search\CentraHelper::instance()->ev_read($buffer,$id);
+}
+
+function ev_error($buffer, $error, $id)
+{
+    search\CentraHelper::instance()->ev_error($buffer,$error,$id);
+}

+ 1 - 1
helper/search/processor.php

@@ -24,7 +24,7 @@ class processor implements IProcessor
 
         if($type == self::GetRelatedWord)
         {
-            $words = $param['words'];
+            $words = $param['keyword'];
             $result = searcher::instance()->get_word($words);
             return serialize($result);
         }

+ 7 - 3
helper/search/searcher.php

@@ -11,6 +11,7 @@ namespace search;
 use algorithm;
 use brand_helper;
 use category_helper;
+use Model;
 
 function mb_str_split( $string ) {
     return preg_split('/(?<!^)(?!$)/u', $string );
@@ -261,10 +262,13 @@ class searcher
             if($i == 0) {
                 $ids = $ids;
             }
-            else {
+            else
+            {
                 $tmp = algorithm::intersection($ids,$ids);
-                if(!empty($tmp)) {
-                    $ids = $tmp;
+                $ids = $tmp;
+
+                if(empty($ids)) {
+                    break;
                 }
             }
             ++$i;

+ 22 - 25
helper/search/server.php

@@ -8,29 +8,15 @@
 
 namespace search;
 
-function ev_accept($socket, $flag, $base)
-{
-    CentraHelper::instance()->ev_accept($socket,$flag,$base);
-}
-function ev_read($buffer, $id)
-{
-    CentraHelper::instance()->ev_read($buffer,$id);
-}
-
-function ev_error($buffer, $error, $id)
-{
-    CentraHelper::instance()->ev_error($buffer,$error,$id);
-}
-
 interface IProcessor
 {
     public function handle_input($body);
 }
 
-
 class CentraHelper
 {
-    const body_len = 10;
+    const body_header_len = 10;
+    const time_out = 3600;
 
     private $socket;
     private $ev_base;
@@ -62,9 +48,10 @@ class CentraHelper
         $this->processor = $processor;
     }
 
-    public function run_loop()
+    public function run_loop($sockfd)
     {
-        $this->socket = stream_socket_server ('tcp://0.0.0.0:2000', $errno, $errstr);
+        $this->socket = $sockfd;
+        //$this->socket = stream_socket_server ($this->remote_addr(), $errno, $errstr);
         stream_set_blocking($this->socket, 0);
         $this->ev_base = event_base_new();
         $this->ev = event_new();
@@ -82,7 +69,7 @@ class CentraHelper
         $buffer = event_buffer_new($connection, 'ev_read', NULL, 'ev_error', $this->connect_id);
 
         event_buffer_base_set($buffer, $base);
-        event_buffer_timeout_set($buffer, 30, 30);
+        event_buffer_timeout_set($buffer, self::time_out, self::time_out);
         event_buffer_watermark_set($buffer, EV_READ, 0, 0xffffff);
         event_buffer_priority_set($buffer, 10);
         event_buffer_enable($buffer, EV_READ | EV_PERSIST);
@@ -104,21 +91,21 @@ class CentraHelper
         $left = strlen($content);
         do
         {
-            if($left > self::body_len)
+            if($left > self::body_header_len)
             {
-                $len = substr($content,$start,self::body_len);
+                $len = substr($content,$start,self::body_header_len);
                 $len = intval($len);
-                if($left >= self::body_len + $len)
+                if($left >= self::body_header_len + $len)
                 {
-                    $body = substr($content,$start + self::body_len,$len);
+                    $body = substr($content,$start + self::body_header_len,$len);
                     if($this->processor != null) {
                         $data = $this->processor->handle_input($body);
                         $header = sprintf("%010d",strlen($data));
                         $data = $header . $data;
                         event_buffer_write($buffer,$data,strlen($data));
                     }
-                    $start += self::body_len + $len;
-                    $left = $left - self::body_len - $len;
+                    $start += self::body_header_len + $len;
+                    $left = $left - self::body_header_len - $len;
                 }
                 else {
                     break;
@@ -143,4 +130,14 @@ class CentraHelper
         unset($this->buffers[$id]);
         unset($this->contents[$id]);
     }
+
+    private function remote_addr()
+    {
+        global $config;
+
+        $host = $config['searcher']['host'];
+        $port = $config['searcher']['port'];
+
+        return "{$host}:{$port}";
+    }
 }

+ 154 - 0
helper/search/tcp_client.php

@@ -0,0 +1,154 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: stanley-king
+ * Date: 2016/10/15
+ * Time: 下午3:52
+ */
+
+namespace search;
+
+use Log;
+
+class tcp_client
+{
+    const body_header_len = 10;
+
+    private static $stInstance;
+    private $mSocket;
+
+    private function __construct()
+    {
+        $this->mSocket = false;
+    }
+
+    public function __destruct()
+    {
+        $this->fini_socket();
+    }
+
+    public static function instance()
+    {
+        if(self::$stInstance == null) {
+            self::$stInstance = new tcp_client();
+        }
+        return self::$stInstance;
+    }
+
+    public function get_words($word)
+    {
+        if($this->init_socket() == false) {
+            return false;
+        }
+
+        $param = array("type" => processor::GetRelatedWord, "keyword" => $word);
+        $ret = $this->write_param($param);
+
+        if($ret == true)
+        {
+            $body = $this->read_body();
+            if($body != false) {
+                return unserialize($body);
+            }
+        }
+        $this->fini_socket();
+        return false;
+    }
+
+    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+    private function init_socket()
+    {
+        if($this->mSocket != false)
+        {
+            if(feof($this->mSocket) == true) {
+                $this->fini_socket();
+            } else {
+                return true;
+            }
+        }
+        else
+        {
+            $host = $this->remote_addr();
+            $this->mSocket = stream_socket_client ($host,$err,$errno,30);
+            if($this->mSocket == false) {
+                Log::record("connect search server err --- code:{$errno} : {$err}");
+                return false;
+            } else {
+                return true;
+            }
+        }
+    }
+
+    private function fini_socket()
+    {
+        if($this->mSocket != false) {
+            stream_socket_shutdown($this->mSocket,STREAM_SHUT_RDWR);
+            fclose($this->mSocket);
+            $this->mSocket = false;
+        }
+    }
+
+    private function read_body()
+    {
+        $body_len = $this->read(self::body_header_len);
+        $body_len = intval($body_len);
+        if($body_len > 0) {
+            $body = $this->read($body_len);
+            return $body;
+        } else {
+            return false;
+        }
+    }
+
+    private function read($len)
+    {
+        $left = $len;
+        $data = "";
+        while ($left > 0)
+        {
+            if(feof($this->mSocket) == true) {
+                return false;
+            } else {
+                $tmp = fread($this->mSocket,$left);
+                $left = $left - strlen($tmp);
+                $data .= $tmp;
+            }
+        }
+
+        return $data;
+    }
+    private function write($data,$len)
+    {
+        while ($len > 0)
+        {
+            $ret = fwrite($this->mSocket,$data);
+            if($ret == false || $ret <= 0) {
+                return false;
+            }
+            else {
+                $len -= $ret;
+            }
+        }
+
+        return true;
+    }
+
+    private function write_param($param)
+    {
+        $param = serialize($param);
+        $len = sprintf("%010d",strlen($param));
+
+        $data = $len . $param;
+        return $this->write($data,strlen($data));
+    }
+
+    private function remote_addr()
+    {
+        global $config;
+
+        $host = $config['searcher']['host'];
+        $port = $config['searcher']['port'];
+
+        return "{$host}:{$port}";
+    }
+}

+ 0 - 1
mobile/control/index.php

@@ -33,7 +33,6 @@ class indexControl extends specialControl
         return self::outsuccess($ret,"shop/home");
     }
 
-
     public function indexOp()
     {
         $special_list = $this->mb_special->getMbSpecialItemUsableListByIDEx(self::HomeSpecialID);

+ 1 - 1
mobile/control/member_buy.php

@@ -99,7 +99,7 @@ class member_buyControl extends mbMemberControl
 
     public function payOp()
     {
-        $pay_sn = $_GET['paysn'];
+        $pay_sn = $_GET['pay_sn'];
         $payment = $_GET['payment'];
         $usebonus = intval($_GET['usebonus']) == 0 ? false : true;
 

+ 13 - 7
mobile/control/member_order.php

@@ -40,7 +40,7 @@ class member_orderControl extends mbMemberControl
             $condition['order_state'] = array("in",array(ORDER_STATE_NEW,ORDER_STATE_PAY,ORDER_STATE_SEND,ORDER_STATE_SUCCESS,ORDER_STATE_CANCEL));
         }
 
-        $order_list_array = $model_order->getNormalOrderList($condition, $this->page_size, '*', 'order_id desc', '', array('order_address', 'order_goods'));
+        $order_list_array = $model_order->getNormalOrderList($condition, $this->page_size, '*', 'order_id desc', '', array('order_common','order_address', 'order_goods'));
         $order_helper = new order_helper($order_list_array);
         $result = $order_helper->format();
         $page_count = $model_order->gettotalpage();
@@ -55,8 +55,17 @@ class member_orderControl extends mbMemberControl
             return self::outerr(errcode::ErrParamter);
         }
         $order = $this->get_order(array("buyer_id" => $_SESSION['member_id'],'pay_sn' => $pay_sn));
-        $pay_ments = pay_helper::pay_types();
+        self::outsuccess(array('order' => $order));
+    }
 
+    public function pay_infoOp()
+    {
+        $pay_sn = $_POST['pay_sn'];
+        if(empty($pay_sn)) {
+            return self::outerr(errcode::ErrParamter);
+        }
+        $order = $this->get_order(array("buyer_id" => $_SESSION['member_id'],'pay_sn' => $pay_sn));
+        $pay_ments = pay_helper::pay_types();
         self::outsuccess(array('order' => $order,"paytype" => $pay_ments));
     }
 
@@ -91,7 +100,7 @@ class member_orderControl extends mbMemberControl
     private function get_order($condition)
     {
         $model_order = Model('order');
-        $order_list = $model_order->getNormalOrderList($condition, $this->page_size, '*', 'order_id desc', '', array('order_address', 'order_goods'));
+        $order_list = $model_order->getNormalOrderList($condition, $this->page_size, '*', 'order_id desc', '', array('order_common','order_address', 'order_goods'));
         $order_helper = new order_helper($order_list);
         $orders = $order_helper->format();
         $model_order->cls();
@@ -104,7 +113,7 @@ class member_orderControl extends mbMemberControl
 
     public function payOp()
     {
-        $pay_sn = $_GET['paysn'];
+        $pay_sn = $_GET['pay_sn'];
         $payment = $_GET['payment'];
         if(empty($pay_sn) || empty($payment)) {
             return self::outerr(errcode::ErrParamter,"支付号或者支付类型错误");
@@ -120,9 +129,6 @@ class member_orderControl extends mbMemberControl
         }
     }
 
-    /**
-     * 取消订单
-     */
     ///////////////////////////////////////////////////////////////////下面是老接口///////////////////////////////////////////////////////////////////////////
     public function order_listOp()
     {

+ 1 - 1
mobile/control/member_refund.php

@@ -137,7 +137,7 @@ class member_refundControl extends mbMemberControl
     private function get_order($condition)
     {
         $model_order = Model('order');
-        $order_list = $model_order->getNormalOrderList($condition, $this->page_size, '*', 'order_id desc', '', array('order_address', 'order_goods'));
+        $order_list = $model_order->getNormalOrderList($condition, $this->page_size, '*', 'order_id desc', '', array('order_common','order_address', 'order_goods'));
         $order_helper = new order_helper($order_list);
         $orders = $order_helper->format();
         $model_order->cls();

+ 59 - 32
mobile/control/search.php

@@ -5,6 +5,10 @@ defined('InShopNC') or exit('Access Invalid!');
 
 require_once (BASE_ROOT_PATH . '/helper/category_helper.php');
 require_once (BASE_ROOT_PATH . '/helper/goods_helper.php');
+require_once(BASE_ROOT_PATH . '/helper/search/searcher.php');
+require_once(BASE_ROOT_PATH . '/helper/search/server.php');
+require_once(BASE_ROOT_PATH . '/helper/search/processor.php');
+require_once(BASE_ROOT_PATH . '/helper/search/tcp_client.php');
 
 class searchControl extends mobileHomeControl
 {
@@ -55,6 +59,61 @@ class searchControl extends mobileHomeControl
         }
     }
 
+    public function get_wordsOp()
+    {
+        $keyword = $_GET['keyword'];
+        if(empty($keyword)) {
+            return self::outerr(errcode::ErrParamter);
+        }
+        else
+        {
+            $result = search\tcp_client::instance()->get_words($keyword);
+            if($result == false) {
+                $result = null;
+                $pages = 0;
+            } else {
+                $result = $this->separate_page($result,$pages);
+            }
+            return self::outsuccess(array('words' => $result,'mobile_page' => mobile_page($pages)));
+        }
+    }
+    public function historyOp()
+    {
+
+    }
+
+    /**
+     * @return array
+     */
+    private function condition()
+    {
+        $condition = array();
+        //分类查询
+        if (!empty(trim($_GET['gc_id']))) {
+            $condition['gc_id'] = intval($_GET['gc_id']);
+        }
+        // 关键字
+        if (!empty(trim($_GET['keyword']))) {
+            $condition['goods_name|goods_jingle'] = array('like', '%' . urldecode($_GET['keyword']) . '%');
+        }
+        // 品牌查询
+        if (!empty(trim($_GET['brand_id']))) {
+            $condition['brand_id'] = intval($_GET['brand_id']);
+        }
+        // 功效查询
+        if (!empty(trim($_GET['efficacy_id']))) {
+            $condition['efficacy_id'] = intval($_GET['efficacy_id']);
+        }
+        //重新组合后的分类查询
+        if (!empty(trim($_GET['hot_id']))) {
+            $id = intval($_GET['hot_id']);
+            $cids = category_helper::instance()->cids($id);
+            $condition['gc_id'] = array('in', $cids);
+        }
+        return $condition;
+    }
+
+    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     public function goods_listOp()
     {
         $model_goods = Model('goods');
@@ -90,7 +149,6 @@ class searchControl extends mobileHomeControl
             if ($order == 1) {
                 $sequence = 'asc';
             }
-
             switch ($key)
             {
                 case '1' : //销量
@@ -492,35 +550,4 @@ class searchControl extends mobileHomeControl
         }
         Tpl::showpage('goods_detail');
     }
-
-    /**
-     * @return array
-     */
-    private function condition()
-    {
-        $condition = array();
-        //分类查询
-        if (!empty(trim($_GET['gc_id']))) {
-            $condition['gc_id'] = intval($_GET['gc_id']);
-        }
-        // 关键字
-        if (!empty(trim($_GET['keyword']))) {
-            $condition['goods_name|goods_jingle'] = array('like', '%' . urldecode($_GET['keyword']) . '%');
-        }
-        // 品牌查询
-        if (!empty(trim($_GET['brand_id']))) {
-            $condition['brand_id'] = intval($_GET['brand_id']);
-        }
-        // 功效查询
-        if (!empty(trim($_GET['efficacy_id']))) {
-            $condition['efficacy_id'] = intval($_GET['efficacy_id']);
-        }
-        //重新组合后的分类查询
-        if (!empty(trim($_GET['hot_id']))) {
-            $id = intval($_GET['hot_id']);
-            $cids = category_helper::instance()->cids($id);
-            $condition['gc_id'] = array('in', $cids);
-        }
-        return $condition;
-    }
 }

+ 0 - 1
mobile/control/special.php

@@ -31,7 +31,6 @@ class specialControl extends mobileHomeControl
         $special_id = intval($_GET['special_id']);
         $ret = $this->get_special($special_id);
         self::outsuccess($ret);
-
     }
 
     protected function get_special($special_id)

+ 6 - 0
test/TestSearch.php

@@ -12,6 +12,7 @@ require_once(BASE_ROOT_PATH . '/fooder.php');
 require_once(BASE_ROOT_PATH . '/helper/search/searcher.php');
 require_once(BASE_ROOT_PATH . '/helper/search/server.php');
 require_once(BASE_ROOT_PATH . '/helper/search/processor.php');
+require_once(BASE_ROOT_PATH . '/helper/search/tcp_client.php');
 require_once(BASE_ROOT_PATH . '/helper/category_helper.php');
 
 
@@ -35,4 +36,9 @@ class TestSearch extends PHPUnit_Framework_TestCase
         $result = $searcher->get_word('眼光影');
     }
 
+    public function testRequest()
+    {
+        $result = search\tcp_client::instance()->get_words('眼光影');
+    }
+
 }