Browse Source

add pl_mall

root 1 year ago
commit
7de10687ea
100 changed files with 26793 additions and 0 deletions
  1. 74 0
      GatewayWorker/Applications/YourApp/Events.php
  2. 37 0
      GatewayWorker/Applications/YourApp/start_businessworker.php
  3. 79 0
      GatewayWorker/Applications/YourApp/start_gateway.php
  4. 28 0
      GatewayWorker/Applications/YourApp/start_register.php
  5. 7 0
      GatewayWorker/composer.json
  6. 37 0
      GatewayWorker/start.php
  7. 2 0
      GatewayWorker/start_for_win.bat
  8. 7 0
      GatewayWorker/vendor/autoload.php
  9. 445 0
      GatewayWorker/vendor/composer/ClassLoader.php
  10. 21 0
      GatewayWorker/vendor/composer/LICENSE
  11. 9 0
      GatewayWorker/vendor/composer/autoload_classmap.php
  12. 9 0
      GatewayWorker/vendor/composer/autoload_namespaces.php
  13. 13 0
      GatewayWorker/vendor/composer/autoload_psr4.php
  14. 52 0
      GatewayWorker/vendor/composer/autoload_real.php
  15. 49 0
      GatewayWorker/vendor/composer/autoload_static.php
  16. 179 0
      GatewayWorker/vendor/composer/installed.json
  17. 1 0
      GatewayWorker/vendor/workerman/_home_wwwroot_dsmall.csdeshang.com_GatewayWorker_start.php.pid
  18. 4 0
      GatewayWorker/vendor/workerman/gateway-worker/.gitignore
  19. 21 0
      GatewayWorker/vendor/workerman/gateway-worker/MIT-LICENSE.txt
  20. 38 0
      GatewayWorker/vendor/workerman/gateway-worker/README.md
  21. 12 0
      GatewayWorker/vendor/workerman/gateway-worker/composer.json
  22. 561 0
      GatewayWorker/vendor/workerman/gateway-worker/src/BusinessWorker.php
  23. 1027 0
      GatewayWorker/vendor/workerman/gateway-worker/src/Gateway.php
  24. 136 0
      GatewayWorker/vendor/workerman/gateway-worker/src/Lib/Context.php
  25. 76 0
      GatewayWorker/vendor/workerman/gateway-worker/src/Lib/Db.php
  26. 1976 0
      GatewayWorker/vendor/workerman/gateway-worker/src/Lib/DbConnection.php
  27. 1361 0
      GatewayWorker/vendor/workerman/gateway-worker/src/Lib/Gateway.php
  28. 216 0
      GatewayWorker/vendor/workerman/gateway-worker/src/Protocols/GatewayProtocol.php
  29. 190 0
      GatewayWorker/vendor/workerman/gateway-worker/src/Register.php
  30. 1607 0
      GatewayWorker/vendor/workerman/gatewayclient/Gateway.php
  31. 21 0
      GatewayWorker/vendor/workerman/gatewayclient/MIT-LICENSE.txt
  32. 69 0
      GatewayWorker/vendor/workerman/gatewayclient/README.md
  33. 9 0
      GatewayWorker/vendor/workerman/gatewayclient/composer.json
  34. 78 0
      GatewayWorker/vendor/workerman/mysql/README.md
  35. 16 0
      GatewayWorker/vendor/workerman/mysql/composer.json
  36. 1986 0
      GatewayWorker/vendor/workerman/mysql/src/Connection.php
  37. BIN
      GatewayWorker/vendor/workerman/workerman.log
  38. 6 0
      GatewayWorker/vendor/workerman/workerman/.gitignore
  39. 69 0
      GatewayWorker/vendor/workerman/workerman/Autoloader.php
  40. 377 0
      GatewayWorker/vendor/workerman/workerman/Connection/AsyncTcpConnection.php
  41. 209 0
      GatewayWorker/vendor/workerman/workerman/Connection/AsyncUdpConnection.php
  42. 125 0
      GatewayWorker/vendor/workerman/workerman/Connection/ConnectionInterface.php
  43. 1012 0
      GatewayWorker/vendor/workerman/workerman/Connection/TcpConnection.php
  44. 191 0
      GatewayWorker/vendor/workerman/workerman/Connection/UdpConnection.php
  45. 195 0
      GatewayWorker/vendor/workerman/workerman/Events/Ev.php
  46. 219 0
      GatewayWorker/vendor/workerman/workerman/Events/Event.php
  47. 107 0
      GatewayWorker/vendor/workerman/workerman/Events/EventInterface.php
  48. 227 0
      GatewayWorker/vendor/workerman/workerman/Events/Libevent.php
  49. 264 0
      GatewayWorker/vendor/workerman/workerman/Events/React/Base.php
  50. 27 0
      GatewayWorker/vendor/workerman/workerman/Events/React/ExtEventLoop.php
  51. 27 0
      GatewayWorker/vendor/workerman/workerman/Events/React/ExtLibEventLoop.php
  52. 26 0
      GatewayWorker/vendor/workerman/workerman/Events/React/StreamSelectLoop.php
  53. 339 0
      GatewayWorker/vendor/workerman/workerman/Events/Select.php
  54. 221 0
      GatewayWorker/vendor/workerman/workerman/Events/Swoole.php
  55. 48 0
      GatewayWorker/vendor/workerman/workerman/Lib/Constants.php
  56. 22 0
      GatewayWorker/vendor/workerman/workerman/Lib/Timer.php
  57. 21 0
      GatewayWorker/vendor/workerman/workerman/MIT-LICENSE.txt
  58. 61 0
      GatewayWorker/vendor/workerman/workerman/Protocols/Frame.php
  59. 326 0
      GatewayWorker/vendor/workerman/workerman/Protocols/Http.php
  60. 48 0
      GatewayWorker/vendor/workerman/workerman/Protocols/Http/Chunk.php
  61. 617 0
      GatewayWorker/vendor/workerman/workerman/Protocols/Http/Request.php
  62. 378 0
      GatewayWorker/vendor/workerman/workerman/Protocols/Http/Response.php
  63. 64 0
      GatewayWorker/vendor/workerman/workerman/Protocols/Http/ServerSentEvents.php
  64. 359 0
      GatewayWorker/vendor/workerman/workerman/Protocols/Http/Session.php
  65. 153 0
      GatewayWorker/vendor/workerman/workerman/Protocols/Http/Session/FileSessionHandler.php
  66. 119 0
      GatewayWorker/vendor/workerman/workerman/Protocols/Http/Session/RedisSessionHandler.php
  67. 90 0
      GatewayWorker/vendor/workerman/workerman/Protocols/Http/mime.types
  68. 52 0
      GatewayWorker/vendor/workerman/workerman/Protocols/ProtocolInterface.php
  69. 70 0
      GatewayWorker/vendor/workerman/workerman/Protocols/Text.php
  70. 503 0
      GatewayWorker/vendor/workerman/workerman/Protocols/Websocket.php
  71. 472 0
      GatewayWorker/vendor/workerman/workerman/Protocols/Ws.php
  72. 386 0
      GatewayWorker/vendor/workerman/workerman/README.md
  73. 213 0
      GatewayWorker/vendor/workerman/workerman/Timer.php
  74. 2541 0
      GatewayWorker/vendor/workerman/workerman/Worker.php
  75. 38 0
      GatewayWorker/vendor/workerman/workerman/composer.json
  76. 849 0
      README.md
  77. 7 0
      apidoc.json
  78. 1 0
      app/.htaccess
  79. 22 0
      app/AppService.php
  80. 205 0
      app/BaseController.php
  81. 58 0
      app/ExceptionHandle.php
  82. 8 0
      app/Request.php
  83. 2 0
      app/admin/common.php
  84. 7 0
      app/admin/config/jump.php
  85. 19 0
      app/admin/config/session.php
  86. 167 0
      app/admin/controller/Account.php
  87. 415 0
      app/admin/controller/Activity.php
  88. 321 0
      app/admin/controller/Admin.php
  89. 895 0
      app/admin/controller/AdminControl.php
  90. 194 0
      app/admin/controller/Adminlog.php
  91. 407 0
      app/admin/controller/Adv.php
  92. 380 0
      app/admin/controller/Appadv.php
  93. 71 0
      app/admin/controller/Arrivalnotice.php
  94. 354 0
      app/admin/controller/Article.php
  95. 256 0
      app/admin/controller/Articleclass.php
  96. 552 0
      app/admin/controller/Bill.php
  97. 248 0
      app/admin/controller/Bonus.php
  98. 457 0
      app/admin/controller/Brand.php
  99. 158 0
      app/admin/controller/Chain.php
  100. 65 0
      app/admin/controller/Common.php

+ 74 - 0
GatewayWorker/Applications/YourApp/Events.php

@@ -0,0 +1,74 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link http://www.workerman.net/
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+
+/**
+ * 用于检测业务代码死循环或者长时间阻塞等问题
+ * 如果发现业务卡死,可以将下面declare打开(去掉//注释),并执行php start.php reload
+ * 然后观察一段时间workerman.log看是否有process_timeout异常
+ */
+//declare(ticks=1);
+
+use \GatewayWorker\Lib\Gateway;
+
+/**
+ * 主逻辑
+ * 主要是处理 onConnect onMessage onClose 三个方法
+ * onConnect 和 onClose 如果不需要可以不用实现并删除
+ */
+class Events
+{
+    /**
+     * 当客户端连接时触发
+     * 如果业务不需此回调可以删除onConnect
+     * 
+     * @param int $client_id 连接id
+     */
+    public static function onConnect($client_id)
+    {
+        // 向当前client_id发送数据 
+        Gateway::sendToClient($client_id, json_encode(array(
+            'type'      => 'init',
+            'client_id' => $client_id
+        )));
+    }
+    
+   /**
+    * 当客户端发来消息时触发
+    * @param int $client_id 连接id
+    * @param mixed $message 具体消息
+    */
+   public static function onMessage($client_id, $message)
+   {
+       if(strpos($message,'get_state:')===0){
+           $user_ids=explode(',',str_replace('get_state:','',$message));
+           $u_state=array();
+           foreach($user_ids as $user_id){
+               $u_state[$user_id]=Gateway::isUidOnline('0:'.$user_id);
+           }
+           Gateway::sendToClient($client_id, json_encode(array(
+                'type'      => 'get_state',
+                'u_state' => $u_state,
+            )));
+       }
+   }
+   
+   /**
+    * 当用户断开连接时触发
+    * @param int $client_id 连接id
+    */
+   public static function onClose($client_id)
+   {
+
+   }
+}

+ 37 - 0
GatewayWorker/Applications/YourApp/start_businessworker.php

@@ -0,0 +1,37 @@
+<?php 
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link http://www.workerman.net/
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+use \Workerman\Worker;
+use \Workerman\WebServer;
+use \GatewayWorker\Gateway;
+use \GatewayWorker\BusinessWorker;
+use \Workerman\Autoloader;
+
+// 自动加载类
+require_once __DIR__ . '/../../vendor/autoload.php';
+
+// bussinessWorker 进程
+$worker = new BusinessWorker();
+// worker名称
+$worker->name = 'YourAppBusinessWorker';
+// bussinessWorker进程数量
+$worker->count = 4;
+// 服务注册地址
+$worker->registerAddress = '127.0.0.1:1888';
+
+// 如果不是在根目录启动,则运行runAll方法
+if(!defined('GLOBAL_START'))
+{
+    Worker::runAll();
+}
+

+ 79 - 0
GatewayWorker/Applications/YourApp/start_gateway.php

@@ -0,0 +1,79 @@
+<?php 
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link http://www.workerman.net/
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+use \Workerman\Worker;
+use \Workerman\WebServer;
+use \GatewayWorker\Gateway;
+use \GatewayWorker\BusinessWorker;
+use \Workerman\Autoloader;
+
+// 自动加载类
+require_once __DIR__ . '/../../vendor/autoload.php';
+$context = array(
+    'ssl' => array(
+        'local_cert'  => '/www/server/panel/vhost/cert/dskms.csdeshang.com/fullchain.pem', // 或者crt文件
+        'local_pk'    => '/www/server/panel/vhost/cert/dskms.csdeshang.com/privkey.pem',
+        'verify_peer' => false
+    )
+);
+// gateway 进程,这里使用Text协议,可以用telnet测试
+$gateway = new Gateway("websocket://0.0.0.0:8881",$context);
+
+$gateway->pingInterval = 55;
+
+$gateway->pingNotResponseLimit = 1;
+
+$gateway->pingData = '';
+// 开启SSL,websocket+SSL 即wss
+$gateway->transport = 'ssl';
+// gateway名称,status方便查看
+$gateway->name = 'YourAppGateway';
+// gateway进程数
+$gateway->count = 4;
+// 本机ip,分布式部署时使用内网ip
+$gateway->lanIp = '127.0.0.1';
+// 内部通讯起始端口,假如$gateway->count=4,起始端口为4000
+// 则一般会使用4000 4001 4002 4003 4个端口作为内部通讯端口 
+$gateway->startPort = 6000;
+// 服务注册地址
+$gateway->registerAddress = '127.0.0.1:1888';
+
+// 心跳间隔
+//$gateway->pingInterval = 10;
+// 心跳数据
+//$gateway->pingData = '{"type":"ping"}';
+
+/* 
+// 当客户端连接上来时,设置连接的onWebSocketConnect,即在websocket握手时的回调
+$gateway->onConnect = function($connection)
+{
+    $connection->onWebSocketConnect = function($connection , $http_header)
+    {
+        // 可以在这里判断连接来源是否合法,不合法就关掉连接
+        // $_SERVER['HTTP_ORIGIN']标识来自哪个站点的页面发起的websocket链接
+        if($_SERVER['HTTP_ORIGIN'] != 'http://kedou.workerman.net')
+        {
+            $connection->close();
+        }
+        // onWebSocketConnect 里面$_GET $_SERVER是可用的
+        // var_dump($_GET, $_SERVER);
+    };
+}; 
+*/
+
+// 如果不是在根目录启动,则运行runAll方法
+if(!defined('GLOBAL_START'))
+{
+    Worker::runAll();
+}
+

+ 28 - 0
GatewayWorker/Applications/YourApp/start_register.php

@@ -0,0 +1,28 @@
+<?php 
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link http://www.workerman.net/
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+use \Workerman\Worker;
+use \GatewayWorker\Register;
+
+// 自动加载类
+require_once __DIR__ . '/../../vendor/autoload.php';
+
+// register 必须是text协议
+$register = new Register('text://0.0.0.0:1888');
+
+// 如果不是在根目录启动,则运行runAll方法
+if(!defined('GLOBAL_START'))
+{
+    Worker::runAll();
+}
+

+ 7 - 0
GatewayWorker/composer.json

@@ -0,0 +1,7 @@
+{
+    "require": {
+        "workerman/mysql": "^1.0",
+        "workerman/gateway-worker": "^3.0",
+        "workerman/gatewayclient": "^3.0"
+    }
+}

+ 37 - 0
GatewayWorker/start.php

@@ -0,0 +1,37 @@
+<?php
+/**
+ * run with command
+ * php start.php start
+ */
+
+ini_set('display_errors', 'on');
+use Workerman\Worker;
+
+if(strpos(strtolower(PHP_OS), 'win') === 0)
+{
+    exit("start.php not support windows, please use start_for_win.bat\n");
+}
+
+// 检查扩展
+if(!extension_loaded('pcntl'))
+{
+    exit("Please install pcntl extension. See http://doc3.workerman.net/appendices/install-extension.html\n");
+}
+
+if(!extension_loaded('posix'))
+{
+    exit("Please install posix extension. See http://doc3.workerman.net/appendices/install-extension.html\n");
+}
+
+// 标记是全局启动
+define('GLOBAL_START', 1);
+
+require_once __DIR__ . '/vendor/autoload.php';
+
+// 加载所有Applications/*/start.php,以便启动所有服务
+foreach(glob(__DIR__.'/Applications/*/start*.php') as $start_file)
+{
+    require_once $start_file;
+}
+// 运行所有服务
+Worker::runAll();

+ 2 - 0
GatewayWorker/start_for_win.bat

@@ -0,0 +1,2 @@
+php Applications\YourApp\start_register.php Applications\YourApp\start_gateway.php Applications\YourApp\start_businessworker.php
+pause

+ 7 - 0
GatewayWorker/vendor/autoload.php

@@ -0,0 +1,7 @@
+<?php
+
+// autoload.php @generated by Composer
+
+require_once __DIR__ . '/composer/autoload_real.php';
+
+return ComposerAutoloaderInit7d081a0cc7b2cb693d20cacb30b82f1a::getLoader();

+ 445 - 0
GatewayWorker/vendor/composer/ClassLoader.php

@@ -0,0 +1,445 @@
+<?php
+
+/*
+ * This file is part of Composer.
+ *
+ * (c) Nils Adermann <naderman@naderman.de>
+ *     Jordi Boggiano <j.boggiano@seld.be>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Composer\Autoload;
+
+/**
+ * ClassLoader implements a PSR-0, PSR-4 and classmap class loader.
+ *
+ *     $loader = new \Composer\Autoload\ClassLoader();
+ *
+ *     // register classes with namespaces
+ *     $loader->add('Symfony\Component', __DIR__.'/component');
+ *     $loader->add('Symfony',           __DIR__.'/framework');
+ *
+ *     // activate the autoloader
+ *     $loader->register();
+ *
+ *     // to enable searching the include path (eg. for PEAR packages)
+ *     $loader->setUseIncludePath(true);
+ *
+ * In this example, if you try to use a class in the Symfony\Component
+ * namespace or one of its children (Symfony\Component\Console for instance),
+ * the autoloader will first look for the class under the component/
+ * directory, and it will then fallback to the framework/ directory if not
+ * found before giving up.
+ *
+ * This class is loosely based on the Symfony UniversalClassLoader.
+ *
+ * @author Fabien Potencier <fabien@symfony.com>
+ * @author Jordi Boggiano <j.boggiano@seld.be>
+ * @see    http://www.php-fig.org/psr/psr-0/
+ * @see    http://www.php-fig.org/psr/psr-4/
+ */
+class ClassLoader
+{
+    // PSR-4
+    private $prefixLengthsPsr4 = array();
+    private $prefixDirsPsr4 = array();
+    private $fallbackDirsPsr4 = array();
+
+    // PSR-0
+    private $prefixesPsr0 = array();
+    private $fallbackDirsPsr0 = array();
+
+    private $useIncludePath = false;
+    private $classMap = array();
+    private $classMapAuthoritative = false;
+    private $missingClasses = array();
+    private $apcuPrefix;
+
+    public function getPrefixes()
+    {
+        if (!empty($this->prefixesPsr0)) {
+            return call_user_func_array('array_merge', $this->prefixesPsr0);
+        }
+
+        return array();
+    }
+
+    public function getPrefixesPsr4()
+    {
+        return $this->prefixDirsPsr4;
+    }
+
+    public function getFallbackDirs()
+    {
+        return $this->fallbackDirsPsr0;
+    }
+
+    public function getFallbackDirsPsr4()
+    {
+        return $this->fallbackDirsPsr4;
+    }
+
+    public function getClassMap()
+    {
+        return $this->classMap;
+    }
+
+    /**
+     * @param array $classMap Class to filename map
+     */
+    public function addClassMap(array $classMap)
+    {
+        if ($this->classMap) {
+            $this->classMap = array_merge($this->classMap, $classMap);
+        } else {
+            $this->classMap = $classMap;
+        }
+    }
+
+    /**
+     * Registers a set of PSR-0 directories for a given prefix, either
+     * appending or prepending to the ones previously set for this prefix.
+     *
+     * @param string       $prefix  The prefix
+     * @param array|string $paths   The PSR-0 root directories
+     * @param bool         $prepend Whether to prepend the directories
+     */
+    public function add($prefix, $paths, $prepend = false)
+    {
+        if (!$prefix) {
+            if ($prepend) {
+                $this->fallbackDirsPsr0 = array_merge(
+                    (array) $paths,
+                    $this->fallbackDirsPsr0
+                );
+            } else {
+                $this->fallbackDirsPsr0 = array_merge(
+                    $this->fallbackDirsPsr0,
+                    (array) $paths
+                );
+            }
+
+            return;
+        }
+
+        $first = $prefix[0];
+        if (!isset($this->prefixesPsr0[$first][$prefix])) {
+            $this->prefixesPsr0[$first][$prefix] = (array) $paths;
+
+            return;
+        }
+        if ($prepend) {
+            $this->prefixesPsr0[$first][$prefix] = array_merge(
+                (array) $paths,
+                $this->prefixesPsr0[$first][$prefix]
+            );
+        } else {
+            $this->prefixesPsr0[$first][$prefix] = array_merge(
+                $this->prefixesPsr0[$first][$prefix],
+                (array) $paths
+            );
+        }
+    }
+
+    /**
+     * Registers a set of PSR-4 directories for a given namespace, either
+     * appending or prepending to the ones previously set for this namespace.
+     *
+     * @param string       $prefix  The prefix/namespace, with trailing '\\'
+     * @param array|string $paths   The PSR-4 base directories
+     * @param bool         $prepend Whether to prepend the directories
+     *
+     * @throws \InvalidArgumentException
+     */
+    public function addPsr4($prefix, $paths, $prepend = false)
+    {
+        if (!$prefix) {
+            // Register directories for the root namespace.
+            if ($prepend) {
+                $this->fallbackDirsPsr4 = array_merge(
+                    (array) $paths,
+                    $this->fallbackDirsPsr4
+                );
+            } else {
+                $this->fallbackDirsPsr4 = array_merge(
+                    $this->fallbackDirsPsr4,
+                    (array) $paths
+                );
+            }
+        } elseif (!isset($this->prefixDirsPsr4[$prefix])) {
+            // Register directories for a new namespace.
+            $length = strlen($prefix);
+            if ('\\' !== $prefix[$length - 1]) {
+                throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
+            }
+            $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
+            $this->prefixDirsPsr4[$prefix] = (array) $paths;
+        } elseif ($prepend) {
+            // Prepend directories for an already registered namespace.
+            $this->prefixDirsPsr4[$prefix] = array_merge(
+                (array) $paths,
+                $this->prefixDirsPsr4[$prefix]
+            );
+        } else {
+            // Append directories for an already registered namespace.
+            $this->prefixDirsPsr4[$prefix] = array_merge(
+                $this->prefixDirsPsr4[$prefix],
+                (array) $paths
+            );
+        }
+    }
+
+    /**
+     * Registers a set of PSR-0 directories for a given prefix,
+     * replacing any others previously set for this prefix.
+     *
+     * @param string       $prefix The prefix
+     * @param array|string $paths  The PSR-0 base directories
+     */
+    public function set($prefix, $paths)
+    {
+        if (!$prefix) {
+            $this->fallbackDirsPsr0 = (array) $paths;
+        } else {
+            $this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths;
+        }
+    }
+
+    /**
+     * Registers a set of PSR-4 directories for a given namespace,
+     * replacing any others previously set for this namespace.
+     *
+     * @param string       $prefix The prefix/namespace, with trailing '\\'
+     * @param array|string $paths  The PSR-4 base directories
+     *
+     * @throws \InvalidArgumentException
+     */
+    public function setPsr4($prefix, $paths)
+    {
+        if (!$prefix) {
+            $this->fallbackDirsPsr4 = (array) $paths;
+        } else {
+            $length = strlen($prefix);
+            if ('\\' !== $prefix[$length - 1]) {
+                throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
+            }
+            $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
+            $this->prefixDirsPsr4[$prefix] = (array) $paths;
+        }
+    }
+
+    /**
+     * Turns on searching the include path for class files.
+     *
+     * @param bool $useIncludePath
+     */
+    public function setUseIncludePath($useIncludePath)
+    {
+        $this->useIncludePath = $useIncludePath;
+    }
+
+    /**
+     * Can be used to check if the autoloader uses the include path to check
+     * for classes.
+     *
+     * @return bool
+     */
+    public function getUseIncludePath()
+    {
+        return $this->useIncludePath;
+    }
+
+    /**
+     * Turns off searching the prefix and fallback directories for classes
+     * that have not been registered with the class map.
+     *
+     * @param bool $classMapAuthoritative
+     */
+    public function setClassMapAuthoritative($classMapAuthoritative)
+    {
+        $this->classMapAuthoritative = $classMapAuthoritative;
+    }
+
+    /**
+     * Should class lookup fail if not found in the current class map?
+     *
+     * @return bool
+     */
+    public function isClassMapAuthoritative()
+    {
+        return $this->classMapAuthoritative;
+    }
+
+    /**
+     * APCu prefix to use to cache found/not-found classes, if the extension is enabled.
+     *
+     * @param string|null $apcuPrefix
+     */
+    public function setApcuPrefix($apcuPrefix)
+    {
+        $this->apcuPrefix = function_exists('apcu_fetch') && ini_get('apc.enabled') ? $apcuPrefix : null;
+    }
+
+    /**
+     * The APCu prefix in use, or null if APCu caching is not enabled.
+     *
+     * @return string|null
+     */
+    public function getApcuPrefix()
+    {
+        return $this->apcuPrefix;
+    }
+
+    /**
+     * Registers this instance as an autoloader.
+     *
+     * @param bool $prepend Whether to prepend the autoloader or not
+     */
+    public function register($prepend = false)
+    {
+        spl_autoload_register(array($this, 'loadClass'), true, $prepend);
+    }
+
+    /**
+     * Unregisters this instance as an autoloader.
+     */
+    public function unregister()
+    {
+        spl_autoload_unregister(array($this, 'loadClass'));
+    }
+
+    /**
+     * Loads the given class or interface.
+     *
+     * @param  string    $class The name of the class
+     * @return bool|null True if loaded, null otherwise
+     */
+    public function loadClass($class)
+    {
+        if ($file = $this->findFile($class)) {
+            includeFile($file);
+
+            return true;
+        }
+    }
+
+    /**
+     * Finds the path to the file where the class is defined.
+     *
+     * @param string $class The name of the class
+     *
+     * @return string|false The path if found, false otherwise
+     */
+    public function findFile($class)
+    {
+        // class map lookup
+        if (isset($this->classMap[$class])) {
+            return $this->classMap[$class];
+        }
+        if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) {
+            return false;
+        }
+        if (null !== $this->apcuPrefix) {
+            $file = apcu_fetch($this->apcuPrefix.$class, $hit);
+            if ($hit) {
+                return $file;
+            }
+        }
+
+        $file = $this->findFileWithExtension($class, '.php');
+
+        // Search for Hack files if we are running on HHVM
+        if (false === $file && defined('HHVM_VERSION')) {
+            $file = $this->findFileWithExtension($class, '.hh');
+        }
+
+        if (null !== $this->apcuPrefix) {
+            apcu_add($this->apcuPrefix.$class, $file);
+        }
+
+        if (false === $file) {
+            // Remember that this class does not exist.
+            $this->missingClasses[$class] = true;
+        }
+
+        return $file;
+    }
+
+    private function findFileWithExtension($class, $ext)
+    {
+        // PSR-4 lookup
+        $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext;
+
+        $first = $class[0];
+        if (isset($this->prefixLengthsPsr4[$first])) {
+            $subPath = $class;
+            while (false !== $lastPos = strrpos($subPath, '\\')) {
+                $subPath = substr($subPath, 0, $lastPos);
+                $search = $subPath.'\\';
+                if (isset($this->prefixDirsPsr4[$search])) {
+                    $pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1);
+                    foreach ($this->prefixDirsPsr4[$search] as $dir) {
+                        if (file_exists($file = $dir . $pathEnd)) {
+                            return $file;
+                        }
+                    }
+                }
+            }
+        }
+
+        // PSR-4 fallback dirs
+        foreach ($this->fallbackDirsPsr4 as $dir) {
+            if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) {
+                return $file;
+            }
+        }
+
+        // PSR-0 lookup
+        if (false !== $pos = strrpos($class, '\\')) {
+            // namespaced class name
+            $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1)
+                . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR);
+        } else {
+            // PEAR-like class name
+            $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext;
+        }
+
+        if (isset($this->prefixesPsr0[$first])) {
+            foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) {
+                if (0 === strpos($class, $prefix)) {
+                    foreach ($dirs as $dir) {
+                        if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
+                            return $file;
+                        }
+                    }
+                }
+            }
+        }
+
+        // PSR-0 fallback dirs
+        foreach ($this->fallbackDirsPsr0 as $dir) {
+            if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
+                return $file;
+            }
+        }
+
+        // PSR-0 include paths.
+        if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) {
+            return $file;
+        }
+
+        return false;
+    }
+}
+
+/**
+ * Scope isolated include.
+ *
+ * Prevents access to $this/self from included files.
+ */
+function includeFile($file)
+{
+    include $file;
+}

+ 21 - 0
GatewayWorker/vendor/composer/LICENSE

@@ -0,0 +1,21 @@
+
+Copyright (c) Nils Adermann, Jordi Boggiano
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is furnished
+to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+

+ 9 - 0
GatewayWorker/vendor/composer/autoload_classmap.php

@@ -0,0 +1,9 @@
+<?php
+
+// autoload_classmap.php @generated by Composer
+
+$vendorDir = dirname(dirname(__FILE__));
+$baseDir = dirname($vendorDir);
+
+return array(
+);

+ 9 - 0
GatewayWorker/vendor/composer/autoload_namespaces.php

@@ -0,0 +1,9 @@
+<?php
+
+// autoload_namespaces.php @generated by Composer
+
+$vendorDir = dirname(dirname(__FILE__));
+$baseDir = dirname($vendorDir);
+
+return array(
+);

+ 13 - 0
GatewayWorker/vendor/composer/autoload_psr4.php

@@ -0,0 +1,13 @@
+<?php
+
+// autoload_psr4.php @generated by Composer
+
+$vendorDir = dirname(dirname(__FILE__));
+$baseDir = dirname($vendorDir);
+
+return array(
+    'Workerman\\MySQL\\' => array($vendorDir . '/workerman/mysql/src'),
+    'Workerman\\' => array($vendorDir . '/workerman/workerman'),
+    'GatewayWorker\\' => array($vendorDir . '/workerman/gateway-worker/src'),
+    'GatewayClient\\' => array($vendorDir . '/workerman/gatewayclient'),
+);

+ 52 - 0
GatewayWorker/vendor/composer/autoload_real.php

@@ -0,0 +1,52 @@
+<?php
+
+// autoload_real.php @generated by Composer
+
+class ComposerAutoloaderInit7d081a0cc7b2cb693d20cacb30b82f1a
+{
+    private static $loader;
+
+    public static function loadClassLoader($class)
+    {
+        if ('Composer\Autoload\ClassLoader' === $class) {
+            require __DIR__ . '/ClassLoader.php';
+        }
+    }
+
+    public static function getLoader()
+    {
+        if (null !== self::$loader) {
+            return self::$loader;
+        }
+
+        spl_autoload_register(array('ComposerAutoloaderInit7d081a0cc7b2cb693d20cacb30b82f1a', 'loadClassLoader'), true, true);
+        self::$loader = $loader = new \Composer\Autoload\ClassLoader();
+        spl_autoload_unregister(array('ComposerAutoloaderInit7d081a0cc7b2cb693d20cacb30b82f1a', 'loadClassLoader'));
+
+        $useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
+        if ($useStaticLoader) {
+            require_once __DIR__ . '/autoload_static.php';
+
+            call_user_func(\Composer\Autoload\ComposerStaticInit7d081a0cc7b2cb693d20cacb30b82f1a::getInitializer($loader));
+        } else {
+            $map = require __DIR__ . '/autoload_namespaces.php';
+            foreach ($map as $namespace => $path) {
+                $loader->set($namespace, $path);
+            }
+
+            $map = require __DIR__ . '/autoload_psr4.php';
+            foreach ($map as $namespace => $path) {
+                $loader->setPsr4($namespace, $path);
+            }
+
+            $classMap = require __DIR__ . '/autoload_classmap.php';
+            if ($classMap) {
+                $loader->addClassMap($classMap);
+            }
+        }
+
+        $loader->register(true);
+
+        return $loader;
+    }
+}

+ 49 - 0
GatewayWorker/vendor/composer/autoload_static.php

@@ -0,0 +1,49 @@
+<?php
+
+// autoload_static.php @generated by Composer
+
+namespace Composer\Autoload;
+
+class ComposerStaticInit7d081a0cc7b2cb693d20cacb30b82f1a
+{
+    public static $prefixLengthsPsr4 = array (
+        'W' => 
+        array (
+            'Workerman\\MySQL\\' => 16,
+            'Workerman\\' => 10,
+        ),
+        'G' => 
+        array (
+            'GatewayWorker\\' => 14,
+            'GatewayClient\\' => 14,
+        ),
+    );
+
+    public static $prefixDirsPsr4 = array (
+        'Workerman\\MySQL\\' => 
+        array (
+            0 => __DIR__ . '/..' . '/workerman/mysql/src',
+        ),
+        'Workerman\\' => 
+        array (
+            0 => __DIR__ . '/..' . '/workerman/workerman',
+        ),
+        'GatewayWorker\\' => 
+        array (
+            0 => __DIR__ . '/..' . '/workerman/gateway-worker/src',
+        ),
+        'GatewayClient\\' => 
+        array (
+            0 => __DIR__ . '/..' . '/workerman/gatewayclient',
+        ),
+    );
+
+    public static function getInitializer(ClassLoader $loader)
+    {
+        return \Closure::bind(function () use ($loader) {
+            $loader->prefixLengthsPsr4 = ComposerStaticInit7d081a0cc7b2cb693d20cacb30b82f1a::$prefixLengthsPsr4;
+            $loader->prefixDirsPsr4 = ComposerStaticInit7d081a0cc7b2cb693d20cacb30b82f1a::$prefixDirsPsr4;
+
+        }, null, ClassLoader::class);
+    }
+}

+ 179 - 0
GatewayWorker/vendor/composer/installed.json

@@ -0,0 +1,179 @@
+[
+    {
+        "name": "workerman/gateway-worker",
+        "version": "v3.0.16",
+        "version_normalized": "3.0.16.0",
+        "source": {
+            "type": "git",
+            "url": "https://github.com/walkor/GatewayWorker.git",
+            "reference": "f153c28c76cf60cc882d6b66f89b622176268fb7"
+        },
+        "dist": {
+            "type": "zip",
+            "url": "https://api.github.com/repos/walkor/GatewayWorker/zipball/f153c28c76cf60cc882d6b66f89b622176268fb7",
+            "reference": "f153c28c76cf60cc882d6b66f89b622176268fb7",
+            "shasum": "",
+            "mirrors": [
+                {
+                    "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
+                    "preferred": true
+                }
+            ]
+        },
+        "require": {
+            "workerman/workerman": ">=3.5.0"
+        },
+        "time": "2020-06-04T02:58:35+00:00",
+        "type": "library",
+        "installation-source": "dist",
+        "autoload": {
+            "psr-4": {
+                "GatewayWorker\\": "./src"
+            }
+        },
+        "notification-url": "https://packagist.org/downloads/",
+        "license": [
+            "MIT"
+        ],
+        "homepage": "http://www.workerman.net",
+        "keywords": [
+            "communication",
+            "distributed"
+        ]
+    },
+    {
+        "name": "workerman/gatewayclient",
+        "version": "v3.0.13",
+        "version_normalized": "3.0.13.0",
+        "source": {
+            "type": "git",
+            "url": "https://github.com/walkor/GatewayClient.git",
+            "reference": "6f4e76f38947be5cabca2c6fee367151f248d949"
+        },
+        "dist": {
+            "type": "zip",
+            "url": "https://api.github.com/repos/walkor/GatewayClient/zipball/6f4e76f38947be5cabca2c6fee367151f248d949",
+            "reference": "6f4e76f38947be5cabca2c6fee367151f248d949",
+            "shasum": "",
+            "mirrors": [
+                {
+                    "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
+                    "preferred": true
+                }
+            ]
+        },
+        "time": "2018-09-15T03:03:50+00:00",
+        "type": "library",
+        "installation-source": "dist",
+        "autoload": {
+            "psr-4": {
+                "GatewayClient\\": "./"
+            }
+        },
+        "notification-url": "https://packagist.org/downloads/",
+        "license": [
+            "MIT"
+        ],
+        "homepage": "http://www.workerman.net"
+    },
+    {
+        "name": "workerman/mysql",
+        "version": "v1.0.6",
+        "version_normalized": "1.0.6.0",
+        "source": {
+            "type": "git",
+            "url": "https://github.com/walkor/mysql.git",
+            "reference": "28272aa68f9ea1a482f9bb0cf709d169f772d228"
+        },
+        "dist": {
+            "type": "zip",
+            "url": "https://api.github.com/repos/walkor/mysql/zipball/28272aa68f9ea1a482f9bb0cf709d169f772d228",
+            "reference": "28272aa68f9ea1a482f9bb0cf709d169f772d228",
+            "shasum": "",
+            "mirrors": [
+                {
+                    "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
+                    "preferred": true
+                }
+            ]
+        },
+        "require": {
+            "ext-pdo": "*",
+            "ext-pdo_mysql": "*",
+            "php": ">=5.3"
+        },
+        "time": "2019-08-02T10:43:09+00:00",
+        "type": "library",
+        "installation-source": "dist",
+        "autoload": {
+            "psr-4": {
+                "Workerman\\MySQL\\": "./src"
+            }
+        },
+        "notification-url": "https://packagist.org/downloads/",
+        "license": [
+            "MIT"
+        ],
+        "description": "Long-living MySQL connection for daemon.",
+        "homepage": "http://www.workerman.net",
+        "keywords": [
+            "mysql",
+            "pdo",
+            "pdo_mysql"
+        ]
+    },
+    {
+        "name": "workerman/workerman",
+        "version": "v4.0.6",
+        "version_normalized": "4.0.6.0",
+        "source": {
+            "type": "git",
+            "url": "https://github.com/walkor/Workerman.git",
+            "reference": "14964ed1f3655e10f8bd0bd45e78fb75104fc9cf"
+        },
+        "dist": {
+            "type": "zip",
+            "url": "https://api.github.com/repos/walkor/Workerman/zipball/14964ed1f3655e10f8bd0bd45e78fb75104fc9cf",
+            "reference": "14964ed1f3655e10f8bd0bd45e78fb75104fc9cf",
+            "shasum": "",
+            "mirrors": [
+                {
+                    "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
+                    "preferred": true
+                }
+            ]
+        },
+        "require": {
+            "php": ">=5.3"
+        },
+        "suggest": {
+            "ext-event": "For better performance. "
+        },
+        "time": "2020-06-12T16:21:56+00:00",
+        "type": "library",
+        "installation-source": "dist",
+        "autoload": {
+            "psr-4": {
+                "Workerman\\": "./"
+            }
+        },
+        "notification-url": "https://packagist.org/downloads/",
+        "license": [
+            "MIT"
+        ],
+        "authors": [
+            {
+                "name": "walkor",
+                "email": "walkor@workerman.net",
+                "homepage": "http://www.workerman.net",
+                "role": "Developer"
+            }
+        ],
+        "description": "An asynchronous event driven PHP framework for easily building fast, scalable network applications.",
+        "homepage": "http://www.workerman.net",
+        "keywords": [
+            "asynchronous",
+            "event-loop"
+        ]
+    }
+]

+ 1 - 0
GatewayWorker/vendor/workerman/_home_wwwroot_dsmall.csdeshang.com_GatewayWorker_start.php.pid

@@ -0,0 +1 @@
+5535

+ 4 - 0
GatewayWorker/vendor/workerman/gateway-worker/.gitignore

@@ -0,0 +1,4 @@
+.buildpath
+.project
+.settings
+.idea

+ 21 - 0
GatewayWorker/vendor/workerman/gateway-worker/MIT-LICENSE.txt

@@ -0,0 +1,21 @@
+The MIT License
+
+Copyright (c) 2009-2015 walkor<walkor@workerman.net> and contributors (see https://github.com/walkor/workerman/contributors)
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.

+ 38 - 0
GatewayWorker/vendor/workerman/gateway-worker/README.md

@@ -0,0 +1,38 @@
+GatewayWorker 
+=================
+
+GatewayWorker基于[Workerman](https://github.com/walkor/Workerman)开发的一个项目框架,用于快速开发长连接应用,例如app推送服务端、即时IM服务端、游戏服务端、物联网、智能家居等等。
+
+GatewayWorker使用经典的Gateway和Worker进程模型。Gateway进程负责维持客户端连接,并转发客户端的数据给Worker进程处理;Worker进程负责处理实际的业务逻辑,并将结果推送给对应的客户端。Gateway服务和Worker服务可以分开部署在不同的服务器上,实现分布式集群。
+
+GatewayWorker提供非常方便的API,可以全局广播数据、可以向某个群体广播数据、也可以向某个特定客户端推送数据。配合Workerman的定时器,也可以定时推送数据。
+
+快速开始
+======
+开发者可以从一个简单的demo开始(demo中包含了GatewayWorker内核,以及start_gateway.php start_business.php等启动入口文件)<br>
+[点击这里下载demo](http://www.workerman.net/download/GatewayWorker.zip)。<br>
+demo说明见源码readme。
+
+手册
+=======
+http://www.workerman.net/gatewaydoc/
+
+安装内核
+=======
+
+只安装GatewayWorker内核文件(不包含start_gateway.php start_businessworker.php等启动入口文件)
+```
+composer require workerman/gateway-worker
+```
+
+使用GatewayWorker开发的项目
+=======
+## [tadpole](http://kedou.workerman.net/)  
+[Live demo](http://kedou.workerman.net/)  
+[Source code](https://github.com/walkor/workerman)  
+![workerman todpole](http://www.workerman.net/img/workerman-todpole.png)   
+
+## [chat room](http://chat.workerman.net/)  
+[Live demo](http://chat.workerman.net/)  
+[Source code](https://github.com/walkor/workerman-chat)  
+![workerman-chat](http://www.workerman.net/img/workerman-chat.png)  

+ 12 - 0
GatewayWorker/vendor/workerman/gateway-worker/composer.json

@@ -0,0 +1,12 @@
+{
+    "name"  : "workerman/gateway-worker",
+    "keywords": ["distributed","communication"],
+    "homepage": "http://www.workerman.net",
+    "license" : "MIT",
+    "require": {
+        "workerman/workerman" : ">=3.5.0"
+    },
+    "autoload": {
+        "psr-4": {"GatewayWorker\\": "./src"}
+    }
+}

+ 561 - 0
GatewayWorker/vendor/workerman/gateway-worker/src/BusinessWorker.php

@@ -0,0 +1,561 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link      http://www.workerman.net/
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace GatewayWorker;
+
+use Workerman\Connection\TcpConnection;
+
+use Workerman\Worker;
+use Workerman\Lib\Timer;
+use Workerman\Connection\AsyncTcpConnection;
+use GatewayWorker\Protocols\GatewayProtocol;
+use GatewayWorker\Lib\Context;
+
+/**
+ *
+ * BusinessWorker 用于处理Gateway转发来的数据
+ *
+ * @author walkor<walkor@workerman.net>
+ *
+ */
+class BusinessWorker extends Worker
+{
+    /**
+     * 保存与 gateway 的连接 connection 对象
+     *
+     * @var array
+     */
+    public $gatewayConnections = array();
+
+    /**
+     * 注册中心地址
+     *
+     * @var string|array
+     */
+    public $registerAddress = '127.0.0.1:1236';
+
+    /**
+     * 事件处理类,默认是 Event 类
+     *
+     * @var string
+     */
+    public $eventHandler = 'Events';
+
+    /**
+     * 业务超时时间,可用来定位程序卡在哪里
+     *
+     * @var int
+     */
+    public $processTimeout = 30;
+
+    /**
+     * 业务超时时间,可用来定位程序卡在哪里
+     *
+     * @var callable
+     */
+    public $processTimeoutHandler = '\\Workerman\\Worker::log';
+    
+    /**
+     * 秘钥
+     *
+     * @var string
+     */
+    public $secretKey = '';
+
+    /**
+     * businessWorker进程将消息转发给gateway进程的发送缓冲区大小
+     *
+     * @var int
+     */
+    public $sendToGatewayBufferSize = 10240000;
+
+    /**
+     * 保存用户设置的 worker 启动回调
+     *
+     * @var callback
+     */
+    protected $_onWorkerStart = null;
+
+    /**
+     * 保存用户设置的 workerReload 回调
+     *
+     * @var callback
+     */
+    protected $_onWorkerReload = null;
+    
+    /**
+     * 保存用户设置的 workerStop 回调
+     *
+     * @var callback
+     */
+    protected $_onWorkerStop= null;
+
+    /**
+     * 到注册中心的连接
+     *
+     * @var AsyncTcpConnection
+     */
+    protected $_registerConnection = null;
+
+    /**
+     * 处于连接状态的 gateway 通讯地址
+     *
+     * @var array
+     */
+    protected $_connectingGatewayAddresses = array();
+
+    /**
+     * 所有 geteway 内部通讯地址
+     *
+     * @var array
+     */
+    protected $_gatewayAddresses = array();
+
+    /**
+     * 等待连接个 gateway 地址
+     *
+     * @var array
+     */
+    protected $_waitingConnectGatewayAddresses = array();
+
+    /**
+     * Event::onConnect 回调
+     *
+     * @var callback
+     */
+    protected $_eventOnConnect = null;
+
+    /**
+     * Event::onMessage 回调
+     *
+     * @var callback
+     */
+    protected $_eventOnMessage = null;
+
+    /**
+     * Event::onClose 回调
+     *
+     * @var callback
+     */
+    protected $_eventOnClose = null;
+
+    /**
+     * websocket回调
+     *
+     * @var null
+     */
+    protected $_eventOnWebSocketConnect = null;
+
+    /**
+     * SESSION 版本缓存
+     *
+     * @var array
+     */
+    protected $_sessionVersion = array();
+
+    /**
+     * 用于保持长连接的心跳时间间隔
+     *
+     * @var int
+     */
+    const PERSISTENCE_CONNECTION_PING_INTERVAL = 25;
+
+    /**
+     * 构造函数
+     *
+     * @param string $socket_name
+     * @param array  $context_option
+     */
+    public function __construct($socket_name = '', $context_option = array())
+    {
+        parent::__construct($socket_name, $context_option);
+        $backrace                = debug_backtrace();
+        $this->_autoloadRootPath = dirname($backrace[0]['file']);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function run()
+    {
+        $this->_onWorkerStart  = $this->onWorkerStart;
+        $this->_onWorkerReload = $this->onWorkerReload;
+        $this->_onWorkerStop = $this->onWorkerStop;
+        $this->onWorkerStop   = array($this, 'onWorkerStop');
+        $this->onWorkerStart   = array($this, 'onWorkerStart');
+        $this->onWorkerReload  = array($this, 'onWorkerReload');
+        parent::run();
+    }
+
+    /**
+     * 当进程启动时一些初始化工作
+     *
+     * @return void
+     */
+    protected function onWorkerStart()
+    {
+        if (!class_exists('\Protocols\GatewayProtocol')) {
+            class_alias('GatewayWorker\Protocols\GatewayProtocol', 'Protocols\GatewayProtocol');
+        }
+
+        if (!is_array($this->registerAddress)) {
+            $this->registerAddress = array($this->registerAddress);
+        }
+        $this->connectToRegister();
+
+        \GatewayWorker\Lib\Gateway::setBusinessWorker($this);
+        \GatewayWorker\Lib\Gateway::$secretKey = $this->secretKey;
+        if ($this->_onWorkerStart) {
+            call_user_func($this->_onWorkerStart, $this);
+        }
+        
+        if (is_callable($this->eventHandler . '::onWorkerStart')) {
+            call_user_func($this->eventHandler . '::onWorkerStart', $this);
+        }
+
+        if (function_exists('pcntl_signal')) {
+            // 业务超时信号处理
+            pcntl_signal(SIGALRM, array($this, 'timeoutHandler'), false);
+        } else {
+            $this->processTimeout = 0;
+        }
+
+        // 设置回调
+        if (is_callable($this->eventHandler . '::onConnect')) {
+            $this->_eventOnConnect = $this->eventHandler . '::onConnect';
+        }
+
+        if (is_callable($this->eventHandler . '::onMessage')) {
+            $this->_eventOnMessage = $this->eventHandler . '::onMessage';
+        } else {
+            echo "Waring: {$this->eventHandler}::onMessage is not callable\n";
+        }
+
+        if (is_callable($this->eventHandler . '::onClose')) {
+            $this->_eventOnClose = $this->eventHandler . '::onClose';
+        }
+
+        if (is_callable($this->eventHandler . '::onWebSocketConnect')) {
+            $this->_eventOnWebSocketConnect = $this->eventHandler . '::onWebSocketConnect';
+        }
+
+    }
+
+    /**
+     * onWorkerReload 回调
+     *
+     * @param Worker $worker
+     */
+    protected function onWorkerReload($worker)
+    {
+        // 防止进程立刻退出
+        $worker->reloadable = false;
+        // 延迟 0.05 秒退出,避免 BusinessWorker 瞬间全部退出导致没有可用的 BusinessWorker 进程
+        Timer::add(0.05, array('Workerman\Worker', 'stopAll'));
+        // 执行用户定义的 onWorkerReload 回调
+        if ($this->_onWorkerReload) {
+            call_user_func($this->_onWorkerReload, $this);
+        }
+    }
+    
+    /**
+     * 当进程关闭时一些清理工作
+     *
+     * @return void
+     */
+    protected function onWorkerStop()
+    {
+        if ($this->_onWorkerStop) {
+            call_user_func($this->_onWorkerStop, $this);
+        }
+        if (is_callable($this->eventHandler . '::onWorkerStop')) {
+            call_user_func($this->eventHandler . '::onWorkerStop', $this);
+        }
+    }
+
+    /**
+     * 连接服务注册中心
+     * 
+     * @return void
+     */
+    public function connectToRegister()
+    {
+        foreach ($this->registerAddress as $register_address) {
+            $register_connection = new AsyncTcpConnection("text://{$register_address}");
+            $secret_key = $this->secretKey;
+            $register_connection->onConnect = function () use ($register_connection, $secret_key, $register_address) {
+                $register_connection->send('{"event":"worker_connect","secret_key":"' . $secret_key . '"}');
+                // 如果Register服务器不在本地服务器,则需要保持心跳
+                if (strpos($register_address, '127.0.0.1') !== 0) {
+                    $register_connection->ping_timer = Timer::add(self::PERSISTENCE_CONNECTION_PING_INTERVAL, function () use ($register_connection) {
+                        $register_connection->send('{"event":"ping"}');
+                    });
+                }
+            };
+            $register_connection->onClose = function ($register_connection) {
+                if(!empty($register_connection->ping_timer)) {
+                    Timer::del($register_connection->ping_timer);
+                }
+                $register_connection->reconnect(1);
+            };
+            $register_connection->onMessage = array($this, 'onRegisterConnectionMessage');
+            $register_connection->connect();
+        }
+    }
+
+
+    /**
+     * 当注册中心发来消息时
+     *
+     * @return void
+     */
+    public function onRegisterConnectionMessage($register_connection, $data)
+    {
+        $data = json_decode($data, true);
+        if (!isset($data['event'])) {
+            echo "Received bad data from Register\n";
+            return;
+        }
+        $event = $data['event'];
+        switch ($event) {
+            case 'broadcast_addresses':
+                if (!is_array($data['addresses'])) {
+                    echo "Received bad data from Register. Addresses empty\n";
+                    return;
+                }
+                $addresses               = $data['addresses'];
+                $this->_gatewayAddresses = array();
+                foreach ($addresses as $addr) {
+                    $this->_gatewayAddresses[$addr] = $addr;
+                }
+                $this->checkGatewayConnections($addresses);
+                break;
+            default:
+                echo "Receive bad event:$event from Register.\n";
+        }
+    }
+
+    /**
+     * 当 gateway 转发来数据时
+     *
+     * @param TcpConnection $connection
+     * @param mixed         $data
+     */
+    public function onGatewayMessage($connection, $data)
+    {
+        $cmd = $data['cmd'];
+        if ($cmd === GatewayProtocol::CMD_PING) {
+            return;
+        }
+        // 上下文数据
+        Context::$client_ip     = $data['client_ip'];
+        Context::$client_port   = $data['client_port'];
+        Context::$local_ip      = $data['local_ip'];
+        Context::$local_port    = $data['local_port'];
+        Context::$connection_id = $data['connection_id'];
+        Context::$client_id     = Context::addressToClientId($data['local_ip'], $data['local_port'],
+            $data['connection_id']);
+        // $_SERVER 变量
+        $_SERVER = array(
+            'REMOTE_ADDR'       => long2ip($data['client_ip']),
+            'REMOTE_PORT'       => $data['client_port'],
+            'GATEWAY_ADDR'      => long2ip($data['local_ip']),
+            'GATEWAY_PORT'      => $data['gateway_port'],
+            'GATEWAY_CLIENT_ID' => Context::$client_id,
+        );
+        // 检查session版本,如果是过期的session数据则拉取最新的数据
+        if ($cmd !== GatewayProtocol::CMD_ON_CLOSE && isset($this->_sessionVersion[Context::$client_id]) && $this->_sessionVersion[Context::$client_id] !== crc32($data['ext_data'])) {
+            $_SESSION = Context::$old_session = \GatewayWorker\Lib\Gateway::getSession(Context::$client_id);
+            $this->_sessionVersion[Context::$client_id] = crc32($data['ext_data']);
+        } else {
+            if (!isset($this->_sessionVersion[Context::$client_id])) {
+                $this->_sessionVersion[Context::$client_id] = crc32($data['ext_data']);
+            }
+            // 尝试解析 session
+            if ($data['ext_data'] != '') {
+                Context::$old_session = $_SESSION = Context::sessionDecode($data['ext_data']);
+            } else {
+                Context::$old_session = $_SESSION = null;
+            }
+        }
+
+        if ($this->processTimeout) {
+            pcntl_alarm($this->processTimeout);
+        }
+        // 尝试执行 Event::onConnection、Event::onMessage、Event::onClose
+        switch ($cmd) {
+            case GatewayProtocol::CMD_ON_CONNECT:
+                if ($this->_eventOnConnect) {
+                    call_user_func($this->_eventOnConnect, Context::$client_id);
+                }
+                break;
+            case GatewayProtocol::CMD_ON_MESSAGE:
+                if ($this->_eventOnMessage) {
+                    call_user_func($this->_eventOnMessage, Context::$client_id, $data['body']);
+                }
+                break;
+            case GatewayProtocol::CMD_ON_CLOSE:
+                unset($this->_sessionVersion[Context::$client_id]);
+                if ($this->_eventOnClose) {
+                    call_user_func($this->_eventOnClose, Context::$client_id);
+                }
+                break;
+            case GatewayProtocol::CMD_ON_WEBSOCKET_CONNECT:
+                if ($this->_eventOnWebSocketConnect) {
+                    call_user_func($this->_eventOnWebSocketConnect, Context::$client_id, $data['body']);
+                }
+                break;
+        }
+        if ($this->processTimeout) {
+            pcntl_alarm(0);
+        }
+        
+        // session 必须是数组
+        if ($_SESSION !== null && !is_array($_SESSION)) {
+            throw new \Exception('$_SESSION must be an array. But $_SESSION=' . var_export($_SESSION, true) . ' is not array.');
+        }
+
+        // 判断 session 是否被更改
+        if ($_SESSION !== Context::$old_session && $cmd !== GatewayProtocol::CMD_ON_CLOSE) {
+            $session_str_now = $_SESSION !== null ? Context::sessionEncode($_SESSION) : '';
+            \GatewayWorker\Lib\Gateway::setSocketSession(Context::$client_id, $session_str_now);
+            $this->_sessionVersion[Context::$client_id] = crc32($session_str_now);
+        }
+
+        Context::clear();
+    }
+
+    /**
+     * 当与 Gateway 的连接断开时触发
+     *
+     * @param TcpConnection $connection
+     * @return  void
+     */
+    public function onGatewayClose($connection)
+    {
+        $addr = $connection->remoteAddress;
+        unset($this->gatewayConnections[$addr], $this->_connectingGatewayAddresses[$addr]);
+        if (isset($this->_gatewayAddresses[$addr]) && !isset($this->_waitingConnectGatewayAddresses[$addr])) {
+            Timer::add(1, array($this, 'tryToConnectGateway'), array($addr), false);
+            $this->_waitingConnectGatewayAddresses[$addr] = $addr;
+        }
+    }
+
+    /**
+     * 尝试连接 Gateway 内部通讯地址
+     *
+     * @param string $addr
+     */
+    public function tryToConnectGateway($addr)
+    {
+        if (!isset($this->gatewayConnections[$addr]) && !isset($this->_connectingGatewayAddresses[$addr]) && isset($this->_gatewayAddresses[$addr])) {
+            $gateway_connection                    = new AsyncTcpConnection("GatewayProtocol://$addr");
+            $gateway_connection->remoteAddress     = $addr;
+            $gateway_connection->onConnect         = array($this, 'onConnectGateway');
+            $gateway_connection->onMessage         = array($this, 'onGatewayMessage');
+            $gateway_connection->onClose           = array($this, 'onGatewayClose');
+            $gateway_connection->onError           = array($this, 'onGatewayError');
+            $gateway_connection->maxSendBufferSize = $this->sendToGatewayBufferSize;
+            if (TcpConnection::$defaultMaxSendBufferSize == $gateway_connection->maxSendBufferSize) {
+                $gateway_connection->maxSendBufferSize = 50 * 1024 * 1024;
+            }
+            $gateway_data         = GatewayProtocol::$empty;
+            $gateway_data['cmd']  = GatewayProtocol::CMD_WORKER_CONNECT;
+            $gateway_data['body'] = json_encode(array(
+                'worker_key' =>"{$this->name}:{$this->id}", 
+                'secret_key' => $this->secretKey,
+            ));
+            $gateway_connection->send($gateway_data);
+            $gateway_connection->connect();
+            $this->_connectingGatewayAddresses[$addr] = $addr;
+        }
+        unset($this->_waitingConnectGatewayAddresses[$addr]);
+    }
+
+    /**
+     * 检查 gateway 的通信端口是否都已经连
+     * 如果有未连接的端口,则尝试连接
+     *
+     * @param array $addresses_list
+     */
+    public function checkGatewayConnections($addresses_list)
+    {
+        if (empty($addresses_list)) {
+            return;
+        }
+        foreach ($addresses_list as $addr) {
+            if (!isset($this->_waitingConnectGatewayAddresses[$addr])) {
+                $this->tryToConnectGateway($addr);
+            }
+        }
+    }
+
+    /**
+     * 当连接上 gateway 的通讯端口时触发
+     * 将连接 connection 对象保存起来
+     *
+     * @param TcpConnection $connection
+     * @return void
+     */
+    public function onConnectGateway($connection)
+    {
+        $this->gatewayConnections[$connection->remoteAddress] = $connection;
+        unset($this->_connectingGatewayAddresses[$connection->remoteAddress], $this->_waitingConnectGatewayAddresses[$connection->remoteAddress]);
+    }
+
+    /**
+     * 当与 gateway 的连接出现错误时触发
+     *
+     * @param TcpConnection $connection
+     * @param int           $error_no
+     * @param string        $error_msg
+     */
+    public function onGatewayError($connection, $error_no, $error_msg)
+    {
+        echo "GatewayConnection Error : $error_no ,$error_msg\n";
+    }
+
+    /**
+     * 获取所有 Gateway 内部通讯地址
+     *
+     * @return array
+     */
+    public function getAllGatewayAddresses()
+    {
+        return $this->_gatewayAddresses;
+    }
+
+    /**
+     * 业务超时回调
+     *
+     * @param int $signal
+     * @throws \Exception
+     */
+    public function timeoutHandler($signal)
+    {
+        switch ($signal) {
+            // 超时时钟
+            case SIGALRM:
+                // 超时异常
+                $e         = new \Exception("process_timeout", 506);
+                $trace_str = $e->getTraceAsString();
+                // 去掉第一行timeoutHandler的调用栈
+                $trace_str = $e->getMessage() . ":\n" . substr($trace_str, strpos($trace_str, "\n") + 1) . "\n";
+                // 开发者没有设置超时处理函数,或者超时处理函数返回空则执行退出
+                if (!$this->processTimeoutHandler || !call_user_func($this->processTimeoutHandler, $trace_str, $e)) {
+                    Worker::stopAll();
+                }
+                break;
+        }
+    }
+}

+ 1027 - 0
GatewayWorker/vendor/workerman/gateway-worker/src/Gateway.php

@@ -0,0 +1,1027 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link      http://www.workerman.net/
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace GatewayWorker;
+
+use GatewayWorker\Lib\Context;
+
+use Workerman\Connection\TcpConnection;
+
+use Workerman\Worker;
+use Workerman\Lib\Timer;
+use Workerman\Autoloader;
+use Workerman\Connection\AsyncTcpConnection;
+use GatewayWorker\Protocols\GatewayProtocol;
+
+/**
+ *
+ * Gateway,基于Worker 开发
+ * 用于转发客户端的数据给Worker处理,以及转发Worker的数据给客户端
+ *
+ * @author walkor<walkor@workerman.net>
+ *
+ */
+class Gateway extends Worker
+{
+    /**
+     * 版本
+     *
+     * @var string
+     */
+    const VERSION = '3.0.16';
+
+    /**
+     * 本机 IP
+     *  单机部署默认 127.0.0.1,如果是分布式部署,需要设置成本机 IP
+     *
+     * @var string
+     */
+    public $lanIp = '127.0.0.1';
+
+    /**
+     * 本机端口
+     *
+     * @var string
+     */
+    public $lanPort = 0;
+
+    /**
+     * gateway 内部通讯起始端口,每个 gateway 实例应该都不同,步长1000
+     *
+     * @var int
+     */
+    public $startPort = 2000;
+
+    /**
+     * 注册服务地址,用于注册 Gateway BusinessWorker,使之能够通讯
+     *
+     * @var string|array
+     */
+    public $registerAddress = '127.0.0.1:1236';
+
+    /**
+     * 是否可以平滑重启,gateway 不能平滑重启,否则会导致连接断开
+     *
+     * @var bool
+     */
+    public $reloadable = false;
+
+    /**
+     * 心跳时间间隔
+     *
+     * @var int
+     */
+    public $pingInterval = 0;
+
+    /**
+     * $pingNotResponseLimit * $pingInterval 时间内,客户端未发送任何数据,断开客户端连接
+     *
+     * @var int
+     */
+    public $pingNotResponseLimit = 0;
+
+    /**
+     * 服务端向客户端发送的心跳数据
+     *
+     * @var string
+     */
+    public $pingData = '';
+    
+    /**
+     * 秘钥
+     *
+     * @var string
+     */
+    public $secretKey = '';
+
+    /**
+     * 路由函数
+     *
+     * @var callback
+     */
+    public $router = null;
+
+
+    /**
+     * gateway进程转发给businessWorker进程的发送缓冲区大小
+     *
+     * @var int
+     */
+    public $sendToWorkerBufferSize = 10240000;
+
+    /**
+     * gateway进程将数据发给客户端时每个客户端发送缓冲区大小
+     *
+     * @var int
+     */
+    public $sendToClientBufferSize = 1024000;
+
+    /**
+     * 协议加速
+     *
+     * @var bool
+     */
+    public $protocolAccelerate = false;
+
+    /**
+     * 保存客户端的所有 connection 对象
+     *
+     * @var array
+     */
+    protected $_clientConnections = array();
+
+    /**
+     * uid 到 connection 的映射,一对多关系
+     */
+    protected $_uidConnections = array();
+
+    /**
+     * group 到 connection 的映射,一对多关系
+     *
+     * @var array
+     */
+    protected $_groupConnections = array();
+
+    /**
+     * 保存所有 worker 的内部连接的 connection 对象
+     *
+     * @var array
+     */
+    protected $_workerConnections = array();
+
+    /**
+     * gateway 内部监听 worker 内部连接的 worker
+     *
+     * @var Worker
+     */
+    protected $_innerTcpWorker = null;
+
+    /**
+     * 当 worker 启动时
+     *
+     * @var callback
+     */
+    protected $_onWorkerStart = null;
+
+    /**
+     * 当有客户端连接时
+     *
+     * @var callback
+     */
+    protected $_onConnect = null;
+
+    /**
+     * 当客户端发来消息时
+     *
+     * @var callback
+     */
+    protected $_onMessage = null;
+
+    /**
+     * 当客户端连接关闭时
+     *
+     * @var callback
+     */
+    protected $_onClose = null;
+
+    /**
+     * 当 worker 停止时
+     *
+     * @var callback
+     */
+    protected $_onWorkerStop = null;
+
+    /**
+     * 进程启动时间
+     *
+     * @var int
+     */
+    protected $_startTime = 0;
+
+    /**
+     * gateway 监听的端口
+     *
+     * @var int
+     */
+    protected $_gatewayPort = 0;
+    
+    /**
+     * connectionId 记录器
+     * @var int
+     */
+    protected static $_connectionIdRecorder = 0;
+
+    /**
+     * 用于保持长连接的心跳时间间隔
+     *
+     * @var int
+     */
+    const PERSISTENCE_CONNECTION_PING_INTERVAL = 25;
+
+    /**
+     * 构造函数
+     *
+     * @param string $socket_name
+     * @param array  $context_option
+     */
+    public function __construct($socket_name, $context_option = array())
+    {
+        parent::__construct($socket_name, $context_option);
+		$this->_gatewayPort = substr(strrchr($socket_name,':'),1);
+        $this->router = array("\\GatewayWorker\\Gateway", 'routerBind');
+
+        $backtrace               = debug_backtrace();
+        $this->_autoloadRootPath = dirname($backtrace[0]['file']);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function run()
+    {
+        // 保存用户的回调,当对应的事件发生时触发
+        $this->_onWorkerStart = $this->onWorkerStart;
+        $this->onWorkerStart  = array($this, 'onWorkerStart');
+        // 保存用户的回调,当对应的事件发生时触发
+        $this->_onConnect = $this->onConnect;
+        $this->onConnect  = array($this, 'onClientConnect');
+
+        // onMessage禁止用户设置回调
+        $this->onMessage = array($this, 'onClientMessage');
+
+        // 保存用户的回调,当对应的事件发生时触发
+        $this->_onClose = $this->onClose;
+        $this->onClose  = array($this, 'onClientClose');
+        // 保存用户的回调,当对应的事件发生时触发
+        $this->_onWorkerStop = $this->onWorkerStop;
+        $this->onWorkerStop  = array($this, 'onWorkerStop');
+
+        if (!is_array($this->registerAddress)) {
+            $this->registerAddress = array($this->registerAddress);
+        }
+
+        // 记录进程启动的时间
+        $this->_startTime = time();
+        // 运行父方法
+        parent::run();
+    }
+
+    /**
+     * 当客户端发来数据时,转发给worker处理
+     *
+     * @param TcpConnection $connection
+     * @param mixed         $data
+     */
+    public function onClientMessage($connection, $data)
+    {
+        $connection->pingNotResponseCount = -1;
+        $this->sendToWorker(GatewayProtocol::CMD_ON_MESSAGE, $connection, $data);
+    }
+
+    /**
+     * 当客户端连接上来时,初始化一些客户端的数据
+     * 包括全局唯一的client_id、初始化session等
+     *
+     * @param TcpConnection $connection
+     */
+    public function onClientConnect($connection)
+    {
+        $connection->id = self::generateConnectionId();
+        // 保存该连接的内部通讯的数据包报头,避免每次重新初始化
+        $connection->gatewayHeader = array(
+            'local_ip'      => ip2long($this->lanIp),
+            'local_port'    => $this->lanPort,
+            'client_ip'     => ip2long($connection->getRemoteIp()),
+            'client_port'   => $connection->getRemotePort(),
+            'gateway_port'  => $this->_gatewayPort,
+            'connection_id' => $connection->id,
+            'flag'          => 0,
+        );
+        // 连接的 session
+        $connection->session                       = '';
+        // 该连接的心跳参数
+        $connection->pingNotResponseCount          = -1;
+        // 该链接发送缓冲区大小
+        $connection->maxSendBufferSize             = $this->sendToClientBufferSize;
+        // 保存客户端连接 connection 对象
+        $this->_clientConnections[$connection->id] = $connection;
+
+        // 如果用户有自定义 onConnect 回调,则执行
+        if ($this->_onConnect) {
+            call_user_func($this->_onConnect, $connection);
+            if (isset($connection->onWebSocketConnect)) {
+                $connection->_onWebSocketConnect = $connection->onWebSocketConnect;
+            }
+        }
+        if ($connection->protocol === '\Workerman\Protocols\Websocket') {
+            $connection->onWebSocketConnect = array($this, 'onWebsocketConnect');
+        }
+
+        $this->sendToWorker(GatewayProtocol::CMD_ON_CONNECT, $connection);
+    }
+
+    /**
+     * websocket握手时触发
+     *
+     * @param $connection
+     * @param $http_buffer
+     */
+    public function onWebsocketConnect($connection, $http_buffer)
+    {
+        if (isset($connection->_onWebSocketConnect)) {
+            call_user_func($connection->_onWebSocketConnect, $connection, $http_buffer);
+            unset($connection->_onWebSocketConnect);
+        }
+        $this->sendToWorker(GatewayProtocol::CMD_ON_WEBSOCKET_CONNECT, $connection, array('get' => $_GET, 'server' => $_SERVER, 'cookie' => $_COOKIE));
+    }
+    
+    /**
+     * 生成connection id
+     * @return int
+     */
+    protected function generateConnectionId()
+    {
+        $max_unsigned_int = 4294967295;
+        if (self::$_connectionIdRecorder >= $max_unsigned_int) {
+            self::$_connectionIdRecorder = 0;
+        }
+        while(++self::$_connectionIdRecorder <= $max_unsigned_int) {
+            if(!isset($this->_clientConnections[self::$_connectionIdRecorder])) {
+                break;
+            }
+        }
+        return self::$_connectionIdRecorder;
+    }
+
+    /**
+     * 发送数据给 worker 进程
+     *
+     * @param int           $cmd
+     * @param TcpConnection $connection
+     * @param mixed         $body
+     * @return bool
+     */
+    protected function sendToWorker($cmd, $connection, $body = '')
+    {
+        $gateway_data             = $connection->gatewayHeader;
+        $gateway_data['cmd']      = $cmd;
+        $gateway_data['body']     = $body;
+        $gateway_data['ext_data'] = $connection->session;
+        if ($this->_workerConnections) {
+            // 调用路由函数,选择一个worker把请求转发给它
+            /** @var TcpConnection $worker_connection */
+            $worker_connection = call_user_func($this->router, $this->_workerConnections, $connection, $cmd, $body);
+            if (false === $worker_connection->send($gateway_data)) {
+                $msg = "SendBufferToWorker fail. May be the send buffer are overflow. See http://doc2.workerman.net/send-buffer-overflow.html";
+                static::log($msg);
+                return false;
+            }
+        } // 没有可用的 worker
+        else {
+            // gateway 启动后 1-2 秒内 SendBufferToWorker fail 是正常现象,因为与 worker 的连接还没建立起来,
+            // 所以不记录日志,只是关闭连接
+            $time_diff = 2;
+            if (time() - $this->_startTime >= $time_diff) {
+                $msg = 'SendBufferToWorker fail. The connections between Gateway and BusinessWorker are not ready. See http://doc2.workerman.net/send-buffer-to-worker-fail.html';
+                static::log($msg);
+            }
+            $connection->destroy();
+            return false;
+        }
+        return true;
+    }
+
+    /**
+     * 随机路由,返回 worker connection 对象
+     *
+     * @param array         $worker_connections
+     * @param TcpConnection $client_connection
+     * @param int           $cmd
+     * @param mixed         $buffer
+     * @return TcpConnection
+     */
+    public static function routerRand($worker_connections, $client_connection, $cmd, $buffer)
+    {
+        return $worker_connections[array_rand($worker_connections)];
+    }
+
+    /**
+     * client_id 与 worker 绑定
+     *
+     * @param array         $worker_connections
+     * @param TcpConnection $client_connection
+     * @param int           $cmd
+     * @param mixed         $buffer
+     * @return TcpConnection
+     */
+    public static function routerBind($worker_connections, $client_connection, $cmd, $buffer)
+    {
+        if (!isset($client_connection->businessworker_address) || !isset($worker_connections[$client_connection->businessworker_address])) {
+            $client_connection->businessworker_address = array_rand($worker_connections);
+        }
+        return $worker_connections[$client_connection->businessworker_address];
+    }
+
+    /**
+     * 当客户端关闭时
+     *
+     * @param TcpConnection $connection
+     */
+    public function onClientClose($connection)
+    {
+        // 尝试通知 worker,触发 Event::onClose
+        $this->sendToWorker(GatewayProtocol::CMD_ON_CLOSE, $connection);
+        unset($this->_clientConnections[$connection->id]);
+        // 清理 uid 数据
+        if (!empty($connection->uid)) {
+            $uid = $connection->uid;
+            unset($this->_uidConnections[$uid][$connection->id]);
+            if (empty($this->_uidConnections[$uid])) {
+                unset($this->_uidConnections[$uid]);
+            }
+        }
+        // 清理 group 数据
+        if (!empty($connection->groups)) {
+            foreach ($connection->groups as $group) {
+                unset($this->_groupConnections[$group][$connection->id]);
+                if (empty($this->_groupConnections[$group])) {
+                    unset($this->_groupConnections[$group]);
+                }
+            }
+        }
+        // 触发 onClose
+        if ($this->_onClose) {
+            call_user_func($this->_onClose, $connection);
+        }
+    }
+
+    /**
+     * 当 Gateway 启动的时候触发的回调函数
+     *
+     * @return void
+     */
+    public function onWorkerStart()
+    {
+        // 分配一个内部通讯端口
+        $this->lanPort = $this->startPort + $this->id;
+
+        // 如果有设置心跳,则定时执行
+        if ($this->pingInterval > 0) {
+            $timer_interval = $this->pingNotResponseLimit > 0 ? $this->pingInterval / 2 : $this->pingInterval;
+            Timer::add($timer_interval, array($this, 'ping'));
+        }
+
+        // 如果BusinessWorker ip不是127.0.0.1,则需要加gateway到BusinessWorker的心跳
+        if ($this->lanIp !== '127.0.0.1') {
+            Timer::add(self::PERSISTENCE_CONNECTION_PING_INTERVAL, array($this, 'pingBusinessWorker'));
+        }
+
+        if (!class_exists('\Protocols\GatewayProtocol')) {
+            class_alias('GatewayWorker\Protocols\GatewayProtocol', 'Protocols\GatewayProtocol');
+        }
+
+        // 初始化 gateway 内部的监听,用于监听 worker 的连接已经连接上发来的数据
+        $this->_innerTcpWorker = new Worker("GatewayProtocol://{$this->lanIp}:{$this->lanPort}");
+        $this->_innerTcpWorker->listen();
+	$this->_innerTcpWorker->name = 'GatewayInnerWorker';
+
+        // 重新设置自动加载根目录
+        Autoloader::setRootPath($this->_autoloadRootPath);
+
+        // 设置内部监听的相关回调
+        $this->_innerTcpWorker->onMessage = array($this, 'onWorkerMessage');
+
+        $this->_innerTcpWorker->onConnect = array($this, 'onWorkerConnect');
+        $this->_innerTcpWorker->onClose   = array($this, 'onWorkerClose');
+
+        // 注册 gateway 的内部通讯地址,worker 去连这个地址,以便 gateway 与 worker 之间建立起 TCP 长连接
+        $this->registerAddress();
+
+        if ($this->_onWorkerStart) {
+            call_user_func($this->_onWorkerStart, $this);
+        }
+    }
+
+
+    /**
+     * 当 worker 通过内部通讯端口连接到 gateway 时
+     *
+     * @param TcpConnection $connection
+     */
+    public function onWorkerConnect($connection)
+    {
+        $connection->maxSendBufferSize = $this->sendToWorkerBufferSize;
+        $connection->authorized = $this->secretKey ? false : true;
+    }
+
+    /**
+     * 当 worker 发来数据时
+     *
+     * @param TcpConnection $connection
+     * @param mixed         $data
+     * @throws \Exception
+     *
+     * @return void
+     */
+    public function onWorkerMessage($connection, $data)
+    {
+        $cmd = $data['cmd'];
+        if (empty($connection->authorized) && $cmd !== GatewayProtocol::CMD_WORKER_CONNECT && $cmd !== GatewayProtocol::CMD_GATEWAY_CLIENT_CONNECT) {
+            self::log("Unauthorized request from " . $connection->getRemoteIp() . ":" . $connection->getRemotePort());
+            $connection->close();
+            return;
+        }
+        switch ($cmd) {
+            // BusinessWorker连接Gateway
+            case GatewayProtocol::CMD_WORKER_CONNECT:
+                $worker_info = json_decode($data['body'], true);
+                if ($worker_info['secret_key'] !== $this->secretKey) {
+                    self::log("Gateway: Worker key does not match ".var_export($this->secretKey, true)." !== ". var_export($this->secretKey));
+                    $connection->close();
+                    return;
+                }
+                $key = $connection->getRemoteIp() . ':' . $worker_info['worker_key'];
+                // 在一台服务器上businessWorker->name不能相同
+                if (isset($this->_workerConnections[$key])) {
+                    self::log("Gateway: Worker->name conflict. Key:{$key}");
+		            $connection->close();
+                    return;
+                }
+		        $connection->key = $key;
+                $this->_workerConnections[$key] = $connection;
+                $connection->authorized = true;
+                return;
+            // GatewayClient连接Gateway
+            case GatewayProtocol::CMD_GATEWAY_CLIENT_CONNECT:
+                $worker_info = json_decode($data['body'], true);
+                if ($worker_info['secret_key'] !== $this->secretKey) {
+                    self::log("Gateway: GatewayClient key does not match ".var_export($this->secretKey, true)." !== ".var_export($this->secretKey, true));
+                    $connection->close();
+                    return;
+                }
+                $connection->authorized = true;
+                return;
+            // 向某客户端发送数据,Gateway::sendToClient($client_id, $message);
+            case GatewayProtocol::CMD_SEND_TO_ONE:
+                if (isset($this->_clientConnections[$data['connection_id']])) {
+                    $this->_clientConnections[$data['connection_id']]->send($data['body']);
+                }
+                return;
+            // 踢出用户,Gateway::closeClient($client_id, $message);
+            case GatewayProtocol::CMD_KICK:
+                if (isset($this->_clientConnections[$data['connection_id']])) {
+                    $this->_clientConnections[$data['connection_id']]->close($data['body']);
+                }
+                return;
+            // 立即销毁用户连接, Gateway::destroyClient($client_id);
+            case GatewayProtocol::CMD_DESTROY:
+                if (isset($this->_clientConnections[$data['connection_id']])) {
+                    $this->_clientConnections[$data['connection_id']]->destroy();
+                }
+                return;
+            // 广播, Gateway::sendToAll($message, $client_id_array)
+            case GatewayProtocol::CMD_SEND_TO_ALL:
+                $raw = (bool)($data['flag'] & GatewayProtocol::FLAG_NOT_CALL_ENCODE);
+                $body = $data['body'];
+                if (!$raw && $this->protocolAccelerate && $this->protocol) {
+                    $body = $this->preEncodeForClient($body);
+                    $raw = true;
+                }
+                $ext_data = $data['ext_data'] ? json_decode($data['ext_data'], true) : '';
+                // $client_id_array 不为空时,只广播给 $client_id_array 指定的客户端
+                if (isset($ext_data['connections'])) {
+                    foreach ($ext_data['connections'] as $connection_id) {
+                        if (isset($this->_clientConnections[$connection_id])) {
+                            $this->_clientConnections[$connection_id]->send($body, $raw);
+                        }
+                    }
+                } // $client_id_array 为空时,广播给所有在线客户端
+                else {
+                    $exclude_connection_id = !empty($ext_data['exclude']) ? $ext_data['exclude'] : null;
+                    foreach ($this->_clientConnections as $client_connection) {
+                        if (!isset($exclude_connection_id[$client_connection->id])) {
+                            $client_connection->send($body, $raw);
+                        }
+                    }
+                }
+                return;
+            case GatewayProtocol::CMD_SELECT:
+                $client_info_array = array();
+                $ext_data = json_decode($data['ext_data'], true);
+                if (!$ext_data) {
+                    echo 'CMD_SELECT ext_data=' . var_export($data['ext_data'], true) . '\r\n';
+                    $buffer = serialize($client_info_array);
+                    $connection->send(pack('N', strlen($buffer)) . $buffer, true);
+                    return;
+                }
+                $fields = $ext_data['fields'];
+                $where  = $ext_data['where'];
+                if ($where) {
+                    $connection_box_map = array(
+                        'groups'        => $this->_groupConnections,
+                        'uid'           => $this->_uidConnections
+                    );
+                    // $where = ['groups'=>[x,x..], 'uid'=>[x,x..], 'connection_id'=>[x,x..]]
+                    foreach ($where as $key => $items) {
+                        if ($key !== 'connection_id') {
+                            $connections_box = $connection_box_map[$key];
+                            foreach ($items as $item) {
+                                if (isset($connections_box[$item])) {
+                                    foreach ($connections_box[$item] as $connection_id => $client_connection) {
+                                        if (!isset($client_info_array[$connection_id])) {
+                                            $client_info_array[$connection_id] = array();
+                                            // $fields = ['groups', 'uid', 'session']
+                                            foreach ($fields as $field) {
+                                                $client_info_array[$connection_id][$field] = isset($client_connection->$field) ? $client_connection->$field : null;
+                                            }
+                                        }
+                                    }
+
+                                }
+                            }
+                        } else {
+                            foreach ($items as $connection_id) {
+                                if (isset($this->_clientConnections[$connection_id])) {
+                                    $client_connection = $this->_clientConnections[$connection_id];
+                                    $client_info_array[$connection_id] = array();
+                                    // $fields = ['groups', 'uid', 'session']
+                                    foreach ($fields as $field) {
+                                        $client_info_array[$connection_id][$field] = isset($client_connection->$field) ? $client_connection->$field : null;
+                                    }
+                                }
+                            }
+                        }
+                    }
+                } else {
+                    foreach ($this->_clientConnections as $connection_id => $client_connection) {
+                        foreach ($fields as $field) {
+                            $client_info_array[$connection_id][$field] = isset($client_connection->$field) ? $client_connection->$field : null;
+                        }
+                    }
+                }
+                $buffer = serialize($client_info_array);
+                $connection->send(pack('N', strlen($buffer)) . $buffer, true);
+                return;
+            // 获取在线群组列表
+            case GatewayProtocol::CMD_GET_GROUP_ID_LIST:
+                $buffer = serialize(array_keys($this->_groupConnections));
+                $connection->send(pack('N', strlen($buffer)) . $buffer, true);
+                return;
+            // 重新赋值 session
+            case GatewayProtocol::CMD_SET_SESSION:
+                if (isset($this->_clientConnections[$data['connection_id']])) {
+                    $this->_clientConnections[$data['connection_id']]->session = $data['ext_data'];
+                }
+                return;
+            // session合并
+            case GatewayProtocol::CMD_UPDATE_SESSION:
+                if (!isset($this->_clientConnections[$data['connection_id']])) {
+                    return;
+                } else {
+                    if (!$this->_clientConnections[$data['connection_id']]->session) {
+                        $this->_clientConnections[$data['connection_id']]->session = $data['ext_data'];
+                        return;
+                    }
+                    $session = Context::sessionDecode($this->_clientConnections[$data['connection_id']]->session);
+                    $session_for_merge = Context::sessionDecode($data['ext_data']);
+                    $session = array_replace_recursive($session, $session_for_merge);
+                    $this->_clientConnections[$data['connection_id']]->session = Context::sessionEncode($session);
+                }
+                return;
+            case GatewayProtocol::CMD_GET_SESSION_BY_CLIENT_ID:
+                if (!isset($this->_clientConnections[$data['connection_id']])) {
+                    $session = serialize(null);
+                } else {
+                    if (!$this->_clientConnections[$data['connection_id']]->session) {
+                        $session = serialize(array());
+                    } else {
+                        $session = $this->_clientConnections[$data['connection_id']]->session;
+                    }
+                }
+                $connection->send(pack('N', strlen($session)) . $session, true);
+                return;
+            // 获得客户端sessions
+            case GatewayProtocol::CMD_GET_ALL_CLIENT_SESSIONS:
+                $client_info_array = array();
+                foreach ($this->_clientConnections as $connection_id => $client_connection) {
+                    $client_info_array[$connection_id] = $client_connection->session;
+                }
+                $buffer = serialize($client_info_array);
+                $connection->send(pack('N', strlen($buffer)) . $buffer, true);
+                return;
+            // 判断某个 client_id 是否在线 Gateway::isOnline($client_id)
+            case GatewayProtocol::CMD_IS_ONLINE:
+                $buffer = serialize((int)isset($this->_clientConnections[$data['connection_id']]));
+                $connection->send(pack('N', strlen($buffer)) . $buffer, true);
+                return;
+            // 将 client_id 与 uid 绑定
+            case GatewayProtocol::CMD_BIND_UID:
+                $uid = $data['ext_data'];
+                if (empty($uid)) {
+                    echo "bindUid(client_id, uid) uid empty, uid=" . var_export($uid, true);
+                    return;
+                }
+                $connection_id = $data['connection_id'];
+                if (!isset($this->_clientConnections[$connection_id])) {
+                    return;
+                }
+                $client_connection = $this->_clientConnections[$connection_id];
+                if (isset($client_connection->uid)) {
+                    $current_uid = $client_connection->uid;
+                    unset($this->_uidConnections[$current_uid][$connection_id]);
+                    if (empty($this->_uidConnections[$current_uid])) {
+                        unset($this->_uidConnections[$current_uid]);
+                    }
+                }
+                $client_connection->uid                      = $uid;
+                $this->_uidConnections[$uid][$connection_id] = $client_connection;
+                return;
+            // client_id 与 uid 解绑 Gateway::unbindUid($client_id, $uid);
+            case GatewayProtocol::CMD_UNBIND_UID:
+                $connection_id = $data['connection_id'];
+                if (!isset($this->_clientConnections[$connection_id])) {
+                    return;
+                }
+                $client_connection = $this->_clientConnections[$connection_id];
+                if (isset($client_connection->uid)) {
+                    $current_uid = $client_connection->uid;
+                    unset($this->_uidConnections[$current_uid][$connection_id]);
+                    if (empty($this->_uidConnections[$current_uid])) {
+                        unset($this->_uidConnections[$current_uid]);
+                    }
+                    $client_connection->uid_info = '';
+                    $client_connection->uid      = null;
+                }
+                return;
+            // 发送数据给 uid Gateway::sendToUid($uid, $msg);
+            case GatewayProtocol::CMD_SEND_TO_UID:
+                $uid_array = json_decode($data['ext_data'], true);
+                foreach ($uid_array as $uid) {
+                    if (!empty($this->_uidConnections[$uid])) {
+                        foreach ($this->_uidConnections[$uid] as $connection) {
+                            /** @var TcpConnection $connection */
+                            $connection->send($data['body']);
+                        }
+                    }
+                }
+                return;
+            // 将 $client_id 加入用户组 Gateway::joinGroup($client_id, $group);
+            case GatewayProtocol::CMD_JOIN_GROUP:
+                $group = $data['ext_data'];
+                if (empty($group)) {
+                    echo "join(group) group empty, group=" . var_export($group, true);
+                    return;
+                }
+                $connection_id = $data['connection_id'];
+                if (!isset($this->_clientConnections[$connection_id])) {
+                    return;
+                }
+                $client_connection = $this->_clientConnections[$connection_id];
+                if (!isset($client_connection->groups)) {
+                    $client_connection->groups = array();
+                }
+                $client_connection->groups[$group]               = $group;
+                $this->_groupConnections[$group][$connection_id] = $client_connection;
+                return;
+            // 将 $client_id 从某个用户组中移除 Gateway::leaveGroup($client_id, $group);
+            case GatewayProtocol::CMD_LEAVE_GROUP:
+                $group = $data['ext_data'];
+                if (empty($group)) {
+                    echo "leave(group) group empty, group=" . var_export($group, true);
+                    return;
+                }
+                $connection_id = $data['connection_id'];
+                if (!isset($this->_clientConnections[$connection_id])) {
+                    return;
+                }
+                $client_connection = $this->_clientConnections[$connection_id];
+                if (!isset($client_connection->groups[$group])) {
+                    return;
+                }
+                unset($client_connection->groups[$group], $this->_groupConnections[$group][$connection_id]);
+                if (empty($this->_groupConnections[$group])) {
+                    unset($this->_groupConnections[$group]);
+                }
+                return;
+            // 解散分组
+            case GatewayProtocol::CMD_UNGROUP:
+                $group = $data['ext_data'];
+                if (empty($group)) {
+                    echo "leave(group) group empty, group=" . var_export($group, true);
+                    return;
+                }
+                if (empty($this->_groupConnections[$group])) {
+                    return;
+                }
+                foreach ($this->_groupConnections[$group] as $client_connection) {
+                    unset($client_connection->groups[$group]);
+                }
+                unset($this->_groupConnections[$group]);
+                return;
+            // 向某个用户组发送消息 Gateway::sendToGroup($group, $msg);
+            case GatewayProtocol::CMD_SEND_TO_GROUP:
+                $raw = (bool)($data['flag'] & GatewayProtocol::FLAG_NOT_CALL_ENCODE);
+                $body = $data['body'];
+                if (!$raw && $this->protocolAccelerate && $this->protocol) {
+                    $body = $this->preEncodeForClient($body);
+                    $raw = true;
+                }
+                $ext_data = json_decode($data['ext_data'], true);
+                $group_array = $ext_data['group'];
+                $exclude_connection_id = $ext_data['exclude'];
+
+                foreach ($group_array as $group) {
+                    if (!empty($this->_groupConnections[$group])) {
+                        foreach ($this->_groupConnections[$group] as $connection) {
+                            if(!isset($exclude_connection_id[$connection->id]))
+                            {
+                                /** @var TcpConnection $connection */
+                                $connection->send($body, $raw);
+                            }
+                        }
+                    }
+                }
+                return;
+            // 获取某用户组成员信息 Gateway::getClientSessionsByGroup($group);
+            case GatewayProtocol::CMD_GET_CLIENT_SESSIONS_BY_GROUP:
+                $group = $data['ext_data'];
+                if (!isset($this->_groupConnections[$group])) {
+                    $buffer = serialize(array());
+                    $connection->send(pack('N', strlen($buffer)) . $buffer, true);
+                    return;
+                }
+                $client_info_array = array();
+                foreach ($this->_groupConnections[$group] as $connection_id => $client_connection) {
+                    $client_info_array[$connection_id] = $client_connection->session;
+                }
+                $buffer = serialize($client_info_array);
+                $connection->send(pack('N', strlen($buffer)) . $buffer, true);
+                return;
+            // 获取用户组成员数 Gateway::getClientCountByGroup($group);
+            case GatewayProtocol::CMD_GET_CLIENT_COUNT_BY_GROUP:
+                $group = $data['ext_data'];
+                $count = 0;
+                if ($group !== '') {
+                    if (isset($this->_groupConnections[$group])) {
+                        $count = count($this->_groupConnections[$group]);
+                    }
+                } else {
+                    $count = count($this->_clientConnections);
+                }
+                $buffer = serialize($count);
+                $connection->send(pack('N', strlen($buffer)) . $buffer, true);
+                return;
+            // 获取与某个 uid 绑定的所有 client_id Gateway::getClientIdByUid($uid);
+            case GatewayProtocol::CMD_GET_CLIENT_ID_BY_UID:
+                $uid = $data['ext_data'];
+                if (empty($this->_uidConnections[$uid])) {
+                    $buffer = serialize(array());
+                } else {
+                    $buffer = serialize(array_keys($this->_uidConnections[$uid]));
+                }
+                $connection->send(pack('N', strlen($buffer)) . $buffer, true);
+                return;
+            default :
+                $err_msg = "gateway inner pack err cmd=$cmd";
+                echo $err_msg;
+        }
+    }
+
+
+    /**
+     * 当worker连接关闭时
+     *
+     * @param TcpConnection $connection
+     */
+    public function onWorkerClose($connection)
+    {
+        if (isset($connection->key)) {
+            unset($this->_workerConnections[$connection->key]);
+        }
+    }
+
+    /**
+     * 存储当前 Gateway 的内部通信地址
+     *
+     * @return bool
+     */
+    public function registerAddress()
+    {
+        $address = $this->lanIp . ':' . $this->lanPort;
+        foreach ($this->registerAddress as $register_address) {
+            $register_connection = new AsyncTcpConnection("text://{$register_address}");
+            $secret_key = $this->secretKey;
+            $register_connection->onConnect = function($register_connection) use ($address, $secret_key, $register_address){
+                $register_connection->send('{"event":"gateway_connect", "address":"' . $address . '", "secret_key":"' . $secret_key . '"}');
+                // 如果Register服务器不在本地服务器,则需要保持心跳
+                if (strpos($register_address, '127.0.0.1') !== 0) {
+                    $register_connection->ping_timer = Timer::add(self::PERSISTENCE_CONNECTION_PING_INTERVAL, function () use ($register_connection) {
+                        $register_connection->send('{"event":"ping"}');
+                    });
+                }
+            };
+            $register_connection->onClose = function ($register_connection) {
+                if(!empty($register_connection->ping_timer)) {
+                    Timer::del($register_connection->ping_timer);
+                }
+                $register_connection->reconnect(1);
+            };
+            $register_connection->connect();
+        }
+    }
+
+
+    /**
+     * 心跳逻辑
+     *
+     * @return void
+     */
+    public function ping()
+    {
+        $ping_data = $this->pingData ? (string)$this->pingData : null;
+        $raw = false;
+        if ($this->protocolAccelerate && $ping_data && $this->protocol) {
+            $ping_data = $this->preEncodeForClient($ping_data);
+            $raw = true;
+        }
+        // 遍历所有客户端连接
+        foreach ($this->_clientConnections as $connection) {
+            // 上次发送的心跳还没有回复次数大于限定值就断开
+            if ($this->pingNotResponseLimit > 0 &&
+                $connection->pingNotResponseCount >= $this->pingNotResponseLimit * 2
+            ) {
+                $connection->destroy();
+                continue;
+            }
+            // $connection->pingNotResponseCount 为 -1 说明最近客户端有发来消息,则不给客户端发送心跳
+            $connection->pingNotResponseCount++;
+            if ($ping_data) {
+                if ($connection->pingNotResponseCount === 0 ||
+                    ($this->pingNotResponseLimit > 0 && $connection->pingNotResponseCount % 2 === 1)
+                ) {
+                    continue;
+                }
+                $connection->send($ping_data, $raw);
+            }
+        }
+    }
+
+    /**
+     * 向 BusinessWorker 发送心跳数据,用于保持长连接
+     *
+     * @return void
+     */
+    public function pingBusinessWorker()
+    {
+        $gateway_data        = GatewayProtocol::$empty;
+        $gateway_data['cmd'] = GatewayProtocol::CMD_PING;
+        foreach ($this->_workerConnections as $connection) {
+            $connection->send($gateway_data);
+        }
+    }
+
+    /**
+     * @param mixed $data
+     *
+     * @return string
+     */
+    protected function preEncodeForClient($data)
+    {
+        foreach ($this->_clientConnections as $client_connection) {
+            return call_user_func(array($client_connection->protocol, 'encode'), $data, $client_connection);
+        }
+    }
+
+    /**
+     * 当 gateway 关闭时触发,清理数据
+     *
+     * @return void
+     */
+    public function onWorkerStop()
+    {
+        // 尝试触发用户设置的回调
+        if ($this->_onWorkerStop) {
+            call_user_func($this->_onWorkerStop, $this);
+        }
+    }
+
+    /**
+     * Log.
+     * @param string $msg
+     */
+    public static function log($msg){
+        Timer::add(1, function() use ($msg) {
+            Worker::log($msg);
+        }, null, false);
+    }
+}

+ 136 - 0
GatewayWorker/vendor/workerman/gateway-worker/src/Lib/Context.php

@@ -0,0 +1,136 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link      http://www.workerman.net/
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace GatewayWorker\Lib;
+
+use Exception;
+
+/**
+ * 上下文 包含当前用户 uid, 内部通信 local_ip local_port socket_id,以及客户端 client_ip client_port
+ */
+class Context
+{
+    /**
+     * 内部通讯 id
+     *
+     * @var string
+     */
+    public static $local_ip;
+
+    /**
+     * 内部通讯端口
+     *
+     * @var int
+     */
+    public static $local_port;
+
+    /**
+     * 客户端 ip
+     *
+     * @var string
+     */
+    public static $client_ip;
+
+    /**
+     * 客户端端口
+     *
+     * @var int
+     */
+    public static $client_port;
+
+    /**
+     * client_id
+     *
+     * @var string
+     */
+    public static $client_id;
+
+    /**
+     * 连接 connection->id
+     *
+     * @var int
+     */
+    public static $connection_id;
+    
+    /**
+     * 旧的session
+     * 
+     * @var string
+     */
+    public static $old_session;
+
+    /**
+     * 编码 session
+     *
+     * @param mixed $session_data
+     * @return string
+     */
+    public static function sessionEncode($session_data = '')
+    {
+        if ($session_data !== '') {
+            return serialize($session_data);
+        }
+        return '';
+    }
+
+    /**
+     * 解码 session
+     *
+     * @param string $session_buffer
+     * @return mixed
+     */
+    public static function sessionDecode($session_buffer)
+    {
+        return unserialize($session_buffer);
+    }
+
+    /**
+     * 清除上下文
+     *
+     * @return void
+     */
+    public static function clear()
+    {
+        self::$local_ip = self::$local_port = self::$client_ip = self::$client_port =
+        self::$client_id = self::$connection_id  = self::$old_session = null;
+    }
+
+    /**
+     * 通讯地址到 client_id 的转换
+     *
+     * @param int $local_ip
+     * @param int $local_port
+     * @param int $connection_id
+     * @return string
+     */
+    public static function addressToClientId($local_ip, $local_port, $connection_id)
+    {
+        return bin2hex(pack('NnN', $local_ip, $local_port, $connection_id));
+    }
+
+    /**
+     * client_id 到通讯地址的转换
+     *
+     * @param string $client_id
+     * @return array
+     * @throws Exception
+     */
+    public static function clientIdToAddress($client_id)
+    {
+        if (strlen($client_id) !== 20) {
+            echo new Exception("client_id $client_id is invalid");
+            return false;
+        }
+        return unpack('Nlocal_ip/nlocal_port/Nconnection_id', pack('H*', $client_id));
+    }
+}

+ 76 - 0
GatewayWorker/vendor/workerman/gateway-worker/src/Lib/Db.php

@@ -0,0 +1,76 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link      http://www.workerman.net/
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace GatewayWorker\Lib;
+
+use Config\Db as DbConfig;
+use Exception;
+
+/**
+ * 数据库类
+ */
+class Db
+{
+    /**
+     * 实例数组
+     *
+     * @var array
+     */
+    protected static $instance = array();
+
+    /**
+     * 获取实例
+     *
+     * @param string $config_name
+     * @return DbConnection
+     * @throws Exception
+     */
+    public static function instance($config_name)
+    {
+        if (!isset(DbConfig::$$config_name)) {
+            echo "\\Config\\Db::$config_name not set\n";
+            throw new Exception("\\Config\\Db::$config_name not set\n");
+        }
+
+        if (empty(self::$instance[$config_name])) {
+            $config                       = DbConfig::$$config_name;
+            self::$instance[$config_name] = new DbConnection($config['host'], $config['port'],
+                $config['user'], $config['password'], $config['dbname'],$config['charset']);
+        }
+        return self::$instance[$config_name];
+    }
+
+    /**
+     * 关闭数据库实例
+     *
+     * @param string $config_name
+     */
+    public static function close($config_name)
+    {
+        if (isset(self::$instance[$config_name])) {
+            self::$instance[$config_name]->closeConnection();
+            self::$instance[$config_name] = null;
+        }
+    }
+
+    /**
+     * 关闭所有数据库实例
+     */
+    public static function closeAll()
+    {
+        foreach (self::$instance as $connection) {
+            $connection->closeConnection();
+        }
+        self::$instance = array();
+    }
+}

+ 1976 - 0
GatewayWorker/vendor/workerman/gateway-worker/src/Lib/DbConnection.php

@@ -0,0 +1,1976 @@
+<?php
+namespace GatewayWorker\Lib;
+
+use Exception;
+use PDO;
+use PDOException;
+
+/**
+ * 数据库连接类,依赖 PDO_MYSQL 扩展
+ * 在 https://github.com/auraphp/Aura.SqlQuery 的基础上修改而成
+ */
+class DbConnection
+{
+    /**
+     * SELECT
+     *
+     * @var array
+     */
+    protected $union = array();
+
+    /**
+     * 是否是更新
+     *
+     * @var bool
+     */
+    protected $for_update = false;
+
+    /**
+     * 选择的列
+     *
+     * @var array
+     */
+    protected $cols = array();
+
+    /**
+     * 从哪些表里面 SELECT
+     *
+     * @var array
+     */
+    protected $from = array();
+
+    /**
+     * $from 当前的 key
+     *
+     * @var int
+     */
+    protected $from_key = -1;
+
+    /**
+     * GROUP BY 的列
+     *
+     * @var array
+     */
+    protected $group_by = array();
+
+    /**
+     * HAVING 条件数组.
+     *
+     * @var array
+     */
+    protected $having = array();
+
+    /**
+     * HAVING 语句中绑定的值.
+     *
+     * @var array
+     */
+    protected $bind_having = array();
+
+    /**
+     * 每页多少条记录
+     *
+     * @var int
+     */
+    protected $paging = 10;
+
+    /**
+     * sql 中绑定的值
+     *
+     * @var array
+     */
+    protected $bind_values = array();
+
+    /**
+     * WHERE 条件.
+     *
+     * @var array
+     */
+    protected $where = array();
+
+    /**
+     * WHERE 语句绑定的值
+     *
+     * @var array
+     */
+    protected $bind_where = array();
+
+    /**
+     * ORDER BY 的列
+     *
+     * @var array
+     */
+    protected $order_by = array();
+
+    /**
+     * ORDER BY 的排序方式,默认为升序
+     *
+     * @var bool
+     */
+    protected $order_asc = true;
+    /**
+     * SELECT 多少记录
+     *
+     * @var int
+     */
+    protected $limit = 0;
+
+    /**
+     * 返回记录的游标
+     *
+     * @var int
+     */
+    protected $offset = 0;
+
+    /**
+     * flags 列表
+     *
+     * @var array
+     */
+    protected $flags = array();
+
+    /**
+     * 操作哪个表
+     *
+     * @var string
+     */
+    protected $table;
+
+    /**
+     * 表.列 和 last-insert-id 映射
+     *
+     * @var array
+     */
+    protected $last_insert_id_names = array();
+
+    /**
+     * INSERT 或者 UPDATE 的列
+     *
+     * @param array
+     */
+    protected $col_values;
+
+    /**
+     * 返回的列
+     *
+     * @var array
+     */
+    protected $returning = array();
+
+    /**
+     * sql 的类型 SELECT INSERT DELETE UPDATE
+     *
+     * @var string
+     */
+    protected $type = '';
+
+    /**
+     * pdo 实例
+     *
+     * @var PDO
+     */
+    protected $pdo;
+
+    /**
+     * PDOStatement 实例
+     *
+     * @var \PDOStatement
+     */
+    protected $sQuery;
+
+    /**
+     * 数据库用户名密码等配置
+     *
+     * @var array
+     */
+    protected $settings = array();
+
+    /**
+     * sql 的参数
+     *
+     * @var array
+     */
+    protected $parameters = array();
+
+    /**
+     * 最后一条直行的 sql
+     *
+     * @var string
+     */
+    protected $lastSql = '';
+
+    /**
+     * 是否执行成功
+     *
+     * @var bool
+     */
+    protected $success = false;
+
+    /**
+     * 选择哪些列
+     *
+     * @param string|array $cols
+     * @return self
+     */
+    public function select($cols = '*')
+    {
+        $this->type = 'SELECT';
+        if (!is_array($cols)) {
+            $cols = array($cols);
+        }
+        $this->cols($cols);
+        return $this;
+    }
+
+    /**
+     * 从哪个表删除
+     *
+     * @param string $table
+     * @return self
+     */
+    public function delete($table)
+    {
+        $this->type  = 'DELETE';
+        $this->table = $this->quoteName($table);
+        $this->fromRaw($this->quoteName($table));
+        return $this;
+    }
+
+    /**
+     * 更新哪个表
+     *
+     * @param string $table
+     * @return self
+     */
+    public function update($table)
+    {
+        $this->type  = 'UPDATE';
+        $this->table = $this->quoteName($table);
+        return $this;
+    }
+
+    /**
+     * 向哪个表插入
+     *
+     * @param string $table
+     * @return self
+     */
+    public function insert($table)
+    {
+        $this->type  = 'INSERT';
+        $this->table = $this->quoteName($table);
+        return $this;
+    }
+
+    /**
+     *
+     * 设置 SQL_CALC_FOUND_ROWS 标记.
+     *
+     * @param bool $enable
+     * @return self
+     */
+    public function calcFoundRows($enable = true)
+    {
+        $this->setFlag('SQL_CALC_FOUND_ROWS', $enable);
+        return $this;
+    }
+
+    /**
+     * 设置 SQL_CACHE 标记
+     *
+     * @param bool $enable
+     * @return self
+     */
+    public function cache($enable = true)
+    {
+        $this->setFlag('SQL_CACHE', $enable);
+        return $this;
+    }
+
+    /**
+     * 设置 SQL_NO_CACHE 标记
+     *
+     * @param bool $enable
+     * @return self
+     */
+    public function noCache($enable = true)
+    {
+        $this->setFlag('SQL_NO_CACHE', $enable);
+        return $this;
+    }
+
+    /**
+     * 设置 STRAIGHT_JOIN 标记.
+     *
+     * @param bool $enable
+     * @return self
+     */
+    public function straightJoin($enable = true)
+    {
+        $this->setFlag('STRAIGHT_JOIN', $enable);
+        return $this;
+    }
+
+    /**
+     * 设置 HIGH_PRIORITY 标记
+     *
+     * @param bool $enable
+     * @return self
+     */
+    public function highPriority($enable = true)
+    {
+        $this->setFlag('HIGH_PRIORITY', $enable);
+        return $this;
+    }
+
+    /**
+     * 设置 SQL_SMALL_RESULT 标记
+     *
+     * @param bool $enable
+     * @return self
+     */
+    public function smallResult($enable = true)
+    {
+        $this->setFlag('SQL_SMALL_RESULT', $enable);
+        return $this;
+    }
+
+    /**
+     * 设置 SQL_BIG_RESULT 标记
+     *
+     * @param bool $enable
+     * @return self
+     */
+    public function bigResult($enable = true)
+    {
+        $this->setFlag('SQL_BIG_RESULT', $enable);
+        return $this;
+    }
+
+    /**
+     * 设置 SQL_BUFFER_RESULT 标记
+     *
+     * @param bool $enable
+     * @return self
+     */
+    public function bufferResult($enable = true)
+    {
+        $this->setFlag('SQL_BUFFER_RESULT', $enable);
+        return $this;
+    }
+
+    /**
+     * 设置 FOR UPDATE 标记
+     *
+     * @param bool $enable
+     * @return self
+     */
+    public function forUpdate($enable = true)
+    {
+        $this->for_update = (bool)$enable;
+        return $this;
+    }
+
+    /**
+     * 设置 DISTINCT 标记
+     *
+     * @param bool $enable
+     * @return self
+     */
+    public function distinct($enable = true)
+    {
+        $this->setFlag('DISTINCT', $enable);
+        return $this;
+    }
+
+    /**
+     * 设置 LOW_PRIORITY 标记
+     *
+     * @param bool $enable
+     * @return self
+     */
+    public function lowPriority($enable = true)
+    {
+        $this->setFlag('LOW_PRIORITY', $enable);
+        return $this;
+    }
+
+    /**
+     * 设置 IGNORE 标记
+     *
+     * @param bool $enable
+     * @return self
+     */
+    public function ignore($enable = true)
+    {
+        $this->setFlag('IGNORE', $enable);
+        return $this;
+    }
+
+    /**
+     * 设置 QUICK 标记
+     *
+     * @param bool $enable
+     * @return self
+     */
+    public function quick($enable = true)
+    {
+        $this->setFlag('QUICK', $enable);
+        return $this;
+    }
+
+    /**
+     * 设置 DELAYED 标记
+     *
+     * @param bool $enable
+     * @return self
+     */
+    public function delayed($enable = true)
+    {
+        $this->setFlag('DELAYED', $enable);
+        return $this;
+    }
+
+    /**
+     * 序列化
+     *
+     * @return string
+     */
+    public function __toString()
+    {
+        $union = '';
+        if ($this->union) {
+            $union = implode(' ', $this->union) . ' ';
+        }
+        return $union . $this->build();
+    }
+
+    /**
+     * 设置每页多少条记录
+     *
+     * @param int $paging
+     * @return self
+     */
+    public function setPaging($paging)
+    {
+        $this->paging = (int)$paging;
+        return $this;
+    }
+
+    /**
+     * 获取每页多少条记录
+     *
+     * @return int
+     */
+    public function getPaging()
+    {
+        return $this->paging;
+    }
+
+    /**
+     * 获取绑定在占位符上的值
+     */
+    public function getBindValues()
+    {
+        switch ($this->type) {
+            case 'SELECT':
+                return $this->getBindValuesSELECT();
+            case 'DELETE':
+            case 'UPDATE':
+            case 'INSERT':
+                return $this->getBindValuesCOMMON();
+            default :
+                throw new Exception("type err");
+        }
+    }
+
+    /**
+     * 获取绑定在占位符上的值
+     *
+     * @return array
+     */
+    public function getBindValuesSELECT()
+    {
+        $bind_values = $this->bind_values;
+        $i           = 1;
+        foreach ($this->bind_where as $val) {
+            $bind_values[$i] = $val;
+            $i++;
+        }
+        foreach ($this->bind_having as $val) {
+            $bind_values[$i] = $val;
+            $i++;
+        }
+        return $bind_values;
+    }
+
+    /**
+     *
+     * SELECT选择哪些列
+     *
+     * @param mixed  $key
+     * @param string $val
+     * @return void
+     */
+    protected function addColSELECT($key, $val)
+    {
+        if (is_string($key)) {
+            $this->cols[$val] = $key;
+        } else {
+            $this->addColWithAlias($val);
+        }
+    }
+
+    /**
+     * SELECT 增加选择的列
+     *
+     * @param string $spec
+     */
+    protected function addColWithAlias($spec)
+    {
+        $parts = explode(' ', $spec);
+        $count = count($parts);
+        if ($count == 2) {
+            $this->cols[$parts[1]] = $parts[0];
+        } elseif ($count == 3 && strtoupper($parts[1]) == 'AS') {
+            $this->cols[$parts[2]] = $parts[0];
+        } else {
+            $this->cols[] = $spec;
+        }
+    }
+
+    /**
+     * from 哪个表
+     *
+     * @param string $table
+     * @return self
+     */
+    public function from($table)
+    {
+        return $this->fromRaw($this->quoteName($table));
+    }
+
+    /**
+     * from的表
+     *
+     * @param string $table
+     * @return self
+     */
+    public function fromRaw($table)
+    {
+        $this->from[] = array($table);
+        $this->from_key++;
+        return $this;
+    }
+
+    /**
+     *
+     * 子查询
+     *
+     * @param string $table
+     * @param string $name The alias name for the sub-select.
+     * @return self
+     */
+    public function fromSubSelect($table, $name)
+    {
+        $this->from[] = array("($table) AS " . $this->quoteName($name));
+        $this->from_key++;
+        return $this;
+    }
+
+
+    /**
+     * 增加 join 语句
+     *
+     * @param string $table
+     * @param string $cond
+     * @param string $type
+     * @return self
+     * @throws Exception
+     */
+    public function join($table, $cond = null, $type = '')
+    {
+        return $this->joinInternal($type, $table, $cond);
+    }
+
+    /**
+     * 增加 join 语句
+     *
+     * @param string $join inner, left, natural
+     * @param string $table
+     * @param string $cond
+     * @return self
+     * @throws Exception
+     */
+    protected function joinInternal($join, $table, $cond = null)
+    {
+        if (!$this->from) {
+            throw new Exception('Cannot join() without from()');
+        }
+
+        $join                          = strtoupper(ltrim("$join JOIN"));
+        $table                         = $this->quoteName($table);
+        $cond                          = $this->fixJoinCondition($cond);
+        $this->from[$this->from_key][] = rtrim("$join $table $cond");
+        return $this;
+    }
+
+    /**
+     * quote
+     *
+     * @param string $cond
+     * @return string
+     *
+     */
+    protected function fixJoinCondition($cond)
+    {
+        if (!$cond) {
+            return '';
+        }
+
+        $cond = $this->quoteNamesIn($cond);
+
+        if (strtoupper(substr(ltrim($cond), 0, 3)) == 'ON ') {
+            return $cond;
+        }
+
+        if (strtoupper(substr(ltrim($cond), 0, 6)) == 'USING ') {
+            return $cond;
+        }
+
+        return 'ON ' . $cond;
+    }
+
+    /**
+     * inner join
+     *
+     * @param string $table
+     * @param string $cond
+     * @return self
+     * @throws Exception
+     */
+    public function innerJoin($table, $cond = null)
+    {
+        return $this->joinInternal('INNER', $table, $cond);
+    }
+
+    /**
+     * left join
+     *
+     * @param string $table
+     * @param string $cond
+     * @return self
+     * @throws Exception
+     */
+    public function leftJoin($table, $cond = null)
+    {
+        return $this->joinInternal('LEFT', $table, $cond);
+    }
+
+    /**
+     * right join
+     *
+     * @param string $table
+     * @param string $cond
+     * @return self
+     * @throws Exception
+     */
+    public function rightJoin($table, $cond = null)
+    {
+        return $this->joinInternal('RIGHT', $table, $cond);
+    }
+
+    /**
+     * joinSubSelect
+     *
+     * @param string $join inner, left, natural
+     * @param string $spec
+     * @param string $name sub-select 的别名
+     * @param string $cond
+     * @return self
+     * @throws Exception
+     */
+    public function joinSubSelect($join, $spec, $name, $cond = null)
+    {
+        if (!$this->from) {
+            throw new \Exception('Cannot join() without from() first.');
+        }
+
+        $join                          = strtoupper(ltrim("$join JOIN"));
+        $name                          = $this->quoteName($name);
+        $cond                          = $this->fixJoinCondition($cond);
+        $this->from[$this->from_key][] = rtrim("$join ($spec) AS $name $cond");
+        return $this;
+    }
+
+    /**
+     * group by 语句
+     *
+     * @param array $cols
+     * @return self
+     */
+    public function groupBy(array $cols)
+    {
+        foreach ($cols as $col) {
+            $this->group_by[] = $this->quoteNamesIn($col);
+        }
+        return $this;
+    }
+
+    /**
+     * having 语句
+     *
+     * @param string $cond
+     * @return self
+     */
+    public function having($cond)
+    {
+        $this->addClauseCondWithBind('having', 'AND', func_get_args());
+        return $this;
+    }
+
+    /**
+     * or having 语句
+     *
+     * @param string $cond The HAVING condition.
+     * @return self
+     */
+    public function orHaving($cond)
+    {
+        $this->addClauseCondWithBind('having', 'OR', func_get_args());
+        return $this;
+    }
+
+    /**
+     * 设置每页的记录数量
+     *
+     * @param int $page
+     * @return self
+     */
+    public function page($page)
+    {
+        $this->limit  = 0;
+        $this->offset = 0;
+
+        $page = (int)$page;
+        if ($page > 0) {
+            $this->limit  = $this->paging;
+            $this->offset = $this->paging * ($page - 1);
+        }
+        return $this;
+    }
+
+    /**
+     * union
+     *
+     * @return self
+     */
+    public function union()
+    {
+        $this->union[] = $this->build() . ' UNION';
+        $this->reset();
+        return $this;
+    }
+
+    /**
+     * unionAll
+     *
+     * @return self
+     */
+    public function unionAll()
+    {
+        $this->union[] = $this->build() . ' UNION ALL';
+        $this->reset();
+        return $this;
+    }
+
+    /**
+     * 重置
+     */
+    protected function reset()
+    {
+        $this->resetFlags();
+        $this->cols       = array();
+        $this->from       = array();
+        $this->from_key   = -1;
+        $this->where      = array();
+        $this->group_by   = array();
+        $this->having     = array();
+        $this->order_by   = array();
+        $this->limit      = 0;
+        $this->offset     = 0;
+        $this->for_update = false;
+    }
+
+    /**
+     * 清除所有数据
+     */
+    protected function resetAll()
+    {
+        $this->union                = array();
+        $this->for_update           = false;
+        $this->cols                 = array();
+        $this->from                 = array();
+        $this->from_key             = -1;
+        $this->group_by             = array();
+        $this->having               = array();
+        $this->bind_having          = array();
+        $this->paging               = 10;
+        $this->bind_values          = array();
+        $this->where                = array();
+        $this->bind_where           = array();
+        $this->order_by             = array();
+        $this->limit                = 0;
+        $this->offset               = 0;
+        $this->flags                = array();
+        $this->table                = '';
+        $this->last_insert_id_names = array();
+        $this->col_values           = array();
+        $this->returning            = array();
+        $this->parameters           = array();
+    }
+
+    /**
+     * 创建 SELECT SQL
+     *
+     * @return string
+     */
+    protected function buildSELECT()
+    {
+        return 'SELECT'
+        . $this->buildFlags()
+        . $this->buildCols()
+        . $this->buildFrom()
+        . $this->buildWhere()
+        . $this->buildGroupBy()
+        . $this->buildHaving()
+        . $this->buildOrderBy()
+        . $this->buildLimit()
+        . $this->buildForUpdate();
+    }
+
+    /**
+     * 创建 DELETE SQL
+     */
+    protected function buildDELETE()
+    {
+        return 'DELETE'
+        . $this->buildFlags()
+        . $this->buildFrom()
+        . $this->buildWhere()
+        . $this->buildOrderBy()
+        . $this->buildLimit()
+        . $this->buildReturning();
+    }
+
+    /**
+     * 生成 SELECT 列语句
+     *
+     * @return string
+     * @throws Exception
+     */
+    protected function buildCols()
+    {
+        if (!$this->cols) {
+            throw new Exception('No columns in the SELECT.');
+        }
+
+        $cols = array();
+        foreach ($this->cols as $key => $val) {
+            if (is_int($key)) {
+                $cols[] = $this->quoteNamesIn($val);
+            } else {
+                $cols[] = $this->quoteNamesIn("$val AS $key");
+            }
+        }
+
+        return $this->indentCsv($cols);
+    }
+
+    /**
+     * 生成 FROM 语句.
+     *
+     * @return string
+     */
+    protected function buildFrom()
+    {
+        if (!$this->from) {
+            return '';
+        }
+
+        $refs = array();
+        foreach ($this->from as $from) {
+            $refs[] = implode(' ', $from);
+        }
+        return ' FROM' . $this->indentCsv($refs);
+    }
+
+    /**
+     * 生成 GROUP BY 语句.
+     *
+     * @return string
+     */
+    protected function buildGroupBy()
+    {
+        if (!$this->group_by) {
+            return '';
+        }
+        return ' GROUP BY' . $this->indentCsv($this->group_by);
+    }
+
+    /**
+     * 生成 HAVING 语句.
+     *
+     * @return string
+     */
+    protected function buildHaving()
+    {
+        if (!$this->having) {
+            return '';
+        }
+        return ' HAVING' . $this->indent($this->having);
+    }
+
+    /**
+     * 生成 FOR UPDATE 语句
+     *
+     * @return string
+     */
+    protected function buildForUpdate()
+    {
+        if (!$this->for_update) {
+            return '';
+        }
+        return ' FOR UPDATE';
+    }
+
+    /**
+     * where
+     *
+     * @param string|array $cond
+     * @return self
+     */
+    public function where($cond)
+    {
+        if (is_array($cond)) {
+            foreach ($cond as $key => $val) {
+                if (is_string($key)) {
+                    $this->addWhere('AND', array($key, $val));
+                } else {
+                    $this->addWhere('AND', array($val));
+                }
+            }
+        } else {
+            $this->addWhere('AND', func_get_args());
+        }
+        return $this;
+    }
+
+    /**
+     * or where
+     *
+     * @param string|array $cond
+     * @return self
+     */
+    public function orWhere($cond)
+    {
+        if (is_array($cond)) {
+            foreach ($cond as $key => $val) {
+                if (is_string($key)) {
+                    $this->addWhere('OR', array($key, $val));
+                } else {
+                    $this->addWhere('OR', array($val));
+                }
+            }
+        } else {
+            $this->addWhere('OR', func_get_args());
+        }
+        return $this;
+    }
+
+    /**
+     * limit
+     *
+     * @param int $limit
+     * @return self
+     */
+    public function limit($limit)
+    {
+        $this->limit = (int)$limit;
+        return $this;
+    }
+
+    /**
+     * limit offset
+     *
+     * @param int $offset
+     * @return self
+     */
+    public function offset($offset)
+    {
+        $this->offset = (int)$offset;
+        return $this;
+    }
+
+    /**
+     * orderby.
+     *
+     * @param array $cols
+     * @return self
+     */
+    public function orderBy(array $cols)
+    {
+        return $this->addOrderBy($cols);
+    }
+
+    /**
+     * order by ASC OR DESC
+     *
+     * @param array $cols
+     * @param bool  $order_asc
+     * @return self
+     */
+    public function orderByASC(array $cols, $order_asc = true)
+    {
+        $this->order_asc = $order_asc;
+        return $this->addOrderBy($cols);
+    }
+
+    /**
+     * order by DESC
+     *
+     * @param array $cols
+     * @return self
+     */
+    public function orderByDESC(array $cols)
+    {
+        $this->order_asc = false;
+        return $this->addOrderBy($cols);
+    }
+
+    // -------------abstractquery----------
+    /**
+     * 返回逗号分隔的字符串
+     *
+     * @param array $list
+     * @return string
+     */
+    protected function indentCsv(array $list)
+    {
+        return ' ' . implode(',', $list);
+    }
+
+    /**
+     * 返回空格分隔的字符串
+     *
+     * @param array $list
+     * @return string
+     */
+    protected function indent(array $list)
+    {
+        return ' ' . implode(' ', $list);
+    }
+
+    /**
+     * 批量为占位符绑定值
+     *
+     * @param array $bind_values
+     * @return self
+     *
+     */
+    public function bindValues(array $bind_values)
+    {
+        foreach ($bind_values as $key => $val) {
+            $this->bindValue($key, $val);
+        }
+        return $this;
+    }
+
+    /**
+     * 单个为占位符绑定值
+     *
+     * @param string $name
+     * @param mixed  $value
+     * @return self
+     */
+    public function bindValue($name, $value)
+    {
+        $this->bind_values[$name] = $value;
+        return $this;
+    }
+
+    /**
+     * 生成 flag
+     *
+     * @return string
+     */
+    protected function buildFlags()
+    {
+        if (!$this->flags) {
+            return '';
+        }
+        return ' ' . implode(' ', array_keys($this->flags));
+    }
+
+    /**
+     * 设置 flag.
+     *
+     * @param string $flag
+     * @param bool   $enable
+     */
+    protected function setFlag($flag, $enable = true)
+    {
+        if ($enable) {
+            $this->flags[$flag] = true;
+        } else {
+            unset($this->flags[$flag]);
+        }
+    }
+
+    /**
+     * 重置 flag
+     */
+    protected function resetFlags()
+    {
+        $this->flags = array();
+    }
+
+    /**
+     *
+     * 添加 where 语句
+     *
+     * @param string $andor 'AND' or 'OR
+     * @param array  $conditions
+     * @return self
+     *
+     */
+    protected function addWhere($andor, $conditions)
+    {
+        $this->addClauseCondWithBind('where', $andor, $conditions);
+        return $this;
+    }
+
+    /**
+     * 添加条件和绑定值
+     *
+     * @param string $clause where 、having等
+     * @param string $andor  AND、OR等
+     * @param array  $conditions
+     */
+    protected function addClauseCondWithBind($clause, $andor, $conditions)
+    {
+        $cond = array_shift($conditions);
+        $cond = $this->quoteNamesIn($cond);
+
+        $bind =& $this->{"bind_{$clause}"};
+        foreach ($conditions as $value) {
+            $bind[] = $value;
+        }
+
+        $clause =& $this->$clause;
+        if ($clause) {
+            $clause[] = "$andor $cond";
+        } else {
+            $clause[] = $cond;
+        }
+    }
+
+    /**
+     * 生成 where 语句
+     *
+     * @return string
+     */
+    protected function buildWhere()
+    {
+        if (!$this->where) {
+            return '';
+        }
+        return ' WHERE' . $this->indent($this->where);
+    }
+
+    /**
+     * 增加 order by
+     *
+     * @param array $spec The columns and direction to order by.
+     * @return self
+     */
+    protected function addOrderBy(array $spec)
+    {
+        foreach ($spec as $col) {
+            $this->order_by[] = $this->quoteNamesIn($col);
+        }
+        return $this;
+    }
+
+    /**
+     * 生成 order by 语句
+     *
+     * @return string
+     */
+    protected function buildOrderBy()
+    {
+        if (!$this->order_by) {
+            return '';
+        }
+
+        if ($this->order_asc) {
+            return ' ORDER BY' . $this->indentCsv($this->order_by) . ' ASC';
+        } else {
+            return ' ORDER BY' . $this->indentCsv($this->order_by) . ' DESC';
+        }
+    }
+
+    /**
+     * 生成 limit 语句
+     *
+     * @return string
+     */
+    protected function buildLimit()
+    {
+        $has_limit  = $this->type == 'DELETE' || $this->type == 'UPDATE';
+        $has_offset = $this->type == 'SELECT';
+
+        if ($has_offset && $this->limit) {
+            $clause = " LIMIT {$this->limit}";
+            if ($this->offset) {
+                $clause .= " OFFSET {$this->offset}";
+            }
+            return $clause;
+        } elseif ($has_limit && $this->limit) {
+            return " LIMIT {$this->limit}";
+        }
+        return '';
+    }
+
+    /**
+     * Quotes
+     *
+     * @param string $spec
+     * @return string|array
+     */
+    public function quoteName($spec)
+    {
+        $spec = trim($spec);
+        $seps = array(' AS ', ' ', '.');
+        foreach ($seps as $sep) {
+            $pos = strripos($spec, $sep);
+            if ($pos) {
+                return $this->quoteNameWithSeparator($spec, $sep, $pos);
+            }
+        }
+        return $this->replaceName($spec);
+    }
+
+    /**
+     * 指定分隔符的 Quotes
+     *
+     * @param string $spec
+     * @param string $sep
+     * @param int    $pos
+     * @return string
+     */
+    protected function quoteNameWithSeparator($spec, $sep, $pos)
+    {
+        $len   = strlen($sep);
+        $part1 = $this->quoteName(substr($spec, 0, $pos));
+        $part2 = $this->replaceName(substr($spec, $pos + $len));
+        return "{$part1}{$sep}{$part2}";
+    }
+
+    /**
+     * Quotes "table.col" 格式的字符串
+     *
+     * @param string $text
+     * @return string|array
+     */
+    public function quoteNamesIn($text)
+    {
+        $list = $this->getListForQuoteNamesIn($text);
+        $last = count($list) - 1;
+        $text = null;
+        foreach ($list as $key => $val) {
+            if (($key + 1) % 3) {
+                $text .= $this->quoteNamesInLoop($val, $key == $last);
+            }
+        }
+        return $text;
+    }
+
+    /**
+     * 返回 quote 元素列表
+     *
+     * @param string $text
+     * @return array
+     */
+    protected function getListForQuoteNamesIn($text)
+    {
+        $apos = "'";
+        $quot = '"';
+        return preg_split(
+            "/(($apos+|$quot+|\\$apos+|\\$quot+).*?\\2)/",
+            $text,
+            -1,
+            PREG_SPLIT_DELIM_CAPTURE
+        );
+    }
+
+    /**
+     * 循环 quote
+     *
+     * @param string $val
+     * @param bool   $is_last
+     * @return string
+     */
+    protected function quoteNamesInLoop($val, $is_last)
+    {
+        if ($is_last) {
+            return $this->replaceNamesAndAliasIn($val);
+        }
+        return $this->replaceNamesIn($val);
+    }
+
+    /**
+     * 替换成别名
+     *
+     * @param string $val
+     * @return string
+     */
+    protected function replaceNamesAndAliasIn($val)
+    {
+        $quoted = $this->replaceNamesIn($val);
+        $pos    = strripos($quoted, ' AS ');
+        if ($pos) {
+            $alias  = $this->replaceName(substr($quoted, $pos + 4));
+            $quoted = substr($quoted, 0, $pos) . " AS $alias";
+        }
+        return $quoted;
+    }
+
+    /**
+     * Quotes name
+     *
+     * @param string $name
+     * @return string
+     */
+    protected function replaceName($name)
+    {
+        $name = trim($name);
+        if ($name == '*') {
+            return $name;
+        }
+        return '`' . $name . '`';
+    }
+
+    /**
+     * Quotes
+     *
+     * @param string $text
+     * @return string|array
+     */
+    protected function replaceNamesIn($text)
+    {
+        $is_string_literal = strpos($text, "'") !== false
+            || strpos($text, '"') !== false;
+        if ($is_string_literal) {
+            return $text;
+        }
+
+        $word = '[a-z_][a-z0-9_]+';
+
+        $find = "/(\\b)($word)\\.($word)(\\b)/i";
+
+        $repl = '$1`$2`.`$3`$4';
+
+        $text = preg_replace($find, $repl, $text);
+
+        return $text;
+    }
+
+    // ---------- insert --------------
+    /**
+     * 设置 `table.column` 与 last-insert-id 的映射
+     *
+     * @param array $last_insert_id_names
+     */
+    public function setLastInsertIdNames(array $last_insert_id_names)
+    {
+        $this->last_insert_id_names = $last_insert_id_names;
+    }
+
+    /**
+     * insert into.
+     *
+     * @param string $table
+     * @return self
+     */
+    public function into($table)
+    {
+        $this->table = $this->quoteName($table);
+        return $this;
+    }
+
+    /**
+     * 生成 INSERT 语句
+     *
+     * @return string
+     */
+    protected function buildINSERT()
+    {
+        return 'INSERT'
+        . $this->buildFlags()
+        . $this->buildInto()
+        . $this->buildValuesForInsert()
+        . $this->buildReturning();
+    }
+
+    /**
+     * 生成 INTO 语句
+     *
+     * @return string
+     */
+    protected function buildInto()
+    {
+        return " INTO " . $this->table;
+    }
+
+    /**
+     * PDO::lastInsertId()
+     *
+     * @param string $col
+     * @return mixed
+     */
+    public function getLastInsertIdName($col)
+    {
+        $key = str_replace('`', '', $this->table) . '.' . $col;
+        if (isset($this->last_insert_id_names[$key])) {
+            return $this->last_insert_id_names[$key];
+        }
+
+        return null;
+    }
+
+    /**
+     * 设置一列,如果有第二各参数,则把第二个参数绑定在占位符上
+     *
+     * @param string $col
+     * @return self
+     */
+    public function col($col)
+    {
+        return call_user_func_array(array($this, 'addCol'), func_get_args());
+    }
+
+    /**
+     * 设置多列
+     *
+     * @param array $cols
+     * @return self
+     */
+    public function cols(array $cols)
+    {
+        if ($this->type == 'SELECT') {
+            foreach ($cols as $key => $val) {
+                $this->addColSELECT($key, $val);
+            }
+            return $this;
+        }
+        return $this->addCols($cols);
+    }
+
+    /**
+     * 直接设置列的值
+     *
+     * @param string $col
+     * @param string $value
+     * @return self
+     */
+    public function set($col, $value)
+    {
+        return $this->setCol($col, $value);
+    }
+
+    /**
+     * 为 INSERT 语句绑定值
+     *
+     * @return string
+     */
+    protected function buildValuesForInsert()
+    {
+        return ' (' . $this->indentCsv(array_keys($this->col_values)) . ') VALUES (' .
+        $this->indentCsv(array_values($this->col_values)) . ')';
+    }
+
+    // ------update-------
+    /**
+     * 更新哪个表
+     *
+     * @param string $table
+     * @return self
+     */
+    public function table($table)
+    {
+        $this->table = $this->quoteName($table);
+        return $this;
+    }
+
+    /**
+     * 生成完整 SQL 语句
+     *
+     * @return string
+     * @throws Exception
+     */
+    protected function build()
+    {
+        switch ($this->type) {
+            case 'DELETE':
+                return $this->buildDELETE();
+            case 'INSERT':
+                return $this->buildINSERT();
+            case 'UPDATE':
+                return $this->buildUPDATE();
+            case 'SELECT':
+                return $this->buildSELECT();
+        }
+        throw new Exception("type empty");
+    }
+
+    /**
+     * 生成更新的 SQL 语句
+     */
+    protected function buildUPDATE()
+    {
+        return 'UPDATE'
+        . $this->buildFlags()
+        . $this->buildTable()
+        . $this->buildValuesForUpdate()
+        . $this->buildWhere()
+        . $this->buildOrderBy()
+        . $this->buildLimit()
+        . $this->buildReturning();
+    }
+
+    /**
+     * 哪个表
+     *
+     * @return string
+     */
+    protected function buildTable()
+    {
+        return " {$this->table}";
+    }
+
+    /**
+     * 为更新语句绑定值
+     *
+     * @return string
+     */
+    protected function buildValuesForUpdate()
+    {
+        $values = array();
+        foreach ($this->col_values as $col => $value) {
+            $values[] = "{$col} = {$value}";
+        }
+        return ' SET' . $this->indentCsv($values);
+    }
+
+    // ----------Dml---------------
+    /**
+     * 获取绑定的值
+     *
+     * @return array
+     */
+    public function getBindValuesCOMMON()
+    {
+        $bind_values = $this->bind_values;
+        $i           = 1;
+        foreach ($this->bind_where as $val) {
+            $bind_values[$i] = $val;
+            $i++;
+        }
+        return $bind_values;
+    }
+
+    /**
+     * 设置列
+     *
+     * @param string $col
+     * @return self
+     */
+    protected function addCol($col)
+    {
+        $key                    = $this->quoteName($col);
+        $this->col_values[$key] = ":$col";
+        $args                   = func_get_args();
+        if (count($args) > 1) {
+            $this->bindValue($col, $args[1]);
+        }
+        return $this;
+    }
+
+    /**
+     * 设置多个列
+     *
+     * @param array $cols
+     * @return self
+     */
+    protected function addCols(array $cols)
+    {
+        foreach ($cols as $key => $val) {
+            if (is_int($key)) {
+                $this->addCol($val);
+            } else {
+                $this->addCol($key, $val);
+            }
+        }
+        return $this;
+    }
+
+    /**
+     * 设置单列的值
+     *
+     * @param string $col .
+     * @param string $value
+     * @return self
+     */
+    protected function setCol($col, $value)
+    {
+        if ($value === null) {
+            $value = 'NULL';
+        }
+
+        $key                    = $this->quoteName($col);
+        $value                  = $this->quoteNamesIn($value);
+        $this->col_values[$key] = $value;
+        return $this;
+    }
+
+    /**
+     * 增加返回的列
+     *
+     * @param array $cols
+     * @return self
+     *
+     */
+    protected function addReturning(array $cols)
+    {
+        foreach ($cols as $col) {
+            $this->returning[] = $this->quoteNamesIn($col);
+        }
+        return $this;
+    }
+
+    /**
+     * 生成 RETURNING 语句
+     *
+     * @return string
+     */
+    protected function buildReturning()
+    {
+        if (!$this->returning) {
+            return '';
+        }
+        return ' RETURNING' . $this->indentCsv($this->returning);
+    }
+
+    /**
+     * 构造函数
+     *
+     * @param string $host
+     * @param int    $port
+     * @param string $user
+     * @param string $password
+     * @param string $db_name
+     * @param string $charset
+     */
+    public function __construct($host, $port, $user, $password, $db_name, $charset = 'utf8')
+    {
+        $this->settings = array(
+            'host'     => $host,
+            'port'     => $port,
+            'user'     => $user,
+            'password' => $password,
+            'dbname'   => $db_name,
+            'charset'  => $charset,
+        );
+        $this->connect();
+    }
+
+    /**
+     * 创建 PDO 实例
+     */
+    protected function connect()
+    {
+        $dsn       = 'mysql:dbname=' . $this->settings["dbname"] . ';host=' .
+            $this->settings["host"] . ';port=' . $this->settings['port'];
+        $this->pdo = new PDO($dsn, $this->settings["user"], $this->settings["password"],
+            array(
+                PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . (!empty($this->settings['charset']) ?
+                        $this->settings['charset'] : 'utf8')
+            ));
+        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+        $this->pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
+        $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
+    }
+
+   /**
+    * 关闭连接
+    */
+    public function closeConnection()
+    {
+        $this->pdo = null;
+    }
+
+    /**
+     * 执行
+     *
+     * @param string $query
+     * @param string $parameters
+     * @throws PDOException
+     */
+    protected function execute($query, $parameters = "")
+    {
+        try {
+            $this->sQuery = @$this->pdo->prepare($query);
+            $this->bindMore($parameters);
+            if (!empty($this->parameters)) {
+                foreach ($this->parameters as $param) {
+                    $parameters = explode("\x7F", $param);
+                    $this->sQuery->bindParam($parameters[0], $parameters[1]);
+                }
+            }
+            $this->success = $this->sQuery->execute();
+        } catch (PDOException $e) {
+            // 服务端断开时重连一次
+            if ($e->errorInfo[1] == 2006 || $e->errorInfo[1] == 2013) {
+                $this->closeConnection();
+                $this->connect();
+
+                try {
+                    $this->sQuery = $this->pdo->prepare($query);
+                    $this->bindMore($parameters);
+                    if (!empty($this->parameters)) {
+                        foreach ($this->parameters as $param) {
+                            $parameters = explode("\x7F", $param);
+                            $this->sQuery->bindParam($parameters[0], $parameters[1]);
+                        }
+                    }
+                    $this->success = $this->sQuery->execute();
+                } catch (PDOException $ex) {
+                    $this->rollBackTrans();
+                    throw $ex;
+                }
+            } else {
+                $this->rollBackTrans();
+                $msg = $e->getMessage();
+                $err_msg = "SQL:".$this->lastSQL()." ".$msg;
+                $exception = new \PDOException($err_msg, (int)$e->getCode());
+                throw $exception;
+            }
+        }
+        $this->parameters = array();
+    }
+
+    /**
+     * 绑定
+     *
+     * @param string $para
+     * @param string $value
+     */
+    public function bind($para, $value)
+    {
+        if (is_string($para)) {
+            $this->parameters[sizeof($this->parameters)] = ":" . $para . "\x7F" . $value;
+        } else {
+            $this->parameters[sizeof($this->parameters)] = $para . "\x7F" . $value;
+        }
+    }
+
+    /**
+     * 绑定多个
+     *
+     * @param array $parray
+     */
+    public function bindMore($parray)
+    {
+        if (empty($this->parameters) && is_array($parray)) {
+            $columns = array_keys($parray);
+            foreach ($columns as $i => &$column) {
+                $this->bind($column, $parray[$column]);
+            }
+        }
+    }
+
+    /**
+     * 执行 SQL
+     *
+     * @param string $query
+     * @param array  $params
+     * @param int    $fetchmode
+     * @return mixed
+     */
+    public function query($query = '', $params = null, $fetchmode = PDO::FETCH_ASSOC)
+    {
+        $query = trim($query);
+        if (empty($query)) {
+            $query = $this->build();
+            if (!$params) {
+                $params = $this->getBindValues();
+            }
+        }
+
+        $this->resetAll();
+        $this->lastSql = $query;
+
+        $this->execute($query, $params);
+
+        $rawStatement = explode(" ", $query);
+
+        $statement = strtolower(trim($rawStatement[0]));
+        if ($statement === 'select' || $statement === 'show') {
+            return $this->sQuery->fetchAll($fetchmode);
+        } elseif ($statement === 'update' || $statement === 'delete') {
+            return $this->sQuery->rowCount();
+        } elseif ($statement === 'insert') {
+            if ($this->sQuery->rowCount() > 0) {
+                return $this->lastInsertId();
+            }
+        } else {
+            return null;
+        }
+
+        return null;
+    }
+
+    /**
+     * 返回一列
+     *
+     * @param  string $query
+     * @param  array  $params
+     * @return array
+     */
+    public function column($query = '', $params = null)
+    {
+        $query = trim($query);
+        if (empty($query)) {
+            $query = $this->build();
+            if (!$params) {
+                $params = $this->getBindValues();
+            }
+        }
+
+        $this->resetAll();
+        $this->lastSql = $query;
+
+        $this->execute($query, $params);
+        $columns = $this->sQuery->fetchAll(PDO::FETCH_NUM);
+        $column  = null;
+        foreach ($columns as $cells) {
+            $column[] = $cells[0];
+        }
+        return $column;
+    }
+
+    /**
+     * 返回一行
+     *
+     * @param  string $query
+     * @param  array  $params
+     * @param  int    $fetchmode
+     * @return array
+     */
+    public function row($query = '', $params = null, $fetchmode = PDO::FETCH_ASSOC)
+    {
+        $query = trim($query);
+        if (empty($query)) {
+            $query = $this->build();
+            if (!$params) {
+                $params = $this->getBindValues();
+            }
+        }
+
+        $this->resetAll();
+        $this->lastSql = $query;
+
+        $this->execute($query, $params);
+        return $this->sQuery->fetch($fetchmode);
+    }
+
+    /**
+     * 返回单个值
+     *
+     * @param  string $query
+     * @param  array  $params
+     * @return string
+     */
+    public function single($query = '', $params = null)
+    {
+        $query = trim($query);
+        if (empty($query)) {
+            $query = $this->build();
+            if (!$params) {
+                $params = $this->getBindValues();
+            }
+        }
+
+        $this->resetAll();
+        $this->lastSql = $query;
+
+        $this->execute($query, $params);
+        return $this->sQuery->fetchColumn();
+    }
+
+    /**
+     * 返回 lastInsertId
+     *
+     * @return string
+     */
+    public function lastInsertId()
+    {
+        return $this->pdo->lastInsertId();
+    }
+
+    /**
+     * 返回最后一条执行的 sql
+     *
+     * @return  string
+     */
+    public function lastSQL()
+    {
+        return $this->lastSql;
+    }
+
+    /**
+     * 开始事务
+     */
+    public function beginTrans()
+    {
+        try {
+            $this->pdo->beginTransaction();
+        } catch (PDOException $e) {
+            // 服务端断开时重连一次
+            if ($e->errorInfo[1] == 2006 || $e->errorInfo[1] == 2013) {
+                $this->pdo->beginTransaction();
+            } else {
+                throw $e;
+            }
+        }
+    }
+
+    /**
+     * 提交事务
+     */
+    public function commitTrans()
+    {
+        $this->pdo->commit();
+    }
+
+    /**
+     * 事务回滚
+     */
+    public function rollBackTrans()
+    {
+        if ($this->pdo->inTransaction()) {
+            $this->pdo->rollBack();
+        }
+    }
+}

+ 1361 - 0
GatewayWorker/vendor/workerman/gateway-worker/src/Lib/Gateway.php

@@ -0,0 +1,1361 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link      http://www.workerman.net/
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace GatewayWorker\Lib;
+
+use Exception;
+use GatewayWorker\Protocols\GatewayProtocol;
+use Workerman\Connection\TcpConnection;
+
+/**
+ * 数据发送相关
+ */
+class Gateway
+{
+    /**
+     * gateway 实例
+     *
+     * @var object
+     */
+    protected static $businessWorker = null;
+
+    /**
+     * 注册中心地址
+     *
+     * @var string|array
+     */
+    public static $registerAddress = '127.0.0.1:1236';
+
+    /**
+     * 秘钥
+     * @var string
+     */
+    public static $secretKey = '';
+
+    /**
+     * 链接超时时间
+     * @var int
+     */
+    public static $connectTimeout = 3;
+
+    /**
+     * 与Gateway是否是长链接
+     * @var bool
+     */
+    public static $persistentConnection = false;
+    
+    /**
+     * 向所有客户端连接(或者 client_id_array 指定的客户端连接)广播消息
+     *
+     * @param string $message           向客户端发送的消息
+     * @param array  $client_id_array   客户端 id 数组
+     * @param array  $exclude_client_id 不给这些client_id发
+     * @param bool   $raw               是否发送原始数据(即不调用gateway的协议的encode方法)
+     * @return void
+     * @throws Exception
+     */
+    public static function sendToAll($message, $client_id_array = null, $exclude_client_id = null, $raw = false)
+    {
+        $gateway_data         = GatewayProtocol::$empty;
+        $gateway_data['cmd']  = GatewayProtocol::CMD_SEND_TO_ALL;
+        $gateway_data['body'] = $message;
+        if ($raw) {
+            $gateway_data['flag'] |= GatewayProtocol::FLAG_NOT_CALL_ENCODE;
+        }
+
+        if ($exclude_client_id) {
+            if (!is_array($exclude_client_id)) {
+                $exclude_client_id = array($exclude_client_id);
+            }
+            if ($client_id_array) {
+                $exclude_client_id = array_flip($exclude_client_id);
+            }
+        }
+
+        if ($client_id_array) {
+            if (!is_array($client_id_array)) {
+                echo new \Exception('bad $client_id_array:'.var_export($client_id_array, true));
+                return;
+            }
+            $data_array = array();
+            foreach ($client_id_array as $client_id) {
+                if (isset($exclude_client_id[$client_id])) {
+                    continue;
+                }
+                $address = Context::clientIdToAddress($client_id);
+                if ($address) {
+                    $key                                         = long2ip($address['local_ip']) . ":{$address['local_port']}";
+                    $data_array[$key][$address['connection_id']] = $address['connection_id'];
+                }
+            }
+            foreach ($data_array as $addr => $connection_id_list) {
+                $the_gateway_data             = $gateway_data;
+                $the_gateway_data['ext_data'] = json_encode(array('connections' => $connection_id_list));
+                static::sendToGateway($addr, $the_gateway_data);
+            }
+            return;
+        } elseif (empty($client_id_array) && is_array($client_id_array)) {
+            return;
+        }
+
+        if (!$exclude_client_id) {
+            return static::sendToAllGateway($gateway_data);
+        }
+
+        $address_connection_array = static::clientIdArrayToAddressArray($exclude_client_id);
+
+        // 如果有businessWorker实例,说明运行在workerman环境中,通过businessWorker中的长连接发送数据
+        if (static::$businessWorker) {
+            foreach (static::$businessWorker->gatewayConnections as $address => $gateway_connection) {
+                $gateway_data['ext_data'] = isset($address_connection_array[$address]) ?
+                    json_encode(array('exclude'=> $address_connection_array[$address])) : '';
+                /** @var TcpConnection $gateway_connection */
+                $gateway_connection->send($gateway_data);
+            }
+        } // 运行在其它环境中,通过注册中心得到gateway地址
+        else {
+            $all_addresses = static::getAllGatewayAddressesFromRegister();
+            foreach ($all_addresses as $address) {
+                $gateway_data['ext_data'] = isset($address_connection_array[$address]) ?
+                    json_encode(array('exclude'=> $address_connection_array[$address])) : '';
+                static::sendToGateway($address, $gateway_data);
+            }
+        }
+
+    }
+
+    /**
+     * 向某个client_id对应的连接发消息
+     *
+     * @param int    $client_id
+     * @param string $message
+     * @return void
+     */
+    public static function sendToClient($client_id, $message)
+    {
+        return static::sendCmdAndMessageToClient($client_id, GatewayProtocol::CMD_SEND_TO_ONE, $message);
+    }
+
+    /**
+     * 向当前客户端连接发送消息
+     *
+     * @param string $message
+     * @return bool
+     */
+    public static function sendToCurrentClient($message)
+    {
+        return static::sendCmdAndMessageToClient(null, GatewayProtocol::CMD_SEND_TO_ONE, $message);
+    }
+
+    /**
+     * 判断某个uid是否在线
+     *
+     * @param string $uid
+     * @return int 0|1
+     */
+    public static function isUidOnline($uid)
+    {
+        return (int)static::getClientIdByUid($uid);
+    }
+    
+    /**
+     * 判断client_id对应的连接是否在线
+     *
+     * @param int $client_id
+     * @return int 0|1
+     */
+    public static function isOnline($client_id)
+    {
+        $address_data = Context::clientIdToAddress($client_id);
+        if (!$address_data) {
+            return 0;
+        }
+        $address      = long2ip($address_data['local_ip']) . ":{$address_data['local_port']}";
+        if (isset(static::$businessWorker)) {
+            if (!isset(static::$businessWorker->gatewayConnections[$address])) {
+                return 0;
+            }
+        }
+        $gateway_data                  = GatewayProtocol::$empty;
+        $gateway_data['cmd']           = GatewayProtocol::CMD_IS_ONLINE;
+        $gateway_data['connection_id'] = $address_data['connection_id'];
+        return (int)static::sendAndRecv($address, $gateway_data);
+    }
+
+    /**
+     * 获取所有在线用户的session,client_id为 key(弃用,请用getAllClientSessions代替)
+     *
+     * @param string $group
+     * @return array
+     */
+    public static function getAllClientInfo($group = '')
+    {
+        echo "Warning: Gateway::getAllClientInfo is deprecated and will be removed in a future, please use Gateway::getAllClientSessions instead.";
+        return static::getAllClientSessions($group);
+    }
+
+    /**
+     * 获取所有在线client_id的session,client_id为 key
+     *
+     * @param string $group
+     * @return array
+     */
+    public static function getAllClientSessions($group = '')
+    {
+        $gateway_data = GatewayProtocol::$empty;
+        if (!$group) {
+            $gateway_data['cmd']      = GatewayProtocol::CMD_GET_ALL_CLIENT_SESSIONS;
+        } else {
+            $gateway_data['cmd']      = GatewayProtocol::CMD_GET_CLIENT_SESSIONS_BY_GROUP;
+            $gateway_data['ext_data'] = $group;
+        }
+        $status_data      = array();
+        $all_buffer_array = static::getBufferFromAllGateway($gateway_data);
+        foreach ($all_buffer_array as $local_ip => $buffer_array) {
+            foreach ($buffer_array as $local_port => $data) {
+                if ($data) {
+                    foreach ($data as $connection_id => $session_buffer) {
+                        $client_id = Context::addressToClientId($local_ip, $local_port, $connection_id);
+                        if ($client_id === Context::$client_id) {
+                            $status_data[$client_id] = (array)$_SESSION;
+                        } else {
+                            $status_data[$client_id] = $session_buffer ? Context::sessionDecode($session_buffer) : array();
+                        }
+                    }
+                }
+            }
+        }
+        return $status_data;
+    }
+
+    /**
+     * 获取某个组的连接信息(弃用,请用getClientSessionsByGroup代替)
+     *
+     * @param string $group
+     * @return array
+     */
+    public static function getClientInfoByGroup($group)
+    {
+        echo "Warning: Gateway::getClientInfoByGroup is deprecated and will be removed in a future, please use Gateway::getClientSessionsByGroup instead.";
+        return static::getAllClientSessions($group);
+    }
+
+    /**
+     * 获取某个组的所有client_id的session信息
+     *
+     * @param string $group
+     *
+     * @return array
+     */
+    public static function getClientSessionsByGroup($group)
+    {
+        if (static::isValidGroupId($group)) {
+            return static::getAllClientSessions($group);
+        }
+        return array();
+    }
+
+    /**
+     * 获取所有在线client_id数
+     *
+     * @return int
+     */
+    public static function getAllClientIdCount()
+    {
+        return static::getClientCountByGroup();
+    }
+
+    /**
+     * 获取所有在线client_id数(getAllClientIdCount的别名)
+     *
+     * @return int
+     */
+    public static function getAllClientCount()
+    {
+        return static::getAllClientIdCount();
+    }
+
+    /**
+     * 获取某个组的在线client_id数
+     *
+     * @param string $group
+     * @return int
+     */
+    public static function getClientIdCountByGroup($group = '')
+    {
+        $gateway_data             = GatewayProtocol::$empty;
+        $gateway_data['cmd']      = GatewayProtocol::CMD_GET_CLIENT_COUNT_BY_GROUP;
+        $gateway_data['ext_data'] = $group;
+        $total_count              = 0;
+        $all_buffer_array         = static::getBufferFromAllGateway($gateway_data);
+        foreach ($all_buffer_array as $local_ip => $buffer_array) {
+            foreach ($buffer_array as $local_port => $count) {
+                if ($count) {
+                    $total_count += $count;
+                }
+            }
+        }
+        return $total_count;
+    }
+
+    /**
+     * getClientIdCountByGroup 函数的别名
+     *
+     * @param string $group
+     * @return int
+     */
+    public static function getClientCountByGroup($group = '')
+    {
+        return static::getClientIdCountByGroup($group);
+    }
+
+    /**
+     * 获取某个群组在线client_id列表
+     *
+     * @param string $group
+     * @return array
+     */
+    public static function getClientIdListByGroup($group)
+    {
+        if (!static::isValidGroupId($group)) {
+            return array();
+        }
+
+        $data = static::select(array('uid'), array('groups' => is_array($group) ? $group : array($group)));
+        $client_id_map = array();
+        foreach ($data as $local_ip => $buffer_array) {
+            foreach ($buffer_array as $local_port => $items) {
+                //$items = ['connection_id'=>['uid'=>x, 'group'=>[x,x..], 'session'=>[..]], 'client_id'=>[..], ..];
+                foreach ($items as $connection_id => $info) {
+                    $client_id = Context::addressToClientId($local_ip, $local_port, $connection_id);
+                    $client_id_map[$client_id] = $client_id;
+                }
+            }
+        }
+        return $client_id_map;
+    }
+
+    /**
+     * 获取集群所有在线client_id列表
+     *
+     * @return array
+     */
+    public static function getAllClientIdList()
+    {
+        return static::formatClientIdFromGatewayBuffer(static::select(array('uid')));
+    }
+
+    /**
+     * 格式化client_id
+     *
+     * @param $data
+     * @return array
+     */
+    protected static function formatClientIdFromGatewayBuffer($data)
+    {
+        $client_id_list = array();
+        foreach ($data as $local_ip => $buffer_array) {
+            foreach ($buffer_array as $local_port => $items) {
+                //$items = ['connection_id'=>['uid'=>x, 'group'=>[x,x..], 'session'=>[..]], 'client_id'=>[..], ..];
+                foreach ($items as $connection_id => $info) {
+                    $client_id = Context::addressToClientId($local_ip, $local_port, $connection_id);
+                    $client_id_list[$client_id] = $client_id;
+                }
+            }
+        }
+        return $client_id_list;
+    }
+
+
+    /**
+     * 获取与 uid 绑定的 client_id 列表
+     *
+     * @param string $uid
+     * @return array
+     */
+    public static function getClientIdByUid($uid)
+    {
+        $gateway_data             = GatewayProtocol::$empty;
+        $gateway_data['cmd']      = GatewayProtocol::CMD_GET_CLIENT_ID_BY_UID;
+        $gateway_data['ext_data'] = $uid;
+        $client_list              = array();
+        $all_buffer_array         = static::getBufferFromAllGateway($gateway_data);
+        foreach ($all_buffer_array as $local_ip => $buffer_array) {
+            foreach ($buffer_array as $local_port => $connection_id_array) {
+                if ($connection_id_array) {
+                    foreach ($connection_id_array as $connection_id) {
+                        $client_list[] = Context::addressToClientId($local_ip, $local_port, $connection_id);
+                    }
+                }
+            }
+        }
+        return $client_list;
+    }
+
+    /**
+     * 获取某个群组在线uid列表
+     *
+     * @param string $group
+     * @return array
+     */
+    public static function getUidListByGroup($group)
+    {
+        if (!static::isValidGroupId($group)) {
+            return array();
+        }
+
+        $group = is_array($group) ? $group : array($group);
+        $data = static::select(array('uid'), array('groups' => $group));
+        $uid_map = array();
+        foreach ($data as $local_ip => $buffer_array) {
+            foreach ($buffer_array as $local_port => $items) {
+                //$items = ['connection_id'=>['uid'=>x, 'group'=>[x,x..], 'session'=>[..]], 'client_id'=>[..], ..];
+                foreach ($items as $connection_id => $info) {
+                    if (!empty($info['uid'])) {
+                        $uid_map[$info['uid']] = $info['uid'];
+                    }
+                }
+            }
+        }
+        return $uid_map;
+    }
+
+    /**
+     * 获取某个群组在线uid数
+     *
+     * @param string $group
+     * @return int
+     */
+    public static function getUidCountByGroup($group)
+    {
+        if (static::isValidGroupId($group)) {
+            return count(static::getUidListByGroup($group));
+        }
+        return 0;
+    }
+
+    /**
+     * 获取全局在线uid列表
+     *
+     * @return array
+     */
+    public static function getAllUidList()
+    {
+        $data = static::select(array('uid'));
+        $uid_map = array();
+        foreach ($data as $local_ip => $buffer_array) {
+            foreach ($buffer_array as $local_port => $items) {
+                //$items = ['connection_id'=>['uid'=>x, 'group'=>[x,x..], 'session'=>[..]], 'client_id'=>[..], ..];
+                foreach ($items as $connection_id => $info) {
+                    if (!empty($info['uid'])) {
+                        $uid_map[$info['uid']] = $info['uid'];
+                    }
+                }
+            }
+        }
+        return $uid_map;
+    }
+
+    /**
+     * 获取全局在线uid数
+     * @return int
+     */
+    public static function getAllUidCount()
+    {
+        return count(static::getAllUidList());
+    }
+
+    /**
+     * 通过client_id获取uid
+     *
+     * @param $client_id
+     * @return mixed
+     */
+    public static function getUidByClientId($client_id)
+    {
+        $data = static::select(array('uid'), array('client_id'=>array($client_id)));
+        foreach ($data as $local_ip => $buffer_array) {
+            foreach ($buffer_array as $local_port => $items) {
+                //$items = ['connection_id'=>['uid'=>x, 'group'=>[x,x..], 'session'=>[..]], 'client_id'=>[..], ..];
+                foreach ($items as $info) {
+                    return $info['uid'];
+                }
+            }
+        }
+    }
+
+    /**
+     * 获取所有在线的群组id
+     *
+     * @return array
+     */
+    public static function getAllGroupIdList()
+    {
+        $gateway_data             = GatewayProtocol::$empty;
+        $gateway_data['cmd']      = GatewayProtocol::CMD_GET_GROUP_ID_LIST;
+        $group_id_list            = array();
+        $all_buffer_array         = static::getBufferFromAllGateway($gateway_data);
+        foreach ($all_buffer_array as $local_ip => $buffer_array) {
+            foreach ($buffer_array as $local_port => $group_id_array) {
+                if (is_array($group_id_array)) {
+                    foreach ($group_id_array as $group_id) {
+                        if (!isset($group_id_list[$group_id])) {
+                            $group_id_list[$group_id] = $group_id;
+                        }
+                    }
+                }
+            }
+        }
+        return $group_id_list;
+    }
+
+
+    /**
+     * 获取所有在线分组的uid数量,也就是每个分组的在线用户数
+     *
+     * @return array
+     */
+    public static function getAllGroupUidCount()
+    {
+        $group_uid_map = static::getAllGroupUidList();
+        $group_uid_count_map = array();
+        foreach ($group_uid_map as $group_id => $uid_list) {
+            $group_uid_count_map[$group_id] = count($uid_list);
+        }
+        return $group_uid_count_map;
+    }
+
+
+
+    /**
+     * 获取所有分组uid在线列表
+     *
+     * @return array
+     */
+    public static function getAllGroupUidList()
+    {
+        $data = static::select(array('uid','groups'));
+        $group_uid_map = array();
+        foreach ($data as $local_ip => $buffer_array) {
+            foreach ($buffer_array as $local_port => $items) {
+                //$items = ['connection_id'=>['uid'=>x, 'group'=>[x,x..], 'session'=>[..]], 'client_id'=>[..], ..];
+                foreach ($items as $connection_id => $info) {
+                    if (empty($info['uid']) || empty($info['groups'])) {
+                        break;
+                    }
+                    $uid = $info['uid'];
+                    foreach ($info['groups'] as $group_id) {
+                        if(!isset($group_uid_map[$group_id])) {
+                            $group_uid_map[$group_id] = array();
+                        }
+                        $group_uid_map[$group_id][$uid] = $uid;
+                    }
+                }
+            }
+        }
+        return $group_uid_map;
+    }
+
+    /**
+     * 获取所有群组在线client_id列表
+     *
+     * @return array
+     */
+    public static function getAllGroupClientIdList()
+    {
+        $data = static::select(array('groups'));
+        $group_client_id_map = array();
+        foreach ($data as $local_ip => $buffer_array) {
+            foreach ($buffer_array as $local_port => $items) {
+                //$items = ['connection_id'=>['uid'=>x, 'group'=>[x,x..], 'session'=>[..]], 'client_id'=>[..], ..];
+                foreach ($items as $connection_id => $info) {
+                    if (empty($info['groups'])) {
+                        break;
+                    }
+                    $client_id = Context::addressToClientId($local_ip, $local_port, $connection_id);
+                    foreach ($info['groups'] as $group_id) {
+                        if(!isset($group_client_id_map[$group_id])) {
+                            $group_client_id_map[$group_id] = array();
+                        }
+                        $group_client_id_map[$group_id][$client_id] = $client_id;
+                    }
+                }
+            }
+        }
+        return $group_client_id_map;
+    }
+
+    /**
+     * 获取所有群组在线client_id数量,也就是获取每个群组在线连接数
+     *
+     * @return array
+     */
+    public static function getAllGroupClientIdCount()
+    {
+        $group_client_map = static::getAllGroupClientIdList();
+        $group_client_count_map = array();
+        foreach ($group_client_map as $group_id => $client_id_list) {
+            $group_client_count_map[$group_id] = count($client_id_list);
+        }
+        return $group_client_count_map;
+    }
+
+
+    /**
+     * 根据条件到gateway搜索数据
+     *
+     * @param array $fields
+     * @param array $where
+     * @return array
+     */
+    protected static function select($fields = array('session','uid','groups'), $where = array())
+    {
+        $t = microtime(true);
+        $gateway_data             = GatewayProtocol::$empty;
+        $gateway_data['cmd']      = GatewayProtocol::CMD_SELECT;
+        $gateway_data['ext_data'] = array('fields' => $fields, 'where' => $where);
+        $gateway_data_list   = array();
+        // 有client_id,能计算出需要和哪些gateway通讯,只和必要的gateway通讯能降低系统负载
+        if (isset($where['client_id'])) {
+            $client_id_list = $where['client_id'];
+            unset($gateway_data['ext_data']['where']['client_id']);
+            $gateway_data['ext_data']['where']['connection_id'] = array();
+            foreach ($client_id_list as $client_id) {
+                $address_data = Context::clientIdToAddress($client_id);
+                if (!$address_data) {
+                    continue;
+                }
+                $address = long2ip($address_data['local_ip']) . ":{$address_data['local_port']}";
+                if (!isset($gateway_data_list[$address])) {
+                    $gateway_data_list[$address] = $gateway_data;
+                }
+                $gateway_data_list[$address]['ext_data']['where']['connection_id'][$address_data['connection_id']] = $address_data['connection_id'];
+            }
+            foreach ($gateway_data_list as $address => $item) {
+                $gateway_data_list[$address]['ext_data'] = json_encode($item['ext_data']);
+            }
+            // 有其它条件,则还是需要向所有gateway发送
+            if (count($where) !== 1) {
+                $gateway_data['ext_data'] = json_encode($gateway_data['ext_data']);
+                foreach (static::getAllGatewayAddress() as $address) {
+                    if (!isset($gateway_data_list[$address])) {
+                        $gateway_data_list[$address] = $gateway_data;
+                    }
+                }
+            }
+            $data = static::getBufferFromSomeGateway($gateway_data_list);
+        } else {
+            $gateway_data['ext_data'] = json_encode($gateway_data['ext_data']);
+            $data = static::getBufferFromAllGateway($gateway_data);
+        }
+
+        return $data;
+    }
+
+    /**
+     * 生成验证包,用于验证此客户端的合法性
+     * 
+     * @return string
+     */
+    protected static function generateAuthBuffer()
+    {
+        $gateway_data         = GatewayProtocol::$empty;
+        $gateway_data['cmd']  = GatewayProtocol::CMD_GATEWAY_CLIENT_CONNECT;
+        $gateway_data['body'] = json_encode(array(
+            'secret_key' => static::$secretKey,
+        ));
+        return GatewayProtocol::encode($gateway_data);
+    }
+
+    /**
+     * 批量向某些gateway发包,并得到返回数组
+     *
+     * @param array $gateway_data_array
+     * @return array
+     * @throws Exception
+     */
+    protected static function getBufferFromSomeGateway($gateway_data_array)
+    {
+        $gateway_buffer_array = array();
+        $auth_buffer = static::$secretKey ? static::generateAuthBuffer() : '';
+        foreach ($gateway_data_array as $address => $gateway_data) {
+            if ($auth_buffer) {
+                $gateway_buffer_array[$address] = $auth_buffer.GatewayProtocol::encode($gateway_data);
+            } else {
+                $gateway_buffer_array[$address] = GatewayProtocol::encode($gateway_data);
+            }
+        }
+        return static::getBufferFromGateway($gateway_buffer_array);
+    }
+
+    /**
+     * 批量向所有 gateway 发包,并得到返回数组
+     *
+     * @param string $gateway_data
+     * @return array
+     * @throws Exception
+     */
+    protected static function getBufferFromAllGateway($gateway_data)
+    {
+        $addresses = static::getAllGatewayAddress();
+        $gateway_buffer_array = array();
+        $gateway_buffer = GatewayProtocol::encode($gateway_data);
+        $gateway_buffer = static::$secretKey ? static::generateAuthBuffer() . $gateway_buffer : $gateway_buffer;
+        foreach ($addresses as $address) {
+            $gateway_buffer_array[$address] = $gateway_buffer;
+        }
+
+        return static::getBufferFromGateway($gateway_buffer_array);
+    }
+
+    /**
+     * 获取所有gateway内部通讯地址
+     *
+     * @return array
+     * @throws Exception
+     */
+    protected static function getAllGatewayAddress()
+    {
+        if (isset(static::$businessWorker)) {
+            $addresses = static::$businessWorker->getAllGatewayAddresses();
+            if (empty($addresses)) {
+                throw new Exception('businessWorker::getAllGatewayAddresses return empty');
+            }
+        } else {
+            $addresses = static::getAllGatewayAddressesFromRegister();
+            if (empty($addresses)) {
+                return array();
+            }
+        }
+        return $addresses;
+    }
+
+    /**
+     * 批量向gateway发送并获取数据
+     * @param $gateway_buffer_array
+     * @return array
+     */
+    protected static function getBufferFromGateway($gateway_buffer_array)
+    {
+        $client_array = $status_data = $client_address_map = $receive_buffer_array = $recv_length_array = array();
+        // 批量向所有gateway进程发送请求数据
+        foreach ($gateway_buffer_array as $address => $gateway_buffer) {
+            $client = stream_socket_client("tcp://$address", $errno, $errmsg, static::$connectTimeout);
+            if ($client && strlen($gateway_buffer) === stream_socket_sendto($client, $gateway_buffer)) {
+                $socket_id                        = (int)$client;
+                $client_array[$socket_id]         = $client;
+                $client_address_map[$socket_id]   = explode(':', $address);
+                $receive_buffer_array[$socket_id] = '';
+            }
+        }
+        // 超时5秒
+        $timeout    = 5;
+        $time_start = microtime(true);
+        // 批量接收请求
+        while (count($client_array) > 0) {
+            $write = $except = array();
+            $read  = $client_array;
+            if (@stream_select($read, $write, $except, $timeout)) {
+                foreach ($read as $client) {
+                    $socket_id = (int)$client;
+                    $buffer    = stream_socket_recvfrom($client, 65535);
+                    if ($buffer !== '' && $buffer !== false) {
+                        $receive_buffer_array[$socket_id] .= $buffer;
+                        $receive_length = strlen($receive_buffer_array[$socket_id]);
+                        if (empty($recv_length_array[$socket_id]) && $receive_length >= 4) {
+                            $recv_length_array[$socket_id] = current(unpack('N', $receive_buffer_array[$socket_id]));
+                        }
+                        if (!empty($recv_length_array[$socket_id]) && $receive_length >= $recv_length_array[$socket_id] + 4) {
+                            unset($client_array[$socket_id]);
+                        }
+                    } elseif (feof($client)) {
+                        unset($client_array[$socket_id]);
+                    }
+                }
+            }
+            if (microtime(true) - $time_start > $timeout) {
+                break;
+            }
+        }
+        $format_buffer_array = array();
+        foreach ($receive_buffer_array as $socket_id => $buffer) {
+            $local_ip                                    = ip2long($client_address_map[$socket_id][0]);
+            $local_port                                  = $client_address_map[$socket_id][1];
+            $format_buffer_array[$local_ip][$local_port] = unserialize(substr($buffer, 4));
+        }
+        return $format_buffer_array;
+    }
+
+    /**
+     * 踢掉某个客户端,并以$message通知被踢掉客户端
+     *
+     * @param string $client_id
+     * @param string $message
+     * @return void
+     */
+    public static function closeClient($client_id, $message = null)
+    {
+        if ($client_id === Context::$client_id) {
+            return static::closeCurrentClient($message);
+        } // 不是发给当前用户则使用存储中的地址
+        else {
+            $address_data = Context::clientIdToAddress($client_id);
+            if (!$address_data) {
+                return false;
+            }
+            $address      = long2ip($address_data['local_ip']) . ":{$address_data['local_port']}";
+            return static::kickAddress($address, $address_data['connection_id'], $message);
+        }
+    }
+
+    /**
+     * 踢掉当前客户端,并以$message通知被踢掉客户端
+     *
+     * @param string $message
+     * @return bool
+     * @throws Exception
+     */
+    public static function closeCurrentClient($message = null)
+    {
+        if (!Context::$connection_id) {
+            throw new Exception('closeCurrentClient can not be called in async context');
+        }
+        $address = long2ip(Context::$local_ip) . ':' . Context::$local_port;
+        return static::kickAddress($address, Context::$connection_id, $message);
+    }
+
+    /**
+     * 踢掉某个客户端并直接立即销毁相关连接
+     *
+     * @param int $client_id
+     * @return bool
+     */
+    public static function destoryClient($client_id)
+    {
+        if ($client_id === Context::$client_id) {
+            return static::destoryCurrentClient();
+        } // 不是发给当前用户则使用存储中的地址
+        else {
+            $address_data = Context::clientIdToAddress($client_id);
+            if (!$address_data) {
+                return false;
+            }
+            $address = long2ip($address_data['local_ip']) . ":{$address_data['local_port']}";
+            return static::destroyAddress($address, $address_data['connection_id']);
+        }
+    }
+
+    /**
+     * 踢掉当前客户端并直接立即销毁相关连接
+     *
+     * @return bool
+     * @throws Exception
+     */
+    public static function destoryCurrentClient()
+    {
+        if (!Context::$connection_id) {
+            throw new Exception('destoryCurrentClient can not be called in async context');
+        }
+        $address = long2ip(Context::$local_ip) . ':' . Context::$local_port;
+        return static::destroyAddress($address, Context::$connection_id);
+    }
+
+    /**
+     * 将 client_id 与 uid 绑定
+     *
+     * @param int        $client_id
+     * @param int|string $uid
+     * @return void
+     */
+    public static function bindUid($client_id, $uid)
+    {
+        static::sendCmdAndMessageToClient($client_id, GatewayProtocol::CMD_BIND_UID, '', $uid);
+    }
+
+    /**
+     * 将 client_id 与 uid 解除绑定
+     *
+     * @param int        $client_id
+     * @param int|string $uid
+     * @return void
+     */
+    public static function unbindUid($client_id, $uid)
+    {
+        static::sendCmdAndMessageToClient($client_id, GatewayProtocol::CMD_UNBIND_UID, '', $uid);
+    }
+
+    /**
+     * 将 client_id 加入组
+     *
+     * @param int        $client_id
+     * @param int|string $group
+     * @return void
+     */
+    public static function joinGroup($client_id, $group)
+    {
+
+        static::sendCmdAndMessageToClient($client_id, GatewayProtocol::CMD_JOIN_GROUP, '', $group);
+    }
+
+    /**
+     * 将 client_id 离开组
+     *
+     * @param int        $client_id
+     * @param int|string $group
+     *
+     * @return void
+     */
+    public static function leaveGroup($client_id, $group)
+    {
+        static::sendCmdAndMessageToClient($client_id, GatewayProtocol::CMD_LEAVE_GROUP, '', $group);
+    }
+
+    /**
+     * 取消分组
+     *
+     * @param int|string $group
+     *
+     * @return void
+     */
+    public static function ungroup($group)
+    {
+        if (!static::isValidGroupId($group)) {
+            return false;
+        }
+        $gateway_data             = GatewayProtocol::$empty;
+        $gateway_data['cmd']      = GatewayProtocol::CMD_UNGROUP;
+        $gateway_data['ext_data'] = $group;
+        return static::sendToAllGateway($gateway_data);
+
+    }
+
+    /**
+     * 向所有 uid 发送
+     *
+     * @param int|string|array $uid
+     * @param string           $message
+     *
+     * @return void
+     */
+    public static function sendToUid($uid, $message)
+    {
+        $gateway_data         = GatewayProtocol::$empty;
+        $gateway_data['cmd']  = GatewayProtocol::CMD_SEND_TO_UID;
+        $gateway_data['body'] = $message;
+
+        if (!is_array($uid)) {
+            $uid = array($uid);
+        }
+
+        $gateway_data['ext_data'] = json_encode($uid);
+
+        static::sendToAllGateway($gateway_data);
+    }
+
+    /**
+     * 向 group 发送
+     *
+     * @param int|string|array $group             组(不允许是 0 '0' false null array()等为空的值)
+     * @param string           $message           消息
+     * @param array            $exclude_client_id 不给这些client_id发
+     * @param bool             $raw               发送原始数据(即不调用gateway的协议的encode方法)
+     *
+     * @return void
+     */
+    public static function sendToGroup($group, $message, $exclude_client_id = null, $raw = false)
+    {
+        if (!static::isValidGroupId($group)) {
+            return false;
+        }
+        $gateway_data         = GatewayProtocol::$empty;
+        $gateway_data['cmd']  = GatewayProtocol::CMD_SEND_TO_GROUP;
+        $gateway_data['body'] = $message;
+        if ($raw) {
+            $gateway_data['flag'] |= GatewayProtocol::FLAG_NOT_CALL_ENCODE;
+        }
+
+        if (!is_array($group)) {
+            $group = array($group);
+        }
+
+        // 分组发送,没有排除的client_id,直接发送
+        $default_ext_data_buffer = json_encode(array('group'=> $group, 'exclude'=> null));
+        if (empty($exclude_client_id)) {
+            $gateway_data['ext_data'] = $default_ext_data_buffer;
+            return static::sendToAllGateway($gateway_data);
+        }
+
+        // 分组发送,有排除的client_id,需要将client_id转换成对应gateway进程内的connectionId
+        if (!is_array($exclude_client_id)) {
+            $exclude_client_id = array($exclude_client_id);
+        }
+
+        $address_connection_array = static::clientIdArrayToAddressArray($exclude_client_id);
+        // 如果有businessWorker实例,说明运行在workerman环境中,通过businessWorker中的长连接发送数据
+        if (static::$businessWorker) {
+            foreach (static::$businessWorker->gatewayConnections as $address => $gateway_connection) {
+                $gateway_data['ext_data'] = isset($address_connection_array[$address]) ?
+                    json_encode(array('group'=> $group, 'exclude'=> $address_connection_array[$address])) :
+                    $default_ext_data_buffer;
+                /** @var TcpConnection $gateway_connection */
+                $gateway_connection->send($gateway_data);
+            }
+        } // 运行在其它环境中,通过注册中心得到gateway地址
+        else {
+            $addresses = static::getAllGatewayAddressesFromRegister();
+            foreach ($addresses as $address) {
+                $gateway_data['ext_data'] = isset($address_connection_array[$address]) ?
+                    json_encode(array('group'=> $group, 'exclude'=> $address_connection_array[$address])) :
+                    $default_ext_data_buffer;
+                static::sendToGateway($address, $gateway_data);
+            }
+        }
+    }
+
+    /**
+     * 更新 session,框架自动调用,开发者不要调用
+     *
+     * @param int    $client_id
+     * @param string $session_str
+     * @return bool
+     */
+    public static function setSocketSession($client_id, $session_str)
+    {
+        return static::sendCmdAndMessageToClient($client_id, GatewayProtocol::CMD_SET_SESSION, '', $session_str);
+    }
+
+    /**
+     * 设置 session,原session值会被覆盖
+     *
+     * @param int   $client_id
+     * @param array $session
+     *
+     * @return void
+     */
+    public static function setSession($client_id, array $session)
+    {
+        if (Context::$client_id === $client_id) {
+            $_SESSION = $session;
+            Context::$old_session = $_SESSION;
+        }
+        static::setSocketSession($client_id, Context::sessionEncode($session));
+    }
+    
+    /**
+     * 更新 session,实际上是与老的session合并
+     *
+     * @param int   $client_id
+     * @param array $session
+     *
+     * @return void
+     */
+    public static function updateSession($client_id, array $session)
+    {
+        if (Context::$client_id === $client_id) {
+            $_SESSION = array_replace_recursive((array)$_SESSION, $session);
+            Context::$old_session = $_SESSION;
+        }
+        static::sendCmdAndMessageToClient($client_id, GatewayProtocol::CMD_UPDATE_SESSION, '', Context::sessionEncode($session));
+    }
+    
+    /**
+     * 获取某个client_id的session
+     *
+     * @param int   $client_id
+     * @return mixed false表示出错、null表示用户不存在、array表示具体的session信息 
+     */
+    public static function getSession($client_id)
+    {
+        $address_data = Context::clientIdToAddress($client_id);
+        if (!$address_data) {
+            return false;
+        }
+        $address      = long2ip($address_data['local_ip']) . ":{$address_data['local_port']}";
+        if (isset(static::$businessWorker)) {
+            if (!isset(static::$businessWorker->gatewayConnections[$address])) {
+                return null;
+            }
+        }
+        $gateway_data                  = GatewayProtocol::$empty;
+        $gateway_data['cmd']           = GatewayProtocol::CMD_GET_SESSION_BY_CLIENT_ID;
+        $gateway_data['connection_id'] = $address_data['connection_id'];
+        return static::sendAndRecv($address, $gateway_data);
+    }
+
+    /**
+     * 向某个用户网关发送命令和消息
+     *
+     * @param int    $client_id
+     * @param int    $cmd
+     * @param string $message
+     * @param string $ext_data
+     * @return boolean
+     */
+    protected static function sendCmdAndMessageToClient($client_id, $cmd, $message, $ext_data = '')
+    {
+        // 如果是发给当前用户则直接获取上下文中的地址
+        if ($client_id === Context::$client_id || $client_id === null) {
+            $address       = long2ip(Context::$local_ip) . ':' . Context::$local_port;
+            $connection_id = Context::$connection_id;
+        } else {
+            $address_data  = Context::clientIdToAddress($client_id);
+            if (!$address_data) {
+                return false;
+            }
+            $address       = long2ip($address_data['local_ip']) . ":{$address_data['local_port']}";
+            $connection_id = $address_data['connection_id'];
+        }
+        $gateway_data                  = GatewayProtocol::$empty;
+        $gateway_data['cmd']           = $cmd;
+        $gateway_data['connection_id'] = $connection_id;
+        $gateway_data['body']          = $message;
+        if (!empty($ext_data)) {
+            $gateway_data['ext_data'] = $ext_data;
+        }
+
+        return static::sendToGateway($address, $gateway_data);
+    }
+
+    /**
+     * 发送数据并返回
+     *
+     * @param int   $address
+     * @param mixed $data
+     * @return bool
+     * @throws Exception
+     */
+    protected static function sendAndRecv($address, $data)
+    {
+        $buffer = GatewayProtocol::encode($data);
+        $buffer = static::$secretKey ? static::generateAuthBuffer() . $buffer : $buffer;
+        $client = stream_socket_client("tcp://$address", $errno, $errmsg, static::$connectTimeout);
+        if (!$client) {
+            throw new Exception("can not connect to tcp://$address $errmsg");
+        }
+        if (strlen($buffer) === stream_socket_sendto($client, $buffer)) {
+            $timeout = 5;
+            // 阻塞读
+            stream_set_blocking($client, 1);
+            // 1秒超时
+            stream_set_timeout($client, 1);
+            $all_buffer = '';
+            $time_start = microtime(true);
+            $pack_len = 0;
+            while (1) {
+                $buf = stream_socket_recvfrom($client, 655350);
+                if ($buf !== '' && $buf !== false) {
+                    $all_buffer .= $buf;
+                } else {
+                    if (feof($client)) {
+                        throw new Exception("connection close tcp://$address");
+                    } elseif (microtime(true) - $time_start > $timeout) {
+                        break;
+                    }
+                    continue;
+                }
+                $recv_len = strlen($all_buffer);
+                if (!$pack_len && $recv_len >= 4) {
+                    $pack_len= current(unpack('N', $all_buffer));
+                }
+                // 回复的数据都是以\n结尾
+                if (($pack_len && $recv_len >= $pack_len + 4) || microtime(true) - $time_start > $timeout) {
+                    break;
+                }
+            }
+            // 返回结果
+            return unserialize(substr($all_buffer, 4));
+        } else {
+            throw new Exception("sendAndRecv($address, \$bufer) fail ! Can not send data!", 502);
+        }
+    }
+
+    /**
+     * 发送数据到网关
+     *
+     * @param string $address
+     * @param array  $gateway_data
+     * @return bool
+     */
+    protected static function sendToGateway($address, $gateway_data)
+    {
+        return static::sendBufferToGateway($address, GatewayProtocol::encode($gateway_data));
+    }
+
+    /**
+     * 发送buffer数据到网关
+     * @param string $address
+     * @param string $gateway_buffer
+     * @return bool
+     */
+    protected static function sendBufferToGateway($address, $gateway_buffer)
+    {
+        // 有$businessWorker说明是workerman环境,使用$businessWorker发送数据
+        if (static::$businessWorker) {
+            if (!isset(static::$businessWorker->gatewayConnections[$address])) {
+                return false;
+            }
+            return static::$businessWorker->gatewayConnections[$address]->send($gateway_buffer, true);
+        }
+        // 非workerman环境
+        $gateway_buffer = static::$secretKey ? static::generateAuthBuffer() . $gateway_buffer : $gateway_buffer;
+        $flag           = static::$persistentConnection ? STREAM_CLIENT_PERSISTENT | STREAM_CLIENT_CONNECT : STREAM_CLIENT_CONNECT;
+        $client         = stream_socket_client("tcp://$address", $errno, $errmsg, static::$connectTimeout, $flag);
+        return strlen($gateway_buffer) == stream_socket_sendto($client, $gateway_buffer);
+    }
+
+    /**
+     * 向所有 gateway 发送数据
+     *
+     * @param string $gateway_data
+     * @throws Exception
+     *
+     * @return void
+     */
+    protected static function sendToAllGateway($gateway_data)
+    {
+        $buffer = GatewayProtocol::encode($gateway_data);
+        // 如果有businessWorker实例,说明运行在workerman环境中,通过businessWorker中的长连接发送数据
+        if (static::$businessWorker) {
+            foreach (static::$businessWorker->gatewayConnections as $gateway_connection) {
+                /** @var TcpConnection $gateway_connection */
+                $gateway_connection->send($buffer, true);
+            }
+        } // 运行在其它环境中,通过注册中心得到gateway地址
+        else {
+            $all_addresses = static::getAllGatewayAddressesFromRegister();
+            foreach ($all_addresses as $address) {
+                static::sendBufferToGateway($address, $buffer);
+            }
+        }
+    }
+
+    /**
+     * 踢掉某个网关的 socket
+     *
+     * @param string $address
+     * @param int    $connection_id
+     * @return bool
+     */
+    protected static function kickAddress($address, $connection_id, $message)
+    {
+        $gateway_data                  = GatewayProtocol::$empty;
+        $gateway_data['cmd']           = GatewayProtocol::CMD_KICK;
+        $gateway_data['connection_id'] = $connection_id;
+        $gateway_data['body'] = $message;
+        return static::sendToGateway($address, $gateway_data);
+    }
+
+    /**
+     * 销毁某个网关的 socket
+     *
+     * @param string $address
+     * @param int    $connection_id
+     * @return bool
+     */
+    protected static function destroyAddress($address, $connection_id)
+    {
+        $gateway_data                  = GatewayProtocol::$empty;
+        $gateway_data['cmd']           = GatewayProtocol::CMD_DESTROY;
+        $gateway_data['connection_id'] = $connection_id;
+        return static::sendToGateway($address, $gateway_data);
+    }
+
+    /**
+     * 将clientid数组转换成address数组
+     *
+     * @param array $client_id_array
+     * @return array
+     */
+    protected static function clientIdArrayToAddressArray(array $client_id_array)
+    {
+        $address_connection_array = array();
+        foreach ($client_id_array as $client_id) {
+            $address_data = Context::clientIdToAddress($client_id);
+            if ($address_data) {
+                $address                                                            = long2ip($address_data['local_ip']) .
+                    ":{$address_data['local_port']}";
+                $address_connection_array[$address][$address_data['connection_id']] = $address_data['connection_id'];
+            }
+        }
+        return $address_connection_array;
+    }
+
+    /**
+     * 设置 gateway 实例
+     *
+     * @param \GatewayWorker\BusinessWorker $business_worker_instance
+     */
+    public static function setBusinessWorker($business_worker_instance)
+    {
+        static::$businessWorker = $business_worker_instance;
+    }
+
+    /**
+     * 获取通过注册中心获取所有 gateway 通讯地址
+     *
+     * @return array
+     * @throws Exception
+     */
+    protected static function getAllGatewayAddressesFromRegister()
+    {
+        static $addresses_cache, $last_update;
+        $time_now = time();
+        $expiration_time = 1;
+        $register_addresses = (array)static::$registerAddress;
+        if(empty($addresses_cache) || $time_now - $last_update > $expiration_time) {
+            foreach ($register_addresses as $register_address) {
+                $client = stream_socket_client('tcp://' . $register_address, $errno, $errmsg, static::$connectTimeout);
+                if ($client) {
+                    break;
+                }
+            }
+            if (!$client) {
+                throw new Exception('Can not connect to tcp://' . $register_address . ' ' . $errmsg);
+            }
+
+            fwrite($client, '{"event":"worker_connect","secret_key":"' . static::$secretKey . '"}' . "\n");
+            stream_set_timeout($client, 5);
+            $ret = fgets($client, 655350);
+            if (!$ret || !$data = json_decode(trim($ret), true)) {
+                throw new Exception('getAllGatewayAddressesFromRegister fail. tcp://' .
+                    $register_address . ' return ' . var_export($ret, true));
+            }
+            $last_update = $time_now;
+            $addresses_cache = $data['addresses'];
+        }
+        if (!$addresses_cache) {
+            throw new Exception('Gateway::getAllGatewayAddressesFromRegister() with registerAddress:' .
+                json_encode(static::$registerAddress) . '  return ' . var_export($addresses_cache, true));
+        }
+        return $addresses_cache;
+    }
+
+    /**
+     * 检查群组id是否合法
+     *
+     * @param $group
+     * @return bool
+     */
+    protected static function isValidGroupId($group)
+    {
+        if (empty($group)) {
+            echo new \Exception('group('.var_export($group, true).') empty');
+            return false;
+        }
+        return true;
+    }
+}
+
+if (!class_exists('\Protocols\GatewayProtocol')) {
+    class_alias('GatewayWorker\Protocols\GatewayProtocol', 'Protocols\GatewayProtocol');
+}

+ 216 - 0
GatewayWorker/vendor/workerman/gateway-worker/src/Protocols/GatewayProtocol.php

@@ -0,0 +1,216 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link      http://www.workerman.net/
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace GatewayWorker\Protocols;
+
+/**
+ * Gateway 与 Worker 间通讯的二进制协议
+ *
+ * struct GatewayProtocol
+ * {
+ *     unsigned int        pack_len,
+ *     unsigned char       cmd,//命令字
+ *     unsigned int        local_ip,
+ *     unsigned short      local_port,
+ *     unsigned int        client_ip,
+ *     unsigned short      client_port,
+ *     unsigned int        connection_id,
+ *     unsigned char       flag,
+ *     unsigned short      gateway_port,
+ *     unsigned int        ext_len,
+ *     char[ext_len]       ext_data,
+ *     char[pack_length-HEAD_LEN] body//包体
+ * }
+ * NCNnNnNCnN
+ */
+class GatewayProtocol
+{
+    // 发给worker,gateway有一个新的连接
+    const CMD_ON_CONNECT = 1;
+
+    // 发给worker的,客户端有消息
+    const CMD_ON_MESSAGE = 3;
+
+    // 发给worker上的关闭链接事件
+    const CMD_ON_CLOSE = 4;
+
+    // 发给gateway的向单个用户发送数据
+    const CMD_SEND_TO_ONE = 5;
+
+    // 发给gateway的向所有用户发送数据
+    const CMD_SEND_TO_ALL = 6;
+
+    // 发给gateway的踢出用户
+    // 1、如果有待发消息,将在发送完后立即销毁用户连接
+    // 2、如果无待发消息,将立即销毁用户连接
+    const CMD_KICK = 7;
+
+    // 发给gateway的立即销毁用户连接
+    const CMD_DESTROY = 8;
+
+    // 发给gateway,通知用户session更新
+    const CMD_UPDATE_SESSION = 9;
+
+    // 获取在线状态
+    const CMD_GET_ALL_CLIENT_SESSIONS = 10;
+
+    // 判断是否在线
+    const CMD_IS_ONLINE = 11;
+
+    // client_id绑定到uid
+    const CMD_BIND_UID = 12;
+
+    // 解绑
+    const CMD_UNBIND_UID = 13;
+
+    // 向uid发送数据
+    const CMD_SEND_TO_UID = 14;
+
+    // 根据uid获取绑定的clientid
+    const CMD_GET_CLIENT_ID_BY_UID = 15;
+
+    // 加入组
+    const CMD_JOIN_GROUP = 20;
+
+    // 离开组
+    const CMD_LEAVE_GROUP = 21;
+
+    // 向组成员发消息
+    const CMD_SEND_TO_GROUP = 22;
+
+    // 获取组成员
+    const CMD_GET_CLIENT_SESSIONS_BY_GROUP = 23;
+
+    // 获取组在线连接数
+    const CMD_GET_CLIENT_COUNT_BY_GROUP = 24;
+
+    // 按照条件查找
+    const CMD_SELECT = 25;
+
+    // 获取在线的群组ID
+    const CMD_GET_GROUP_ID_LIST = 26;
+
+    // 取消分组
+    const CMD_UNGROUP = 27;
+
+    // worker连接gateway事件
+    const CMD_WORKER_CONNECT = 200;
+
+    // 心跳
+    const CMD_PING = 201;
+
+    // GatewayClient连接gateway事件
+    const CMD_GATEWAY_CLIENT_CONNECT = 202;
+
+    // 根据client_id获取session
+    const CMD_GET_SESSION_BY_CLIENT_ID = 203;
+
+    // 发给gateway,覆盖session
+    const CMD_SET_SESSION = 204;
+
+    // 当websocket握手时触发,只有websocket协议支持此命令字
+    const CMD_ON_WEBSOCKET_CONNECT = 205;
+
+    // 包体是标量
+    const FLAG_BODY_IS_SCALAR = 0x01;
+
+    // 通知gateway在send时不调用协议encode方法,在广播组播时提升性能
+    const FLAG_NOT_CALL_ENCODE = 0x02;
+
+    /**
+     * 包头长度
+     *
+     * @var int
+     */
+    const HEAD_LEN = 28;
+
+    public static $empty = array(
+        'cmd'           => 0,
+        'local_ip'      => 0,
+        'local_port'    => 0,
+        'client_ip'     => 0,
+        'client_port'   => 0,
+        'connection_id' => 0,
+        'flag'          => 0,
+        'gateway_port'  => 0,
+        'ext_data'      => '',
+        'body'          => '',
+    );
+
+    /**
+     * 返回包长度
+     *
+     * @param string $buffer
+     * @return int return current package length
+     */
+    public static function input($buffer)
+    {
+        if (strlen($buffer) < self::HEAD_LEN) {
+            return 0;
+        }
+
+        $data = unpack("Npack_len", $buffer);
+        return $data['pack_len'];
+    }
+
+    /**
+     * 获取整个包的 buffer
+     *
+     * @param mixed $data
+     * @return string
+     */
+    public static function encode($data)
+    {
+        $flag = (int)is_scalar($data['body']);
+        if (!$flag) {
+            $data['body'] = serialize($data['body']);
+        }
+        $data['flag'] |= $flag;
+        $ext_len      = strlen($data['ext_data']);
+        $package_len  = self::HEAD_LEN + $ext_len + strlen($data['body']);
+        return pack("NCNnNnNCnN", $package_len,
+            $data['cmd'], $data['local_ip'],
+            $data['local_port'], $data['client_ip'],
+            $data['client_port'], $data['connection_id'],
+            $data['flag'], $data['gateway_port'],
+            $ext_len) . $data['ext_data'] . $data['body'];
+    }
+
+    /**
+     * 从二进制数据转换为数组
+     *
+     * @param string $buffer
+     * @return array
+     */
+    public static function decode($buffer)
+    {
+        $data = unpack("Npack_len/Ccmd/Nlocal_ip/nlocal_port/Nclient_ip/nclient_port/Nconnection_id/Cflag/ngateway_port/Next_len",
+            $buffer);
+        if ($data['ext_len'] > 0) {
+            $data['ext_data'] = substr($buffer, self::HEAD_LEN, $data['ext_len']);
+            if ($data['flag'] & self::FLAG_BODY_IS_SCALAR) {
+                $data['body'] = substr($buffer, self::HEAD_LEN + $data['ext_len']);
+            } else {
+                $data['body'] = unserialize(substr($buffer, self::HEAD_LEN + $data['ext_len']));
+            }
+        } else {
+            $data['ext_data'] = '';
+            if ($data['flag'] & self::FLAG_BODY_IS_SCALAR) {
+                $data['body'] = substr($buffer, self::HEAD_LEN);
+            } else {
+                $data['body'] = unserialize(substr($buffer, self::HEAD_LEN));
+            }
+        }
+        return $data;
+    }
+}

+ 190 - 0
GatewayWorker/vendor/workerman/gateway-worker/src/Register.php

@@ -0,0 +1,190 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link      http://www.workerman.net/
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace GatewayWorker;
+
+use Workerman\Worker;
+use Workerman\Lib\Timer;
+
+/**
+ *
+ * 注册中心,用于注册 Gateway 和 BusinessWorker
+ *
+ * @author walkor<walkor@workerman.net>
+ *
+ */
+class Register extends Worker
+{
+    /**
+     * {@inheritdoc}
+     */
+    public $name = 'Register';
+
+    /**
+     * {@inheritdoc}
+     */
+    public $reloadable = false;
+    
+    /**
+     * 秘钥
+     * @var string
+     */
+    public $secretKey = '';
+
+    /**
+     * 所有 gateway 的连接
+     *
+     * @var array
+     */
+    protected $_gatewayConnections = array();
+
+    /**
+     * 所有 worker 的连接
+     *
+     * @var array
+     */
+    protected $_workerConnections = array();
+
+    /**
+     * 进程启动时间
+     *
+     * @var int
+     */
+    protected $_startTime = 0;
+
+    /**
+     * {@inheritdoc}
+     */
+    public function run()
+    {
+        // 设置 onMessage 连接回调
+        $this->onConnect = array($this, 'onConnect');
+
+        // 设置 onMessage 回调
+        $this->onMessage = array($this, 'onMessage');
+
+        // 设置 onClose 回调
+        $this->onClose = array($this, 'onClose');
+
+        // 记录进程启动的时间
+        $this->_startTime = time();
+        
+        // 强制使用text协议
+        $this->protocol = '\Workerman\Protocols\Text';
+        
+        // 运行父方法
+        parent::run();
+    }
+
+    /**
+     * 设置个定时器,将未及时发送验证的连接关闭
+     *
+     * @param \Workerman\Connection\ConnectionInterface $connection
+     * @return void
+     */
+    public function onConnect($connection)
+    {
+        $connection->timeout_timerid = Timer::add(10, function () use ($connection) {
+            Worker::log("Register auth timeout (".$connection->getRemoteIp()."). See http://doc2.workerman.net/register-auth-timeout.html");
+            $connection->close();
+        }, null, false);
+    }
+
+    /**
+     * 设置消息回调
+     *
+     * @param \Workerman\Connection\ConnectionInterface $connection
+     * @param string                                    $buffer
+     * @return void
+     */
+    public function onMessage($connection, $buffer)
+    {
+        // 删除定时器
+        Timer::del($connection->timeout_timerid);
+        $data       = @json_decode($buffer, true);
+        if (empty($data['event'])) {
+            $error = "Bad request for Register service. Request info(IP:".$connection->getRemoteIp().", Request Buffer:$buffer). See http://doc2.workerman.net/register-auth-timeout.html";
+            Worker::log($error);
+            return $connection->close($error);
+        }
+        $event      = $data['event'];
+        $secret_key = isset($data['secret_key']) ? $data['secret_key'] : '';
+        // 开始验证
+        switch ($event) {
+            // 是 gateway 连接
+            case 'gateway_connect':
+                if (empty($data['address'])) {
+                    echo "address not found\n";
+                    return $connection->close();
+                }
+                if ($secret_key !== $this->secretKey) {
+                    Worker::log("Register: Key does not match ".var_export($secret_key, true)." !== ".var_export($this->secretKey, true));
+                    return $connection->close();
+                }
+                $this->_gatewayConnections[$connection->id] = $data['address'];
+                $this->broadcastAddresses();
+                break;
+            // 是 worker 连接
+            case 'worker_connect':
+                if ($secret_key !== $this->secretKey) {
+                    Worker::log("Register: Key does not match ".var_export($secret_key, true)." !== ".var_export($this->secretKey, true));
+                    return $connection->close();
+                }
+                $this->_workerConnections[$connection->id] = $connection;
+                $this->broadcastAddresses($connection);
+                break;
+            case 'ping':
+                break;
+            default:
+                Worker::log("Register unknown event:$event IP: ".$connection->getRemoteIp()." Buffer:$buffer. See http://doc2.workerman.net/register-auth-timeout.html");
+                $connection->close();
+        }
+    }
+
+    /**
+     * 连接关闭时
+     *
+     * @param \Workerman\Connection\ConnectionInterface $connection
+     */
+    public function onClose($connection)
+    {
+        if (isset($this->_gatewayConnections[$connection->id])) {
+            unset($this->_gatewayConnections[$connection->id]);
+            $this->broadcastAddresses();
+        }
+        if (isset($this->_workerConnections[$connection->id])) {
+            unset($this->_workerConnections[$connection->id]);
+        }
+    }
+
+    /**
+     * 向 BusinessWorker 广播 gateway 内部通讯地址
+     *
+     * @param \Workerman\Connection\ConnectionInterface $connection
+     */
+    public function broadcastAddresses($connection = null)
+    {
+        $data   = array(
+            'event'     => 'broadcast_addresses',
+            'addresses' => array_unique(array_values($this->_gatewayConnections)),
+        );
+        $buffer = json_encode($data);
+        if ($connection) {
+            $connection->send($buffer);
+            return;
+        }
+        foreach ($this->_workerConnections as $con) {
+            $con->send($buffer);
+        }
+    }
+}

+ 1607 - 0
GatewayWorker/vendor/workerman/gatewayclient/Gateway.php

@@ -0,0 +1,1607 @@
+<?php
+namespace GatewayClient;
+use \Exception;
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link http://www.workerman.net/
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+
+/**
+ * 数据发送相关
+ * @version 3.0.12
+ */
+
+/**
+ * 数据发送相关
+ */
+class Gateway
+{
+    /**
+     * gateway 实例
+     *
+     * @var object
+     */
+    protected static $businessWorker = null;
+
+    /**
+     * 注册中心地址
+     *
+     * @var string|array
+     */
+    public static $registerAddress = '127.0.0.1:1236';
+
+    /**
+     * 秘钥
+     * @var string
+     */
+    public static $secretKey = '';
+
+    /**
+     * 链接超时时间
+     * @var int
+     */
+    public static $connectTimeout = 3;
+
+    /**
+     * 与Gateway是否是长链接
+     * @var bool
+     */
+    public static $persistentConnection = false;
+    
+    /**
+     * 向所有客户端连接(或者 client_id_array 指定的客户端连接)广播消息
+     *
+     * @param string $message           向客户端发送的消息
+     * @param array  $client_id_array   客户端 id 数组
+     * @param array  $exclude_client_id 不给这些client_id发
+     * @param bool   $raw               是否发送原始数据(即不调用gateway的协议的encode方法)
+     * @return void
+     * @throws Exception
+     */
+    public static function sendToAll($message, $client_id_array = null, $exclude_client_id = null, $raw = false)
+    {
+        $gateway_data         = GatewayProtocol::$empty;
+        $gateway_data['cmd']  = GatewayProtocol::CMD_SEND_TO_ALL;
+        $gateway_data['body'] = $message;
+        if ($raw) {
+            $gateway_data['flag'] |= GatewayProtocol::FLAG_NOT_CALL_ENCODE;
+        }
+
+        if ($exclude_client_id) {
+            if (!is_array($exclude_client_id)) {
+                $exclude_client_id = array($exclude_client_id);
+            }
+            if ($client_id_array) {
+                $exclude_client_id = array_flip($exclude_client_id);
+            }
+        }
+
+        if ($client_id_array) {
+            if (!is_array($client_id_array)) {
+                echo new \Exception('bad $client_id_array:'.var_export($client_id_array, true));
+                return;
+            }
+            $data_array = array();
+            foreach ($client_id_array as $client_id) {
+                if (isset($exclude_client_id[$client_id])) {
+                    continue;
+                }
+                $address = Context::clientIdToAddress($client_id);
+                if ($address) {
+                    $key                                         = long2ip($address['local_ip']) . ":{$address['local_port']}";
+                    $data_array[$key][$address['connection_id']] = $address['connection_id'];
+                }
+            }
+            foreach ($data_array as $addr => $connection_id_list) {
+                $the_gateway_data             = $gateway_data;
+                $the_gateway_data['ext_data'] = json_encode(array('connections' => $connection_id_list));
+                static::sendToGateway($addr, $the_gateway_data);
+            }
+            return;
+        } elseif (empty($client_id_array) && is_array($client_id_array)) {
+            return;
+        }
+
+        if (!$exclude_client_id) {
+            return static::sendToAllGateway($gateway_data);
+        }
+
+        $address_connection_array = static::clientIdArrayToAddressArray($exclude_client_id);
+
+        // 如果有businessWorker实例,说明运行在workerman环境中,通过businessWorker中的长连接发送数据
+        if (static::$businessWorker) {
+            foreach (static::$businessWorker->gatewayConnections as $address => $gateway_connection) {
+                $gateway_data['ext_data'] = isset($address_connection_array[$address]) ?
+                    json_encode(array('exclude'=> $address_connection_array[$address])) : '';
+                /** @var TcpConnection $gateway_connection */
+                $gateway_connection->send($gateway_data);
+            }
+        } // 运行在其它环境中,通过注册中心得到gateway地址
+        else {
+            $all_addresses = static::getAllGatewayAddressesFromRegister();
+            foreach ($all_addresses as $address) {
+                $gateway_data['ext_data'] = isset($address_connection_array[$address]) ?
+                    json_encode(array('exclude'=> $address_connection_array[$address])) : '';
+                static::sendToGateway($address, $gateway_data);
+            }
+        }
+
+    }
+
+    /**
+     * 向某个client_id对应的连接发消息
+     *
+     * @param int    $client_id
+     * @param string $message
+     * @return void
+     */
+    public static function sendToClient($client_id, $message)
+    {
+        return static::sendCmdAndMessageToClient($client_id, GatewayProtocol::CMD_SEND_TO_ONE, $message);
+    }
+
+    /**
+     * 判断某个uid是否在线
+     *
+     * @param string $uid
+     * @return int 0|1
+     */
+    public static function isUidOnline($uid)
+    {
+        return (int)static::getClientIdByUid($uid);
+    }
+    
+    /**
+     * 判断client_id对应的连接是否在线
+     *
+     * @param int $client_id
+     * @return int 0|1
+     */
+    public static function isOnline($client_id)
+    {
+        $address_data = Context::clientIdToAddress($client_id);
+        if (!$address_data) {
+            return 0;
+        }
+        $address      = long2ip($address_data['local_ip']) . ":{$address_data['local_port']}";
+        if (isset(static::$businessWorker)) {
+            if (!isset(static::$businessWorker->gatewayConnections[$address])) {
+                return 0;
+            }
+        }
+        $gateway_data                  = GatewayProtocol::$empty;
+        $gateway_data['cmd']           = GatewayProtocol::CMD_IS_ONLINE;
+        $gateway_data['connection_id'] = $address_data['connection_id'];
+        return (int)static::sendAndRecv($address, $gateway_data);
+    }
+
+    /**
+     * 获取所有在线用户的session,client_id为 key(弃用,请用getAllClientSessions代替)
+     *
+     * @param string $group
+     * @return array
+     */
+    public static function getAllClientInfo($group = '')
+    {
+        echo "Warning: Gateway::getAllClientInfo is deprecated and will be removed in a future, please use Gateway::getAllClientSessions instead.";
+        return static::getAllClientSessions($group);
+    }
+
+    /**
+     * 获取所有在线client_id的session,client_id为 key
+     *
+     * @param string $group
+     * @return array
+     */
+    public static function getAllClientSessions($group = '')
+    {
+        $gateway_data = GatewayProtocol::$empty;
+        if (!$group) {
+            $gateway_data['cmd']      = GatewayProtocol::CMD_GET_ALL_CLIENT_SESSIONS;
+        } else {
+            $gateway_data['cmd']      = GatewayProtocol::CMD_GET_CLIENT_SESSIONS_BY_GROUP;
+            $gateway_data['ext_data'] = $group;
+        }
+        $status_data      = array();
+        $all_buffer_array = static::getBufferFromAllGateway($gateway_data);
+        foreach ($all_buffer_array as $local_ip => $buffer_array) {
+            foreach ($buffer_array as $local_port => $data) {
+                if ($data) {
+                    foreach ($data as $connection_id => $session_buffer) {
+                        $client_id = Context::addressToClientId($local_ip, $local_port, $connection_id);
+                        if ($client_id === Context::$client_id) {
+                            $status_data[$client_id] = (array)$_SESSION;
+                        } else {
+                            $status_data[$client_id] = $session_buffer ? Context::sessionDecode($session_buffer) : array();
+                        }
+                    }
+                }
+            }
+        }
+        return $status_data;
+    }
+
+    /**
+     * 获取某个组的连接信息(弃用,请用getClientSessionsByGroup代替)
+     *
+     * @param string $group
+     * @return array
+     */
+    public static function getClientInfoByGroup($group)
+    {
+        echo "Warning: Gateway::getClientInfoByGroup is deprecated and will be removed in a future, please use Gateway::getClientSessionsByGroup instead.";
+        return static::getAllClientSessions($group);
+    }
+
+    /**
+     * 获取某个组的所有client_id的session信息
+     *
+     * @param string $group
+     *
+     * @return array
+     */
+    public static function getClientSessionsByGroup($group)
+    {
+        if (static::isValidGroupId($group)) {
+            return static::getAllClientSessions($group);
+        }
+        return array();
+    }
+
+    /**
+     * 获取所有在线client_id数
+     *
+     * @return int
+     */
+    public static function getAllClientIdCount()
+    {
+        return static::getClientCountByGroup();
+    }
+
+    /**
+     * 获取所有在线client_id数(getAllClientIdCount的别名)
+     *
+     * @return int
+     */
+    public static function getAllClientCount()
+    {
+        return static::getAllClientIdCount();
+    }
+
+    /**
+     * 获取某个组的在线client_id数
+     *
+     * @param string $group
+     * @return int
+     */
+    public static function getClientIdCountByGroup($group = '')
+    {
+        $gateway_data             = GatewayProtocol::$empty;
+        $gateway_data['cmd']      = GatewayProtocol::CMD_GET_CLIENT_COUNT_BY_GROUP;
+        $gateway_data['ext_data'] = $group;
+        $total_count              = 0;
+        $all_buffer_array         = static::getBufferFromAllGateway($gateway_data);
+        foreach ($all_buffer_array as $local_ip => $buffer_array) {
+            foreach ($buffer_array as $local_port => $count) {
+                if ($count) {
+                    $total_count += $count;
+                }
+            }
+        }
+        return $total_count;
+    }
+
+    /**
+     * getClientIdCountByGroup 函数的别名
+     *
+     * @param string $group
+     * @return int
+     */
+    public static function getClientCountByGroup($group = '')
+    {
+        return static::getClientIdCountByGroup($group);
+    }
+
+    /**
+     * 获取某个群组在线client_id列表
+     *
+     * @param string $group
+     * @return array
+     */
+    public static function getClientIdListByGroup($group)
+    {
+        if (!static::isValidGroupId($group)) {
+            return array();
+        }
+
+        $data = static::select(array('uid'), array('groups' => is_array($group) ? $group : array($group)));
+        $client_id_map = array();
+        foreach ($data as $local_ip => $buffer_array) {
+            foreach ($buffer_array as $local_port => $items) {
+                //$items = ['connection_id'=>['uid'=>x, 'group'=>[x,x..], 'session'=>[..]], 'client_id'=>[..], ..];
+                foreach ($items as $connection_id => $info) {
+                    $client_id = Context::addressToClientId($local_ip, $local_port, $connection_id);
+                    $client_id_map[$client_id] = $client_id;
+                }
+            }
+        }
+        return $client_id_map;
+    }
+
+    /**
+     * 获取集群所有在线client_id列表
+     *
+     * @return array
+     */
+    public static function getAllClientIdList()
+    {
+        return static::formatClientIdFromGatewayBuffer(static::select(array('uid')));
+    }
+
+    /**
+     * 格式化client_id
+     *
+     * @param $data
+     * @return array
+     */
+    protected static function formatClientIdFromGatewayBuffer($data)
+    {
+        $client_id_list = array();
+        foreach ($data as $local_ip => $buffer_array) {
+            foreach ($buffer_array as $local_port => $items) {
+                //$items = ['connection_id'=>['uid'=>x, 'group'=>[x,x..], 'session'=>[..]], 'client_id'=>[..], ..];
+                foreach ($items as $connection_id => $info) {
+                    $client_id = Context::addressToClientId($local_ip, $local_port, $connection_id);
+                    $client_id_list[$client_id] = $client_id;
+                }
+            }
+        }
+        return $client_id_list;
+    }
+
+
+    /**
+     * 获取与 uid 绑定的 client_id 列表
+     *
+     * @param string $uid
+     * @return array
+     */
+    public static function getClientIdByUid($uid)
+    {
+        $gateway_data             = GatewayProtocol::$empty;
+        $gateway_data['cmd']      = GatewayProtocol::CMD_GET_CLIENT_ID_BY_UID;
+        $gateway_data['ext_data'] = $uid;
+        $client_list              = array();
+        $all_buffer_array         = static::getBufferFromAllGateway($gateway_data);
+        foreach ($all_buffer_array as $local_ip => $buffer_array) {
+            foreach ($buffer_array as $local_port => $connection_id_array) {
+                if ($connection_id_array) {
+                    foreach ($connection_id_array as $connection_id) {
+                        $client_list[] = Context::addressToClientId($local_ip, $local_port, $connection_id);
+                    }
+                }
+            }
+        }
+        return $client_list;
+    }
+
+    /**
+     * 获取某个群组在线uid列表
+     *
+     * @param string $group
+     * @return array
+     */
+    public static function getUidListByGroup($group)
+    {
+        if (!static::isValidGroupId($group)) {
+            return array();
+        }
+
+        $group = is_array($group) ? $group : array($group);
+        $data = static::select(array('uid'), array('groups' => $group));
+        $uid_map = array();
+        foreach ($data as $local_ip => $buffer_array) {
+            foreach ($buffer_array as $local_port => $items) {
+                //$items = ['connection_id'=>['uid'=>x, 'group'=>[x,x..], 'session'=>[..]], 'client_id'=>[..], ..];
+                foreach ($items as $connection_id => $info) {
+                    if (!empty($info['uid'])) {
+                        $uid_map[$info['uid']] = $info['uid'];
+                    }
+                }
+            }
+        }
+        return $uid_map;
+    }
+
+    /**
+     * 获取某个群组在线uid数
+     *
+     * @param string $group
+     * @return int
+     */
+    public static function getUidCountByGroup($group)
+    {
+        if (static::isValidGroupId($group)) {
+            return count(static::getUidListByGroup($group));
+        }
+        return 0;
+    }
+
+    /**
+     * 获取全局在线uid列表
+     *
+     * @return array
+     */
+    public static function getAllUidList()
+    {
+        $data = static::select(array('uid'));
+        $uid_map = array();
+        foreach ($data as $local_ip => $buffer_array) {
+            foreach ($buffer_array as $local_port => $items) {
+                //$items = ['connection_id'=>['uid'=>x, 'group'=>[x,x..], 'session'=>[..]], 'client_id'=>[..], ..];
+                foreach ($items as $connection_id => $info) {
+                    if (!empty($info['uid'])) {
+                        $uid_map[$info['uid']] = $info['uid'];
+                    }
+                }
+            }
+        }
+        return $uid_map;
+    }
+
+    /**
+     * 获取全局在线uid数
+     * @return int
+     */
+    public static function getAllUidCount()
+    {
+        return count(static::getAllUidList());
+    }
+
+    /**
+     * 通过client_id获取uid
+     *
+     * @param $client_id
+     * @return mixed
+     */
+    public static function getUidByClientId($client_id)
+    {
+        $data = static::select(array('uid'), array('client_id'=>array($client_id)));
+        foreach ($data as $local_ip => $buffer_array) {
+            foreach ($buffer_array as $local_port => $items) {
+                //$items = ['connection_id'=>['uid'=>x, 'group'=>[x,x..], 'session'=>[..]], 'client_id'=>[..], ..];
+                foreach ($items as $info) {
+                    return $info['uid'];
+                }
+            }
+        }
+    }
+
+    /**
+     * 获取所有在线的群组id
+     *
+     * @return array
+     */
+    public static function getAllGroupIdList()
+    {
+        $gateway_data             = GatewayProtocol::$empty;
+        $gateway_data['cmd']      = GatewayProtocol::CMD_GET_GROUP_ID_LIST;
+        $group_id_list            = array();
+        $all_buffer_array         = static::getBufferFromAllGateway($gateway_data);
+        foreach ($all_buffer_array as $local_ip => $buffer_array) {
+            foreach ($buffer_array as $local_port => $group_id_array) {
+                if (is_array($group_id_array)) {
+                    foreach ($group_id_array as $group_id) {
+                        if (!isset($group_id_list[$group_id])) {
+                            $group_id_list[$group_id] = $group_id;
+                        }
+                    }
+                }
+            }
+        }
+        return $group_id_list;
+    }
+
+
+    /**
+     * 获取所有在线分组的uid数量,也就是每个分组的在线用户数
+     *
+     * @return array
+     */
+    public static function getAllGroupUidCount()
+    {
+        $group_uid_map = static::getAllGroupUidList();
+        $group_uid_count_map = array();
+        foreach ($group_uid_map as $group_id => $uid_list) {
+            $group_uid_count_map[$group_id] = count($uid_list);
+        }
+        return $group_uid_count_map;
+    }
+
+
+
+    /**
+     * 获取所有分组uid在线列表
+     *
+     * @return array
+     */
+    public static function getAllGroupUidList()
+    {
+        $data = static::select(array('uid','groups'));
+        $group_uid_map = array();
+        foreach ($data as $local_ip => $buffer_array) {
+            foreach ($buffer_array as $local_port => $items) {
+                //$items = ['connection_id'=>['uid'=>x, 'group'=>[x,x..], 'session'=>[..]], 'client_id'=>[..], ..];
+                foreach ($items as $connection_id => $info) {
+                    if (empty($info['uid']) || empty($info['groups'])) {
+                        break;
+                    }
+                    $uid = $info['uid'];
+                    foreach ($info['groups'] as $group_id) {
+                        if(!isset($group_uid_map[$group_id])) {
+                            $group_uid_map[$group_id] = array();
+                        }
+                        $group_uid_map[$group_id][$uid] = $uid;
+                    }
+                }
+            }
+        }
+        return $group_uid_map;
+    }
+
+    /**
+     * 获取所有群组在线client_id列表
+     *
+     * @return array
+     */
+    public static function getAllGroupClientIdList()
+    {
+        $data = static::select(array('groups'));
+        $group_client_id_map = array();
+        foreach ($data as $local_ip => $buffer_array) {
+            foreach ($buffer_array as $local_port => $items) {
+                //$items = ['connection_id'=>['uid'=>x, 'group'=>[x,x..], 'session'=>[..]], 'client_id'=>[..], ..];
+                foreach ($items as $connection_id => $info) {
+                    if (empty($info['groups'])) {
+                        break;
+                    }
+                    $client_id = Context::addressToClientId($local_ip, $local_port, $connection_id);
+                    foreach ($info['groups'] as $group_id) {
+                        if(!isset($group_client_id_map[$group_id])) {
+                            $group_client_id_map[$group_id] = array();
+                        }
+                        $group_client_id_map[$group_id][$client_id] = $client_id;
+                    }
+                }
+            }
+        }
+        return $group_client_id_map;
+    }
+
+    /**
+     * 获取所有群组在线client_id数量,也就是获取每个群组在线连接数
+     *
+     * @return array
+     */
+    public static function getAllGroupClientIdCount()
+    {
+        $group_client_map = static::getAllGroupClientIdList();
+        $group_client_count_map = array();
+        foreach ($group_client_map as $group_id => $client_id_list) {
+            $group_client_count_map[$group_id] = count($client_id_list);
+        }
+        return $group_client_count_map;
+    }
+
+
+    /**
+     * 根据条件到gateway搜索数据
+     *
+     * @param array $fields
+     * @param array $where
+     * @return array
+     */
+    protected static function select($fields = array('session','uid','groups'), $where = array())
+    {
+        $t = microtime(true);
+        $gateway_data             = GatewayProtocol::$empty;
+        $gateway_data['cmd']      = GatewayProtocol::CMD_SELECT;
+        $gateway_data['ext_data'] = array('fields' => $fields, 'where' => $where);
+        $gateway_data_list   = array();
+        // 有client_id,能计算出需要和哪些gateway通讯,只和必要的gateway通讯能降低系统负载
+        if (isset($where['client_id'])) {
+            $client_id_list = $where['client_id'];
+            unset($gateway_data['ext_data']['where']['client_id']);
+            $gateway_data['ext_data']['where']['connection_id'] = array();
+            foreach ($client_id_list as $client_id) {
+                $address_data = Context::clientIdToAddress($client_id);
+                if (!$address_data) {
+                    continue;
+                }
+                $address = long2ip($address_data['local_ip']) . ":{$address_data['local_port']}";
+                if (!isset($gateway_data_list[$address])) {
+                    $gateway_data_list[$address] = $gateway_data;
+                }
+                $gateway_data_list[$address]['ext_data']['where']['connection_id'][$address_data['connection_id']] = $address_data['connection_id'];
+            }
+            foreach ($gateway_data_list as $address => $item) {
+                $gateway_data_list[$address]['ext_data'] = json_encode($item['ext_data']);
+            }
+            // 有其它条件,则还是需要向所有gateway发送
+            if (count($where) !== 1) {
+                $gateway_data['ext_data'] = json_encode($gateway_data['ext_data']);
+                foreach (static::getAllGatewayAddress() as $address) {
+                    if (!isset($gateway_data_list[$address])) {
+                        $gateway_data_list[$address] = $gateway_data;
+                    }
+                }
+            }
+            $data = static::getBufferFromSomeGateway($gateway_data_list);
+        } else {
+            $gateway_data['ext_data'] = json_encode($gateway_data['ext_data']);
+            $data = static::getBufferFromAllGateway($gateway_data);
+        }
+
+        return $data;
+    }
+
+    /**
+     * 生成验证包,用于验证此客户端的合法性
+     * 
+     * @return string
+     */
+    protected static function generateAuthBuffer()
+    {
+        $gateway_data         = GatewayProtocol::$empty;
+        $gateway_data['cmd']  = GatewayProtocol::CMD_GATEWAY_CLIENT_CONNECT;
+        $gateway_data['body'] = json_encode(array(
+            'secret_key' => static::$secretKey,
+        ));
+        return GatewayProtocol::encode($gateway_data);
+    }
+
+    /**
+     * 批量向某些gateway发包,并得到返回数组
+     *
+     * @param array $gateway_data_array
+     * @return array
+     * @throws Exception
+     */
+    protected static function getBufferFromSomeGateway($gateway_data_array)
+    {
+        $gateway_buffer_array = array();
+        $auth_buffer = static::$secretKey ? static::generateAuthBuffer() : '';
+        foreach ($gateway_data_array as $address => $gateway_data) {
+            if ($auth_buffer) {
+                $gateway_buffer_array[$address] = $auth_buffer.GatewayProtocol::encode($gateway_data);
+            } else {
+                $gateway_buffer_array[$address] = GatewayProtocol::encode($gateway_data);
+            }
+        }
+        return static::getBufferFromGateway($gateway_buffer_array);
+    }
+
+    /**
+     * 批量向所有 gateway 发包,并得到返回数组
+     *
+     * @param string $gateway_data
+     * @return array
+     * @throws Exception
+     */
+    protected static function getBufferFromAllGateway($gateway_data)
+    {
+        $addresses = static::getAllGatewayAddress();
+        $gateway_buffer_array = array();
+        $gateway_buffer = GatewayProtocol::encode($gateway_data);
+        $gateway_buffer = static::$secretKey ? static::generateAuthBuffer() . $gateway_buffer : $gateway_buffer;
+        foreach ($addresses as $address) {
+            $gateway_buffer_array[$address] = $gateway_buffer;
+        }
+
+        return static::getBufferFromGateway($gateway_buffer_array);
+    }
+
+    /**
+     * 获取所有gateway内部通讯地址
+     *
+     * @return array
+     * @throws Exception
+     */
+    protected static function getAllGatewayAddress()
+    {
+        if (isset(static::$businessWorker)) {
+            $addresses = static::$businessWorker->getAllGatewayAddresses();
+            if (empty($addresses)) {
+                throw new Exception('businessWorker::getAllGatewayAddresses return empty');
+            }
+        } else {
+            $addresses = static::getAllGatewayAddressesFromRegister();
+            if (empty($addresses)) {
+                return array();
+            }
+        }
+        return $addresses;
+    }
+
+    /**
+     * 批量向gateway发送并获取数据
+     * @param $gateway_buffer_array
+     * @return array
+     */
+    protected static function getBufferFromGateway($gateway_buffer_array)
+    {
+        $client_array = $status_data = $client_address_map = $receive_buffer_array = $recv_length_array = array();
+        // 批量向所有gateway进程发送请求数据
+        foreach ($gateway_buffer_array as $address => $gateway_buffer) {
+            $client = stream_socket_client("tcp://$address", $errno, $errmsg, static::$connectTimeout);
+            if ($client && strlen($gateway_buffer) === stream_socket_sendto($client, $gateway_buffer)) {
+                $socket_id                        = (int)$client;
+                $client_array[$socket_id]         = $client;
+                $client_address_map[$socket_id]   = explode(':', $address);
+                $receive_buffer_array[$socket_id] = '';
+            }
+        }
+        // 超时5秒
+        $timeout    = 5;
+        $time_start = microtime(true);
+        // 批量接收请求
+        while (count($client_array) > 0) {
+            $write = $except = array();
+            $read  = $client_array;
+            if (@stream_select($read, $write, $except, $timeout)) {
+                foreach ($read as $client) {
+                    $socket_id = (int)$client;
+                    $buffer    = stream_socket_recvfrom($client, 65535);
+                    if ($buffer !== '' && $buffer !== false) {
+                        $receive_buffer_array[$socket_id] .= $buffer;
+                        $receive_length = strlen($receive_buffer_array[$socket_id]);
+                        if (empty($recv_length_array[$socket_id]) && $receive_length >= 4) {
+                            $recv_length_array[$socket_id] = current(unpack('N', $receive_buffer_array[$socket_id]));
+                        }
+                        if (!empty($recv_length_array[$socket_id]) && $receive_length >= $recv_length_array[$socket_id] + 4) {
+                            unset($client_array[$socket_id]);
+                        }
+                    } elseif (feof($client)) {
+                        unset($client_array[$socket_id]);
+                    }
+                }
+            }
+            if (microtime(true) - $time_start > $timeout) {
+                break;
+            }
+        }
+        $format_buffer_array = array();
+        foreach ($receive_buffer_array as $socket_id => $buffer) {
+            $local_ip                                    = ip2long($client_address_map[$socket_id][0]);
+            $local_port                                  = $client_address_map[$socket_id][1];
+            $format_buffer_array[$local_ip][$local_port] = unserialize(substr($buffer, 4));
+        }
+        return $format_buffer_array;
+    }
+
+    /**
+     * 踢掉某个客户端,并以$message通知被踢掉客户端
+     *
+     * @param int $client_id
+     * @param string $message
+     * @return void
+     */
+    public static function closeClient($client_id, $message = null)
+    {
+        if ($client_id === Context::$client_id) {
+            return static::closeCurrentClient($message);
+        } // 不是发给当前用户则使用存储中的地址
+        else {
+            $address_data = Context::clientIdToAddress($client_id);
+            if (!$address_data) {
+                return false;
+            }
+            $address      = long2ip($address_data['local_ip']) . ":{$address_data['local_port']}";
+            return static::kickAddress($address, $address_data['connection_id'], $message);
+        }
+    }
+
+    /**
+     * 踢掉某个客户端并直接立即销毁相关连接
+     *
+     * @param int $client_id
+     * @return bool
+     */
+    public static function destoryClient($client_id)
+    {
+        if ($client_id === Context::$client_id) {
+            return static::destoryCurrentClient();
+        } // 不是发给当前用户则使用存储中的地址
+        else {
+            $address_data = Context::clientIdToAddress($client_id);
+            if (!$address_data) {
+                return false;
+            }
+            $address = long2ip($address_data['local_ip']) . ":{$address_data['local_port']}";
+            return static::destroyAddress($address, $address_data['connection_id']);
+        }
+    }
+
+    /**
+     * 踢掉当前客户端并直接立即销毁相关连接
+     *
+     * @return bool
+     * @throws Exception
+     */
+    public static function destoryCurrentClient()
+    {
+        if (!Context::$connection_id) {
+            throw new Exception('destoryCurrentClient can not be called in async context');
+        }
+        $address = long2ip(Context::$local_ip) . ':' . Context::$local_port;
+        return static::destroyAddress($address, Context::$connection_id);
+    }
+
+    /**
+     * 将 client_id 与 uid 绑定
+     *
+     * @param int        $client_id
+     * @param int|string $uid
+     * @return void
+     */
+    public static function bindUid($client_id, $uid)
+    {
+        static::sendCmdAndMessageToClient($client_id, GatewayProtocol::CMD_BIND_UID, '', $uid);
+    }
+
+    /**
+     * 将 client_id 与 uid 解除绑定
+     *
+     * @param int        $client_id
+     * @param int|string $uid
+     * @return void
+     */
+    public static function unbindUid($client_id, $uid)
+    {
+        static::sendCmdAndMessageToClient($client_id, GatewayProtocol::CMD_UNBIND_UID, '', $uid);
+    }
+
+    /**
+     * 将 client_id 加入组
+     *
+     * @param int        $client_id
+     * @param int|string $group
+     * @return void
+     */
+    public static function joinGroup($client_id, $group)
+    {
+
+        static::sendCmdAndMessageToClient($client_id, GatewayProtocol::CMD_JOIN_GROUP, '', $group);
+    }
+
+    /**
+     * 将 client_id 离开组
+     *
+     * @param int        $client_id
+     * @param int|string $group
+     *
+     * @return void
+     */
+    public static function leaveGroup($client_id, $group)
+    {
+        static::sendCmdAndMessageToClient($client_id, GatewayProtocol::CMD_LEAVE_GROUP, '', $group);
+    }
+
+    /**
+     * 取消分组
+     *
+     * @param int|string $group
+     *
+     * @return void
+     */
+    public static function ungroup($group)
+    {
+        if (!static::isValidGroupId($group)) {
+            return false;
+        }
+        $gateway_data             = GatewayProtocol::$empty;
+        $gateway_data['cmd']      = GatewayProtocol::CMD_UNGROUP;
+        $gateway_data['ext_data'] = $group;
+        return static::sendToAllGateway($gateway_data);
+
+    }
+
+    /**
+     * 向所有 uid 发送
+     *
+     * @param int|string|array $uid
+     * @param string           $message
+     *
+     * @return void
+     */
+    public static function sendToUid($uid, $message)
+    {
+        $gateway_data         = GatewayProtocol::$empty;
+        $gateway_data['cmd']  = GatewayProtocol::CMD_SEND_TO_UID;
+        $gateway_data['body'] = $message;
+
+        if (!is_array($uid)) {
+            $uid = array($uid);
+        }
+
+        $gateway_data['ext_data'] = json_encode($uid);
+
+        static::sendToAllGateway($gateway_data);
+    }
+
+    /**
+     * 向 group 发送
+     *
+     * @param int|string|array $group             组(不允许是 0 '0' false null array()等为空的值)
+     * @param string           $message           消息
+     * @param array            $exclude_client_id 不给这些client_id发
+     * @param bool             $raw               发送原始数据(即不调用gateway的协议的encode方法)
+     *
+     * @return void
+     */
+    public static function sendToGroup($group, $message, $exclude_client_id = null, $raw = false)
+    {
+        if (!static::isValidGroupId($group)) {
+            return false;
+        }
+        $gateway_data         = GatewayProtocol::$empty;
+        $gateway_data['cmd']  = GatewayProtocol::CMD_SEND_TO_GROUP;
+        $gateway_data['body'] = $message;
+        if ($raw) {
+            $gateway_data['flag'] |= GatewayProtocol::FLAG_NOT_CALL_ENCODE;
+        }
+
+        if (!is_array($group)) {
+            $group = array($group);
+        }
+
+        // 分组发送,没有排除的client_id,直接发送
+        $default_ext_data_buffer = json_encode(array('group'=> $group, 'exclude'=> null));
+        if (empty($exclude_client_id)) {
+            $gateway_data['ext_data'] = $default_ext_data_buffer;
+            return static::sendToAllGateway($gateway_data);
+        }
+
+        // 分组发送,有排除的client_id,需要将client_id转换成对应gateway进程内的connectionId
+        if (!is_array($exclude_client_id)) {
+            $exclude_client_id = array($exclude_client_id);
+        }
+
+        $address_connection_array = static::clientIdArrayToAddressArray($exclude_client_id);
+        // 如果有businessWorker实例,说明运行在workerman环境中,通过businessWorker中的长连接发送数据
+        if (static::$businessWorker) {
+            foreach (static::$businessWorker->gatewayConnections as $address => $gateway_connection) {
+                $gateway_data['ext_data'] = isset($address_connection_array[$address]) ?
+                    json_encode(array('group'=> $group, 'exclude'=> $address_connection_array[$address])) :
+                    $default_ext_data_buffer;
+                /** @var TcpConnection $gateway_connection */
+                $gateway_connection->send($gateway_data);
+            }
+        } // 运行在其它环境中,通过注册中心得到gateway地址
+        else {
+            $addresses = static::getAllGatewayAddressesFromRegister();
+            foreach ($addresses as $address) {
+                $gateway_data['ext_data'] = isset($address_connection_array[$address]) ?
+                    json_encode(array('group'=> $group, 'exclude'=> $address_connection_array[$address])) :
+                    $default_ext_data_buffer;
+                static::sendToGateway($address, $gateway_data);
+            }
+        }
+    }
+
+    /**
+     * 更新 session,框架自动调用,开发者不要调用
+     *
+     * @param int    $client_id
+     * @param string $session_str
+     * @return bool
+     */
+    public static function setSocketSession($client_id, $session_str)
+    {
+        return static::sendCmdAndMessageToClient($client_id, GatewayProtocol::CMD_SET_SESSION, '', $session_str);
+    }
+
+    /**
+     * 设置 session,原session值会被覆盖
+     *
+     * @param int   $client_id
+     * @param array $session
+     *
+     * @return void
+     */
+    public static function setSession($client_id, array $session)
+    {
+        if (Context::$client_id === $client_id) {
+            $_SESSION = $session;
+            Context::$old_session = $_SESSION;
+        }
+        static::setSocketSession($client_id, Context::sessionEncode($session));
+    }
+    
+    /**
+     * 更新 session,实际上是与老的session合并
+     *
+     * @param int   $client_id
+     * @param array $session
+     *
+     * @return void
+     */
+    public static function updateSession($client_id, array $session)
+    {
+        if (Context::$client_id === $client_id) {
+            $_SESSION = array_replace_recursive((array)$_SESSION, $session);
+            Context::$old_session = $_SESSION;
+        }
+        static::sendCmdAndMessageToClient($client_id, GatewayProtocol::CMD_UPDATE_SESSION, '', Context::sessionEncode($session));
+    }
+    
+    /**
+     * 获取某个client_id的session
+     *
+     * @param int   $client_id
+     * @return mixed false表示出错、null表示用户不存在、array表示具体的session信息 
+     */
+    public static function getSession($client_id)
+    {
+        $address_data = Context::clientIdToAddress($client_id);
+        if (!$address_data) {
+            return false;
+        }
+        $address      = long2ip($address_data['local_ip']) . ":{$address_data['local_port']}";
+        if (isset(static::$businessWorker)) {
+            if (!isset(static::$businessWorker->gatewayConnections[$address])) {
+                return null;
+            }
+        }
+        $gateway_data                  = GatewayProtocol::$empty;
+        $gateway_data['cmd']           = GatewayProtocol::CMD_GET_SESSION_BY_CLIENT_ID;
+        $gateway_data['connection_id'] = $address_data['connection_id'];
+        return static::sendAndRecv($address, $gateway_data);
+    }
+
+    /**
+     * 向某个用户网关发送命令和消息
+     *
+     * @param int    $client_id
+     * @param int    $cmd
+     * @param string $message
+     * @param string $ext_data
+     * @return boolean
+     */
+    protected static function sendCmdAndMessageToClient($client_id, $cmd, $message, $ext_data = '')
+    {
+        // 如果是发给当前用户则直接获取上下文中的地址
+        if ($client_id === Context::$client_id || $client_id === null) {
+            $address       = long2ip(Context::$local_ip) . ':' . Context::$local_port;
+            $connection_id = Context::$connection_id;
+        } else {
+            $address_data  = Context::clientIdToAddress($client_id);
+            if (!$address_data) {
+                return false;
+            }
+            $address       = long2ip($address_data['local_ip']) . ":{$address_data['local_port']}";
+            $connection_id = $address_data['connection_id'];
+        }
+        
+        $gateway_data                  = GatewayProtocol::$empty;
+        $gateway_data['cmd']           = $cmd;
+        $gateway_data['connection_id'] = $connection_id;
+        $gateway_data['body']          = $message;
+        if (!empty($ext_data)) {
+            $gateway_data['ext_data'] = $ext_data;
+        }
+
+        return static::sendToGateway($address, $gateway_data);
+    }
+
+    /**
+     * 发送数据并返回
+     *
+     * @param int   $address
+     * @param mixed $data
+     * @return bool
+     * @throws Exception
+     */
+    protected static function sendAndRecv($address, $data)
+    {
+        $buffer = GatewayProtocol::encode($data);
+        $buffer = static::$secretKey ? static::generateAuthBuffer() . $buffer : $buffer;
+        $client = stream_socket_client("tcp://$address", $errno, $errmsg, static::$connectTimeout);
+        if (!$client) {
+            throw new Exception("can not connect to tcp://$address $errmsg");
+        }
+        if (strlen($buffer) === stream_socket_sendto($client, $buffer)) {
+            $timeout = 5;
+            // 阻塞读
+            stream_set_blocking($client, 1);
+            // 1秒超时
+            stream_set_timeout($client, 1);
+            $all_buffer = '';
+            $time_start = microtime(true);
+            $pack_len = 0;
+            while (1) {
+                $buf = stream_socket_recvfrom($client, 655350);
+                if ($buf !== '' && $buf !== false) {
+                    $all_buffer .= $buf;
+                } else {
+                    if (feof($client)) {
+                        throw new Exception("connection close tcp://$address");
+                    } elseif (microtime(true) - $time_start > $timeout) {
+                        break;
+                    }
+                    continue;
+                }
+                $recv_len = strlen($all_buffer);
+                if (!$pack_len && $recv_len >= 4) {
+                    $pack_len= current(unpack('N', $all_buffer));
+                }
+                // 回复的数据都是以\n结尾
+                if (($pack_len && $recv_len >= $pack_len + 4) || microtime(true) - $time_start > $timeout) {
+                    break;
+                }
+            }
+            // 返回结果
+            return unserialize(substr($all_buffer, 4));
+        } else {
+            throw new Exception("sendAndRecv($address, \$bufer) fail ! Can not send data!", 502);
+        }
+    }
+
+    /**
+     * 发送数据到网关
+     *
+     * @param string $address
+     * @param array  $gateway_data
+     * @return bool
+     */
+    protected static function sendToGateway($address, $gateway_data)
+    {
+        return static::sendBufferToGateway($address, GatewayProtocol::encode($gateway_data));
+    }
+
+    /**
+     * 发送buffer数据到网关
+     * @param string $address
+     * @param string $gateway_buffer
+     * @return bool
+     */
+    protected static function sendBufferToGateway($address, $gateway_buffer)
+    {
+        // 有$businessWorker说明是workerman环境,使用$businessWorker发送数据
+        if (static::$businessWorker) {
+            if (!isset(static::$businessWorker->gatewayConnections[$address])) {
+                return false;
+            }
+            return static::$businessWorker->gatewayConnections[$address]->send($gateway_buffer, true);
+        }
+        
+        // 非workerman环境
+        $gateway_buffer = static::$secretKey ? static::generateAuthBuffer() . $gateway_buffer : $gateway_buffer;
+        $flag           = static::$persistentConnection ? STREAM_CLIENT_PERSISTENT | STREAM_CLIENT_CONNECT : STREAM_CLIENT_CONNECT;
+        $client         = stream_socket_client("tcp://$address", $errno, $errmsg, static::$connectTimeout, $flag);
+        $a=strlen($gateway_buffer) == stream_socket_sendto($client, $gateway_buffer);
+        return $a;
+    }
+
+    /**
+     * 向所有 gateway 发送数据
+     *
+     * @param string $gateway_data
+     * @throws Exception
+     *
+     * @return void
+     */
+    protected static function sendToAllGateway($gateway_data)
+    {
+        $buffer = GatewayProtocol::encode($gateway_data);
+        // 如果有businessWorker实例,说明运行在workerman环境中,通过businessWorker中的长连接发送数据
+        if (static::$businessWorker) {
+            foreach (static::$businessWorker->gatewayConnections as $gateway_connection) {
+                /** @var TcpConnection $gateway_connection */
+                $gateway_connection->send($buffer, true);
+            }
+        } // 运行在其它环境中,通过注册中心得到gateway地址
+        else {
+            $all_addresses = static::getAllGatewayAddressesFromRegister();
+            foreach ($all_addresses as $address) {
+                static::sendBufferToGateway($address, $buffer);
+            }
+        }
+    }
+
+    /**
+     * 踢掉某个网关的 socket
+     *
+     * @param string $address
+     * @param int    $connection_id
+     * @return bool
+     */
+    protected static function kickAddress($address, $connection_id, $message)
+    {
+        $gateway_data                  = GatewayProtocol::$empty;
+        $gateway_data['cmd']           = GatewayProtocol::CMD_KICK;
+        $gateway_data['connection_id'] = $connection_id;
+        $gateway_data['body'] = $message;
+        return static::sendToGateway($address, $gateway_data);
+    }
+
+    /**
+     * 销毁某个网关的 socket
+     *
+     * @param string $address
+     * @param int    $connection_id
+     * @return bool
+     */
+    protected static function destroyAddress($address, $connection_id)
+    {
+        $gateway_data                  = GatewayProtocol::$empty;
+        $gateway_data['cmd']           = GatewayProtocol::CMD_DESTROY;
+        $gateway_data['connection_id'] = $connection_id;
+        return static::sendToGateway($address, $gateway_data);
+    }
+
+    /**
+     * 将clientid数组转换成address数组
+     *
+     * @param array $client_id_array
+     * @return array
+     */
+    protected static function clientIdArrayToAddressArray(array $client_id_array)
+    {
+        $address_connection_array = array();
+        foreach ($client_id_array as $client_id) {
+            $address_data = Context::clientIdToAddress($client_id);
+            if ($address_data) {
+                $address                                                            = long2ip($address_data['local_ip']) .
+                    ":{$address_data['local_port']}";
+                $address_connection_array[$address][$address_data['connection_id']] = $address_data['connection_id'];
+            }
+        }
+        return $address_connection_array;
+    }
+
+    /**
+     * 设置 gateway 实例
+     *
+     * @param \GatewayWorker\BusinessWorker $business_worker_instance
+     */
+    public static function setBusinessWorker($business_worker_instance)
+    {
+        static::$businessWorker = $business_worker_instance;
+    }
+
+    /**
+     * 获取通过注册中心获取所有 gateway 通讯地址
+     *
+     * @return array
+     * @throws Exception
+     */
+    protected static function getAllGatewayAddressesFromRegister()
+    {
+        static $addresses_cache, $last_update;
+        $time_now = time();
+        $expiration_time = 1;
+        $register_addresses = (array)static::$registerAddress;
+        if(empty($addresses_cache) || $time_now - $last_update > $expiration_time) {
+            foreach ($register_addresses as $register_address) {
+                set_error_handler(function(){});
+                $client = stream_socket_client('tcp://' . $register_address, $errno, $errmsg, static::$connectTimeout);
+                restore_error_handler();
+                if ($client) {
+                    break;
+                }
+            }
+            if (!$client) {
+                throw new Exception('Can not connect to tcp://' . $register_address . ' ' . $errmsg);
+            }
+
+            fwrite($client, '{"event":"worker_connect","secret_key":"' . static::$secretKey . '"}' . "\n");
+            stream_set_timeout($client, 5);
+            $ret = fgets($client, 655350);
+            if (!$ret || !$data = json_decode(trim($ret), true)) {
+                throw new Exception('getAllGatewayAddressesFromRegister fail. tcp://' .
+                    $register_address . ' return ' . var_export($ret, true));
+            }
+            $last_update = $time_now;
+            $addresses_cache = $data['addresses'];
+        }
+        if (!$addresses_cache) {
+            throw new Exception('Gateway::getAllGatewayAddressesFromRegister() with registerAddress:' .
+                json_encode(static::$registerAddress) . '  return ' . var_export($addresses_cache, true));
+        }
+        return $addresses_cache;
+    }
+
+    /**
+     * 检查群组id是否合法
+     *
+     * @param $group
+     * @return bool
+     */
+    protected static function isValidGroupId($group)
+    {
+        if (empty($group)) {
+            echo new \Exception('group('.var_export($group, true).') empty');
+            return false;
+        }
+        return true;
+    }
+}
+
+
+/**
+ * 上下文 包含当前用户uid, 内部通信local_ip local_port socket_id ,以及客户端client_ip client_port
+ */
+class Context
+{
+    /**
+     * 内部通讯id
+     * @var string
+     */
+    public static $local_ip;
+    /**
+     * 内部通讯端口
+     * @var int
+     */
+    public static $local_port;
+    /**
+     * 客户端ip
+     * @var string
+     */
+    public static $client_ip;
+    /**
+     * 客户端端口
+     * @var int
+     */
+    public static $client_port;
+    /**
+     * client_id
+     * @var string
+     */
+    public static $client_id;
+    /**
+     * 连接connection->id
+     * @var int
+     */
+    public static $connection_id;
+
+    /**
+     * 旧的session
+     *
+     * @var string
+     */
+    public static $old_session;
+
+    /**
+     * 编码session
+     * @param mixed $session_data
+     * @return string
+     */
+    public static function sessionEncode($session_data = '')
+    {
+        if($session_data !== '')
+        {
+            return serialize($session_data);
+        }
+        return '';
+    }
+
+    /**
+     * 解码session
+     * @param string $session_buffer
+     * @return mixed
+     */
+    public static function sessionDecode($session_buffer)
+    {
+        return unserialize($session_buffer);
+    }
+
+    /**
+     * 清除上下文
+     * @return void
+     */
+    public static function clear()
+    {
+        static::$local_ip = static::$local_port = static::$client_ip = static::$client_port =
+        static::$client_id = static::$connection_id  = static::$old_session = null;
+    }
+
+    /**
+     * 通讯地址到client_id的转换
+     * @return string
+     */
+    public static function addressToClientId($local_ip, $local_port, $connection_id)
+    {
+        return bin2hex(pack('NnN', $local_ip, $local_port, $connection_id));
+    }
+
+    /**
+     * client_id到通讯地址的转换
+     * @return array
+     */
+    public static function clientIdToAddress($client_id)
+    {
+        if(strlen($client_id) !== 20)
+        {
+            throw new \Exception("client_id $client_id is invalid");
+        }
+        return unpack('Nlocal_ip/nlocal_port/Nconnection_id' ,pack('H*', $client_id));
+    }
+
+}
+
+
+/**
+ * Gateway 与 Worker 间通讯的二进制协议
+ *
+ * struct GatewayProtocol
+ * {
+ *     unsigned int        pack_len,
+ *     unsigned char       cmd,//命令字
+ *     unsigned int        local_ip,
+ *     unsigned short      local_port,
+ *     unsigned int        client_ip,
+ *     unsigned short      client_port,
+ *     unsigned int        connection_id,
+ *     unsigned char       flag,
+ *     unsigned short      gateway_port,
+ *     unsigned int        ext_len,
+ *     char[ext_len]       ext_data,
+ *     char[pack_length-HEAD_LEN] body//包体
+ * }
+ * NCNnNnNCnN
+ */
+class GatewayProtocol
+{
+    // 发给worker,gateway有一个新的连接
+    const CMD_ON_CONNECT = 1;
+    // 发给worker的,客户端有消息
+    const CMD_ON_MESSAGE = 3;
+    // 发给worker上的关闭链接事件
+    const CMD_ON_CLOSE = 4;
+    // 发给gateway的向单个用户发送数据
+    const CMD_SEND_TO_ONE = 5;
+    // 发给gateway的向所有用户发送数据
+    const CMD_SEND_TO_ALL = 6;
+    // 发给gateway的踢出用户
+    // 1、如果有待发消息,将在发送完后立即销毁用户连接
+    // 2、如果无待发消息,将立即销毁用户连接
+    const CMD_KICK = 7;
+    // 发给gateway的立即销毁用户连接
+    const CMD_DESTROY = 8;
+    // 发给gateway,通知用户session更新
+    const CMD_UPDATE_SESSION = 9;
+    // 获取在线状态
+    const CMD_GET_ALL_CLIENT_SESSIONS = 10;
+    // 判断是否在线
+    const CMD_IS_ONLINE = 11;
+    // client_id绑定到uid
+    const CMD_BIND_UID = 12;
+    // 解绑
+    const CMD_UNBIND_UID = 13;
+    // 向uid发送数据
+    const CMD_SEND_TO_UID = 14;
+    // 根据uid获取绑定的clientid
+    const CMD_GET_CLIENT_ID_BY_UID = 15;
+    // 加入组
+    const CMD_JOIN_GROUP = 20;
+    // 离开组
+    const CMD_LEAVE_GROUP = 21;
+    // 向组成员发消息
+    const CMD_SEND_TO_GROUP = 22;
+    // 获取组成员
+    const CMD_GET_CLIENT_SESSIONS_BY_GROUP = 23;
+    // 获取组在线连接数
+    const CMD_GET_CLIENT_COUNT_BY_GROUP = 24;
+    // 按照条件查找
+    const CMD_SELECT = 25;
+    // 获取在线的群组ID
+    const CMD_GET_GROUP_ID_LIST = 26;
+    // 取消分组
+    const CMD_UNGROUP = 27;
+    // worker连接gateway事件
+    const CMD_WORKER_CONNECT = 200;
+    // 心跳
+    const CMD_PING = 201;
+    // GatewayClient连接gateway事件
+    const CMD_GATEWAY_CLIENT_CONNECT = 202;
+    // 根据client_id获取session
+    const CMD_GET_SESSION_BY_CLIENT_ID = 203;
+    // 发给gateway,覆盖session
+    const CMD_SET_SESSION = 204;
+    // 当websocket握手时触发,只有websocket协议支持此命令字
+    const CMD_ON_WEBSOCKET_CONNECT = 205;
+    // 包体是标量
+    const FLAG_BODY_IS_SCALAR = 0x01;
+    // 通知gateway在send时不调用协议encode方法,在广播组播时提升性能
+    const FLAG_NOT_CALL_ENCODE = 0x02;
+    /**
+     * 包头长度
+     *
+     * @var int
+     */
+    const HEAD_LEN = 28;
+    public static $empty = array(
+        'cmd'           => 0,
+        'local_ip'      => 0,
+        'local_port'    => 0,
+        'client_ip'     => 0,
+        'client_port'   => 0,
+        'connection_id' => 0,
+        'flag'          => 0,
+        'gateway_port'  => 0,
+        'ext_data'      => '',
+        'body'          => '',
+    );
+    /**
+     * 返回包长度
+     *
+     * @param string $buffer
+     * @return int return current package length
+     */
+    public static function input($buffer)
+    {
+        if (strlen($buffer) < self::HEAD_LEN) {
+            return 0;
+        }
+        $data = unpack("Npack_len", $buffer);
+        return $data['pack_len'];
+    }
+    /**
+     * 获取整个包的 buffer
+     *
+     * @param mixed $data
+     * @return string
+     */
+    public static function encode($data)
+    {
+        $flag = (int)is_scalar($data['body']);
+        if (!$flag) {
+            $data['body'] = serialize($data['body']);
+        }
+        $data['flag'] |= $flag;
+        $ext_len      = strlen($data['ext_data']);
+        $package_len  = self::HEAD_LEN + $ext_len + strlen($data['body']);
+        return pack("NCNnNnNCnN", $package_len,
+                $data['cmd'], $data['local_ip'],
+                $data['local_port'], $data['client_ip'],
+                $data['client_port'], $data['connection_id'],
+                $data['flag'], $data['gateway_port'],
+                $ext_len) . $data['ext_data'] . $data['body'];
+    }
+    /**
+     * 从二进制数据转换为数组
+     *
+     * @param string $buffer
+     * @return array
+     */
+    public static function decode($buffer)
+    {
+        $data = unpack("Npack_len/Ccmd/Nlocal_ip/nlocal_port/Nclient_ip/nclient_port/Nconnection_id/Cflag/ngateway_port/Next_len",
+            $buffer);
+        if ($data['ext_len'] > 0) {
+            $data['ext_data'] = substr($buffer, self::HEAD_LEN, $data['ext_len']);
+            if ($data['flag'] & self::FLAG_BODY_IS_SCALAR) {
+                $data['body'] = substr($buffer, self::HEAD_LEN + $data['ext_len']);
+            } else {
+                $data['body'] = unserialize(substr($buffer, self::HEAD_LEN + $data['ext_len']));
+            }
+        } else {
+            $data['ext_data'] = '';
+            if ($data['flag'] & self::FLAG_BODY_IS_SCALAR) {
+                $data['body'] = substr($buffer, self::HEAD_LEN);
+            } else {
+                $data['body'] = unserialize(substr($buffer, self::HEAD_LEN));
+            }
+        }
+        return $data;
+    }
+}

+ 21 - 0
GatewayWorker/vendor/workerman/gatewayclient/MIT-LICENSE.txt

@@ -0,0 +1,21 @@
+The MIT License
+
+Copyright (c) 2009-2015 walkor<walkor@workerman.net> and contributors (see https://github.com/walkor/workerman/contributors)
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.

+ 69 - 0
GatewayWorker/vendor/workerman/gatewayclient/README.md

@@ -0,0 +1,69 @@
+# GatewayClient
+
+GatewayWorker1.0请使用[1.0版本的GatewayClient](https://github.com/walkor/GatewayClient/releases/tag/v1.0)
+
+GatewayWorker2.0.1-2.0.4请使用[2.0.4版本的GatewayClient](https://github.com/walkor/GatewayClient/releases/tag/2.0.4)
+
+GatewayWorker2.0.5-2.0.6版本请使用[2.0.6版本的GatewayClient](https://github.com/walkor/GatewayClient/releases/tag/2.0.6)
+
+GatewayWorker2.0.7版本请使用 [2.0.7版本的GatewayClient](https://github.com/walkor/GatewayClient/releases/tag/v2.0.7)
+
+GatewayWorker3.0.0-3.0.7版本请使用 [3.0.0版本的GatewayClient](https://github.com/walkor/GatewayClient/releases/tag/v3.0.0)<br>
+
+GatewayWorker3.0.8及以上版本请使用 [3.0.13版本的GatewayClient](https://github.com/walkor/GatewayClient/releases/tag/v3.0.13)<br>
+
+注意:GatewayClient3.0.0以后支持composer并加了命名空间```GatewayClient``` <br>
+
+## 安装(composer安装适用于3.0.0及以上版本)
+```
+composer require workerman/gatewayclient
+```
+
+## 使用
+```php
+// GatewayClient 3.0.0版本以后加了命名空间
+use GatewayClient\Gateway;
+
+/**
+ * === 指定registerAddress表明与哪个GatewayWorker(集群)通讯。===
+ * GatewayWorker里用Register服务来区分集群,即一个GatewayWorker(集群)只有一个Register服务,
+ * GatewayClient要与之通讯必须知道这个Register服务地址才能通讯,这个地址格式为 ip:端口 ,
+ * 其中ip为Register服务运行的ip(如果GatewayWorker是单机部署则ip就是运行GatewayWorker的服务器ip),
+ * 端口是对应ip的服务器上start_register.php文件中监听的端口,也就是GatewayWorker启动时看到的Register的端口。
+ * GatewayClient要想推送数据给客户端,必须知道客户端位于哪个GatewayWorker(集群),
+ * 然后去连这个GatewayWorker(集群)Register服务的 ip:端口,才能与对应GatewayWorker(集群)通讯。
+ * 这个 ip:端口 在GatewayClient一侧使用 Gateway::$registerAddress 来指定。
+ * 
+ * === 如果GatewayClient和GatewayWorker不在同一台服务器需要以下步骤 ===
+ * 1、需要设置start_gateway.php中的lanIp为实际的本机内网ip(如不在一个局域网也可以设置成外网ip),设置完后要重启GatewayWorker
+ * 2、GatewayClient这里的Gateway::$registerAddress的ip填写填写上面步骤1lanIp所指定的ip,端口
+ * 3、需要开启GatewayWorker所在服务器的防火墙,让以下端口可以被GatewayClient所在服务器访问,
+ *    端口包括Rgister服务的端口以及start_gateway.php中lanIp与startPort指定的几个端口
+ *
+ * === 如果GatewayClient和GatewayWorker在同一台服务器 ===
+ * GatewayClient和Register服务都在一台服务器上,ip填写127.0.0.1及即可,无需其它设置。
+ **/
+Gateway::$registerAddress = '127.0.0.1:1236';
+
+// GatewayClient支持GatewayWorker中的所有接口(Gateway::closeCurrentClient Gateway::sendToCurrentClient除外)
+Gateway::sendToAll($data);
+Gateway::sendToClient($client_id, $data);
+Gateway::closeClient($client_id);
+Gateway::isOnline($client_id);
+Gateway::bindUid($client_id, $uid);
+Gateway::isUidOnline($uid);
+Gateway::getClientIdByUid($client_id);
+Gateway::unbindUid($client_id, $uid);
+Gateway::sendToUid($uid, $dat);
+Gateway::joinGroup($client_id, $group);
+Gateway::sendToGroup($group, $data);
+Gateway::leaveGroup($client_id, $group);
+Gateway::getClientCountByGroup($group);
+Gateway::getClientSessionsByGroup($group);
+Gateway::getAllClientCount();
+Gateway::getAllClientSessions();
+Gateway::setSession($client_id, $session);
+Gateway::updateSession($client_id, $session);
+Gateway::getSession($client_id);
+```
+

+ 9 - 0
GatewayWorker/vendor/workerman/gatewayclient/composer.json

@@ -0,0 +1,9 @@
+{
+    "name"  : "workerman/gatewayclient",
+    "type"  : "library",
+    "homepage": "http://www.workerman.net",
+    "license" : "MIT",
+    "autoload": {
+        "psr-4": {"GatewayClient\\": "./"}
+    }
+}

+ 78 - 0
GatewayWorker/vendor/workerman/mysql/README.md

@@ -0,0 +1,78 @@
+# Workerman\Mysql\Connection
+
+Long-living MySQL connection for daemon.
+
+# Install
+```composer require workerman/mysql```
+
+# Usage
+```php
+$db = new Workerman\MySQL\Connection($mysql_host, $mysql_port, $user, $password, $db_bname);
+
+// Get all rows.
+$db1->select('ID,Sex')->from('Persons')->where('sex= :sex')->bindValues(array('sex'=>'M'))->query();
+// Equivalent to.
+$db1->select('ID,Sex')->from('Persons')->where("sex='F'")->query();
+// Equivalent to.
+$db->query("SELECT ID,Sex FROM `Persons` WHERE sex='M'");
+
+
+// Get one row.
+$db->select('ID,Sex')->from('Persons')->where('sex= :sex')->bindValues(array('sex'=>'M'))->row();
+// Equivalent to.
+$db->select('ID,Sex')->from('Persons')->where("sex= 'F' ")->row();
+// Equivalent to.
+$db->row("SELECT ID,Sex FROM `Persons` WHERE sex='M'");
+
+
+// Get a column.
+$db->select('ID')->from('Persons')->where('sex= :sex')->bindValues(array('sex'=>'M'))->column();
+// Equivalent to.
+$db->select('ID')->from('Persons')->where("sex= 'F' ")->column();
+// Equivalent to.
+$db->column("SELECT `ID` FROM `Persons` WHERE sex='M'");
+
+// Get single.
+$db->select('ID,Sex')->from('Persons')->where('sex= :sex')->bindValues(array('sex'=>'M'))->single();
+// Equivalent to.
+$db->select('ID,Sex')->from('Persons')->where("sex= 'F' ")->single();
+// Equivalent to.
+$db->single("SELECT ID,Sex FROM `Persons` WHERE sex='M'");
+
+// Complex query.
+$db->select('*')->from('table1')->innerJoin('table2','table1.uid = table2.uid')->where('age > :age')
+->groupBy(array('aid'))->having('foo="foo"')->orderByASC/*orderByDESC*/(array('did'))
+->limit(10)->offset(20)->bindValues(array('age' => 13));
+// Equivalent to.
+$db->query(SELECT * FROM `table1` INNER JOIN `table2` ON `table1`.`uid` = `table2`.`uid` WHERE age > 13
+GROUP BY aid HAVING foo="foo" ORDER BY did LIMIT 10 OFFSET 20“);
+
+// Insert.
+$insert_id = $db->insert('Persons')->cols(array(
+    'Firstname'=>'abc', 
+    'Lastname'=>'efg', 
+    'Sex'=>'M', 
+    'Age'=>13))->query();
+// Equivalent to.
+$insert_id = $db->query("INSERT INTO `Persons` ( `Firstname`,`Lastname`,`Sex`,`Age`) 
+VALUES ( 'abc', 'efg', 'M', 13)");
+
+// Updagte.
+$row_count = $db->update('Persons')->cols(array('sex'))->where('ID=1')
+->bindValue('sex', 'F')->query();
+// Equivalent to.
+$row_count = $db->update('Persons')->cols(array('sex'=>'F'))->where('ID=1')->query();
+// Equivalent to.
+$row_count = $db->query("UPDATE `Persons` SET `sex` = 'F' WHERE ID=1");
+
+// Delete.
+$row_count = $db->delete('Persons')->where('ID=9')->query();
+// Equivalent to.
+$row_count = $db->query("DELETE FROM `Persons` WHERE ID=9");
+
+// Transaction.
+$db1->beginTrans();
+....
+$db1->commitTrans(); // or $db1->rollBackTrans();
+
+```

+ 16 - 0
GatewayWorker/vendor/workerman/mysql/composer.json

@@ -0,0 +1,16 @@
+{
+    "name"  : "workerman/mysql",
+    "type"  : "library",
+    "keywords": ["mysql", "pdo", "pdo_mysql"],
+    "homepage": "http://www.workerman.net",
+    "license" : "MIT",
+    "description": "Long-living MySQL connection for daemon.",
+    "require": {
+        "php": ">=5.3",
+        "ext-pdo": "*",
+        "ext-pdo_mysql": "*"
+    },
+    "autoload": {
+        "psr-4": {"Workerman\\MySQL\\": "./src"}
+    }
+}

+ 1986 - 0
GatewayWorker/vendor/workerman/mysql/src/Connection.php

@@ -0,0 +1,1986 @@
+<?php
+namespace Workerman\MySQL;
+
+use Exception;
+use PDO;
+use PDOException;
+
+/**
+ * 数据库连接类,依赖 PDO_MYSQL 扩展
+ * 在 https://github.com/auraphp/Aura.SqlQuery 的基础上修改而成
+ */
+class Connection
+{
+    /**
+     * SELECT
+     *
+     * @var array
+     */
+    protected $union = array();
+
+    /**
+     * 是否是更新
+     *
+     * @var bool
+     */
+    protected $for_update = false;
+
+    /**
+     * 选择的列
+     *
+     * @var array
+     */
+    protected $cols = array();
+
+    /**
+     * 从哪些表里面 SELECT
+     *
+     * @var array
+     */
+    protected $from = array();
+
+    /**
+     * $from 当前的 key
+     *
+     * @var int
+     */
+    protected $from_key = -1;
+
+    /**
+     * GROUP BY 的列
+     *
+     * @var array
+     */
+    protected $group_by = array();
+
+    /**
+     * HAVING 条件数组.
+     *
+     * @var array
+     */
+    protected $having = array();
+
+    /**
+     * HAVING 语句中绑定的值.
+     *
+     * @var array
+     */
+    protected $bind_having = array();
+
+    /**
+     * 每页多少条记录
+     *
+     * @var int
+     */
+    protected $paging = 10;
+
+    /**
+     * sql 中绑定的值
+     *
+     * @var array
+     */
+    protected $bind_values = array();
+
+    /**
+     * WHERE 条件.
+     *
+     * @var array
+     */
+    protected $where = array();
+
+    /**
+     * WHERE 语句绑定的值
+     *
+     * @var array
+     */
+    protected $bind_where = array();
+
+    /**
+     * ORDER BY 的列
+     *
+     * @var array
+     */
+    protected $order_by = array();
+
+    /**
+     * ORDER BY 的排序方式,默认为升序
+     *
+     * @var bool
+     */
+    protected $order_asc = true;
+    /**
+     * SELECT 多少记录
+     *
+     * @var int
+     */
+    protected $limit = 0;
+
+    /**
+     * 返回记录的游标
+     *
+     * @var int
+     */
+    protected $offset = 0;
+
+    /**
+     * flags 列表
+     *
+     * @var array
+     */
+    protected $flags = array();
+
+    /**
+     * 操作哪个表
+     *
+     * @var string
+     */
+    protected $table;
+
+    /**
+     * 表.列 和 last-insert-id 映射
+     *
+     * @var array
+     */
+    protected $last_insert_id_names = array();
+
+    /**
+     * INSERT 或者 UPDATE 的列
+     *
+     * @param array
+     */
+    protected $col_values;
+
+    /**
+     * 返回的列
+     *
+     * @var array
+     */
+    protected $returning = array();
+
+    /**
+     * sql 的类型 SELECT INSERT DELETE UPDATE
+     *
+     * @var string
+     */
+    protected $type = '';
+
+    /**
+     * pdo 实例
+     *
+     * @var PDO
+     */
+    protected $pdo;
+
+    /**
+     * PDOStatement 实例
+     *
+     * @var \PDOStatement
+     */
+    protected $sQuery;
+
+    /**
+     * 数据库用户名密码等配置
+     *
+     * @var array
+     */
+    protected $settings = array();
+
+    /**
+     * sql 的参数
+     *
+     * @var array
+     */
+    protected $parameters = array();
+
+    /**
+     * 最后一条直行的 sql
+     *
+     * @var string
+     */
+    protected $lastSql = '';
+
+    /**
+     * 是否执行成功
+     *
+     * @var bool
+     */
+    protected $success = false;
+
+    /**
+     * 选择哪些列
+     *
+     * @param string|array $cols
+     * @return self
+     */
+    public function select($cols = '*')
+    {
+        $this->type = 'SELECT';
+        if (!is_array($cols)) {
+            $cols = explode(',', $cols);
+        }
+        $this->cols($cols);
+        return $this;
+    }
+
+    /**
+     * 从哪个表删除
+     *
+     * @param string $table
+     * @return self
+     */
+    public function delete($table)
+    {
+        $this->type  = 'DELETE';
+        $this->table = $this->quoteName($table);
+        $this->fromRaw($this->quoteName($table));
+        return $this;
+    }
+
+    /**
+     * 更新哪个表
+     *
+     * @param string $table
+     * @return self
+     */
+    public function update($table)
+    {
+        $this->type  = 'UPDATE';
+        $this->table = $this->quoteName($table);
+        return $this;
+    }
+
+    /**
+     * 向哪个表插入
+     *
+     * @param string $table
+     * @return self
+     */
+    public function insert($table)
+    {
+        $this->type  = 'INSERT';
+        $this->table = $this->quoteName($table);
+        return $this;
+    }
+
+    /**
+     *
+     * 设置 SQL_CALC_FOUND_ROWS 标记.
+     *
+     * @param bool $enable
+     * @return self
+     */
+    public function calcFoundRows($enable = true)
+    {
+        $this->setFlag('SQL_CALC_FOUND_ROWS', $enable);
+        return $this;
+    }
+
+    /**
+     * 设置 SQL_CACHE 标记
+     *
+     * @param bool $enable
+     * @return self
+     */
+    public function cache($enable = true)
+    {
+        $this->setFlag('SQL_CACHE', $enable);
+        return $this;
+    }
+
+    /**
+     * 设置 SQL_NO_CACHE 标记
+     *
+     * @param bool $enable
+     * @return self
+     */
+    public function noCache($enable = true)
+    {
+        $this->setFlag('SQL_NO_CACHE', $enable);
+        return $this;
+    }
+
+    /**
+     * 设置 STRAIGHT_JOIN 标记.
+     *
+     * @param bool $enable
+     * @return self
+     */
+    public function straightJoin($enable = true)
+    {
+        $this->setFlag('STRAIGHT_JOIN', $enable);
+        return $this;
+    }
+
+    /**
+     * 设置 HIGH_PRIORITY 标记
+     *
+     * @param bool $enable
+     * @return self
+     */
+    public function highPriority($enable = true)
+    {
+        $this->setFlag('HIGH_PRIORITY', $enable);
+        return $this;
+    }
+
+    /**
+     * 设置 SQL_SMALL_RESULT 标记
+     *
+     * @param bool $enable
+     * @return self
+     */
+    public function smallResult($enable = true)
+    {
+        $this->setFlag('SQL_SMALL_RESULT', $enable);
+        return $this;
+    }
+
+    /**
+     * 设置 SQL_BIG_RESULT 标记
+     *
+     * @param bool $enable
+     * @return self
+     */
+    public function bigResult($enable = true)
+    {
+        $this->setFlag('SQL_BIG_RESULT', $enable);
+        return $this;
+    }
+
+    /**
+     * 设置 SQL_BUFFER_RESULT 标记
+     *
+     * @param bool $enable
+     * @return self
+     */
+    public function bufferResult($enable = true)
+    {
+        $this->setFlag('SQL_BUFFER_RESULT', $enable);
+        return $this;
+    }
+
+    /**
+     * 设置 FOR UPDATE 标记
+     *
+     * @param bool $enable
+     * @return self
+     */
+    public function forUpdate($enable = true)
+    {
+        $this->for_update = (bool)$enable;
+        return $this;
+    }
+
+    /**
+     * 设置 DISTINCT 标记
+     *
+     * @param bool $enable
+     * @return self
+     */
+    public function distinct($enable = true)
+    {
+        $this->setFlag('DISTINCT', $enable);
+        return $this;
+    }
+
+    /**
+     * 设置 LOW_PRIORITY 标记
+     *
+     * @param bool $enable
+     * @return self
+     */
+    public function lowPriority($enable = true)
+    {
+        $this->setFlag('LOW_PRIORITY', $enable);
+        return $this;
+    }
+
+    /**
+     * 设置 IGNORE 标记
+     *
+     * @param bool $enable
+     * @return self
+     */
+    public function ignore($enable = true)
+    {
+        $this->setFlag('IGNORE', $enable);
+        return $this;
+    }
+
+    /**
+     * 设置 QUICK 标记
+     *
+     * @param bool $enable
+     * @return self
+     */
+    public function quick($enable = true)
+    {
+        $this->setFlag('QUICK', $enable);
+        return $this;
+    }
+
+    /**
+     * 设置 DELAYED 标记
+     *
+     * @param bool $enable
+     * @return self
+     */
+    public function delayed($enable = true)
+    {
+        $this->setFlag('DELAYED', $enable);
+        return $this;
+    }
+
+    /**
+     * 序列化
+     *
+     * @return string
+     */
+    public function __toString()
+    {
+        $union = '';
+        if ($this->union) {
+            $union = implode(' ', $this->union) . ' ';
+        }
+        return $union . $this->build();
+    }
+
+    /**
+     * 设置每页多少条记录
+     *
+     * @param int $paging
+     * @return self
+     */
+    public function setPaging($paging)
+    {
+        $this->paging = (int)$paging;
+        return $this;
+    }
+
+    /**
+     * 获取每页多少条记录
+     *
+     * @return int
+     */
+    public function getPaging()
+    {
+        return $this->paging;
+    }
+
+    /**
+     * 获取绑定在占位符上的值
+     */
+    public function getBindValues()
+    {
+        switch ($this->type) {
+            case 'SELECT':
+                return $this->getBindValuesSELECT();
+            case 'DELETE':
+            case 'UPDATE':
+            case 'INSERT':
+                return $this->getBindValuesCOMMON();
+            default :
+                throw new Exception("type err");
+        }
+    }
+
+    /**
+     * 获取绑定在占位符上的值
+     *
+     * @return array
+     */
+    public function getBindValuesSELECT()
+    {
+        $bind_values = $this->bind_values;
+        $i           = 1;
+        foreach ($this->bind_where as $val) {
+            $bind_values[$i] = $val;
+            $i++;
+        }
+        foreach ($this->bind_having as $val) {
+            $bind_values[$i] = $val;
+            $i++;
+        }
+        return $bind_values;
+    }
+
+    /**
+     *
+     * SELECT选择哪些列
+     *
+     * @param mixed  $key
+     * @param string $val
+     * @return void
+     */
+    protected function addColSELECT($key, $val)
+    {
+        if (is_string($key)) {
+            $this->cols[$val] = $key;
+        } else {
+            $this->addColWithAlias($val);
+        }
+    }
+
+    /**
+     * SELECT 增加选择的列
+     *
+     * @param string $spec
+     */
+    protected function addColWithAlias($spec)
+    {
+        $parts = explode(' ', $spec);
+        $count = count($parts);
+        if ($count == 2 && trim($parts[0]) != '' && trim($parts[1]) != '') {
+            $this->cols[$parts[1]] = $parts[0];
+        } elseif ($count == 3 && strtoupper($parts[1]) == 'AS') {
+            $this->cols[$parts[2]] = $parts[0];
+        } else {
+            $this->cols[] = trim($spec);
+        }
+    }
+
+    /**
+     * from 哪个表
+     *
+     * @param string $table
+     * @return self
+     */
+    public function from($table)
+    {
+        return $this->fromRaw($this->quoteName($table));
+    }
+
+    /**
+     * from的表
+     *
+     * @param string $table
+     * @return self
+     */
+    public function fromRaw($table)
+    {
+        $this->from[] = array($table);
+        $this->from_key++;
+        return $this;
+    }
+
+    /**
+     *
+     * 子查询
+     *
+     * @param string $table
+     * @param string $name The alias name for the sub-select.
+     * @return self
+     */
+    public function fromSubSelect($table, $name)
+    {
+        $this->from[] = array("($table) AS " . $this->quoteName($name));
+        $this->from_key++;
+        return $this;
+    }
+
+
+    /**
+     * 增加 join 语句
+     *
+     * @param string $table
+     * @param string $cond
+     * @param string $type
+     * @return self
+     * @throws Exception
+     */
+    public function join($table, $cond = null, $type = '')
+    {
+        return $this->joinInternal($type, $table, $cond);
+    }
+
+    /**
+     * 增加 join 语句
+     *
+     * @param string $join inner, left, natural
+     * @param string $table
+     * @param string $cond
+     * @return self
+     * @throws Exception
+     */
+    protected function joinInternal($join, $table, $cond = null)
+    {
+        if (!$this->from) {
+            throw new Exception('Cannot join() without from()');
+        }
+
+        $join                          = strtoupper(ltrim("$join JOIN"));
+        $table                         = $this->quoteName($table);
+        $cond                          = $this->fixJoinCondition($cond);
+        $this->from[$this->from_key][] = rtrim("$join $table $cond");
+        return $this;
+    }
+
+    /**
+     * quote
+     *
+     * @param string $cond
+     * @return string
+     *
+     */
+    protected function fixJoinCondition($cond)
+    {
+        if (!$cond) {
+            return '';
+        }
+
+        $cond = $this->quoteNamesIn($cond);
+
+        if (strtoupper(substr(ltrim($cond), 0, 3)) == 'ON ') {
+            return $cond;
+        }
+
+        if (strtoupper(substr(ltrim($cond), 0, 6)) == 'USING ') {
+            return $cond;
+        }
+
+        return 'ON ' . $cond;
+    }
+
+    /**
+     * inner join
+     *
+     * @param string $table
+     * @param string $cond
+     * @return self
+     * @throws Exception
+     */
+    public function innerJoin($table, $cond = null)
+    {
+        return $this->joinInternal('INNER', $table, $cond);
+    }
+
+    /**
+     * left join
+     *
+     * @param string $table
+     * @param string $cond
+     * @return self
+     * @throws Exception
+     */
+    public function leftJoin($table, $cond = null)
+    {
+        return $this->joinInternal('LEFT', $table, $cond);
+    }
+
+    /**
+     * right join
+     *
+     * @param string $table
+     * @param string $cond
+     * @return self
+     * @throws Exception
+     */
+    public function rightJoin($table, $cond = null)
+    {
+        return $this->joinInternal('RIGHT', $table, $cond);
+    }
+
+    /**
+     * joinSubSelect
+     *
+     * @param string $join inner, left, natural
+     * @param string $spec
+     * @param string $name sub-select 的别名
+     * @param string $cond
+     * @return self
+     * @throws Exception
+     */
+    public function joinSubSelect($join, $spec, $name, $cond = null)
+    {
+        if (!$this->from) {
+            throw new \Exception('Cannot join() without from() first.');
+        }
+
+        $join                          = strtoupper(ltrim("$join JOIN"));
+        $name                          = $this->quoteName($name);
+        $cond                          = $this->fixJoinCondition($cond);
+        $this->from[$this->from_key][] = rtrim("$join ($spec) AS $name $cond");
+        return $this;
+    }
+
+    /**
+     * group by 语句
+     *
+     * @param array $cols
+     * @return self
+     */
+    public function groupBy(array $cols)
+    {
+        foreach ($cols as $col) {
+            $this->group_by[] = $this->quoteNamesIn($col);
+        }
+        return $this;
+    }
+
+    /**
+     * having 语句
+     *
+     * @param string $cond
+     * @return self
+     */
+    public function having($cond)
+    {
+        $this->addClauseCondWithBind('having', 'AND', func_get_args());
+        return $this;
+    }
+
+    /**
+     * or having 语句
+     *
+     * @param string $cond The HAVING condition.
+     * @return self
+     */
+    public function orHaving($cond)
+    {
+        $this->addClauseCondWithBind('having', 'OR', func_get_args());
+        return $this;
+    }
+
+    /**
+     * 设置每页的记录数量
+     *
+     * @param int $page
+     * @return self
+     */
+    public function page($page)
+    {
+        $this->limit  = 0;
+        $this->offset = 0;
+
+        $page = (int)$page;
+        if ($page > 0) {
+            $this->limit  = $this->paging;
+            $this->offset = $this->paging * ($page - 1);
+        }
+        return $this;
+    }
+
+    /**
+     * union
+     *
+     * @return self
+     */
+    public function union()
+    {
+        $this->union[] = $this->build() . ' UNION';
+        $this->reset();
+        return $this;
+    }
+
+    /**
+     * unionAll
+     *
+     * @return self
+     */
+    public function unionAll()
+    {
+        $this->union[] = $this->build() . ' UNION ALL';
+        $this->reset();
+        return $this;
+    }
+
+    /**
+     * 重置
+     */
+    protected function reset()
+    {
+        $this->resetFlags();
+        $this->cols       = array();
+        $this->from       = array();
+        $this->from_key   = -1;
+        $this->where      = array();
+        $this->group_by   = array();
+        $this->having     = array();
+        $this->order_by   = array();
+        $this->limit      = 0;
+        $this->offset     = 0;
+        $this->for_update = false;
+    }
+
+    /**
+     * 清除所有数据
+     */
+    protected function resetAll()
+    {
+        $this->union                = array();
+        $this->for_update           = false;
+        $this->cols                 = array();
+        $this->from                 = array();
+        $this->from_key             = -1;
+        $this->group_by             = array();
+        $this->having               = array();
+        $this->bind_having          = array();
+        $this->paging               = 10;
+        $this->bind_values          = array();
+        $this->where                = array();
+        $this->bind_where           = array();
+        $this->order_by             = array();
+        $this->limit                = 0;
+        $this->offset               = 0;
+        $this->flags                = array();
+        $this->table                = '';
+        $this->last_insert_id_names = array();
+        $this->col_values           = array();
+        $this->returning            = array();
+        $this->parameters           = array();
+    }
+
+    /**
+     * 创建 SELECT SQL
+     *
+     * @return string
+     */
+    protected function buildSELECT()
+    {
+        return 'SELECT'
+        . $this->buildFlags()
+        . $this->buildCols()
+        . $this->buildFrom()
+        . $this->buildWhere()
+        . $this->buildGroupBy()
+        . $this->buildHaving()
+        . $this->buildOrderBy()
+        . $this->buildLimit()
+        . $this->buildForUpdate();
+    }
+
+    /**
+     * 创建 DELETE SQL
+     */
+    protected function buildDELETE()
+    {
+        return 'DELETE'
+        . $this->buildFlags()
+        . $this->buildFrom()
+        . $this->buildWhere()
+        . $this->buildOrderBy()
+        . $this->buildLimit()
+        . $this->buildReturning();
+    }
+
+    /**
+     * 生成 SELECT 列语句
+     *
+     * @return string
+     * @throws Exception
+     */
+    protected function buildCols()
+    {
+        if (!$this->cols) {
+            throw new Exception('No columns in the SELECT.');
+        }
+
+        $cols = array();
+        foreach ($this->cols as $key => $val) {
+            if (is_int($key)) {
+                $cols[] = $this->quoteNamesIn($val);
+            } else {
+                $cols[] = $this->quoteNamesIn("$val AS $key");
+            }
+        }
+
+        return $this->indentCsv($cols);
+    }
+
+    /**
+     * 生成 FROM 语句.
+     *
+     * @return string
+     */
+    protected function buildFrom()
+    {
+        if (!$this->from) {
+            return '';
+        }
+
+        $refs = array();
+        foreach ($this->from as $from) {
+            $refs[] = implode(' ', $from);
+        }
+        return ' FROM' . $this->indentCsv($refs);
+    }
+
+    /**
+     * 生成 GROUP BY 语句.
+     *
+     * @return string
+     */
+    protected function buildGroupBy()
+    {
+        if (!$this->group_by) {
+            return '';
+        }
+        return ' GROUP BY' . $this->indentCsv($this->group_by);
+    }
+
+    /**
+     * 生成 HAVING 语句.
+     *
+     * @return string
+     */
+    protected function buildHaving()
+    {
+        if (!$this->having) {
+            return '';
+        }
+        return ' HAVING' . $this->indent($this->having);
+    }
+
+    /**
+     * 生成 FOR UPDATE 语句
+     *
+     * @return string
+     */
+    protected function buildForUpdate()
+    {
+        if (!$this->for_update) {
+            return '';
+        }
+        return ' FOR UPDATE';
+    }
+
+    /**
+     * where
+     *
+     * @param string|array $cond
+     * @return self
+     */
+    public function where($cond)
+    {
+        if (is_array($cond)) {
+            foreach ($cond as $key => $val) {
+                if (is_string($key)) {
+                    $this->addWhere('AND', array($key, $val));
+                } else {
+                    $this->addWhere('AND', array($val));
+                }
+            }
+        } else {
+            $this->addWhere('AND', func_get_args());
+        }
+        return $this;
+    }
+
+    /**
+     * or where
+     *
+     * @param string|array $cond
+     * @return self
+     */
+    public function orWhere($cond)
+    {
+        if (is_array($cond)) {
+            foreach ($cond as $key => $val) {
+                if (is_string($key)) {
+                    $this->addWhere('OR', array($key, $val));
+                } else {
+                    $this->addWhere('OR', array($val));
+                }
+            }
+        } else {
+            $this->addWhere('OR', func_get_args());
+        }
+        return $this;
+    }
+
+    /**
+     * limit
+     *
+     * @param int $limit
+     * @return self
+     */
+    public function limit($limit)
+    {
+        $this->limit = (int)$limit;
+        return $this;
+    }
+
+    /**
+     * limit offset
+     *
+     * @param int $offset
+     * @return self
+     */
+    public function offset($offset)
+    {
+        $this->offset = (int)$offset;
+        return $this;
+    }
+
+    /**
+     * orderby.
+     *
+     * @param array $cols
+     * @return self
+     */
+    public function orderBy(array $cols)
+    {
+        return $this->addOrderBy($cols);
+    }
+
+    /**
+     * order by ASC OR DESC
+     *
+     * @param array $cols
+     * @param bool  $order_asc
+     * @return self
+     */
+    public function orderByASC(array $cols, $order_asc = true)
+    {
+        $this->order_asc = $order_asc;
+        return $this->addOrderBy($cols);
+    }
+
+    /**
+     * order by DESC
+     *
+     * @param array $cols
+     * @return self
+     */
+    public function orderByDESC(array $cols)
+    {
+        $this->order_asc = false;
+        return $this->addOrderBy($cols);
+    }
+
+    // -------------abstractquery----------
+    /**
+     * 返回逗号分隔的字符串
+     *
+     * @param array $list
+     * @return string
+     */
+    protected function indentCsv(array $list)
+    {
+        return ' ' . implode(',', $list);
+    }
+
+    /**
+     * 返回空格分隔的字符串
+     *
+     * @param array $list
+     * @return string
+     */
+    protected function indent(array $list)
+    {
+        return ' ' . implode(' ', $list);
+    }
+
+    /**
+     * 批量为占位符绑定值
+     *
+     * @param array $bind_values
+     * @return self
+     *
+     */
+    public function bindValues(array $bind_values)
+    {
+        foreach ($bind_values as $key => $val) {
+            $this->bindValue($key, $val);
+        }
+        return $this;
+    }
+
+    /**
+     * 单个为占位符绑定值
+     *
+     * @param string $name
+     * @param mixed  $value
+     * @return self
+     */
+    public function bindValue($name, $value)
+    {
+        $this->bind_values[$name] = $value;
+        return $this;
+    }
+
+    /**
+     * 生成 flag
+     *
+     * @return string
+     */
+    protected function buildFlags()
+    {
+        if (!$this->flags) {
+            return '';
+        }
+        return ' ' . implode(' ', array_keys($this->flags));
+    }
+
+    /**
+     * 设置 flag.
+     *
+     * @param string $flag
+     * @param bool   $enable
+     */
+    protected function setFlag($flag, $enable = true)
+    {
+        if ($enable) {
+            $this->flags[$flag] = true;
+        } else {
+            unset($this->flags[$flag]);
+        }
+    }
+
+    /**
+     * 重置 flag
+     */
+    protected function resetFlags()
+    {
+        $this->flags = array();
+    }
+
+    /**
+     *
+     * 添加 where 语句
+     *
+     * @param string $andor 'AND' or 'OR
+     * @param array  $conditions
+     * @return self
+     *
+     */
+    protected function addWhere($andor, $conditions)
+    {
+        $this->addClauseCondWithBind('where', $andor, $conditions);
+        return $this;
+    }
+
+    /**
+     * 添加条件和绑定值
+     *
+     * @param string $clause where 、having等
+     * @param string $andor  AND、OR等
+     * @param array  $conditions
+     */
+    protected function addClauseCondWithBind($clause, $andor, $conditions)
+    {
+        $cond = array_shift($conditions);
+        $cond = $this->quoteNamesIn($cond);
+
+        $bind =& $this->{"bind_{$clause}"};
+        foreach ($conditions as $value) {
+            $bind[] = $value;
+        }
+
+        $clause =& $this->$clause;
+        if ($clause) {
+            $clause[] = "$andor $cond";
+        } else {
+            $clause[] = $cond;
+        }
+    }
+
+    /**
+     * 生成 where 语句
+     *
+     * @return string
+     */
+    protected function buildWhere()
+    {
+        if (!$this->where) {
+            return '';
+        }
+        return ' WHERE' . $this->indent($this->where);
+    }
+
+    /**
+     * 增加 order by
+     *
+     * @param array $spec The columns and direction to order by.
+     * @return self
+     */
+    protected function addOrderBy(array $spec)
+    {
+        foreach ($spec as $col) {
+            $this->order_by[] = $this->quoteNamesIn($col);
+        }
+        return $this;
+    }
+
+    /**
+     * 生成 order by 语句
+     *
+     * @return string
+     */
+    protected function buildOrderBy()
+    {
+        if (!$this->order_by) {
+            return '';
+        }
+
+        if ($this->order_asc) {
+            return ' ORDER BY' . $this->indentCsv($this->order_by) . ' ASC';
+        } else {
+            return ' ORDER BY' . $this->indentCsv($this->order_by) . ' DESC';
+        }
+    }
+
+    /**
+     * 生成 limit 语句
+     *
+     * @return string
+     */
+    protected function buildLimit()
+    {
+        $has_limit  = $this->type == 'DELETE' || $this->type == 'UPDATE';
+        $has_offset = $this->type == 'SELECT';
+
+        if ($has_offset && $this->limit) {
+            $clause = " LIMIT {$this->limit}";
+            if ($this->offset) {
+                $clause .= " OFFSET {$this->offset}";
+            }
+            return $clause;
+        } elseif ($has_limit && $this->limit) {
+            return " LIMIT {$this->limit}";
+        }
+        return '';
+    }
+
+    /**
+     * Quotes
+     *
+     * @param string $spec
+     * @return string|array
+     */
+    public function quoteName($spec)
+    {
+        $spec = trim($spec);
+        $seps = array(' AS ', ' ', '.');
+        foreach ($seps as $sep) {
+            $pos = strripos($spec, $sep);
+            if ($pos) {
+                return $this->quoteNameWithSeparator($spec, $sep, $pos);
+            }
+        }
+        return $this->replaceName($spec);
+    }
+
+    /**
+     * 指定分隔符的 Quotes
+     *
+     * @param string $spec
+     * @param string $sep
+     * @param int    $pos
+     * @return string
+     */
+    protected function quoteNameWithSeparator($spec, $sep, $pos)
+    {
+        $len   = strlen($sep);
+        $part1 = $this->quoteName(substr($spec, 0, $pos));
+        $part2 = $this->replaceName(substr($spec, $pos + $len));
+        return "{$part1}{$sep}{$part2}";
+    }
+
+    /**
+     * Quotes "table.col" 格式的字符串
+     *
+     * @param string $text
+     * @return string|array
+     */
+    public function quoteNamesIn($text)
+    {
+        $list = $this->getListForQuoteNamesIn($text);
+        $last = count($list) - 1;
+        $text = null;
+        foreach ($list as $key => $val) {
+            if (($key + 1) % 3) {
+                $text .= $this->quoteNamesInLoop($val, $key == $last);
+            }
+        }
+        return $text;
+    }
+
+    /**
+     * 返回 quote 元素列表
+     *
+     * @param string $text
+     * @return array
+     */
+    protected function getListForQuoteNamesIn($text)
+    {
+        $apos = "'";
+        $quot = '"';
+        return preg_split(
+            "/(($apos+|$quot+|\\$apos+|\\$quot+).*?\\2)/",
+            $text,
+            -1,
+            PREG_SPLIT_DELIM_CAPTURE
+        );
+    }
+
+    /**
+     * 循环 quote
+     *
+     * @param string $val
+     * @param bool   $is_last
+     * @return string
+     */
+    protected function quoteNamesInLoop($val, $is_last)
+    {
+        if ($is_last) {
+            return $this->replaceNamesAndAliasIn($val);
+        }
+        return $this->replaceNamesIn($val);
+    }
+
+    /**
+     * 替换成别名
+     *
+     * @param string $val
+     * @return string
+     */
+    protected function replaceNamesAndAliasIn($val)
+    {
+        $quoted = $this->replaceNamesIn($val);
+        $pos    = strripos($quoted, ' AS ');
+        if ($pos !== false) {
+            $bracket = strripos($quoted, ')');
+            if ($bracket === false) {
+                $alias = $this->replaceName(substr($quoted, $pos + 4));
+                $quoted = substr($quoted, 0, $pos) . " AS $alias";
+            }
+        }
+        return $quoted;
+    }
+
+    /**
+     * Quotes name
+     *
+     * @param string $name
+     * @return string
+     */
+    protected function replaceName($name)
+    {
+        $name = trim($name);
+        if ($name == '*') {
+            return $name;
+        }
+        return '`' . $name . '`';
+    }
+
+    /**
+     * Quotes
+     *
+     * @param string $text
+     * @return string|array
+     */
+    protected function replaceNamesIn($text)
+    {
+        $is_string_literal = strpos($text, "'") !== false
+            || strpos($text, '"') !== false;
+        if ($is_string_literal) {
+            return $text;
+        }
+
+        $word = '[a-z_][a-z0-9_]*';
+
+        $find = "/(\\b)($word)\\.($word)(\\b)/i";
+
+        $repl = '$1`$2`.`$3`$4';
+
+        $text = preg_replace($find, $repl, $text);
+
+        return $text;
+    }
+
+    // ---------- insert --------------
+    /**
+     * 设置 `table.column` 与 last-insert-id 的映射
+     *
+     * @param array $last_insert_id_names
+     */
+    public function setLastInsertIdNames(array $last_insert_id_names)
+    {
+        $this->last_insert_id_names = $last_insert_id_names;
+    }
+
+    /**
+     * insert into.
+     *
+     * @param string $table
+     * @return self
+     */
+    public function into($table)
+    {
+        $this->table = $this->quoteName($table);
+        return $this;
+    }
+
+    /**
+     * 生成 INSERT 语句
+     *
+     * @return string
+     */
+    protected function buildINSERT()
+    {
+        return 'INSERT'
+        . $this->buildFlags()
+        . $this->buildInto()
+        . $this->buildValuesForInsert()
+        . $this->buildReturning();
+    }
+
+    /**
+     * 生成 INTO 语句
+     *
+     * @return string
+     */
+    protected function buildInto()
+    {
+        return " INTO " . $this->table;
+    }
+
+    /**
+     * PDO::lastInsertId()
+     *
+     * @param string $col
+     * @return mixed
+     */
+    public function getLastInsertIdName($col)
+    {
+        $key = str_replace('`', '', $this->table) . '.' . $col;
+        if (isset($this->last_insert_id_names[$key])) {
+            return $this->last_insert_id_names[$key];
+        }
+
+        return null;
+    }
+
+    /**
+     * 设置一列,如果有第二各参数,则把第二个参数绑定在占位符上
+     *
+     * @param string $col
+     * @return self
+     */
+    public function col($col)
+    {
+        return call_user_func_array(array($this, 'addCol'), func_get_args());
+    }
+
+    /**
+     * 设置多列
+     *
+     * @param array $cols
+     * @return self
+     */
+    public function cols(array $cols)
+    {
+        if ($this->type == 'SELECT') {
+            foreach ($cols as $key => $val) {
+                $this->addColSELECT($key, $val);
+            }
+            return $this;
+        }
+        return $this->addCols($cols);
+    }
+
+    /**
+     * 直接设置列的值
+     *
+     * @param string $col
+     * @param string $value
+     * @return self
+     */
+    public function set($col, $value)
+    {
+        return $this->setCol($col, $value);
+    }
+
+    /**
+     * 为 INSERT 语句绑定值
+     *
+     * @return string
+     */
+    protected function buildValuesForInsert()
+    {
+        return ' (' . $this->indentCsv(array_keys($this->col_values)) . ') VALUES (' .
+        $this->indentCsv(array_values($this->col_values)) . ')';
+    }
+
+    // ------update-------
+    /**
+     * 更新哪个表
+     *
+     * @param string $table
+     * @return self
+     */
+    public function table($table)
+    {
+        $this->table = $this->quoteName($table);
+        return $this;
+    }
+
+    /**
+     * 生成完整 SQL 语句
+     *
+     * @return string
+     * @throws Exception
+     */
+    protected function build()
+    {
+        switch ($this->type) {
+            case 'DELETE':
+                return $this->buildDELETE();
+            case 'INSERT':
+                return $this->buildINSERT();
+            case 'UPDATE':
+                return $this->buildUPDATE();
+            case 'SELECT':
+                return $this->buildSELECT();
+        }
+        throw new Exception("type empty");
+    }
+
+    /**
+     * 生成更新的 SQL 语句
+     */
+    protected function buildUPDATE()
+    {
+        return 'UPDATE'
+        . $this->buildFlags()
+        . $this->buildTable()
+        . $this->buildValuesForUpdate()
+        . $this->buildWhere()
+        . $this->buildOrderBy()
+        . $this->buildLimit()
+        . $this->buildReturning();
+    }
+
+    /**
+     * 哪个表
+     *
+     * @return string
+     */
+    protected function buildTable()
+    {
+        return " {$this->table}";
+    }
+
+    /**
+     * 为更新语句绑定值
+     *
+     * @return string
+     */
+    protected function buildValuesForUpdate()
+    {
+        $values = array();
+        foreach ($this->col_values as $col => $value) {
+            $values[] = "{$col} = {$value}";
+        }
+        return ' SET' . $this->indentCsv($values);
+    }
+
+    // ----------Dml---------------
+    /**
+     * 获取绑定的值
+     *
+     * @return array
+     */
+    public function getBindValuesCOMMON()
+    {
+        $bind_values = $this->bind_values;
+        $i           = 1;
+        foreach ($this->bind_where as $val) {
+            $bind_values[$i] = $val;
+            $i++;
+        }
+        return $bind_values;
+    }
+
+    /**
+     * 设置列
+     *
+     * @param string $col
+     * @return self
+     */
+    protected function addCol($col)
+    {
+        $key                    = $this->quoteName($col);
+        $this->col_values[$key] = ":$col";
+        $args                   = func_get_args();
+        if (count($args) > 1) {
+            $this->bindValue($col, $args[1]);
+        }
+        return $this;
+    }
+
+    /**
+     * 设置多个列
+     *
+     * @param array $cols
+     * @return self
+     */
+    protected function addCols(array $cols)
+    {
+        foreach ($cols as $key => $val) {
+            if (is_int($key)) {
+                $this->addCol($val);
+            } else {
+                $this->addCol($key, $val);
+            }
+        }
+        return $this;
+    }
+
+    /**
+     * 设置单列的值
+     *
+     * @param string $col .
+     * @param string $value
+     * @return self
+     */
+    protected function setCol($col, $value)
+    {
+        if ($value === null) {
+            $value = 'NULL';
+        }
+
+        $key                    = $this->quoteName($col);
+        $value                  = $this->quoteNamesIn($value);
+        $this->col_values[$key] = $value;
+        return $this;
+    }
+
+    /**
+     * 增加返回的列
+     *
+     * @param array $cols
+     * @return self
+     *
+     */
+    protected function addReturning(array $cols)
+    {
+        foreach ($cols as $col) {
+            $this->returning[] = $this->quoteNamesIn($col);
+        }
+        return $this;
+    }
+
+    /**
+     * 生成 RETURNING 语句
+     *
+     * @return string
+     */
+    protected function buildReturning()
+    {
+        if (!$this->returning) {
+            return '';
+        }
+        return ' RETURNING' . $this->indentCsv($this->returning);
+    }
+
+    /**
+     * 构造函数
+     *
+     * @param string $host
+     * @param int    $port
+     * @param string $user
+     * @param string $password
+     * @param string $db_name
+     * @param string $charset
+     */
+    public function __construct($host, $port, $user, $password, $db_name, $charset = 'utf8')
+    {
+        $this->settings = array(
+            'host'     => $host,
+            'port'     => $port,
+            'user'     => $user,
+            'password' => $password,
+            'dbname'   => $db_name,
+            'charset'  => $charset,
+        );
+        $this->connect();
+    }
+
+    /**
+     * 创建 PDO 实例
+     */
+    protected function connect()
+    {
+        $dsn       = 'mysql:dbname=' . $this->settings["dbname"] . ';host=' .
+            $this->settings["host"] . ';port=' . $this->settings['port'];
+        $this->pdo = new PDO($dsn, $this->settings["user"], $this->settings["password"],
+            array(
+                PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . (!empty($this->settings['charset']) ?
+                        $this->settings['charset'] : 'utf8')
+            ));
+        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+        $this->pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
+        $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
+    }
+
+   /**
+    * 关闭连接
+    */
+    public function closeConnection()
+    {
+        $this->pdo = null;
+    }
+
+    /**
+     * 执行
+     *
+     * @param string $query
+     * @param string $parameters
+     * @throws PDOException
+     */
+    protected function execute($query, $parameters = "")
+    {
+        try {
+            if (is_null($this->pdo)) { 
+                $this->connect(); 
+            }
+            $this->sQuery = @$this->pdo->prepare($query);
+            $this->bindMore($parameters);
+            if (!empty($this->parameters)) {
+                foreach ($this->parameters as $param) {
+                    $this->sQuery->bindParam($param[0], $param[1]);
+                }
+            }
+            $this->success = $this->sQuery->execute();
+        } catch (PDOException $e) {
+            // 服务端断开时重连一次
+            if ($e->errorInfo[1] == 2006 || $e->errorInfo[1] == 2013) {
+                $this->closeConnection();
+                $this->connect();
+
+                try {
+                    $this->sQuery = $this->pdo->prepare($query);
+                    $this->bindMore($parameters);
+                    if (!empty($this->parameters)) {
+                        foreach ($this->parameters as $param) {
+                            $this->sQuery->bindParam($param[0], $param[1]);
+                        }
+                    }
+                    $this->success = $this->sQuery->execute();
+                } catch (PDOException $ex) {
+                    $this->rollBackTrans();
+                    throw $ex;
+                }
+            } else {
+                $this->rollBackTrans();
+                $msg = $e->getMessage();
+                $err_msg = "SQL:".$this->lastSQL()." ".$msg;
+                $exception = new \PDOException($err_msg, (int)$e->getCode());
+                throw $exception;
+            }
+        }
+        $this->parameters = array();
+    }
+
+    /**
+     * 绑定
+     *
+     * @param string $para
+     * @param string $value
+     */
+    public function bind($para, $value)
+    {
+        if (is_string($para)) {
+            $this->parameters[sizeof($this->parameters)] = array(":" . $para, $value);
+        } else {
+            $this->parameters[sizeof($this->parameters)] = array($para, $value);
+        }
+    }
+
+    /**
+     * 绑定多个
+     *
+     * @param array $parray
+     */
+    public function bindMore($parray)
+    {
+        if (empty($this->parameters) && is_array($parray)) {
+            $columns = array_keys($parray);
+            foreach ($columns as $i => &$column) {
+                $this->bind($column, $parray[$column]);
+            }
+        }
+    }
+
+    /**
+     * 执行 SQL
+     *
+     * @param string $query
+     * @param array  $params
+     * @param int    $fetchmode
+     * @return mixed
+     */
+    public function query($query = '', $params = null, $fetchmode = PDO::FETCH_ASSOC)
+    {
+        $query = trim($query);
+        if (empty($query)) {
+            $query = $this->build();
+            if (!$params) {
+                $params = $this->getBindValues();
+            }
+        }
+
+        $this->resetAll();
+        $this->lastSql = $query;
+
+        $this->execute($query, $params);
+
+        $rawStatement = explode(" ", $query);
+
+        $statement = strtolower(trim($rawStatement[0]));
+        if ($statement === 'select' || $statement === 'show') {
+            return $this->sQuery->fetchAll($fetchmode);
+        } elseif ($statement === 'update' || $statement === 'delete' || $statement === 'replace') {
+            return $this->sQuery->rowCount();
+        } elseif ($statement === 'insert') {
+            if ($this->sQuery->rowCount() > 0) {
+                return $this->lastInsertId();
+            }
+        } else {
+            return null;
+        }
+
+        return null;
+    }
+
+    /**
+     * 返回一列
+     *
+     * @param  string $query
+     * @param  array  $params
+     * @return array
+     */
+    public function column($query = '', $params = null)
+    {
+        $query = trim($query);
+        if (empty($query)) {
+            $query = $this->build();
+            if (!$params) {
+                $params = $this->getBindValues();
+            }
+        }
+
+        $this->resetAll();
+        $this->lastSql = $query;
+
+        $this->execute($query, $params);
+        $columns = $this->sQuery->fetchAll(PDO::FETCH_NUM);
+        $column  = null;
+        foreach ($columns as $cells) {
+            $column[] = $cells[0];
+        }
+        return $column;
+    }
+
+    /**
+     * 返回一行
+     *
+     * @param  string $query
+     * @param  array  $params
+     * @param  int    $fetchmode
+     * @return array
+     */
+    public function row($query = '', $params = null, $fetchmode = PDO::FETCH_ASSOC)
+    {
+        $query = trim($query);
+        if (empty($query)) {
+            $query = $this->build();
+            if (!$params) {
+                $params = $this->getBindValues();
+            }
+        }
+
+        $this->resetAll();
+        $this->lastSql = $query;
+
+        $this->execute($query, $params);
+        return $this->sQuery->fetch($fetchmode);
+    }
+
+    /**
+     * 返回单个值
+     *
+     * @param  string $query
+     * @param  array  $params
+     * @return string
+     */
+    public function single($query = '', $params = null)
+    {
+        $query = trim($query);
+        if (empty($query)) {
+            $query = $this->build();
+            if (!$params) {
+                $params = $this->getBindValues();
+            }
+        }
+
+        $this->resetAll();
+        $this->lastSql = $query;
+
+        $this->execute($query, $params);
+        return $this->sQuery->fetchColumn();
+    }
+
+    /**
+     * 返回 lastInsertId
+     *
+     * @return string
+     */
+    public function lastInsertId()
+    {
+        return $this->pdo->lastInsertId();
+    }
+
+    /**
+     * 返回最后一条执行的 sql
+     *
+     * @return  string
+     */
+    public function lastSQL()
+    {
+        return $this->lastSql;
+    }
+
+    /**
+     * 开始事务
+     */
+    public function beginTrans()
+    {
+        try {
+            if (is_null($this->pdo)) { 
+                $this->connect(); 
+            }
+            return $this->pdo->beginTransaction();
+        } catch (PDOException $e) {
+            // 服务端断开时重连一次
+            if ($e->errorInfo[1] == 2006 || $e->errorInfo[1] == 2013) {
+                $this->closeConnection();
+                $this->connect();
+                return $this->pdo->beginTransaction();
+            } else {
+                throw $e;
+            }
+        }
+    }
+
+    /**
+     * 提交事务
+     */
+    public function commitTrans()
+    {
+        return $this->pdo->commit();
+    }
+
+    /**
+     * 事务回滚
+     */
+    public function rollBackTrans()
+    {
+        if ($this->pdo->inTransaction()) {
+            return $this->pdo->rollBack();
+        }
+        return true;
+    }
+}

BIN
GatewayWorker/vendor/workerman/workerman.log


+ 6 - 0
GatewayWorker/vendor/workerman/workerman/.gitignore

@@ -0,0 +1,6 @@
+logs
+.buildpath
+.project
+.settings
+.idea
+.DS_Store

+ 69 - 0
GatewayWorker/vendor/workerman/workerman/Autoloader.php

@@ -0,0 +1,69 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link      http://www.workerman.net/
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Workerman;
+
+/**
+ * Autoload.
+ */
+class Autoloader
+{
+    /**
+     * Autoload root path.
+     *
+     * @var string
+     */
+    protected static $_autoloadRootPath = '';
+
+    /**
+     * Set autoload root path.
+     *
+     * @param string $root_path
+     * @return void
+     */
+    public static function setRootPath($root_path)
+    {
+        self::$_autoloadRootPath = $root_path;
+    }
+
+    /**
+     * Load files by namespace.
+     *
+     * @param string $name
+     * @return boolean
+     */
+    public static function loadByNamespace($name)
+    {
+        $class_path = \str_replace('\\', \DIRECTORY_SEPARATOR, $name);
+        if (\strpos($name, 'Workerman\\') === 0) {
+            $class_file = __DIR__ . \substr($class_path, \strlen('Workerman')) . '.php';
+        } else {
+            if (self::$_autoloadRootPath) {
+                $class_file = self::$_autoloadRootPath . \DIRECTORY_SEPARATOR . $class_path . '.php';
+            }
+            if (empty($class_file) || !\is_file($class_file)) {
+                $class_file = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . "$class_path.php";
+            }
+        }
+
+        if (\is_file($class_file)) {
+            require_once($class_file);
+            if (\class_exists($name, false)) {
+                return true;
+            }
+        }
+        return false;
+    }
+}
+
+\spl_autoload_register('\Workerman\Autoloader::loadByNamespace');

+ 377 - 0
GatewayWorker/vendor/workerman/workerman/Connection/AsyncTcpConnection.php

@@ -0,0 +1,377 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link      http://www.workerman.net/
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Workerman\Connection;
+
+use Workerman\Events\EventInterface;
+use Workerman\Lib\Timer;
+use Workerman\Worker;
+use \Exception;
+
+/**
+ * AsyncTcpConnection.
+ */
+class AsyncTcpConnection extends TcpConnection
+{
+    /**
+     * Emitted when socket connection is successfully established.
+     *
+     * @var callable|null
+     */
+    public $onConnect = null;
+
+    /**
+     * Transport layer protocol.
+     *
+     * @var string
+     */
+    public $transport = 'tcp';
+
+    /**
+     * Status.
+     *
+     * @var int
+     */
+    protected $_status = self::STATUS_INITIAL;
+
+    /**
+     * Remote host.
+     *
+     * @var string
+     */
+    protected $_remoteHost = '';
+
+    /**
+     * Remote port.
+     *
+     * @var int
+     */
+    protected $_remotePort = 80;
+
+    /**
+     * Connect start time.
+     *
+     * @var float
+     */
+    protected $_connectStartTime = 0;
+
+    /**
+     * Remote URI.
+     *
+     * @var string
+     */
+    protected $_remoteURI = '';
+
+    /**
+     * Context option.
+     *
+     * @var array
+     */
+    protected $_contextOption = null;
+
+    /**
+     * Reconnect timer.
+     *
+     * @var int
+     */
+    protected $_reconnectTimer = null;
+
+
+    /**
+     * PHP built-in protocols.
+     *
+     * @var array
+     */
+    protected static $_builtinTransports = array(
+        'tcp'   => 'tcp',
+        'udp'   => 'udp',
+        'unix'  => 'unix',
+        'ssl'   => 'ssl',
+        'sslv2' => 'sslv2',
+        'sslv3' => 'sslv3',
+        'tls'   => 'tls'
+    );
+
+    /**
+     * Construct.
+     *
+     * @param string $remote_address
+     * @param array $context_option
+     * @throws Exception
+     */
+    public function __construct($remote_address, array $context_option = array())
+    {
+        $address_info = \parse_url($remote_address);
+        if (!$address_info) {
+            list($scheme, $this->_remoteAddress) = \explode(':', $remote_address, 2);
+            if (!$this->_remoteAddress) {
+                Worker::safeEcho(new \Exception('bad remote_address'));
+            }
+        } else {
+            if (!isset($address_info['port'])) {
+                $address_info['port'] = 0;
+            }
+            if (!isset($address_info['path'])) {
+                $address_info['path'] = '/';
+            }
+            if (!isset($address_info['query'])) {
+                $address_info['query'] = '';
+            } else {
+                $address_info['query'] = '?' . $address_info['query'];
+            }
+            $this->_remoteAddress = "{$address_info['host']}:{$address_info['port']}";
+            $this->_remoteHost    = $address_info['host'];
+            $this->_remotePort    = $address_info['port'];
+            $this->_remoteURI     = "{$address_info['path']}{$address_info['query']}";
+            $scheme               = isset($address_info['scheme']) ? $address_info['scheme'] : 'tcp';
+        }
+
+        $this->id = $this->_id = self::$_idRecorder++;
+        if(\PHP_INT_MAX === self::$_idRecorder){
+            self::$_idRecorder = 0;
+        }
+        // Check application layer protocol class.
+        if (!isset(self::$_builtinTransports[$scheme])) {
+            $scheme         = \ucfirst($scheme);
+            $this->protocol = '\\Protocols\\' . $scheme;
+            if (!\class_exists($this->protocol)) {
+                $this->protocol = "\\Workerman\\Protocols\\$scheme";
+                if (!\class_exists($this->protocol)) {
+                    throw new Exception("class \\Protocols\\$scheme not exist");
+                }
+            }
+        } else {
+            $this->transport = self::$_builtinTransports[$scheme];
+        }
+
+        // For statistics.
+        ++self::$statistics['connection_count'];
+        $this->maxSendBufferSize         = self::$defaultMaxSendBufferSize;
+        $this->maxPackageSize            = self::$defaultMaxPackageSize;
+        $this->_contextOption            = $context_option;
+        static::$connections[$this->_id] = $this;
+    }
+
+    /**
+     * Do connect.
+     *
+     * @return void
+     */
+    public function connect()
+    {
+        if ($this->_status !== self::STATUS_INITIAL && $this->_status !== self::STATUS_CLOSING &&
+            $this->_status !== self::STATUS_CLOSED) {
+            return;
+        }
+        $this->_status           = self::STATUS_CONNECTING;
+        $this->_connectStartTime = \microtime(true);
+        if ($this->transport !== 'unix') {
+            if (!$this->_remotePort) {
+                $this->_remotePort = $this->transport === 'ssl' ? 443 : 80;
+                $this->_remoteAddress = $this->_remoteHost.':'.$this->_remotePort;
+            }
+            // Open socket connection asynchronously.
+            if ($this->_contextOption) {
+                $context = \stream_context_create($this->_contextOption);
+                $this->_socket = \stream_socket_client("tcp://{$this->_remoteHost}:{$this->_remotePort}",
+                    $errno, $errstr, 0, \STREAM_CLIENT_ASYNC_CONNECT, $context);
+            } else {
+                $this->_socket = \stream_socket_client("tcp://{$this->_remoteHost}:{$this->_remotePort}",
+                    $errno, $errstr, 0, \STREAM_CLIENT_ASYNC_CONNECT);
+            }
+        } else {
+            $this->_socket = \stream_socket_client("{$this->transport}://{$this->_remoteAddress}", $errno, $errstr, 0,
+                \STREAM_CLIENT_ASYNC_CONNECT);
+        }
+        // If failed attempt to emit onError callback.
+        if (!$this->_socket || !\is_resource($this->_socket)) {
+            $this->emitError(\WORKERMAN_CONNECT_FAIL, $errstr);
+            if ($this->_status === self::STATUS_CLOSING) {
+                $this->destroy();
+            }
+            if ($this->_status === self::STATUS_CLOSED) {
+                $this->onConnect = null;
+            }
+            return;
+        }
+        // Add socket to global event loop waiting connection is successfully established or faild.
+        Worker::$globalEvent->add($this->_socket, EventInterface::EV_WRITE, array($this, 'checkConnection'));
+        // For windows.
+        if(\DIRECTORY_SEPARATOR === '\\') {
+            Worker::$globalEvent->add($this->_socket, EventInterface::EV_EXCEPT, array($this, 'checkConnection'));
+        }
+    }
+
+    /**
+     * Reconnect.
+     *
+     * @param int $after
+     * @return void
+     */
+    public function reconnect($after = 0)
+    {
+        $this->_status                   = self::STATUS_INITIAL;
+        static::$connections[$this->_id] = $this;
+        if ($this->_reconnectTimer) {
+            Timer::del($this->_reconnectTimer);
+        }
+        if ($after > 0) {
+            $this->_reconnectTimer = Timer::add($after, array($this, 'connect'), null, false);
+            return;
+        }
+        $this->connect();
+    }
+
+    /**
+     * CancelReconnect.
+     */
+    public function cancelReconnect()
+    {
+        if ($this->_reconnectTimer) {
+            Timer::del($this->_reconnectTimer);
+        }
+    }
+
+    /**
+     * Get remote address.
+     *
+     * @return string
+     */
+    public function getRemoteHost()
+    {
+        return $this->_remoteHost;
+    }
+
+    /**
+     * Get remote URI.
+     *
+     * @return string
+     */
+    public function getRemoteURI()
+    {
+        return $this->_remoteURI;
+    }
+
+    /**
+     * Try to emit onError callback.
+     *
+     * @param int    $code
+     * @param string $msg
+     * @return void
+     */
+    protected function emitError($code, $msg)
+    {
+        $this->_status = self::STATUS_CLOSING;
+        if ($this->onError) {
+            try {
+                \call_user_func($this->onError, $this, $code, $msg);
+            } catch (\Exception $e) {
+                Worker::log($e);
+                exit(250);
+            } catch (\Error $e) {
+                Worker::log($e);
+                exit(250);
+            }
+        }
+    }
+
+    /**
+     * Check connection is successfully established or faild.
+     *
+     * @param resource $socket
+     * @return void
+     */
+    public function checkConnection()
+    {
+        // Remove EV_EXPECT for windows.
+        if(\DIRECTORY_SEPARATOR === '\\') {
+            Worker::$globalEvent->del($this->_socket, EventInterface::EV_EXCEPT);
+        }
+
+        // Remove write listener.
+        Worker::$globalEvent->del($this->_socket, EventInterface::EV_WRITE);
+
+        if ($this->_status !== self::STATUS_CONNECTING) {
+            return;
+        }
+
+        // Check socket state.
+        if ($address = \stream_socket_get_name($this->_socket, true)) {
+            // Nonblocking.
+            \stream_set_blocking($this->_socket, false);
+            // Compatible with hhvm
+            if (\function_exists('stream_set_read_buffer')) {
+                \stream_set_read_buffer($this->_socket, 0);
+            }
+            // Try to open keepalive for tcp and disable Nagle algorithm.
+            if (\function_exists('socket_import_stream') && $this->transport === 'tcp') {
+                $raw_socket = \socket_import_stream($this->_socket);
+                \socket_set_option($raw_socket, \SOL_SOCKET, \SO_KEEPALIVE, 1);
+                \socket_set_option($raw_socket, \SOL_TCP, \TCP_NODELAY, 1);
+            }
+
+            // SSL handshake.
+            if ($this->transport === 'ssl') {
+                $this->_sslHandshakeCompleted = $this->doSslHandshake($this->_socket);
+                if ($this->_sslHandshakeCompleted === false) {
+                    return;
+                }
+            } else {
+                // There are some data waiting to send.
+                if ($this->_sendBuffer) {
+                    Worker::$globalEvent->add($this->_socket, EventInterface::EV_WRITE, array($this, 'baseWrite'));
+                }
+            }
+
+            // Register a listener waiting read event.
+            Worker::$globalEvent->add($this->_socket, EventInterface::EV_READ, array($this, 'baseRead'));
+
+            $this->_status                = self::STATUS_ESTABLISHED;
+            $this->_remoteAddress         = $address;
+
+            // Try to emit onConnect callback.
+            if ($this->onConnect) {
+                try {
+                    \call_user_func($this->onConnect, $this);
+                } catch (\Exception $e) {
+                    Worker::log($e);
+                    exit(250);
+                } catch (\Error $e) {
+                    Worker::log($e);
+                    exit(250);
+                }
+            }
+            // Try to emit protocol::onConnect
+            if (\method_exists($this->protocol, 'onConnect')) {
+                try {
+                    \call_user_func(array($this->protocol, 'onConnect'), $this);
+                } catch (\Exception $e) {
+                    Worker::log($e);
+                    exit(250);
+                } catch (\Error $e) {
+                    Worker::log($e);
+                    exit(250);
+                }
+            }
+        } else {
+            // Connection failed.
+            $this->emitError(\WORKERMAN_CONNECT_FAIL, 'connect ' . $this->_remoteAddress . ' fail after ' . round(\microtime(true) - $this->_connectStartTime, 4) . ' seconds');
+            if ($this->_status === self::STATUS_CLOSING) {
+                $this->destroy();
+            }
+            if ($this->_status === self::STATUS_CLOSED) {
+                $this->onConnect = null;
+            }
+        }
+    }
+}

+ 209 - 0
GatewayWorker/vendor/workerman/workerman/Connection/AsyncUdpConnection.php

@@ -0,0 +1,209 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link      http://www.workerman.net/
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Workerman\Connection;
+
+use Workerman\Events\EventInterface;
+use Workerman\Worker;
+use \Exception;
+
+/**
+ * AsyncTcpConnection.
+ */
+class AsyncUdpConnection extends UdpConnection
+{
+    /**
+     * Emitted when socket connection is successfully established.
+     *
+     * @var callable
+     */
+    public $onConnect = null;
+
+    /**
+     * Emitted when socket connection closed.
+     *
+     * @var callable
+     */
+    public $onClose = null;
+
+    /**
+     * Connected or not.
+     *
+     * @var bool
+     */
+    protected $connected = false;
+
+    /**
+     * Context option.
+     *
+     * @var array
+     */
+    protected $_contextOption = null;
+
+    /**
+     * Construct.
+     *
+     * @param string $remote_address
+     * @throws Exception
+     */
+    public function __construct($remote_address, $context_option = null)
+    {
+        // Get the application layer communication protocol and listening address.
+        list($scheme, $address) = \explode(':', $remote_address, 2);
+        // Check application layer protocol class.
+        if ($scheme !== 'udp') {
+            $scheme         = \ucfirst($scheme);
+            $this->protocol = '\\Protocols\\' . $scheme;
+            if (!\class_exists($this->protocol)) {
+                $this->protocol = "\\Workerman\\Protocols\\$scheme";
+                if (!\class_exists($this->protocol)) {
+                    throw new Exception("class \\Protocols\\$scheme not exist");
+                }
+            }
+        }
+        
+        $this->_remoteAddress = \substr($address, 2);
+        $this->_contextOption = $context_option;
+    }
+    
+    /**
+     * For udp package.
+     *
+     * @param resource $socket
+     * @return bool
+     */
+    public function baseRead($socket)
+    {
+        $recv_buffer = \stream_socket_recvfrom($socket, Worker::MAX_UDP_PACKAGE_SIZE, 0, $remote_address);
+        if (false === $recv_buffer || empty($remote_address)) {
+            return false;
+        }
+        
+        if ($this->onMessage) {
+            if ($this->protocol) {
+                $parser      = $this->protocol;
+                $recv_buffer = $parser::decode($recv_buffer, $this);
+            }
+            ++ConnectionInterface::$statistics['total_request'];
+            try {
+                \call_user_func($this->onMessage, $this, $recv_buffer);
+            } catch (\Exception $e) {
+                Worker::log($e);
+                exit(250);
+            } catch (\Error $e) {
+                Worker::log($e);
+                exit(250);
+            }
+        }
+        return true;
+    }
+
+    /**
+     * Sends data on the connection.
+     *
+     * @param string $send_buffer
+     * @param bool   $raw
+     * @return void|boolean
+     */
+    public function send($send_buffer, $raw = false)
+    {
+        if (false === $raw && $this->protocol) {
+            $parser      = $this->protocol;
+            $send_buffer = $parser::encode($send_buffer, $this);
+            if ($send_buffer === '') {
+                return;
+            }
+        }
+        if ($this->connected === false) {
+            $this->connect();
+        }
+        return \strlen($send_buffer) === \stream_socket_sendto($this->_socket, $send_buffer, 0);
+    }
+    
+    
+    /**
+     * Close connection.
+     *
+     * @param mixed $data
+     * @param bool $raw
+     *
+     * @return bool
+     */
+    public function close($data = null, $raw = false)
+    {
+        if ($data !== null) {
+            $this->send($data, $raw);
+        }
+        Worker::$globalEvent->del($this->_socket, EventInterface::EV_READ);
+        \fclose($this->_socket);
+        $this->connected = false;
+        // Try to emit onClose callback.
+        if ($this->onClose) {
+            try {
+                \call_user_func($this->onClose, $this);
+            } catch (\Exception $e) {
+                Worker::log($e);
+                exit(250);
+            } catch (\Error $e) {
+                Worker::log($e);
+                exit(250);
+            }
+        }
+        $this->onConnect = $this->onMessage = $this->onClose = null;
+        return true;
+    }
+
+    /**
+     * Connect.
+     *
+     * @return void
+     */
+    public function connect()
+    {
+        if ($this->connected === true) {
+            return;
+        }
+        if ($this->_contextOption) {
+            $context = \stream_context_create($this->_contextOption);
+            $this->_socket = \stream_socket_client("udp://{$this->_remoteAddress}", $errno, $errmsg,
+                30, \STREAM_CLIENT_CONNECT, $context);
+        } else {
+            $this->_socket = \stream_socket_client("udp://{$this->_remoteAddress}", $errno, $errmsg);
+        }
+
+        if (!$this->_socket) {
+            Worker::safeEcho(new \Exception($errmsg));
+            return;
+        }
+        
+        \stream_set_blocking($this->_socket, false);
+        
+        if ($this->onMessage) {
+            Worker::$globalEvent->add($this->_socket, EventInterface::EV_READ, array($this, 'baseRead'));
+        }
+        $this->connected = true;
+        // Try to emit onConnect callback.
+        if ($this->onConnect) {
+            try {
+                \call_user_func($this->onConnect, $this);
+            } catch (\Exception $e) {
+                Worker::log($e);
+                exit(250);
+            } catch (\Error $e) {
+                Worker::log($e);
+                exit(250);
+            }
+        }
+    }
+
+}

+ 125 - 0
GatewayWorker/vendor/workerman/workerman/Connection/ConnectionInterface.php

@@ -0,0 +1,125 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link      http://www.workerman.net/
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Workerman\Connection;
+
+/**
+ * ConnectionInterface.
+ */
+abstract class  ConnectionInterface
+{
+    /**
+     * Statistics for status command.
+     *
+     * @var array
+     */
+    public static $statistics = array(
+        'connection_count' => 0,
+        'total_request'    => 0,
+        'throw_exception'  => 0,
+        'send_fail'        => 0,
+    );
+
+    /**
+     * Emitted when data is received.
+     *
+     * @var callable
+     */
+    public $onMessage = null;
+
+    /**
+     * Emitted when the other end of the socket sends a FIN packet.
+     *
+     * @var callable
+     */
+    public $onClose = null;
+
+    /**
+     * Emitted when an error occurs with connection.
+     *
+     * @var callable
+     */
+    public $onError = null;
+
+    /**
+     * Sends data on the connection.
+     *
+     * @param mixed $send_buffer
+     * @return void|boolean
+     */
+    abstract public function send($send_buffer);
+
+    /**
+     * Get remote IP.
+     *
+     * @return string
+     */
+    abstract public function getRemoteIp();
+
+    /**
+     * Get remote port.
+     *
+     * @return int
+     */
+    abstract public function getRemotePort();
+
+    /**
+     * Get remote address.
+     *
+     * @return string
+     */
+    abstract public function getRemoteAddress();
+
+    /**
+     * Get local IP.
+     *
+     * @return string
+     */
+    abstract public function getLocalIp();
+
+    /**
+     * Get local port.
+     *
+     * @return int
+     */
+    abstract public function getLocalPort();
+
+    /**
+     * Get local address.
+     *
+     * @return string
+     */
+    abstract public function getLocalAddress();
+
+    /**
+     * Is ipv4.
+     *
+     * @return bool
+     */
+    abstract public function isIPv4();
+
+    /**
+     * Is ipv6.
+     *
+     * @return bool
+     */
+    abstract public function isIPv6();
+
+    /**
+     * Close connection.
+     *
+     * @param $data
+     * @return void
+     */
+    abstract public function close($data = null);
+}

+ 1012 - 0
GatewayWorker/vendor/workerman/workerman/Connection/TcpConnection.php

@@ -0,0 +1,1012 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link      http://www.workerman.net/
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Workerman\Connection;
+
+use Workerman\Events\EventInterface;
+use Workerman\Worker;
+use \Exception;
+
+/**
+ * TcpConnection.
+ */
+class TcpConnection extends ConnectionInterface
+{
+    /**
+     * Read buffer size.
+     *
+     * @var int
+     */
+    const READ_BUFFER_SIZE = 65535;
+
+    /**
+     * Status initial.
+     *
+     * @var int
+     */
+    const STATUS_INITIAL = 0;
+
+    /**
+     * Status connecting.
+     *
+     * @var int
+     */
+    const STATUS_CONNECTING = 1;
+
+    /**
+     * Status connection established.
+     *
+     * @var int
+     */
+    const STATUS_ESTABLISHED = 2;
+
+    /**
+     * Status closing.
+     *
+     * @var int
+     */
+    const STATUS_CLOSING = 4;
+
+    /**
+     * Status closed.
+     *
+     * @var int
+     */
+    const STATUS_CLOSED = 8;
+
+    /**
+     * Emitted when data is received.
+     *
+     * @var callable
+     */
+    public $onMessage = null;
+
+    /**
+     * Emitted when the other end of the socket sends a FIN packet.
+     *
+     * @var callable
+     */
+    public $onClose = null;
+
+    /**
+     * Emitted when an error occurs with connection.
+     *
+     * @var callable
+     */
+    public $onError = null;
+
+    /**
+     * Emitted when the send buffer becomes full.
+     *
+     * @var callable
+     */
+    public $onBufferFull = null;
+
+    /**
+     * Emitted when the send buffer becomes empty.
+     *
+     * @var callable
+     */
+    public $onBufferDrain = null;
+
+    /**
+     * Application layer protocol.
+     * The format is like this Workerman\\Protocols\\Http.
+     *
+     * @var \Workerman\Protocols\ProtocolInterface
+     */
+    public $protocol = null;
+
+    /**
+     * Transport (tcp/udp/unix/ssl).
+     *
+     * @var string
+     */
+    public $transport = 'tcp';
+
+    /**
+     * Which worker belong to.
+     *
+     * @var Worker
+     */
+    public $worker = null;
+
+    /**
+     * Bytes read.
+     *
+     * @var int
+     */
+    public $bytesRead = 0;
+
+    /**
+     * Bytes written.
+     *
+     * @var int
+     */
+    public $bytesWritten = 0;
+
+    /**
+     * Connection->id.
+     *
+     * @var int
+     */
+    public $id = 0;
+
+    /**
+     * A copy of $worker->id which used to clean up the connection in worker->connections
+     *
+     * @var int
+     */
+    protected $_id = 0;
+
+    /**
+     * Sets the maximum send buffer size for the current connection.
+     * OnBufferFull callback will be emited When the send buffer is full.
+     *
+     * @var int
+     */
+    public $maxSendBufferSize = 1048576;
+
+    /**
+     * Default send buffer size.
+     *
+     * @var int
+     */
+    public static $defaultMaxSendBufferSize = 1048576;
+
+    /**
+     * Sets the maximum acceptable packet size for the current connection.
+     *
+     * @var int
+     */
+    public $maxPackageSize = 1048576;
+    
+    /**
+     * Default maximum acceptable packet size.
+     *
+     * @var int
+     */
+    public static $defaultMaxPackageSize = 10485760;
+
+    /**
+     * Id recorder.
+     *
+     * @var int
+     */
+    protected static $_idRecorder = 1;
+
+    /**
+     * Socket
+     *
+     * @var resource
+     */
+    protected $_socket = null;
+
+    /**
+     * Send buffer.
+     *
+     * @var string
+     */
+    protected $_sendBuffer = '';
+
+    /**
+     * Receive buffer.
+     *
+     * @var string
+     */
+    protected $_recvBuffer = '';
+
+    /**
+     * Current package length.
+     *
+     * @var int
+     */
+    protected $_currentPackageLength = 0;
+
+    /**
+     * Connection status.
+     *
+     * @var int
+     */
+    protected $_status = self::STATUS_ESTABLISHED;
+
+    /**
+     * Remote address.
+     *
+     * @var string
+     */
+    protected $_remoteAddress = '';
+
+    /**
+     * Is paused.
+     *
+     * @var bool
+     */
+    protected $_isPaused = false;
+
+    /**
+     * SSL handshake completed or not.
+     *
+     * @var bool
+     */
+    protected $_sslHandshakeCompleted = false;
+
+    /**
+     * All connection instances.
+     *
+     * @var array
+     */
+    public static $connections = array();
+
+    /**
+     * Status to string.
+     *
+     * @var array
+     */
+    public static $_statusToString = array(
+        self::STATUS_INITIAL     => 'INITIAL',
+        self::STATUS_CONNECTING  => 'CONNECTING',
+        self::STATUS_ESTABLISHED => 'ESTABLISHED',
+        self::STATUS_CLOSING     => 'CLOSING',
+        self::STATUS_CLOSED      => 'CLOSED',
+    );
+
+
+    /**
+     * Adding support of custom functions within protocols
+     *
+     * @param string $name
+     * @param array  $arguments
+     * @return void
+     */
+    public function __call($name, array $arguments) {
+        // Try to emit custom function within protocol
+        if (\method_exists($this->protocol, $name)) {
+            try {
+                return \call_user_func(array($this->protocol, $name), $this, $arguments);
+            } catch (\Exception $e) {
+                Worker::log($e);
+                exit(250);
+            } catch (\Error $e) {
+                Worker::log($e);
+                exit(250);
+            }
+        }
+    }
+
+    /**
+     * Construct.
+     *
+     * @param resource $socket
+     * @param string   $remote_address
+     */
+    public function __construct($socket, $remote_address = '')
+    {
+        ++self::$statistics['connection_count'];
+        $this->id = $this->_id = self::$_idRecorder++;
+        if(self::$_idRecorder === \PHP_INT_MAX){
+            self::$_idRecorder = 0;
+        }
+        $this->_socket = $socket;
+        \stream_set_blocking($this->_socket, 0);
+        // Compatible with hhvm
+        if (\function_exists('stream_set_read_buffer')) {
+            \stream_set_read_buffer($this->_socket, 0);
+        }
+        Worker::$globalEvent->add($this->_socket, EventInterface::EV_READ, array($this, 'baseRead'));
+        $this->maxSendBufferSize        = self::$defaultMaxSendBufferSize;
+        $this->maxPackageSize           = self::$defaultMaxPackageSize;
+        $this->_remoteAddress           = $remote_address;
+        static::$connections[$this->id] = $this;
+    }
+
+    /**
+     * Get status.
+     *
+     * @param bool $raw_output
+     *
+     * @return int|string
+     */
+    public function getStatus($raw_output = true)
+    {
+        if ($raw_output) {
+            return $this->_status;
+        }
+        return self::$_statusToString[$this->_status];
+    }
+
+    /**
+     * Sends data on the connection.
+     *
+     * @param mixed $send_buffer
+     * @param bool  $raw
+     * @return bool|null
+     */
+    public function send($send_buffer, $raw = false)
+    {
+        if ($this->_status === self::STATUS_CLOSING || $this->_status === self::STATUS_CLOSED) {
+            return false;
+        }
+
+        // Try to call protocol::encode($send_buffer) before sending.
+        if (false === $raw && $this->protocol !== null) {
+            $parser      = $this->protocol;
+            $send_buffer = $parser::encode($send_buffer, $this);
+            if ($send_buffer === '') {
+                return;
+            }
+        }
+
+        if ($this->_status !== self::STATUS_ESTABLISHED ||
+            ($this->transport === 'ssl' && $this->_sslHandshakeCompleted !== true)
+        ) {
+            if ($this->_sendBuffer && $this->bufferIsFull()) {
+                ++self::$statistics['send_fail'];
+                return false;
+            }
+            $this->_sendBuffer .= $send_buffer;
+            $this->checkBufferWillFull();
+            return;
+        }
+
+        // Attempt to send data directly.
+        if ($this->_sendBuffer === '') {
+            if ($this->transport === 'ssl') {
+                Worker::$globalEvent->add($this->_socket, EventInterface::EV_WRITE, array($this, 'baseWrite'));
+                $this->_sendBuffer = $send_buffer;
+                $this->checkBufferWillFull();
+                return;
+            }
+            $len = 0;
+            try {
+                $len = @\fwrite($this->_socket, $send_buffer);
+            } catch (\Exception $e) {
+                Worker::log($e);
+            } catch (\Error $e) {
+                Worker::log($e);
+            }
+            // send successful.
+            if ($len === \strlen($send_buffer)) {
+                $this->bytesWritten += $len;
+                return true;
+            }
+            // Send only part of the data.
+            if ($len > 0) {
+                $this->_sendBuffer = \substr($send_buffer, $len);
+                $this->bytesWritten += $len;
+            } else {
+                // Connection closed?
+                if (!\is_resource($this->_socket) || \feof($this->_socket)) {
+                    ++self::$statistics['send_fail'];
+                    if ($this->onError) {
+                        try {
+                            \call_user_func($this->onError, $this, \WORKERMAN_SEND_FAIL, 'client closed');
+                        } catch (\Exception $e) {
+                            Worker::log($e);
+                            exit(250);
+                        } catch (\Error $e) {
+                            Worker::log($e);
+                            exit(250);
+                        }
+                    }
+                    $this->destroy();
+                    return false;
+                }
+                $this->_sendBuffer = $send_buffer;
+            }
+            Worker::$globalEvent->add($this->_socket, EventInterface::EV_WRITE, array($this, 'baseWrite'));
+            // Check if the send buffer will be full.
+            $this->checkBufferWillFull();
+            return;
+        }
+
+        if ($this->bufferIsFull()) {
+            ++self::$statistics['send_fail'];
+            return false;
+        }
+
+        $this->_sendBuffer .= $send_buffer;
+        // Check if the send buffer is full.
+        $this->checkBufferWillFull();
+    }
+
+    /**
+     * Get remote IP.
+     *
+     * @return string
+     */
+    public function getRemoteIp()
+    {
+        $pos = \strrpos($this->_remoteAddress, ':');
+        if ($pos) {
+            return (string) \substr($this->_remoteAddress, 0, $pos);
+        }
+        return '';
+    }
+
+    /**
+     * Get remote port.
+     *
+     * @return int
+     */
+    public function getRemotePort()
+    {
+        if ($this->_remoteAddress) {
+            return (int) \substr(\strrchr($this->_remoteAddress, ':'), 1);
+        }
+        return 0;
+    }
+
+    /**
+     * Get remote address.
+     *
+     * @return string
+     */
+    public function getRemoteAddress()
+    {
+        return $this->_remoteAddress;
+    }
+
+    /**
+     * Get local IP.
+     *
+     * @return string
+     */
+    public function getLocalIp()
+    {
+        $address = $this->getLocalAddress();
+        $pos = \strrpos($address, ':');
+        if (!$pos) {
+            return '';
+        }
+        return \substr($address, 0, $pos);
+    }
+
+    /**
+     * Get local port.
+     *
+     * @return int
+     */
+    public function getLocalPort()
+    {
+        $address = $this->getLocalAddress();
+        $pos = \strrpos($address, ':');
+        if (!$pos) {
+            return 0;
+        }
+        return (int)\substr(\strrchr($address, ':'), 1);
+    }
+
+    /**
+     * Get local address.
+     *
+     * @return string
+     */
+    public function getLocalAddress()
+    {
+        return (string)@\stream_socket_get_name($this->_socket, false);
+    }
+
+    /**
+     * Get send buffer queue size.
+     *
+     * @return integer
+     */
+    public function getSendBufferQueueSize()
+    {
+        return \strlen($this->_sendBuffer);
+    }
+
+    /**
+     * Get recv buffer queue size.
+     *
+     * @return integer
+     */
+    public function getRecvBufferQueueSize()
+    {
+        return \strlen($this->_recvBuffer);
+    }
+
+    /**
+     * Is ipv4.
+     *
+     * return bool.
+     */
+    public function isIpV4()
+    {
+        if ($this->transport === 'unix') {
+            return false;
+        }
+        return \strpos($this->getRemoteIp(), ':') === false;
+    }
+
+    /**
+     * Is ipv6.
+     *
+     * return bool.
+     */
+    public function isIpV6()
+    {
+        if ($this->transport === 'unix') {
+            return false;
+        }
+        return \strpos($this->getRemoteIp(), ':') !== false;
+    }
+
+    /**
+     * Pauses the reading of data. That is onMessage will not be emitted. Useful to throttle back an upload.
+     *
+     * @return void
+     */
+    public function pauseRecv()
+    {
+        Worker::$globalEvent->del($this->_socket, EventInterface::EV_READ);
+        $this->_isPaused = true;
+    }
+
+    /**
+     * Resumes reading after a call to pauseRecv.
+     *
+     * @return void
+     */
+    public function resumeRecv()
+    {
+        if ($this->_isPaused === true) {
+            Worker::$globalEvent->add($this->_socket, EventInterface::EV_READ, array($this, 'baseRead'));
+            $this->_isPaused = false;
+            $this->baseRead($this->_socket, false);
+        }
+    }
+
+
+
+    /**
+     * Base read handler.
+     *
+     * @param resource $socket
+     * @param bool $check_eof
+     * @return void
+     */
+    public function baseRead($socket, $check_eof = true)
+    {
+        // SSL handshake.
+        if ($this->transport === 'ssl' && $this->_sslHandshakeCompleted !== true) {
+            if ($this->doSslHandshake($socket)) {
+                $this->_sslHandshakeCompleted = true;
+                if ($this->_sendBuffer) {
+                    Worker::$globalEvent->add($socket, EventInterface::EV_WRITE, array($this, 'baseWrite'));
+                }
+            } else {
+                return;
+            }
+        }
+
+        $buffer = '';
+        try {
+            $buffer = @\fread($socket, self::READ_BUFFER_SIZE);
+        } catch (\Exception $e) {} catch (\Error $e) {}
+
+        // Check connection closed.
+        if ($buffer === '' || $buffer === false) {
+            if ($check_eof && (\feof($socket) || !\is_resource($socket) || $buffer === false)) {
+                $this->destroy();
+                return;
+            }
+        } else {
+            $this->bytesRead += \strlen($buffer);
+            $this->_recvBuffer .= $buffer;
+        }
+
+        // If the application layer protocol has been set up.
+        if ($this->protocol !== null) {
+            $parser = $this->protocol;
+            while ($this->_recvBuffer !== '' && !$this->_isPaused) {
+                // The current packet length is known.
+                if ($this->_currentPackageLength) {
+                    // Data is not enough for a package.
+                    if ($this->_currentPackageLength > \strlen($this->_recvBuffer)) {
+                        break;
+                    }
+                } else {
+                    // Get current package length.
+                    try {
+                        $this->_currentPackageLength = $parser::input($this->_recvBuffer, $this);
+                    } catch (\Exception $e) {} catch (\Error $e) {}
+                    // The packet length is unknown.
+                    if ($this->_currentPackageLength === 0) {
+                        break;
+                    } elseif ($this->_currentPackageLength > 0 && $this->_currentPackageLength <= $this->maxPackageSize) {
+                        // Data is not enough for a package.
+                        if ($this->_currentPackageLength > \strlen($this->_recvBuffer)) {
+                            break;
+                        }
+                    } // Wrong package.
+                    else {
+                        Worker::safeEcho('Error package. package_length=' . \var_export($this->_currentPackageLength, true));
+                        $this->destroy();
+                        return;
+                    }
+                }
+
+                // The data is enough for a packet.
+                ++self::$statistics['total_request'];
+                // The current packet length is equal to the length of the buffer.
+                if (\strlen($this->_recvBuffer) === $this->_currentPackageLength) {
+                    $one_request_buffer = $this->_recvBuffer;
+                    $this->_recvBuffer  = '';
+                } else {
+                    // Get a full package from the buffer.
+                    $one_request_buffer = \substr($this->_recvBuffer, 0, $this->_currentPackageLength);
+                    // Remove the current package from the receive buffer.
+                    $this->_recvBuffer = \substr($this->_recvBuffer, $this->_currentPackageLength);
+                }
+                // Reset the current packet length to 0.
+                $this->_currentPackageLength = 0;
+                if (!$this->onMessage) {
+                    continue;
+                }
+                try {
+                    // Decode request buffer before Emitting onMessage callback.
+                    \call_user_func($this->onMessage, $this, $parser::decode($one_request_buffer, $this));
+                } catch (\Exception $e) {
+                    Worker::log($e);
+                    exit(250);
+                } catch (\Error $e) {
+                    Worker::log($e);
+                    exit(250);
+                }
+            }
+            return;
+        }
+
+        if ($this->_recvBuffer === '' || $this->_isPaused) {
+            return;
+        }
+
+        // Applications protocol is not set.
+        ++self::$statistics['total_request'];
+        if (!$this->onMessage) {
+            $this->_recvBuffer = '';
+            return;
+        }
+        try {
+            \call_user_func($this->onMessage, $this, $this->_recvBuffer);
+        } catch (\Exception $e) {
+            Worker::log($e);
+            exit(250);
+        } catch (\Error $e) {
+            Worker::log($e);
+            exit(250);
+        }
+        // Clean receive buffer.
+        $this->_recvBuffer = '';
+    }
+
+    /**
+     * Base write handler.
+     *
+     * @return void|bool
+     */
+    public function baseWrite()
+    {
+        \set_error_handler(function(){});
+        if ($this->transport === 'ssl') {
+            $len = @\fwrite($this->_socket, $this->_sendBuffer, 8192);
+        } else {
+            $len = @\fwrite($this->_socket, $this->_sendBuffer);
+        }
+        \restore_error_handler();
+        if ($len === \strlen($this->_sendBuffer)) {
+            $this->bytesWritten += $len;
+            Worker::$globalEvent->del($this->_socket, EventInterface::EV_WRITE);
+            $this->_sendBuffer = '';
+            // Try to emit onBufferDrain callback when the send buffer becomes empty.
+            if ($this->onBufferDrain) {
+                try {
+                    \call_user_func($this->onBufferDrain, $this);
+                } catch (\Exception $e) {
+                    Worker::log($e);
+                    exit(250);
+                } catch (\Error $e) {
+                    Worker::log($e);
+                    exit(250);
+                }
+            }
+            if ($this->_status === self::STATUS_CLOSING) {
+                $this->destroy();
+            }
+            return true;
+        }
+        if ($len > 0) {
+            $this->bytesWritten += $len;
+            $this->_sendBuffer = \substr($this->_sendBuffer, $len);
+        } else {
+            ++self::$statistics['send_fail'];
+            $this->destroy();
+        }
+    }
+
+    /**
+     * SSL handshake.
+     *
+     * @param $socket
+     * @return bool
+     */
+    public function doSslHandshake($socket){
+        if (\feof($socket)) {
+            $this->destroy();
+            return false;
+        }
+        $async = $this instanceof AsyncTcpConnection;
+        
+        /**
+          *  We disabled ssl3 because https://blog.qualys.com/ssllabs/2014/10/15/ssl-3-is-dead-killed-by-the-poodle-attack.
+          *  You can enable ssl3 by the codes below.
+          */
+        /*if($async){
+            $type = STREAM_CRYPTO_METHOD_SSLv2_CLIENT | STREAM_CRYPTO_METHOD_SSLv23_CLIENT | STREAM_CRYPTO_METHOD_SSLv3_CLIENT;
+        }else{
+            $type = STREAM_CRYPTO_METHOD_SSLv2_SERVER | STREAM_CRYPTO_METHOD_SSLv23_SERVER | STREAM_CRYPTO_METHOD_SSLv3_SERVER;
+        }*/
+        
+        if($async){
+            $type = \STREAM_CRYPTO_METHOD_SSLv2_CLIENT | \STREAM_CRYPTO_METHOD_SSLv23_CLIENT;
+        }else{
+            $type = \STREAM_CRYPTO_METHOD_SSLv2_SERVER | \STREAM_CRYPTO_METHOD_SSLv23_SERVER;
+        }
+        
+        // Hidden error.
+        \set_error_handler(function($errno, $errstr, $file){
+            if (!Worker::$daemonize) {
+                Worker::safeEcho("SSL handshake error: $errstr \n");
+            }
+        });
+        $ret = \stream_socket_enable_crypto($socket, true, $type);
+        \restore_error_handler();
+        // Negotiation has failed.
+        if (false === $ret) {
+            $this->destroy();
+            return false;
+        } elseif (0 === $ret) {
+            // There isn't enough data and should try again.
+            return 0;
+        }
+        if (isset($this->onSslHandshake)) {
+            try {
+                \call_user_func($this->onSslHandshake, $this);
+            } catch (\Exception $e) {
+                Worker::log($e);
+                exit(250);
+            } catch (\Error $e) {
+                Worker::log($e);
+                exit(250);
+            }
+        }
+        return true;
+    }
+
+    /**
+     * This method pulls all the data out of a readable stream, and writes it to the supplied destination.
+     *
+     * @param self $dest
+     * @return void
+     */
+    public function pipe(self $dest)
+    {
+        $source              = $this;
+        $this->onMessage     = function ($source, $data) use ($dest) {
+            $dest->send($data);
+        };
+        $this->onClose       = function ($source) use ($dest) {
+            $dest->destroy();
+        };
+        $dest->onBufferFull  = function ($dest) use ($source) {
+            $source->pauseRecv();
+        };
+        $dest->onBufferDrain = function ($dest) use ($source) {
+            $source->resumeRecv();
+        };
+    }
+
+    /**
+     * Remove $length of data from receive buffer.
+     *
+     * @param int $length
+     * @return void
+     */
+    public function consumeRecvBuffer($length)
+    {
+        $this->_recvBuffer = \substr($this->_recvBuffer, $length);
+    }
+
+    /**
+     * Close connection.
+     *
+     * @param mixed $data
+     * @param bool $raw
+     * @return void
+     */
+    public function close($data = null, $raw = false)
+    {
+        if($this->_status === self::STATUS_CONNECTING){
+            $this->destroy();
+            return;
+        }
+
+        if ($this->_status === self::STATUS_CLOSING || $this->_status === self::STATUS_CLOSED) {
+            return;
+        }
+
+        if ($data !== null) {
+            $this->send($data, $raw);
+        }
+
+        $this->_status = self::STATUS_CLOSING;
+        
+        if ($this->_sendBuffer === '') {
+            $this->destroy();
+        } else {
+            $this->pauseRecv();
+        }
+    }
+
+    /**
+     * Get the real socket.
+     *
+     * @return resource
+     */
+    public function getSocket()
+    {
+        return $this->_socket;
+    }
+
+    /**
+     * Check whether the send buffer will be full.
+     *
+     * @return void
+     */
+    protected function checkBufferWillFull()
+    {
+        if ($this->maxSendBufferSize <= \strlen($this->_sendBuffer)) {
+            if ($this->onBufferFull) {
+                try {
+                    \call_user_func($this->onBufferFull, $this);
+                } catch (\Exception $e) {
+                    Worker::log($e);
+                    exit(250);
+                } catch (\Error $e) {
+                    Worker::log($e);
+                    exit(250);
+                }
+            }
+        }
+    }
+
+    /**
+     * Whether send buffer is full.
+     *
+     * @return bool
+     */
+    protected function bufferIsFull()
+    {
+        // Buffer has been marked as full but still has data to send then the packet is discarded.
+        if ($this->maxSendBufferSize <= \strlen($this->_sendBuffer)) {
+            if ($this->onError) {
+                try {
+                    \call_user_func($this->onError, $this, \WORKERMAN_SEND_FAIL, 'send buffer full and drop package');
+                } catch (\Exception $e) {
+                    Worker::log($e);
+                    exit(250);
+                } catch (\Error $e) {
+                    Worker::log($e);
+                    exit(250);
+                }
+            }
+            return true;
+        }
+        return false;
+    }
+    
+    /**
+     * Whether send buffer is Empty.
+     *
+     * @return bool
+     */
+    public function bufferIsEmpty()
+    {
+    	return empty($this->_sendBuffer);
+    }
+
+    /**
+     * Destroy connection.
+     *
+     * @return void
+     */
+    public function destroy()
+    {
+        // Avoid repeated calls.
+        if ($this->_status === self::STATUS_CLOSED) {
+            return;
+        }
+        // Remove event listener.
+        Worker::$globalEvent->del($this->_socket, EventInterface::EV_READ);
+        Worker::$globalEvent->del($this->_socket, EventInterface::EV_WRITE);
+
+        // Close socket.
+        try {
+            @\fclose($this->_socket);
+        } catch (\Exception $e) {} catch (\Error $e) {}
+
+        $this->_status = self::STATUS_CLOSED;
+        // Try to emit onClose callback.
+        if ($this->onClose) {
+            try {
+                \call_user_func($this->onClose, $this);
+            } catch (\Exception $e) {
+                Worker::log($e);
+                exit(250);
+            } catch (\Error $e) {
+                Worker::log($e);
+                exit(250);
+            }
+        }
+        // Try to emit protocol::onClose
+        if ($this->protocol && \method_exists($this->protocol, 'onClose')) {
+            try {
+                \call_user_func(array($this->protocol, 'onClose'), $this);
+            } catch (\Exception $e) {
+                Worker::log($e);
+                exit(250);
+            } catch (\Error $e) {
+                Worker::log($e);
+                exit(250);
+            }
+        }
+        $this->_sendBuffer = $this->_recvBuffer = '';
+        $this->_currentPackageLength = 0;
+        $this->_isPaused = $this->_sslHandshakeCompleted = false;
+        if ($this->_status === self::STATUS_CLOSED) {
+            // Cleaning up the callback to avoid memory leaks.
+            $this->onMessage = $this->onClose = $this->onError = $this->onBufferFull = $this->onBufferDrain = null;
+            // Remove from worker->connections.
+            if ($this->worker) {
+                unset($this->worker->connections[$this->_id]);
+            }
+            unset(static::$connections[$this->_id]);
+        }
+    }
+
+    /**
+     * Destruct.
+     *
+     * @return void
+     */
+    public function __destruct()
+    {
+        static $mod;
+        self::$statistics['connection_count']--;
+        if (Worker::getGracefulStop()) {
+            if (!isset($mod)) {
+                $mod = \ceil((self::$statistics['connection_count'] + 1) / 3);
+            }
+
+            if (0 === self::$statistics['connection_count'] % $mod) {
+                Worker::log('worker[' . \posix_getpid() . '] remains ' . self::$statistics['connection_count'] . ' connection(s)');
+            }
+
+            if(0 === self::$statistics['connection_count']) {
+                Worker::stopAll();
+            }
+        }
+    }
+}

+ 191 - 0
GatewayWorker/vendor/workerman/workerman/Connection/UdpConnection.php

@@ -0,0 +1,191 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link      http://www.workerman.net/
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Workerman\Connection;
+
+/**
+ * UdpConnection.
+ */
+class UdpConnection extends ConnectionInterface
+{
+    /**
+     * Application layer protocol.
+     * The format is like this Workerman\\Protocols\\Http.
+     *
+     * @var \Workerman\Protocols\ProtocolInterface
+     */
+    public $protocol = null;
+
+    /**
+     * Udp socket.
+     *
+     * @var resource
+     */
+    protected $_socket = null;
+
+    /**
+     * Remote address.
+     *
+     * @var string
+     */
+    protected $_remoteAddress = '';
+
+    /**
+     * Construct.
+     *
+     * @param resource $socket
+     * @param string   $remote_address
+     */
+    public function __construct($socket, $remote_address)
+    {
+        $this->_socket        = $socket;
+        $this->_remoteAddress = $remote_address;
+    }
+
+    /**
+     * Sends data on the connection.
+     *
+     * @param string $send_buffer
+     * @param bool   $raw
+     * @return void|boolean
+     */
+    public function send($send_buffer, $raw = false)
+    {
+        if (false === $raw && $this->protocol) {
+            $parser      = $this->protocol;
+            $send_buffer = $parser::encode($send_buffer, $this);
+            if ($send_buffer === '') {
+                return;
+            }
+        }
+        return \strlen($send_buffer) === \stream_socket_sendto($this->_socket, $send_buffer, 0, $this->_remoteAddress);
+    }
+
+    /**
+     * Get remote IP.
+     *
+     * @return string
+     */
+    public function getRemoteIp()
+    {
+        $pos = \strrpos($this->_remoteAddress, ':');
+        if ($pos) {
+            return \trim(\substr($this->_remoteAddress, 0, $pos), '[]');
+        }
+        return '';
+    }
+
+    /**
+     * Get remote port.
+     *
+     * @return int
+     */
+    public function getRemotePort()
+    {
+        if ($this->_remoteAddress) {
+            return (int)\substr(\strrchr($this->_remoteAddress, ':'), 1);
+        }
+        return 0;
+    }
+
+    /**
+     * Get remote address.
+     *
+     * @return string
+     */
+    public function getRemoteAddress()
+    {
+        return $this->_remoteAddress;
+    }
+
+    /**
+     * Get local IP.
+     *
+     * @return string
+     */
+    public function getLocalIp()
+    {
+        $address = $this->getLocalAddress();
+        $pos = \strrpos($address, ':');
+        if (!$pos) {
+            return '';
+        }
+        return \substr($address, 0, $pos);
+    }
+
+    /**
+     * Get local port.
+     *
+     * @return int
+     */
+    public function getLocalPort()
+    {
+        $address = $this->getLocalAddress();
+        $pos = \strrpos($address, ':');
+        if (!$pos) {
+            return 0;
+        }
+        return (int)\substr(\strrchr($address, ':'), 1);
+    }
+
+    /**
+     * Get local address.
+     *
+     * @return string
+     */
+    public function getLocalAddress()
+    {
+        return (string)@\stream_socket_get_name($this->_socket, false);
+    }
+
+    /**
+     * Is ipv4.
+     *
+     * @return bool.
+     */
+    public function isIpV4()
+    {
+        if ($this->transport === 'unix') {
+            return false;
+        }
+        return \strpos($this->getRemoteIp(), ':') === false;
+    }
+
+    /**
+     * Is ipv6.
+     *
+     * @return bool.
+     */
+    public function isIpV6()
+    {
+        if ($this->transport === 'unix') {
+            return false;
+        }
+        return \strpos($this->getRemoteIp(), ':') !== false;
+    }
+
+    /**
+     * Close connection.
+     *
+     * @param mixed $data
+     * @param bool  $raw
+     * @return bool
+     */
+    public function close($data = null, $raw = false)
+    {
+        if ($data !== null) {
+            $this->send($data, $raw);
+        }
+        return true;
+    }
+}

+ 195 - 0
GatewayWorker/vendor/workerman/workerman/Events/Ev.php

@@ -0,0 +1,195 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author  有个鬼<42765633@qq.com>
+ * @link    http://www.workerman.net/
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Workerman\Events;
+
+use Workerman\Worker;
+use \EvWatcher;
+
+/**
+ * ev eventloop
+ */
+class Ev implements EventInterface
+{
+    /**
+     * All listeners for read/write event.
+     *
+     * @var array
+     */
+    protected $_allEvents = array();
+
+    /**
+     * Event listeners of signal.
+     *
+     * @var array
+     */
+    protected $_eventSignal = array();
+
+    /**
+     * All timer event listeners.
+     * [func, args, event, flag, time_interval]
+     *
+     * @var array
+     */
+    protected $_eventTimer = array();
+
+    /**
+     * Timer id.
+     *
+     * @var int
+     */
+    protected static $_timerId = 1;
+
+    /**
+     * Add a timer.
+     * {@inheritdoc}
+     */
+    public function add($fd, $flag, $func, $args = null)
+    {
+        $callback = function ($event, $socket) use ($fd, $func) {
+            try {
+                \call_user_func($func, $fd);
+            } catch (\Exception $e) {
+                Worker::log($e);
+                exit(250);
+            } catch (\Error $e) {
+                Worker::log($e);
+                exit(250);
+            }
+        };
+        switch ($flag) {
+            case self::EV_SIGNAL:
+                $event                   = new \EvSignal($fd, $callback);
+                $this->_eventSignal[$fd] = $event;
+                return true;
+            case self::EV_TIMER:
+            case self::EV_TIMER_ONCE:
+                $repeat                             = $flag === self::EV_TIMER_ONCE ? 0 : $fd;
+                $param                              = array($func, (array)$args, $flag, $fd, self::$_timerId);
+                $event                              = new \EvTimer($fd, $repeat, array($this, 'timerCallback'), $param);
+                $this->_eventTimer[self::$_timerId] = $event;
+                return self::$_timerId++;
+            default :
+                $fd_key                           = (int)$fd;
+                $real_flag                        = $flag === self::EV_READ ? \Ev::READ : \Ev::WRITE;
+                $event                            = new \EvIo($fd, $real_flag, $callback);
+                $this->_allEvents[$fd_key][$flag] = $event;
+                return true;
+        }
+
+    }
+
+    /**
+     * Remove a timer.
+     * {@inheritdoc}
+     */
+    public function del($fd, $flag)
+    {
+        switch ($flag) {
+            case self::EV_READ:
+            case self::EV_WRITE:
+                $fd_key = (int)$fd;
+                if (isset($this->_allEvents[$fd_key][$flag])) {
+                    $this->_allEvents[$fd_key][$flag]->stop();
+                    unset($this->_allEvents[$fd_key][$flag]);
+                }
+                if (empty($this->_allEvents[$fd_key])) {
+                    unset($this->_allEvents[$fd_key]);
+                }
+                break;
+            case  self::EV_SIGNAL:
+                $fd_key = (int)$fd;
+                if (isset($this->_eventSignal[$fd_key])) {
+                    $this->_eventSignal[$fd_key]->stop();
+                    unset($this->_eventSignal[$fd_key]);
+                }
+                break;
+            case self::EV_TIMER:
+            case self::EV_TIMER_ONCE:
+                if (isset($this->_eventTimer[$fd])) {
+                    $this->_eventTimer[$fd]->stop();
+                    unset($this->_eventTimer[$fd]);
+                }
+                break;
+        }
+        return true;
+    }
+
+    /**
+     * Timer callback.
+     *
+     * @param EvWatcher $event
+     */
+    public function timerCallback(EvWatcher $event)
+    {
+        $param    = $event->data;
+        $timer_id = $param[4];
+        if ($param[2] === self::EV_TIMER_ONCE) {
+            $this->_eventTimer[$timer_id]->stop();
+            unset($this->_eventTimer[$timer_id]);
+        }
+        try {
+            \call_user_func_array($param[0], $param[1]);
+        } catch (\Exception $e) {
+            Worker::log($e);
+            exit(250);
+        } catch (\Error $e) {
+            Worker::log($e);
+            exit(250);
+        }
+    }
+
+    /**
+     * Remove all timers.
+     *
+     * @return void
+     */
+    public function clearAllTimer()
+    {
+        foreach ($this->_eventTimer as $event) {
+            $event->stop();
+        }
+        $this->_eventTimer = array();
+    }
+
+    /**
+     * Main loop.
+     *
+     * @see EventInterface::loop()
+     */
+    public function loop()
+    {
+        \Ev::run();
+    }
+
+    /**
+     * Destroy loop.
+     *
+     * @return void
+     */
+    public function destroy()
+    {
+        foreach ($this->_allEvents as $event) {
+            $event->stop();
+        }
+    }
+
+    /**
+     * Get timer count.
+     *
+     * @return integer
+     */
+    public function getTimerCount()
+    {
+        return \count($this->_eventTimer);
+    }
+}

+ 219 - 0
GatewayWorker/vendor/workerman/workerman/Events/Event.php

@@ -0,0 +1,219 @@
+<?php 
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    有个鬼<42765633@qq.com>
+ * @copyright 有个鬼<42765633@qq.com>
+ * @link      http://www.workerman.net/
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Workerman\Events;
+
+use Workerman\Worker;
+
+/**
+ * libevent eventloop
+ */
+class Event implements EventInterface
+{
+    /**
+     * Event base.
+     * @var object
+     */
+    protected $_eventBase = null;
+    
+    /**
+     * All listeners for read/write event.
+     * @var array
+     */
+    protected $_allEvents = array();
+    
+    /**
+     * Event listeners of signal.
+     * @var array
+     */
+    protected $_eventSignal = array();
+    
+    /**
+     * All timer event listeners.
+     * [func, args, event, flag, time_interval]
+     * @var array
+     */
+    protected $_eventTimer = array();
+
+    /**
+     * Timer id.
+     * @var int
+     */
+    protected static $_timerId = 1;
+    
+    /**
+     * construct
+     * @return void
+     */
+    public function __construct()
+    {
+        if (\class_exists('\\\\EventBase', false)) {
+            $class_name = '\\\\EventBase';
+        } else {
+            $class_name = '\EventBase';
+        }
+        $this->_eventBase = new $class_name();
+    }
+   
+    /**
+     * @see EventInterface::add()
+     */
+    public function add($fd, $flag, $func, $args=array())
+    {
+        if (\class_exists('\\\\Event', false)) {
+            $class_name = '\\\\Event';
+        } else {
+            $class_name = '\Event';
+        }
+        switch ($flag) {
+            case self::EV_SIGNAL:
+
+                $fd_key = (int)$fd;
+                $event = $class_name::signal($this->_eventBase, $fd, $func);
+                if (!$event||!$event->add()) {
+                    return false;
+                }
+                $this->_eventSignal[$fd_key] = $event;
+                return true;
+
+            case self::EV_TIMER:
+            case self::EV_TIMER_ONCE:
+
+                $param = array($func, (array)$args, $flag, $fd, self::$_timerId);
+                $event = new $class_name($this->_eventBase, -1, $class_name::TIMEOUT|$class_name::PERSIST, array($this, "timerCallback"), $param);
+                if (!$event||!$event->addTimer($fd)) {
+                    return false;
+                }
+                $this->_eventTimer[self::$_timerId] = $event;
+                return self::$_timerId++;
+                
+            default :
+                $fd_key = (int)$fd;
+                $real_flag = $flag === self::EV_READ ? $class_name::READ | $class_name::PERSIST : $class_name::WRITE | $class_name::PERSIST;
+                $event = new $class_name($this->_eventBase, $fd, $real_flag, $func, $fd);
+                if (!$event||!$event->add()) {
+                    return false;
+                }
+                $this->_allEvents[$fd_key][$flag] = $event;
+                return true;
+        }
+    }
+    
+    /**
+     * @see Events\EventInterface::del()
+     */
+    public function del($fd, $flag)
+    {
+        switch ($flag) {
+
+            case self::EV_READ:
+            case self::EV_WRITE:
+
+                $fd_key = (int)$fd;
+                if (isset($this->_allEvents[$fd_key][$flag])) {
+                    $this->_allEvents[$fd_key][$flag]->del();
+                    unset($this->_allEvents[$fd_key][$flag]);
+                }
+                if (empty($this->_allEvents[$fd_key])) {
+                    unset($this->_allEvents[$fd_key]);
+                }
+                break;
+
+            case  self::EV_SIGNAL:
+                $fd_key = (int)$fd;
+                if (isset($this->_eventSignal[$fd_key])) {
+                    $this->_eventSignal[$fd_key]->del();
+                    unset($this->_eventSignal[$fd_key]);
+                }
+                break;
+
+            case self::EV_TIMER:
+            case self::EV_TIMER_ONCE:
+                if (isset($this->_eventTimer[$fd])) {
+                    $this->_eventTimer[$fd]->del();
+                    unset($this->_eventTimer[$fd]);
+                }
+                break;
+        }
+        return true;
+    }
+    
+    /**
+     * Timer callback.
+     * @param null $fd
+     * @param int $what
+     * @param int $timer_id
+     */
+    public function timerCallback($fd, $what, $param)
+    {
+        $timer_id = $param[4];
+        
+        if ($param[2] === self::EV_TIMER_ONCE) {
+            $this->_eventTimer[$timer_id]->del();
+            unset($this->_eventTimer[$timer_id]);
+        }
+
+        try {
+            \call_user_func_array($param[0], $param[1]);
+        } catch (\Exception $e) {
+            Worker::log($e);
+            exit(250);
+        } catch (\Error $e) {
+            Worker::log($e);
+            exit(250);
+        }
+    }
+    
+    /**
+     * @see Events\EventInterface::clearAllTimer() 
+     * @return void
+     */
+    public function clearAllTimer()
+    {
+        foreach ($this->_eventTimer as $event) {
+            $event->del();
+        }
+        $this->_eventTimer = array();
+    }
+     
+
+    /**
+     * @see EventInterface::loop()
+     */
+    public function loop()
+    {
+        $this->_eventBase->loop();
+    }
+
+    /**
+     * Destroy loop.
+     *
+     * @return void
+     */
+    public function destroy()
+    {
+        foreach ($this->_eventSignal as $event) {
+            $event->del();
+        }
+    }
+
+    /**
+     * Get timer count.
+     *
+     * @return integer
+     */
+    public function getTimerCount()
+    {
+        return \count($this->_eventTimer);
+    }
+}

+ 107 - 0
GatewayWorker/vendor/workerman/workerman/Events/EventInterface.php

@@ -0,0 +1,107 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link      http://www.workerman.net/
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Workerman\Events;
+
+interface EventInterface
+{
+    /**
+     * Read event.
+     *
+     * @var int
+     */
+    const EV_READ = 1;
+
+    /**
+     * Write event.
+     *
+     * @var int
+     */
+    const EV_WRITE = 2;
+
+    /**
+     * Except event
+     *
+     * @var int
+     */
+    const EV_EXCEPT = 3;
+
+    /**
+     * Signal event.
+     *
+     * @var int
+     */
+    const EV_SIGNAL = 4;
+
+    /**
+     * Timer event.
+     *
+     * @var int
+     */
+    const EV_TIMER = 8;
+
+    /**
+     * Timer once event.
+     *
+     * @var int
+     */
+    const EV_TIMER_ONCE = 16;
+
+    /**
+     * Add event listener to event loop.
+     *
+     * @param mixed    $fd
+     * @param int      $flag
+     * @param callable $func
+     * @param mixed    $args
+     * @return bool
+     */
+    public function add($fd, $flag, $func, $args = null);
+
+    /**
+     * Remove event listener from event loop.
+     *
+     * @param mixed $fd
+     * @param int   $flag
+     * @return bool
+     */
+    public function del($fd, $flag);
+
+    /**
+     * Remove all timers.
+     *
+     * @return void
+     */
+    public function clearAllTimer();
+
+    /**
+     * Main loop.
+     *
+     * @return void
+     */
+    public function loop();
+
+    /**
+     * Destroy loop.
+     *
+     * @return mixed
+     */
+    public function destroy();
+
+    /**
+     * Get Timer count.
+     *
+     * @return mixed
+     */
+    public function getTimerCount();
+}

+ 227 - 0
GatewayWorker/vendor/workerman/workerman/Events/Libevent.php

@@ -0,0 +1,227 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link      http://www.workerman.net/
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Workerman\Events;
+
+use Workerman\Worker;
+
+/**
+ * libevent eventloop
+ */
+class Libevent implements EventInterface
+{
+    /**
+     * Event base.
+     *
+     * @var resource
+     */
+    protected $_eventBase = null;
+
+    /**
+     * All listeners for read/write event.
+     *
+     * @var array
+     */
+    protected $_allEvents = array();
+
+    /**
+     * Event listeners of signal.
+     *
+     * @var array
+     */
+    protected $_eventSignal = array();
+
+    /**
+     * All timer event listeners.
+     * [func, args, event, flag, time_interval]
+     *
+     * @var array
+     */
+    protected $_eventTimer = array();
+
+    /**
+     * construct
+     */
+    public function __construct()
+    {
+        $this->_eventBase = \event_base_new();
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function add($fd, $flag, $func, $args = array())
+    {
+        switch ($flag) {
+            case self::EV_SIGNAL:
+                $fd_key                      = (int)$fd;
+                $real_flag                   = \EV_SIGNAL | \EV_PERSIST;
+                $this->_eventSignal[$fd_key] = \event_new();
+                if (!\event_set($this->_eventSignal[$fd_key], $fd, $real_flag, $func, null)) {
+                    return false;
+                }
+                if (!\event_base_set($this->_eventSignal[$fd_key], $this->_eventBase)) {
+                    return false;
+                }
+                if (!\event_add($this->_eventSignal[$fd_key])) {
+                    return false;
+                }
+                return true;
+            case self::EV_TIMER:
+            case self::EV_TIMER_ONCE:
+                $event    = \event_new();
+                $timer_id = (int)$event;
+                if (!\event_set($event, 0, \EV_TIMEOUT, array($this, 'timerCallback'), $timer_id)) {
+                    return false;
+                }
+
+                if (!\event_base_set($event, $this->_eventBase)) {
+                    return false;
+                }
+
+                $time_interval = $fd * 1000000;
+                if (!\event_add($event, $time_interval)) {
+                    return false;
+                }
+                $this->_eventTimer[$timer_id] = array($func, (array)$args, $event, $flag, $time_interval);
+                return $timer_id;
+
+            default :
+                $fd_key    = (int)$fd;
+                $real_flag = $flag === self::EV_READ ? \EV_READ | \EV_PERSIST : \EV_WRITE | \EV_PERSIST;
+
+                $event = \event_new();
+
+                if (!\event_set($event, $fd, $real_flag, $func, null)) {
+                    return false;
+                }
+
+                if (!\event_base_set($event, $this->_eventBase)) {
+                    return false;
+                }
+
+                if (!\event_add($event)) {
+                    return false;
+                }
+
+                $this->_allEvents[$fd_key][$flag] = $event;
+
+                return true;
+        }
+
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function del($fd, $flag)
+    {
+        switch ($flag) {
+            case self::EV_READ:
+            case self::EV_WRITE:
+                $fd_key = (int)$fd;
+                if (isset($this->_allEvents[$fd_key][$flag])) {
+                    \event_del($this->_allEvents[$fd_key][$flag]);
+                    unset($this->_allEvents[$fd_key][$flag]);
+                }
+                if (empty($this->_allEvents[$fd_key])) {
+                    unset($this->_allEvents[$fd_key]);
+                }
+                break;
+            case  self::EV_SIGNAL:
+                $fd_key = (int)$fd;
+                if (isset($this->_eventSignal[$fd_key])) {
+                    \event_del($this->_eventSignal[$fd_key]);
+                    unset($this->_eventSignal[$fd_key]);
+                }
+                break;
+            case self::EV_TIMER:
+            case self::EV_TIMER_ONCE:
+                // 这里 fd 为timerid 
+                if (isset($this->_eventTimer[$fd])) {
+                    \event_del($this->_eventTimer[$fd][2]);
+                    unset($this->_eventTimer[$fd]);
+                }
+                break;
+        }
+        return true;
+    }
+
+    /**
+     * Timer callback.
+     *
+     * @param mixed $_null1
+     * @param int   $_null2
+     * @param mixed $timer_id
+     */
+    protected function timerCallback($_null1, $_null2, $timer_id)
+    {
+        if ($this->_eventTimer[$timer_id][3] === self::EV_TIMER) {
+            \event_add($this->_eventTimer[$timer_id][2], $this->_eventTimer[$timer_id][4]);
+        }
+        try {
+            \call_user_func_array($this->_eventTimer[$timer_id][0], $this->_eventTimer[$timer_id][1]);
+        } catch (\Exception $e) {
+            Worker::log($e);
+            exit(250);
+        } catch (\Error $e) {
+            Worker::log($e);
+            exit(250);
+        }
+        if (isset($this->_eventTimer[$timer_id]) && $this->_eventTimer[$timer_id][3] === self::EV_TIMER_ONCE) {
+            $this->del($timer_id, self::EV_TIMER_ONCE);
+        }
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function clearAllTimer()
+    {
+        foreach ($this->_eventTimer as $task_data) {
+            \event_del($task_data[2]);
+        }
+        $this->_eventTimer = array();
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function loop()
+    {
+        \event_base_loop($this->_eventBase);
+    }
+
+    /**
+     * Destroy loop.
+     *
+     * @return void
+     */
+    public function destroy()
+    {
+        foreach ($this->_eventSignal as $event) {
+            \event_del($event);
+        }
+    }
+
+    /**
+     * Get timer count.
+     *
+     * @return integer
+     */
+    public function getTimerCount()
+    {
+        return \count($this->_eventTimer);
+    }
+}
+

+ 264 - 0
GatewayWorker/vendor/workerman/workerman/Events/React/Base.php

@@ -0,0 +1,264 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link      http://www.workerman.net/
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Workerman\Events\React;
+
+use Workerman\Events\EventInterface;
+use React\EventLoop\TimerInterface;
+use React\EventLoop\LoopInterface;
+
+/**
+ * Class StreamSelectLoop
+ * @package Workerman\Events\React
+ */
+class Base implements LoopInterface
+{
+    /**
+     * @var array
+     */
+    protected $_timerIdMap = array();
+
+    /**
+     * @var int
+     */
+    protected $_timerIdIndex = 0;
+
+    /**
+     * @var array
+     */
+    protected $_signalHandlerMap = array();
+
+    /**
+     * @var LoopInterface
+     */
+    protected $_eventLoop = null;
+
+    /**
+     * Base constructor.
+     */
+    public function __construct()
+    {
+        $this->_eventLoop = new \React\EventLoop\StreamSelectLoop();
+    }
+
+    /**
+     * Add event listener to event loop.
+     *
+     * @param $fd
+     * @param $flag
+     * @param $func
+     * @param array $args
+     * @return bool
+     */
+    public function add($fd, $flag, $func, array $args = array())
+    {
+        $args = (array)$args;
+        switch ($flag) {
+            case EventInterface::EV_READ:
+                return $this->addReadStream($fd, $func);
+            case EventInterface::EV_WRITE:
+                return $this->addWriteStream($fd, $func);
+            case EventInterface::EV_SIGNAL:
+                if (isset($this->_signalHandlerMap[$fd])) {
+                    $this->removeSignal($fd, $this->_signalHandlerMap[$fd]);
+                }
+                $this->_signalHandlerMap[$fd] = $func;
+                return $this->addSignal($fd, $func);
+            case EventInterface::EV_TIMER:
+                $timer_obj = $this->addPeriodicTimer($fd, function() use ($func, $args) {
+                    \call_user_func_array($func, $args);
+                });
+                $this->_timerIdMap[++$this->_timerIdIndex] = $timer_obj;
+                return $this->_timerIdIndex;
+            case EventInterface::EV_TIMER_ONCE:
+                $index = ++$this->_timerIdIndex;
+                $timer_obj = $this->addTimer($fd, function() use ($func, $args, $index) {
+                    $this->del($index,EventInterface::EV_TIMER_ONCE);
+                    \call_user_func_array($func, $args);
+                });
+                $this->_timerIdMap[$index] = $timer_obj;
+                return $this->_timerIdIndex;
+        }
+        return false;
+    }
+
+    /**
+     * Remove event listener from event loop.
+     *
+     * @param mixed $fd
+     * @param int   $flag
+     * @return bool
+     */
+    public function del($fd, $flag)
+    {
+        switch ($flag) {
+            case EventInterface::EV_READ:
+                return $this->removeReadStream($fd);
+            case EventInterface::EV_WRITE:
+                return $this->removeWriteStream($fd);
+            case EventInterface::EV_SIGNAL:
+                if (!isset($this->_eventLoop[$fd])) {
+                    return false;
+                }
+                $func = $this->_eventLoop[$fd];
+                unset($this->_eventLoop[$fd]);
+                return $this->removeSignal($fd, $func);
+
+            case EventInterface::EV_TIMER:
+            case EventInterface::EV_TIMER_ONCE:
+                if (isset($this->_timerIdMap[$fd])){
+                    $timer_obj = $this->_timerIdMap[$fd];
+                    unset($this->_timerIdMap[$fd]);
+                    $this->cancelTimer($timer_obj);
+                    return true;
+                }
+        }
+        return false;
+    }
+
+
+    /**
+     * Main loop.
+     *
+     * @return void
+     */
+    public function loop()
+    {
+        $this->run();
+    }
+
+
+    /**
+     * Destroy loop.
+     *
+     * @return void
+     */
+    public function destroy()
+    {
+
+    }
+
+    /**
+     * Get timer count.
+     *
+     * @return integer
+     */
+    public function getTimerCount()
+    {
+        return \count($this->_timerIdMap);
+    }
+
+    /**
+     * @param resource $stream
+     * @param callable $listener
+     */
+    public function addReadStream($stream, $listener)
+    {
+        return $this->_eventLoop->addReadStream($stream, $listener);
+    }
+
+    /**
+     * @param resource $stream
+     * @param callable $listener
+     */
+    public function addWriteStream($stream, $listener)
+    {
+        return $this->_eventLoop->addWriteStream($stream, $listener);
+    }
+
+    /**
+     * @param resource $stream
+     */
+    public function removeReadStream($stream)
+    {
+        return $this->_eventLoop->removeReadStream($stream);
+    }
+
+    /**
+     * @param resource $stream
+     */
+    public function removeWriteStream($stream)
+    {
+        return $this->_eventLoop->removeWriteStream($stream);
+    }
+
+    /**
+     * @param float|int $interval
+     * @param callable $callback
+     * @return \React\EventLoop\Timer\Timer|TimerInterface
+     */
+    public function addTimer($interval, $callback)
+    {
+        return $this->_eventLoop->addTimer($interval, $callback);
+    }
+
+    /**
+     * @param float|int $interval
+     * @param callable $callback
+     * @return \React\EventLoop\Timer\Timer|TimerInterface
+     */
+    public function addPeriodicTimer($interval, $callback)
+    {
+        return $this->_eventLoop->addPeriodicTimer($interval, $callback);
+    }
+
+    /**
+     * @param TimerInterface $timer
+     */
+    public function cancelTimer(TimerInterface $timer)
+    {
+        return $this->_eventLoop->cancelTimer($timer);
+    }
+
+    /**
+     * @param callable $listener
+     */
+    public function futureTick($listener)
+    {
+        return $this->_eventLoop->futureTick($listener);
+    }
+
+    /**
+     * @param int $signal
+     * @param callable $listener
+     */
+    public function addSignal($signal, $listener)
+    {
+        return $this->_eventLoop->addSignal($signal, $listener);
+    }
+
+    /**
+     * @param int $signal
+     * @param callable $listener
+     */
+    public function removeSignal($signal, $listener)
+    {
+        return $this->_eventLoop->removeSignal($signal, $listener);
+    }
+
+    /**
+     * Run.
+     */
+    public function run()
+    {
+        return $this->_eventLoop->run();
+    }
+
+    /**
+     * Stop.
+     */
+    public function stop()
+    {
+        return $this->_eventLoop->stop();
+    }
+}

+ 27 - 0
GatewayWorker/vendor/workerman/workerman/Events/React/ExtEventLoop.php

@@ -0,0 +1,27 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link      http://www.workerman.net/
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Workerman\Events\React;
+
+/**
+ * Class ExtEventLoop
+ * @package Workerman\Events\React
+ */
+class ExtEventLoop extends Base
+{
+
+    public function __construct()
+    {
+        $this->_eventLoop = new \React\EventLoop\ExtEventLoop();
+    }
+}

+ 27 - 0
GatewayWorker/vendor/workerman/workerman/Events/React/ExtLibEventLoop.php

@@ -0,0 +1,27 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link      http://www.workerman.net/
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Workerman\Events\React;
+use Workerman\Events\EventInterface;
+
+/**
+ * Class ExtLibEventLoop
+ * @package Workerman\Events\React
+ */
+class ExtLibEventLoop extends Base
+{
+    public function __construct()
+    {
+        $this->_eventLoop = new \React\EventLoop\ExtLibeventLoop();
+    }
+}

+ 26 - 0
GatewayWorker/vendor/workerman/workerman/Events/React/StreamSelectLoop.php

@@ -0,0 +1,26 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link      http://www.workerman.net/
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Workerman\Events\React;
+
+/**
+ * Class StreamSelectLoop
+ * @package Workerman\Events\React
+ */
+class StreamSelectLoop extends Base
+{
+    public function __construct()
+    {
+        $this->_eventLoop = new \React\EventLoop\StreamSelectLoop();
+    }
+}

+ 339 - 0
GatewayWorker/vendor/workerman/workerman/Events/Select.php

@@ -0,0 +1,339 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link      http://www.workerman.net/
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Workerman\Events;
+
+/**
+ * select eventloop
+ */
+class Select implements EventInterface
+{
+    /**
+     * All listeners for read/write event.
+     *
+     * @var array
+     */
+    public $_allEvents = array();
+
+    /**
+     * Event listeners of signal.
+     *
+     * @var array
+     */
+    public $_signalEvents = array();
+
+    /**
+     * Fds waiting for read event.
+     *
+     * @var array
+     */
+    protected $_readFds = array();
+
+    /**
+     * Fds waiting for write event.
+     *
+     * @var array
+     */
+    protected $_writeFds = array();
+
+    /**
+     * Fds waiting for except event.
+     *
+     * @var array
+     */
+    protected $_exceptFds = array();
+
+    /**
+     * Timer scheduler.
+     * {['data':timer_id, 'priority':run_timestamp], ..}
+     *
+     * @var \SplPriorityQueue
+     */
+    protected $_scheduler = null;
+
+    /**
+     * All timer event listeners.
+     * [[func, args, flag, timer_interval], ..]
+     *
+     * @var array
+     */
+    protected $_eventTimer = array();
+
+    /**
+     * Timer id.
+     *
+     * @var int
+     */
+    protected $_timerId = 1;
+
+    /**
+     * Select timeout.
+     *
+     * @var int
+     */
+    protected $_selectTimeout = 100000000;
+
+    /**
+     * Paired socket channels
+     *
+     * @var array
+     */
+    protected $channel = array();
+
+    /**
+     * Construct.
+     */
+    public function __construct()
+    {
+        // Init SplPriorityQueue.
+        $this->_scheduler = new \SplPriorityQueue();
+        $this->_scheduler->setExtractFlags(\SplPriorityQueue::EXTR_BOTH);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function add($fd, $flag, $func, $args = array())
+    {
+        switch ($flag) {
+            case self::EV_READ:
+            case self::EV_WRITE:
+                $count = $flag === self::EV_READ ? \count($this->_readFds) : \count($this->_writeFds);
+                if ($count >= 1024) {
+                    echo "Warning: system call select exceeded the maximum number of connections 1024, please install event/libevent extension for more connections.\n";
+                } else if (\DIRECTORY_SEPARATOR !== '/' && $count >= 256) {
+                    echo "Warning: system call select exceeded the maximum number of connections 256.\n";
+                }
+                $fd_key                           = (int)$fd;
+                $this->_allEvents[$fd_key][$flag] = array($func, $fd);
+                if ($flag === self::EV_READ) {
+                    $this->_readFds[$fd_key] = $fd;
+                } else {
+                    $this->_writeFds[$fd_key] = $fd;
+                }
+                break;
+            case self::EV_EXCEPT:
+                $fd_key = (int)$fd;
+                $this->_allEvents[$fd_key][$flag] = array($func, $fd);
+                $this->_exceptFds[$fd_key] = $fd;
+                break;
+            case self::EV_SIGNAL:
+                // Windows not support signal.
+                if(\DIRECTORY_SEPARATOR !== '/') {
+                    return false;
+                }
+                $fd_key                              = (int)$fd;
+                $this->_signalEvents[$fd_key][$flag] = array($func, $fd);
+                \pcntl_signal($fd, array($this, 'signalHandler'));
+                break;
+            case self::EV_TIMER:
+            case self::EV_TIMER_ONCE:
+                $timer_id = $this->_timerId++;
+                $run_time = \microtime(true) + $fd;
+                $this->_scheduler->insert($timer_id, -$run_time);
+                $this->_eventTimer[$timer_id] = array($func, (array)$args, $flag, $fd);
+                $select_timeout = ($run_time - \microtime(true)) * 1000000;
+                if( $this->_selectTimeout > $select_timeout ){ 
+                    $this->_selectTimeout = $select_timeout;   
+                }  
+                return $timer_id;
+        }
+
+        return true;
+    }
+
+    /**
+     * Signal handler.
+     *
+     * @param int $signal
+     */
+    public function signalHandler($signal)
+    {
+        \call_user_func_array($this->_signalEvents[$signal][self::EV_SIGNAL][0], array($signal));
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function del($fd, $flag)
+    {
+        $fd_key = (int)$fd;
+        switch ($flag) {
+            case self::EV_READ:
+                unset($this->_allEvents[$fd_key][$flag], $this->_readFds[$fd_key]);
+                if (empty($this->_allEvents[$fd_key])) {
+                    unset($this->_allEvents[$fd_key]);
+                }
+                return true;
+            case self::EV_WRITE:
+                unset($this->_allEvents[$fd_key][$flag], $this->_writeFds[$fd_key]);
+                if (empty($this->_allEvents[$fd_key])) {
+                    unset($this->_allEvents[$fd_key]);
+                }
+                return true;
+            case self::EV_EXCEPT:
+                unset($this->_allEvents[$fd_key][$flag], $this->_exceptFds[$fd_key]);
+                if(empty($this->_allEvents[$fd_key]))
+                {
+                    unset($this->_allEvents[$fd_key]);
+                }
+                return true;
+            case self::EV_SIGNAL:
+                if(\DIRECTORY_SEPARATOR !== '/') {
+                    return false;
+                }
+                unset($this->_signalEvents[$fd_key]);
+                \pcntl_signal($fd, SIG_IGN);
+                break;
+            case self::EV_TIMER:
+            case self::EV_TIMER_ONCE;
+                unset($this->_eventTimer[$fd_key]);
+                return true;
+        }
+        return false;
+    }
+
+    /**
+     * Tick for timer.
+     *
+     * @return void
+     */
+    protected function tick()
+    {
+        while (!$this->_scheduler->isEmpty()) {
+            $scheduler_data       = $this->_scheduler->top();
+            $timer_id             = $scheduler_data['data'];
+            $next_run_time        = -$scheduler_data['priority'];
+            $time_now             = \microtime(true);
+            $this->_selectTimeout = ($next_run_time - $time_now) * 1000000;
+            if ($this->_selectTimeout <= 0) {
+                $this->_scheduler->extract();
+
+                if (!isset($this->_eventTimer[$timer_id])) {
+                    continue;
+                }
+
+                // [func, args, flag, timer_interval]
+                $task_data = $this->_eventTimer[$timer_id];
+                if ($task_data[2] === self::EV_TIMER) {
+                    $next_run_time = $time_now + $task_data[3];
+                    $this->_scheduler->insert($timer_id, -$next_run_time);
+                }
+                \call_user_func_array($task_data[0], $task_data[1]);
+                if (isset($this->_eventTimer[$timer_id]) && $task_data[2] === self::EV_TIMER_ONCE) {
+                    $this->del($timer_id, self::EV_TIMER_ONCE);
+                }
+                continue;
+            }
+            return;
+        }
+        $this->_selectTimeout = 100000000;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function clearAllTimer()
+    {
+        $this->_scheduler = new \SplPriorityQueue();
+        $this->_scheduler->setExtractFlags(\SplPriorityQueue::EXTR_BOTH);
+        $this->_eventTimer = array();
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function loop()
+    {
+        while (1) {
+            if(\DIRECTORY_SEPARATOR === '/') {
+                // Calls signal handlers for pending signals
+                \pcntl_signal_dispatch();
+            }
+
+            $read  = $this->_readFds;
+            $write = $this->_writeFds;
+            $except = $this->_exceptFds;
+
+            if ($read || $write || $except) {
+                // Waiting read/write/signal/timeout events.
+                try {
+                    $ret = @stream_select($read, $write, $except, 0, $this->_selectTimeout);
+                } catch (\Exception $e) {} catch (\Error $e) {}
+
+            } else {
+                usleep($this->_selectTimeout);
+                $ret = false;
+            }
+
+
+            if (!$this->_scheduler->isEmpty()) {
+                $this->tick();
+            }
+
+            if (!$ret) {
+                continue;
+            }
+
+            if ($read) {
+                foreach ($read as $fd) {
+                    $fd_key = (int)$fd;
+                    if (isset($this->_allEvents[$fd_key][self::EV_READ])) {
+                        \call_user_func_array($this->_allEvents[$fd_key][self::EV_READ][0],
+                            array($this->_allEvents[$fd_key][self::EV_READ][1]));
+                    }
+                }
+            }
+
+            if ($write) {
+                foreach ($write as $fd) {
+                    $fd_key = (int)$fd;
+                    if (isset($this->_allEvents[$fd_key][self::EV_WRITE])) {
+                        \call_user_func_array($this->_allEvents[$fd_key][self::EV_WRITE][0],
+                            array($this->_allEvents[$fd_key][self::EV_WRITE][1]));
+                    }
+                }
+            }
+
+            if($except) {
+                foreach($except as $fd) {
+                    $fd_key = (int) $fd;
+                    if(isset($this->_allEvents[$fd_key][self::EV_EXCEPT])) {
+                        \call_user_func_array($this->_allEvents[$fd_key][self::EV_EXCEPT][0],
+                            array($this->_allEvents[$fd_key][self::EV_EXCEPT][1]));
+                    }
+                }
+            }
+        }
+    }
+
+    /**
+     * Destroy loop.
+     *
+     * @return void
+     */
+    public function destroy()
+    {
+
+    }
+
+    /**
+     * Get timer count.
+     *
+     * @return integer
+     */
+    public function getTimerCount()
+    {
+        return \count($this->_eventTimer);
+    }
+}

+ 221 - 0
GatewayWorker/vendor/workerman/workerman/Events/Swoole.php

@@ -0,0 +1,221 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    Ares<aresrr#qq.com>
+ * @link      http://www.workerman.net/
+ * @link      https://github.com/ares333/Workerman
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Workerman\Events;
+
+use Swoole\Event;
+use Swoole\Timer;
+
+class Swoole implements EventInterface
+{
+
+    protected $_timer = array();
+
+    protected $_timerOnceMap = array();
+
+    protected $mapId = 0;
+
+    protected $_fd = array();
+
+    // milisecond
+    public static $signalDispatchInterval = 200;
+
+    protected $_hasSignal = false;
+
+    /**
+     *
+     * {@inheritdoc}
+     *
+     * @see \Workerman\Events\EventInterface::add()
+     */
+    public function add($fd, $flag, $func, $args = null)
+    {
+        if (! isset($args)) {
+            $args = array();
+        }
+        switch ($flag) {
+            case self::EV_SIGNAL:
+                $res = \pcntl_signal($fd, $func, false);
+                if (! $this->_hasSignal && $res) {
+                    Timer::tick(static::$signalDispatchInterval,
+                        function () {
+                            \pcntl_signal_dispatch();
+                        });
+                    $this->_hasSignal = true;
+                }
+                return $res;
+            case self::EV_TIMER:
+            case self::EV_TIMER_ONCE:
+                $method = self::EV_TIMER === $flag ? 'tick' : 'after';
+                if ($this->mapId > \PHP_INT_MAX) {
+                    $this->mapId = 0;
+                }
+                $mapId = $this->mapId++;
+                $timer_id = Timer::$method($fd * 1000,
+                    function ($timer_id = null) use ($func, $args, $mapId) {
+                        \call_user_func_array($func, $args);
+                        // EV_TIMER_ONCE
+                        if (! isset($timer_id)) {
+                            // may be deleted in $func
+                            if (\array_key_exists($mapId, $this->_timerOnceMap)) {
+                                $timer_id = $this->_timerOnceMap[$mapId];
+                                unset($this->_timer[$timer_id],
+                                    $this->_timerOnceMap[$mapId]);
+                            }
+                        }
+                    });
+                if ($flag === self::EV_TIMER_ONCE) {
+                    $this->_timerOnceMap[$mapId] = $timer_id;
+                    $this->_timer[$timer_id] = $mapId;
+                } else {
+                    $this->_timer[$timer_id] = null;
+                }
+                return $timer_id;
+            case self::EV_READ:
+            case self::EV_WRITE:
+                $fd_key = (int) $fd;
+                if (! isset($this->_fd[$fd_key])) {
+                    if ($flag === self::EV_READ) {
+                        $res = Event::add($fd, $func, null, SWOOLE_EVENT_READ);
+                        $fd_type = SWOOLE_EVENT_READ;
+                    } else {
+                        $res = Event::add($fd, null, $func, SWOOLE_EVENT_WRITE);
+                        $fd_type = SWOOLE_EVENT_WRITE;
+                    }
+                    if ($res) {
+                        $this->_fd[$fd_key] = $fd_type;
+                    }
+                } else {
+                    $fd_val = $this->_fd[$fd_key];
+                    $res = true;
+                    if ($flag === self::EV_READ) {
+                        if (($fd_val & SWOOLE_EVENT_READ) !== SWOOLE_EVENT_READ) {
+                            $res = Event::set($fd, $func, null,
+                                SWOOLE_EVENT_READ | SWOOLE_EVENT_WRITE);
+                            $this->_fd[$fd_key] |= SWOOLE_EVENT_READ;
+                        }
+                    } else {
+                        if (($fd_val & SWOOLE_EVENT_WRITE) !== SWOOLE_EVENT_WRITE) {
+                            $res = Event::set($fd, null, $func,
+                                SWOOLE_EVENT_READ | SWOOLE_EVENT_WRITE);
+                            $this->_fd[$fd_key] |= SWOOLE_EVENT_WRITE;
+                        }
+                    }
+                }
+                return $res;
+        }
+    }
+
+    /**
+     *
+     * {@inheritdoc}
+     *
+     * @see \Workerman\Events\EventInterface::del()
+     */
+    public function del($fd, $flag)
+    {
+        switch ($flag) {
+            case self::EV_SIGNAL:
+                return \pcntl_signal($fd, SIG_IGN, false);
+            case self::EV_TIMER:
+            case self::EV_TIMER_ONCE:
+                // already remove in EV_TIMER_ONCE callback.
+                if (! \array_key_exists($fd, $this->_timer)) {
+                    return true;
+                }
+                $res = Timer::clear($fd);
+                if ($res) {
+                    $mapId = $this->_timer[$fd];
+                    if (isset($mapId)) {
+                        unset($this->_timerOnceMap[$mapId]);
+                    }
+                    unset($this->_timer[$fd]);
+                }
+                return $res;
+            case self::EV_READ:
+            case self::EV_WRITE:
+                $fd_key = (int) $fd;
+                if (isset($this->_fd[$fd_key])) {
+                    $fd_val = $this->_fd[$fd_key];
+                    if ($flag === self::EV_READ) {
+                        $flag_remove = ~ SWOOLE_EVENT_READ;
+                    } else {
+                        $flag_remove = ~ SWOOLE_EVENT_WRITE;
+                    }
+                    $fd_val &= $flag_remove;
+                    if (0 === $fd_val) {
+                        $res = Event::del($fd);
+                        if ($res) {
+                            unset($this->_fd[$fd_key]);
+                        }
+                    } else {
+                        $res = Event::set($fd, null, null, $fd_val);
+                        if ($res) {
+                            $this->_fd[$fd_key] = $fd_val;
+                        }
+                    }
+                } else {
+                    $res = true;
+                }
+                return $res;
+        }
+    }
+
+    /**
+     *
+     * {@inheritdoc}
+     *
+     * @see \Workerman\Events\EventInterface::clearAllTimer()
+     */
+    public function clearAllTimer()
+    {
+        foreach (array_keys($this->_timer) as $v) {
+            Timer::clear($v);
+        }
+        $this->_timer = array();
+        $this->_timerOnceMap = array();
+    }
+
+    /**
+     *
+     * {@inheritdoc}
+     *
+     * @see \Workerman\Events\EventInterface::loop()
+     */
+    public function loop()
+    {
+        Event::wait();
+    }
+
+    /**
+     *
+     * {@inheritdoc}
+     *
+     * @see \Workerman\Events\EventInterface::destroy()
+     */
+    public function destroy()
+    {
+        //Event::exit();
+    }
+
+    /**
+     *
+     * {@inheritdoc}
+     *
+     * @see \Workerman\Events\EventInterface::getTimerCount()
+     */
+    public function getTimerCount()
+    {
+        return \count($this->_timer);
+    }
+}

+ 48 - 0
GatewayWorker/vendor/workerman/workerman/Lib/Constants.php

@@ -0,0 +1,48 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ *
+ * @link      http://www.workerman.net/
+ */
+
+// Display errors.
+ini_set('display_errors', 'on');
+// Reporting all.
+error_reporting(E_ALL);
+// JIT is not stable, temporarily disabled.
+ini_set('pcre.jit', 0);
+
+// For onError callback.
+const WORKERMAN_CONNECT_FAIL = 1;
+// For onError callback.
+const WORKERMAN_SEND_FAIL = 2;
+
+// Define OS Type
+const OS_TYPE_LINUX   = 'linux';
+const OS_TYPE_WINDOWS = 'windows';
+
+// Compatible with php7
+if (!class_exists('Error')) {
+    class Error extends Exception
+    {
+    }
+}
+
+if (!interface_exists('SessionHandlerInterface')) {
+    interface SessionHandlerInterface {
+        public function close();
+        public function destroy($session_id);
+        public function gc($maxlifetime);
+        public function open($save_path ,$session_name);
+        public function read($session_id);
+        public function write($session_id , $session_data);
+    }
+}

+ 22 - 0
GatewayWorker/vendor/workerman/workerman/Lib/Timer.php

@@ -0,0 +1,22 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link      http://www.workerman.net/
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Workerman\Lib;
+
+/**
+ * Do not use Workerman\Lib\Timer.
+ * Please use Workerman\Timer.
+ * This class is only used for compatibility with workerman 3.*
+ * @package Workerman\Lib
+ */
+class Timer extends \Workerman\Timer {}

+ 21 - 0
GatewayWorker/vendor/workerman/workerman/MIT-LICENSE.txt

@@ -0,0 +1,21 @@
+The MIT License
+
+Copyright (c) 2009-2015 walkor<walkor@workerman.net> and contributors (see https://github.com/walkor/workerman/contributors)
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.

+ 61 - 0
GatewayWorker/vendor/workerman/workerman/Protocols/Frame.php

@@ -0,0 +1,61 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link      http://www.workerman.net/
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Workerman\Protocols;
+
+use Workerman\Connection\TcpConnection;
+
+/**
+ * Frame Protocol.
+ */
+class Frame
+{
+    /**
+     * Check the integrity of the package.
+     *
+     * @param string        $buffer
+     * @param TcpConnection $connection
+     * @return int
+     */
+    public static function input($buffer, TcpConnection $connection)
+    {
+        if (\strlen($buffer) < 4) {
+            return 0;
+        }
+        $unpack_data = \unpack('Ntotal_length', $buffer);
+        return $unpack_data['total_length'];
+    }
+
+    /**
+     * Decode.
+     *
+     * @param string $buffer
+     * @return string
+     */
+    public static function decode($buffer)
+    {
+        return \substr($buffer, 4);
+    }
+
+    /**
+     * Encode.
+     *
+     * @param string $buffer
+     * @return string
+     */
+    public static function encode($buffer)
+    {
+        $total_length = 4 + \strlen($buffer);
+        return \pack('N', $total_length) . $buffer;
+    }
+}

+ 326 - 0
GatewayWorker/vendor/workerman/workerman/Protocols/Http.php

@@ -0,0 +1,326 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link      http://www.workerman.net/
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Workerman\Protocols;
+
+use Workerman\Connection\TcpConnection;
+use Workerman\Protocols\Http\Request;
+use Workerman\Protocols\Http\Response;
+use Workerman\Protocols\Websocket;
+use Workerman\Worker;
+
+/**
+ * Class Http.
+ * @package Workerman\Protocols
+ */
+class Http
+{
+    /**
+     * Request class name.
+     *
+     * @var string
+     */
+    protected static $_requestClass = 'Workerman\Protocols\Http\Request';
+
+    /**
+     * Session name.
+     *
+     * @var string
+     */
+    protected static $_sessionName = 'PHPSID';
+
+    /**
+     * Upload tmp dir.
+     *
+     * @var string
+     */
+    protected static $_uploadTmpDir = '';
+
+    /**
+     * Open cache.
+     *
+     * @var bool.
+     */
+    protected static $_enableCache = true;
+
+    /**
+     * Get or set session name.
+     *
+     * @param null $name
+     * @return string
+     */
+    public static function sessionName($name = null)
+    {
+        if ($name !== null && $name !== '') {
+            static::$_sessionName = (string)$name;
+        }
+        return static::$_sessionName;
+    }
+
+    /**
+     * Get or set the request class name.
+     *
+     * @param null $class_name
+     * @return string
+     */
+    public static function requestClass($class_name = null)
+    {
+        if ($class_name) {
+            static::$_requestClass = $class_name;
+        }
+        return static::$_requestClass;
+    }
+
+    /**
+     * Enable or disable Cache.
+     *
+     * @param $value
+     */
+    public static function enableCache($value)
+    {
+        static::$_enableCache = (bool)$value;
+    }
+
+    /**
+     * Check the integrity of the package.
+     *
+     * @param string $recv_buffer
+     * @param TcpConnection $connection
+     * @return int
+     */
+    public static function input($recv_buffer, TcpConnection $connection)
+    {
+        static $input = array();
+        if (!isset($recv_buffer[512]) && isset($input[$recv_buffer])) {
+            return $input[$recv_buffer];
+        }
+        $crlf_pos = \strpos($recv_buffer, "\r\n\r\n");
+        if (false === $crlf_pos) {
+            // Judge whether the package length exceeds the limit.
+            if ($recv_len = \strlen($recv_buffer) >= 16384) {
+                $connection->close("HTTP/1.1 413 Request Entity Too Large\r\n\r\n");
+                return 0;
+            }
+            return 0;
+        }
+
+        $head_len = $crlf_pos + 4;
+        $method = \strstr($recv_buffer, ' ', true);
+
+        if ($method === 'GET' || $method === 'OPTIONS' || $method === 'HEAD' || $method === 'DELETE') {
+            if (!isset($recv_buffer[512])) {
+                $input[$recv_buffer] = $head_len;
+                if (\count($input) > 512) {
+                    unset($input[key($input)]);
+                }
+            }
+            return $head_len;
+        } else if ($method !== 'POST' && $method !== 'PUT') {
+            $connection->close("HTTP/1.1 400 Bad Request\r\n\r\n", true);
+            return 0;
+        }
+
+        $header = \substr($recv_buffer, 0, $crlf_pos);
+        $length = false;
+        if ($pos = \strpos($header, "\r\nContent-Length: ")) {
+            $length = $head_len + (int)\substr($header, $pos + 18, 10);
+        } else if (\preg_match("/\r\ncontent-length: ?(\d+)/i", $header, $match)) {
+            $length = $head_len + $match[1];
+        }
+
+        if ($length !== false) {
+            if (!isset($recv_buffer[512])) {
+                $input[$recv_buffer] = $length;
+                if (\count($input) > 512) {
+                    unset($input[key($input)]);
+                }
+            }
+            return $length;
+        }
+
+        $connection->close("HTTP/1.1 400 Bad Request\r\n\r\n", true);
+        return 0;
+    }
+
+    /**
+     * Http decode.
+     *
+     * @param string $recv_buffer
+     * @param TcpConnection $connection
+     * @return \Workerman\Protocols\Http\Request
+     */
+    public static function decode($recv_buffer, TcpConnection $connection)
+    {
+        static $requests = array();
+        $cacheable = static::$_enableCache && !isset($recv_buffer[512]);
+        if (true === $cacheable && isset($requests[$recv_buffer])) {
+            $request = $requests[$recv_buffer];
+            $request->connection = $connection;
+            $connection->__request = $request;
+            $request->properties = array();
+            return $request;
+        }
+        $request = new static::$_requestClass($recv_buffer);
+        $request->connection = $connection;
+        $connection->__request = $request;
+        if (true === $cacheable) {
+            $requests[$recv_buffer] = $request;
+            if (\count($requests) > 512) {
+                unset($requests[key($requests)]);
+            }
+        }
+        return $request;
+    }
+
+    /**
+     * Http encode.
+     *
+     * @param string|Response $response
+     * @param TcpConnection $connection
+     * @return string
+     */
+    public static function encode($response, TcpConnection $connection)
+    {
+        if (isset($connection->__request)) {
+            $connection->__request->session = null;
+            $connection->__request->connection = null;
+            $connection->__request = null;
+        }
+        if (\is_scalar($response) || null === $response) {
+            $ext_header = '';
+            if (isset($connection->__header)) {
+                foreach ($connection->__header as $name => $value) {
+                    if (\is_array($value)) {
+                        foreach ($value as $item) {
+                            $ext_header = "$name: $item\r\n";
+                        }
+                    } else {
+                        $ext_header = "$name: $value\r\n";
+                    }
+                }
+                unset($connection->__header);
+            }
+            $body_len = \strlen($response);
+            return "HTTP/1.1 200 OK\r\nServer: workerman\r\n{$ext_header}Connection: keep-alive\r\nContent-Type: text/html;charset=utf-8\r\nContent-Length: $body_len\r\n\r\n$response";
+        }
+
+        if (isset($connection->__header)) {
+            $response->withHeaders($connection->__header);
+            unset($connection->__header);
+        }
+
+        if (isset($response->file)) {
+            $file = $response->file['file'];
+            $offset = $response->file['offset'];
+            $length = $response->file['length'];
+            $file_size = (int)\filesize($file);
+            $body_len = $length > 0 ? $length : $file_size - $offset;
+            $response->withHeaders(array(
+                'Content-Length' => $body_len,
+                'Accept-Ranges'  => 'bytes',
+            ));
+            if ($offset || $length) {
+                $offset_end = $offset + $body_len - 1;
+                $response->header('Content-Range', "bytes $offset-$offset_end/$file_size");
+            }
+            if ($body_len < 2 * 1024 * 1024) {
+                $connection->send((string)$response . file_get_contents($file, false, null, $offset, $body_len), true);
+                return '';
+            }
+            $handler = \fopen($file, 'r');
+            if (false === $handler) {
+                $connection->close(new Response(403, null, '403 Forbidden'));
+                return '';
+            }
+            $connection->send((string)$response, true);
+            static::sendStream($connection, $handler, $offset, $length);
+            return '';
+        }
+
+        return (string)$response;
+    }
+
+    /**
+     * Send remainder of a stream to client.
+     *
+     * @param TcpConnection $connection
+     * @param $handler
+     * @param $offset
+     * @param $length
+     */
+    protected static function sendStream(TcpConnection $connection, $handler, $offset = 0, $length = 0)
+    {
+        $connection->bufferFull = false;
+        if ($offset !== 0) {
+            \fseek($handler, $offset);
+        }
+        $offset_end = $offset + $length;
+        // Read file content from disk piece by piece and send to client.
+        $do_write = function () use ($connection, $handler, $length, $offset_end) {
+            // Send buffer not full.
+            while ($connection->bufferFull === false) {
+                // Read from disk.
+                $size = 1024 * 1024;
+                if ($length !== 0) {
+                    $tell = \ftell($handler);
+                    $remain_size = $offset_end - $tell;
+                    if ($remain_size <= 0) {
+                        fclose($handler);
+                        $connection->onBufferDrain = null;
+                        return;
+                    }
+                    $size = $remain_size > $size ? $size : $remain_size;
+                }
+
+                $buffer = \fread($handler, $size);
+                // Read eof.
+                if ($buffer === '' || $buffer === false) {
+                    fclose($handler);
+                    $connection->onBufferDrain = null;
+                    return;
+                }
+                $connection->send($buffer, true);
+            }
+        };
+        // Send buffer full.
+        $connection->onBufferFull = function ($connection) {
+            $connection->bufferFull = true;
+        };
+        // Send buffer drain.
+        $connection->onBufferDrain = function ($connection) use ($do_write) {
+            $connection->bufferFull = false;
+            $do_write();
+        };
+        $do_write();
+    }
+
+    /**
+     * Set or get uploadTmpDir.
+     *
+     * @return bool|string
+     */
+    public static function uploadTmpDir($dir = null)
+    {
+        if (null !== $dir) {
+            static::$_uploadTmpDir = $dir;
+        }
+        if (static::$_uploadTmpDir === '') {
+            if ($upload_tmp_dir = \ini_get('upload_tmp_dir')) {
+                static::$_uploadTmpDir = $upload_tmp_dir;
+            } else if ($upload_tmp_dir = \sys_get_temp_dir()) {
+                static::$_uploadTmpDir = $upload_tmp_dir;
+            }
+        }
+        return static::$_uploadTmpDir;
+    }
+}

+ 48 - 0
GatewayWorker/vendor/workerman/workerman/Protocols/Http/Chunk.php

@@ -0,0 +1,48 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link      http://www.workerman.net/
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Workerman\Protocols\Http;
+
+
+/**
+ * Class Chunk
+ * @package Workerman\Protocols\Http
+ */
+class Chunk
+{
+    /**
+     * Chunk buffer.
+     *
+     * @var string
+     */
+    protected $_buffer = null;
+
+    /**
+     * Chunk constructor.
+     * @param $buffer
+     */
+    public function __construct($buffer)
+    {
+        $this->_buffer = $buffer;
+    }
+
+    /**
+     * __toString
+     *
+     * @return string
+     */
+    public function __toString()
+    {
+        return \dechex(\strlen($this->_buffer))."\r\n$this->_buffer\r\n";
+    }
+}

+ 617 - 0
GatewayWorker/vendor/workerman/workerman/Protocols/Http/Request.php

@@ -0,0 +1,617 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link      http://www.workerman.net/
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Workerman\Protocols\Http;
+
+use Workerman\Connection\TcpConnection;
+use Workerman\Protocols\Http\Session;
+use Workerman\Protocols\Http;
+use Workerman\Worker;
+
+/**
+ * Class Request
+ * @package Workerman\Protocols\Http
+ */
+class Request
+{
+    /**
+     * Connection.
+     *
+     * @var TcpConnection
+     */
+    public $connection = null;
+
+    /**
+     * Session instance.
+     *
+     * @var Session
+     */
+    public $session = null;
+
+    /**
+     * Properties.
+     *
+     * @var array
+     */
+    public $properties = array();
+
+    /**
+     * Http buffer.
+     *
+     * @var string
+     */
+    protected $_buffer = null;
+
+    /**
+     * Request data.
+     *
+     * @var array
+     */
+    protected $_data = null;
+
+    /**
+     * Header cache.
+     *
+     * @var array
+     */
+    protected static $_headerCache = array();
+
+    /**
+     * Get cache.
+     *
+     * @var array
+     */
+    protected static $_getCache = array();
+
+    /**
+     * Post cache.
+     *
+     * @var array
+     */
+    protected static $_postCache = array();
+
+    /**
+     * Enable cache.
+     *
+     * @var bool
+     */
+    protected static $_enableCache = true;
+
+
+    /**
+     * Request constructor.
+     *
+     * @param $buffer
+     */
+    public function __construct($buffer)
+    {
+        $this->_buffer = $buffer;
+    }
+
+    /**
+     * $_GET.
+     *
+     * @param null $name
+     * @param null $default
+     * @return mixed|null
+     */
+    public function get($name = null, $default = null)
+    {
+        if (!isset($this->_data['get'])) {
+            $this->parseGet();
+        }
+        if (null === $name) {
+            return $this->_data['get'];
+        }
+        return isset($this->_data['get'][$name]) ? $this->_data['get'][$name] : $default;
+    }
+
+    /**
+     * $_POST.
+     *
+     * @param $name
+     * @param null $default
+     * @return mixed|null
+     */
+    public function post($name = null, $default = null)
+    {
+        if (!isset($this->_data['post'])) {
+            $this->parsePost();
+        }
+        if (null === $name) {
+            return $this->_data['post'];
+        }
+        return isset($this->_data['post'][$name]) ? $this->_data['post'][$name] : $default;
+    }
+
+    /**
+     * Get header item by name.
+     *
+     * @param null $name
+     * @param null $default
+     * @return string|null
+     */
+    public function header($name = null, $default = null)
+    {
+        if (!isset($this->_data['headers'])) {
+            $this->parseHeaders();
+        }
+        if (null === $name) {
+            return $this->_data['headers'];
+        }
+        $name = \strtolower($name);
+        return isset($this->_data['headers'][$name]) ? $this->_data['headers'][$name] : $default;
+    }
+
+    /**
+     * Get cookie item by name.
+     *
+     * @param null $name
+     * @param null $default
+     * @return string|null
+     */
+    public function cookie($name = null, $default = null)
+    {
+        if (!isset($this->_data['cookie'])) {
+            \parse_str(\str_replace('; ', '&', $this->header('cookie')), $this->_data['cookie']);
+        }
+        if ($name === null) {
+            return $this->_data['cookie'];
+        }
+        return isset($this->_data['cookie'][$name]) ? $this->_data['cookie'][$name] : $default;
+    }
+
+    /**
+     * Get upload files.
+     *
+     * @param null $name
+     * @return array|null
+     */
+    public function file($name = null)
+    {
+        if (!isset($this->_data['files'])) {
+            $this->parsePost();
+        }
+        if (null === $name) {
+            return $this->_data['files'];
+        }
+        return isset($this->_data['files'][$name]) ? $this->_data['files'][$name] : null;
+    }
+
+    /**
+     * Get method.
+     *
+     * @return string
+     */
+    public function method()
+    {
+        if (!isset($this->_data['method'])) {
+            $this->parseHeadFirstLine();
+        }
+        return $this->_data['method'];
+    }
+
+    /**
+     * Get http protocol version.
+     *
+     * @return string.
+     */
+    public function protocolVersion()
+    {
+        if (!isset($this->_data['protocolVersion'])) {
+            $this->parseProtocolVersion();
+        }
+        return $this->_data['protocolVersion'];
+    }
+
+    /**
+     * Get host.
+     *
+     * @param bool $without_port
+     * @return string
+     */
+    public function host($without_port = false)
+    {
+        $host = $this->header('host');
+        if ($without_port && $pos = \strpos($host, ':')) {
+            return \substr($host, 0, $pos);
+        }
+        return $host;
+    }
+
+    /**
+     * Get uri.
+     *
+     * @return mixed
+     */
+    public function uri()
+    {
+        if (!isset($this->_data['uri'])) {
+            $this->parseHeadFirstLine();
+        }
+        return $this->_data['uri'];
+    }
+
+    /**
+     * Get path.
+     *
+     * @return mixed
+     */
+    public function path()
+    {
+        if (!isset($this->_data['path'])) {
+            $this->_data['path'] = \parse_url($this->uri(), PHP_URL_PATH);
+        }
+        return $this->_data['path'];
+    }
+
+    /**
+     * Get query string.
+     *
+     * @return mixed
+     */
+    public function queryString()
+    {
+        if (!isset($this->_data['query_string'])) {
+            $this->_data['query_string'] = \parse_url($this->uri(), PHP_URL_QUERY);
+        }
+        return $this->_data['query_string'];
+    }
+
+    /**
+     * Get session.
+     *
+     * @return bool|\Workerman\Protocols\Http\Session
+     */
+    public function session()
+    {
+        if ($this->session === null) {
+            $session_id = $this->sessionId();
+            if ($session_id === false) {
+                return false;
+            }
+            $this->session = new Session($session_id);
+        }
+        return $this->session;
+    }
+
+    /**
+     * Get session id.
+     *
+     * @return bool|mixed
+     */
+    public function sessionId()
+    {
+        if (!isset($this->_data['sid'])) {
+            $session_name = Http::sessionName();
+            $sid = $this->cookie($session_name);
+            if ($sid === '' || $sid === null) {
+                if ($this->connection === null) {
+                    Worker::safeEcho('Request->session() fail, header already send');
+                    return false;
+                }
+                $sid = static::createSessionId();
+                $cookie_params = \session_get_cookie_params();
+                $this->connection->__header['Set-Cookie'] = array($session_name . '=' . $sid
+                    . (empty($cookie_params['domain']) ? '' : '; Domain=' . $cookie_params['domain'])
+                    . (empty($cookie_params['lifetime']) ? '' : '; Max-Age=' . ($cookie_params['lifetime'] + \time()))
+                    . (empty($cookie_params['path']) ? '' : '; Path=' . $cookie_params['path'])
+                    . (!$cookie_params['secure'] ? '' : '; Secure')
+                    . (!$cookie_params['httponly'] ? '' : '; HttpOnly'));
+            }
+            $this->_data['sid'] = $sid;
+        }
+        return $this->_data['sid'];
+    }
+
+    /**
+     * Get http raw head.
+     *
+     * @return string
+     */
+    public function rawHead()
+    {
+        if (!isset($this->_data['head'])) {
+            $this->_data['head'] = \strstr($this->_buffer, "\r\n\r\n", true);
+        }
+        return $this->_data['head'];
+    }
+
+    /**
+     * Get http raw body.
+     *
+     * @return string
+     */
+    public function rawBody()
+    {
+        return \substr($this->_buffer, \strpos($this->_buffer, "\r\n\r\n") + 4);
+    }
+
+    /**
+     * Get raw buffer.
+     *
+     * @return string
+     */
+    public function rawBuffer()
+    {
+        return $this->_buffer;
+    }
+
+    /**
+     * Enable or disable cache.
+     *
+     * @param $value
+     */
+    public static function enableCache($value)
+    {
+        static::$_enableCache = (bool)$value;
+    }
+
+    /**
+     * Parse first line of http header buffer.
+     *
+     * @return void
+     */
+    protected function parseHeadFirstLine()
+    {
+        $first_line = \strstr($this->_buffer, "\r\n", true);
+        $tmp = \explode(' ', $first_line, 3);
+        $this->_data['method'] = $tmp[0];
+        $this->_data['uri'] = isset($tmp[1]) ? $tmp[1] : '/';
+    }
+
+    /**
+     * Parse protocol version.
+     *
+     * @return void
+     */
+    protected function parseProtocolVersion()
+    {
+        $first_line = \strstr($this->_buffer, "\r\n", true);
+        $protoco_version = substr(\strstr($first_line, 'HTTP/'), 5);
+        $this->_data['protocolVersion'] = $protoco_version ? $protoco_version : '1.0';
+    }
+
+    /**
+     * Parse headers.
+     *
+     * @return void
+     */
+    protected function parseHeaders()
+    {
+        $this->_data['headers'] = array();
+        $raw_head = $this->rawHead();
+        $head_buffer = \substr($raw_head, \strpos($raw_head, "\r\n") + 2);
+        $cacheable = static::$_enableCache && !isset($head_buffer[2048]);
+        if ($cacheable && isset(static::$_headerCache[$head_buffer])) {
+            $this->_data['headers'] = static::$_headerCache[$head_buffer];
+            return;
+        }
+        $head_data = \explode("\r\n", $head_buffer);
+        foreach ($head_data as $content) {
+            if (false !== \strpos($content, ':')) {
+                list($key, $value) = \explode(':', $content, 2);
+                $this->_data['headers'][\strtolower($key)] = \ltrim($value);
+            } else {
+                $this->_data['headers'][\strtolower($content)] = '';
+            }
+        }
+        if ($cacheable) {
+            static::$_headerCache[$head_buffer] = $this->_data['headers'];
+            if (\count(static::$_headerCache) > 128) {
+                unset(static::$_headerCache[key(static::$_headerCache)]);
+            }
+        }
+    }
+
+    /**
+     * Parse head.
+     *
+     * @return void
+     */
+    protected function parseGet()
+    {
+        $query_string = $this->queryString();
+        $this->_data['get'] = array();
+        if ($query_string === '') {
+            return;
+        }
+        $cacheable = static::$_enableCache && !isset($query_string[1024]);
+        if ($cacheable && isset(static::$_getCache[$query_string])) {
+            $this->_data['get'] = static::$_getCache[$query_string];
+            return;
+        }
+        \parse_str($query_string, $this->_data['get']);
+        if ($cacheable) {
+            static::$_getCache[$query_string] = $this->_data['get'];
+            if (\count(static::$_getCache) > 256) {
+                unset(static::$_getCache[key(static::$_getCache)]);
+            }
+        }
+    }
+
+    /**
+     * Parse post.
+     *
+     * @return void
+     */
+    protected function parsePost()
+    {
+        $body_buffer = $this->rawBody();
+        $this->_data['post'] = $this->_data['files'] = array();
+        if ($body_buffer === '') {
+            return;
+        }
+        $cacheable = static::$_enableCache && !isset($body_buffer[1024]);
+        if ($cacheable && isset(static::$_postCache[$body_buffer])) {
+            $this->_data['post'] = static::$_postCache[$body_buffer];
+            return;
+        }
+        $content_type = $this->header('content-type');
+        if ($content_type !== null && \preg_match('/boundary="?(\S+)"?/', $content_type, $match)) {
+            $http_post_boundary = '--' . $match[1];
+            $this->parseUploadFiles($http_post_boundary);
+            return;
+        }
+        \parse_str($body_buffer, $this->_data['post']);
+        if ($cacheable) {
+            static::$_postCache[$body_buffer] = $this->_data['post'];
+            if (\count(static::$_postCache) > 256) {
+                unset(static::$_postCache[key(static::$_postCache)]);
+            }
+        }
+    }
+
+    /**
+     * Parse upload files.
+     *
+     * @param $http_post_boundary
+     * @return void
+     */
+    protected function parseUploadFiles($http_post_boundary)
+    {
+        $http_body = $this->rawBody();
+        $http_body = \substr($http_body, 0, \strlen($http_body) - (\strlen($http_post_boundary) + 4));
+        $boundary_data_array = \explode($http_post_boundary . "\r\n", $http_body);
+        if ($boundary_data_array[0] === '') {
+            unset($boundary_data_array[0]);
+        }
+        $key = -1;
+        $files = array();
+        foreach ($boundary_data_array as $boundary_data_buffer) {
+            list($boundary_header_buffer, $boundary_value) = \explode("\r\n\r\n", $boundary_data_buffer, 2);
+            // Remove \r\n from the end of buffer.
+            $boundary_value = \substr($boundary_value, 0, -2);
+            $key++;
+            foreach (\explode("\r\n", $boundary_header_buffer) as $item) {
+                list($header_key, $header_value) = \explode(": ", $item);
+                $header_key = \strtolower($header_key);
+                switch ($header_key) {
+                    case "content-disposition":
+                        // Is file data.
+                        if (\preg_match('/name="(.*?)"; filename="(.*?)"$/i', $header_value, $match)) {
+                            $error = 0;
+                            $tmp_file = '';
+                            $size = \strlen($boundary_value);
+                            $tmp_upload_dir = HTTP::uploadTmpDir();
+                            if (!$tmp_upload_dir) {
+                                $error = UPLOAD_ERR_NO_TMP_DIR;
+                            } else {
+                                $tmp_file = \tempnam($tmp_upload_dir, 'workerman.upload.');
+                                if ($tmp_file === false || false == \file_put_contents($tmp_file, $boundary_value)) {
+                                    $error = UPLOAD_ERR_CANT_WRITE;
+                                }
+                            }
+                            // Parse upload files.
+                            $files[$key] = array(
+                                'key' => $match[1],
+                                'name' => $match[2],
+                                'tmp_name' => $tmp_file,
+                                'size' => $size,
+                                'error' => $error
+                            );
+                            break;
+                        } // Is post field.
+                        else {
+                            // Parse $_POST.
+                            if (\preg_match('/name="(.*?)"$/', $header_value, $match)) {
+                                $this->_data['post'][$match[1]] = $boundary_value;
+                            }
+                        }
+                        break;
+                    case "content-type":
+                        // add file_type
+                        $files[$key]['type'] = \trim($header_value);
+                        break;
+                }
+            }
+        }
+
+        foreach ($files as $file) {
+            $key = $file['key'];
+            unset($file['key']);
+            $this->_data['files'][$key] = $file;
+        }
+    }
+
+    /**
+     * Create session id.
+     *
+     * @return string
+     */
+    protected static function createSessionId()
+    {
+        return \bin2hex(\pack('d', \microtime(true)) . \pack('N', \mt_rand()));
+    }
+
+    /**
+     * Setter.
+     *
+     * @param $name
+     * @param $value
+     * @return void
+     */
+    public function __set($name, $value)
+    {
+        $this->properties[$name] = $value;
+    }
+
+    /**
+     * Getter.
+     *
+     * @param $name
+     * @return mixed|null
+     */
+    public function __get($name)
+    {
+        return isset($this->properties[$name]) ? $this->properties[$name] : null;
+    }
+
+    /**
+     * Isset.
+     *
+     * @param $name
+     * @return bool
+     */
+    public function __isset($name)
+    {
+        return isset($this->properties[$name]);
+    }
+
+    /**
+     * Unset.
+     *
+     * @param $name
+     * @return void
+     */
+    public function __unset($name)
+    {
+        unset($this->properties[$name]);
+    }
+
+    /**
+     * __destruct.
+     *
+     * @return void
+     */
+    public function __destruct()
+    {
+        if (isset($this->_data['files'])) {
+            foreach ($this->_data['files'] as $item) {
+                if (\is_file($item['tmp_name'])) {
+                    \unlink($item['tmp_name']);
+                }
+            }
+        }
+    }
+}

+ 378 - 0
GatewayWorker/vendor/workerman/workerman/Protocols/Http/Response.php

@@ -0,0 +1,378 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link      http://www.workerman.net/
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Workerman\Protocols\Http;
+
+/**
+ * Class Response
+ * @package Workerman\Protocols\Http
+ */
+class Response
+{
+    /**
+     * Header data.
+     *
+     * @var array
+     */
+    protected $_header = null;
+
+    /**
+     * Http status.
+     *
+     * @var int
+     */
+    protected $_status = null;
+
+    /**
+     * Http reason.
+     *
+     * @var string
+     */
+    protected $_reason = null;
+
+    /**
+     * Http version.
+     *
+     * @var string
+     */
+    protected $_version = '1.1';
+
+    /**
+     * Http body.
+     *
+     * @var string
+     */
+    protected $_body = null;
+
+    /**
+     * Mine type map.
+     * @var array
+     */
+    protected static $_mimeTypeMap = null;
+
+    /**
+     * Phrases.
+     *
+     * @var array
+     */
+    protected static $_phrases = array(
+        100 => 'Continue',
+        101 => 'Switching Protocols',
+        102 => 'Processing',
+        200 => 'OK',
+        201 => 'Created',
+        202 => 'Accepted',
+        203 => 'Non-Authoritative Information',
+        204 => 'No Content',
+        205 => 'Reset Content',
+        206 => 'Partial Content',
+        207 => 'Multi-status',
+        208 => 'Already Reported',
+        300 => 'Multiple Choices',
+        301 => 'Moved Permanently',
+        302 => 'Found',
+        303 => 'See Other',
+        304 => 'Not Modified',
+        305 => 'Use Proxy',
+        306 => 'Switch Proxy',
+        307 => 'Temporary Redirect',
+        400 => 'Bad Request',
+        401 => 'Unauthorized',
+        402 => 'Payment Required',
+        403 => 'Forbidden',
+        404 => 'Not Found',
+        405 => 'Method Not Allowed',
+        406 => 'Not Acceptable',
+        407 => 'Proxy Authentication Required',
+        408 => 'Request Time-out',
+        409 => 'Conflict',
+        410 => 'Gone',
+        411 => 'Length Required',
+        412 => 'Precondition Failed',
+        413 => 'Request Entity Too Large',
+        414 => 'Request-URI Too Large',
+        415 => 'Unsupported Media Type',
+        416 => 'Requested range not satisfiable',
+        417 => 'Expectation Failed',
+        418 => 'I\'m a teapot',
+        422 => 'Unprocessable Entity',
+        423 => 'Locked',
+        424 => 'Failed Dependency',
+        425 => 'Unordered Collection',
+        426 => 'Upgrade Required',
+        428 => 'Precondition Required',
+        429 => 'Too Many Requests',
+        431 => 'Request Header Fields Too Large',
+        451 => 'Unavailable For Legal Reasons',
+        500 => 'Internal Server Error',
+        501 => 'Not Implemented',
+        502 => 'Bad Gateway',
+        503 => 'Service Unavailable',
+        504 => 'Gateway Time-out',
+        505 => 'HTTP Version not supported',
+        506 => 'Variant Also Negotiates',
+        507 => 'Insufficient Storage',
+        508 => 'Loop Detected',
+        511 => 'Network Authentication Required',
+    );
+
+    /**
+     * Init.
+     *
+     * @return void
+     */
+    public static function init() {
+        static::initMimeTypeMap();
+    }
+
+    /**
+     * Response constructor.
+     *
+     * @param int $status
+     * @param array $headers
+     * @param string $body
+     */
+    public function __construct(
+        $status = 200,
+        $headers = array(),
+        $body = ''
+    ) {
+        $this->_status = $status;
+        $this->_header = $headers;
+        $this->_body = $body;
+    }
+
+    /**
+     * Set header.
+     *
+     * @param $name
+     * @param $value
+     * @return $this
+     */
+    public function header($name, $value) {
+        $this->_header[$name] = $value;
+        return $this;
+    }
+
+    /**
+     * Set headers.
+     *
+     * @param $headers
+     * @return $this
+     */
+    public function withHeaders($headers) {
+        $this->_header = \array_merge($this->_header, $headers);
+        return $this;
+    }
+
+    /**
+     * Set status.
+     *
+     * @param $code
+     * @param null $reason_phrase
+     * @return $this
+     */
+    public function withStatus($code, $reason_phrase = null) {
+        $this->_status = $code;
+        $this->_reason = $reason_phrase;
+        return $this;
+    }
+
+    /**
+     * Set protocol version.
+     *
+     * @param $version
+     * @return $this
+     */
+    public function withProtocolVersion($version) {
+        $this->_version = $version;
+        return $this;
+    }
+
+    /**
+     * Set http body.
+     *
+     * @param $body
+     * @return $this
+     */
+    public function withBody($body) {
+        $this->_body = $body;
+        return $this;
+    }
+
+    /**
+     * Send file.
+     *
+     * @param $file
+     * @param int $offset
+     * @param int $length
+     * @return $this
+     */
+    public function withFile($file, $offset = 0, $length = 0) {
+        if (!\is_file($file)) {
+            return $this->withStatus(404)->withBody('<h3>404 Not Found</h3>');
+        }
+        $this->file = array('file' => $file, 'offset' => $offset, 'length' => $length);
+        return $this;
+    }
+
+    /**
+     * Set cookie.
+     *
+     * @param $name
+     * @param string $value
+     * @param int $maxage
+     * @param string $path
+     * @param string $domain
+     * @param bool $secure
+     * @param bool $http_only
+     * @return $this
+     */
+    public function cookie($name, $value = '', $max_age = 0, $path = '', $domain = '', $secure = false, $http_only = false)
+    {
+        $this->_header['Set-Cookie'][] = $name . '=' . \rawurlencode($value)
+            . (empty($domain) ? '' : '; Domain=' . $domain)
+            . (empty($max_age) ? '' : '; Max-Age=' . $max_age)
+            . (empty($path) ? '' : '; Path=' . $path)
+            . (!$secure ? '' : '; Secure')
+            . (!$http_only ? '' : '; HttpOnly');
+        return $this;
+    }
+
+    /**
+     * Create header for file.
+     *
+     * @param $file
+     * @return string
+     */
+    protected function createHeadForFile($file_info)
+    {
+        $file = $file_info['file'];
+        $reason = $this->_reason ? $this->_reason : static::$_phrases[$this->_status];
+        $head = "HTTP/{$this->_version} {$this->_status} $reason\r\n";
+        $headers = $this->_header;
+        if (!isset($headers['Server'])) {
+            $head .= "Server: workerman\r\n";
+        }
+        foreach ($headers as $name => $value) {
+            if (\is_array($value)) {
+                foreach ($value as $item) {
+                    $head .= "$name: $item\r\n";
+                }
+                continue;
+            }
+            $head .= "$name: $value\r\n";
+        }
+
+        if (!isset($headers['Connection'])) {
+            $head .= "Connection: keep-alive\r\n";
+        }
+
+        $file_info = \pathinfo($file);
+        $extension = isset($file_info['extension']) ? $file_info['extension'] : '';
+        $base_name = isset($file_info['basename']) ? $file_info['basename'] : 'unknown';
+        if (!isset($headers['Content-Type'])) {
+            if (isset(self::$_mimeTypeMap[$extension])) {
+                $head .= "Content-Type: " . self::$_mimeTypeMap[$extension] . "\r\n";
+            } else {
+                $head .= "Content-Type: application/octet-stream\r\n";
+            }
+        }
+
+        if (!isset($headers['Content-Disposition']) && !isset(self::$_mimeTypeMap[$extension])) {
+            $head .= "Content-Disposition: attachment; filename=\"$base_name\"\r\n";
+        }
+
+        if (!isset($headers['Last-Modified'])) {
+            if ($mtime = \filemtime($file)) {
+                $head .= 'Last-Modified: '.\date('D, d M Y H:i:s', $mtime) . ' ' . \date_default_timezone_get() ."\r\n";
+            }
+        }
+
+        return "{$head}\r\n";
+    }
+
+    /**
+     * __toString.
+     *
+     * @return string
+     */
+    public function __toString()
+    {
+        if (isset($this->file)) {
+            return $this->createHeadForFile($this->file);
+        }
+
+        $reason = $this->_reason ? $this->_reason : static::$_phrases[$this->_status];
+        $body_len = \strlen($this->_body);
+        if (empty($this->_header)) {
+            return "HTTP/{$this->_version} {$this->_status} $reason\r\nServer: workerman\r\nContent-Type: text/html;charset=utf-8\r\nContent-Length: $body_len\r\nConnection: keep-alive\r\n\r\n{$this->_body}";
+        }
+
+        $head = "HTTP/{$this->_version} {$this->_status} $reason\r\n";
+        $headers = $this->_header;
+        if (!isset($headers['Server'])) {
+            $head .= "Server: workerman\r\n";
+        }
+        foreach ($headers as $name => $value) {
+            if (\is_array($value)) {
+                foreach ($value as $item) {
+                    $head .= "$name: $item\r\n";
+                }
+                continue;
+            }
+            $head .= "$name: $value\r\n";
+        }
+
+        if (!isset($headers['Connection'])) {
+            $head .= "Connection: keep-alive\r\n";
+        }
+
+        if (!isset($headers['Content-Type'])) {
+            $head .= "Content-Type: text/html;charset=utf-8\r\n";
+        } else if ($headers['Content-Type'] === 'text/event-stream') {
+            return $head . $this->_body;
+        }
+
+        if (!isset($headers['Transfer-Encoding'])) {
+            $head .= "Content-Length: $body_len\r\n\r\n";
+        } else {
+            return "$head\r\n".dechex($body_len)."\r\n{$this->_body}\r\n";
+        }
+
+        // The whole http package
+        return $head . $this->_body;
+    }
+
+    /**
+     * Init mime map.
+     *
+     * @return void
+     */
+    public static function initMimeTypeMap()
+    {
+        $mime_file = __DIR__ . '/mime.types';
+        $items = \file($mime_file, \FILE_IGNORE_NEW_LINES | \FILE_SKIP_EMPTY_LINES);
+        foreach ($items as $content) {
+            if (\preg_match("/\s*(\S+)\s+(\S.+)/", $content, $match)) {
+                $mime_type       = $match[1];
+                $extension_var   = $match[2];
+                $extension_array = \explode(' ', \substr($extension_var, 0, -1));
+                foreach ($extension_array as $file_extension) {
+                    static::$_mimeTypeMap[$file_extension] = $mime_type;
+                }
+            }
+        }
+    }
+}
+Response::init();

+ 64 - 0
GatewayWorker/vendor/workerman/workerman/Protocols/Http/ServerSentEvents.php

@@ -0,0 +1,64 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link      http://www.workerman.net/
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Workerman\Protocols\Http;
+
+/**
+ * Class ServerSentEvents
+ * @package Workerman\Protocols\Http
+ */
+class ServerSentEvents
+{
+    /**
+     * Data.
+     * @var array
+     */
+    protected $_data = null;
+
+    /**
+     * ServerSentEvents constructor.
+     * $data for example ['event'=>'ping', 'data' => 'some thing', 'id' => 1000, 'retry' => 5000]
+     * @param array $data
+     */
+    public function __construct(array $data)
+    {
+        $this->_data = $data;
+    }
+
+    /**
+     * __toString.
+     *
+     * @return string
+     */
+    public function __toString()
+    {
+        $buffer = '';
+        $data = $this->_data;
+        if (isset($data[''])) {
+            $buffer = ": {$data['']}\n";
+        }
+        if (isset($data['event'])) {
+            $buffer .= "event: {$data['event']}\n";
+        }
+        if (isset($data['data'])) {
+            $buffer .= 'data: ' . \str_replace("\n", "\ndata: ", $data['data']) . "\n\n";
+        }
+        if (isset($data['id'])) {
+            $buffer .= "id: {$data['id']}\n";
+        }
+        if (isset($data['retry'])) {
+            $buffer .= "retry: {$data['retry']}\n";
+        }
+        return $buffer;
+    }
+}

+ 359 - 0
GatewayWorker/vendor/workerman/workerman/Protocols/Http/Session.php

@@ -0,0 +1,359 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link      http://www.workerman.net/
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Workerman\Protocols\Http;
+
+
+/**
+ * Class Session
+ * @package Workerman\Protocols\Http
+ */
+class Session
+{
+    /**
+     * Session andler class which implements SessionHandlerInterface.
+     *
+     * @var string
+     */
+    protected static $_handlerClass = 'Workerman\Protocols\Http\Session\FileSessionHandler';
+
+    /**
+     * Parameters of __constructor for session handler class.
+     *
+     * @var null
+     */
+    protected static $_handlerConfig = null;
+
+    /**
+     * Session.gc_probability
+     *
+     * @var int
+     */
+    protected static $_sessionGcProbability = 1;
+
+    /**
+     * Session.gc_divisor
+     *
+     * @var int
+     */
+    protected static $_sessionGcDivisor = 1000;
+
+    /**
+     * Session.gc_maxlifetime
+     *
+     * @var int
+     */
+    protected static $_sessionGcMaxLifeTime = 1440;
+
+    /**
+     * Session handler instance.
+     *
+     * @var \SessionHandlerInterface
+     */
+    protected static $_handler = null;
+
+    /**
+     * Session data.
+     *
+     * @var array
+     */
+    protected $_data = array();
+
+    /**
+     * Session changed and need to save.
+     *
+     * @var bool
+     */
+    protected $_needSave = false;
+
+    /**
+     * Session id.
+     *
+     * @var null
+     */
+    protected $_sessionId = null;
+
+    /**
+     * Session constructor.
+     *
+     * @param $session_id
+     */
+    public function __construct($session_id)
+    {
+        static::checkSessionId($session_id);
+        if (static::$_handler === null) {
+            static::initHandler();
+        }
+        $this->_sessionId = $session_id;
+        if ($data = static::$_handler->read($session_id)) {
+            $this->_data = \unserialize($data);
+        }
+    }
+
+    /**
+     * Get session id.
+     *
+     * @return string
+     */
+    public function getId()
+    {
+        return $this->_sessionId;
+    }
+
+    /**
+     * Get session.
+     *
+     * @param $name
+     * @param null $default
+     * @return mixed|null
+     */
+    public function get($name, $default = null)
+    {
+        return isset($this->_data[$name]) ? $this->_data[$name] : $default;
+    }
+
+    /**
+     * Store data in the session.
+     *
+     * @param $name
+     * @param $value
+     */
+    public function set($name, $value)
+    {
+        $this->_data[$name] = $value;
+        $this->_needSave = true;
+    }
+
+    /**
+     * Delete an item from the session.
+     *
+     * @param $name
+     */
+    public function delete($name)
+    {
+        unset($this->_data[$name]);
+        $this->_needSave = true;
+    }
+
+    /**
+     * Retrieve and delete an item from the session.
+     *
+     * @param $name
+     * @param null $default
+     * @return mixed|null
+     */
+    public function pull($name, $default = null)
+    {
+        $value = $this->get($name, $default);
+        $this->delete($name);
+        return $value;
+    }
+
+    /**
+     * Store data in the session.
+     *
+     * @param $key
+     * @param null $value
+     */
+    public function put($key, $value = null)
+    {
+        if (!\is_array($key)) {
+            $this->set($key, $value);
+            return;
+        }
+
+        foreach ($key as $k => $v) {
+            $this->_data[$k] = $v;
+        }
+        $this->_needSave = true;
+    }
+
+    /**
+     * Remove a piece of data from the session.
+     *
+     * @param $name
+     */
+    public function forget($name)
+    {
+        if (\is_scalar($name)) {
+            $this->delete($name);
+            return;
+        }
+        if (\is_array($name)) {
+            foreach ($name as $key) {
+                unset($this->_data[$key]);
+            }
+        }
+        $this->_needSave = true;
+    }
+
+    /**
+     * Retrieve all the data in the session.
+     *
+     * @return array
+     */
+    public function all()
+    {
+        return $this->_data;
+    }
+
+    /**
+     * Remove all data from the session.
+     *
+     * @return void
+     */
+    public function flush()
+    {
+        $this->_needSave = true;
+        $this->_data = array();
+    }
+
+    /**
+     * Determining If An Item Exists In The Session.
+     *
+     * @param $name
+     * @return bool
+     */
+    public function has($name)
+    {
+        return isset($this->_data[$name]);
+    }
+
+    /**
+     * To determine if an item is present in the session, even if its value is null.
+     *
+     * @param $name
+     * @return bool
+     */
+    public function exists($name)
+    {
+        return \array_key_exists($name, $this->_data);
+    }
+
+    /**
+     * Save session to store.
+     *
+     * @return void
+     */
+    public function save()
+    {
+        if ($this->_needSave) {
+            if (empty($this->_data)) {
+                static::$_handler->destroy($this->_sessionId);
+            } else {
+                static::$_handler->write($this->_sessionId, \serialize($this->_data));
+            }
+        }
+        $this->_needSave = false;
+    }
+
+    /**
+     * Init.
+     *
+     * @return void
+     */
+    public static function init()
+    {
+        if ($gc_probability = \ini_get('session.gc_probability')) {
+            self::$_sessionGcProbability = (int)$gc_probability;
+        }
+
+        if ($gc_divisor = \ini_get('session.gc_divisor')) {
+            self::$_sessionGcDivisor = (int)$gc_divisor;
+        }
+
+        if ($gc_max_life_time = \ini_get('session.gc_maxlifetime')) {
+            self::$_sessionGcMaxLifeTime = (int)$gc_max_life_time;
+        }
+    }
+
+    /**
+     * Set session handler class.
+     *
+     * @param null $class_name
+     * @param null $config
+     * @return string
+     */
+    public static function handlerClass($class_name = null, $config = null)
+    {
+        if ($class_name) {
+            static::$_handlerClass = $class_name;
+        }
+        if ($config) {
+            static::$_handlerConfig = $config;
+        }
+        return static::$_handlerClass;
+    }
+
+    /**
+     * Init handler.
+     *
+     * @return void
+     */
+    protected static function initHandler()
+    {
+        if (static::$_handlerConfig === null) {
+            static::$_handler = new static::$_handlerClass();
+        } else {
+            static::$_handler = new static::$_handlerClass(static::$_handlerConfig);
+        }
+    }
+
+    /**
+     * Try GC sessions.
+     *
+     * @return void
+     */
+    public function tryGcSessions()
+    {
+        if (\rand(1, static::$_sessionGcDivisor) > static::$_sessionGcProbability) {
+            return;
+        }
+        static::$_handler->gc(static::$_sessionGcMaxLifeTime);
+    }
+
+    /**
+     * __destruct.
+     *
+     * @return void
+     */
+    public function __destruct()
+    {
+        $this->save();
+        $this->tryGcSessions();
+    }
+
+    /**
+     * Check session id.
+     *
+     * @param $session_id
+     */
+    protected static function checkSessionId($session_id)
+    {
+        if (!\preg_match('/^[a-zA-Z0-9]+$/', $session_id)) {
+            throw new SessionException("session_id $session_id is invalid");
+        }
+    }
+}
+
+/**
+ * Class SessionException
+ * @package Workerman\Protocols\Http
+ */
+class SessionException extends \RuntimeException
+{
+
+}
+
+// Init session.
+Session::init();

+ 153 - 0
GatewayWorker/vendor/workerman/workerman/Protocols/Http/Session/FileSessionHandler.php

@@ -0,0 +1,153 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link      http://www.workerman.net/
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Workerman\Protocols\Http\Session;
+
+/**
+ * Class FileSessionHandler
+ * @package Workerman\Protocols\Http\Session
+ */
+class FileSessionHandler implements \SessionHandlerInterface
+{
+    /**
+     * Session save path.
+     *
+     * @var string
+     */
+    protected static $_sessionSavePath = null;
+
+    /**
+     * Session file prefix.
+     *
+     * @var string
+     */
+    protected static $_sessionFilePrefix = 'session_';
+
+    /**
+     * Init.
+     */
+    public static function init() {
+        $save_path = @\session_save_path();
+        if (!$save_path || \strpos($save_path, 'tcp://') === 0) {
+            $save_path = \sys_get_temp_dir();
+        }
+        static::sessionSavePath($save_path);
+    }
+
+    /**
+     * FileSessionHandler constructor.
+     * @param array $config
+     */
+    public function __construct($config = array()) {
+        if (isset($config['save_path'])) {
+            static::sessionSavePath($config['save_path']);
+        }
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function open($save_path, $name)
+    {
+        return true;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function read($session_id)
+    {
+        $session_file = static::sessionFile($session_id);
+        \clearstatcache();
+        if (\is_file($session_file)) {
+            $data = \file_get_contents($session_file);
+            return $data ? $data : '';
+        }
+        return '';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function write($session_id, $session_data)
+    {
+        $temp_file = static::$_sessionSavePath.uniqid(mt_rand(), true);
+        if (!\file_put_contents($temp_file, $session_data)) {
+            return false;
+        }
+        return \rename($temp_file, static::sessionFile($session_id));
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function close()
+    {
+        return true;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function destroy($session_id)
+    {
+        $session_file = static::sessionFile($session_id);
+        if (\is_file($session_file)) {
+            \unlink($session_file);
+        }
+        return true;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function gc($maxlifetime) {
+        $time_now = \time();
+        foreach (\glob(static::$_sessionSavePath . static::$_sessionFilePrefix . '*') as $file) {
+            if(\is_file($file) && $time_now - \filemtime($file) > $maxlifetime) {
+                \unlink($file);
+            }
+        }
+    }
+
+    /**
+     * Get session file path.
+     *
+     * @param $session_id
+     * @return string
+     */
+    protected static function sessionFile($session_id) {
+        return static::$_sessionSavePath.static::$_sessionFilePrefix.$session_id;
+    }
+
+    /**
+     * Get or set session file path.
+     *
+     * @param $path
+     * @return string
+     */
+    public static function sessionSavePath($path) {
+        if ($path) {
+            if ($path[\strlen($path)-1] !== DIRECTORY_SEPARATOR) {
+                $path .= DIRECTORY_SEPARATOR;
+            }
+            static::$_sessionSavePath = $path;
+            if (!\is_dir($path)) {
+                \mkdir($path, 0777, true);
+            }
+        }
+        return $path;
+    }
+}
+
+FileSessionHandler::init();

+ 119 - 0
GatewayWorker/vendor/workerman/workerman/Protocols/Http/Session/RedisSessionHandler.php

@@ -0,0 +1,119 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link      http://www.workerman.net/
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Workerman\Protocols\Http\Session;
+
+/**
+ * Class RedisSessionHandler
+ * @package Workerman\Protocols\Http\Session
+ */
+class RedisSessionHandler extends \SessionHandler
+{
+
+    /**
+     * @var \Redis
+     */
+    protected $_redis;
+
+    /**
+     * @var int
+     */
+    protected $_maxLifeTime;
+
+    /**
+     * RedisSessionHandler constructor.
+     * @param $config = [
+     *  'host'     => '127.0.0.1',
+     *  'port'     => 6379,
+     *  'timeout'  => 2,
+     *  'auth'     => '******',
+     *  'database' => 2,
+     *  'prefix'   => 'redis_session_',
+     * ]
+     */
+    public function __construct($config)
+    {
+        if (false === extension_loaded('redis')) {
+            throw new \RuntimeException('Please install redis extension.');
+        }
+        $this->_maxLifeTime = (int)ini_get('session.gc_maxlifetime');
+
+        if (!isset($config['timeout'])) {
+            $config['timeout'] = 2;
+        }
+
+        $this->_redis = new \Redis();
+        if (false === $this->_redis->connect($config['host'], $config['port'], $config['timeout'])) {
+            throw new \RuntimeException("Redis connect {$config['host']}:{$config['port']} fail.");
+        }
+        if (!empty($config['auth'])) {
+            $this->_redis->auth($config['auth']);
+        }
+        if (!empty($config['database'])) {
+            $this->_redis->select($config['database']);
+        }
+        if (empty($config['prefix'])) {
+            $config['prefix'] = 'redis_session_';
+        }
+        $this->_redis->setOption(\Redis::OPT_PREFIX, $config['prefix']);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function open($save_path, $name)
+    {
+        return true;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function read($session_id)
+    {
+        return $this->_redis->get($session_id);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function write($session_id, $session_data)
+    {
+        return true === $this->_redis->setex($session_id, $this->_maxLifeTime, $session_data);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function destroy($session_id)
+    {
+        $this->_redis->del($session_id);
+        return true;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function close()
+    {
+        return true;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function gc($maxlifetime)
+    {
+        return true;
+    }
+}

+ 90 - 0
GatewayWorker/vendor/workerman/workerman/Protocols/Http/mime.types

@@ -0,0 +1,90 @@
+
+types {
+    text/html                             html htm shtml;
+    text/css                              css;
+    text/xml                              xml;
+    image/gif                             gif;
+    image/jpeg                            jpeg jpg;
+    application/javascript                js;
+    application/atom+xml                  atom;
+    application/rss+xml                   rss;
+
+    text/mathml                           mml;
+    text/plain                            txt;
+    text/vnd.sun.j2me.app-descriptor      jad;
+    text/vnd.wap.wml                      wml;
+    text/x-component                      htc;
+
+    image/png                             png;
+    image/tiff                            tif tiff;
+    image/vnd.wap.wbmp                    wbmp;
+    image/x-icon                          ico;
+    image/x-jng                           jng;
+    image/x-ms-bmp                        bmp;
+    image/svg+xml                         svg svgz;
+    image/webp                            webp;
+
+    application/font-woff                 woff;
+    application/java-archive              jar war ear;
+    application/json                      json;
+    application/mac-binhex40              hqx;
+    application/msword                    doc;
+    application/pdf                       pdf;
+    application/postscript                ps eps ai;
+    application/rtf                       rtf;
+    application/vnd.apple.mpegurl         m3u8;
+    application/vnd.ms-excel              xls;
+    application/vnd.ms-fontobject         eot;
+    application/vnd.ms-powerpoint         ppt;
+    application/vnd.wap.wmlc              wmlc;
+    application/vnd.google-earth.kml+xml  kml;
+    application/vnd.google-earth.kmz      kmz;
+    application/x-7z-compressed           7z;
+    application/x-cocoa                   cco;
+    application/x-java-archive-diff       jardiff;
+    application/x-java-jnlp-file          jnlp;
+    application/x-makeself                run;
+    application/x-perl                    pl pm;
+    application/x-pilot                   prc pdb;
+    application/x-rar-compressed          rar;
+    application/x-redhat-package-manager  rpm;
+    application/x-sea                     sea;
+    application/x-shockwave-flash         swf;
+    application/x-stuffit                 sit;
+    application/x-tcl                     tcl tk;
+    application/x-x509-ca-cert            der pem crt;
+    application/x-xpinstall               xpi;
+    application/xhtml+xml                 xhtml;
+    application/xspf+xml                  xspf;
+    application/zip                       zip;
+
+    application/octet-stream              bin exe dll;
+    application/octet-stream              deb;
+    application/octet-stream              dmg;
+    application/octet-stream              iso img;
+    application/octet-stream              msi msp msm;
+
+    application/vnd.openxmlformats-officedocument.wordprocessingml.document    docx;
+    application/vnd.openxmlformats-officedocument.spreadsheetml.sheet          xlsx;
+    application/vnd.openxmlformats-officedocument.presentationml.presentation  pptx;
+
+    audio/midi                            mid midi kar;
+    audio/mpeg                            mp3;
+    audio/ogg                             ogg;
+    audio/x-m4a                           m4a;
+    audio/x-realaudio                     ra;
+
+    video/3gpp                            3gpp 3gp;
+    video/mp2t                            ts;
+    video/mp4                             mp4;
+    video/mpeg                            mpeg mpg;
+    video/quicktime                       mov;
+    video/webm                            webm;
+    video/x-flv                           flv;
+    video/x-m4v                           m4v;
+    video/x-mng                           mng;
+    video/x-ms-asf                        asx asf;
+    video/x-ms-wmv                        wmv;
+    video/x-msvideo                       avi;
+    font/ttf                              ttf;
+}

+ 52 - 0
GatewayWorker/vendor/workerman/workerman/Protocols/ProtocolInterface.php

@@ -0,0 +1,52 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link      http://www.workerman.net/
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Workerman\Protocols;
+
+use Workerman\Connection\ConnectionInterface;
+
+/**
+ * Protocol interface
+ */
+interface ProtocolInterface
+{
+    /**
+     * Check the integrity of the package.
+     * Please return the length of package.
+     * If length is unknow please return 0 that mean wating more data.
+     * If the package has something wrong please return false the connection will be closed.
+     *
+     * @param string              $recv_buffer
+     * @param ConnectionInterface $connection
+     * @return int|false
+     */
+    public static function input($recv_buffer, ConnectionInterface $connection);
+
+    /**
+     * Decode package and emit onMessage($message) callback, $message is the result that decode returned.
+     *
+     * @param string              $recv_buffer
+     * @param ConnectionInterface $connection
+     * @return mixed
+     */
+    public static function decode($recv_buffer, ConnectionInterface $connection);
+
+    /**
+     * Encode package brefore sending to client.
+     * 
+     * @param mixed               $data
+     * @param ConnectionInterface $connection
+     * @return string
+     */
+    public static function encode($data, ConnectionInterface $connection);
+}

+ 70 - 0
GatewayWorker/vendor/workerman/workerman/Protocols/Text.php

@@ -0,0 +1,70 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link      http://www.workerman.net/
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Workerman\Protocols;
+
+use Workerman\Connection\ConnectionInterface;
+
+/**
+ * Text Protocol.
+ */
+class Text
+{
+    /**
+     * Check the integrity of the package.
+     *
+     * @param string        $buffer
+     * @param ConnectionInterface $connection
+     * @return int
+     */
+    public static function input($buffer, ConnectionInterface $connection)
+    {
+        // Judge whether the package length exceeds the limit.
+        if (isset($connection->maxPackageSize) && \strlen($buffer) >= $connection->maxPackageSize) {
+            $connection->close();
+            return 0;
+        }
+        //  Find the position of  "\n".
+        $pos = \strpos($buffer, "\n");
+        // No "\n", packet length is unknown, continue to wait for the data so return 0.
+        if ($pos === false) {
+            return 0;
+        }
+        // Return the current package length.
+        return $pos + 1;
+    }
+
+    /**
+     * Encode.
+     *
+     * @param string $buffer
+     * @return string
+     */
+    public static function encode($buffer)
+    {
+        // Add "\n"
+        return $buffer . "\n";
+    }
+
+    /**
+     * Decode.
+     *
+     * @param string $buffer
+     * @return string
+     */
+    public static function decode($buffer)
+    {
+        // Remove "\n"
+        return \rtrim($buffer, "\r\n");
+    }
+}

+ 503 - 0
GatewayWorker/vendor/workerman/workerman/Protocols/Websocket.php

@@ -0,0 +1,503 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link      http://www.workerman.net/
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Workerman\Protocols;
+
+use Workerman\Connection\ConnectionInterface;
+use Workerman\Connection\TcpConnection;
+use Workerman\Worker;
+
+/**
+ * WebSocket protocol.
+ */
+class Websocket implements \Workerman\Protocols\ProtocolInterface
+{
+    /**
+     * Websocket blob type.
+     *
+     * @var string
+     */
+    const BINARY_TYPE_BLOB = "\x81";
+
+    /**
+     * Websocket arraybuffer type.
+     *
+     * @var string
+     */
+    const BINARY_TYPE_ARRAYBUFFER = "\x82";
+
+    /**
+     * Check the integrity of the package.
+     *
+     * @param string              $buffer
+     * @param ConnectionInterface $connection
+     * @return int
+     */
+    public static function input($buffer, ConnectionInterface $connection)
+    {
+        // Receive length.
+        $recv_len = \strlen($buffer);
+        // We need more data.
+        if ($recv_len < 6) {
+            return 0;
+        }
+
+        // Has not yet completed the handshake.
+        if (empty($connection->websocketHandshake)) {
+            return static::dealHandshake($buffer, $connection);
+        }
+
+        // Buffer websocket frame data.
+        if ($connection->websocketCurrentFrameLength) {
+            // We need more frame data.
+            if ($connection->websocketCurrentFrameLength > $recv_len) {
+                // Return 0, because it is not clear the full packet length, waiting for the frame of fin=1.
+                return 0;
+            }
+        } else {
+            $firstbyte    = \ord($buffer[0]);
+            $secondbyte   = \ord($buffer[1]);
+            $data_len     = $secondbyte & 127;
+            $is_fin_frame = $firstbyte >> 7;
+            $masked       = $secondbyte >> 7;
+
+            if (!$masked) {
+                Worker::safeEcho("frame not masked so close the connection\n");
+                $connection->close();
+                return 0;
+            }
+
+            $opcode       = $firstbyte & 0xf;
+            switch ($opcode) {
+                case 0x0:
+                    break;
+                // Blob type.
+                case 0x1:
+                    break;
+                // Arraybuffer type.
+                case 0x2:
+                    break;
+                // Close package.
+                case 0x8:
+                    // Try to emit onWebSocketClose callback.
+                    if (isset($connection->onWebSocketClose) || isset($connection->worker->onWebSocketClose)) {
+                        try {
+                            \call_user_func(isset($connection->onWebSocketClose)?$connection->onWebSocketClose:$connection->worker->onWebSocketClose, $connection);
+                        } catch (\Exception $e) {
+                            Worker::log($e);
+                            exit(250);
+                        } catch (\Error $e) {
+                            Worker::log($e);
+                            exit(250);
+                        }
+                    } // Close connection.
+                    else {
+                        $connection->close("\x88\x02\x03\xe8", true);
+                    }
+                    return 0;
+                // Ping package.
+                case 0x9:
+                    break;
+                // Pong package.
+                case 0xa:
+                    break;
+                // Wrong opcode. 
+                default :
+                    Worker::safeEcho("error opcode $opcode and close websocket connection. Buffer:" . bin2hex($buffer) . "\n");
+                    $connection->close();
+                    return 0;
+            }
+
+            // Calculate packet length.
+            $head_len = 6;
+            if ($data_len === 126) {
+                $head_len = 8;
+                if ($head_len > $recv_len) {
+                    return 0;
+                }
+                $pack     = \unpack('nn/ntotal_len', $buffer);
+                $data_len = $pack['total_len'];
+            } else {
+                if ($data_len === 127) {
+                    $head_len = 14;
+                    if ($head_len > $recv_len) {
+                        return 0;
+                    }
+                    $arr      = \unpack('n/N2c', $buffer);
+                    $data_len = $arr['c1']*4294967296 + $arr['c2'];
+                }
+            }
+            $current_frame_length = $head_len + $data_len;
+
+            $total_package_size = \strlen($connection->websocketDataBuffer) + $current_frame_length;
+            if ($total_package_size > $connection->maxPackageSize) {
+                Worker::safeEcho("error package. package_length=$total_package_size\n");
+                $connection->close();
+                return 0;
+            }
+
+            if ($is_fin_frame) {
+                if ($opcode === 0x9) {
+                    if ($recv_len >= $current_frame_length) {
+                        $ping_data = static::decode(\substr($buffer, 0, $current_frame_length), $connection);
+                        $connection->consumeRecvBuffer($current_frame_length);
+                        $tmp_connection_type = isset($connection->websocketType) ? $connection->websocketType : static::BINARY_TYPE_BLOB;
+                        $connection->websocketType = "\x8a";
+                        if (isset($connection->onWebSocketPing) || isset($connection->worker->onWebSocketPing)) {
+                            try {
+                                \call_user_func(isset($connection->onWebSocketPing)?$connection->onWebSocketPing:$connection->worker->onWebSocketPing, $connection, $ping_data);
+                            } catch (\Exception $e) {
+                                Worker::log($e);
+                                exit(250);
+                            } catch (\Error $e) {
+                                Worker::log($e);
+                                exit(250);
+                            }
+                        } else {
+                            $connection->send($ping_data);
+                        }
+                        $connection->websocketType = $tmp_connection_type;
+                        if ($recv_len > $current_frame_length) {
+                            return static::input(\substr($buffer, $current_frame_length), $connection);
+                        }
+                    }
+                    return 0;
+                } else if ($opcode === 0xa) {
+                    if ($recv_len >= $current_frame_length) {
+                        $pong_data = static::decode(\substr($buffer, 0, $current_frame_length), $connection);
+                        $connection->consumeRecvBuffer($current_frame_length);
+                        $tmp_connection_type = isset($connection->websocketType) ? $connection->websocketType : static::BINARY_TYPE_BLOB;
+                        $connection->websocketType = "\x8a";
+                        // Try to emit onWebSocketPong callback.
+                        if (isset($connection->onWebSocketPong) || isset($connection->worker->onWebSocketPong)) {
+                            try {
+                                \call_user_func(isset($connection->onWebSocketPong)?$connection->onWebSocketPong:$connection->worker->onWebSocketPong, $connection, $pong_data);
+                            } catch (\Exception $e) {
+                                Worker::log($e);
+                                exit(250);
+                            } catch (\Error $e) {
+                                Worker::log($e);
+                                exit(250);
+                            }
+                        }
+                        $connection->websocketType = $tmp_connection_type;
+                        if ($recv_len > $current_frame_length) {
+                            return static::input(\substr($buffer, $current_frame_length), $connection);
+                        }
+                    }
+                    return 0;
+                }
+                return $current_frame_length;
+            } else {
+                $connection->websocketCurrentFrameLength = $current_frame_length;
+            }
+        }
+
+        // Received just a frame length data.
+        if ($connection->websocketCurrentFrameLength === $recv_len) {
+            static::decode($buffer, $connection);
+            $connection->consumeRecvBuffer($connection->websocketCurrentFrameLength);
+            $connection->websocketCurrentFrameLength = 0;
+            return 0;
+        } // The length of the received data is greater than the length of a frame.
+        elseif ($connection->websocketCurrentFrameLength < $recv_len) {
+            static::decode(\substr($buffer, 0, $connection->websocketCurrentFrameLength), $connection);
+            $connection->consumeRecvBuffer($connection->websocketCurrentFrameLength);
+            $current_frame_length                    = $connection->websocketCurrentFrameLength;
+            $connection->websocketCurrentFrameLength = 0;
+            // Continue to read next frame.
+            return static::input(\substr($buffer, $current_frame_length), $connection);
+        } // The length of the received data is less than the length of a frame.
+        else {
+            return 0;
+        }
+    }
+
+    /**
+     * Websocket encode.
+     *
+     * @param string              $buffer
+     * @param ConnectionInterface $connection
+     * @return string
+     */
+    public static function encode($buffer, ConnectionInterface $connection)
+    {
+        if (!is_scalar($buffer)) {
+            throw new \Exception("You can't send(" . \gettype($buffer) . ") to client, you need to convert it to a string. ");
+        }
+        $len = \strlen($buffer);
+        if (empty($connection->websocketType)) {
+            $connection->websocketType = static::BINARY_TYPE_BLOB;
+        }
+
+        $first_byte = $connection->websocketType;
+
+        if ($len <= 125) {
+            $encode_buffer = $first_byte . \chr($len) . $buffer;
+        } else {
+            if ($len <= 65535) {
+                $encode_buffer = $first_byte . \chr(126) . \pack("n", $len) . $buffer;
+            } else {
+                $encode_buffer = $first_byte . \chr(127) . \pack("xxxxN", $len) . $buffer;
+            }
+        }
+
+        // Handshake not completed so temporary buffer websocket data waiting for send.
+        if (empty($connection->websocketHandshake)) {
+            if (empty($connection->tmpWebsocketData)) {
+                $connection->tmpWebsocketData = '';
+            }
+            // If buffer has already full then discard the current package.
+            if (\strlen($connection->tmpWebsocketData) > $connection->maxSendBufferSize) {
+                if ($connection->onError) {
+                    try {
+                        \call_user_func($connection->onError, $connection, \WORKERMAN_SEND_FAIL, 'send buffer full and drop package');
+                    } catch (\Exception $e) {
+                        Worker::log($e);
+                        exit(250);
+                    } catch (\Error $e) {
+                        Worker::log($e);
+                        exit(250);
+                    }
+                }
+                return '';
+            }
+            $connection->tmpWebsocketData .= $encode_buffer;
+            // Check buffer is full.
+            if ($connection->maxSendBufferSize <= \strlen($connection->tmpWebsocketData)) {
+                if ($connection->onBufferFull) {
+                    try {
+                        \call_user_func($connection->onBufferFull, $connection);
+                    } catch (\Exception $e) {
+                        Worker::log($e);
+                        exit(250);
+                    } catch (\Error $e) {
+                        Worker::log($e);
+                        exit(250);
+                    }
+                }
+            }
+
+            // Return empty string.
+            return '';
+        }
+
+        return $encode_buffer;
+    }
+
+    /**
+     * Websocket decode.
+     *
+     * @param string              $buffer
+     * @param ConnectionInterface $connection
+     * @return string
+     */
+    public static function decode($buffer, ConnectionInterface $connection)
+    {
+        $len = \ord($buffer[1]) & 127;
+        if ($len === 126) {
+            $masks = \substr($buffer, 4, 4);
+            $data  = \substr($buffer, 8);
+        } else {
+            if ($len === 127) {
+                $masks = \substr($buffer, 10, 4);
+                $data  = \substr($buffer, 14);
+            } else {
+                $masks = \substr($buffer, 2, 4);
+                $data  = \substr($buffer, 6);
+            }
+        }
+        $dataLength = \strlen($data);
+        $masks = \str_repeat($masks, \floor($dataLength / 4)) . \substr($masks, 0, $dataLength % 4);
+        $decoded = $data ^ $masks;
+        if ($connection->websocketCurrentFrameLength) {
+            $connection->websocketDataBuffer .= $decoded;
+            return $connection->websocketDataBuffer;
+        } else {
+            if ($connection->websocketDataBuffer !== '') {
+                $decoded                         = $connection->websocketDataBuffer . $decoded;
+                $connection->websocketDataBuffer = '';
+            }
+            return $decoded;
+        }
+    }
+
+    /**
+     * Websocket handshake.
+     *
+     * @param string                              $buffer
+     * @param TcpConnection $connection
+     * @return int
+     */
+    public static function dealHandshake($buffer, TcpConnection $connection)
+    {
+        // HTTP protocol.
+        if (0 === \strpos($buffer, 'GET')) {
+            // Find \r\n\r\n.
+            $heder_end_pos = \strpos($buffer, "\r\n\r\n");
+            if (!$heder_end_pos) {
+                return 0;
+            }
+            $header_length = $heder_end_pos + 4;
+
+            // Get Sec-WebSocket-Key.
+            $Sec_WebSocket_Key = '';
+            if (\preg_match("/Sec-WebSocket-Key: *(.*?)\r\n/i", $buffer, $match)) {
+                $Sec_WebSocket_Key = $match[1];
+            } else {
+                $connection->send("HTTP/1.1 200 Websocket\r\nServer: workerman/".Worker::VERSION."\r\n\r\n<div style=\"text-align:center\"><h1>Websocket</h1><hr>powered by <a href=\"https://www.workerman.net\">workerman ".Worker::VERSION."</a></div>",
+                    true);
+                $connection->close();
+                return 0;
+            }
+            // Calculation websocket key.
+            $new_key = \base64_encode(\sha1($Sec_WebSocket_Key . "258EAFA5-E914-47DA-95CA-C5AB0DC85B11", true));
+            // Handshake response data.
+            $handshake_message = "HTTP/1.1 101 Switching Protocols\r\n"
+                                ."Upgrade: websocket\r\n"
+                                ."Sec-WebSocket-Version: 13\r\n"
+                                ."Connection: Upgrade\r\n"
+                                ."Sec-WebSocket-Accept: " . $new_key . "\r\n";
+
+            // Websocket data buffer.
+            $connection->websocketDataBuffer = '';
+            // Current websocket frame length.
+            $connection->websocketCurrentFrameLength = 0;
+            // Current websocket frame data.
+            $connection->websocketCurrentFrameBuffer = '';
+            // Consume handshake data.
+            $connection->consumeRecvBuffer($header_length);
+
+            // blob or arraybuffer
+            if (empty($connection->websocketType)) {
+                $connection->websocketType = static::BINARY_TYPE_BLOB;
+            }
+
+            $has_server_header = false;
+
+            // Try to emit onWebSocketConnect callback.
+            if (isset($connection->onWebSocketConnect) || isset($connection->worker->onWebSocketConnect)) {
+                static::parseHttpHeader($buffer);
+                try {
+                    \call_user_func(isset($connection->onWebSocketConnect)?$connection->onWebSocketConnect:$connection->worker->onWebSocketConnect, $connection, $buffer);
+                } catch (\Exception $e) {
+                    Worker::log($e);
+                    exit(250);
+                } catch (\Error $e) {
+                    Worker::log($e);
+                    exit(250);
+                }
+                if (!empty($_SESSION) && \class_exists('\GatewayWorker\Lib\Context')) {
+                    $connection->session = \GatewayWorker\Lib\Context::sessionEncode($_SESSION);
+                }
+                $_GET = $_SERVER = $_SESSION = $_COOKIE = array();
+
+                if (isset($connection->headers)) {
+                    if (\is_array($connection->headers))  {
+                        foreach ($connection->headers as $header) {
+                            if (\strpos($header, 'Server:') === 0) {
+                                $has_server_header = true;
+                            }
+                            $handshake_message .= "$header\r\n";
+                        }
+                    } else {
+                        $handshake_message .= "$connection->headers\r\n";
+                    }
+                }
+            }
+            if (!$has_server_header) {
+                $handshake_message .= "Server: workerman/".Worker::VERSION."\r\n";
+            }
+            $handshake_message .= "\r\n";
+            // Send handshake response.
+            $connection->send($handshake_message, true);
+            // Mark handshake complete..
+            $connection->websocketHandshake = true;
+            // There are data waiting to be sent.
+            if (!empty($connection->tmpWebsocketData)) {
+                $connection->send($connection->tmpWebsocketData, true);
+                $connection->tmpWebsocketData = '';
+            }
+            if (\strlen($buffer) > $header_length) {
+                return static::input(\substr($buffer, $header_length), $connection);
+            }
+            return 0;
+        } // Is flash policy-file-request.
+        elseif (0 === \strpos($buffer, '<polic')) {
+            $policy_xml = '<?xml version="1.0"?><cross-domain-policy><site-control permitted-cross-domain-policies="all"/><allow-access-from domain="*" to-ports="*"/></cross-domain-policy>' . "\0";
+            $connection->send($policy_xml, true);
+            $connection->consumeRecvBuffer(\strlen($buffer));
+            return 0;
+        }
+        // Bad websocket handshake request.
+        $connection->send("HTTP/1.1 200 Websocket\r\nServer: workerman/".Worker::VERSION."\r\n\r\n<div style=\"text-align:center\"><h1>Websocket</h1><hr>powered by <a href=\"https://www.workerman.net\">workerman ".Worker::VERSION."</a></div>",
+            true);
+        $connection->close();
+        return 0;
+    }
+
+    /**
+     * Parse http header.
+     *
+     * @param string $buffer
+     * @return void
+     */
+    protected static function parseHttpHeader($buffer)
+    {
+        // Parse headers.
+        list($http_header, ) = \explode("\r\n\r\n", $buffer, 2);
+        $header_data = \explode("\r\n", $http_header);
+
+        if ($_SERVER) {
+            $_SERVER = array();
+        }
+
+        list($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI'], $_SERVER['SERVER_PROTOCOL']) = \explode(' ',
+            $header_data[0]);
+
+        unset($header_data[0]);
+        foreach ($header_data as $content) {
+            // \r\n\r\n
+            if (empty($content)) {
+                continue;
+            }
+            list($key, $value)       = \explode(':', $content, 2);
+            $key                     = \str_replace('-', '_', \strtoupper($key));
+            $value                   = \trim($value);
+            $_SERVER['HTTP_' . $key] = $value;
+            switch ($key) {
+                // HTTP_HOST
+                case 'HOST':
+                    $tmp                    = \explode(':', $value);
+                    $_SERVER['SERVER_NAME'] = $tmp[0];
+                    if (isset($tmp[1])) {
+                        $_SERVER['SERVER_PORT'] = $tmp[1];
+                    }
+                    break;
+                // cookie
+                case 'COOKIE':
+                    \parse_str(\str_replace('; ', '&', $_SERVER['HTTP_COOKIE']), $_COOKIE);
+                    break;
+            }
+        }
+
+        // QUERY_STRING
+        $_SERVER['QUERY_STRING'] = \parse_url($_SERVER['REQUEST_URI'], \PHP_URL_QUERY);
+        if ($_SERVER['QUERY_STRING']) {
+            // $GET
+            \parse_str($_SERVER['QUERY_STRING'], $_GET);
+        } else {
+            $_SERVER['QUERY_STRING'] = '';
+        }
+    }
+}

+ 472 - 0
GatewayWorker/vendor/workerman/workerman/Protocols/Ws.php

@@ -0,0 +1,472 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link      http://www.workerman.net/
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Workerman\Protocols;
+
+use Workerman\Worker;
+use Workerman\Lib\Timer;
+use Workerman\Connection\TcpConnection;
+use Workerman\Connection\ConnectionInterface;
+
+/**
+ * Websocket protocol for client.
+ */
+class Ws
+{
+    /**
+     * Websocket blob type.
+     *
+     * @var string
+     */
+    const BINARY_TYPE_BLOB = "\x81";
+
+    /**
+     * Websocket arraybuffer type.
+     *
+     * @var string
+     */
+    const BINARY_TYPE_ARRAYBUFFER = "\x82";
+
+    /**
+     * Check the integrity of the package.
+     *
+     * @param string              $buffer
+     * @param ConnectionInterface $connection
+     * @return int
+     */
+    public static function input($buffer, ConnectionInterface $connection)
+    {
+        if (empty($connection->handshakeStep)) {
+            Worker::safeEcho("recv data before handshake. Buffer:" . \bin2hex($buffer) . "\n");
+            return false;
+        }
+        // Recv handshake response
+        if ($connection->handshakeStep === 1) {
+            return self::dealHandshake($buffer, $connection);
+        }
+        $recv_len = \strlen($buffer);
+        if ($recv_len < 2) {
+            return 0;
+        }
+        // Buffer websocket frame data.
+        if ($connection->websocketCurrentFrameLength) {
+            // We need more frame data.
+            if ($connection->websocketCurrentFrameLength > $recv_len) {
+                // Return 0, because it is not clear the full packet length, waiting for the frame of fin=1.
+                return 0;
+            }
+        } else {
+
+            $firstbyte    = \ord($buffer[0]);
+            $secondbyte   = \ord($buffer[1]);
+            $data_len     = $secondbyte & 127;
+            $is_fin_frame = $firstbyte >> 7;
+            $masked       = $secondbyte >> 7;
+
+            if ($masked) {
+                Worker::safeEcho("frame masked so close the connection\n");
+                $connection->close();
+                return 0;
+            }
+
+            $opcode       = $firstbyte & 0xf;
+
+            switch ($opcode) {
+                case 0x0:
+                    break;
+                // Blob type.
+                case 0x1:
+                    break;
+                // Arraybuffer type.
+                case 0x2:
+                    break;
+                // Close package.
+                case 0x8:
+                    // Try to emit onWebSocketClose callback.
+                    if (isset($connection->onWebSocketClose)) {
+                        try {
+                            \call_user_func($connection->onWebSocketClose, $connection);
+                        } catch (\Exception $e) {
+                            Worker::log($e);
+                            exit(250);
+                        } catch (\Error $e) {
+                            Worker::log($e);
+                            exit(250);
+                        }
+                    } // Close connection.
+                    else {
+                        $connection->close();
+                    }
+                    return 0;
+                // Ping package.
+                case 0x9:
+                    break;
+                // Pong package.
+                case 0xa:
+                    break;
+                // Wrong opcode.
+                default :
+                    Worker::safeEcho("error opcode $opcode and close websocket connection. Buffer:" . $buffer . "\n");
+                    $connection->close();
+                    return 0;
+            }
+            // Calculate packet length.
+            if ($data_len === 126) {
+                if (\strlen($buffer) < 4) {
+                    return 0;
+                }
+                $pack = \unpack('nn/ntotal_len', $buffer);
+                $current_frame_length = $pack['total_len'] + 4;
+            } else if ($data_len === 127) {
+                if (\strlen($buffer) < 10) {
+                    return 0;
+                }
+                $arr = \unpack('n/N2c', $buffer);
+                $current_frame_length = $arr['c1']*4294967296 + $arr['c2'] + 10;
+            } else {
+                $current_frame_length = $data_len + 2;
+            }
+
+            $total_package_size = \strlen($connection->websocketDataBuffer) + $current_frame_length;
+            if ($total_package_size > $connection->maxPackageSize) {
+                Worker::safeEcho("error package. package_length=$total_package_size\n");
+                $connection->close();
+                return 0;
+            }
+
+            if ($is_fin_frame) {
+                if ($opcode === 0x9) {
+                    if ($recv_len >= $current_frame_length) {
+                        $ping_data = static::decode(\substr($buffer, 0, $current_frame_length), $connection);
+                        $connection->consumeRecvBuffer($current_frame_length);
+                        $tmp_connection_type = isset($connection->websocketType) ? $connection->websocketType : static::BINARY_TYPE_BLOB;
+                        $connection->websocketType = "\x8a";
+                        if (isset($connection->onWebSocketPing)) {
+                            try {
+                                \call_user_func($connection->onWebSocketPing, $connection, $ping_data);
+                            } catch (\Exception $e) {
+                                Worker::log($e);
+                                exit(250);
+                            } catch (\Error $e) {
+                                Worker::log($e);
+                                exit(250);
+                            }
+                        } else {
+                            $connection->send($ping_data);
+                        }
+                        $connection->websocketType = $tmp_connection_type;
+                        if ($recv_len > $current_frame_length) {
+                            return static::input(\substr($buffer, $current_frame_length), $connection);
+                        }
+                    }
+                    return 0;
+
+                } else if ($opcode === 0xa) {
+                    if ($recv_len >= $current_frame_length) {
+                        $pong_data = static::decode(\substr($buffer, 0, $current_frame_length), $connection);
+                        $connection->consumeRecvBuffer($current_frame_length);
+                        $tmp_connection_type = isset($connection->websocketType) ? $connection->websocketType : static::BINARY_TYPE_BLOB;
+                        $connection->websocketType = "\x8a";
+                        // Try to emit onWebSocketPong callback.
+                        if (isset($connection->onWebSocketPong)) {
+                            try {
+                                \call_user_func($connection->onWebSocketPong, $connection, $pong_data);
+                            } catch (\Exception $e) {
+                                Worker::log($e);
+                                exit(250);
+                            } catch (\Error $e) {
+                                Worker::log($e);
+                                exit(250);
+                            }
+                        }
+                        $connection->websocketType = $tmp_connection_type;
+                        if ($recv_len > $current_frame_length) {
+                            return static::input(\substr($buffer, $current_frame_length), $connection);
+                        }
+                    }
+                    return 0;
+                }
+                return $current_frame_length;
+            } else {
+                $connection->websocketCurrentFrameLength = $current_frame_length;
+            }
+        }
+        // Received just a frame length data.
+        if ($connection->websocketCurrentFrameLength === $recv_len) {
+            self::decode($buffer, $connection);
+            $connection->consumeRecvBuffer($connection->websocketCurrentFrameLength);
+            $connection->websocketCurrentFrameLength = 0;
+            return 0;
+        } // The length of the received data is greater than the length of a frame.
+        elseif ($connection->websocketCurrentFrameLength < $recv_len) {
+            self::decode(\substr($buffer, 0, $connection->websocketCurrentFrameLength), $connection);
+            $connection->consumeRecvBuffer($connection->websocketCurrentFrameLength);
+            $current_frame_length                    = $connection->websocketCurrentFrameLength;
+            $connection->websocketCurrentFrameLength = 0;
+            // Continue to read next frame.
+            return self::input(\substr($buffer, $current_frame_length), $connection);
+        } // The length of the received data is less than the length of a frame.
+        else {
+            return 0;
+        }
+    }
+
+    /**
+     * Websocket encode.
+     *
+     * @param string              $buffer
+     * @param ConnectionInterface $connection
+     * @return string
+     */
+    public static function encode($payload, ConnectionInterface $connection)
+    {
+        if (empty($connection->websocketType)) {
+            $connection->websocketType = self::BINARY_TYPE_BLOB;
+        }
+        $payload = (string)$payload;
+        if (empty($connection->handshakeStep)) {
+            static::sendHandshake($connection);
+        }
+        $mask = 1;
+        $mask_key = "\x00\x00\x00\x00";
+
+        $pack = '';
+        $length = $length_flag = \strlen($payload);
+        if (65535 < $length) {
+            $pack   = \pack('NN', ($length & 0xFFFFFFFF00000000) >> 32, $length & 0x00000000FFFFFFFF);
+            $length_flag = 127;
+        } else if (125 < $length) {
+            $pack   = \pack('n*', $length);
+            $length_flag = 126;
+        }
+
+        $head = ($mask << 7) | $length_flag;
+        $head = $connection->websocketType . \chr($head) . $pack;
+
+        $frame = $head . $mask_key;
+        // append payload to frame:
+        $mask_key = \str_repeat($mask_key, \floor($length / 4)) . \substr($mask_key, 0, $length % 4);
+        $frame .= $payload ^ $mask_key;
+        if ($connection->handshakeStep === 1) {
+            // If buffer has already full then discard the current package.
+            if (\strlen($connection->tmpWebsocketData) > $connection->maxSendBufferSize) {
+                if ($connection->onError) {
+                    try {
+                        \call_user_func($connection->onError, $connection, \WORKERMAN_SEND_FAIL, 'send buffer full and drop package');
+                    } catch (\Exception $e) {
+                        Worker::log($e);
+                        exit(250);
+                    } catch (\Error $e) {
+                        Worker::log($e);
+                        exit(250);
+                    }
+                }
+                return '';
+            }
+            $connection->tmpWebsocketData = $connection->tmpWebsocketData . $frame;
+            // Check buffer is full.
+            if ($connection->maxSendBufferSize <= \strlen($connection->tmpWebsocketData)) {
+                if ($connection->onBufferFull) {
+                    try {
+                        \call_user_func($connection->onBufferFull, $connection);
+                    } catch (\Exception $e) {
+                        Worker::log($e);
+                        exit(250);
+                    } catch (\Error $e) {
+                        Worker::log($e);
+                        exit(250);
+                    }
+                }
+            }
+            return '';
+        }
+        return $frame;
+    }
+
+    /**
+     * Websocket decode.
+     *
+     * @param string              $buffer
+     * @param ConnectionInterface $connection
+     * @return string
+     */
+    public static function decode($bytes, ConnectionInterface $connection)
+    {
+        $data_length = \ord($bytes[1]);
+
+        if ($data_length === 126) {
+            $decoded_data = \substr($bytes, 4);
+        } else if ($data_length === 127) {
+            $decoded_data = \substr($bytes, 10);
+        } else {
+            $decoded_data = \substr($bytes, 2);
+        }
+        if ($connection->websocketCurrentFrameLength) {
+            $connection->websocketDataBuffer .= $decoded_data;
+            return $connection->websocketDataBuffer;
+        } else {
+            if ($connection->websocketDataBuffer !== '') {
+                $decoded_data                    = $connection->websocketDataBuffer . $decoded_data;
+                $connection->websocketDataBuffer = '';
+            }
+            return $decoded_data;
+        }
+    }
+
+    /**
+     * Send websocket handshake data.
+     *
+     * @return void
+     */
+    public static function onConnect($connection)
+    {
+        static::sendHandshake($connection);
+    }
+
+    /**
+     * Clean
+     *
+     * @param $connection
+     */
+    public static function onClose($connection)
+    {
+        $connection->handshakeStep               = null;
+        $connection->websocketCurrentFrameLength = 0;
+        $connection->tmpWebsocketData            = '';
+        $connection->websocketDataBuffer         = '';
+        if (!empty($connection->websocketPingTimer)) {
+            Timer::del($connection->websocketPingTimer);
+            $connection->websocketPingTimer = null;
+        }
+    }
+
+    /**
+     * Send websocket handshake.
+     *
+     * @param TcpConnection $connection
+     * @return void
+     */
+    public static function sendHandshake(TcpConnection $connection)
+    {
+        if (!empty($connection->handshakeStep)) {
+            return;
+        }
+        // Get Host.
+        $port = $connection->getRemotePort();
+        $host = $port === 80 ? $connection->getRemoteHost() : $connection->getRemoteHost() . ':' . $port;
+        // Handshake header.
+        $connection->websocketSecKey = \base64_encode(\md5(\mt_rand(), true));
+        $user_header = isset($connection->headers) ? $connection->headers :
+            (isset($connection->wsHttpHeader) ? $connection->wsHttpHeader : null);
+        $user_header_str = '';
+        if (!empty($user_header)) {
+            if (\is_array($user_header)){
+                foreach($user_header as $k=>$v){
+                    $user_header_str .= "$k: $v\r\n";
+                }
+            } else {
+                $user_header_str .= $user_header;
+            }
+            $user_header_str = "\r\n".\trim($user_header_str);
+        }
+        $header = 'GET ' . $connection->getRemoteURI() . " HTTP/1.1\r\n".
+        (!\preg_match("/\nHost:/i", $user_header_str) ? "Host: $host\r\n" : '').
+        "Connection: Upgrade\r\n".
+        "Upgrade: websocket\r\n".
+        (isset($connection->websocketOrigin) ? "Origin: ".$connection->websocketOrigin."\r\n":'').
+        (isset($connection->WSClientProtocol)?"Sec-WebSocket-Protocol: ".$connection->WSClientProtocol."\r\n":'').
+        "Sec-WebSocket-Version: 13\r\n".
+        "Sec-WebSocket-Key: " . $connection->websocketSecKey . $user_header_str . "\r\n\r\n";
+        $connection->send($header, true);
+        $connection->handshakeStep               = 1;
+        $connection->websocketCurrentFrameLength = 0;
+        $connection->websocketDataBuffer         = '';
+        $connection->tmpWebsocketData            = '';
+    }
+
+    /**
+     * Websocket handshake.
+     *
+     * @param string                              $buffer
+     * @param TcpConnection $connection
+     * @return int
+     */
+    public static function dealHandshake($buffer, TcpConnection $connection)
+    {
+        $pos = \strpos($buffer, "\r\n\r\n");
+        if ($pos) {
+            //checking Sec-WebSocket-Accept
+            if (\preg_match("/Sec-WebSocket-Accept: *(.*?)\r\n/i", $buffer, $match)) {
+                if ($match[1] !== \base64_encode(\sha1($connection->websocketSecKey . "258EAFA5-E914-47DA-95CA-C5AB0DC85B11", true))) {
+                    Worker::safeEcho("Sec-WebSocket-Accept not match. Header:\n" . \substr($buffer, 0, $pos) . "\n");
+                    $connection->close();
+                    return 0;
+                }
+            } else {
+                Worker::safeEcho("Sec-WebSocket-Accept not found. Header:\n" . \substr($buffer, 0, $pos) . "\n");
+                $connection->close();
+                return 0;
+            }
+
+            // handshake complete
+
+            // Get WebSocket subprotocol (if specified by server)
+            if (\preg_match("/Sec-WebSocket-Protocol: *(.*?)\r\n/i", $buffer, $match)) {
+                $connection->WSServerProtocol = \trim($match[1]);
+            }
+
+            $connection->handshakeStep = 2;
+            $handshake_response_length = $pos + 4;
+            // Try to emit onWebSocketConnect callback.
+            if (isset($connection->onWebSocketConnect)) {
+                try {
+                    \call_user_func($connection->onWebSocketConnect, $connection, \substr($buffer, 0, $handshake_response_length));
+                } catch (\Exception $e) {
+                    Worker::log($e);
+                    exit(250);
+                } catch (\Error $e) {
+                    Worker::log($e);
+                    exit(250);
+                }
+            }
+            // Headbeat.
+            if (!empty($connection->websocketPingInterval)) {
+                $connection->websocketPingTimer = Timer::add($connection->websocketPingInterval, function() use ($connection){
+                    if (false === $connection->send(\pack('H*', '898000000000'), true)) {
+                        Timer::del($connection->websocketPingTimer);
+                        $connection->websocketPingTimer = null;
+                    }
+                });
+            }
+
+            $connection->consumeRecvBuffer($handshake_response_length);
+            if (!empty($connection->tmpWebsocketData)) {
+                $connection->send($connection->tmpWebsocketData, true);
+                $connection->tmpWebsocketData = '';
+            }
+            if (\strlen($buffer) > $handshake_response_length) {
+                return self::input(\substr($buffer, $handshake_response_length), $connection);
+            }
+        }
+        return 0;
+    }
+
+    public static function WSSetProtocol($connection, $params) {
+	$connection->WSClientProtocol = $params[0];
+    }
+
+    public static function WSGetServerProtocol($connection) {
+	return (\property_exists($connection, 'WSServerProtocol') ? $connection->WSServerProtocol : null);
+    }
+
+}

+ 386 - 0
GatewayWorker/vendor/workerman/workerman/README.md

@@ -0,0 +1,386 @@
+# Workerman
+[![Gitter](https://badges.gitter.im/walkor/Workerman.svg)](https://gitter.im/walkor/Workerman?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=body_badge)
+[![Latest Stable Version](https://poser.pugx.org/workerman/workerman/v/stable)](https://packagist.org/packages/workerman/workerman)
+[![Total Downloads](https://poser.pugx.org/workerman/workerman/downloads)](https://packagist.org/packages/workerman/workerman)
+[![Monthly Downloads](https://poser.pugx.org/workerman/workerman/d/monthly)](https://packagist.org/packages/workerman/workerman)
+[![Daily Downloads](https://poser.pugx.org/workerman/workerman/d/daily)](https://packagist.org/packages/workerman/workerman)
+[![License](https://poser.pugx.org/workerman/workerman/license)](https://packagist.org/packages/workerman/workerman)
+
+## What is it
+Workerman is an asynchronous event-driven PHP framework with high performance to build fast and scalable network applications. 
+Workerman supports HTTP, Websocket, SSL and other custom protocols. 
+Workerman supports event extension.
+
+## Requires
+PHP 5.3 or Higher  
+A POSIX compatible operating system (Linux, OSX, BSD)  
+POSIX and PCNTL extensions required   
+Event extension recommended for better performance  
+
+## Installation
+
+```
+composer require workerman/workerman
+```
+
+## Basic Usage
+
+### A websocket server 
+```php
+<?php
+
+use Workerman\Worker;
+
+require_once __DIR__ . '/vendor/autoload.php';
+
+// Create a Websocket server
+$ws_worker = new Worker('websocket://0.0.0.0:2346');
+
+// 4 processes
+$ws_worker->count = 4;
+
+// Emitted when new connection come
+$ws_worker->onConnect = function ($connection) {
+    echo "New connection\n";
+};
+
+// Emitted when data received
+$ws_worker->onMessage = function ($connection, $data) {
+    // Send hello $data
+    $connection->send('Hello ' . $data);
+};
+
+// Emitted when connection closed
+$ws_worker->onClose = function ($connection) {
+    echo "Connection closed\n";
+};
+
+// Run worker
+Worker::runAll();
+```
+
+### An http server
+```php
+use Workerman\Worker;
+
+require_once __DIR__ . '/vendor/autoload.php';
+
+// #### http worker ####
+$http_worker = new Worker('http://0.0.0.0:2345');
+
+// 4 processes
+$http_worker->count = 4;
+
+// Emitted when data received
+$http_worker->onMessage = function ($connection, $request) {
+    //$request->get();
+    //$request->post();
+    //$request->header();
+    //$request->cookie();
+    //$requset->session();
+    //$request->uri();
+    //$request->path();
+    //$request->method();
+
+    // Send data to client
+    $connection->send("Hello World");
+};
+
+// Run all workers
+Worker::runAll();
+```
+
+### A tcp server
+```php
+use Workerman\Worker;
+
+require_once __DIR__ . '/vendor/autoload.php';
+
+// #### create socket and listen 1234 port ####
+$tcp_worker = new Worker('tcp://0.0.0.0:1234');
+
+// 4 processes
+$tcp_worker->count = 4;
+
+// Emitted when new connection come
+$tcp_worker->onConnect = function ($connection) {
+    echo "New Connection\n";
+};
+
+// Emitted when data received
+$tcp_worker->onMessage = function ($connection, $data) {
+    // Send data to client
+    $connection->send("Hello $data \n");
+};
+
+// Emitted when new connection come
+$tcp_worker->onClose = function ($connection) {
+    echo "Connection closed\n";
+};
+
+Worker::runAll();
+```
+
+### Enable SSL
+```php
+<?php
+
+use Workerman\Worker;
+
+require_once __DIR__ . '/vendor/autoload.php';
+
+// SSL context.
+$context = array(
+    'ssl' => array(
+        'local_cert'  => '/your/path/of/server.pem',
+        'local_pk'    => '/your/path/of/server.key',
+        'verify_peer' => false,
+    )
+);
+
+// Create a Websocket server with ssl context.
+$ws_worker = new Worker('websocket://0.0.0.0:2346', $context);
+
+// Enable SSL. WebSocket+SSL means that Secure WebSocket (wss://). 
+// The similar approaches for Https etc.
+$ws_worker->transport = 'ssl';
+
+$ws_worker->onMessage = function ($connection, $data) {
+    // Send hello $data
+    $connection->send('Hello ' . $data);
+};
+
+Worker::runAll();
+```
+
+### Custom protocol
+Protocols/MyTextProtocol.php
+```php
+
+namespace Protocols;
+
+/**
+ * User defined protocol
+ * Format Text+"\n"
+ */
+class MyTextProtocol
+{
+    public static function input($recv_buffer)
+    {
+        // Find the position of the first occurrence of "\n"
+        $pos = strpos($recv_buffer, "\n");
+
+        // Not a complete package. Return 0 because the length of package can not be calculated
+        if ($pos === false) {
+            return 0;
+        }
+
+        // Return length of the package
+        return $pos+1;
+    }
+
+    public static function decode($recv_buffer)
+    {
+        return trim($recv_buffer);
+    }
+
+    public static function encode($data)
+    {
+        return $data . "\n";
+    }
+}
+```
+
+```php
+use Workerman\Worker;
+
+require_once __DIR__ . '/vendor/autoload.php';
+
+// #### MyTextProtocol worker ####
+$text_worker = new Worker('MyTextProtocol://0.0.0.0:5678');
+
+$text_worker->onConnect = function ($connection) {
+    echo "New connection\n";
+};
+
+$text_worker->onMessage = function ($connection, $data) {
+    // Send data to client
+    $connection->send("Hello world\n");
+};
+
+$text_worker->onClose = function ($connection) {
+    echo "Connection closed\n";
+};
+
+// Run all workers
+Worker::runAll();
+```
+
+### Timer
+```php
+
+use Workerman\Worker;
+use Workerman\Timer;
+
+require_once __DIR__ . '/vendor/autoload.php';
+
+$task = new Worker();
+$task->onWorkerStart = function ($task) {
+    // 2.5 seconds
+    $time_interval = 2.5; 
+    $timer_id = Timer::add($time_interval, function () {
+        echo "Timer run\n";
+    });
+};
+
+// Run all workers
+Worker::runAll();
+```
+
+### AsyncTcpConnection (tcp/ws/text/frame etc...)
+```php
+
+use Workerman\Worker;
+use Workerman\Connection\AsyncTcpConnection;
+
+require_once __DIR__ . '/vendor/autoload.php';
+
+$worker = new Worker();
+$worker->onWorkerStart = function () {
+    // Websocket protocol for client.
+    $ws_connection = new AsyncTcpConnection('ws://echo.websocket.org:80');
+    $ws_connection->onConnect = function ($connection) {
+        $connection->send('Hello');
+    };
+    $ws_connection->onMessage = function ($connection, $data) {
+        echo "Recv: $data\n";
+    };
+    $ws_connection->onError = function ($connection, $code, $msg) {
+        echo "Error: $msg\n";
+    };
+    $ws_connection->onClose = function ($connection) {
+        echo "Connection closed\n";
+    };
+    $ws_connection->connect();
+};
+
+Worker::runAll();
+```
+
+
+
+## Available commands
+```php start.php start  ```  
+```php start.php start -d  ```  
+![workerman start](http://www.workerman.net/img/workerman-start.png)  
+```php start.php status  ```  
+![workerman satus](http://www.workerman.net/img/workerman-status.png?a=123)  
+```php start.php connections```  
+```php start.php stop  ```  
+```php start.php restart  ```  
+```php start.php reload  ```  
+
+## Documentation
+
+中文主页:[http://www.workerman.net](http://www.workerman.net)
+
+中文文档: [http://doc.workerman.net](http://doc.workerman.net)
+
+Documentation:[https://github.com/walkor/workerman-manual](https://github.com/walkor/workerman-manual/blob/master/english/src/SUMMARY.md)
+
+# Benchmarks
+```
+CPU:      Intel(R) Core(TM) i3-3220 CPU @ 3.30GHz and 4 processors totally
+Memory:   8G
+OS:       Ubuntu 14.04 LTS
+Software: ab
+PHP:      5.5.9
+```
+
+**Codes**
+```php
+<?php
+use Workerman\Worker;
+
+$worker = new Worker('tcp://0.0.0.0:1234');
+$worker->count = 3;
+$worker->onMessage = function ($connection, $data) {
+    $connection->send("HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nServer: workerman\r\nContent-Length: 5\r\n\r\nhello");
+};
+
+Worker::runAll();
+```
+**Result**
+
+```shell
+ab -n1000000 -c100 -k http://127.0.0.1:1234/
+This is ApacheBench, Version 2.3 <$Revision: 1528965 $>
+Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
+Licensed to The Apache Software Foundation, http://www.apache.org/
+
+Benchmarking 127.0.0.1 (be patient)
+Completed 100000 requests
+Completed 200000 requests
+Completed 300000 requests
+Completed 400000 requests
+Completed 500000 requests
+Completed 600000 requests
+Completed 700000 requests
+Completed 800000 requests
+Completed 900000 requests
+Completed 1000000 requests
+Finished 1000000 requests
+
+
+Server Software:        workerman/3.1.4
+Server Hostname:        127.0.0.1
+Server Port:            1234
+
+Document Path:          /
+Document Length:        5 bytes
+
+Concurrency Level:      100
+Time taken for tests:   7.240 seconds
+Complete requests:      1000000
+Failed requests:        0
+Keep-Alive requests:    1000000
+Total transferred:      73000000 bytes
+HTML transferred:       5000000 bytes
+Requests per second:    138124.14 [#/sec] (mean)
+Time per request:       0.724 [ms] (mean)
+Time per request:       0.007 [ms] (mean, across all concurrent requests)
+Transfer rate:          9846.74 [Kbytes/sec] received
+
+Connection Times (ms)
+              min  mean[+/-sd] median   max
+Connect:        0    0   0.0      0       5
+Processing:     0    1   0.2      1       9
+Waiting:        0    1   0.2      1       9
+Total:          0    1   0.2      1       9
+
+Percentage of the requests served within a certain time (ms)
+  50%      1
+  66%      1
+  75%      1
+  80%      1
+  90%      1
+  95%      1
+  98%      1
+  99%      1
+ 100%      9 (longest request)
+
+```
+
+
+## Other links with workerman
+
+[PHPSocket.IO](https://github.com/walkor/phpsocket.io)   
+[php-socks5](https://github.com/walkor/php-socks5)  
+[php-http-proxy](https://github.com/walkor/php-http-proxy)  
+
+## Donate
+<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=UQGGS9UB35WWG"><img src="http://donate.workerman.net/img/donate.png"></a>
+
+## LICENSE
+
+Workerman is released under the [MIT license](https://github.com/walkor/workerman/blob/master/MIT-LICENSE.txt).

+ 213 - 0
GatewayWorker/vendor/workerman/workerman/Timer.php

@@ -0,0 +1,213 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link      http://www.workerman.net/
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Workerman;
+
+use Workerman\Events\EventInterface;
+use Workerman\Worker;
+use \Exception;
+
+/**
+ * Timer.
+ *
+ * example:
+ * Workerman\Timer::add($time_interval, callback, array($arg1, $arg2..));
+ */
+class Timer
+{
+    /**
+     * Tasks that based on ALARM signal.
+     * [
+     *   run_time => [[$func, $args, $persistent, time_interval],[$func, $args, $persistent, time_interval],..]],
+     *   run_time => [[$func, $args, $persistent, time_interval],[$func, $args, $persistent, time_interval],..]],
+     *   ..
+     * ]
+     *
+     * @var array
+     */
+    protected static $_tasks = array();
+
+    /**
+     * event
+     *
+     * @var EventInterface
+     */
+    protected static $_event = null;
+
+    /**
+     * timer id
+     *
+     * @var int
+     */
+    protected static $_timerId = 0;
+
+    /**
+     * timer status
+     * [
+     *   timer_id1 => bool,
+     *   timer_id2 => bool,
+     *   ....................,
+     * ]
+     *
+     * @var array
+     */
+    protected static $_status = array();
+
+    /**
+     * Init.
+     *
+     * @param EventInterface $event
+     * @return void
+     */
+    public static function init($event = null)
+    {
+        if ($event) {
+            self::$_event = $event;
+            return;
+        }
+        if (\function_exists('pcntl_signal')) {
+            \pcntl_signal(\SIGALRM, array('\Workerman\Lib\Timer', 'signalHandle'), false);
+        }
+    }
+
+    /**
+     * ALARM signal handler.
+     *
+     * @return void
+     */
+    public static function signalHandle()
+    {
+        if (!self::$_event) {
+            \pcntl_alarm(1);
+            self::tick();
+        }
+    }
+
+    /**
+     * Add a timer.
+     *
+     * @param float    $time_interval
+     * @param callable $func
+     * @param mixed    $args
+     * @param bool     $persistent
+     * @return int|bool
+     */
+    public static function add($time_interval, $func, $args = array(), $persistent = true)
+    {
+        if ($time_interval <= 0) {
+            Worker::safeEcho(new Exception("bad time_interval"));
+            return false;
+        }
+
+        if ($args === null) {
+            $args = array();
+        }
+
+        if (self::$_event) {
+            return self::$_event->add($time_interval,
+                $persistent ? EventInterface::EV_TIMER : EventInterface::EV_TIMER_ONCE, $func, $args);
+        }
+
+        if (!\is_callable($func)) {
+            Worker::safeEcho(new Exception("not callable"));
+            return false;
+        }
+
+        if (empty(self::$_tasks)) {
+            \pcntl_alarm(1);
+        }
+
+        $run_time = \time() + $time_interval;
+        if (!isset(self::$_tasks[$run_time])) {
+            self::$_tasks[$run_time] = array();
+        }
+
+        self::$_timerId = self::$_timerId == \PHP_INT_MAX ? 1 : ++self::$_timerId;
+        self::$_status[self::$_timerId] = true;
+        self::$_tasks[$run_time][self::$_timerId] = array($func, (array)$args, $persistent, $time_interval);
+
+        return self::$_timerId;
+    }
+
+
+    /**
+     * Tick.
+     *
+     * @return void
+     */
+    public static function tick()
+    {
+        if (empty(self::$_tasks)) {
+            \pcntl_alarm(0);
+            return;
+        }
+        $time_now = \time();
+        foreach (self::$_tasks as $run_time => $task_data) {
+            if ($time_now >= $run_time) {
+                foreach ($task_data as $index => $one_task) {
+                    $task_func     = $one_task[0];
+                    $task_args     = $one_task[1];
+                    $persistent    = $one_task[2];
+                    $time_interval = $one_task[3];
+                    try {
+                        \call_user_func_array($task_func, $task_args);
+                    } catch (\Exception $e) {
+                        Worker::safeEcho($e);
+                    }
+                    if($persistent && !empty(self::$_status[$index])) {
+                        $new_run_time = \time() + $time_interval;
+                        if(!isset(self::$_tasks[$new_run_time])) self::$_tasks[$new_run_time] = array();
+                        self::$_tasks[$new_run_time][$index] = array($task_func, (array)$task_args, $persistent, $time_interval);
+                    }
+                }
+                unset(self::$_tasks[$run_time]);
+            }
+        }
+    }
+
+    /**
+     * Remove a timer.
+     *
+     * @param mixed $timer_id
+     * @return bool
+     */
+    public static function del($timer_id)
+    {
+        if (self::$_event) {
+            return self::$_event->del($timer_id, EventInterface::EV_TIMER);
+        }
+
+        foreach(self::$_tasks as $run_time => $task_data) 
+        {
+            if(array_key_exists($timer_id, $task_data)) unset(self::$_tasks[$run_time][$timer_id]);
+        }
+
+        if(array_key_exists($timer_id, self::$_status)) unset(self::$_status[$timer_id]);
+
+        return true;
+    }
+
+    /**
+     * Remove all timers.
+     *
+     * @return void
+     */
+    public static function delAll()
+    {
+        self::$_tasks = self::$_status = array();
+        \pcntl_alarm(0);
+        if (self::$_event) {
+            self::$_event->clearAllTimer();
+        }
+    }
+}

+ 2541 - 0
GatewayWorker/vendor/workerman/workerman/Worker.php

@@ -0,0 +1,2541 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link      http://www.workerman.net/
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Workerman;
+require_once __DIR__ . '/Lib/Constants.php';
+
+use Workerman\Events\EventInterface;
+use Workerman\Connection\ConnectionInterface;
+use Workerman\Connection\TcpConnection;
+use Workerman\Connection\UdpConnection;
+use Workerman\Lib\Timer;
+use Workerman\Events\Select;
+use \Exception;
+
+/**
+ * Worker class
+ * A container for listening ports
+ */
+class Worker
+{
+    /**
+     * Version.
+     *
+     * @var string
+     */
+    const VERSION = '4.0.6';
+
+    /**
+     * Status starting.
+     *
+     * @var int
+     */
+    const STATUS_STARTING = 1;
+
+    /**
+     * Status running.
+     *
+     * @var int
+     */
+    const STATUS_RUNNING = 2;
+
+    /**
+     * Status shutdown.
+     *
+     * @var int
+     */
+    const STATUS_SHUTDOWN = 4;
+
+    /**
+     * Status reloading.
+     *
+     * @var int
+     */
+    const STATUS_RELOADING = 8;
+
+    /**
+     * After sending the restart command to the child process KILL_WORKER_TIMER_TIME seconds,
+     * if the process is still living then forced to kill.
+     *
+     * @var int
+     */
+    const KILL_WORKER_TIMER_TIME = 2;
+
+    /**
+     * Default backlog. Backlog is the maximum length of the queue of pending connections.
+     *
+     * @var int
+     */
+    const DEFAULT_BACKLOG = 102400;
+    /**
+     * Max udp package size.
+     *
+     * @var int
+     */
+    const MAX_UDP_PACKAGE_SIZE = 65535;
+
+    /**
+     * The safe distance for columns adjacent
+     *
+     * @var int
+     */
+    const UI_SAFE_LENGTH = 4;
+
+    /**
+     * Worker id.
+     *
+     * @var int
+     */
+    public $id = 0;
+
+    /**
+     * Name of the worker processes.
+     *
+     * @var string
+     */
+    public $name = 'none';
+
+    /**
+     * Number of worker processes.
+     *
+     * @var int
+     */
+    public $count = 1;
+
+    /**
+     * Unix user of processes, needs appropriate privileges (usually root).
+     *
+     * @var string
+     */
+    public $user = '';
+
+    /**
+     * Unix group of processes, needs appropriate privileges (usually root).
+     *
+     * @var string
+     */
+    public $group = '';
+
+    /**
+     * reloadable.
+     *
+     * @var bool
+     */
+    public $reloadable = true;
+
+    /**
+     * reuse port.
+     *
+     * @var bool
+     */
+    public $reusePort = false;
+
+    /**
+     * Emitted when worker processes start.
+     *
+     * @var callable
+     */
+    public $onWorkerStart = null;
+
+    /**
+     * Emitted when a socket connection is successfully established.
+     *
+     * @var callable
+     */
+    public $onConnect = null;
+
+    /**
+     * Emitted when data is received.
+     *
+     * @var callable
+     */
+    public $onMessage = null;
+
+    /**
+     * Emitted when the other end of the socket sends a FIN packet.
+     *
+     * @var callable
+     */
+    public $onClose = null;
+
+    /**
+     * Emitted when an error occurs with connection.
+     *
+     * @var callable
+     */
+    public $onError = null;
+
+    /**
+     * Emitted when the send buffer becomes full.
+     *
+     * @var callable
+     */
+    public $onBufferFull = null;
+
+    /**
+     * Emitted when the send buffer becomes empty.
+     *
+     * @var callable
+     */
+    public $onBufferDrain = null;
+
+    /**
+     * Emitted when worker processes stoped.
+     *
+     * @var callable
+     */
+    public $onWorkerStop = null;
+
+    /**
+     * Emitted when worker processes get reload signal.
+     *
+     * @var callable
+     */
+    public $onWorkerReload = null;
+
+    /**
+     * Transport layer protocol.
+     *
+     * @var string
+     */
+    public $transport = 'tcp';
+
+    /**
+     * Store all connections of clients.
+     *
+     * @var array
+     */
+    public $connections = array();
+
+    /**
+     * Application layer protocol.
+     *
+     * @var string
+     */
+    public $protocol = null;
+
+    /**
+     * Root path for autoload.
+     *
+     * @var string
+     */
+    protected $_autoloadRootPath = '';
+
+    /**
+     * Pause accept new connections or not.
+     *
+     * @var bool
+     */
+    protected $_pauseAccept = true;
+
+    /**
+     * Is worker stopping ?
+     * @var bool
+     */
+    public $stopping = false;
+
+    /**
+     * Daemonize.
+     *
+     * @var bool
+     */
+    public static $daemonize = false;
+
+    /**
+     * Stdout file.
+     *
+     * @var string
+     */
+    public static $stdoutFile = '/dev/null';
+
+    /**
+     * The file to store master process PID.
+     *
+     * @var string
+     */
+    public static $pidFile = '';
+
+    /**
+     * Log file.
+     *
+     * @var mixed
+     */
+    public static $logFile = '';
+
+    /**
+     * Global event loop.
+     *
+     * @var EventInterface
+     */
+    public static $globalEvent = null;
+
+    /**
+     * Emitted when the master process get reload signal.
+     *
+     * @var callable
+     */
+    public static $onMasterReload = null;
+
+    /**
+     * Emitted when the master process terminated.
+     *
+     * @var callable
+     */
+    public static $onMasterStop = null;
+
+    /**
+     * EventLoopClass
+     *
+     * @var string
+     */
+    public static $eventLoopClass = '';
+
+    /**
+     * Process title
+     *
+     * @var string
+     */
+    public static $processTitle = 'WorkerMan';
+
+    /**
+     * The PID of master process.
+     *
+     * @var int
+     */
+    protected static $_masterPid = 0;
+
+    /**
+     * Listening socket.
+     *
+     * @var resource
+     */
+    protected $_mainSocket = null;
+
+    /**
+     * Socket name. The format is like this http://0.0.0.0:80 .
+     *
+     * @var string
+     */
+    protected $_socketName = '';
+
+    /** parse from _socketName avoid parse again in master or worker
+     * LocalSocket The format is like tcp://0.0.0.0:8080
+     * @var string
+     */
+
+    protected $_localSocket=null;
+
+    /**
+     * Context of socket.
+     *
+     * @var resource
+     */
+    protected $_context = null;
+
+    /**
+     * All worker instances.
+     *
+     * @var Worker[]
+     */
+    protected static $_workers = array();
+
+    /**
+     * All worker processes pid.
+     * The format is like this [worker_id=>[pid=>pid, pid=>pid, ..], ..]
+     *
+     * @var array
+     */
+    protected static $_pidMap = array();
+
+    /**
+     * All worker processes waiting for restart.
+     * The format is like this [pid=>pid, pid=>pid].
+     *
+     * @var array
+     */
+    protected static $_pidsToRestart = array();
+
+    /**
+     * Mapping from PID to worker process ID.
+     * The format is like this [worker_id=>[0=>$pid, 1=>$pid, ..], ..].
+     *
+     * @var array
+     */
+    protected static $_idMap = array();
+
+    /**
+     * Current status.
+     *
+     * @var int
+     */
+    protected static $_status = self::STATUS_STARTING;
+
+    /**
+     * Maximum length of the worker names.
+     *
+     * @var int
+     */
+    protected static $_maxWorkerNameLength = 12;
+
+    /**
+     * Maximum length of the socket names.
+     *
+     * @var int
+     */
+    protected static $_maxSocketNameLength = 12;
+
+    /**
+     * Maximum length of the process user names.
+     *
+     * @var int
+     */
+    protected static $_maxUserNameLength = 12;
+
+    /**
+     * Maximum length of the Proto names.
+     *
+     * @var int
+     */
+    protected static $_maxProtoNameLength = 4;
+
+    /**
+     * Maximum length of the Processes names.
+     *
+     * @var int
+     */
+    protected static $_maxProcessesNameLength = 9;
+
+    /**
+     * Maximum length of the Status names.
+     *
+     * @var int
+     */
+    protected static $_maxStatusNameLength = 1;
+
+    /**
+     * The file to store status info of current worker process.
+     *
+     * @var string
+     */
+    protected static $_statisticsFile = '';
+
+    /**
+     * Start file.
+     *
+     * @var string
+     */
+    protected static $_startFile = '';
+
+    /**
+     * OS.
+     *
+     * @var string
+     */
+    protected static $_OS = \OS_TYPE_LINUX;
+
+    /**
+     * Processes for windows.
+     *
+     * @var array
+     */
+    protected static $_processForWindows = array();
+
+    /**
+     * Status info of current worker process.
+     *
+     * @var array
+     */
+    protected static $_globalStatistics = array(
+        'start_timestamp'  => 0,
+        'worker_exit_info' => array()
+    );
+
+    /**
+     * Available event loops.
+     *
+     * @var array
+     */
+    protected static $_availableEventLoops = array(
+        'libevent' => '\Workerman\Events\Libevent',
+        'event'    => '\Workerman\Events\Event'
+        // Temporarily removed swoole because it is not stable enough
+        //'swoole'   => '\Workerman\Events\Swoole'
+    );
+
+    /**
+     * PHP built-in protocols.
+     *
+     * @var array
+     */
+    protected static $_builtinTransports = array(
+        'tcp'   => 'tcp',
+        'udp'   => 'udp',
+        'unix'  => 'unix',
+        'ssl'   => 'tcp'
+    );
+
+    /**
+     * PHP built-in error types.
+     *
+     * @var array
+     */
+    protected static $_errorType = array(
+        \E_ERROR             => 'E_ERROR',             // 1
+        \E_WARNING           => 'E_WARNING',           // 2
+        \E_PARSE             => 'E_PARSE',             // 4
+        \E_NOTICE            => 'E_NOTICE',            // 8
+        \E_CORE_ERROR        => 'E_CORE_ERROR',        // 16
+        \E_CORE_WARNING      => 'E_CORE_WARNING',      // 32
+        \E_COMPILE_ERROR     => 'E_COMPILE_ERROR',     // 64
+        \E_COMPILE_WARNING   => 'E_COMPILE_WARNING',   // 128
+        \E_USER_ERROR        => 'E_USER_ERROR',        // 256
+        \E_USER_WARNING      => 'E_USER_WARNING',      // 512
+        \E_USER_NOTICE       => 'E_USER_NOTICE',       // 1024
+        \E_STRICT            => 'E_STRICT',            // 2048
+        \E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR', // 4096
+        \E_DEPRECATED        => 'E_DEPRECATED',        // 8192
+        \E_USER_DEPRECATED   => 'E_USER_DEPRECATED'   // 16384
+    );
+
+    /**
+     * Graceful stop or not.
+     *
+     * @var bool
+     */
+    protected static $_gracefulStop = false;
+
+    /**
+     * Standard output stream
+     * @var resource
+     */
+    protected static $_outputStream = null;
+
+    /**
+     * If $outputStream support decorated
+     * @var bool
+     */
+    protected static $_outputDecorated = null;
+
+    /**
+     * Run all worker instances.
+     *
+     * @return void
+     */
+    public static function runAll()
+    {
+        static::checkSapiEnv();
+        static::init();
+        static::lock();
+        static::parseCommand();
+        static::daemonize();
+        static::initWorkers();
+        static::installSignal();
+        static::saveMasterPid();
+        static::unlock();
+        static::displayUI();
+        static::forkWorkers();
+        static::resetStd();
+        static::monitorWorkers();
+    }
+
+    /**
+     * Check sapi.
+     *
+     * @return void
+     */
+    protected static function checkSapiEnv()
+    {
+        // Only for cli.
+        if (\PHP_SAPI !== 'cli') {
+            exit("Only run in command line mode \n");
+        }
+        if (\DIRECTORY_SEPARATOR === '\\') {
+            self::$_OS = \OS_TYPE_WINDOWS;
+        }
+    }
+
+    /**
+     * Init.
+     *
+     * @return void
+     */
+    protected static function init()
+    {
+        \set_error_handler(function($code, $msg, $file, $line){
+            Worker::safeEcho("$msg in file $file on line $line\n");
+        });
+
+        // Start file.
+        $backtrace        = \debug_backtrace();
+        static::$_startFile = $backtrace[\count($backtrace) - 1]['file'];
+
+
+        $unique_prefix = \str_replace('/', '_', static::$_startFile);
+
+        // Pid file.
+        if (empty(static::$pidFile)) {
+            static::$pidFile = __DIR__ . "/../$unique_prefix.pid";
+        }
+
+        // Log file.
+        if (empty(static::$logFile)) {
+            static::$logFile = __DIR__ . '/../workerman.log';
+        }
+        $log_file = (string)static::$logFile;
+        if (!\is_file($log_file)) {
+            \touch($log_file);
+            \chmod($log_file, 0622);
+        }
+
+        // State.
+        static::$_status = static::STATUS_STARTING;
+
+        // For statistics.
+        static::$_globalStatistics['start_timestamp'] = \time();
+        static::$_statisticsFile                      = \sys_get_temp_dir() . "/$unique_prefix.status";
+
+        // Process title.
+        static::setProcessTitle(static::$processTitle . ': master process  start_file=' . static::$_startFile);
+
+        // Init data for worker id.
+        static::initId();
+
+        // Timer init.
+        Timer::init();
+    }
+
+    /**
+     * Lock.
+     *
+     * @return void
+     */
+    protected static function lock()
+    {
+        $fd = \fopen(static::$_startFile, 'r');
+        if ($fd && !flock($fd, LOCK_EX)) {
+            static::log('Workerman['.static::$_startFile.'] already running.');
+            exit;
+        }
+    }
+
+    /**
+     * Unlock.
+     *
+     * @return void
+     */
+    protected static function unlock()
+    {
+        $fd = \fopen(static::$_startFile, 'r');
+        $fd && flock($fd, \LOCK_UN);
+    }
+
+    /**
+     * Init All worker instances.
+     *
+     * @return void
+     */
+    protected static function initWorkers()
+    {
+        if (static::$_OS !== \OS_TYPE_LINUX) {
+            return;
+        }
+        foreach (static::$_workers as $worker) {
+            // Worker name.
+            if (empty($worker->name)) {
+                $worker->name = 'none';
+            }
+
+            // Get unix user of the worker process.
+            if (empty($worker->user)) {
+                $worker->user = static::getCurrentUser();
+            } else {
+                if (\posix_getuid() !== 0 && $worker->user !== static::getCurrentUser()) {
+                    static::log('Warning: You must have the root privileges to change uid and gid.');
+                }
+            }
+
+            // Socket name.
+            $worker->socket = $worker->getSocketName();
+
+            // Status name.
+            $worker->status = '<g> [OK] </g>';
+
+            // Get column mapping for UI
+            foreach(static::getUiColumns() as $column_name => $prop){
+                !isset($worker->{$prop}) && $worker->{$prop} = 'NNNN';
+                $prop_length = \strlen($worker->{$prop});
+                $key = '_max' . \ucfirst(\strtolower($column_name)) . 'NameLength';
+                static::$$key = \max(static::$$key, $prop_length);
+            }
+
+            // Listen.
+            if (!$worker->reusePort) {
+                $worker->listen();
+            }
+        }
+    }
+
+    /**
+     * Reload all worker instances.
+     *
+     * @return void
+     */
+    public static function reloadAllWorkers()
+    {
+        static::init();
+        static::initWorkers();
+        static::displayUI();
+        static::$_status = static::STATUS_RELOADING;
+    }
+
+    /**
+     * Get all worker instances.
+     *
+     * @return array
+     */
+    public static function getAllWorkers()
+    {
+        return static::$_workers;
+    }
+
+    /**
+     * Get global event-loop instance.
+     *
+     * @return EventInterface
+     */
+    public static function getEventLoop()
+    {
+        return static::$globalEvent;
+    }
+
+    /**
+     * Get main socket resource
+     * @return resource
+     */
+    public function getMainSocket(){
+        return $this->_mainSocket;
+    }
+
+    /**
+     * Init idMap.
+     * return void
+     */
+    protected static function initId()
+    {
+        foreach (static::$_workers as $worker_id => $worker) {
+            $new_id_map = array();
+            $worker->count = $worker->count < 1 ? 1 : $worker->count;
+            for($key = 0; $key < $worker->count; $key++) {
+                $new_id_map[$key] = isset(static::$_idMap[$worker_id][$key]) ? static::$_idMap[$worker_id][$key] : 0;
+            }
+            static::$_idMap[$worker_id] = $new_id_map;
+        }
+    }
+
+    /**
+     * Get unix user of current porcess.
+     *
+     * @return string
+     */
+    protected static function getCurrentUser()
+    {
+        $user_info = \posix_getpwuid(\posix_getuid());
+        return $user_info['name'];
+    }
+
+    /**
+     * Display staring UI.
+     *
+     * @return void
+     */
+    protected static function displayUI()
+    {
+        global $argv;
+        if (\in_array('-q', $argv)) {
+            return;
+        }
+        if (static::$_OS !== \OS_TYPE_LINUX) {
+            static::safeEcho("----------------------- WORKERMAN -----------------------------\r\n");
+            static::safeEcho('Workerman version:'. static::VERSION. '          PHP version:'. \PHP_VERSION. "\r\n");
+            static::safeEcho("------------------------ WORKERS -------------------------------\r\n");
+            static::safeEcho("worker               listen                              processes status\r\n");
+            return;
+        }
+
+        //show version
+        $line_version = 'Workerman version:' . static::VERSION . \str_pad('PHP version:', 22, ' ', \STR_PAD_LEFT) . \PHP_VERSION . \PHP_EOL;
+        !\defined('LINE_VERSIOIN_LENGTH') && \define('LINE_VERSIOIN_LENGTH', \strlen($line_version));
+        $total_length = static::getSingleLineTotalLength();
+        $line_one = '<n>' . \str_pad('<w> WORKERMAN </w>', $total_length + \strlen('<w></w>'), '-', \STR_PAD_BOTH) . '</n>'. \PHP_EOL;
+        $line_two = \str_pad('<w> WORKERS </w>' , $total_length  + \strlen('<w></w>'), '-', \STR_PAD_BOTH) . \PHP_EOL;
+        static::safeEcho($line_one . $line_version . $line_two);
+
+        //Show title
+        $title = '';
+        foreach(static::getUiColumns() as $column_name => $prop){
+            $key = '_max' . \ucfirst(\strtolower($column_name)) . 'NameLength';
+            //just keep compatible with listen name
+            $column_name === 'socket' && $column_name = 'listen';
+            $title.= "<w>{$column_name}</w>"  .  \str_pad('', static::$$key + static::UI_SAFE_LENGTH - \strlen($column_name));
+        }
+        $title && static::safeEcho($title . \PHP_EOL);
+
+        //Show content
+        foreach (static::$_workers as $worker) {
+            $content = '';
+            foreach(static::getUiColumns() as $column_name => $prop){
+                $key = '_max' . \ucfirst(\strtolower($column_name)) . 'NameLength';
+                \preg_match_all("/(<n>|<\/n>|<w>|<\/w>|<g>|<\/g>)/is", $worker->{$prop}, $matches);
+                $place_holder_length = !empty($matches) ? \strlen(\implode('', $matches[0])) : 0;
+                $content .= \str_pad($worker->{$prop}, static::$$key + static::UI_SAFE_LENGTH + $place_holder_length);
+            }
+            $content && static::safeEcho($content . \PHP_EOL);
+        }
+
+        //Show last line
+        $line_last = \str_pad('', static::getSingleLineTotalLength(), '-') . \PHP_EOL;
+        !empty($content) && static::safeEcho($line_last);
+
+        if (static::$daemonize) {
+            static::safeEcho("Input \"php $argv[0] stop\" to stop. Start success.\n\n");
+        } else {
+            static::safeEcho("Press Ctrl+C to stop. Start success.\n");
+        }
+    }
+
+    /**
+     * Get UI columns to be shown in terminal
+     *
+     * 1. $column_map: array('ui_column_name' => 'clas_property_name')
+     * 2. Consider move into configuration in future
+     *
+     * @return array
+     */
+    public static function getUiColumns()
+    {
+        return array(
+            'proto'     =>  'transport',
+            'user'      =>  'user',
+            'worker'    =>  'name',
+            'socket'    =>  'socket',
+            'processes' =>  'count',
+            'status'    =>  'status',
+        );
+    }
+
+    /**
+     * Get single line total length for ui
+     *
+     * @return int
+     */
+    public static function getSingleLineTotalLength()
+    {
+        $total_length = 0;
+
+        foreach(static::getUiColumns() as $column_name => $prop){
+            $key = '_max' . \ucfirst(\strtolower($column_name)) . 'NameLength';
+            $total_length += static::$$key + static::UI_SAFE_LENGTH;
+        }
+
+        //keep beauty when show less colums
+        !\defined('LINE_VERSIOIN_LENGTH') && \define('LINE_VERSIOIN_LENGTH', 0);
+        $total_length <= LINE_VERSIOIN_LENGTH && $total_length = LINE_VERSIOIN_LENGTH;
+
+        return $total_length;
+    }
+
+    /**
+     * Parse command.
+     *
+     * @return void
+     */
+    protected static function parseCommand()
+    {
+        if (static::$_OS !== \OS_TYPE_LINUX) {
+            return;
+        }
+        global $argv;
+        // Check argv;
+        $start_file = $argv[0];
+        $available_commands = array(
+            'start',
+            'stop',
+            'restart',
+            'reload',
+            'status',
+            'connections',
+        );
+        $usage = "Usage: php yourfile <command> [mode]\nCommands: \nstart\t\tStart worker in DEBUG mode.\n\t\tUse mode -d to start in DAEMON mode.\nstop\t\tStop worker.\n\t\tUse mode -g to stop gracefully.\nrestart\t\tRestart workers.\n\t\tUse mode -d to start in DAEMON mode.\n\t\tUse mode -g to stop gracefully.\nreload\t\tReload codes.\n\t\tUse mode -g to reload gracefully.\nstatus\t\tGet worker status.\n\t\tUse mode -d to show live status.\nconnections\tGet worker connections.\n";
+        if (!isset($argv[1]) || !\in_array($argv[1], $available_commands)) {
+            if (isset($argv[1])) {
+                static::safeEcho('Unknown command: ' . $argv[1] . "\n");
+            }
+            exit($usage);
+        }
+
+        // Get command.
+        $command  = \trim($argv[1]);
+        $command2 = isset($argv[2]) ? $argv[2] : '';
+
+        // Start command.
+        $mode = '';
+        if ($command === 'start') {
+            if ($command2 === '-d' || static::$daemonize) {
+                $mode = 'in DAEMON mode';
+            } else {
+                $mode = 'in DEBUG mode';
+            }
+        }
+        static::log("Workerman[$start_file] $command $mode");
+
+        // Get master process PID.
+        $master_pid      = \is_file(static::$pidFile) ? \file_get_contents(static::$pidFile) : 0;
+        $master_is_alive = $master_pid && \posix_kill($master_pid, 0) && \posix_getpid() !== $master_pid;
+        // Master is still alive?
+        if ($master_is_alive) {
+            if ($command === 'start') {
+                static::log("Workerman[$start_file] already running");
+                exit;
+            }
+        } elseif ($command !== 'start' && $command !== 'restart') {
+            static::log("Workerman[$start_file] not run");
+            exit;
+        }
+
+        // execute command.
+        switch ($command) {
+            case 'start':
+                if ($command2 === '-d') {
+                    static::$daemonize = true;
+                }
+                break;
+            case 'status':
+                while (1) {
+                    if (\is_file(static::$_statisticsFile)) {
+                        @\unlink(static::$_statisticsFile);
+                    }
+                    // Master process will send SIGUSR2 signal to all child processes.
+                    \posix_kill($master_pid, SIGUSR2);
+                    // Sleep 1 second.
+                    \sleep(1);
+                    // Clear terminal.
+                    if ($command2 === '-d') {
+                        static::safeEcho("\33[H\33[2J\33(B\33[m", true);
+                    }
+                    // Echo status data.
+                    static::safeEcho(static::formatStatusData());
+                    if ($command2 !== '-d') {
+                        exit(0);
+                    }
+                    static::safeEcho("\nPress Ctrl+C to quit.\n\n");
+                }
+                exit(0);
+            case 'connections':
+                if (\is_file(static::$_statisticsFile) && \is_writable(static::$_statisticsFile)) {
+                    \unlink(static::$_statisticsFile);
+                }
+                // Master process will send SIGIO signal to all child processes.
+                \posix_kill($master_pid, SIGIO);
+                // Waiting amoment.
+                \usleep(500000);
+                // Display statisitcs data from a disk file.
+                if(\is_readable(static::$_statisticsFile)) {
+                    \readfile(static::$_statisticsFile);
+                }
+                exit(0);
+            case 'restart':
+            case 'stop':
+                if ($command2 === '-g') {
+                    static::$_gracefulStop = true;
+                    $sig = \SIGTERM;
+                    static::log("Workerman[$start_file] is gracefully stopping ...");
+                } else {
+                    static::$_gracefulStop = false;
+                    $sig = \SIGINT;
+                    static::log("Workerman[$start_file] is stopping ...");
+                }
+                // Send stop signal to master process.
+                $master_pid && \posix_kill($master_pid, $sig);
+                // Timeout.
+                $timeout    = 5;
+                $start_time = \time();
+                // Check master process is still alive?
+                while (1) {
+                    $master_is_alive = $master_pid && \posix_kill($master_pid, 0);
+                    if ($master_is_alive) {
+                        // Timeout?
+                        if (!static::$_gracefulStop && \time() - $start_time >= $timeout) {
+                            static::log("Workerman[$start_file] stop fail");
+                            exit;
+                        }
+                        // Waiting amoment.
+                        \usleep(10000);
+                        continue;
+                    }
+                    // Stop success.
+                    static::log("Workerman[$start_file] stop success");
+                    if ($command === 'stop') {
+                        exit(0);
+                    }
+                    if ($command2 === '-d') {
+                        static::$daemonize = true;
+                    }
+                    break;
+                }
+                break;
+            case 'reload':
+                if($command2 === '-g'){
+                    $sig = \SIGQUIT;
+                }else{
+                    $sig = \SIGUSR1;
+                }
+                \posix_kill($master_pid, $sig);
+                exit;
+            default :
+                if (isset($command)) {
+                    static::safeEcho('Unknown command: ' . $command . "\n");
+                }
+                exit($usage);
+        }
+    }
+
+    /**
+     * Format status data.
+     *
+     * @return string
+     */
+    protected static function formatStatusData()
+    {
+        static $total_request_cache = array();
+        if (!\is_readable(static::$_statisticsFile)) {
+            return '';
+        }
+        $info = \file(static::$_statisticsFile, \FILE_IGNORE_NEW_LINES);
+        if (!$info) {
+            return '';
+        }
+        $status_str = '';
+        $current_total_request = array();
+        $worker_info = \unserialize($info[0]);
+        \ksort($worker_info, SORT_NUMERIC);
+        unset($info[0]);
+        $data_waiting_sort = array();
+        $read_process_status = false;
+        $total_requests = 0;
+        $total_qps = 0;
+        $total_connections = 0;
+        $total_fails = 0;
+        $total_memory = 0;
+        $total_timers = 0;
+        $maxLen1 = static::$_maxSocketNameLength;
+        $maxLen2 = static::$_maxWorkerNameLength;
+        foreach($info as $key => $value) {
+            if (!$read_process_status) {
+                $status_str .= $value . "\n";
+                if (\preg_match('/^pid.*?memory.*?listening/', $value)) {
+                    $read_process_status = true;
+                }
+                continue;
+            }
+            if(\preg_match('/^[0-9]+/', $value, $pid_math)) {
+                $pid = $pid_math[0];
+                $data_waiting_sort[$pid] = $value;
+                if(\preg_match('/^\S+?\s+?(\S+?)\s+?(\S+?)\s+?(\S+?)\s+?(\S+?)\s+?(\S+?)\s+?(\S+?)\s+?(\S+?)\s+?/', $value, $match)) {
+                    $total_memory += \intval(\str_ireplace('M','',$match[1]));
+                    $maxLen1 = \max($maxLen1,\strlen($match[2]));
+                    $maxLen2 = \max($maxLen2,\strlen($match[3]));
+                    $total_connections += \intval($match[4]);
+                    $total_fails += \intval($match[5]);
+                    $total_timers += \intval($match[6]);
+                    $current_total_request[$pid] = $match[7];
+                    $total_requests += \intval($match[7]);
+                }
+            }
+        }
+        foreach($worker_info as $pid => $info) {
+            if (!isset($data_waiting_sort[$pid])) {
+                $status_str .= "$pid\t" . \str_pad('N/A', 7) . " "
+                    . \str_pad($info['listen'], static::$_maxSocketNameLength) . " "
+                    . \str_pad($info['name'], static::$_maxWorkerNameLength) . " "
+                    . \str_pad('N/A', 11) . " " . \str_pad('N/A', 9) . " "
+                    . \str_pad('N/A', 7) . " " . \str_pad('N/A', 13) . " N/A    [busy] \n";
+                continue;
+            }
+            //$qps = isset($total_request_cache[$pid]) ? $current_total_request[$pid]
+            if (!isset($total_request_cache[$pid]) || !isset($current_total_request[$pid])) {
+                $qps = 0;
+            } else {
+                $qps = $current_total_request[$pid] - $total_request_cache[$pid];
+                $total_qps += $qps;
+            }
+            $status_str .= $data_waiting_sort[$pid]. " " . \str_pad($qps, 6) ." [idle]\n";
+        }
+        $total_request_cache = $current_total_request;
+        $status_str .= "----------------------------------------------PROCESS STATUS---------------------------------------------------\n";
+        $status_str .= "Summary\t" . \str_pad($total_memory.'M', 7) . " "
+            . \str_pad('-', $maxLen1) . " "
+            . \str_pad('-', $maxLen2) . " "
+            . \str_pad($total_connections, 11) . " " . \str_pad($total_fails, 9) . " "
+            . \str_pad($total_timers, 7) . " " . \str_pad($total_requests, 13) . " "
+            . \str_pad($total_qps,6)." [Summary] \n";
+        return $status_str;
+    }
+
+
+    /**
+     * Install signal handler.
+     *
+     * @return void
+     */
+    protected static function installSignal()
+    {
+        if (static::$_OS !== \OS_TYPE_LINUX) {
+            return;
+        }
+        $signalHandler = '\Workerman\Worker::signalHandler';
+        // stop
+        \pcntl_signal(\SIGINT, $signalHandler, false);
+        // graceful stop
+        \pcntl_signal(\SIGTERM, $signalHandler, false);
+        // reload
+        \pcntl_signal(\SIGUSR1, $signalHandler, false);
+        // graceful reload
+        \pcntl_signal(\SIGQUIT, $signalHandler, false);
+        // status
+        \pcntl_signal(\SIGUSR2, $signalHandler, false);
+        // connection status
+        \pcntl_signal(\SIGIO, $signalHandler, false);
+        // ignore
+        \pcntl_signal(\SIGPIPE, \SIG_IGN, false);
+    }
+
+    /**
+     * Reinstall signal handler.
+     *
+     * @return void
+     */
+    protected static function reinstallSignal()
+    {
+        if (static::$_OS !== \OS_TYPE_LINUX) {
+            return;
+        }
+        $signalHandler = '\Workerman\Worker::signalHandler';
+        // uninstall stop signal handler
+        \pcntl_signal(\SIGINT, \SIG_IGN, false);
+        // uninstall graceful stop signal handler
+        \pcntl_signal(\SIGTERM, \SIG_IGN, false);
+        // uninstall reload signal handler
+        \pcntl_signal(\SIGUSR1, \SIG_IGN, false);
+        // uninstall graceful reload signal handler
+        \pcntl_signal(\SIGQUIT, \SIG_IGN, false);
+        // uninstall status signal handler
+        \pcntl_signal(\SIGUSR2, \SIG_IGN, false);
+        // uninstall connections status signal handler
+        \pcntl_signal(\SIGIO, \SIG_IGN, false);
+        // reinstall stop signal handler
+        static::$globalEvent->add(\SIGINT, EventInterface::EV_SIGNAL, $signalHandler);
+        // reinstall graceful stop signal handler
+        static::$globalEvent->add(\SIGTERM, EventInterface::EV_SIGNAL, $signalHandler);
+        // reinstall reload signal handler
+        static::$globalEvent->add(\SIGUSR1, EventInterface::EV_SIGNAL, $signalHandler);
+        // reinstall graceful reload signal handler
+        static::$globalEvent->add(\SIGQUIT, EventInterface::EV_SIGNAL, $signalHandler);
+        // reinstall status signal handler
+        static::$globalEvent->add(\SIGUSR2, EventInterface::EV_SIGNAL, $signalHandler);
+        // reinstall connection status signal handler
+        static::$globalEvent->add(\SIGIO, EventInterface::EV_SIGNAL, $signalHandler);
+    }
+
+    /**
+     * Signal handler.
+     *
+     * @param int $signal
+     */
+    public static function signalHandler($signal)
+    {
+        switch ($signal) {
+            // Stop.
+            case \SIGINT:
+                static::$_gracefulStop = false;
+                static::stopAll();
+                break;
+            // Graceful stop.
+            case \SIGTERM:
+                static::$_gracefulStop = true;
+                static::stopAll();
+                break;
+            // Reload.
+            case \SIGQUIT:
+            case \SIGUSR1:
+                if($signal === \SIGQUIT){
+                    static::$_gracefulStop = true;
+                }else{
+                    static::$_gracefulStop = false;
+                }
+                static::$_pidsToRestart = static::getAllWorkerPids();
+                static::reload();
+                break;
+            // Show status.
+            case \SIGUSR2:
+                static::writeStatisticsToStatusFile();
+                break;
+            // Show connection status.
+            case \SIGIO:
+                static::writeConnectionsStatisticsToStatusFile();
+                break;
+        }
+    }
+
+    /**
+     * Run as deamon mode.
+     *
+     * @throws Exception
+     */
+    protected static function daemonize()
+    {
+        if (!static::$daemonize || static::$_OS !== \OS_TYPE_LINUX) {
+            return;
+        }
+        \umask(0);
+        $pid = \pcntl_fork();
+        if (-1 === $pid) {
+            throw new Exception('Fork fail');
+        } elseif ($pid > 0) {
+            exit(0);
+        }
+        if (-1 === \posix_setsid()) {
+            throw new Exception("Setsid fail");
+        }
+        // Fork again avoid SVR4 system regain the control of terminal.
+        $pid = \pcntl_fork();
+        if (-1 === $pid) {
+            throw new Exception("Fork fail");
+        } elseif (0 !== $pid) {
+            exit(0);
+        }
+    }
+
+    /**
+     * Redirect standard input and output.
+     *
+     * @throws Exception
+     */
+    public static function resetStd()
+    {
+        if (!static::$daemonize || static::$_OS !== \OS_TYPE_LINUX) {
+            return;
+        }
+        global $STDOUT, $STDERR;
+        $handle = \fopen(static::$stdoutFile, "a");
+        if ($handle) {
+            unset($handle);
+            \set_error_handler(function(){});
+            \fclose($STDOUT);
+            \fclose($STDERR);
+            \fclose(\STDOUT);
+            \fclose(\STDERR);
+            $STDOUT = \fopen(static::$stdoutFile, "a");
+            $STDERR = \fopen(static::$stdoutFile, "a");
+            // change output stream
+            static::$_outputStream = null;
+            static::outputStream($STDOUT);
+            \restore_error_handler();
+            return;
+        }
+
+        throw new Exception('Can not open stdoutFile ' . static::$stdoutFile);
+    }
+
+    /**
+     * Save pid.
+     *
+     * @throws Exception
+     */
+    protected static function saveMasterPid()
+    {
+        if (static::$_OS !== \OS_TYPE_LINUX) {
+            return;
+        }
+
+        static::$_masterPid = \posix_getpid();
+        if (false === \file_put_contents(static::$pidFile, static::$_masterPid)) {
+            throw new Exception('can not save pid to ' . static::$pidFile);
+        }
+    }
+
+    /**
+     * Get event loop name.
+     *
+     * @return string
+     */
+    protected static function getEventLoopName()
+    {
+        if (static::$eventLoopClass) {
+            return static::$eventLoopClass;
+        }
+
+        if (!\class_exists('\Swoole\Event', false)) {
+            unset(static::$_availableEventLoops['swoole']);
+        }
+
+        $loop_name = '';
+        foreach (static::$_availableEventLoops as $name=>$class) {
+            if (\extension_loaded($name)) {
+                $loop_name = $name;
+                break;
+            }
+        }
+
+        if ($loop_name) {
+            if (\interface_exists('\React\EventLoop\LoopInterface')) {
+                switch ($loop_name) {
+                    case 'libevent':
+                        static::$eventLoopClass = '\Workerman\Events\React\ExtLibEventLoop';
+                        break;
+                    case 'event':
+                        static::$eventLoopClass = '\Workerman\Events\React\ExtEventLoop';
+                        break;
+                    default :
+                        static::$eventLoopClass = '\Workerman\Events\React\StreamSelectLoop';
+                        break;
+                }
+            } else {
+                static::$eventLoopClass = static::$_availableEventLoops[$loop_name];
+            }
+        } else {
+            static::$eventLoopClass = \interface_exists('\React\EventLoop\LoopInterface') ? '\Workerman\Events\React\StreamSelectLoop' : '\Workerman\Events\Select';
+        }
+        return static::$eventLoopClass;
+    }
+
+    /**
+     * Get all pids of worker processes.
+     *
+     * @return array
+     */
+    protected static function getAllWorkerPids()
+    {
+        $pid_array = array();
+        foreach (static::$_pidMap as $worker_pid_array) {
+            foreach ($worker_pid_array as $worker_pid) {
+                $pid_array[$worker_pid] = $worker_pid;
+            }
+        }
+        return $pid_array;
+    }
+
+    /**
+     * Fork some worker processes.
+     *
+     * @return void
+     */
+    protected static function forkWorkers()
+    {
+        if (static::$_OS === \OS_TYPE_LINUX) {
+            static::forkWorkersForLinux();
+        } else {
+            static::forkWorkersForWindows();
+        }
+    }
+
+    /**
+     * Fork some worker processes.
+     *
+     * @return void
+     */
+    protected static function forkWorkersForLinux()
+    {
+
+        foreach (static::$_workers as $worker) {
+            if (static::$_status === static::STATUS_STARTING) {
+                if (empty($worker->name)) {
+                    $worker->name = $worker->getSocketName();
+                }
+                $worker_name_length = \strlen($worker->name);
+                if (static::$_maxWorkerNameLength < $worker_name_length) {
+                    static::$_maxWorkerNameLength = $worker_name_length;
+                }
+            }
+
+            while (\count(static::$_pidMap[$worker->workerId]) < $worker->count) {
+                static::forkOneWorkerForLinux($worker);
+            }
+        }
+    }
+
+    /**
+     * Fork some worker processes.
+     *
+     * @return void
+     */
+    protected static function forkWorkersForWindows()
+    {
+        $files = static::getStartFilesForWindows();
+        global $argv;
+        if(\in_array('-q', $argv) || \count($files) === 1)
+        {
+            if(\count(static::$_workers) > 1)
+            {
+                static::safeEcho("@@@ Error: multi workers init in one php file are not support @@@\r\n");
+                static::safeEcho("@@@ See http://doc.workerman.net/faq/multi-woker-for-windows.html @@@\r\n");
+            }
+            elseif(\count(static::$_workers) <= 0)
+            {
+                exit("@@@no worker inited@@@\r\n\r\n");
+            }
+
+            \reset(static::$_workers);
+            /** @var Worker $worker */
+            $worker = current(static::$_workers);
+
+            // Display UI.
+            static::safeEcho(\str_pad($worker->name, 21) . \str_pad($worker->getSocketName(), 36) . \str_pad($worker->count, 10) . "[ok]\n");
+            $worker->listen();
+            $worker->run();
+            exit("@@@child exit@@@\r\n");
+        }
+        else
+        {
+            static::$globalEvent = new \Workerman\Events\Select();
+            Timer::init(static::$globalEvent);
+            foreach($files as $start_file)
+            {
+                static::forkOneWorkerForWindows($start_file);
+            }
+        }
+    }
+
+    /**
+     * Get start files for windows.
+     *
+     * @return array
+     */
+    public static function getStartFilesForWindows() {
+        global $argv;
+        $files = array();
+        foreach($argv as $file)
+        {
+            if(\is_file($file))
+            {
+                $files[$file] = $file;
+            }
+        }
+        return $files;
+    }
+
+    /**
+     * Fork one worker process.
+     *
+     * @param string $start_file
+     */
+    public static function forkOneWorkerForWindows($start_file)
+    {
+        $start_file = \realpath($start_file);
+        $std_file = \sys_get_temp_dir() . '/'.\str_replace(array('/', "\\", ':'), '_', $start_file).'.out.txt';
+
+        $descriptorspec = array(
+            0 => array('pipe', 'a'), // stdin
+            1 => array('file', $std_file, 'w'), // stdout
+            2 => array('file', $std_file, 'w') // stderr
+        );
+
+
+        $pipes       = array();
+        $process     = \proc_open("php \"$start_file\" -q", $descriptorspec, $pipes);
+        $std_handler = \fopen($std_file, 'a+');
+        \stream_set_blocking($std_handler, false);
+
+        if (empty(static::$globalEvent)) {
+            static::$globalEvent = new Select();
+            Timer::init(static::$globalEvent);
+        }
+        $timer_id = Timer::add(0.1, function()use($std_handler)
+        {
+            Worker::safeEcho(\fread($std_handler, 65535));
+        });
+
+        // 保存子进程句柄
+        static::$_processForWindows[$start_file] = array($process, $start_file, $timer_id);
+    }
+
+    /**
+     * check worker status for windows.
+     * @return void
+     */
+    public static function checkWorkerStatusForWindows()
+    {
+        foreach(static::$_processForWindows as $process_data)
+        {
+            $process = $process_data[0];
+            $start_file = $process_data[1];
+            $timer_id = $process_data[2];
+            $status = \proc_get_status($process);
+            if(isset($status['running']))
+            {
+                if(!$status['running'])
+                {
+                    static::safeEcho("process $start_file terminated and try to restart\n");
+                    Timer::del($timer_id);
+                    \proc_close($process);
+                    static::forkOneWorkerForWindows($start_file);
+                }
+            }
+            else
+            {
+                static::safeEcho("proc_get_status fail\n");
+            }
+        }
+    }
+
+
+    /**
+     * Fork one worker process.
+     *
+     * @param self $worker
+     * @throws Exception
+     */
+    protected static function forkOneWorkerForLinux(self $worker)
+    {
+        // Get available worker id.
+        $id = static::getId($worker->workerId, 0);
+        if ($id === false) {
+            return;
+        }
+        $pid = \pcntl_fork();
+        // For master process.
+        if ($pid > 0) {
+            static::$_pidMap[$worker->workerId][$pid] = $pid;
+            static::$_idMap[$worker->workerId][$id]   = $pid;
+        } // For child processes.
+        elseif (0 === $pid) {
+            \srand();
+            \mt_srand();
+            if ($worker->reusePort) {
+                $worker->listen();
+            }
+            if (static::$_status === static::STATUS_STARTING) {
+                static::resetStd();
+            }
+            static::$_pidMap  = array();
+            // Remove other listener.
+            foreach(static::$_workers as $key => $one_worker) {
+                if ($one_worker->workerId !== $worker->workerId) {
+                    $one_worker->unlisten();
+                    unset(static::$_workers[$key]);
+                }
+            }
+            Timer::delAll();
+            static::setProcessTitle(self::$processTitle . ': worker process  ' . $worker->name . ' ' . $worker->getSocketName());
+            $worker->setUserAndGroup();
+            $worker->id = $id;
+            $worker->run();
+            $err = new Exception('event-loop exited');
+            static::log($err);
+            exit(250);
+        } else {
+            throw new Exception("forkOneWorker fail");
+        }
+    }
+
+    /**
+     * Get worker id.
+     *
+     * @param int $worker_id
+     * @param int $pid
+     *
+     * @return integer
+     */
+    protected static function getId($worker_id, $pid)
+    {
+        return \array_search($pid, static::$_idMap[$worker_id]);
+    }
+
+    /**
+     * Set unix user and group for current process.
+     *
+     * @return void
+     */
+    public function setUserAndGroup()
+    {
+        // Get uid.
+        $user_info = \posix_getpwnam($this->user);
+        if (!$user_info) {
+            static::log("Warning: User {$this->user} not exsits");
+            return;
+        }
+        $uid = $user_info['uid'];
+        // Get gid.
+        if ($this->group) {
+            $group_info = \posix_getgrnam($this->group);
+            if (!$group_info) {
+                static::log("Warning: Group {$this->group} not exsits");
+                return;
+            }
+            $gid = $group_info['gid'];
+        } else {
+            $gid = $user_info['gid'];
+        }
+
+        // Set uid and gid.
+        if ($uid !== \posix_getuid() || $gid !== \posix_getgid()) {
+            if (!\posix_setgid($gid) || !\posix_initgroups($user_info['name'], $gid) || !\posix_setuid($uid)) {
+                static::log("Warning: change gid or uid fail.");
+            }
+        }
+    }
+
+    /**
+     * Set process name.
+     *
+     * @param string $title
+     * @return void
+     */
+    protected static function setProcessTitle($title)
+    {
+        \set_error_handler(function(){});
+        // >=php 5.5
+        if (\function_exists('cli_set_process_title')) {
+            \cli_set_process_title($title);
+        } // Need proctitle when php<=5.5 .
+        elseif (\extension_loaded('proctitle') && \function_exists('setproctitle')) {
+            \setproctitle($title);
+        }
+        \restore_error_handler();
+    }
+
+    /**
+     * Monitor all child processes.
+     *
+     * @return void
+     */
+    protected static function monitorWorkers()
+    {
+        if (static::$_OS === \OS_TYPE_LINUX) {
+            static::monitorWorkersForLinux();
+        } else {
+            static::monitorWorkersForWindows();
+        }
+    }
+
+    /**
+     * Monitor all child processes.
+     *
+     * @return void
+     */
+    protected static function monitorWorkersForLinux()
+    {
+        static::$_status = static::STATUS_RUNNING;
+        while (1) {
+            // Calls signal handlers for pending signals.
+            \pcntl_signal_dispatch();
+            // Suspends execution of the current process until a child has exited, or until a signal is delivered
+            $status = 0;
+            $pid    = \pcntl_wait($status, \WUNTRACED);
+            // Calls signal handlers for pending signals again.
+            \pcntl_signal_dispatch();
+            // If a child has already exited.
+            if ($pid > 0) {
+                // Find out which worker process exited.
+                foreach (static::$_pidMap as $worker_id => $worker_pid_array) {
+                    if (isset($worker_pid_array[$pid])) {
+                        $worker = static::$_workers[$worker_id];
+                        // Exit status.
+                        if ($status !== 0) {
+                            static::log("worker[" . $worker->name . ":$pid] exit with status $status");
+                        }
+
+                        // For Statistics.
+                        if (!isset(static::$_globalStatistics['worker_exit_info'][$worker_id][$status])) {
+                            static::$_globalStatistics['worker_exit_info'][$worker_id][$status] = 0;
+                        }
+                        ++static::$_globalStatistics['worker_exit_info'][$worker_id][$status];
+
+                        // Clear process data.
+                        unset(static::$_pidMap[$worker_id][$pid]);
+
+                        // Mark id is available.
+                        $id                              = static::getId($worker_id, $pid);
+                        static::$_idMap[$worker_id][$id] = 0;
+
+                        break;
+                    }
+                }
+                // Is still running state then fork a new worker process.
+                if (static::$_status !== static::STATUS_SHUTDOWN) {
+                    static::forkWorkers();
+                    // If reloading continue.
+                    if (isset(static::$_pidsToRestart[$pid])) {
+                        unset(static::$_pidsToRestart[$pid]);
+                        static::reload();
+                    }
+                }
+            }
+
+            // If shutdown state and all child processes exited then master process exit.
+            if (static::$_status === static::STATUS_SHUTDOWN && !static::getAllWorkerPids()) {
+                static::exitAndClearAll();
+            }
+        }
+    }
+
+    /**
+     * Monitor all child processes.
+     *
+     * @return void
+     */
+    protected static function monitorWorkersForWindows()
+    {
+        Timer::add(1, "\\Workerman\\Worker::checkWorkerStatusForWindows");
+
+        static::$globalEvent->loop();
+    }
+
+    /**
+     * Exit current process.
+     *
+     * @return void
+     */
+    protected static function exitAndClearAll()
+    {
+        foreach (static::$_workers as $worker) {
+            $socket_name = $worker->getSocketName();
+            if ($worker->transport === 'unix' && $socket_name) {
+                list(, $address) = \explode(':', $socket_name, 2);
+                @\unlink($address);
+            }
+        }
+        @\unlink(static::$pidFile);
+        static::log("Workerman[" . \basename(static::$_startFile) . "] has been stopped");
+        if (static::$onMasterStop) {
+            \call_user_func(static::$onMasterStop);
+        }
+        exit(0);
+    }
+
+    /**
+     * Execute reload.
+     *
+     * @return void
+     */
+    protected static function reload()
+    {
+        // For master process.
+        if (static::$_masterPid === \posix_getpid()) {
+            // Set reloading state.
+            if (static::$_status !== static::STATUS_RELOADING && static::$_status !== static::STATUS_SHUTDOWN) {
+                static::log("Workerman[" . \basename(static::$_startFile) . "] reloading");
+                static::$_status = static::STATUS_RELOADING;
+                // Try to emit onMasterReload callback.
+                if (static::$onMasterReload) {
+                    try {
+                        \call_user_func(static::$onMasterReload);
+                    } catch (\Exception $e) {
+                        static::log($e);
+                        exit(250);
+                    } catch (\Error $e) {
+                        static::log($e);
+                        exit(250);
+                    }
+                    static::initId();
+                }
+            }
+
+            if (static::$_gracefulStop) {
+                $sig = \SIGQUIT;
+            } else {
+                $sig = \SIGUSR1;
+            }
+
+            // Send reload signal to all child processes.
+            $reloadable_pid_array = array();
+            foreach (static::$_pidMap as $worker_id => $worker_pid_array) {
+                $worker = static::$_workers[$worker_id];
+                if ($worker->reloadable) {
+                    foreach ($worker_pid_array as $pid) {
+                        $reloadable_pid_array[$pid] = $pid;
+                    }
+                } else {
+                    foreach ($worker_pid_array as $pid) {
+                        // Send reload signal to a worker process which reloadable is false.
+                        \posix_kill($pid, $sig);
+                    }
+                }
+            }
+
+            // Get all pids that are waiting reload.
+            static::$_pidsToRestart = \array_intersect(static::$_pidsToRestart, $reloadable_pid_array);
+
+            // Reload complete.
+            if (empty(static::$_pidsToRestart)) {
+                if (static::$_status !== static::STATUS_SHUTDOWN) {
+                    static::$_status = static::STATUS_RUNNING;
+                }
+                return;
+            }
+            // Continue reload.
+            $one_worker_pid = \current(static::$_pidsToRestart);
+            // Send reload signal to a worker process.
+            \posix_kill($one_worker_pid, $sig);
+            // If the process does not exit after static::KILL_WORKER_TIMER_TIME seconds try to kill it.
+            if(!static::$_gracefulStop){
+                Timer::add(static::KILL_WORKER_TIMER_TIME, '\posix_kill', array($one_worker_pid, \SIGKILL), false);
+            }
+        } // For child processes.
+        else {
+            \reset(static::$_workers);
+            $worker = \current(static::$_workers);
+            // Try to emit onWorkerReload callback.
+            if ($worker->onWorkerReload) {
+                try {
+                    \call_user_func($worker->onWorkerReload, $worker);
+                } catch (\Exception $e) {
+                    static::log($e);
+                    exit(250);
+                } catch (\Error $e) {
+                    static::log($e);
+                    exit(250);
+                }
+            }
+
+            if ($worker->reloadable) {
+                static::stopAll();
+            }
+        }
+    }
+
+    /**
+     * Stop.
+     *
+     * @return void
+     */
+    public static function stopAll()
+    {
+        static::$_status = static::STATUS_SHUTDOWN;
+        // For master process.
+        if (static::$_masterPid === \posix_getpid()) {
+            static::log("Workerman[" . \basename(static::$_startFile) . "] stopping ...");
+            $worker_pid_array = static::getAllWorkerPids();
+            // Send stop signal to all child processes.
+            if (static::$_gracefulStop) {
+                $sig = \SIGTERM;
+            } else {
+                $sig = \SIGINT;
+            }
+            foreach ($worker_pid_array as $worker_pid) {
+                \posix_kill($worker_pid, $sig);
+                if(!static::$_gracefulStop){
+                    Timer::add(static::KILL_WORKER_TIMER_TIME, '\posix_kill', array($worker_pid, \SIGKILL), false);
+                }
+            }
+            Timer::add(1, "\\Workerman\\Worker::checkIfChildRunning");
+            // Remove statistics file.
+            if (\is_file(static::$_statisticsFile)) {
+                @\unlink(static::$_statisticsFile);
+            }
+        } // For child processes.
+        else {
+            // Execute exit.
+            foreach (static::$_workers as $worker) {
+                if(!$worker->stopping){
+                    $worker->stop();
+                    $worker->stopping = true;
+                }
+            }
+            if (!static::$_gracefulStop || ConnectionInterface::$statistics['connection_count'] <= 0) {
+                static::$_workers = array();
+                if (static::$globalEvent) {
+                    static::$globalEvent->destroy();
+                }
+                exit(0);
+            }
+        }
+    }
+
+    /**
+     * check if child processes is really running
+     */
+    public static function checkIfChildRunning()
+    {
+        foreach (static::$_pidMap as $worker_id => $worker_pid_array) {
+            foreach ($worker_pid_array as $pid => $worker_pid) {
+                if (!\posix_kill($pid, 0)) {
+                    unset(static::$_pidMap[$worker_id][$pid]);
+                }
+            }
+        }
+    }
+
+    /**
+     * Get process status.
+     *
+     * @return number
+     */
+    public static function getStatus()
+    {
+        return static::$_status;
+    }
+
+    /**
+     * If stop gracefully.
+     *
+     * @return bool
+     */
+    public static function getGracefulStop()
+    {
+        return static::$_gracefulStop;
+    }
+
+    /**
+     * Write statistics data to disk.
+     *
+     * @return void
+     */
+    protected static function writeStatisticsToStatusFile()
+    {
+        // For master process.
+        if (static::$_masterPid === \posix_getpid()) {
+            $all_worker_info = array();
+            foreach(static::$_pidMap as $worker_id => $pid_array) {
+                /** @var /Workerman/Worker $worker */
+                $worker = static::$_workers[$worker_id];
+                foreach($pid_array as $pid) {
+                    $all_worker_info[$pid] = array('name' => $worker->name, 'listen' => $worker->getSocketName());
+                }
+            }
+
+            \file_put_contents(static::$_statisticsFile, \serialize($all_worker_info)."\n", \FILE_APPEND);
+            $loadavg = \function_exists('sys_getloadavg') ? \array_map('round', \sys_getloadavg(), array(2)) : array('-', '-', '-');
+            \file_put_contents(static::$_statisticsFile,
+                "----------------------------------------------GLOBAL STATUS----------------------------------------------------\n", \FILE_APPEND);
+            \file_put_contents(static::$_statisticsFile,
+                'Workerman version:' . static::VERSION . "          PHP version:" . \PHP_VERSION . "\n", \FILE_APPEND);
+            \file_put_contents(static::$_statisticsFile, 'start time:' . \date('Y-m-d H:i:s',
+                    static::$_globalStatistics['start_timestamp']) . '   run ' . \floor((\time() - static::$_globalStatistics['start_timestamp']) / (24 * 60 * 60)) . ' days ' . \floor(((\time() - static::$_globalStatistics['start_timestamp']) % (24 * 60 * 60)) / (60 * 60)) . " hours   \n",
+                FILE_APPEND);
+            $load_str = 'load average: ' . \implode(", ", $loadavg);
+            \file_put_contents(static::$_statisticsFile,
+                \str_pad($load_str, 33) . 'event-loop:' . static::getEventLoopName() . "\n", \FILE_APPEND);
+            \file_put_contents(static::$_statisticsFile,
+                \count(static::$_pidMap) . ' workers       ' . \count(static::getAllWorkerPids()) . " processes\n",
+                \FILE_APPEND);
+            \file_put_contents(static::$_statisticsFile,
+                \str_pad('worker_name', static::$_maxWorkerNameLength) . " exit_status      exit_count\n", \FILE_APPEND);
+            foreach (static::$_pidMap as $worker_id => $worker_pid_array) {
+                $worker = static::$_workers[$worker_id];
+                if (isset(static::$_globalStatistics['worker_exit_info'][$worker_id])) {
+                    foreach (static::$_globalStatistics['worker_exit_info'][$worker_id] as $worker_exit_status => $worker_exit_count) {
+                        \file_put_contents(static::$_statisticsFile,
+                            \str_pad($worker->name, static::$_maxWorkerNameLength) . " " . \str_pad($worker_exit_status,
+                                16) . " $worker_exit_count\n", \FILE_APPEND);
+                    }
+                } else {
+                    \file_put_contents(static::$_statisticsFile,
+                        \str_pad($worker->name, static::$_maxWorkerNameLength) . " " . \str_pad(0, 16) . " 0\n",
+                        \FILE_APPEND);
+                }
+            }
+            \file_put_contents(static::$_statisticsFile,
+                "----------------------------------------------PROCESS STATUS---------------------------------------------------\n",
+                \FILE_APPEND);
+            \file_put_contents(static::$_statisticsFile,
+                "pid\tmemory  " . \str_pad('listening', static::$_maxSocketNameLength) . " " . \str_pad('worker_name',
+                    static::$_maxWorkerNameLength) . " connections " . \str_pad('send_fail', 9) . " "
+                . \str_pad('timers', 8) . \str_pad('total_request', 13) ." qps    status\n", \FILE_APPEND);
+
+            \chmod(static::$_statisticsFile, 0722);
+
+            foreach (static::getAllWorkerPids() as $worker_pid) {
+                \posix_kill($worker_pid, \SIGUSR2);
+            }
+            return;
+        }
+
+        // For child processes.
+        \reset(static::$_workers);
+        /** @var \Workerman\Worker $worker */
+        $worker            = current(static::$_workers);
+        $worker_status_str = \posix_getpid() . "\t" . \str_pad(round(memory_get_usage(true) / (1024 * 1024), 2) . "M", 7)
+            . " " . \str_pad($worker->getSocketName(), static::$_maxSocketNameLength) . " "
+            . \str_pad(($worker->name === $worker->getSocketName() ? 'none' : $worker->name), static::$_maxWorkerNameLength)
+            . " ";
+        $worker_status_str .= \str_pad(ConnectionInterface::$statistics['connection_count'], 11)
+            . " " .  \str_pad(ConnectionInterface::$statistics['send_fail'], 9)
+            . " " . \str_pad(static::$globalEvent->getTimerCount(), 7)
+            . " " . \str_pad(ConnectionInterface::$statistics['total_request'], 13) . "\n";
+        \file_put_contents(static::$_statisticsFile, $worker_status_str, \FILE_APPEND);
+    }
+
+    /**
+     * Write statistics data to disk.
+     *
+     * @return void
+     */
+    protected static function writeConnectionsStatisticsToStatusFile()
+    {
+        // For master process.
+        if (static::$_masterPid === \posix_getpid()) {
+            \file_put_contents(static::$_statisticsFile, "--------------------------------------------------------------------- WORKERMAN CONNECTION STATUS --------------------------------------------------------------------------------\n", \FILE_APPEND);
+            \file_put_contents(static::$_statisticsFile, "PID      Worker          CID       Trans   Protocol        ipv4   ipv6   Recv-Q       Send-Q       Bytes-R      Bytes-W       Status         Local Address          Foreign Address\n", \FILE_APPEND);
+            \chmod(static::$_statisticsFile, 0722);
+            foreach (static::getAllWorkerPids() as $worker_pid) {
+                \posix_kill($worker_pid, \SIGIO);
+            }
+            return;
+        }
+
+        // For child processes.
+        $bytes_format = function($bytes)
+        {
+            if($bytes > 1024*1024*1024*1024) {
+                return round($bytes/(1024*1024*1024*1024), 1)."TB";
+            }
+            if($bytes > 1024*1024*1024) {
+                return round($bytes/(1024*1024*1024), 1)."GB";
+            }
+            if($bytes > 1024*1024) {
+                return round($bytes/(1024*1024), 1)."MB";
+            }
+            if($bytes > 1024) {
+                return round($bytes/(1024), 1)."KB";
+            }
+            return $bytes."B";
+        };
+
+        $pid = \posix_getpid();
+        $str = '';
+        \reset(static::$_workers);
+        $current_worker = current(static::$_workers);
+        $default_worker_name = $current_worker->name;
+
+        /** @var \Workerman\Worker $worker */
+        foreach(TcpConnection::$connections as $connection) {
+            /** @var \Workerman\Connection\TcpConnection $connection */
+            $transport      = $connection->transport;
+            $ipv4           = $connection->isIpV4() ? ' 1' : ' 0';
+            $ipv6           = $connection->isIpV6() ? ' 1' : ' 0';
+            $recv_q         = $bytes_format($connection->getRecvBufferQueueSize());
+            $send_q         = $bytes_format($connection->getSendBufferQueueSize());
+            $local_address  = \trim($connection->getLocalAddress());
+            $remote_address = \trim($connection->getRemoteAddress());
+            $state          = $connection->getStatus(false);
+            $bytes_read     = $bytes_format($connection->bytesRead);
+            $bytes_written  = $bytes_format($connection->bytesWritten);
+            $id             = $connection->id;
+            $protocol       = $connection->protocol ? $connection->protocol : $connection->transport;
+            $pos            = \strrpos($protocol, '\\');
+            if ($pos) {
+                $protocol = \substr($protocol, $pos+1);
+            }
+            if (\strlen($protocol) > 15) {
+                $protocol = \substr($protocol, 0, 13) . '..';
+            }
+            $worker_name = isset($connection->worker) ? $connection->worker->name : $default_worker_name;
+            if (\strlen($worker_name) > 14) {
+                $worker_name = \substr($worker_name, 0, 12) . '..';
+            }
+            $str .= \str_pad($pid, 9) . \str_pad($worker_name, 16) .  \str_pad($id, 10) . \str_pad($transport, 8)
+                . \str_pad($protocol, 16) . \str_pad($ipv4, 7) . \str_pad($ipv6, 7) . \str_pad($recv_q, 13)
+                . \str_pad($send_q, 13) . \str_pad($bytes_read, 13) . \str_pad($bytes_written, 13) . ' '
+                . \str_pad($state, 14) . ' ' . \str_pad($local_address, 22) . ' ' . \str_pad($remote_address, 22) ."\n";
+        }
+        if ($str) {
+            \file_put_contents(static::$_statisticsFile, $str, \FILE_APPEND);
+        }
+    }
+
+    /**
+     * Check errors when current process exited.
+     *
+     * @return void
+     */
+    public static function checkErrors()
+    {
+        if (static::STATUS_SHUTDOWN !== static::$_status) {
+            $error_msg = static::$_OS === \OS_TYPE_LINUX ? 'Worker['. \posix_getpid() .'] process terminated' : 'Worker process terminated';
+            $errors    = error_get_last();
+            if ($errors && ($errors['type'] === \E_ERROR ||
+                    $errors['type'] === \E_PARSE ||
+                    $errors['type'] === \E_CORE_ERROR ||
+                    $errors['type'] === \E_COMPILE_ERROR ||
+                    $errors['type'] === \E_RECOVERABLE_ERROR)
+            ) {
+                $error_msg .= ' with ERROR: ' . static::getErrorType($errors['type']) . " \"{$errors['message']} in {$errors['file']} on line {$errors['line']}\"";
+            }
+            static::log($error_msg);
+        }
+    }
+
+    /**
+     * Get error message by error code.
+     *
+     * @param integer $type
+     * @return string
+     */
+    protected static function getErrorType($type)
+    {
+        if(isset(self::$_errorType[$type])) {
+            return self::$_errorType[$type];
+        }
+
+        return '';
+    }
+
+    /**
+     * Log.
+     *
+     * @param string $msg
+     * @return void
+     */
+    public static function log($msg)
+    {
+        $msg = $msg . "\n";
+        if (!static::$daemonize) {
+            static::safeEcho($msg);
+        }
+        \file_put_contents((string)static::$logFile, \date('Y-m-d H:i:s') . ' ' . 'pid:'
+            . (static::$_OS === \OS_TYPE_LINUX ? \posix_getpid() : 1) . ' ' . $msg, \FILE_APPEND | \LOCK_EX);
+    }
+
+    /**
+     * Safe Echo.
+     * @param string $msg
+     * @param bool   $decorated
+     * @return bool
+     */
+    public static function safeEcho($msg, $decorated = false)
+    {
+        $stream = static::outputStream();
+        if (!$stream) {
+            return false;
+        }
+        if (!$decorated) {
+            $line = $white = $green = $end = '';
+            if (static::$_outputDecorated) {
+                $line = "\033[1A\n\033[K";
+                $white = "\033[47;30m";
+                $green = "\033[32;40m";
+                $end = "\033[0m";
+            }
+            $msg = \str_replace(array('<n>', '<w>', '<g>'), array($line, $white, $green), $msg);
+            $msg = \str_replace(array('</n>', '</w>', '</g>'), $end, $msg);
+        } elseif (!static::$_outputDecorated) {
+            return false;
+        }
+        \fwrite($stream, $msg);
+        \fflush($stream);
+        return true;
+    }
+
+    /**
+     * @param null $stream
+     * @return bool|resource
+     */
+    private static function outputStream($stream = null)
+    {
+        if (!$stream) {
+            $stream = static::$_outputStream ? static::$_outputStream : \STDOUT;
+        }
+        if (!$stream || !\is_resource($stream) || 'stream' !== \get_resource_type($stream)) {
+            return false;
+        }
+        $stat = \fstat($stream);
+        if (!$stat) {
+            return false;
+        }
+        if (($stat['mode'] & 0170000) === 0100000) {
+            // file
+            static::$_outputDecorated = false;
+        } else {
+            static::$_outputDecorated =
+                static::$_OS === \OS_TYPE_LINUX &&
+                \function_exists('posix_isatty') &&
+                \posix_isatty($stream);
+        }
+        return static::$_outputStream = $stream;
+    }
+
+    /**
+     * Construct.
+     *
+     * @param string $socket_name
+     * @param array  $context_option
+     */
+    public function __construct($socket_name = '', array $context_option = array())
+    {
+        // Save all worker instances.
+        $this->workerId                    = \spl_object_hash($this);
+        static::$_workers[$this->workerId] = $this;
+        static::$_pidMap[$this->workerId]  = array();
+
+        // Get autoload root path.
+        $backtrace               = \debug_backtrace();
+        $this->_autoloadRootPath = \dirname($backtrace[0]['file']);
+        Autoloader::setRootPath($this->_autoloadRootPath);
+
+        // Context for socket.
+        if ($socket_name) {
+            $this->_socketName = $socket_name;
+            if (!isset($context_option['socket']['backlog'])) {
+                $context_option['socket']['backlog'] = static::DEFAULT_BACKLOG;
+            }
+            $this->_context = \stream_context_create($context_option);
+        }
+
+        // Turn reusePort on.
+        if (static::$_OS === \OS_TYPE_LINUX  // if linux
+            && \version_compare(\PHP_VERSION,'7.0.0', 'ge') // if php >= 7.0.0
+            && \strtolower(\php_uname('s')) !== 'darwin' // if not Mac OS
+            && $this->transport !== 'unix') { // if not unix socket
+
+            $this->reusePort = true;
+        }
+    }
+
+
+    /**
+     * Listen.
+     *
+     * @throws Exception
+     */
+    public function listen()
+    {
+        if (!$this->_socketName) {
+            return;
+        }
+
+        // Autoload.
+        Autoloader::setRootPath($this->_autoloadRootPath);
+
+        if (!$this->_mainSocket) {
+
+            $local_socket = $this->parseSocketAddress();
+
+            // Flag.
+            $flags = $this->transport === 'udp' ? \STREAM_SERVER_BIND : \STREAM_SERVER_BIND | \STREAM_SERVER_LISTEN;
+            $errno = 0;
+            $errmsg = '';
+            // SO_REUSEPORT.
+            if ($this->reusePort) {
+                \stream_context_set_option($this->_context, 'socket', 'so_reuseport', 1);
+            }
+
+            // Create an Internet or Unix domain server socket.
+            $this->_mainSocket = \stream_socket_server($local_socket, $errno, $errmsg, $flags, $this->_context);
+            if (!$this->_mainSocket) {
+                throw new Exception($errmsg);
+            }
+
+            if ($this->transport === 'ssl') {
+                \stream_socket_enable_crypto($this->_mainSocket, false);
+            } elseif ($this->transport === 'unix') {
+                $socket_file = \substr($local_socket, 7);
+                if ($this->user) {
+                    \chown($socket_file, $this->user);
+                }
+                if ($this->group) {
+                    \chgrp($socket_file, $this->group);
+                }
+            }
+
+            // Try to open keepalive for tcp and disable Nagle algorithm.
+            if (\function_exists('socket_import_stream') && static::$_builtinTransports[$this->transport] === 'tcp') {
+                \set_error_handler(function(){});
+                $socket = \socket_import_stream($this->_mainSocket);
+                \socket_set_option($socket, \SOL_SOCKET, \SO_KEEPALIVE, 1);
+                \socket_set_option($socket, \SOL_TCP, \TCP_NODELAY, 1);
+                \restore_error_handler();
+            }
+
+            // Non blocking.
+            \stream_set_blocking($this->_mainSocket, false);
+        }
+
+        $this->resumeAccept();
+    }
+
+    /**
+     * Unlisten.
+     *
+     * @return void
+     */
+    public function unlisten() {
+        $this->pauseAccept();
+        if ($this->_mainSocket) {
+            \set_error_handler(function(){});
+            \fclose($this->_mainSocket);
+            \restore_error_handler();
+            $this->_mainSocket = null;
+        }
+    }
+
+    /**
+     * Parse local socket address.
+     *
+     * @throws Exception
+     */
+    protected function parseSocketAddress() {
+        if (!$this->_socketName) {
+            return;
+        }
+        // Get the application layer communication protocol and listening address.
+        list($scheme, $address) = \explode(':', $this->_socketName, 2);
+        // Check application layer protocol class.
+        if (!isset(static::$_builtinTransports[$scheme])) {
+            $scheme         = \ucfirst($scheme);
+            $this->protocol = \substr($scheme,0,1)==='\\' ? $scheme : '\\Protocols\\' . $scheme;
+            if (!\class_exists($this->protocol)) {
+                $this->protocol = "\\Workerman\\Protocols\\$scheme";
+                if (!\class_exists($this->protocol)) {
+                    throw new Exception("class \\Protocols\\$scheme not exist");
+                }
+            }
+
+            if (!isset(static::$_builtinTransports[$this->transport])) {
+                throw new Exception('Bad worker->transport ' . \var_export($this->transport, true));
+            }
+        } else {
+            $this->transport = $scheme;
+        }
+        //local socket
+        return static::$_builtinTransports[$this->transport] . ":" . $address;
+    }
+
+    /**
+     * Pause accept new connections.
+     *
+     * @return void
+     */
+    public function pauseAccept()
+    {
+        if (static::$globalEvent && false === $this->_pauseAccept && $this->_mainSocket) {
+            static::$globalEvent->del($this->_mainSocket, EventInterface::EV_READ);
+            $this->_pauseAccept = true;
+        }
+    }
+
+    /**
+     * Resume accept new connections.
+     *
+     * @return void
+     */
+    public function resumeAccept()
+    {
+        // Register a listener to be notified when server socket is ready to read.
+        if (static::$globalEvent && true === $this->_pauseAccept && $this->_mainSocket) {
+            if ($this->transport !== 'udp') {
+                static::$globalEvent->add($this->_mainSocket, EventInterface::EV_READ, array($this, 'acceptConnection'));
+            } else {
+                static::$globalEvent->add($this->_mainSocket, EventInterface::EV_READ, array($this, 'acceptUdpConnection'));
+            }
+            $this->_pauseAccept = false;
+        }
+    }
+
+    /**
+     * Get socket name.
+     *
+     * @return string
+     */
+    public function getSocketName()
+    {
+        return $this->_socketName ? \lcfirst($this->_socketName) : 'none';
+    }
+
+    /**
+     * Run worker instance.
+     *
+     * @return void
+     */
+    public function run()
+    {
+        //Update process state.
+        static::$_status = static::STATUS_RUNNING;
+
+        // Register shutdown function for checking errors.
+        \register_shutdown_function(array("\\Workerman\\Worker", 'checkErrors'));
+
+        // Set autoload root path.
+        Autoloader::setRootPath($this->_autoloadRootPath);
+
+        // Create a global event loop.
+        if (!static::$globalEvent) {
+            $event_loop_class = static::getEventLoopName();
+            static::$globalEvent = new $event_loop_class;
+            $this->resumeAccept();
+        }
+
+        // Reinstall signal.
+        static::reinstallSignal();
+
+        // Init Timer.
+        Timer::init(static::$globalEvent);
+
+        // Set an empty onMessage callback.
+        if (empty($this->onMessage)) {
+            $this->onMessage = function () {};
+        }
+
+        \restore_error_handler();
+
+        // Try to emit onWorkerStart callback.
+        if ($this->onWorkerStart) {
+            try {
+                \call_user_func($this->onWorkerStart, $this);
+            } catch (\Exception $e) {
+                static::log($e);
+                // Avoid rapid infinite loop exit.
+                sleep(1);
+                exit(250);
+            } catch (\Error $e) {
+                static::log($e);
+                // Avoid rapid infinite loop exit.
+                sleep(1);
+                exit(250);
+            }
+        }
+
+        // Main loop.
+        static::$globalEvent->loop();
+    }
+
+    /**
+     * Stop current worker instance.
+     *
+     * @return void
+     */
+    public function stop()
+    {
+        // Try to emit onWorkerStop callback.
+        if ($this->onWorkerStop) {
+            try {
+                \call_user_func($this->onWorkerStop, $this);
+            } catch (\Exception $e) {
+                static::log($e);
+                exit(250);
+            } catch (\Error $e) {
+                static::log($e);
+                exit(250);
+            }
+        }
+        // Remove listener for server socket.
+        $this->unlisten();
+        // Close all connections for the worker.
+        if (!static::$_gracefulStop) {
+            foreach ($this->connections as $connection) {
+                $connection->close();
+            }
+        }
+        // Clear callback.
+        $this->onMessage = $this->onClose = $this->onError = $this->onBufferDrain = $this->onBufferFull = null;
+    }
+
+    /**
+     * Accept a connection.
+     *
+     * @param resource $socket
+     * @return void
+     */
+    public function acceptConnection($socket)
+    {
+        // Accept a connection on server socket.
+        \set_error_handler(function(){});
+        $new_socket = \stream_socket_accept($socket, 0, $remote_address);
+        \restore_error_handler();
+
+        // Thundering herd.
+        if (!$new_socket) {
+            return;
+        }
+
+        // TcpConnection.
+        $connection                         = new TcpConnection($new_socket, $remote_address);
+        $this->connections[$connection->id] = $connection;
+        $connection->worker                 = $this;
+        $connection->protocol               = $this->protocol;
+        $connection->transport              = $this->transport;
+        $connection->onMessage              = $this->onMessage;
+        $connection->onClose                = $this->onClose;
+        $connection->onError                = $this->onError;
+        $connection->onBufferDrain          = $this->onBufferDrain;
+        $connection->onBufferFull           = $this->onBufferFull;
+
+        // Try to emit onConnect callback.
+        if ($this->onConnect) {
+            try {
+                \call_user_func($this->onConnect, $connection);
+            } catch (\Exception $e) {
+                static::log($e);
+                exit(250);
+            } catch (\Error $e) {
+                static::log($e);
+                exit(250);
+            }
+        }
+    }
+
+    /**
+     * For udp package.
+     *
+     * @param resource $socket
+     * @return bool
+     */
+    public function acceptUdpConnection($socket)
+    {
+        \set_error_handler(function(){});
+        $recv_buffer = \stream_socket_recvfrom($socket, static::MAX_UDP_PACKAGE_SIZE, 0, $remote_address);
+        \restore_error_handler();
+        if (false === $recv_buffer || empty($remote_address)) {
+            return false;
+        }
+        // UdpConnection.
+        $connection           = new UdpConnection($socket, $remote_address);
+        $connection->protocol = $this->protocol;
+        if ($this->onMessage) {
+            try {
+                if ($this->protocol !== null) {
+                    /** @var \Workerman\Protocols\ProtocolInterface $parser */
+                    $parser      = $this->protocol;
+                    if(\method_exists($parser,'input')){
+                        while($recv_buffer !== ''){
+                            $len = $parser::input($recv_buffer, $connection);
+                            if($len === 0)
+                                return true;
+                            $package = \substr($recv_buffer,0,$len);
+                            $recv_buffer = \substr($recv_buffer,$len);
+                            $data = $parser::decode($package,$connection);
+                            if ($data === false)
+                                continue;
+                            \call_user_func($this->onMessage, $connection, $data);
+                        }
+                    }else{
+                        $data = $parser::decode($recv_buffer, $connection);
+                        // Discard bad packets.
+                        if ($data === false)
+                            return true;
+                        \call_user_func($this->onMessage, $connection, $data);
+                    }
+                }else{
+                    \call_user_func($this->onMessage, $connection, $recv_buffer);
+                }
+                ++ConnectionInterface::$statistics['total_request'];
+            } catch (\Exception $e) {
+                static::log($e);
+                exit(250);
+            } catch (\Error $e) {
+                static::log($e);
+                exit(250);
+            }
+        }
+        return true;
+    }
+}

+ 38 - 0
GatewayWorker/vendor/workerman/workerman/composer.json

@@ -0,0 +1,38 @@
+{
+    "name": "workerman/workerman",
+    "type": "library",
+    "keywords": [
+        "event-loop",
+        "asynchronous"
+    ],
+    "homepage": "http://www.workerman.net",
+    "license": "MIT",
+    "description": "An asynchronous event driven PHP framework for easily building fast, scalable network applications.",
+    "authors": [
+        {
+            "name": "walkor",
+            "email": "walkor@workerman.net",
+            "homepage": "http://www.workerman.net",
+            "role": "Developer"
+        }
+    ],
+    "support": {
+        "email": "walkor@workerman.net",
+        "issues": "https://github.com/walkor/workerman/issues",
+        "forum": "http://wenda.workerman.net/",
+        "wiki": "http://doc.workerman.net/",
+        "source": "https://github.com/walkor/workerman"
+    },
+    "require": {
+        "php": ">=5.3"
+    },
+    "suggest": {
+        "ext-event": "For better performance. "
+    },
+    "autoload": {
+        "psr-4": {
+            "Workerman\\": "./"
+        }
+    },
+    "minimum-stability": "dev"
+}

+ 849 - 0
README.md

@@ -0,0 +1,849 @@
+## DSMall介绍
+DSMall商城系统是基于ThinkPhp6.0+Vue开发的一套完善的B2B2C(多店铺商城)电商系统,DSMall商城系统能够快速积累客户、会员数据分析、智能转化客户、 有效提高销售、会员维护、网络营销的一款企业级应用,功能包含拼团、砍价、秒杀、优惠券、积分、分销、刮刮卡等功能,更适合企业二次开发
+
+
+## 导航栏目
+ [帮助手册](http://www.csdeshang.com/home/help/index/id/99.html)
+ | [功能清单](http://www.csdeshang.com/home/dsmall/feature.html)
+ | [官网地址](http://www.csdeshang.com)
+ | [TP6开发手册](https://www.kancloud.cn/manual/thinkphp6_0/1037479)
+ | [Vue.js手册](https://cn.vuejs.org/)
+
+
+## QQ交流群
+DSMall开源商城官方群:10235778  <a target="_blank" href="//shang.qq.com/wpa/qunwpa?idkey=c75ccf9e6f21a2a3eea7914be3131bc4a7a00abe08cd3aa57532349292e84ffe"><img border="0" src="//pub.idqqimg.com/wpa/images/group.png" alt="DSMall开源商城官方群" title="DSMall开源商城官方群"></a>
+
+
+## 演示Demo
+| 演示角色  | 演示地址                                | 账号 | 账号 |
+|-------|-------------------------------------|----|----|
+| 后台PC端 | https://dsmall.csdeshang.com/admin/ |  dsmall  |  123456  |
+| 用户PC端 | https://dsmall.csdeshang.com/ |  buyer  |  123456  |
+| 商家PC端 | https://dsmall.csdeshang.com/home/sellerlogin/login.html |  buyer  |  123456  |
+| 用户手机端 | https://m.dsmall.csdeshang.com/ |  buyer  |  123456  |
+| 商家手机端 | https://m.dsmall.csdeshang.com/home/sellerlogin |  buyer  |  123456  |
+| 门店手机端 | https://m.dsmall.csdeshang.com/chain/login |  seller  |  123456  |
+
+
+## 技术评价
+1. B/S架构
+2. MVC编码架构,采用Thinkphp6.0框架
+3. 支持Compser
+4. 支持阿里云存储
+5. 支持负载均衡
+6. 支持Mysql读写分离
+7. 支持Redis/Memcached
+8. 支持Linux/Unix/Windows服务器,支持Apache/IIS/Nginx等
+
+
+## 系统功能
+1. 设置:站点设置、账号同步、上传设置、SEO设置、邮箱短信、支付方式(支付宝/微信/银联)、权限设置、快递公司、地区管理、数据备份、操作日志
+2. 会员:会员管理、会员级别、经验值管理、会员通知、积分管理、预存款、聊天记录
+3. 商品:商品分类、品牌管理、商品管理、类型管理、规格管理、空间管理
+4. 店铺:店铺管理、店铺资金、店铺保证金、店铺等级、店铺分类、店铺动态、店铺帮助、自营店铺
+5. 交易:实物订单、虚拟订单、退款管理、退货管理、订单结算、咨询管理、举报管理、评价管理、结算管理
+6. 网站:文章分类、文章管理、会员协议、导航管理、广告管理、PC端装修、手机装修、友情链接、平台客服
+7. 营销:分销管理、抢购管理、虚拟抢购管理、拼团管理、限时折扣、满即送、优惠套装、推荐展位、会员等级折扣、代金券管理、活动管理、兑换礼品、平台充值卡、吸粉红包、刮刮卡、幸运大抽奖、幸运砸金蛋、生肖翻翻看
+8. 统计:行业分析、会员统计、店铺统计、销量统计、商品统计
+9. 公众号:公众号配置、微信菜单、关键字回复、绑定列表、消息推送
+10. 直播:直播设置、直播申请、直播聊天
+
+
+
+## 相关依赖SDK安装
+1. 多应用模式扩展  composer require topthink/think-multi-app
+2. think-view      composer require topthink/think-view
+3. think-captcha   composer require topthink/think-captcha
+4. think-image     composer require topthink/think-image
+5. thinkphp-jump   composer require liliuwei/thinkphp-jump
+5. 阿里云OSS       composer require aliyuncs/oss-sdk-php   
+  介绍地址:https://help.aliyun.com/document_detail/32099.html?spm=5176.87240.400427.47.eaLg1R
+6. phpmailer       composer require phpmailer/phpmailer
+7. 阿里云短信      composer require alibabacloud/client
+8. 腾讯云短信      composer require qcloudsms/qcloudsms_php
+9. 签名工具        composer require firebase/php-jwt
+10. 腾讯云点播      composer require tencentcloud/tencentcloud-sdk-php
+   安装tencentcloud  PHP 5.6.33 版本及以上。https://github.com/tencentcloud/tencentcloud-sdk-php
+11. 进入GatewayWorker子目录安装 composer install
+12. 安装GatewayClient  composer require workerman/gatewayclient
+
+
+
+## 安装教程
+1. 将源码解压到服务器空间
+2. 域名应该指向到public目录,因为应用入口文件位于public/index.php。比如我的DSMALL项目在  D:\www\dsmall  域名应该指向到 D:\www\dsmall\public
+3. 进行安装 http://域名/install/install.php
+4. 后台地址:http://域名/index.php/admin
+5. 前台地址:http://域名/index.php/home
+
+如果还有什么不懂的到DSMALL论坛(http://www.csdeshang.com)进行提问,以及下载最新版本。
+
+
+## APIDOC 生成API
+apidoc -i application/api/controller -o public/apidoc/
+
+
+## 更新日志
+#### V6.1.8
+免费版更新
+1. 新增 平台代金券页面信息
+2. 新增 收货地址门店地址核验提示
+3. 新增 商品限制配送说明信息
+4. 更新 可视化编辑促销活动搜索商品,只能搜索到活动开始的商品
+5. 优化 可视化编辑设置
+6. 优化 平台代金券显示
+7. 优化 门店地址显示
+
+授权版更新
+1. 新增 平台代金券领取和查看
+2. 新增 收货地址门店地址核验提示
+3. 新增 商品限制配送说明信息
+4. 修复 商品列表缓存无法更新
+5. 优化 下单流程
+6. 优化 用户绑定页面显示
+
+#### V6.1.7
+免费版更新
+1. 新增 私密代金券查看
+2. 新增 会员余额和店铺资金互转
+3. 新增 平台代金券
+4. 修复 店铺动态页面评论里面会员头像不显示
+5. 修复 后台无法操作屏蔽商家动态
+6. 修复 店铺后台 H5装修 砍价插件无法选择商品
+7. 优化 微信接口
+8. 优化 后台部分显示
+
+授权版更新
+1. 新增 平台代金券
+2. 新增 订单列表搜索
+3. 新增 商品详情页面店铺保障和店铺保证金显示
+4. 修复 部分可视化编辑组件显示错误
+5. 优化 可视化编辑功能部分组件显示
+6. 优化 商品列表和商品详情跳转显示
+
+#### V6.1.6
+免费版更新
+1. 新增 地区名称首字母
+2. 新增 高德地图
+3. 修复 店铺图片空间转移图片跳转
+4. 修复 编辑商品规格图片页面切换图片相册失效
+5. 优化 微信登录
+6. 优化 可视化编辑
+7. 优化 商家促销
+8. 优化 部分后台显示
+
+授权版更新
+1. 新增 高德地图
+2. 新增 商品列表页面展示商品排版切换
+3. 优化 可视化编辑
+4. 优化 商品详情页面显示
+5. 优化 卖家中心分销商品页面显示
+6. 优化 下单页面选择自提点提示
+7. 优化 卖家中心添加规格页面显示
+
+#### V6.1.5
+免费版更新
+1. 新增 快递鸟电子面单
+2. 新增 卡券、网盘和下载类商品
+3. 新增 微信素材库
+4. 优化 微信菜单
+5. 优化 文章图片上传提示
+
+授权版更新
+1. 新增 卡券、网盘和下载类商品
+2. 修复 文章列表页面一页10条数据无法翻页
+3. 修复 积分订单详情页面 下单时间不显示
+4. 优化 实物订单详情页面
+5. 优化 会员中心部分页面
+6. 优化 卖家中心部分页面
+
+#### V6.1.4
+免费版更新
+1. 新增 店铺中心用户可查看店铺的身份证或者营业执照
+2. 新增 商品分类默认图
+3. 新增 私密代金券功能
+4. 更新 最新的省市区信息
+5. 修复 商家等级对图片上传数量的限制不生效
+6. 修复 第三方快捷登录用户注册成为商家无法进入店铺中心
+7. 优化 部分页面语言项
+8. 优化 直播功能
+9. 优化 分销功能
+
+授权版更新
+1. 新增 私密代金券功能
+2. 新增 店铺首页显示店铺评分信息
+3. 修复 手机端首页广告已到期还在显示
+4. 修复 店铺新增账号组失败
+5. 修复 商品多规格会在商品列表里面重复显示
+6. 优化 分销功能
+7. 优化 首页、会员中心、收藏、浏览记录、我的钱包等页面显示,手机端整体页面优化
+
+
+#### V6.1.3
+免费版更新
+1. 新增 后台营销,拼团、预售、砍价活动删除功能
+2. 新增 商家入驻线上缴费
+3. 修复 后台咨询店铺链接错误
+4. 修复 商品已经选择了匿名评价,鼠标放到评价者头像上的时候还是会看到会员名
+5. 修复 F码商品无法下单
+6. 修复 平台后台商品分类设置里面推荐分类和推荐品牌无法清空勾选
+7. 优化 直播
+8. 优化 站点关闭
+9. 优化 商家入住时卖家登录账号和买家登录账号一致 
+10.优化 隐藏手机号
+
+授权版更新
+1. 新增 商家入驻线上缴费
+2. 新增 商家入驻付款页面添加一个平台联系电话,方便商家沟通
+3. 修复 手机端只展示10条评论 无法翻页
+4. 修复 提现选项多个无法滑动
+5. 修复 实名认证页面剪裁图片没有使用按钮
+6. 修复 直播封面说明现只能用图片
+7. 优化 直播
+8. 优化 商家入住时卖家登录账号和买家登录账号一致 
+9. 优化 商家入驻详细地址选择时地址 
+
+
+#### V6.1.2
+免费版更新
+1. 新增 手机端访问商品详情和店铺首页统计入店铺流量
+2. 新增 商品详情页面SEO KEY信息
+3. 更新 后台优惠套餐可设置商品最大值由5个修改为6个
+4. 更新 关注红包名称为注册红包,并删除后台注册红包和奖品红包的链接地址按钮
+5. 更新 个人入驻第一步里面店铺名称为线下店铺名称
+6. 修复 商家中心单个图片转移相册提示语不显示的问题
+7. 修复 会员中心头部用户中心模块里面链接错误的问题
+8. 修复 库存预警和自提订单消息存储不了的问题
+9. 修复 首页悬浮图片删除按钮不显示的问题
+10.修复 文章列表以及文章详情页面文章分类为空,现新增文章分类内容
+11.优化 商家中心拼团、砍价、预售、推荐展位 四个促销活动购买套餐后跳转到对应活动列表页面
+12.优化 退款退货页面申请退款服务类型选择显示
+13.优化 商品详情页面,商品规格库存为0不能点击选择该商品规格
+
+授权版更新
+1. 新增 访问商品和店铺首页,统计入店铺流量
+2. 修复 申请提现后,后台申请显示页面收款银行文字显示错误
+3. 修复 商品详情页面点击图片显示黑屏的问题
+4. 修复 积分代金券页面成功兑换代金券没有提示的问题
+5. 优化 商品分类页面加载很慢的问题
+6. 优化 订单详情页面显示
+7. 优化 登录
+8. 优化 商家中心添加商品流程
+9. 优化 下单页面商品支持货到付款文字显示
+10.优化 手机端编辑发票页面显示
+11.优化 商品规格库存为0时不允许点击
+
+
+#### V6.1.1
+免费版更新
+1. 新增 IM列表加一个我关注的人列表
+2. 新增 商家待后台审核的商品,如果平台关闭审核,是否所有商品通过审核的开关
+3. 新增 普通注册是否开启开关
+4. 更新 商品视频上传大小由原来的2M改为20M
+5. 修复 微信账号绑定会员邮箱和手机切换拉取不到会员信息
+6. 修复 手机号码注册的会员无法获取注册红包的问题
+7. 修复 所有分类页面不显示图片的问题
+8. 修复 闲置分类页面搜索不到商品的问题
+9. 修复 商品规格市场价批量处理的时候不能设置小数点的问题
+10.修复 微信菜单收藏量按钮无法生效的问题
+11.修复 商品规格市场价批量处理的时候不能设置小数点的问题
+12.优化 猜你喜欢只显示一个商品规格
+13.优化 商品详情查看图片时关闭视频
+
+授权版更新
+1. 新增 会员中心显示会员等级
+2. 新增 订单删除功能
+3. 新增 普通注册开关
+4. 美化 首页导航图标
+5. 美化 底部导航图标
+6. 美化 会员中心图标
+7. 美化 首页 分类页面 商品详情 商品列表 下单 登录 注册 会员中心等各个页面
+8. 优化 猜你喜欢只显示一个商品规格
+
+#### V6.1.0
+免费版更新
+1. 新增 附近门店功能
+2. 新增 商家后台商品列表页面按库存排序展示商品
+3. 新增 百度敏感词过滤功能
+4. 新增 百度图片审核功能
+5. 修复 后台会员列表 旺旺客服图片不显示和无法发起会话的问题
+6. 修复 邮件发送 邮件内容里面链接错误的问题
+7. 修复 商品咨询验证码点击不变的问题
+8. 修复 代金券上传图片没有变化的问题
+9. 优化 发货单打印显示
+10.优化 拼团功能和美化
+11.优化 淘宝导入
+12.优化 下单付款页面操作
+
+授权版更新
+1. 新增 手机端积分中心
+2. 新增 卖家后台商品规格管理功能
+3. 新增 附近门店功能
+4. 新增 银联WAP支付
+5. 修复 个人信息页面出生日期显示错误
+6. 修复 商品列表属性筛选窗口 属性太多无法下拉的问题
+7. 修复 手机端好友列表搜索结果无法翻页显示更多的问题
+8. 美化 手机端砍价页面
+9. 美化 拼团
+10.美化 会员中心和店铺中心
+11.美化 砍价
+
+#### V6.0.9
+免费版更新
+1. 更新 取消订单和收到货款的事物放到控制器中处理,因为mysql的事物不能嵌套
+2. 修复 可视化编辑保存报错
+3. 修复 发邮件乱码
+4. 修复 邮件地址没有域名导致跳转错误的问题
+5. 修复 取消后还显示限时折扣的bug
+6. 修复 后台订单统计只统计一条数据的bug
+7. 修复 xss攻击漏洞
+8. 修复 统计导出报错
+9. 优化 PC端生日年份选择
+
+
+授权版更新
+1. 新增 预售功能
+2. 更新 取消订单和收到货款的事物放到控制器中处理,因为mysql的事物不能嵌套
+3. 更新 预存款/充值卡足额支付后的逻辑可统一放到收到货款处理
+4. 修复 用户使用了预存款支付了部分金额后,如果卖家调整了订单金额且金额小于用户已支付的金额则待支付金额显示为负数
+5. 修复 会员协议详情,上传的图片,进行删除,点击确定之后,图片能够删除,弹出不关闭
+6. 修复 取消后还显示限时折扣的bug
+7. 修复 砍价详情页面 已砍价  价格显示多位小数点的问题
+8. 修复 后台调整用户积分后不能享受会员折扣的bug
+9. 修复 推广海报头像太大扫不出二维码的问题
+10.修复 到货通知缺少参数
+11.修复 统计导出报错
+12.修复 xss攻击漏洞
+13.修复 邮件地址没有域名导致跳转错误的问题
+
+#### V6.0.8
+免费版更新
+1. 新增 后台编辑页面底部显示备案号,网安备信息
+2. 新增 物流缺省值
+3. 更新 当前分销模式 从店铺后台可以独立设置分销比例 平台后台统一设置分销比例
+4. 修复 修改用户昵称页面顶部昵称显示不同步显示修改的问题
+5. 修复 后台同时上传后台LOGO和前端LOGO的时候,不生效的问题
+6. 修复 商家后台 商品列表 快捷修改商品价格后 前端不同步显示的问题
+7. 修复 部分页面OSS图片显示的问题
+8. 修复 自提门店,删除门店按钮无效的问题
+9. 修复 会员头像无法上传的问题
+10.修复 生成个人推广海报的时候,获取不到会员头像的问题
+11.修复 商家端无法查看IM聊天内容的问题
+12.修复 商家端统计显示的问题
+13.修复 虚拟商品下单,选择代金券后商品总价显示不随代金券金额减少的问题
+
+授权版更新
+1. 新增 虚拟下单页面显示预存款余额
+2. 修复 当前分销模式 从店铺后台可以独立设置分销比例 平台后台统一设置分销比例
+3. 修复 移动端自提门店,删除门店按钮无效的问题
+4. 修复 移动端会员头像无法上传的问题
+5. 修复 移动端生成个人推广海报的时候,获取不到会员头像的问题
+6. 修复 移动端分类图片不显示的问题
+7. 优化 移动端点击返回按钮返回操作
+8. 优化 移动端子账号权限
+
+#### V6.0.7
+免费版更新
+1. 新增 小程序组件直播错误提示
+2. 更新 聊天服务器改成workman
+3. 更新 线下门店模块,现虚拟兑换码前缀设置移动到店铺设置页面
+4. 修复 腾讯短信参数问题
+5. 修复 企业入驻上传图片报错的问题,且个人入驻付款页面新增显示付款金额明细和应付总金额
+6. 修复 设置了奖品但是中奖概率为0 造成其他中奖概率为100的无法中奖
+7. 修复 用户领取的红包放到充值卡余额
+8. 修复 微信快捷登录后,无法返回砍价页面的问题
+9. 修复 后台更换会员默认头像不生效的问题
+10.修复 商品编辑页面选择了相册分类但是仍然上传到了默认相册的问题
+11.优化 店铺后台,虚拟订单详情买家名称显示
+12.优化 店铺统计页面
+
+授权版更新
+1. 新增 移动端首页导航抢购页面导航
+2. 更新 聊天服务器改成workman
+3. 修复 直播封面和直播背景图片 不能上传之后,点击 使用按钮无效的问题
+4. 修复 移动端没有设置物流公司时无法选择的问题
+5. 修复 选择货到付款时  还可以选择预存款的问题
+6. 修复 抢购活动页面,切换栏目后,再次点击切换没有数据的问题
+7. 修复 微信快捷登录后,无法返回砍价页面的问题
+8. 优化 门店中心,订单详情页面显示
+
+
+#### V6.0.6
+免费版更新
+1. 新增 小程序组件直播
+2. 新增 自提点功能
+3. 新增 订单列表的退货退款中链接
+4. 修复 周边页面第二个店铺以后都无法显示商品的问题
+5. 修复 对比页面 对比商品的规格值不包含现有规格属性的时候会出错的问题
+6. 修复 打印发货单页面 印章图片的位置
+7. 修复 后台咨询管理页面翻页按钮样式错乱的问题
+8. 修复 后台地区管理删除地区配送地区不同步的问题
+9. 修复 IM聊天时间显示的问题
+10.修复 全站图片可上传到OSS
+11.修复 抢购商品和普通商品一起结算时出错的问题
+12.修复 商家后台 账户组勾选权限不显示的问题
+13.优化 商品搜索
+14.优化 专题页面显示
+
+授权版更新
+1. 新增 小程序组件直播
+2. 新增 手机端装修顶部搜索
+3. 新增 自提点功能
+4. 新增 下单页面显示已优惠多少金额
+5. 新增 手机端显示会员等级折扣
+6. 更新 百度地图接口升级
+7. 修复 微博登录BUG
+8. 修复 手机端商品详情页面点击图片放大后返回上一页在点击其他商品进入商品详情页面会进入之前查看大图模式
+9. 优化 手机端积分记录描述
+10.优化 抢购列表页面
+
+
+#### V6.0.5
+免费版更新
+1. 新增 批发功能
+2. 新增 待付款订单可以预存款支付
+3. 更新 直播商品列表样式
+4. 修复 后台无法恢复备份的问题
+5. 修复 后台关闭4个消息模板后,商家后台接受消息页面会报错的问题
+6. 修复 已退款成功的订单会出现在待评价页面
+
+授权版更新
+1. 新增 批发功能
+2. 新增 待付款订单可以预存款支付
+3. 更新 手机端店铺详情页面 无用的店铺背景图
+4. 更新 商品详情页面没有视频就不显示视频按钮
+5. 修复 已退款成功的订单会出现在待评价页面
+6. 修复 手机端文章列表页面无法翻页的问题
+7. 修复 微信绑定已有用户出错的问题
+
+#### V6.0.4 
+免费版更新
+1. 新增 猜你喜欢功能
+2. 新增 商品主图视频功能
+3. 新增 后台自营店铺可以选择店铺分类
+4. 新增 阿里云直播
+5. 更新 分销会员功能
+6. 更新 评论显示
+7. 更新 注册会员页面
+8. 修复 后台管理员可以添加重复名称的问题
+9. 修复 后台店铺帮助 帮助内容列表不显示内容帮助类型的问题
+10.修复 取消规格选中,隐藏不了规格名称的问题
+11.修复 搜索页面默认排序不生效的问题
+12.修复 后台限时折扣列表批量删除按钮无效的问题
+13.修复 限时折扣活动结束,商品不解除锁定的问题
+14.修复 直播审核页面没有设置直播商品会报错的问题
+
+授权版更新
+1. 新增 商品主图视频
+2. 新增 猜你喜欢功能
+3. 新增 阿里云直播
+4. 新增 手机端商品锁定按钮
+5. 更新 手机端规格市场价和重量取消必填项
+6. 更新 手机端商家入驻添加店铺分类必填项提示语
+7. 更新 微信分享功能
+8. 更新 手机端规格值设置
+9. 修复 手机端添加商品ID错误和图片说明尺寸不对的问题
+10.修复 苹果手机无法使用微信登录的问题
+11.修复 未登录时购物车页面会卡住的问题
+
+#### V6.0.3
+免费版更新
+1. 新增 店铺入驻可选入驻类型,(仅个人,仅企业,全部可选,全部关闭)
+2. 新增 如果商品正在直播则显示直播小图标
+3. 新增 如果店铺有直播 则在店铺首页显示
+4. 更新 PC端主播不在线也可以显示直播详情页
+5. 修复 后台数据无法备份的问题
+6. 修复 首页楼层底部广告没有数据时,会显示侧边栏广告的问题
+7. 修复 现在不上传商品图片也可以添加商品
+8. 修复 添加后台管理员密码可以为空
+9. 优化 推荐组合设置
+10.优化 拼团功能
+11.优化 手机号登录
+12.优化 页面可编辑功能
+
+授权版更新
+1. 新增 手机端专题活动模块
+2. 新增 店铺入驻可选入驻类型,(仅个人,仅企业,全部可选,全部关闭)
+3. 更新 手机端登录图片验证码
+4. 修复 可编辑功能轮播图只显示最后一张图的问题
+5. 优化 手机端快捷登录
+6. 优化 页面
+7. 优化 入驻时的店铺定位
+8. 优化 商品列表排序
+
+#### V6.0.2
+免费版更新
+1. 新增 批量打印发货单和批量发货的功能
+2. 新增 后台LOGO可设置
+3. 新增 直播带货功能
+4. 更新 数据表索引
+5. 更新 举报类型
+6. 修复 微博登录和注册短信验证码不生效的问题
+7. 修复 后台一次不能添加多个会员等级的问题
+8. 修复 个人入驻报错的问题
+9. 修复 店铺登录 前端不显示昵称的问题
+10.修复 API接口赠品数据
+
+授权版更新
+1. 新增 直播带货
+2. 新增 订单列表和订单详情显示赠品
+3. 更新 会员中心显示昵称
+4. 更新 页面显示,用户中心,及下单页面的界面部分美化
+5. 更新 订单详情、新增店铺链接
+6. 修复 当满送的商品被删除或者下架之后,不显示满赠商品
+
+#### V6.0.1
+免费版更新
+1. 更新 入驻营业执照日期添加长期,添加说明文字,结束日期不填则表示营业时间为长期
+2. 修复 验证码错误
+3. 修复 代金券套餐价格为0时,店铺添加完代金券后,在编辑代金券会提示没有购买套餐的问题
+4. 修复 拼团,砍价活动,商品被下架了,手机端还显示这个活动的问题
+5. 修复 用户未登录列表中暂时不显示加入购物车按钮
+6. 优化 后台商品列表显示
+7. 优化 语言项
+
+授权版更新
+1. 新增 手机端头部title可在后台设置
+2. 更新 店铺首页新增显示店铺地图的入口
+3. 更新 下单页面新增店铺链接
+4. 更新 下单页面新增显示预存款数额
+5. 更新 会员中心订单列表页面新增显示订单商品信息
+6. 修复 APP支付返回不到商城的问题
+7. 修复 手机端发布商品的时候商品描述不能添加图片的问题
+8. 修复 店铺首页不显示订单数量的问题
+9. 优化 手机端发票商品,商品规格的选中
+10.优化 评论显示
+
+#### V6.0.1
+Thinkphp由TP5.0.24升级为TP6.0
+
+
+#### V5.1.0
+免费版更新
+1. 新增 腾讯云短信
+2. 新增 商品重量 运费可选择按重量计费
+3. 新增 店铺商品排序
+4. 新增 可视化编辑功能
+5. 更新 取消后台手机端默认广告的删除按钮
+6. 更新 平台后台文章、商品、店铺列表新增显示ID数据 以便广告设置添加对应ID
+7. 修复 微信扫码注册的账号WXINFO里面无法更新openid的问题
+8. 修复 获取提现账号名变量错误
+9. 修复 redis的问题
+10.修复 银联支付的问题
+11.修复 经营类目只有二级时,下单会获取不到分类佣金的问题
+12.修复 平台后台支付配置说明文字
+
+授权版更新
+1. 新增 可视化编辑功能
+2. 更新 删除绑定会员的邮箱验证
+3. 更新 手机端新增活动列表页面
+4. 更新 登录页面新增验证码验证
+5. 更新 提现列表去除微信提现账号
+6. 更新 手机端新增店铺地图导航
+7. 更新 商品详情页面商品有多规格的时候优先弹出规格选择窗口
+8. 修复 注册会员验证码跨域报错
+9. 修复 微信码扫描
+10.修复 支付跨域
+11.修复 双域名无法跨域的问题
+12.修复 手机端商品有促销价格的时候切换规格会显示回正常价格而不是显示促销价的问题
+13.修复 浏览记录页面无法跳转商品的问题
+14.修复 申诉内容不显示的问题
+15.修复 添加商品规格的库存默认值
+16.优化 购物车
+
+#### V5.0.8
+免费版更新
+1. 新增 店铺登录验证码
+2. 新增 后台 添加和编辑礼品页面  删除编辑器图片的成功提示
+3. 更新 会员认证图片时限制字段
+4. 更新 下单时验证代金券金额
+5. 更新 后台礼品兑换详情,详细地址显示三级地区
+6. 更新 当微信未结算资金不足时更换资金来源重试
+7. 更新 商品库存更新不放到缓存里,直接更新,用锁控制
+8. 修复 后台退款详情页面 提交按钮显示
+9. 修复 裁剪头像没验证图片地址的漏洞
+10.修复 前端 店铺首页和商品详情页面左侧店铺地图 使用经纬度定位,使定位更准确。
+11.修复 店铺新增优惠套装,移除商品不会移除价格,导致新增优惠套装里面 没有商品也会添加成功,然后列表页面报错的问题。
+12.修复 后台地区只能设置三级
+13.修复 店铺 个人入驻的问题
+14.修复 积分兑换页面 兑换商品名称长度过长显示的问题
+15.修复 店铺后台 订单结算页面导出EXCEL 按钮失效的问题
+16.修复 会员折扣的问题
+17.修复 阿里云短信因为参数长度问题造成发送不成功
+18.修复 发出红包数量会比实际总数量多一个
+19.修复 统计,会员统计,会员规模分析,点击分页与分页箭头报错提示
+20.修复 平台添加礼品时,不能有效的选择小时的时间
+21.修复 虚拟商品下单会发送两次信息的问题。
+22.修复 淘宝助手导入替换详情图片时,有些情况会出现运行超时的问题
+23.修复 平台商品分类绑定类型,平台在商品类型设置的时候,并没有勾选品牌;商家在发布的商品的时候,却是可以调取所有品牌信息进行选择
+24.修复 店铺后台 发货管理页面无法显示赠品商品图片的问题
+25.修复 平台砍价活动,点击取消修改状态失败
+26.优化 后台统计页面
+
+授权版更新
+1. 新增 分享海报
+2. 新增 商品数量手动输入
+3. 更新 手机端未检查seller_name未填写的情况,导致审核失败
+4. 更新 有些时候点击菜单后进入空白的问题
+5. 更新 扩大重新定位的按钮
+6. 修复 签到开关按钮无效,关闭之后一样可以进行签到
+7. 修复 规格显示问题  手机端自动跳转
+8. 修复 手机端商品下架 购物车依然可以选中下单的问题
+9. 修复 当pc域名是一级域名造成的微信登录问题
+10.修复 手机端抢购页面的上下拉问题
+11.修复 app支付完返回不了app的bug
+12.修复 手机端拼团列表时间显示问题
+13.修复 手机端浏览商品没有浏览记录的问题
+
+#### V5.0.7
+免费版更新
+1. 新增 商品详情页面 限时折扣活动没有设置标题的默认标题
+2. 新增 提现到支付宝、微信
+3. 新增 提现额度范围设置
+4. 新增 意见反馈功能
+5. 更新 支付宝APP支付
+6. 更新 支付宝SDK升级
+7. 更新 去除重复的语言项
+8. 更新 网站后台店铺动态评价-评价分数显示
+9. 更新 后台微信消息模板从微信模块移动到邮箱短信消息模块
+10.修复 第三方登录
+11.修复 分销开关不生效
+12.修复 微博API接口调用不了类的问题
+13.修复 H5路径引用
+14.修复 商家限时折扣 商品列表页面  商品名称带有单引号会报错 不显示商品列表
+15.修复 商家添加砍价活动时 变量名错误
+16.修复 商品规格促销  现一个商品多个规格 每个规格可参与各自的促销
+17.修复 微博绑定nickname报错
+18.修复 手机号注册的会员,在商品评分里面隐藏手机号会员名的中间4位号码
+19.修复 商家导出订单出错
+20.修复 商家修改订单金额,获取的佣金是按原实际金额算
+21.修复 退货详情页面上传凭证图片不显示的问题
+22.修复 发送邮件时,html显示问题
+
+授权版更新
+1. 新增 支付中间页面
+2. 新增 手机端用户反馈
+3. 新增 手机端店铺显示距离
+4. 新增 手机端显示商品分销佣金
+5. 新增 拼团列表、成团列表倒计时
+6. 新增 会员资金相关页面和退款页面的整合页面。
+7. 新增 兑换代金券页面,新增兑换所需积分值和一个兑换按钮。
+8. 新增 商家自己拍的照片都大于2M,自己上传不了,如果上传图片大于2M,则后台可以选择裁切及压缩。
+9. 更新 手机端分销管理
+10.更新 手机端组合营销功能(优惠套餐)显示
+11.更新 支付宝+提现到支付宝、微信
+12.更新 手机端商品详情页面新增显示满送活动里的赠送商品
+13.修复 手机端验证码出错的问题
+14.修复 商品详情页面库存显示。
+15.修复 手机端定位不准的问题
+16.修复 订单预存款支付,支付密码填写错误提交后 就不会在弹出输入密码框
+17.修复 手机端商家入驻的时候 新增无用的经营类目的时候,后台审核页面会报错的BUG。
+
+
+#### V5.0.6
+免费版更新 
+1. 新增 阿里云短信
+2. 新增 后台设置手机端访问PC端自动跳转
+3. 新增 售卖区域制定的区域不邮寄
+4. 新增 会员折扣设置小数
+5. 更新 去除自营店铺显示店铺等级
+6. 更新 去除商品编辑市场价必选项
+7. 更新 去除商城运单功能
+8. 修复 百度地图BUG
+9. 修复 HTTPS网站使用微信登录无法生成二维码
+10.修复 顺丰快递物流BUG
+11.修复 后台管理权限BUG
+12.修复 后台添加店铺增加经营类目
+13.修复 邮箱验证删除多余HOME_SITE_URL和转义
+14.修复 积分说明计算方式
+15.修复 添加分销商品语言项
+
+授权版更新
+1. 新增 商家手机端上传商品
+2. 新增 会员支付密码
+3. 新增 售卖区域,就是制定的区域不邮寄
+4. 新增 商品详情骨架屏
+5. 新增 手机端店铺入驻
+6. 更新 去除注册邮箱必填
+7. 修复 手机端货到付款BUG
+8. 修复 没有规格值的的规格不显示
+9. 修复 推广链接注册不显示推荐员BUG
+10.修复 IOS手机 商品详情页面和商品分类页面滑动卡顿的问题
+
+
+#### V5.0.5
+免费版更新
+1. 新增 规格库存编辑
+2. 新增 虚拟代金券
+3. 新增 后台规格名称和规格分类搜索规格的功能
+4. 更新 时间插件新增中文
+5. 更新 语言项重复替换
+6. 更新 网站LOGO图过大会影响页面显示,限制网站LOGO图最大值为220X46
+7. 修复 非自营店铺的店铺动态页面报错的问题
+8. 修复 用户在未登录的情况下点击聊天没有反应的问题
+9. 修复 快递鸟key参数错误的问题
+10.修复 系统发生的短信未记录到短信日志中,且未做限制
+11.修复 支付宝、微信退款原账号 部分退款、不同店铺退款BUG
+12.修复 只有一级分类时发布商品提示未绑定分类的bug
+13.修复 户收货地址的city_id和area_id错了
+
+授权版更新
+1. 新增 虚拟拼团功能
+2. 修复 手机端个人信息页面选择日期选项 选择完月份会比选择时少一个月和IOS系统不能选择日期的问题
+3. 修复 手机端会员中心订单数量气泡不显示的问题
+4. 修复 手机端支付页面偶尔不出现支付列表的BUG
+
+#### V5.0.4
+免费版更新
+1. 新增 用户首次访问显示悬浮窗
+2. 新增 结算订单显示结单日期
+3. 新增 支付宝/微信订单款项原路退回。
+4. 更新 快递查询接口
+5. 更新 去除重复语言项
+6. 修复 手机端充值卡充值失败问题
+7. 修复 后台搜索举报类型
+8. 修复 商品分类图片上传的限制
+9. 修复 运单模板
+10.优化 商品列表,限时折扣以及抢购 图标
+
+
+授权版更新
+1. 新增 店铺详情信息
+2. 更新 当未添加手机端商品详情时,自动显示PC端商品详细
+3. 修复 附近店铺显示已开启的店铺
+
+
+
+#### V5.0.3
+免费版更新
+1. 新增 单店铺门店模块,含子门店信息以及门店管理员
+2. 新增 实名认证功能,后台开启用户需实名认证才能购买商品。
+3. 更新 分享图片功能
+4. 更新 淘宝CSV文件的商品的导入导出
+5. 更新 支付方式,让显示更友好
+6. 修复 免运费功能缺陷
+7. 修复 未登录时加入失败的提示
+8. 修复 聊天中包含商品时的样式
+9. 优化一系列细节,提高用户体验
+
+
+授权版更新
+1. 更新 手机端筛选功能
+2. 修复 手机端不显示系统消息
+3. 修复 手机端禁止登录账户可正常登录
+4. 修复 当未添加手机端商品详情时,自动显示PC端商品详细
+
+
+
+#### V5.0.1
+免费版更新 
+1. 新增 可视化模板编辑
+2. 新增 PC端砍价列表
+3. 新增 广告图加上链接类型
+4. 新增 淘宝导入导出,商品数据包下载和商品图片下载
+5. 修复 店铺中心店铺名过长的问题
+6. 优化 倒计时插件
+7. 优化 推广二维码界面
+
+授权版更新
+1. 更新 界面美化
+
+
+#### V3.2.6
+免费版更新
+1. 新增 整站推荐功能
+2. 新增 后台编辑店铺排序
+3. 新增 套餐设置为0元时,店铺可免费使用。
+4. 更新 美化用户中心侧边栏界面
+5. 修复 管理员权限菜单bug、美化列表页
+6. 修复 取消订单时间限制
+7. 修复 html过滤后商品名称过长的提示
+8. 修复 店铺已关闭,店铺管理中心未有提示
+9. 优化 导航管理
+
+授权版更新
+1. 新增 红包、大转盘、刮刮卡、砸金蛋、生肖翻翻看等平台活动
+2. 新增 砍价活动
+
+
+#### V3.2.3
+免费版更新
+1. 新增 部分API缓存
+2. 新增 初始安装环境监测openssl扩展 以及 BCMath扩展
+3. 修复 取消微信支付报错
+4. 修复 店铺导航显示问题
+5. 修复 货号退款没有增加库存
+
+授权版更新
+1. 新增 部分API缓存
+2. 更新 微信分享
+3. 更新 手机端详情用原图(因为压缩图显示不完整)
+4. 修复 支付密码错误没提示
+
+
+#### V3.2.2
+免费版更新
+1. 新增 依据电商法行业规范单独的营业执照页
+2. 更新 PC端买家中心/PC端卖家中心界面美化
+3. 修复 163邮箱乱码问题
+4. 修复 非自营店铺货到付款地区设置显示错误
+5. 修复 用户中心通过缓存删除单条浏览记录
+6. 修复 店铺导航显示问题
+7. 修复 微信支付必须开启微信扫码支付
+8. 修复 图片水印无法正常显示
+
+授权版更新
+1. 新增 手机端充值卡记录功能
+2. 新增 签到赠送积分
+
+#### V3.2.1
+授权版更新(ThinkPHP+VUEJS)
+1.新增 H5端卖家管理
+3.新增 手机端查找好友,及时聊天
+4.新增 举报商品
+5.新增 商品咨询
+6.更新 卖家账户/买家账户同步登录
+7.优化 用户绑定手机流程
+
+免费版更新
+1.新增 发票管理
+2.更新 卖家账户/买家账户同步登录
+3.优化 用户绑定手机流程
+4.优化 商品界面
+5.优化 部分界面美化
+
+#### V3.1.1
+1. 新增 管理快递公司
+2. 新增 通联支付
+3. 新增 数据导入导出功能
+4. 优化 重复语言包定义
+5. 优化 开店流程
+
+#### V3.0.3
+1. 新增 分销市场功能
+2. 更新 店铺结算放入日执行任务中
+3. 更新 分销员调整上级的BUG
+4. 修复 微信扫码登录BUG
+5. 优化 闲置语言包以及收藏BUG
+
+#### V3.0.1
+1. 优化 后台界面
+2. 优化 商家结算,管理后台可设置商家结算周期,以及商家可自行申请提现。
+3. 优化 手机端分类页的体验
+4. 优化 后台登陆退出
+
+#### V2.5.7
+1. 更新 初始化数据的多余图片
+2. 修复 微信自动登录没有unionid时需要中断
+3. 修复 苹果手机小程序支付的小BUG
+4. 修复 语言包BUG
+5. 修复 SNS显示错位
+6. 优化 后台界面
+
+
+
+
+
+
+
+

+ 7 - 0
apidoc.json

@@ -0,0 +1,7 @@
+{
+  "name": "DSMall多店铺B2B2C商城",
+  "version": "6.1.8",
+  "description": "DSMall多店铺B2B2C商城",
+  "title": "DSMall多店铺B2B2C商城",
+  "url" : "http://dsmall.csdeshang.com/"
+}

+ 1 - 0
app/.htaccess

@@ -0,0 +1 @@
+deny from all

+ 22 - 0
app/AppService.php

@@ -0,0 +1,22 @@
+<?php
+declare (strict_types = 1);
+
+namespace app;
+
+use think\Service;
+
+/**
+ * 应用服务类
+ */
+class AppService extends Service
+{
+    public function register()
+    {
+        // 服务注册
+    }
+
+    public function boot()
+    {
+        // 服务启动
+    }
+}

+ 205 - 0
app/BaseController.php

@@ -0,0 +1,205 @@
+<?php
+declare (strict_types = 1);
+
+namespace app;
+
+use think\App;
+use think\exception\ValidateException;
+use think\Validate;
+use think\facade\View;
+use think\exception\HttpResponseException;
+use think\Response;
+/**
+ * 控制器基础类
+ */
+abstract class BaseController
+{
+    /**
+     * Request实例
+     * @var \think\Request
+     */
+    protected $request;
+
+    /**
+     * 应用实例
+     * @var \think\App
+     */
+    protected $app;
+
+    /**
+     * 是否批量验证
+     * @var bool
+     */
+    protected $batchValidate = false;
+
+    /**
+     * 控制器中间件
+     * @var array
+     */
+    protected $middleware = [];
+
+    /**
+     * 构造方法
+     * @access public
+     * @param  App  $app  应用对象
+     */
+    public function __construct(App $app)
+    {
+        $this->app     = $app;
+        $this->request = $this->app->request;
+
+        // 控制器初始化
+        $this->initialize();
+    }
+
+    // 初始化
+    protected function initialize()
+    {}
+
+    /**
+     * 验证数据
+     * @access protected
+     * @param  array        $data     数据
+     * @param  string|array $validate 验证器名或者验证规则数组
+     * @param  array        $message  提示信息
+     * @param  bool         $batch    是否批量验证
+     * @return array|string|true
+     * @throws ValidateException
+     */
+    protected function validate(array $data, $validate, array $message = [], bool $batch = false)
+    {
+        if (is_array($validate)) {
+            $v = new Validate();
+            $v->rule($validate);
+        } else {
+            if (strpos($validate, '.')) {
+                // 支持场景
+                [$validate, $scene] = explode('.', $validate);
+            }
+            $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
+            $v     = new $class();
+            if (!empty($scene)) {
+                $v->scene($scene);
+            }
+        }
+
+        $v->message($message);
+
+        // 是否批量验证
+        if ($batch || $this->batchValidate) {
+            $v->batch(true);
+        }
+
+        return $v->failException(true)->check($data);
+    }
+    
+    /**
+     * 操作成功跳转的快捷方法
+     * @access protected
+     * @param  mixed $msg 提示信息
+     * @param  string $url 跳转的URL地址
+     * @param  mixed $data 返回的数据
+     * @param  integer $wait 跳转等待时间
+     * @param  array $header 发送的Header信息
+     * @return void
+     */
+    protected function success($msg = '', string $url = null, $data = '', int $wait = 3, array $header = [])
+    {
+        if (is_null($url) && isset($_SERVER["HTTP_REFERER"])) {
+            $url = $_SERVER["HTTP_REFERER"];
+        } elseif ($url) {
+            $url=(string)$url;
+            $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : (string)$this->app->route->buildUrl($url);
+        }
+
+        $result = [
+            'code' => 1,
+            'msg' => $msg,
+            'data' => $data,
+            'url' => $url,
+            'wait' => $wait,
+        ];
+
+        $type = $this->getResponseType();
+        // 把跳转模板的渲染下沉,这样在 response_send 行为里通过getData()获得的数据是一致性的格式
+        if ('html' == strtolower($type)) {
+            $type = 'view';
+            $response = Response::create($this->app->config->get('jump.dispatch_success_tmpl'), $type)->assign($result)->header($header);
+        } else {
+            $response = Response::create($result, $type)->header($header);
+        }
+
+        throw new HttpResponseException($response);
+    }
+
+    /**
+     * 操作错误跳转的快捷方法
+     * @access protected
+     * @param  mixed $msg 提示信息
+     * @param  string $url 跳转的URL地址
+     * @param  mixed $data 返回的数据
+     * @param  integer $wait 跳转等待时间
+     * @param  array $header 发送的Header信息
+     * @return void
+     */
+    protected function error($msg = '', string $url = null, $data = '', int $wait = 3, array $header = [])
+    {
+        if (is_null($url)) {
+            $url = $this->request->isAjax() ? '' : 'javascript:history.back(-1);';
+        } elseif ($url) {
+            $url=(string)$url;
+            $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : (string)$this->app->route->buildUrl($url);
+        }
+
+        $result = [
+            'code' => 0,
+            'msg' => $msg,
+            'data' => $data,
+            'url' => $url,
+            'wait' => $wait,
+        ];
+
+        $type = $this->getResponseType();
+
+        if ('html' == strtolower($type)) {
+            $type = 'view';
+            $response = Response::create($this->app->config->get('jump.dispatch_error_tmpl'), $type)->assign($result)->header($header);
+        } else {
+            $response = Response::create($result, $type)->header($header);
+        }
+
+        throw new HttpResponseException($response);
+    }
+
+    /**
+     * URL重定向
+     * @access protected
+     * @param  string $url 跳转的URL表达式
+     * @param  integer $code http code
+     * @param  array $with 隐式传参
+     * @return void
+     */
+    protected function redirect($url, $params = [], $code = 302, $with = [])
+    {
+        $url=(string)$url;
+        $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : (string)$this->app->route->buildUrl($url,$params);
+        
+        $response = Response::create($url, 'redirect');
+
+        $response->code($code)->with($with);
+
+        throw new HttpResponseException($response);
+    }
+
+    /**
+     * 获取当前的 response 输出类型
+     * @access protected
+     * @return string
+     */
+    protected function getResponseType()
+    {
+        return request()->isAjax()
+            ? 'json'
+            : 'html';
+    }
+}

+ 58 - 0
app/ExceptionHandle.php

@@ -0,0 +1,58 @@
+<?php
+namespace app;
+
+use think\db\exception\DataNotFoundException;
+use think\db\exception\ModelNotFoundException;
+use think\exception\Handle;
+use think\exception\HttpException;
+use think\exception\HttpResponseException;
+use think\exception\ValidateException;
+use think\Response;
+use Throwable;
+
+/**
+ * 应用异常处理类
+ */
+class ExceptionHandle extends Handle
+{
+    /**
+     * 不需要记录信息(日志)的异常类列表
+     * @var array
+     */
+    protected $ignoreReport = [
+        HttpException::class,
+        HttpResponseException::class,
+        ModelNotFoundException::class,
+        DataNotFoundException::class,
+        ValidateException::class,
+    ];
+
+    /**
+     * 记录异常信息(包括日志或者其它方式记录)
+     *
+     * @access public
+     * @param  Throwable $exception
+     * @return void
+     */
+    public function report(Throwable $exception): void
+    {
+        // 使用内置的方式记录异常日志
+        parent::report($exception);
+    }
+
+    /**
+     * Render an exception into an HTTP response.
+     *
+     * @access public
+     * @param \think\Request   $request
+     * @param Throwable $e
+     * @return Response
+     */
+    public function render($request, Throwable $e): Response
+    {
+        // 添加自定义异常处理机制
+
+        // 其他错误交给系统处理
+        return parent::render($request, $e);
+    }
+}

+ 8 - 0
app/Request.php

@@ -0,0 +1,8 @@
+<?php
+namespace app;
+
+// 应用请求对象类
+class Request extends \think\Request
+{
+protected $filter = ['htmlspecialchars'];
+}

+ 2 - 0
app/admin/common.php

@@ -0,0 +1,2 @@
+<?php
+

+ 7 - 0
app/admin/config/jump.php

@@ -0,0 +1,7 @@
+<?php
+return[
+    //默认错误跳转对应的模板文件
+    'dispatch_error_tmpl' => 'public:dispatch_jump',
+    //默认成功跳转对应的模板文件
+    'dispatch_success_tmpl' => 'public:dispatch_jump',
+];

+ 19 - 0
app/admin/config/session.php

@@ -0,0 +1,19 @@
+<?php
+// +----------------------------------------------------------------------
+// | 会话设置
+// +----------------------------------------------------------------------
+
+return [
+    // session name
+    'name'           => 'PHPSESSID',
+    // SESSION_ID的提交变量,解决flash上传跨域
+    'var_session_id' => '',
+    // 驱动方式 支持file cache
+    'type'           => 'file',
+    // 存储连接标识 当type使用cache的时候有效
+    'store'          => null,
+    // 过期时间
+    'expire'         => 86400,
+    // 前缀
+    'prefix'         => 'admin',
+];

+ 167 - 0
app/admin/controller/Account.php

@@ -0,0 +1,167 @@
+<?php
+
+namespace app\admin\controller;
+use think\facade\View;
+use think\facade\Lang;
+
+/**
+ * ============================================================================
+ * DSMall多用户商城
+ * ============================================================================
+ * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
+ * 网站地址: http://www.csdeshang.com
+ * ----------------------------------------------------------------------------
+ * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
+ * 不允许对程序代码以任何形式任何目的的再发布。
+ * ============================================================================
+ * 控制器
+ */
+class Account extends AdminControl {
+
+    public function initialize() {
+        parent::initialize();
+        Lang::load(base_path() . 'admin/lang/'.config('lang.default_lang').'/account.lang.php');
+        Lang::load(base_path() . 'admin/lang/'.config('lang.default_lang').'/config.lang.php');
+    }
+
+    /**
+     * 设置
+     */
+    public function setting() {
+        $config_model = model('config');
+        if (!request()->isPost()) {
+            $list_config = rkcache('config', true);
+            View::assign('list_config', $list_config);
+            /* 设置卖家当前栏目 */
+            $this->setAdminCurItem('setting');
+            return View::fetch();
+        } else {
+            $update_array=array();
+            $update_array['auto_register'] = input('post.auto_register');
+            $result = $config_model->editConfig($update_array);
+            if ($result) {
+                $this->log(lang('ds_edit').lang('ds_account'),1);
+                $this->success(lang('ds_common_save_succ'));
+            }else{
+                $this->log(lang('ds_edit').lang('ds_account'),0);
+            }
+        }
+    }
+    
+    /**
+     * QQ互联
+     */
+    function qq() {
+        $config_model = model('config');
+        if (!request()->isPost()) {
+            $list_config = rkcache('config', true);
+            View::assign('list_config', $list_config);
+
+            //输出子菜单
+            $this->setAdminCurItem('qq');
+            return View::fetch('qq');
+        } else {
+            $update_array = array();
+            $update_array['qq_isuse'] = input('post.qq_isuse');
+            $update_array['qq_appid'] = input('post.qq_appid');
+            $update_array['qq_appkey'] = input('post.qq_appkey');
+
+            $result = $config_model->editConfig($update_array);
+            if ($result === true) {
+                $this->log(lang('ds_edit').lang('qq_settings'), 1);
+                $this->success(lang('ds_common_save_succ'));
+            } else {
+                $this->log(lang('ds_edit').lang('qq_settings'), 0);
+                $this->error(lang('ds_common_save_fail'));
+            }
+        }
+    }
+
+    /**
+     * sina微博设置
+     */
+    public function sina() {
+        $config_model = model('config');
+        if (!request()->isPost()) {
+            $list_config = rkcache('config', true);
+            View::assign('list_config', $list_config);
+
+            //输出子菜单
+            $this->setAdminCurItem('sina');
+            return View::fetch('sina');
+        } else {
+            $update_array = array();
+            $update_array['sina_isuse'] = input('post.sina_isuse');
+            $update_array['sina_wb_akey'] = input('post.sina_wb_akey');
+            $update_array['sina_wb_skey'] = input('post.sina_wb_skey');
+
+            $result = $config_model->editConfig($update_array);
+            if ($result === true) {
+                $this->log(lang('ds_edit').lang('sina_settings'), 1);
+                $this->success(lang('ds_common_save_succ'));
+            } else {
+                $this->log(lang('ds_edit').lang('sina_settings'), 0);
+                $this->error(lang('ds_common_save_fail'));
+            }
+        }
+    }
+
+    /**
+     * 微信登录设置
+     */
+    public function wx() {
+        $config_model = model('config');
+        if (!request()->isPost()) {
+            $list_config = rkcache('config', true);
+            View::assign('list_config', $list_config);
+            //输出子菜单
+            $this->setAdminCurItem('wx');
+            return View::fetch('wx');
+        } else {
+            $update_array = array();
+            $update_array['weixin_isuse'] = input('post.weixin_isuse');
+            $update_array['weixin_appid'] = input('post.weixin_appid');
+            $update_array['weixin_secret'] = input('post.weixin_secret');
+            
+            $result = $config_model->editConfig($update_array);
+            if ($result) {
+                $this->log(lang('account_synchronous_login'));
+                $this->success(lang('ds_common_save_succ'));
+            } else {
+                $this->error(lang('ds_common_save_fail'));
+            }
+        }
+    }
+
+    /**
+     * 获取卖家栏目列表,针对控制器下的栏目
+     */
+    protected function getAdminItemList() {
+        $menu_array = array(
+            array(
+                'name' => 'setting',
+                'text' => lang('account_setting'),
+                'url' => (string)url('Account/setting')
+            ),
+            array(
+                'name' => 'qq',
+                'text' => lang('qq_interconnection'),
+                'url' => (string)url('Account/qq')
+            ),
+            array(
+                'name' => 'sina',
+                'text' => lang('sina_interconnection'),
+                'url' => (string)url('Account/sina')
+            ),
+            array(
+                'name' => 'wx',
+                'text' => lang('wx_login'),
+                'url' => (string)url('Account/wx')
+            ),
+        );
+        return $menu_array;
+    }
+
+}
+
+?>

+ 415 - 0
app/admin/controller/Activity.php

@@ -0,0 +1,415 @@
+<?php
+
+namespace app\admin\controller;
+use think\facade\View;
+use think\facade\Lang;
+
+/**
+ * ============================================================================
+ * DSMall多用户商城
+ * ============================================================================
+ * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
+ * 网站地址: http://www.csdeshang.com
+ * ----------------------------------------------------------------------------
+ * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
+ * 不允许对程序代码以任何形式任何目的的再发布。
+ * ============================================================================
+ * 控制器
+ */
+class Activity extends AdminControl {
+
+    public function initialize() {
+        parent::initialize();
+        Lang::load(base_path() . 'admin/lang/'.config('lang.default_lang').'/activity.lang.php');
+    }
+
+    /**
+     * 活动列表/删除活动
+     */
+    public function index() {
+        $activity_model = model('activity');
+        //条件
+        $condition = array();
+        $condition[] = array('activity_type','=','1'); //只显示商品活动
+        //状态
+        if ((input('param.searchstate'))) {
+            $state = intval(input('param.searchstate')) - 1;
+            $condition[] = array('activity_state','=',"$state");
+        }
+        //标题
+        if ((input('param.searchtitle'))) {
+            $condition[]=array('activity_title','like', "%" . input('param.searchtitle') . "%");
+        }
+        //有效期范围
+        if ((input('param.searchstartdate')) && (input('param.searchenddate'))) {
+            $startdate = strtotime(input('param.searchstartdate'));
+            $enddate = strtotime(input('param.searchenddate'));
+            if ($enddate > 0) {
+                $enddate += 86400;
+            }
+            $condition[]=array('activity_enddate','>=',$startdate);
+            $condition[]=array('activity_startdate','<=',$enddate);
+        }
+        //活动列表
+        $activity_list = $activity_model->getActivityList($condition, 10 , 'activity_sort asc');
+        //输出
+        View::assign('show_page', $activity_model->page_info->render());
+        View::assign('activity_list', $activity_list);
+        View::assign('filtered', $condition ? 1 : 0); //是否有查询条件
+        $this->setAdminCurItem('index');
+        return View::fetch();
+    }
+
+    /**
+     * 新建活动/保存新建活动
+     */
+    public function add() {
+        if (request()->isPost()) {
+            //提交表单
+            $data = [
+                'activity_title' => input('post.activity_title'),
+                'activity_startdate' => strtotime(input('post.activity_startdate')),
+                'activity_enddate' => strtotime(input('post.activity_enddate')),
+                'activity_type' => input('post.activity_type'),
+                'activity_banner' => $_FILES['activity_banner']['name'],
+                'activity_banner_mobile' => $_FILES['activity_banner_mobile']['name'],
+                'activity_sort' => intval(input('post.activity_sort'))
+            ];
+            $activity_validate = ds_validate('activity');
+            if (!$activity_validate->scene('add')->check($data)) {
+                $this->error($activity_validate->getError());
+            }
+
+            $file_name = '';
+            if (!empty($_FILES['activity_banner']['name'])) {
+                $res=ds_upload_pic(ATTACH_ACTIVITY,'activity_banner');
+                if($res['code']){
+                    $file_name=$res['data']['file_name'];
+                }else{
+                    $this->error($res['msg']);
+                }
+            }
+            //保存
+            $data['activity_banner'] = $file_name;
+            
+            $file_name_mobile = '';
+            if (!empty($_FILES['activity_banner_mobile']['name'])) {
+                $res=ds_upload_pic(ATTACH_ACTIVITY,'activity_banner_mobile');
+                if($res['code']){
+                    $file_name_mobile=$res['data']['file_name'];
+                }else{
+                    $this->error($res['msg']);
+                }
+            }
+            //保存
+            $data['activity_banner_mobile'] = $file_name_mobile;
+            $data['activity_desc'] = trim(input('post.activity_desc'));
+            $data['activity_state'] = intval(input('post.activity_state'));
+
+            $activity_model = model('activity');
+            $result = $activity_model->addActivity($data);
+            if ($result) {
+                $this->log(lang('ds_add') . lang('activity_index') . '[' . input('post.activity_title') . ']', null);
+                dsLayerOpenSuccess(lang('ds_common_op_succ'));
+            } else {
+                //添加失败则删除刚刚上传的图片,节省空间资源
+                @unlink($upload_file . DIRECTORY_SEPARATOR . $file_name);
+                @unlink($upload_file . DIRECTORY_SEPARATOR . $file_name_mobile);
+                $this->error(lang('ds_common_op_fail'));
+            }
+        } else {
+            $activity = array(
+                'activity_type' => '1',
+                'activity_startdate' => TIMESTAMP,
+                'activity_enddate' => TIMESTAMP,
+                'activity_banner' => '',
+                'activity_desc' => '',
+                'activity_state' => '1',
+            );
+            View::assign('activity', $activity);
+            return View::fetch('form');
+        }
+    }
+
+    /**
+     * 异步修改
+     */
+    public function ajax() {
+        if (in_array(input('param.branch'), array('activity_title', 'activity_sort'))) {
+            $activity_model = model('activity');
+            $update_array = array();
+            switch (input('param.branch')) {
+                /**
+                 * 活动主题
+                 */
+                case 'activity_title':
+                    if (trim(input('param.value')) == '')
+                        exit;
+                    break;
+                /**
+                 * 排序
+                 */
+                case 'activity_sort':
+                    if (preg_match('/^\d+$/', trim(input('param.value'))) <= 0 or intval(trim(input('param.value'))) < 0 or intval(trim(input('param.value'))) > 255)
+                        exit;
+                    break;
+                default:
+                    exit;
+            }
+            $update_array[input('param.column')] = trim(input('param.value'));
+            if ($activity_model->editActivity($update_array, intval(input('param.id'))))
+                echo 'true';
+        }elseif (in_array(input('param.branch'), array('activitydetail_sort'))) {
+            $activitydetail_model = model('activitydetail');
+            $update_array = array();
+            switch (input('param.branch')) {
+                /**
+                 * 排序
+                 */
+                case 'activitydetail_sort':
+                    if (preg_match('/^\d+$/', trim(input('param.value'))) <= 0 or intval(trim(input('param.value'))) < 0 or intval(trim(input('param.value'))) > 255)
+                        exit;
+                    break;
+                default:
+                    exit;
+            }
+            $update_array[input('param.column')] = trim(input('param.value'));
+            if ($activitydetail_model->editActivitydetail($update_array, array(array('activitydetail_id','=',intval(input('param.id'))))))
+                echo 'true';
+        }
+    }
+
+    /**
+     * 删除活动
+     */
+    public function del() {
+        $id = intval(input('param.activity_id'));
+        if ($id <= 0) {
+            ds_json_encode(10001, lang('param_error'));
+        }
+
+        $activity_model = model('activity');
+        $activitydetail_model = model('activitydetail');
+        //获取可以删除的数据
+        $activity_info = $activity_model->getOneActivityById($id);
+        if (empty($activity_info) || ($activity_info['activity_state'] && $activity_info['activity_enddate']>TIMESTAMP)) {//没有符合条件的活动信息直接返回成功信息
+            ds_json_encode(10001, lang('activity_index_help3'));
+        }
+        $id_arr = array($activity_info['activity_id']);
+        $condition = array();
+        $condition[] = array('activity_id','in',$id_arr);
+        //只有关闭或者过期的活动,能删除
+        if ($activitydetail_model->getActivitydetailList($condition)) {
+            if (!$activitydetail_model->delActivitydetail($condition)) {
+                ds_json_encode(10001, lang('activity_del_fail'));
+            }
+        }
+        try {
+            //删除数据先删除横幅图片,节省空间资源
+            foreach ($id_arr as $v) {
+                $this->delBanner(intval($v));
+            }
+        } catch (Exception $e) {
+            ds_json_encode(10001, $e->getMessage());
+        }
+        if ($activity_model->delActivity($condition)) {
+            $this->log(lang('ds_del') . lang('activity_index') . '[ID:' . $id . ']', null);
+            ds_json_encode(10000, lang('ds_common_del_succ'));
+        }
+        ds_json_encode(10001, lang('activity_del_fail'));
+    }
+
+    /**
+     * 编辑活动/保存编辑活动
+     */
+    public function edit() {
+        $activity_id = intval(input('param.activity_id'));
+        if ($activity_id<=0) {
+            $this->error(lang('miss_argument'));
+        }
+        $activity_model = model('activity');
+        $activity = $activity_model->getOneActivityById($activity_id);
+        if (!request()->isPost()) {
+            View::assign('activity', $activity);
+            return View::fetch('form');
+        } else {
+            //提交表单
+            $data = [
+                'activity_title' => input('post.activity_title'),
+                'activity_startdate' => strtotime(input('post.activity_startdate')),
+                'activity_enddate' => strtotime(input('post.activity_enddate')),
+                'activity_type' => input('post.activity_type'),
+                'activity_sort' => intval(input('post.activity_sort'))
+            ];
+            $activity_validate = ds_validate('activity');
+            if (!$activity_validate->scene('edit')->check($data)) {
+                $this->error($activity_validate->getError());
+            }
+            //构造更新内容
+            $file_name = '';
+            if ($_FILES['activity_banner']['name'] != '') {
+                $res=ds_upload_pic(ATTACH_ACTIVITY,'activity_banner');
+                if($res['code']){
+                    $file_name=$res['data']['file_name'];
+					$data['activity_banner'] = $file_name;
+                }else{
+                    $this->error($res['msg']);
+                }
+                
+            }
+            $file_name_mobile = '';
+            if ($_FILES['activity_banner_mobile']['name'] != '') {
+                $res=ds_upload_pic(ATTACH_ACTIVITY,'activity_banner_mobile');
+                if($res['code']){
+                    $file_name_mobile=$res['data']['file_name'];
+					$data['activity_banner_mobile'] = $file_name_mobile;
+                }else{
+                    $this->error($res['msg']);
+                }
+                
+            }
+            $data['activity_desc'] = trim(input('post.activity_desc'));
+            $data['activity_state'] = intval(input('post.activity_state'));
+            
+            $result = $activity_model->editActivity($data, $activity_id);
+            if ($result) {
+                //删除图片
+                @unlink($upload_file . DIRECTORY_SEPARATOR .$activity['activity_banner']);
+                @unlink($upload_file . DIRECTORY_SEPARATOR .$activity['activity_banner_mobile']);
+                $this->log(lang('ds_edit') . lang('activity_index') . '[ID:' . $activity_id . ']', null);
+                dsLayerOpenSuccess(lang('ds_common_save_succ'));
+            } else {
+                if ($_FILES['activity_banner']['name'] != '') {
+                    @unlink($upload_file . DIRECTORY_SEPARATOR .$file_name);
+                }
+                if ($_FILES['activity_banner_mobile']['name'] != '') {
+                    @unlink($upload_file . DIRECTORY_SEPARATOR .$file_name_mobile);
+                }
+                $this->error(lang('ds_common_save_fail'));
+            }
+        }
+    }
+
+    /**
+     * 活动细节列表
+     */
+    public function detail() {
+        $activity_id = intval(input('param.id'));
+        if ($activity_id <= 0) {
+            $this->error(lang('miss_argument'));
+        }
+        //条件
+        $condition_arr = array();
+        $condition_arr[] = array('activity_id','=',$activity_id);
+        //审核状态
+        if ((input('param.searchstate'))) {
+            $state = intval(input('param.searchstate')) - 1;
+            $condition_arr[] = array('activitydetail_state','=',"$state");
+        }
+        //店铺名称
+        if ((input('param.searchstore'))) {
+            $condition_arr[] = array('store_name','like', "%" . input('param.searchstore') . "%");
+        }
+        //商品名称
+        if ((input('param.searchgoods'))) {
+            $condition_arr[] = array('item_name','like', "%" . input('param.searchgoods') . "%");
+        }
+
+        $activitydetail_model = model('activitydetail');
+        $activitydetail_list = $activitydetail_model->getActivitydetailList($condition_arr, 10);
+        //输出到模板
+        View::assign('show_page', $activitydetail_model->page_info->render());
+        View::assign('activitydetail_list', $activitydetail_list);
+        $this->setAdminCurItem('detail');
+        return View::fetch();
+    }
+
+    /**
+     * 活动内容处理
+     */
+    public function deal() {
+        $activitydetail_id = input('param.activitydetail_id');
+        $activitydetail_id_array = ds_delete_param($activitydetail_id);
+        if ($activitydetail_id_array == FALSE) {
+            ds_json_encode('10001', lang('param_error'));
+        }
+        $condition = array();
+        $condition[] = array('activitydetail_id','in',$activitydetail_id_array);
+
+        //创建活动内容对象
+        $activitydetail_state = intval(input('param.state'));
+        $result = model('activitydetail')->editActivitydetail(array('activitydetail_state' => $activitydetail_state),$condition);
+        if ($result>=0) {
+            $this->log(lang('ds_edit') . lang('activity_index') . '[ID:' . $activitydetail_id . ']', null);
+            if (input('param.ajax')) {
+                ds_json_encode(10000,lang('ds_common_op_succ'));
+            }else{
+                $this->success(lang('ds_common_op_succ'));
+            }
+            
+        } else {
+            if (input('param.ajax')) {
+                ds_json_encode(10001,lang('ds_common_op_fail'));
+            }else{
+                $this->error(lang('ds_common_op_fail'));
+            }
+            
+        }
+    }
+    /**
+     * 删除活动内容
+     */
+    public function del_detail() {
+        $activitydetail_id = input('param.activitydetail_id');
+        $activitydetail_id_array = ds_delete_param($activitydetail_id);
+        if ($activitydetail_id_array == FALSE) {
+            ds_json_encode('10001', lang('param_error'));
+        }
+
+        $activitydetail_model = model('activitydetail');
+        //条件
+        $condition_arr = array();
+        $condition_arr[] =array('activitydetail_id','in',$activitydetail_id_array);
+        $condition_arr[] = array('activitydetail_state','in',array('0','2'));//未审核和已拒绝
+        if ($activitydetail_model->delActivitydetail($condition_arr)) {
+            $this->log(lang('ds_del') . lang('activity_index_content') . '[ID:' . implode(',', $activitydetail_id_array) . ']', null);
+            ds_json_encode(10000, lang('ds_common_del_succ'));
+        } else {
+            ds_json_encode(10001, lang('ds_common_del_fail'));
+        }
+    }
+
+    /**
+     * 根据活动编号删除横幅图片
+     *
+     * @param int $id
+     */
+    private function delBanner($id) {
+        $activity_model = model('activity');
+        $row = $activity_model->getOneActivityById($id);
+        //删除图片文件
+        @unlink(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_ACTIVITY . DIRECTORY_SEPARATOR . $row['activity_banner']);
+    }
+
+    /**
+     * 获取卖家栏目列表,针对控制器下的栏目
+     */
+    protected function getAdminItemList() {
+        $menu_array = array(
+            array(
+                'name' => 'index', 'text' => lang('ds_manage'), 'url' => (string)url('Activity/index')
+            ), array(
+                'name' => 'add',
+                'text' => lang('ds_new'),
+                'url' => "javascript:dsLayerOpen('".(string)url('Activity/add')."','".lang('ds_new')."')"
+            ),
+        );
+        if (request()->action() == 'detail') {
+            $menu_array[] = array(
+                'name' => 'detail', 'text' => lang('processing_application'), 'url' => 'javascript:void(0)'
+            );
+        }
+        return $menu_array;
+    }
+
+}

+ 321 - 0
app/admin/controller/Admin.php

@@ -0,0 +1,321 @@
+<?php
+
+namespace app\admin\controller;
+use think\facade\View;
+use think\facade\Lang;
+
+/**
+ * ============================================================================
+ * DSMall多用户商城
+ * ============================================================================
+ * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
+ * 网站地址: http://www.csdeshang.com
+ * ----------------------------------------------------------------------------
+ * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
+ * 不允许对程序代码以任何形式任何目的的再发布。
+ * ============================================================================
+ * 控制器
+ */
+class Admin extends AdminControl {
+
+    public function initialize() {
+        parent::initialize();
+        Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/admin.lang.php');
+    }
+
+    /**
+     * 管理员列表
+     */
+    public function admin() {
+        $admin_mod = model('admin');
+        $condition = array();
+        $admin_list = $admin_mod->getAdminList($condition, 10);
+        View::assign('admin_list', $admin_list);
+        View::assign('show_page', $admin_mod->page_info->render());
+        $this->setAdminCurItem('admin');
+        return View::fetch('admin');
+    }
+
+    /**
+     * 管理员删除
+     */
+    public function admin_del() {
+        $admin_id = intval(input('param.admin_id'));
+        if (!empty($admin_id)) {
+            if ($admin_id == 1) {
+                $this->error(lang('ds_common_save_fail'));
+            }
+            $admin_mod = model('admin');
+            $admin_mod->delAdmin(array(array('admin_id' ,'=', $admin_id)));
+            $this->log(lang('ds_del') . lang('limit_admin') . '[ID:' . $admin_id . ']', 1);
+            ds_json_encode(10000, lang('ds_common_del_succ'));
+        } else {
+            ds_json_encode(10001, lang('ds_common_del_fail'));
+        }
+    }
+
+    /**
+     * 管理员添加
+     */
+    public function admin_add() {
+        $admin_model = model('admin');
+        if (!request()->isPost()) {
+            //得到权限组
+            $gadmin = $admin_model->getGadminList('gname,gid');
+            View::assign('gadmin', $gadmin);
+            return View::fetch('admin_form');
+        } else {
+            $data['admin_name'] = input('post.admin_name');
+            $data['admin_gid'] = input('post.gid');
+            $data['admin_password'] = md5(input('post.admin_password'));
+            if(empty(input('post.admin_password'))){
+                $this->error(lang('admin_add_password_null'));
+            }
+            $admin_validate = ds_validate('admin');
+            if (!$admin_validate->scene('admin_add')->check($data)) {
+                $this->error($admin_validate->getError());
+            }
+            //判断是否重名
+            $admin_info=$admin_model->getOneAdmin(array(array('admin_name','=',$data['admin_name'])));
+            if($admin_info){
+                $this->error(lang('admin_add_admin_not_exists'));
+            }
+            $rs = $admin_model->addAdmin($data);
+            if ($rs) {
+                $this->log(lang('ds_add') . lang('limit_admin') . '[' . input('post.admin_name') . ']', 1);
+                dsLayerOpenSuccess(lang('ds_common_save_succ'));
+            } else {
+                $this->error(lang('ds_common_save_fail'));
+            }
+        }
+    }
+
+    /**
+     * ajax操作
+     */
+    public function ajax() {
+        $admin_model = model('admin');
+        switch (input('get.branch')) {
+            //管理人员名称验证
+            case 'check_admin_name':
+                $condition[]=array('admin_name','=',input('get.admin_name'));
+                $admin_info = $admin_model->infoAdmin($condition);
+                if (!empty($admin_info)) {
+                    exit('false');
+                } else {
+                    exit('true');
+                }
+                break;
+            //权限组名称验证
+            case 'check_gadmin_name':
+                $condition = array();
+                if (is_numeric(input('param.gid'))) {
+                    $condition[]=array('gid','<>', intval(input('param.gid')));
+                }
+                $condition[]=array('gname','=',input('get.gname'));
+                $info = $admin_model->getOneGadmin($condition);
+                if (!empty($info)) {
+                    exit('false');
+                } else {
+                    exit('true');
+                }
+                break;
+        }
+    }
+
+    /**
+     * 设置管理员权限
+     */
+    public function admin_edit() {
+        $admin_id = intval(input('param.admin_id'));
+        if (request()->isPost()) {
+            //没有更改密码
+            if (input('post.admin_password') != '') {
+                $data['admin_password'] = md5(input('post.admin_password'));
+            }
+            $data['admin_gid'] = intval(input('post.gid'));
+            //查询管理员信息
+            $admin_model = model('admin');
+            $result = $admin_model->editAdmin($data, $admin_id);
+            if ($result) {
+                $this->log(lang('ds_edit') . lang('limit_admin') . '[ID:' . $admin_id . ']', 1);
+                dsLayerOpenSuccess(lang('admin_edit_success'));
+            } else {
+                $this->error(lang('admin_edit_fail'));
+            }
+        } else {
+            //查询用户信息
+            $admin_model = model('admin');
+            $admin = $admin_model->getOneAdmin(array(array('admin_id' ,'=', $admin_id)));
+            if (!is_array($admin) || count($admin) <= 0) {
+                $this->error(lang('admin_edit_admin_error'), (string)url('admin/admin'));
+            }
+            View::assign('admin', $admin);
+
+            //得到权限组
+            $gadmin = $admin_model->getGadminList('gname,gid');
+            View::assign('gadmin', $gadmin);
+            return View::fetch('admin_form');
+        }
+    }
+
+    /**
+     * 取得所有权限项
+     *
+     * @return array
+     */
+    private function permission() {
+        $limit = $this->limitList();
+        if (is_array($limit)) {
+            foreach ($limit as $k => $v) {
+                if (is_array($v['child'])) {
+                    $tmp = array();
+                    foreach ($v['child'] as $key => $value) {
+                        $controller = (!empty($value['controller'])) ? $value['controller'] : $v['controller'];
+                        if (strpos($controller, '|') == false) {//controller参数不带|
+                            $limit[$k]['child'][$key]['action'] = rtrim($controller . '.' . str_replace('|', '|' . $controller . '.', $value['action']), '.');
+                        } else {//controller参数带|
+                            $tmp_str = '';
+                            if (empty($value['action'])) {
+                                $limit[$k]['child'][$key]['action'] = $controller;
+                            } elseif (strpos($value['action'], '|') == false) {//action参数不带|
+                                foreach (explode('|', $controller) as $v1) {
+                                    $tmp_str .= "$v1.{$value['action']}|";
+                                }
+                                $limit[$k]['child'][$key]['action'] = rtrim($tmp_str, '|');
+                            } elseif (strpos($value['action'], '|') != false && strpos($controller, '|') != false) {//action,controller都带|,交差权限
+                                foreach (explode('|', $controller) as $v1) {
+                                    foreach (explode('|', $value['action']) as $v2) {
+                                        $tmp_str .= "$v1.$v2|";
+                                    }
+                                }
+                                $limit[$k]['child'][$key]['action'] = rtrim($tmp_str, '|');
+                            }
+                        }
+                    }
+                }
+            }
+            return $limit;
+        } else {
+            return array();
+        }
+    }
+
+    /**
+     * 权限组
+     */
+    public function gadmin() {
+        $admin_model = model('admin');
+        $gadmin_list = $admin_model->getGadminList();
+        View::assign('gadmin_list', $gadmin_list);
+        $this->setAdminCurItem('gadmin');
+        return View::fetch('gadmin');
+    }
+
+    /**
+     * 添加权限组
+     */
+    public function gadmin_add() {
+        if (!request()->isPost()) {
+            View::assign('limit', $this->permission());
+            return View::fetch('gadmin_add');
+        } else {
+            $limit_str = '';
+            $permission_array = input('post.permission/a');
+            if (is_array($permission_array)) {
+                $limit_str = implode('|', $permission_array);
+            }
+            $data['glimits'] = ds_encrypt($limit_str, MD5_KEY . md5(input('post.gname')));
+            $data['gname'] = input('post.gname');
+            $admin_model = model('admin');
+            if ($admin_model->addGadmin($data)) {
+                $this->log(lang('ds_add') . lang('limit_gadmin') . '[' . input('post.gname') . ']', 1);
+                dsLayerOpenSuccess(lang('ds_common_save_succ'));
+            } else {
+                $this->error(lang('ds_common_save_fail'));
+            }
+        }
+    }
+
+    /**
+     * 设置权限组权限
+     */
+    public function gadmin_set() {
+        $gid = intval(input('param.gid'));
+        $admin_model = model('admin');
+        $ginfo = $admin_model->getOneGadmin(array(array('gid' ,'=', $gid)));
+        if (empty($ginfo)) {
+            $this->error(lang('admin_set_admin_not_exists'));
+        }
+        if (!request()->isPost()) {
+            //解析已有权限
+            $hlimit = ds_decrypt($ginfo['glimits'], MD5_KEY . md5($ginfo['gname']));
+            $ginfo['glimits'] = explode('|', $hlimit);
+            View::assign('ginfo', $ginfo);
+            View::assign('limit', $this->permission());
+            return View::fetch('gadmin_set');
+        } else {
+            $limit_str = '';
+            $permission_array = input('post.permission/a');
+            if (is_array($permission_array)) {
+                $limit_str = implode('|', $permission_array);
+            }
+            $limit_str = ds_encrypt($limit_str, MD5_KEY . md5(input('post.gname')));
+            $data['glimits'] = $limit_str;
+            $data['gname'] = input('post.gname');
+            $update = $admin_model->editGadmin(array(array('gid' ,'=', $gid)), $data);
+            if ($update) {
+                $this->log(lang('ds_edit') . lang('limit_gadmin') . '[' . input('post.gname') . ']', 1);
+                dsLayerOpenSuccess(lang('ds_common_save_succ'));
+            } else {
+                $this->error(lang('ds_common_save_succ'));
+            }
+        }
+    }
+
+    /**
+     * 组删除
+     */
+    public function gadmin_del() {
+        if (is_numeric(input('param.gid'))) {
+            $admin_model = model('admin');
+            $admin_model->delGadmin(array(array('gid' ,'=', intval(input('param.gid')))));
+            $this->log(lang('ds_del') . lang('limit_gadmin') . '[ID' . intval(input('param.gid')) . ']', 1);
+            ds_json_encode(10000, lang('ds_common_op_succ'));
+        } else {
+            ds_json_encode(10000, lang('ds_common_op_fail'));
+        }
+    }
+
+    /**
+     * 获取卖家栏目列表,针对控制器下的栏目
+     */
+    protected function getAdminItemList() {
+        $menu_array = array(
+            array(
+                'name' => 'admin',
+                'text' => lang('limit_admin'),
+                'url' => (string)url('admin/admin')
+            ),
+            array(
+                'name' => 'admin_add',
+                'text' => lang('admin_add_limit_admin'),
+                'url' => "javascript:dsLayerOpen('" . (string)url('admin/admin_add') . "','".lang('admin_add_limit_admin')."')"
+            ),
+            array(
+                'name' => 'gadmin',
+                'text' => lang('limit_gadmin'),
+                'url' => (string)url('admin/gadmin')
+            ),
+            array(
+                'name' => 'gadmin_add',
+                'text' => lang('admin_add_limit_gadmin'),
+                'url' => "javascript:dsLayerOpen('" . (string)url('admin/gadmin_add') . "','".lang('admin_add_limit_gadmin')."')"
+            ),
+        );
+        return $menu_array;
+    }
+
+}
+
+?>

+ 895 - 0
app/admin/controller/AdminControl.php

@@ -0,0 +1,895 @@
+<?php
+
+namespace app\admin\controller;
+use think\facade\View;
+use app\BaseController;
+
+/**
+ * ============================================================================
+ * DSMall多用户商城
+ * ============================================================================
+ * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
+ * 网站地址: http://www.csdeshang.com
+ * ----------------------------------------------------------------------------
+ * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
+ * 不允许对程序代码以任何形式任何目的的再发布。
+ * ============================================================================
+ * 控制器
+ */
+class AdminControl extends BaseController {
+
+    /**
+     * 管理员资料 name id group
+     */
+    protected $admin_info;
+
+    protected $permission;
+    public function initialize() {
+        $config_list = rkcache('config', true);
+        config($config_list,'ds_config');
+        
+        if(request()->controller()!='Login'){
+            $this->admin_info = $this->systemLogin();
+
+            if ($this->admin_info['admin_id'] != 1) {
+                // 验证权限
+                $this->checkPermission();
+            }
+            $this->setMenuList();
+        }
+    }
+
+    /**
+     * 取得当前管理员信息
+     *
+     * @param
+     * @return 数组类型的返回结果
+     */
+    protected final function getAdminInfo() {
+        return $this->admin_info;
+    }
+
+    /**
+     * 系统后台登录验证
+     *
+     * @param
+     * @return array 数组类型的返回结果
+     */
+    protected final function systemLogin() {
+        $admin_info = array(
+            'admin_id' => session('admin_id'),
+            'admin_name' => session('admin_name'),
+            'admin_gid' => session('admin_gid'),
+            'admin_is_super' => session('admin_is_super'),
+        );
+        if (empty($admin_info['admin_id']) || empty($admin_info['admin_name']) || !isset($admin_info['admin_gid']) || !isset($admin_info['admin_is_super'])) {
+            session(null);
+            $this->redirect('admin/Login/index');
+        }
+
+        return $admin_info;
+    }
+
+    public function setMenuList() {
+        $menu_list = $this->menuList();
+
+        $menu_list=$this->parseMenu($menu_list);
+        View::assign('menu_list', $menu_list);
+    }
+
+    /**
+     * 验证当前管理员权限是否可以进行操作
+     *
+     * @param string $link_nav
+     * @return
+     */
+    protected final function checkPermission($link_nav = null){
+        if ($this->admin_info['admin_is_super'] == 1) return true;
+
+        $controller = request()->controller();
+        $action = request()->action();
+        if (empty($this->permission)){
+            
+            $admin_model=model('admin');
+            $gadmin = $admin_model->getOneGadmin(array('gid'=>$this->admin_info['admin_gid']));
+            
+            $permission = ds_decrypt($gadmin['glimits'],MD5_KEY.md5($gadmin['gname']));
+            $this->permission = $permission = explode('|',$permission);
+        }else{
+            $permission = $this->permission;
+        }
+        //显示隐藏小导航,成功与否都直接返回
+        if (is_array($link_nav)){
+            if (!in_array("{$link_nav['controller']}.{$link_nav['action']}",$permission) && !in_array($link_nav['controller'],$permission)){
+                return false;
+            }else{
+                return true;
+            }
+        }
+        //以下几项不需要验证
+        $tmp = array('Index','Dashboard','Login');
+        if (in_array($controller,$tmp)){
+            return true;
+        }
+        if (in_array($controller,$permission) || in_array("$controller.$action",$permission)){
+            return true;
+        }else{
+            $extlimit = array('ajax','export_step1');
+            if (in_array($action,$extlimit) && (in_array($controller,$permission) || strpos(serialize($permission),'"'.$controller.'.'))){
+                return true;
+            }
+            //带前缀的都通过
+            foreach ($permission as $v) {
+                if (!empty($v) && strpos("$controller.$action",$v.'_') !== false) {
+                    return true;break;
+                }
+            }
+        }
+        if($this->admin_info['admin_name']!='dsmall'){
+            $this->error(lang('ds_assign_right'),'Dashboard/welcome');
+        }else if(request()->isPost() || preg_match('/del/',request()->action())){
+            $this->error(lang('ds_assign_right'));
+        }
+    }
+
+    /**
+     * 过滤掉无权查看的菜单
+     *
+     * @param array $menu
+     * @return array
+     */
+    private final function parseMenu($menu = array()) {
+        if ($this->admin_info['admin_is_super'] == 1) {
+            return $menu;
+        }
+        foreach ($menu as $k => $v) {
+            foreach ($v['children'] as $ck => $cv) {
+                $tmp = explode(',', $cv['args']);
+                //以下几项不需要验证
+                $except = array('Index', 'Dashboard', 'Login');
+                if (in_array($tmp[1], $except))
+                    continue;
+                if (!in_array($tmp[1], array_values($this->permission)) && !in_array($tmp[1].'.'.$tmp[0], array_values($this->permission))) {
+                    if($this->admin_info['admin_name']!='dsmall'){
+                        unset($menu[$k]['children'][$ck]);
+                    }
+                }
+            }
+            if (empty($menu[$k]['children'])) {
+                unset($menu[$k]);
+                unset($menu[$k]['children']);
+            }
+        }
+        return $menu;
+    }
+
+    /**
+     * 记录系统日志
+     *
+     * @param $lang 日志语言包
+     * @param $state 1成功0失败null不出现成功失败提示
+     * @param $admin_name
+     * @param $admin_id
+     */
+    protected final function log($lang = '', $state = 1, $admin_name = '', $admin_id = 0) {
+        if ($admin_name == '') {
+            $admin_name = session('admin_name');
+            $admin_id = session('admin_id');
+        }
+        $data = array();
+        if (is_null($state)) {
+            $state = null;
+        } else {
+            $state = $state ? '' : lang('ds_fail');
+        }
+        $data['adminlog_content'] = $lang . $state;
+        $data['adminlog_time'] = TIMESTAMP;
+        $data['admin_name'] = $admin_name;
+        $data['admin_id'] = $admin_id;
+        $data['adminlog_ip'] = request()->ip();
+        $data['adminlog_url'] = request()->controller() . '&' . request()->action();
+        
+        $adminlog_model=model('adminlog');
+        return $adminlog_model->addAdminlog($data);
+    }
+
+    /**
+     * 添加到任务队列
+     *
+     * @param array $goods_array
+     * @param boolean $ifdel 是否删除以原记录
+     */
+    protected function addcron($data = array(), $ifdel = false) {
+        $cron_model = model('cron');
+        if (isset($data[0])) { // 批量插入
+            $where = array();
+            foreach ($data as $k => $v) {
+                // 删除原纪录条件
+                if ($ifdel) {
+                    $where[] = '(cron_type = "' . $data['cron_type'] . '" and cron_value = "' . $data['cron_value'] . '")';
+                }
+            }
+            // 删除原纪录
+            if ($ifdel) {
+                $cron_model->delCron(implode(',', $where));
+            }
+            $cron_model->addCronAll($data);
+        } else { // 单条插入
+            // 删除原纪录
+            if ($ifdel) {
+                $cron_model->delCron(array('cron_type' => $data['cron_type'], 'cron_value' => $data['cron_value']));
+            }
+            $cron_model->addCron($data);
+        }
+    }
+
+    /**
+     * 当前选中的栏目
+     */
+    protected function setAdminCurItem($curitem = '') {
+        View::assign('admin_item', $this->getAdminItemList());
+        View::assign('curitem', $curitem);
+    }
+
+    /**
+     * 获取卖家栏目列表,针对控制器下的栏目
+     */
+    protected function getAdminItemList() {
+        return array();
+    }
+
+    /*
+     * 侧边栏列表
+     */
+
+    function menuList() {
+        return array(
+            'dashboard' => array(
+                'name' => 'dashboard',
+                'text' => lang('ds_dashboard'),
+                'show' => TRUE,
+                'children' => array(
+                    'welcome' => array(
+                        'ico'=>"&#xe70b;",
+                        'text' => lang('ds_welcome'),
+                        'args' => 'welcome,Dashboard,dashboard',
+                    ),
+                    /*
+                    'aboutus' => array(
+                        'text' => lang('ds_aboutus'),
+                        'args' => 'aboutus,dashboard,dashboard',
+                    ),
+                     */
+                    'config' => array(
+                        'ico'=>'&#xe6e0;',
+                        'text' => lang('ds_base'),
+                        'args' => 'base,Config,dashboard',
+                    ),
+                    'member' => array(
+                        'ico'=>'&#xe667;',
+                        'text' => lang('ds_member_manage'),
+                        'args' => 'member,Member,dashboard',
+                    ),
+                ),
+            ),
+            'setting' => array(
+                'name' => 'setting',
+                'text' => lang('ds_setting'),
+                'show' => TRUE,
+                'children' => array(
+                    'config' => array(
+                        'ico'=>'&#xe6e0;',
+                        'text' => lang('ds_base'),
+                        'args' => 'base,Config,setting',
+                    ),
+                    'account' => array(
+                        'ico'=>'&#xe678;',
+                        'text' => lang('ds_account'),
+                        'args' => 'qq,Account,setting',
+                    ),
+                    'upload_set' => array(
+                        'ico'=>'&#xe72a;',
+                        'text' => lang('ds_upload_set'),
+                        'args' => 'default_thumb,Upload,setting',
+                    ),
+                    'seo' => array(
+                        'ico'=>'&#xe6e0;',
+                        'text' => lang('ds_seo_set'),
+                        'args' => 'index,Seo,setting',
+                    ),
+                    'message' => array(
+                        'ico'=>'&#xe71b;',
+                        'text' => lang('ds_message'),
+                        'args' => 'email,Message,setting',
+                    ),
+                    'payment' => array(
+                        'ico'=>'&#xe74d;',
+                        'text' => lang('ds_payment'),
+                        'args' => 'index,Payment,setting',
+                    ),
+                    'admin' => array(
+                        'ico'=>'&#xe67b;',
+                        'text' => lang('ds_admin'),
+                        'args' => 'admin,Admin,setting',
+                    ),
+                    'express' => array(
+                        'ico'=>'&#xe69e;',
+                        'text' => lang('ds_express'),
+                        'args' => 'index,Express,setting',
+                    ),
+                    'Region' => array(
+                        'ico'=>'&#xe720;',
+                        'text' => lang('ds_region'),
+                        'args' => 'index,Region,setting',
+                    ),
+                    'db' => array(
+                        'ico'=>'&#xe6f5;',
+                        'text' => lang('ds_db'),
+                        'args' => 'db,Database,setting',
+                    ),
+                    'admin_log' => array(
+                        'ico'=>'&#xe71f;',
+                        'text' => lang('ds_adminlog'),
+                        'args' => 'loglist,Adminlog,setting',
+                    ),
+                ),
+            ),
+            'member' => array(
+                'name' => 'member',
+                'text' => lang('ds_member'),
+                'show' => TRUE,
+                'children' => array(
+                    'member' => array(
+                        'ico'=>'&#xe667;',
+                        'text' => lang('ds_member_manage'),
+                        'args' => 'member,Member,member',
+                    ),
+                    'member_auth' => array(
+                        'ico'=>'&#xe6ea;',
+                        'text' => lang('member_auth'),
+                        'args' => 'index,MemberAuth,member',
+                    ),
+                    'membergrade' => array(
+                        'ico'=>'&#xe6a3;',
+                        'text' => lang('ds_membergrade'),
+                        'args' => 'index,Membergrade,member',
+                    ),
+                    'exppoints' => array(
+                        'ico'=>'&#xe727;',
+                        'text' => lang('ds_exppoints'),
+                        'args' => 'index,Exppoints,member',
+                    ),
+                    'notice' => array(
+                        'ico'=>'&#xe71b;',
+                        'text' => lang('ds_notice'),
+                        'args' => 'index,Notice,member',
+                    ),
+                    'points' => array(
+                        'ico'=>'&#xe6f5;',
+                        'text' => lang('ds_points'),
+                        'args' => 'index,Points,member',
+                    ),
+                    'predeposit' => array(
+                        'ico'=>'&#xe6e2;',
+                        'text' => lang('ds_predeposit'),
+                        'args' => 'pdrecharge_list,Predeposit,member',
+                    ),
+                    'snsmalbum' => array(
+                        'ico'=>'&#xe72a;',
+                        'text' => lang('ds_snsmalbum'),
+                        'args' => 'index,Snsmalbum,member',
+                    ),
+                    'snsmember' => array(
+                        'ico'=>'&#xe73e;',
+                        'text' => lang('ds_snsmember'),
+                        'args' => 'index,Snsmember,member',
+                    ),
+                    'instant_message' => array(
+                        'ico' => '&#xe71f;',
+                        'text' => lang('instant_message'),
+                        'args' => 'index,InstantMessage,member',
+                    ),
+                ),
+            ),
+            'goods' => array(
+                'name' => 'goods',
+                'text' => lang('ds_goods'),
+                'show' => TRUE,
+                'children' => array(
+                    'goodsclass' => array(
+                        'ico'=>'&#xe652;',
+                        'text' => lang('ds_goodsclass'),
+                        'args' => 'goods_class,Goodsclass,goods',
+                    ),
+                    'Brand' => array(
+                        'ico'=>'&#xe6b0;',
+                        'text' => lang('ds_brand_manage'),
+                        'args' => 'index,Brand,goods',
+                    ),
+                    'Goods' => array(
+                        'ico'=>'&#xe732;',
+                        'text' => lang('ds_goods_manage'),
+                        'args' => 'index,Goods,goods',
+                    ),
+                    'Type' => array(
+                        'ico'=>'&#xe728;',
+                        'text' => lang('ds_type'),
+                        'args' => 'index,Type,goods',
+                    ),
+                    'Spec' => array(
+                        'ico'=>'&#xe71d;',
+                        'text' => lang('ds_spec'),
+                        'args' => 'index,Spec,goods',
+                    ),
+                    'album' => array(
+                        'ico'=>'&#xe72a;',
+                        'text' => lang('ds_album'),
+                        'args' => 'index,Goodsalbum,goods',
+                    ),
+                    'video' => array(
+                        'ico'=>'&#xe6fa;',
+                        'text' => lang('ds_video'),
+                        'args' => 'index,Goodsvideo,goods',
+                    ),
+                    'Arrivalnotice' => array(
+                        'ico'=>'&#xe71b;',
+                        'text' => lang('ds_arrivalnotice'),
+                        'args' => 'index,Arrivalnotice,goods',
+                    ),
+                ),
+            ),
+            'store' => array(
+                'name' => 'store',
+                'text' => lang('ds_store'),
+                'show' => TRUE,
+                'children' => array(
+                    'Store' => array(
+                        'ico'=>'&#xe6ec;',
+                        'text' => lang('ds_store_manage'),
+                        'args' => 'store,Store,store',
+                    ),
+                    'Storemoney' => array(
+                        'ico'=>'&#xe6e2;',
+                        'text' => lang('ds_store_money'),
+                        'args' => 'index,Storemoney,store',
+                    ),
+                    'Storedeposit' => array(
+                        'ico'=>'&#xe72b;',
+                        'text' => lang('ds_store_deposit'),
+                        'args' => 'index,Storedeposit,store',
+                    ),
+                    'Storegrade' => array(
+                        'ico'=>'&#xe6a3;',
+                        'text' => lang('ds_storegrade'),
+                        'args' => 'index,Storegrade,store',
+                    ),
+                    'Storeclass' => array(
+                        'ico'=>'&#xe652;',
+                        'text' => lang('ds_storeclass'),
+                        'args' => 'store_class,Storeclass,store',
+                    ),
+//                    'Chain' => array(
+//                        'ico'=>'&#xe69e;',
+//                        'text' => lang('ds_chain'),
+//                        'args' => 'index,Chain,store',
+//                    ),
+                    'Storesnstrace' => array(
+                        'ico'=>'&#xe6ec;',
+                        'text' => lang('ds_storesnstrace'),
+                        'args' => 'index,Storesnstrace,store',
+                    ),
+                    'Storehelp' => array(
+                        'ico'=>'&#xe6b4;',
+                        'text' => lang('ds_Storehelp'),
+                        'args' => 'index,Storehelp,store',
+                    ),
+                    'Storejoin' => array(
+                        'ico'=>'&#xe6ff;',
+                        'text' => lang('ds_storejoin'),
+                        'args' => 'index,Storejoin,store',
+                    ),
+                    'Ownshop' => array(
+                        'ico'=>'&#xe6ec;',
+                        'text' => lang('ds_ownshop'),
+                        'args' => 'index,Ownshop,store',
+                    ),
+                ),
+            ),
+            'trade' => array(
+                'name' => 'trade',
+                'text' => lang('ds_trade'),
+                'show' => TRUE,
+                'children' => array(
+                    'order' => array(
+                        'ico'=>'&#xe69c;',
+                        'text' => lang('ds_order'),
+                        'args' => 'index,Order,trade',
+                    ),
+                    'vrorder' => array(
+                        'ico'=>'&#xe71f;',
+                        'text' => lang('ds_vrorder'),
+                        'args' => 'index,Vrorder,trade',
+                    ),
+                    'refund' => array(
+                        'ico'=>'&#xe6f3;',
+                        'text' => lang('ds_refund'),
+                        'args' => 'refund_manage,Refund,trade',
+                    ),
+                    'return' => array(
+                        'ico'=>'&#xe6f3;',
+                        'text' => lang('ds_return'),
+                        'args' => 'return_manage,Returnmanage,trade',
+                    ),
+                    'vrrefund' => array(
+                        'ico'=>'&#xe6f3;',
+                        'text' => lang('ds_vrrefund'),
+                        'args' => 'refund_manage,Vrrefund,trade',
+                    ),
+                    'Bill' => array(
+                        'ico'=>'&#xe69c;',
+                        'text' => lang('ds_bill_manage'),
+                        'args' => 'show_statis,Bill,trade',
+                    ),
+                    'consulting' => array(
+                        'ico'=>'&#xe71c;',
+                        'text' => lang('ds_consulting'),
+                        'args' => 'Consulting,Consulting,trade',
+                    ),
+                    'inform' => array(
+                        'ico'=>'&#xe70c;',
+                        'text' => lang('ds_inform'),
+                        'args' => 'inform_list,Inform,trade',
+                    ),
+                    'evaluate' => array(
+                        'ico'=>'&#xe6f2;',
+                        'text' => lang('ds_evaluate'),
+                        'args' => 'evalgoods_list,Evaluate,trade',
+                    ),
+                    'complain' => array(
+                        'ico'=>'&#xe676;',
+                        'text' => lang('ds_complain'),
+                        'args' => 'complain_new_list,Complain,trade',
+                    ),
+                ),
+            ),
+            'website' => array(
+                'name' => 'website',
+                'text' => lang('ds_website'),
+                'show' => TRUE,
+                'children' => array(
+                    'Articleclass' => array(
+                        'ico'=>'&#xe652;',
+                        'text' => lang('ds_articleclass'),
+                        'args' => 'index,Articleclass,website',
+                    ),
+                    'Article' => array(
+                        'ico'=>'&#xe71d;',
+                        'text' => lang('ds_article'),
+                        'args' => 'index,Article,website',
+                    ),
+                    'Document' => array(
+                        'ico'=>'&#xe74f;',
+                        'text' => lang('ds_document'),
+                        'args' => 'index,Document,website',
+                    ),
+                    'Navigation' => array(
+                        'ico'=>'&#xe67d;',
+                        'text' => lang('ds_navigation'),
+                        'args' => 'index,Navigation,website',
+                    ),
+                    'Adv' => array(
+                        'ico'=>'&#xe707;',
+                        'text' => lang('ds_adv'),
+                        'args' => 'ap_manage,Adv,website',
+                    ),
+                    'EditablePagePc' => array(
+                        'ico'=>'&#xe60c;',
+                        'text' => lang('editable_page_pc'),
+                        'args' => 'page_list,EditablePage,website',
+                    ),
+                    'EditablePageH5' => array(
+                        'ico'=>'&#xe601;',
+                        'text' => lang('editable_page_h5'),
+                        'args' => 'h5_page_list,EditablePage,website',
+                    ),
+                    'Link' => array(
+                        'ico'=>'&#xe67d;',
+                        'text' => lang('ds_friendlink'),
+                        'args' => 'index,Link,website',
+                    ),
+                    'Mallconsult' => array(
+                        'ico'=>'&#xe750;',
+                        'text' => lang('ds_mall_consult'),
+                        'args' => 'index,Mallconsult,website',
+                    ),
+                    'Feedback' => array(
+                        'ico'=>'&#xe672;',
+                        'text' => lang('ds_feedback'),
+                        'args' => 'flist,Feedback,website',
+                    ),
+                ),
+            ),
+            'operation' => array(
+                'name' => 'operation',
+                'text' => lang('ds_operation'),
+                'show' => TRUE,
+                'children' => array(
+                    'Operation' => array(
+                        'ico'=>'&#xe734;',
+                        'text' => lang('ds_operation_set'),
+                        'args' => 'index,Operation,operation',
+                    ),
+                    
+                ),
+            ),
+            'stat' => array(
+                'name' => 'stat',
+                'text' => lang('ds_stat'),
+                'show' => TRUE,
+                'children' => array(
+                    'stat_general' => array(
+                        'ico'=>'&#xe734;',
+                        'text' => lang('ds_statgeneral'),
+                        'args' => 'general,Statgeneral,stat',
+                    ),
+                    'stat_industry' => array(
+                         'ico'=>'&#xe745;',
+                        'text' => lang('ds_statindustry'),
+                        'args' => 'scale,Statindustry,stat',
+                    ),
+                    'stat_member' => array(
+                        'ico'=>'&#xe73f;',
+                        'text' => lang('ds_statmember'),
+                        'args' => 'newmember,Statmember,stat',
+                    ),
+                    'stat_store' => array(
+                        'ico'=>'&#xe6ec;',
+                        'text' => lang('ds_statstore'),
+                        'args' => 'newstore,Statstore,stat',
+                    ),
+                    'stat_trade' => array(
+                         'ico'=>'&#xe745;',
+                        'text' => lang('ds_stattrade'),
+                        'args' => 'income,Stattrade,stat',
+                    ),
+                    'stat_goods' => array(
+                        'ico'=>'&#xe732;',
+                        'text' => lang('ds_statgoods'),
+                        'args' => 'pricerange,Statgoods,stat',
+                    ),
+                    'stat_marketing' => array(
+                         'ico'=>'&#xe745;',
+                        'text' => lang('ds_statmarketing'),
+                        'args' => 'promotion,Statmarketing,stat',
+                    ),
+                    'stat_stataftersale' => array(
+                         'ico'=>'&#xe745;',
+                        'text' => lang('ds_stataftersale'),
+                        'args' => 'refund,Stataftersale,stat',
+                    ),
+                ),
+            ),
+            'mobile' => array(
+                'name' => 'mobile',
+                'text' => lang('mobile'),
+                'show' => TRUE,
+                'children' => array(
+                    'app_appadv' => array(
+                        'text' => lang('appadv'),
+                        'args' => 'index,Appadv,mobile',
+                    ),
+                ),
+            ),
+            'wechat' => array(
+                'name' => 'wechat',
+                'text' => lang('wechat'),
+                'show' => TRUE,
+                'children' => array(
+                    'wechat_setting' => array(
+                        'ico'=>'&#xe6e0;',
+                        'text' => lang('wechat_setting'),
+                        'args' => 'setting,Wechat,wechat',
+                    ),
+                    'wechat_material' => array(
+                        'ico'=>'&#xe679;',
+                        'text' => lang('wechat_material'),
+                        'args' => 'material,Wechat,wechat',
+                    ),
+                    'wechat_menu' => array(
+                        'ico'=>'&#xe679;',
+                        'text' => lang('wechat_menu'),
+                        'args' => 'menu,Wechat,wechat',
+                    ),
+                    'wechat_keywords' => array(
+                        'ico'=>'&#xe672;',
+                        'text' => lang('wechat_keywords'),
+                        'args' => 'k_text,Wechat,wechat',
+                    ),
+                    'wechat_member' => array(
+                        'ico'=>'&#xe729;',
+                        'text' => lang('wechat_member'),
+                        'args' => 'member,Wechat,wechat',
+                    ),
+                    'wechat_push' => array(
+                        'ico'=>'&#xe71b;',
+                        'text' => lang('wechat_push'),
+                        'args' => 'SendList,Wechat,wechat',
+                    ),
+                ),
+            ),
+            'flea' => array(
+                'name' => 'flea',
+                'text' => lang('flea'),
+                'show' => FALSE,
+                'children' => array(
+                    'flea_mes' => array(
+                        'text' => lang('flea_mes'),
+                        'args' => 'flea,Flea,flea',
+                    ),
+                    'flea_index' => array(
+                        'ico'=>'&#xe6e0;',
+                        'text' => lang('flea_seo'),
+                        'args' => 'index,Fleaseo,flea',
+                    ),
+                     'flea_class' => array(
+                         'ico'=>'&#xe652;',
+                         'text' => lang('flea_class'),
+                         'args' => 'flea_class,Fleaclass,flea',
+                     ),
+                    'flea_class_index' => array(
+                        'ico'=>'&#xe652;',
+                        'text' => lang('flea_class_index'),
+                        'args' => 'flea_class_index,Fleaclassindex,flea',
+                    ),
+                    'flea_region' => array(
+                        'ico'=>'&#xe720;',
+                        'text' => lang('flea_region'),
+                        'args' => 'flea_region,Flearegion,flea',
+                    ),
+                    'flea_adv_manage' => array(
+                        'ico'=>'&#xe72a;',
+                        'text' => lang('flea_adv_manage'),
+                        'args' => 'adv_manage,Fleaseo,flea',
+                    ),
+                ),
+            ),
+            'live' => array(
+                'name' => 'live',
+                'text' => lang('ds_live'),
+                'show' => TRUE,
+                'children' => array(
+                    'live_setting' => array(
+                        'ico' => '&#xe71f;',
+                        'text' => lang('live_setting'),
+                        'args' => 'index,LiveSetting,live',
+                    ),
+                    'live_apply' => array(
+                        'ico' => '&#xe71f;',
+                        'text' => lang('live_apply'),
+                        'args' => 'index,LiveApply,live',
+                    ),
+                    
+                    'live_goods' => array(
+                        'ico' => '&#xe71f;',
+                        'text' => lang('live_goods'),
+                        'args' => 'index,LiveGoods,live',
+                    ),
+                ),
+            ),
+        );
+    }
+
+    /*
+     * 权限选择列表
+     */
+
+    function limitList() {
+        $_limit = array(
+            array('name' => lang('ds_setting'), 'child' => array(
+                    array('name' => lang('ds_base'), 'action' => null, 'controller' => 'Config'),
+                    array('name' => lang('ds_account'), 'action' => null, 'controller' => 'Account'),
+                    array('name' => lang('ds_upload_set'), 'action' => null, 'controller' => 'Upload'),
+                    array('name' => lang('ds_seo_set'), 'action' => null, 'controller' => 'Seo'),
+                    array('name' => lang('ds_payment'), 'action' => null, 'controller' => 'Payment'),
+                    array('name' => lang('ds_message'), 'action' => null, 'controller' => 'Message'),
+                    array('name' => lang('ds_admin'), 'action' => null, 'controller' => 'Admin'),
+                    array('name' => lang('ds_express'), 'action' => null, 'controller' => 'Express'),
+                    array('name' => lang('ds_region'), 'action' => null, 'controller' => 'Region'),
+                    array('name' => lang('ds_db'), 'action' => null, 'controller' => 'Database'),
+                    array('name' => lang('ds_adminlog'), 'action' => null, 'controller' => 'Adminlog'),
+                )),
+            array('name' => lang('ds_goods'), 'child' => array(
+                    array('name' => lang('ds_goods_manage'), 'action' => null, 'controller' => 'Goods'),
+                    array('name' => lang('ds_goodsclass'), 'action' => null, 'controller' => 'Goodsclass'),
+                    array('name' => lang('ds_brand_manage'), 'action' => null, 'controller' => 'Brand'),
+                    array('name' => lang('ds_type'), 'action' => null, 'controller' => 'Type'),
+                    array('name' => lang('ds_spec'), 'action' => null, 'controller' => 'Spec'),
+                    array('name' => lang('ds_album'), 'action' => null, 'controller' => 'Goodsalbum'),
+                    array('name' => lang('ds_video'), 'action' => null, 'controller' => 'Goodsvideo'),
+                    array('name' => lang('ds_arrivalnotice'), 'action' => null, 'controller' => 'Arrivalnotice'),
+                )),
+            array('name' => lang('ds_store'), 'child' => array(
+                    array('name' => lang('ds_store_manage'), 'action' => null, 'controller' => 'Store'),
+                    array('name' => lang('ds_store_money'), 'action' => null, 'controller' => 'Storemoney'),
+                    array('name' => lang('ds_store_deposit'), 'action' => null, 'controller' => 'Storedeposit'),
+                    array('name' => lang('ds_storegrade'), 'action' => null, 'controller' => 'Storegrade'),
+                    array('name' => lang('ds_storeclass'), 'action' => null, 'controller' => 'Storeclass'),
+//                    array('name' => lang('ds_chain'), 'action' => null, 'controller' => 'Chain'),
+                    array('name' => lang('ds_storesnstrace'), 'action' => null, 'controller' => 'Storesnstrace'),
+                    array('name' => lang('ds_Storehelp'), 'action' => null, 'controller' => 'Storehelp'),
+                    array('name' => lang('ds_storejoin'), 'action' => null, 'controller' => 'Storejoin'),
+                    array('name' => lang('ds_ownshop'), 'action' => null, 'controller' => 'Ownshop'),
+                )),
+            array('name' => lang('ds_member'), 'child' => array(
+                    array('name' => lang('ds_member_manage'), 'action' => null, 'controller' => 'Member'),
+                    array('name' => lang('member_auth'), 'action' => null, 'controller' => 'MemberAuth'),
+                    array('name' => lang('ds_membergrade'), 'action' => null, 'controller' => 'Membergrade'),
+                    array('name' => lang('ds_exppoints'), 'action' => null, 'controller' => 'Exppoints'),
+                    array('name' => lang('ds_notice'), 'action' => null, 'controller' => 'Notice'),
+                    array('name' => lang('ds_points'), 'action' => null, 'controller' => 'Points'),
+                    array('name' => lang('ds_snsmalbum'), 'action' => null, 'controller' => 'Snsmalbum'),
+                    array('name' => lang('ds_snsmember'), 'action' => null, 'controller' => 'Snsmember'),
+                    array('name' => lang('ds_predeposit'), 'action' => null, 'controller' => 'Predeposit'),
+                    array('name' => lang('instant_message'), 'action' => null, 'controller' => 'InstantMessage'),
+                )),
+            array('name' => lang('ds_trade'), 'child' => array(
+                    array('name' => lang('ds_order'), 'action' => null, 'controller' => 'Order'),
+                    array('name' => lang('ds_vrorder'), 'action' => null, 'controller' => 'Vrorder'),
+                    array('name' => lang('ds_refund'), 'action' => null, 'controller' => 'Refund'),
+                    array('name' => lang('ds_return'), 'action' => null, 'controller' => 'Returnmanage'),
+                    array('name' => lang('ds_vrrefund'), 'action' => null, 'controller' => 'Vrrefund'),
+                    array('name' => lang('ds_bill_manage'), 'action' => null, 'controller' => 'Bill'),
+                    array('name' => lang('ds_consulting'), 'action' => null, 'controller' => 'Consulting'),
+                    array('name' => lang('ds_inform'), 'action' => null, 'controller' => 'Inform'),
+                    array('name' => lang('ds_evaluate'), 'action' => null, 'controller' => 'Evaluate'),
+                    array('name' => lang('ds_complain'), 'action' => null, 'controller' => 'Complain'),
+                )),
+            array('name' => lang('ds_website'), 'child' => array(
+                    array('name' => lang('ds_articleclass'), 'action' => null, 'controller' => 'Articleclass'),
+                    array('name' => lang('ds_article'), 'action' => null, 'controller' => 'Article'),
+                    array('name' => lang('ds_document'), 'action' => null, 'controller' => 'Document'),
+                    array('name' => lang('ds_navigation'), 'action' => null, 'controller' => 'Navigation'),
+                    array('name' => lang('ds_adv'), 'action' => null, 'controller' => 'Adv'),
+                    array('name' => lang('editable_page_pc'), 'action' => 'page_list', 'controller' => 'EditablePage'),
+                    array('name' => lang('editable_page_h5'), 'action' => 'h5_page_list', 'controller' => 'EditablePage'),
+                    array('name' => lang('ds_friendlink'), 'action' => null, 'controller' => 'Link'),
+                    array('name' => lang('ds_mall_consult'), 'action' => null, 'controller' => 'Mallconsult'),
+                    array('name' => lang('ds_feedback'), 'action' => null, 'controller' => 'Feedback'),
+            )),
+            array('name' => lang('ds_operation'), 'child' => array(
+                    array('name' => lang('ds_operation_set'), 'action' => null, 'controller' => 'Operation|Promotionwholesale|Promotionxianshi|Promotionmansong|Promotionbundling|Promotionbooth|Groupbuy|Vrgroupbuy|Voucher|Promotionmgdiscount|Promotionpintuan|Promotionbargain|Activity|EditablePage|Inviter|Bonus|Marketmanage|Pointprod|Pointorder|Rechargecard|Flea|Fleaseo|Fleaclass|Fleaclassindex|Flearegion|Fleaseo|Promotionpresell'),
+                )),
+            array('name' => lang('ds_stat'), 'child' => array(
+                    array('name' => lang('ds_statgeneral'), 'action' => null, 'controller' => 'Statgeneral'),
+                    array('name' => lang('ds_statindustry'), 'action' => null, 'controller' => 'Statindustry'),
+                    array('name' => lang('ds_statmember'), 'action' => null, 'controller' => 'Statmember'),
+                    array('name' => lang('ds_statstore'), 'action' => null, 'controller' => 'Statstore'),
+                    array('name' => lang('ds_stattrade'), 'action' => null, 'controller' => 'Stattrade'),
+                    array('name' => lang('ds_statgoods'), 'action' => null, 'controller' => 'Statgoods'),
+                    array('name' => lang('ds_statmarketing'), 'action' => null, 'controller' => 'Statmarketing'),
+                    array('name' => lang('ds_stataftersale'), 'action' => null, 'controller' => 'Stataftersale'),
+                )),
+            array('name' => lang('mobile'), 'child' => array(
+                    array('name' => lang('appadv'), 'action' => null, 'controller' => 'Appadv'),
+                )),
+            array('name' => lang('wechat'), 'child' => array(
+                    array('name' => lang('wechat_setting'), 'action' => 'setting', 'controller' => 'Wechat'),
+                    array('name' => lang('wechat_template_message'), 'action' => 'template_message', 'controller' => 'Wechat'),
+                    array('name' => lang('wechat_menu'), 'action' => 'menu', 'controller' => 'Wechat'),
+                    array('name' => lang('wechat_keywords'), 'action' => 'k_text', 'controller' => 'Wechat'),
+                    array('name' => lang('wechat_member'), 'action' => 'member', 'controller' => 'Wechat'),
+                    array('name' => lang('wechat_push'), 'action' => 'SendList', 'controller' => 'Wechat'),
+                )),
+            array('name' => lang('ds_live'), 'child' => array(
+                    array('name' => lang('live_setting'), 'action' => null, 'controller' => 'LiveSetting'),
+                    array('name' => lang('live_apply'), 'action' => null, 'controller' => 'LiveApply'),
+                    array('name' => lang('live_goods'), 'action' => null, 'controller' => 'LiveGoods'),
+                )),
+        );
+
+        return $_limit;
+    }
+
+}
+
+?>

+ 194 - 0
app/admin/controller/Adminlog.php

@@ -0,0 +1,194 @@
+<?php
+
+namespace app\admin\controller;
+use think\facade\View;
+use think\facade\Db;
+use think\facade\Lang;
+
+/**
+ * ============================================================================
+ * DSMall多用户商城
+ * ============================================================================
+ * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
+ * 网站地址: http://www.csdeshang.com
+ * ----------------------------------------------------------------------------
+ * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
+ * 不允许对程序代码以任何形式任何目的的再发布。
+ * ============================================================================
+ * 控制器
+ */
+class Adminlog extends AdminControl
+{
+    const EXPORT_SIZE = 5000;
+    public function initialize()
+    {
+        parent::initialize(); // TODO: Change the autogenerated stub
+        Lang::load(base_path() . 'admin/lang/'.config('lang.default_lang').'/adminlog.lang.php');
+    }
+
+    /**
+     * 日志列表
+     *
+     */
+    public function loglist()
+    {
+        $condition = array();
+        $time1='';
+        $time2='';
+        if (!empty(input('param.admin_name'))) {
+            $condition[]=array('admin_name','=',input('param.admin_name'));
+        }
+        if (!empty(input('param.time_from'))) {
+            $time1 = strtotime(input('param.time_from'));
+        }
+        if (!empty(input('param.time_to'))) {
+            $time2 = strtotime(input('param.time_to'));
+            if ($time2 !== false)
+                $time2 = $time2 + 86400;
+        }
+        if ($time1 && $time2) {
+            $condition[] = array('adminlog_time','between', array($time1, $time2));
+        }
+        elseif ($time1) {
+            $condition[]=array('adminlog_time','>=', $time1);
+        }
+        elseif ($time2) {
+            $condition[]=array('adminlog_time','<=', $time2);
+        }
+        $adminlog_model= model('adminlog');
+        $order='adminlog_id desc';
+        $adminlog_list = $adminlog_model->getAdminlogList($condition,10,$order);
+        View::assign('adminlog_list', $adminlog_list);
+        View::assign('show_page', $adminlog_model->page_info->render());
+        
+        View::assign('filtered', $condition ? 1 : 0); //是否有查询条件
+        
+        $this->setAdminCurItem('loglist');
+        return View::fetch();
+    }
+
+    /**
+     * 删除日志
+     *
+     */
+    public function list_del()
+    {
+        $adminlog_id = input('param.adminlog_id');
+        $adminlog_id_array = ds_delete_param($adminlog_id);
+        if ($adminlog_id_array == FALSE) {
+            ds_json_encode('10001', lang('param_error'));
+        }
+        $condition = array();
+        $condition[] = array('adminlog_id','in',$adminlog_id_array);
+        $adminlog_model=model('adminlog');
+        if (!$adminlog_model->delAdminlog($condition)) {
+            $this->log(lang('ds_del').lang('admin_log'), 0);
+            ds_json_encode('10001', lang('ds_common_del_fail'));
+        }
+        else {
+            $this->log(lang('ds_del').lang('admin_log'), 1);
+            ds_json_encode('10000', lang('ds_common_del_succ'));
+        }
+    }
+
+    /**
+     * 导出第一步
+     */
+    public function export_step1()
+    {
+        $time1='';
+        $time2='';
+        $condition = array();
+        if (!empty(input('param.admin_name'))) {
+            $condition[]=array('admin_name','=',input('param.admin_name'));
+        }
+        if (!empty(input('param.time_from'))) {
+            $time1 = strtotime(input('param.time_from'));
+        }
+        if (!empty(input('param.time_to'))) {
+            $time2 = strtotime(input('param.time_to'));
+            if ($time2 !== false)
+                $time2 = $time2 + 86400;
+        }
+        if ($time1 && $time2) {
+            $condition[] = array('createtime','between', array($time1, $time2));
+        }
+        elseif ($time1) {
+            $condition[]=array('createtime','>=', $time1);
+        }
+        elseif ($time2) {
+            $condition[]=array('createtime','<=', $time2);
+        }
+        if (!is_numeric(input('param.page'))) {
+            $adminlog_model=model('adminlog');
+            $count = $adminlog_model->getAdminlogCount($condition);
+            $export_list = array();
+            if ($count > self::EXPORT_SIZE) {    //显示下载链接
+                $page = ceil($count / self::EXPORT_SIZE);
+                for ($i = 1; $i <= $page; $i++) {
+                    $limit1 = ($i - 1) * self::EXPORT_SIZE + 1;
+                    $limit2 = $i * self::EXPORT_SIZE > $count ? $count : $i * self::EXPORT_SIZE;
+                    $export_list[$i] = $limit1 . ' ~ ' . $limit2;
+                }
+                View::assign('export_list', $export_list);
+                return View::fetch('/public/excel');
+            }
+            else {    //如果数量小,直接下载
+                $data = Db::name('adminlog')->where($condition)->order('adminlog_id desc')->limit(self::EXPORT_SIZE)->select()->toArray();
+                $this->createExcel($data);
+            }
+        }
+        else {    //下载
+            $limit1 = (input('param.page') - 1) * self::EXPORT_SIZE;
+            $limit2 = self::EXPORT_SIZE;
+            $data = Db::name('adminlog')->where($condition)->order('adminlog_id desc')->limit($limit1,$limit2)->select()->toArray();
+            $this->createExcel($data);
+        }
+    }
+
+    /**
+     * 生成excel
+     *
+     * @param array $data
+     */
+    private function createExcel($data = array())
+    {
+        Lang::load(base_path() .'admin/lang/'.config('lang.default_lang').'/export.lang.php');
+        $excel_obj = new \excel\Excel();
+        $excel_data = array();
+        //设置样式
+        $excel_obj->setStyle(array(
+                                 'id' => 's_title', 'Font' => array('FontName' => lang('ds_song_typeface'), 'Size' => '12', 'Bold' => '1')
+                             ));
+        //header
+        $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('admin_log_man'));
+        $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('admin_log_do'));
+        $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('admin_log_dotime'));
+        $excel_data[0][] = array('styleid' => 's_title', 'data' => 'IP');
+        foreach ((array)$data as $k => $v) {
+            $tmp = array();
+            $tmp[] = array('data' => $v['admin_name']);
+            $tmp[] = array('data' => $v['adminlog_content']);
+            $tmp[] = array('data' => date('Y-m-d H:i:s', $v['adminlog_time']));
+            $tmp[] = array('data' => $v['adminlog_ip']);
+            $excel_data[] = $tmp;
+        }
+        $excel_data = $excel_obj->charset($excel_data, CHARSET);
+        $excel_obj->addArray($excel_data);
+        $excel_obj->addWorksheet($excel_obj->charset(lang('admin_log'), CHARSET));
+        $excel_obj->generateXML($excel_obj->charset(lang('admin_log'), CHARSET) . input('param.page') . '-' . date('Y-m-d-H', TIMESTAMP));
+    }
+
+    protected function getAdminItemList()
+    {
+        $menu_array = array(
+            array(
+                'name' => 'loglist',
+                'text' => lang('admin_log'),
+                'url' => (string)url('Adminlog/loglist')
+            )
+            );
+        return $menu_array;
+
+    }
+}

+ 407 - 0
app/admin/controller/Adv.php

@@ -0,0 +1,407 @@
+<?php
+
+namespace app\admin\controller;
+use think\facade\View;
+use think\facade\Lang;
+
+/**
+ * ============================================================================
+ * DSMall多用户商城
+ * ============================================================================
+ * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
+ * 网站地址: http://www.csdeshang.com
+ * ----------------------------------------------------------------------------
+ * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
+ * 不允许对程序代码以任何形式任何目的的再发布。
+ * ============================================================================
+ * 控制器
+ */
+class Adv extends AdminControl {
+
+    public function initialize() {
+        parent::initialize();
+        Lang::load(base_path() . 'admin/lang/'.config('lang.default_lang').'/adv.lang.php');
+    }
+
+    /**
+     *
+     * 管理广告位
+     */
+    public function ap_manage() {
+        $adv_model = model('adv');
+        /**
+         * 多选删除广告位
+         */
+        if (!request()->isPost()) {
+            /**
+             * 显示广告位管理界面
+             */
+            $condition = array();
+            $orderby = '';
+            $search_name = trim(input('get.search_name'));
+            if ($search_name != '') {
+                $condition[]=array('ap_name','like', "%" . $search_name . "%");
+            }
+            $ap_list = $adv_model->getAdvpositionList($condition, '10', $orderby);
+            $adv_list = $adv_model->getAdvList();
+            View::assign('ap_list', $ap_list);
+            View::assign('adv_list', $adv_list);
+            View::assign('showpage', $adv_model->page_info->render());
+            
+            View::assign('filtered', $condition ? 1 : 0); //是否有查询条件
+            
+            $this->setAdminCurItem('ap_manage');
+            return View::fetch('ap_manage');
+        }
+    }
+
+    /**
+     *
+     * 修改广告位
+     */
+    public function ap_edit() {
+        $ap_id = intval(input('param.ap_id'));
+        $adv_model = model('adv');
+        if (!request()->isPost()) {
+            $condition = array();
+            $condition[] = array('ap_id','=',$ap_id);
+            $ap = $adv_model->getOneAdvposition($condition);
+            View::assign('ref_url', get_referer());
+            View::assign('ap', $ap);
+            return View::fetch('ap_form');
+        } else {
+            $param['ap_name'] = trim(input('post.ap_name'));
+            $param['ap_intro'] = trim(input('post.ap_intro'));
+            $param['ap_width'] = intval(trim(input('post.ap_width')));
+            $param['ap_height'] = intval(trim(input('post.ap_height')));
+            if (input('post.ap_isuse') != '') {
+                $param['ap_isuse'] = intval(input('post.ap_isuse'));
+            }
+
+            $adv_validate = ds_validate('adv');
+            if (!$adv_validate->scene('ap_edit')->check($param)) {
+                $this->error($adv_validate->getError());
+            }
+
+            $result = $adv_model->editAdvposition($ap_id,$param);
+
+            if ($result>=0) {
+                $this->log(lang('ap_change_succ') . '[' . input('post.ap_name') . ']', null);
+                dsLayerOpenSuccess(lang('ap_change_succ'));
+            } else {
+                $this->error(lang('ap_change_fail'));
+            }
+        }
+    }
+
+    /**
+     *
+     * 新增广告位
+     */
+    public function ap_add() {
+        if (!request()->isPost()) {
+            $ap['ap_isuse'] = 1;
+            View::assign('ap', $ap);
+            return View::fetch('ap_form');
+        } else {
+            $adv_model = model('adv');
+
+            $insert_array['ap_name'] = trim(input('post.ap_name'));
+            $insert_array['ap_intro'] = trim(input('post.ap_intro'));
+            $insert_array['ap_isuse'] = intval(input('post.ap_isuse'));
+            $insert_array['ap_width'] = intval(input('post.ap_width'));
+            $insert_array['ap_height'] = intval(input('post.ap_height'));
+
+            $adv_validate = ds_validate('adv');
+            if (!$adv_validate->scene('ap_add')->check($insert_array)) {
+                $this->error($adv_validate->getError());
+            }
+
+            $result = $adv_model->addAdvposition($insert_array);
+
+            if ($result) {
+                $this->log(lang('ap_add_succ') . '[' . input('post.ap_name') . ']', null);
+                dsLayerOpenSuccess(lang('ap_add_succ'));
+           } else {
+                $this->error(lang('ap_add_fail'));
+            }
+        }
+    }
+
+    /**
+     *
+     * 删除广告位
+     */
+    public function ap_del() {
+        $adv_model = model('adv');
+        /**
+         * 删除一个广告
+         */
+        $ap_id = intval(input('param.ap_id'));
+        $result = $adv_model->delAdvposition($ap_id);
+
+        if (!$result) {
+            ds_json_encode('10001', lang('ap_del_fail'));
+        } else {
+            $this->log(lang('ap_del_succ') . '[' . $ap_id . ']', null);
+            ds_json_encode('10000', lang('ap_del_succ'));
+        }
+    }
+
+    /**
+     *
+     * 广告管理
+     */
+    public function adv() {
+        $adv_model = model('adv');
+
+        $ap_id = intval(input('param.ap_id'));
+        if (!request()->isPost()) {
+            $condition = array();
+            if ($ap_id) {
+                 $condition[] = array('ap_id','=',$ap_id);
+            }
+            $adv_info = $adv_model->getAdvList($condition, 20, '', '');
+            View::assign('adv_info', $adv_info);
+            $ap_list = $adv_model->getAdvpositionList();
+            View::assign('ap_list', $ap_list);
+            if ($ap_id) {
+                $ap_condition=array();
+                $ap_condition['ap_id'] = $ap_id;
+                $ap = $adv_model->getOneAdvposition($ap_condition);
+                View::assign('ap_name', $ap['ap_name']);
+            } else {
+                View::assign('ap_name', '');
+            }
+
+            View::assign('show_page', $adv_model->page_info->render());
+            
+            View::assign('filtered', $condition ? 1 : 0); //是否有查询条件
+            $this->setAdminCurItem('adv');
+            return View::fetch('adv_index');
+        }
+    }
+
+    /**
+     * 管理员添加广告
+     */
+    public function adv_add() {
+        $adv_model = model('adv');
+        if (!request()->isPost()) {
+
+            $ap_list = $adv_model->getAdvpositionList();
+            View::assign('ap_list', $ap_list);
+            $adv = array(
+                'ap_id' => 0,
+                'adv_enabled' => '1',
+                'adv_startdate' => TIMESTAMP,
+                'adv_enddate' => TIMESTAMP + 24 * 3600 * 365,
+            );
+            View::assign('adv', $adv);
+            return View::fetch('adv_form');
+        } else {
+            $insert_array['ap_id'] = intval(input('post.ap_id'));
+            $insert_array['adv_title'] = trim(input('post.adv_name'));
+            $insert_array['adv_link'] = input('post.adv_link');
+            $insert_array['adv_bgcolor'] = input('post.adv_bgcolor');
+            $insert_array['adv_sort'] = input('post.adv_sort');
+            $insert_array['adv_enabled'] = input('post.adv_enabled');
+            $insert_array['adv_startdate'] = $this->getunixtime(input('post.adv_startdate'));
+            $insert_array['adv_enddate'] = $this->getunixtime(input('post.adv_enddate'));
+
+            //上传文件保存路径
+            if (!empty($_FILES['adv_code']['name'])) {
+                $res=ds_upload_pic(ATTACH_ADV,'adv_code');
+                if($res['code']){
+                    $file_name=$res['data']['file_name'];
+                    $insert_array['adv_code'] = $file_name;
+                }else{
+                    $this->error($res['msg']);
+                }
+
+            }
+
+            $adv_validate = ds_validate('adv');
+            if (!$adv_validate->scene('adv_add')->check($insert_array)) {
+                $this->error($adv_validate->getError());
+            }
+
+            //广告信息入库
+            $result = $adv_model->addAdv($insert_array);
+
+            if ($result) {
+                $this->log(lang('adv_add_succ') . '[' . input('post.adv_name') . ']', null);
+                dsLayerOpenSuccess(lang('adv_add_succ'));
+//                $this->success(lang('adv_add_succ'), (string)url('Adv/adv', ['ap_id' => input('post.ap_id')]));
+            } else {
+                $this->error(lang('adv_add_fail'));
+            }
+        }
+    }
+
+    /**
+     *
+     * 修改广告
+     */
+    public function adv_edit() {
+        $adv_id = intval(input('param.adv_id'));
+        $adv_model = model('adv');
+        //获取指定广告
+        $condition = array();
+        $condition[] = array('adv_id','=',$adv_id);
+        $adv = $adv_model->getOneAdv($condition);
+        if (!request()->isPost()) {
+            //获取广告列表
+            $ap_list = $adv_model->getAdvpositionList();
+            View::assign('ap_list', $ap_list);
+            View::assign('adv', $adv);
+            View::assign('ref_url', get_referer());
+            return View::fetch('adv_form');
+        } else {
+            $param['ap_id'] = intval(input('post.ap_id'));
+            $param['adv_title'] = trim(input('post.adv_name'));
+            $param['adv_link'] = input('post.adv_link');
+            $param['adv_bgcolor'] = input('post.adv_bgcolor');
+            $param['adv_sort'] = input('post.adv_sort');
+            $param['adv_enabled'] = input('post.adv_enabled');
+            $param['adv_startdate'] = $this->getunixtime(trim(input('post.adv_startdate')));
+            $param['adv_enddate'] = $this->getunixtime(trim(input('post.adv_enddate')));
+
+
+            if (!empty($_FILES['adv_code']['name'])) {
+		//上传文件保存路径
+                $upload_file = BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_ADV;
+                $res=ds_upload_pic(ATTACH_ADV,'adv_code');
+                if($res['code']){
+					//还需删除原来图片
+                    if (!empty($adv['adv_code'])) {
+                        @unlink($upload_file . DIRECTORY_SEPARATOR . $adv['adv_code']);
+                    }
+                    $file_name=$res['data']['file_name'];
+                    $param['adv_code'] = $file_name;
+                }else{
+                    $this->error($res['msg']);
+                }
+                
+
+            }
+
+            $adv_validate = ds_validate('adv');
+            if (!$adv_validate->scene('adv_edit')->check($param)) {
+                $this->error($adv_validate->getError());
+            }
+
+            $result = $adv_model->editAdv($adv_id,$param);
+
+            if ($result>=0) {
+                $this->log(lang('adv_change_succ') . '[' . input('post.ap_name') . ']', null);
+                dsLayerOpenSuccess(lang('adv_change_succ'));
+//               $this->success(lang('adv_change_succ'), input('post.ref_url'));
+            } else {
+                $this->error(lang('adv_change_fail'));
+            }
+        }
+    }
+
+    /**
+     *
+     * 删除广告
+     */
+    public function adv_del() {
+        $adv_model = model('adv');
+        /**
+         * 删除一个广告
+         */
+        $adv_id = intval(input('param.adv_id'));
+        $result = $adv_model->delAdv($adv_id);
+
+        if (!$result) {
+            ds_json_encode('10001', lang('adv_del_fail'));
+        } else {
+            $this->log(lang('adv_del_succ') . '[' . $adv_id . ']', null);
+            ds_json_encode('10000', lang('adv_del_succ'));
+        }
+    }
+
+    /**
+     *
+     * 获取UNIX时间戳
+     */
+    public function getunixtime($time) {
+        $array = explode("-", $time);
+        $unix_time = mktime(0, 0, 0, $array[1], $array[2], $array[0]);
+        return $unix_time;
+    }
+
+    public function ajax() {
+        $adv_model = model('adv');
+        switch (input('get.branch')) {
+            case 'ap_branch':
+                $column = trim(input('param.column'));
+                $value = trim(input('param.value'));
+                $ap_id = intval(input('param.id'));
+                $param[$column] = trim($value);
+                $result = $adv_model->editAdvposition($ap_id,$param);
+                break;
+            //ADV数据表更新
+            case 'adv_branch':
+                $column = trim(input('param.column'));
+                $value = trim(input('param.value'));
+                $adv_id = intval(input('param.id'));
+                $param[$column] = trim($value);
+                $result = $adv_model->editAdv($adv_id,$param);
+                break;
+        }
+        if($result>=0){
+            echo 'true';
+        }else{
+            echo false;
+        }
+    }
+
+    function adv_template() {
+        $pages = $this->_get_editable_pages();
+        View::assign('pages', $pages);
+        $this->setAdminCurItem('adv_template');
+        return View::fetch();
+    }
+
+    /**
+     *    获取可以编辑的页面列表
+     */
+    function _get_editable_pages() {
+        return array(
+            lang('homepage') => (string)url('home/Index/index',['edit_ad'=>1]),
+            lang('flea') => (string)url('home/Flea/index',['edit_ad'=>1]),
+        );
+    }
+
+    /**
+     * 获取卖家栏目列表,针对控制器下的栏目
+     */
+    protected function getAdminItemList() {
+        $menu_array = array(
+            array(
+                'name' => 'ap_manage',
+                'text' => lang('ap_manage'),
+                'url' => (string)url('Adv/ap_manage')
+            ),
+        );
+        $menu_array[] = array(
+            'name' => 'adv',
+            'text' => lang('adv_manage'),
+            'url' => (string)url('Adv/adv')
+        );
+        $menu_array[] = array(
+            'name' => 'adv_add',
+            'text' => lang('adv_add'),
+            'url' => "javascript:dsLayerOpen('".(string)url('Adv/adv_add', ['ap_id' => input('param.ap_id')])."','".lang('adv_add')."')"
+        );
+
+
+        return $menu_array;
+    }
+
+}
+
+?>

+ 380 - 0
app/admin/controller/Appadv.php

@@ -0,0 +1,380 @@
+<?php
+namespace app\admin\controller;
+use think\facade\View;
+use think\facade\Lang;
+
+/**
+ * ============================================================================
+ * DSMall多用户商城
+ * ============================================================================
+ * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
+ * 网站地址: http://www.csdeshang.com
+ * ----------------------------------------------------------------------------
+ * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
+ * 不允许对程序代码以任何形式任何目的的再发布。
+ * ============================================================================
+ * 控制器
+ */
+class Appadv extends AdminControl {
+
+    public function initialize() {
+        parent::initialize();
+        Lang::load(base_path() . 'admin/lang/'.config('lang.default_lang').'/adv.lang.php');
+    }
+    
+    function index()
+    {
+        /**
+         * 显示广告位管理界面
+         */
+        $condition = array();
+        $search_name = trim(input('get.search_name'));
+        if ($search_name != '') {
+            $condition[] = array('ap_name','=',$search_name);
+        }
+        $appadv_model = model('appadv');
+        $ap_list= $appadv_model->getAppadvpositionList($condition,'10');
+        $adv_list = $appadv_model->getAppadvList();
+        
+        View::assign('ap_list',$ap_list);
+        View::assign('adv_list',$adv_list);
+        View::assign('showpage', $appadv_model->page_info->render());
+        
+        View::assign('filtered', $condition ? 1 : 0); //是否有查询条件
+        
+        $this->setAdminCurItem('index');
+        return View::fetch();
+    }
+
+    /**
+     *
+     * 新增广告位
+     */
+    public function ap_add() {
+        if (!request()->isPost()) {
+            $ap['ap_isuse']=1;
+            View::assign('ap',$ap);
+            return View::fetch('ap_form');
+        } else {
+            $appadv_model = model('appadv');
+            $insert_array['ap_name'] = trim(input('post.ap_name'));
+            $insert_array['ap_intro'] = trim(input('post.ap_intro'));
+            $insert_array['ap_isuse'] = intval(input('post.ap_isuse'));
+            $insert_array['ap_width'] = intval(input('post.ap_width'));
+            $insert_array['ap_height'] = intval(input('post.ap_height'));
+
+            $adv_validate = ds_validate('adv');
+            if (!$adv_validate->scene('app_ap_add')->check($insert_array)) {
+                $this->error($adv_validate->getError());
+            }
+
+            $result = $appadv_model->addAppadvposition($insert_array);
+
+            if ($result) {
+                $this->log(lang('ap_add_succ') . '[' . input('post.ap_name') . ']', null);
+                dsLayerOpenSuccess(lang('ap_add_succ'),(string)url('Appadv/index'));
+            } else {
+                $this->error(lang('ap_add_fail'));
+            }
+        }
+    }
+
+
+    /**
+     *
+     * 删除广告位
+     */
+    public function ap_del() {
+        $appadv_model = model('appadv');
+        /**
+         * 删除一个广告位
+         */
+        $ap_id = intval(input('param.ap_id'));
+        $result = $appadv_model->delAppadvposition($ap_id);
+        if (!$result) {
+            ds_json_encode(10001, lang('ap_del_fail'));
+        } else {
+            $this->log(lang('ap_del_succ') . '[' . $ap_id . ']', null);
+            ds_json_encode(10000, lang('ap_del_succ'));
+        }
+    }
+
+    /**
+     *
+     * 删除广告
+     */
+    public function adv_del() {
+        $appadv_model = model('appadv');
+        /**
+         * 删除一个广告
+         */
+        $adv_id = intval(input('param.adv_id'));
+        $result = $appadv_model->delAppadv($adv_id);
+
+        if (!$result) {
+            ds_json_encode(10001, lang('adv_del_fail'));
+        } else {
+            $this->log(lang('adv_del_succ') . '[' . $adv_id . ']', null);
+            ds_json_encode(10000, lang('adv_del_succ'));
+        }
+    }
+
+    /**
+     *
+     * 修改广告
+     */
+    public function adv_edit() {
+        $adv_id = intval(input('param.adv_id'));
+        $appadv_model = model('appadv');
+        //获取指定广告
+        $condition = array();
+        $condition[] = array('adv_id','=',$adv_id);
+        $adv = $appadv_model->getOneAppadv($condition);
+        if (!request()->isPost()) {
+            //获取广告列表
+            $ap_list = $appadv_model->getAppadvpositionList();
+            View::assign('ap_list', $ap_list);
+            View::assign('adv', $adv);
+            View::assign('ref_url', get_referer());
+            return View::fetch('adv_form');
+        } else {
+            $param['ap_id'] = intval(input('post.ap_id'));
+            $param['adv_title'] = trim(input('post.adv_name'));
+            $param['adv_type'] = input('post.adv_type');
+            $param['adv_typedate'] = input('post.adv_typedate');
+            $param['adv_sort'] = input('post.adv_sort');
+            $param['adv_enabled'] = input('post.adv_enabled');
+            $param['adv_startdate'] = $this->getunixtime(trim(input('post.adv_startdate')));
+            $param['adv_enddate'] = $this->getunixtime(trim(input('post.adv_enddate')));
+
+
+            if (!empty($_FILES['adv_code']['name'])) {
+                //上传文件保存路径
+                $upload_file = BASE_UPLOAD_PATH . '/' . ATTACH_APPADV;
+                $res=ds_upload_pic(ATTACH_APPADV,'adv_code');
+                if($res['code']){
+                    //还需删除原来图片
+                    if (!empty($adv['adv_code'])) {
+                        @unlink($upload_file . DIRECTORY_SEPARATOR . $adv['adv_code']);
+                    }
+                    $file_name=$res['data']['file_name'];
+                    $param['adv_code'] = $file_name;
+                }else{
+                    $this->error($res['msg']);
+                }
+                
+            }
+
+            $adv_validate = ds_validate('adv');
+            if (!$adv_validate->scene('app_adv_edit')->check($param)) {
+                $this->error($adv_validate->getError());
+            }
+
+            $result = $appadv_model->editAppadv($adv_id,$param);
+
+            if ($result>=0) {
+                $this->log(lang('adv_change_succ') . '[' . input('post.ap_name') . ']', null);
+                dsLayerOpenSuccess(lang('adv_change_succ'),input('post.ref_url'));
+            } else {
+                $this->error(lang('adv_change_fail'));
+            }
+        }
+    }
+    
+    public function ajax() {
+        $appadv_model = model('appadv');
+        switch (input('get.branch')) {
+            case 'ap_branch':
+                $column = input('param.column');
+                $value = input('param.value');
+                $ap_id = intval(input('param.id'));
+                $param[$column] = trim($value);
+                $result = $appadv_model->editAppadvposition($ap_id,$param);
+                break;
+            //ADV数据表更新
+            case 'adv_branch':
+                $column = input('param.column');
+                $value = input('param.value');
+                $adv_id = intval(input('param.id'));
+                $param[$column] = trim($value);
+                $result = $appadv_model->editAppAdv($adv_id,$param);
+                break;
+        }
+        if($result>=0){
+            echo 'true';
+        }else{
+            echo false;
+        }
+    }
+    
+    
+    /**
+     *
+     * 广告管理
+     */
+    public function adv() {
+        $appadv_model = model('appadv');
+        $ap_id = intval(input('param.ap_id'));
+        if (!request()->isPost()) {
+            $condition = array();
+            if ($ap_id) {
+                $condition[] = array('ap_id','=',$ap_id);
+            }
+            $adv_info = $appadv_model->getAppadvList($condition, 20, '', '');
+            View::assign('adv_info', $adv_info);
+            $ap_list = $appadv_model->getAppadvpositionList();
+            View::assign('ap_list', $ap_list);
+            if ($ap_id) {
+                $ap_condition=array();
+                $ap_condition[] = array('ap_id','=',$ap_id);
+                $ap = $appadv_model->getOneAppadvposition($ap_condition); 
+                View::assign('ap_name', $ap['ap_name']);
+            } else {
+                View::assign('ap_name', '');
+            }
+
+            View::assign('show_page', $appadv_model->page_info->render());
+            
+            View::assign('filtered', $condition ? 1 : 0); //是否有查询条件
+            
+            $this->setAdminCurItem('adv');
+            return View::fetch('adv_index');
+        }
+    }
+
+    /**
+     * 管理员添加广告
+     */
+    public function appadv_add() {
+        $appadv_model = model('appadv');
+        if (!request()->isPost()) {
+
+            $ap_list = $appadv_model->getAppadvpositionList();
+            View::assign('ap_list', $ap_list);
+            $adv = array(
+                'ap_id' => 0,
+                'adv_enabled' => '1',
+                'adv_startdate' => TIMESTAMP,
+                'adv_enddate' => TIMESTAMP + 24 * 3600 * 365,
+                'adv_type'=>''
+            );
+            View::assign('adv', $adv);
+            return View::fetch('adv_form');
+        } else {
+            $insert_array['ap_id'] = intval(input('post.ap_id'));
+            $insert_array['adv_title'] = trim(input('post.adv_name'));
+            $insert_array['adv_type'] = input('post.adv_type');
+            $insert_array['adv_typedate'] = input('post.adv_typedate');
+            $insert_array['adv_sort'] = input('post.adv_sort');
+            $insert_array['adv_enabled'] = input('post.adv_enabled');
+            $insert_array['adv_startdate'] = $this->getunixtime(input('post.adv_startdate'));
+            $insert_array['adv_enddate'] = $this->getunixtime(input('post.adv_enddate'));
+
+            //上传文件保存路径
+            $upload_file = BASE_UPLOAD_PATH . '/' . ATTACH_APPADV;
+            if (!empty($_FILES['adv_code']['name'])) {
+                $res=ds_upload_pic(ATTACH_APPADV,'adv_code');
+                if($res['code']){
+                    //还需删除原来图片
+                    if (!empty($adv['adv_code'])) {
+                        @unlink($upload_file . DIRECTORY_SEPARATOR . $adv['adv_code']);
+                    }
+                    $file_name=$res['data']['file_name'];
+                    $insert_array['adv_code'] = $file_name;
+                }else{
+                    $this->error($res['msg']);
+                }
+            }
+
+            $adv_validate = ds_validate('adv');
+            if (!$adv_validate->scene('app_adv_add')->check($insert_array)) {
+                $this->error($adv_validate->getError());
+            }
+
+            //广告信息入库
+            $result = $appadv_model->addAppadv($insert_array);
+            //更新相应广告位所拥有的广告数量
+            $ap_condition=array();
+            $ap_condition['ap_id']=intval(input('post.ap_id'));
+            $appadv_model->getOneAppadvposition($ap_condition);
+            if ($result) {
+                $this->log(lang('adv_add_succ') . '[' . input('post.adv_name') . ']', null);
+                dsLayerOpenSuccess(lang('adv_add_succ'),(string)url('Appadv/adv', ['ap_id' => input('post.ap_id')]));
+            } else {
+                $this->error(lang('adv_add_fail'));
+            }
+        }
+    }
+
+    /**
+     *
+     * 修改广告位
+     */
+    public function ap_edit() {
+        $ap_id = intval(input('param.ap_id'));
+
+        $appadv_model = model('appadv');
+        if (!request()->isPost()) {
+            $condition = array();
+            $condition[] = array('ap_id','=',$ap_id);
+            $ap = $appadv_model->getOneAppadvposition($condition);
+            View::assign('ref_url', get_referer());
+            View::assign('ap', $ap);
+            return View::fetch('ap_form');
+        } else {
+            $param['ap_name'] = trim(input('post.ap_name'));
+            $param['ap_intro'] = trim(input('post.ap_intro'));
+            $param['ap_width'] = intval(trim(input('post.ap_width')));
+            $param['ap_height'] = intval(trim(input('post.ap_height')));
+            if (input('post.ap_isuse') != '') {
+                $param['ap_isuse'] = intval(input('post.ap_isuse'));
+            }
+            $adv_validate = ds_validate('adv');
+            if (!$adv_validate->scene('app_ap_edit')->check($param)) {
+                $this->error($adv_validate->getError());
+            }
+
+            $result = $appadv_model->editAppadvposition($ap_id,$param);
+
+            if ($result>=0) {
+                $this->log(lang('ap_change_succ') . '[' . input('post.ap_name') . ']', null);
+                dsLayerOpenSuccess(lang('ap_change_succ'),input('post.ref_url'));
+            } else {
+                $this->error(lang('ap_change_fail'));
+            }
+        }
+    }
+    /**
+     *
+     * 获取UNIX时间戳
+     */
+    public function getunixtime($time) {
+        $array = explode("-", $time);
+        $unix_time = mktime(0, 0, 0, $array[1], $array[2], $array[0]);
+        return $unix_time;
+    }
+    /**
+     * 获取卖家栏目列表,针对控制器下的栏目
+     */
+    protected function getAdminItemList() {
+        $menu_array = array(
+            array(
+                'name' => 'index',
+                'text' => lang('ap_manage'),
+                'url' => (string)url('Appadv/index')
+            ),
+        );
+        $menu_array[] = array(
+            'name' => 'adv',
+            'text' => lang('adv_manage'),
+            'url' => (string)url('Appadv/adv')
+        );
+        $menu_array[] = array(
+            'name' => 'adv_add',
+            'text' => lang('adv_add'),
+            'url' => "javascript:dsLayerOpen('".(string)url('Appadv/appadv_add', ['ap_id' => input('param.ap_id')])."','".lang('adv_add')."')"
+        );
+        return $menu_array;
+    }
+    
+}

+ 71 - 0
app/admin/controller/Arrivalnotice.php

@@ -0,0 +1,71 @@
+<?php
+
+namespace app\admin\controller;
+use think\facade\View;
+use think\facade\Lang;
+
+/**
+ * ============================================================================
+ * DSMall多用户商城
+ * ============================================================================
+ * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
+ * 网站地址: http://www.csdeshang.com
+ * ----------------------------------------------------------------------------
+ * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
+ * 不允许对程序代码以任何形式任何目的的再发布。
+ * ============================================================================
+ * 控制器
+ */
+class Arrivalnotice extends AdminControl
+{
+    public function initialize() {
+        parent::initialize();
+        Lang::load(base_path() . 'admin/lang/'.config('lang.default_lang').'/arrivalnotice.lang.php');
+    }
+
+    /**
+     * 到货通知列表
+     * @return mixed
+     */
+    public function index() {
+        $arrivalnotice_model = model('arrivalnotice');
+        $condition = array();
+        if (!empty(input('param.search_goods'))) {
+            $condition[]=array('goods_name','like', '%' . input('param.search_goods') . '%');
+        }
+        if (!empty(input('param.search_state'))) {
+            $condition[]=array('arrivalnotice_state','=',input('param.search_state'));
+        }
+        $arrivalnotice_list = $arrivalnotice_model->getArrivalNoticeList($condition,'','','',5);
+        foreach ($arrivalnotice_list as $key => $value){
+            $arrivalnotice_list[$key]['member_name'] = model('member')->getMemberInfo(['member_id'=>$value['member_id']],'member_name')['member_name'];
+        }
+
+        View::assign('arrivalnotice_list', $arrivalnotice_list);
+        View::assign('show_page', $arrivalnotice_model->page_info->render());
+        $this->setAdminCurItem('index');
+        View::assign('filtered', $condition ? 1 : 0); //是否有查询条件
+        return View::fetch();
+    }
+
+    /**
+     * 到货通知删除
+     */
+    public function arrivalnotice_del(){
+        $arrivalnotice_id = input('param.arrivalnotice_id');
+        $arrivalnotice_id_array = ds_delete_param($arrivalnotice_id);
+        if ($arrivalnotice_id_array == FALSE) {
+            ds_json_encode('10001', lang('param_error'));
+        }
+        $condition = array();
+        $condition[] = array('arrivalnotice_id','in',$arrivalnotice_id_array);
+        $arrivalnotice_model = model('arrivalnotice');
+        //批量删除
+        $result = $arrivalnotice_model->delArrivalNotice($condition);
+        if ($result){
+            ds_json_encode(10000, lang('ds_common_del_succ'));
+        }else{
+            ds_json_encode(10001, lang('ds_common_del_fail'));
+        }
+    }
+}

+ 354 - 0
app/admin/controller/Article.php

@@ -0,0 +1,354 @@
+<?php
+
+namespace app\admin\controller;
+use think\facade\View;
+use think\facade\Lang;
+
+/**
+ * ============================================================================
+ * DSMall多用户商城
+ * ============================================================================
+ * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
+ * 网站地址: http://www.csdeshang.com
+ * ----------------------------------------------------------------------------
+ * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
+ * 不允许对程序代码以任何形式任何目的的再发布。
+ * ============================================================================
+ * 控制器
+ */
+class Article extends AdminControl {
+
+    public function initialize() {
+        parent::initialize();
+        Lang::load(base_path() . 'admin/lang/'.config('lang.default_lang').'/article.lang.php');
+    }
+
+    public function index() {
+
+        /**
+         * 检索条件
+         */
+        $condition = array();
+        $search_ac_id = intval(input('param.search_ac_id'));
+        if ($search_ac_id) {
+            $condition[]=array('ac_id','=',$search_ac_id);
+        }
+        $search_title = trim(input('param.search_title'));
+        if ($search_title) {
+            $condition[]=array('article_title','like', "%" . $search_title . "%");
+        }
+        $article_model = model('article');
+        $article_list = $article_model->getArticleList($condition, 10);
+
+        $articleclass_model = model('articleclass');
+        /**
+         * 整理列表内容
+         */
+        if (is_array($article_list)) {
+            /**
+             * 取文章分类
+             */
+            $class_list = $articleclass_model->getArticleclassList(array());
+            $tmp_class_name = array();
+            if (is_array($class_list)) {
+                foreach ($class_list as $k => $v) {
+                    $tmp_class_name[$v['ac_id']] = $v['ac_name'];
+                }
+            }
+            foreach ($article_list as $k => $v) {
+                /**
+                 * 发布时间
+                 */
+                $article_list[$k]['article_time'] = date('Y-m-d H:i:s', $v['article_time']);
+                /**
+                 * 所属分类
+                 */
+                if (@array_key_exists($v['ac_id'], $tmp_class_name)) {
+                    $article_list[$k]['ac_name'] = $tmp_class_name[$v['ac_id']];
+                }
+            }
+        }
+
+        /**
+         * 分类列表
+         */
+        $parent_list = $articleclass_model->getTreeClassList(2);
+        if (is_array($parent_list)) {
+            $unset_sign = false;
+            foreach ($parent_list as $k => $v) {
+                $parent_list[$k]['ac_name'] = str_repeat("&nbsp;", $v['deep'] * 2) . $v['ac_name'];
+            }
+        }
+
+        View::assign('article_list', $article_list);
+        View::assign('show_page', $article_model->page_info->render());
+        View::assign('search_title', $search_title);
+        View::assign('search_ac_id', $search_ac_id);
+        View::assign('parent_list', $parent_list);
+        
+        View::assign('filtered', $condition ? 1 : 0); //是否有查询条件
+        
+        $this->setAdminCurItem('index');
+        return View::fetch();
+    }
+
+    public function add() {
+        if (!(request()->isPost())) {
+            $article = [
+                'article_id' => 0,
+                'article_title' => '',
+                'ac_id' => input('param.ac_id'),
+                'article_url' => '',
+                'article_show' => 0,
+                'article_sort' => 0,
+                'article_content' => '',
+            ];
+            $articleclass_model = model('articleclass');
+            $cate_list = $articleclass_model->getTreeClassList(2);
+            View::assign('ac_list', $cate_list);
+            View::assign('article', $article);
+            //游离图片
+            $article_pic_list = model('upload')->getUploadList(array('upload_type' => '1', 'item_id' => 0));
+            View::assign('file_upload', $article_pic_list);
+            $this->setAdminCurItem('add');
+            return View::fetch('form');
+        } else {
+            $data = array(
+                'article_title' => input('post.article_title'),
+                'ac_id' => input('post.ac_id'),
+                'article_url' => input('post.article_url'),
+                'article_sort' => input('post.article_sort'),
+                'article_content' => input('post.article_content'),
+                'article_time' => TIMESTAMP,
+            );
+            $data['article_show'] = intval(input('post.article_show'));
+
+            $article_validate = ds_validate('article');
+            if (!$article_validate->scene('add')->check($data)) {
+                $this->error($article_validate->getError());
+            }
+
+            $article_id = model('article')->addArticle($data);
+            if ($article_id) {
+                //更新图片信息ID
+                $upload_model = model('upload');
+                $file_id_array = input('post.file_id/a');
+                if (is_array($file_id_array)) {
+                    foreach ($file_id_array as $k => $v) {
+                        $update_array = array();
+                        $update_array['item_id'] = $article_id;
+                        $upload_model->editUpload($update_array,array(array('upload_id','=',intval($v))));
+                        unset($update_array);
+                    }
+                }
+                //上传文章封面
+                if (!empty($_FILES['_pic']['name'])) {
+                    $res=ds_upload_pic(ATTACH_ARTICLE,'_pic');
+                    if($res['code']){
+                        $article_pic=$res['data']['file_name'];
+                        model('article')->editArticle(array('article_pic' => $article_pic), $article_id);
+                    }else{
+                        $this->error($res['msg'], (string) url('Article/edit', ['article_id' => $article_id]));
+                    }
+                }
+                $this->success(lang('ds_common_save_succ'), 'Article/index');
+            } else {
+                $this->error(lang('ds_common_save_fail'));
+            }
+        }
+    }
+
+    public function edit() {
+        $art_id = intval(input('param.article_id'));
+        if ($art_id<=0) {
+            $this->error(lang('param_error'));
+        }
+        $condition = array();
+        $condition[] = array('article_id','=',$art_id);
+        $article = model('article')->getOneArticle($condition);
+        if(!$article){
+            $this->error(lang('ds_no_record'));
+        }
+        if (!request()->isPost()) {
+            View::assign('article', $article);
+            $articleclass_model = model('articleclass');
+            $cate_list=$articleclass_model->getTreeClassList(2);
+            View::assign('ac_list', $cate_list);
+            //附属图片
+            $article_pic_list=model('upload')->getUploadList(array('upload_type'=>'1','item_id'=>$art_id));
+            View::assign('file_upload', $article_pic_list);
+            $this->setAdminCurItem('edit');
+            return View::fetch('form');
+        } else {
+            $data = array(
+                'article_title' => input('post.article_title'),
+                'ac_id' => input('post.ac_id'),
+                'article_url' => input('post.article_url'),
+                'article_sort' => input('post.article_sort'),
+                'article_content' => input('post.article_content'),
+                'article_time' => TIMESTAMP,
+            );
+            $data['article_show'] = intval(input('post.article_show'));
+            $article_validate = ds_validate('article');
+            if (!$article_validate->scene('edit')->check($data)) {
+                $this->error($article_validate->getError());
+            }
+
+            //上传文章封面
+            if (!empty($_FILES['_pic']['name'])) {
+                $res=ds_upload_pic(ATTACH_ARTICLE,'_pic');
+                if($res['code']){
+                    $file_name=$res['data']['file_name'];
+                    //删除原图
+                    if($article['article_pic']){
+                        @unlink(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_ARTICLE . DIRECTORY_SEPARATOR . $article['article_pic']);
+                    }
+                    $data['article_pic'] = $file_name;
+                }else{
+                    $this->error($res['msg'], (string)url('Article/edit', ['article_id' => $art_id]));
+                }
+            }
+            //验证数据  END
+            $result = model('article')->editArticle($data, $art_id);
+            if ($result) {
+                $this->success(lang('ds_common_save_succ'), 'Article/index');
+            } else {
+                $this->error(lang('ds_common_save_fail'));
+            }
+        }
+    }
+
+    public function drop() {
+        $article_id = input('param.article_id');
+        if (empty($article_id)) {
+            ds_json_encode(10001, lang('param_error'));
+        }
+        $condition = array();
+        $condition[] = array('article_id','=',$article_id);
+        $article = model('article')->getOneArticle($condition);
+        if(!$article){
+            ds_json_encode(10001, lang('ds_no_record'));
+        }
+        //删除图片
+        if($article['article_pic']){
+            @unlink(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_ARTICLE . DIRECTORY_SEPARATOR . $article['article_pic']);
+        }
+        $article_pic_list=model('upload')->getUploadList(array('upload_type'=>'1','item_id'=>$article_id));
+        foreach($article_pic_list as $article_pic){
+            @unlink(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_ARTICLE . DIRECTORY_SEPARATOR . $article_pic['file_name']);
+        }
+        $result = model('article')->delArticle($article_id);
+        if ($result) {
+            ds_json_encode(10000, lang('ds_common_op_succ'));
+        } else {
+            ds_json_encode(10001, lang('error'));
+        }
+    }
+
+    /**
+     * 文章图片上传
+     */
+    public function article_pic_upload() {
+        $file_name = '';
+        $file_object = request()->file('fileupload');
+        if ($file_object) {
+                $res=ds_upload_pic(ATTACH_ARTICLE,'fileupload');
+                if($res['code']){
+                    $file_name=$res['data']['file_name'];
+                }else{
+                    echo $res['msg'];
+                    exit;
+                }
+        } else {
+            echo 'error';
+            exit;
+        }
+
+        /**
+         * 模型实例化
+         */
+        $upload_model = model('upload');
+        /**
+         * 图片数据入库
+         */
+        $insert_array = array();
+        $insert_array['file_name'] = $file_name;
+        $insert_array['upload_type'] = '1';
+        $insert_array['file_size'] = $_FILES['fileupload']['size'];
+        $insert_array['item_id'] = intval(input('param.item_id'));
+        $insert_array['upload_time'] = TIMESTAMP;
+        $result = $upload_model->addUpload($insert_array);
+        if ($result) {
+            $data = array();
+            $data['file_id'] = $result;
+            $data['file_name'] = $file_name;
+            $data['file_path'] = ds_get_pic(ATTACH_ARTICLE , $file_name);
+            /**
+             * 整理为json格式
+             */
+            $output = json_encode($data);
+            echo $output;
+        }
+    }
+
+    /**
+     * ajax操作
+     */
+    public function ajax() {
+        switch (input('param.branch')) {
+            /**
+             * 删除文章图片
+             */
+            case 'del_file_upload':
+                if (intval(input('param.file_id')) > 0) {
+                    $upload_model = model('upload');
+                    /**
+                     * 删除图片
+                     */
+                    $file_array = $upload_model->getOneUpload(intval(input('param.file_id')));
+                    @unlink(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_ARTICLE . DIRECTORY_SEPARATOR . $file_array['file_name']);
+                    /**
+                     * 删除信息
+                     */
+                    $condition = array();
+                    $condition[] = array('upload_id','=',intval(input('param.file_id')));
+                    $upload_model->delUpload($condition);
+                    echo 'true';
+                    exit;
+                } else {
+                    echo 'false';
+                    exit;
+                }
+                break;
+        }
+    }
+    /**
+     * 获取卖家栏目列表,针对控制器下的栏目
+     */
+    protected function getAdminItemList() {
+        $menu_array = array(
+            array(
+                'name' => 'index',
+                'text' => lang('ds_manage'),
+                'url' => (string)url('Article/index')
+            ),
+        );
+
+        if (request()->action() == 'add' || request()->action() == 'index') {
+            $menu_array[] = array(
+                'name' => 'add',
+                'text' => lang('ds_new'),
+                'url' => (string)url('Article/add')
+            );
+        }
+        if (request()->action() == 'edit') {
+            $menu_array[] = array(
+                'name' => 'edit',
+                'text' => lang('ds_edit'),
+                'url' => 'javascript:void(0)'
+            );
+        }
+        return $menu_array;
+    }
+
+}

+ 256 - 0
app/admin/controller/Articleclass.php

@@ -0,0 +1,256 @@
+<?php
+
+namespace app\admin\controller;
+use think\facade\View;
+use think\facade\Lang;
+
+/**
+ * ============================================================================
+ * DSMall多用户商城
+ * ============================================================================
+ * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
+ * 网站地址: http://www.csdeshang.com
+ * ----------------------------------------------------------------------------
+ * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
+ * 不允许对程序代码以任何形式任何目的的再发布。
+ * ============================================================================
+ * 控制器
+ */
+class Articleclass extends AdminControl {
+
+    public function initialize() {
+        parent::initialize();
+        Lang::load(base_path() . 'admin/lang/'.config('lang.default_lang').'/articleclass.lang.php');
+    }
+
+    /**
+     * 文章管理
+     */
+    public function index() {
+        $articleclass_model = model('articleclass');
+        /**
+         * 父ID
+         */
+        $parent_id = input('param.ac_parent_id') ? intval(input('param.ac_parent_id')) : 0;
+        /**
+         * 列表
+         */
+        $tmp_list = $articleclass_model->getTreeClassList(2);
+        $class_list = array();
+        if (is_array($tmp_list)) {
+            foreach ($tmp_list as $k => $v) {
+                if ($v['ac_parent_id'] == $parent_id) {
+                    /**
+                     * 判断是否有子类
+                     */
+                    $v['have_child'] = 0;
+                    if (isset($tmp_list[$k + 1]['deep']) && $tmp_list[$k + 1]['deep'] > $v['deep']) {
+                        $v['have_child'] = 1;
+                    }
+                    $class_list[] = $v;
+                }
+            }
+        }
+        if (input('param.ajax') == '1') {
+            /**
+             * 转码
+             */
+            $output = json_encode($class_list);
+            print_r($output);
+            exit;
+        } else {
+            View::assign('class_list', $class_list);
+            $this->setAdminCurItem('index');
+            return View::fetch('article_class_index');
+        }
+    }
+
+    /**
+     * 文章分类 新增
+     */
+    public function article_class_add() {
+        $articleclass_model = model('articleclass');
+        if (request()->isPost()) {
+            /**
+             * 验证
+             */
+            $data = [
+                'ac_name' => input('param.ac_name'),
+                'ac_sort' => input('param.ac_sort')
+            ];
+            $article_validate = ds_validate('article');
+            if (!$article_validate->scene('article_class_add')->check($data)) {
+                $this->error($article_validate->getError());
+            } else {
+
+                $insert_array = array();
+                $insert_array['ac_name'] = trim(input('param.ac_name'));
+                $insert_array['ac_parent_id'] = intval(input('param.ac_parent_id'));
+                $insert_array['ac_sort'] = trim(input('param.ac_sort'));
+
+                $result = $articleclass_model->addArticleclass($insert_array);
+                if ($result) {
+                    $this->log(lang('ds_add') . lang('article_class_index_class') . '[' . input('ac_name') . ']', 1);
+                    dsLayerOpenSuccess(lang('article_class_add_succ'));
+                } else {
+                    $this->error(lang('article_class_add_fail'));
+                }
+            }
+        } else {
+            /**
+             * 父类列表,只取到第三级
+             */
+            $parent_list = $articleclass_model->getTreeClassList(1);
+            if (is_array($parent_list)) {
+                foreach ($parent_list as $k => $v) {
+                    $parent_list[$k]['ac_name'] = str_repeat("&nbsp;", $v['deep'] * 2) . $v['ac_name'];
+                }
+            }
+            View::assign('ac_parent_id', intval(input('param.ac_parent_id')));
+            View::assign('parent_list', $parent_list);
+            return View::fetch('article_class_edit');
+        }
+    }
+
+    /**
+     * 文章分类编辑
+     */
+    public function article_class_edit() {
+        $articleclass_model = model('articleclass');
+        
+        $ac_id = intval(input('param.ac_id'));
+        
+        if (request()->isPost()) {
+            /**
+             * 验证
+             */
+            $data = [
+                'ac_name' => input('param.ac_name'),
+                'ac_sort' => input('param.ac_sort')
+            ];
+            $article_validate = ds_validate('article');
+            if (!$article_validate->scene('article_class_edit')->check($data)) {
+                $this->error($article_validate->getError());
+            } else {
+
+                $update_array = array();
+                $update_array['ac_name'] = trim(input('post.ac_name'));
+                $update_array['ac_sort'] = trim(input('post.ac_sort'));
+
+                $result = $articleclass_model->editArticleclass($update_array,$ac_id);
+                if ($result>=0) {
+                    $this->log(lang('ds_edit') . lang('article_class_index_class') . '[' . input('post.ac_name') . ']', 1);
+                    dsLayerOpenSuccess(lang('ds_common_op_succ'));
+                } else {
+                    $this->error(lang('ds_common_op_fail'));
+                }
+            }
+        } else {
+            $class_array = $articleclass_model->getOneArticleclass($ac_id);
+            if (empty($class_array)) {
+                $this->error(lang('param_error'));
+            }
+
+            View::assign('class_array', $class_array);
+            return View::fetch('article_class_edit');
+        }
+    }
+
+    /**
+     * 删除分类
+     */
+    public function article_class_del() {
+        $articleclass_model = model('articleclass');
+        
+        $ac_id = input('param.ac_id');
+        $ac_id_array = ds_delete_param($ac_id);
+        if ($ac_id_array === FALSE) {
+            ds_json_encode('10001', lang('param_error'));
+        }
+        
+
+            $del_array = $articleclass_model->getChildClass($ac_id_array);
+        if (is_array($del_array)) {
+            foreach ($del_array as $k => $v) {
+                $articleclass_model->delArticleclass($v['ac_id']);
+            }
+        }
+        $this->log(lang('ds_add') . lang('article_class_index_class') . '[ID:' . $ac_id . ']', 1);
+        ds_json_encode(10000, lang('ds_common_del_succ'));
+    }
+
+    /**
+     * ajax操作
+     */
+    public function ajax() {
+        switch (input('param.branch')) {
+            /**
+             * 分类:验证是否有重复的名称
+             */
+            case 'article_class_name':
+                $articleclass_model = model('articleclass');
+                $class_array = $articleclass_model->getOneArticleclass(intval(input('param.id')));
+
+                $condition[]=array('ac_name','=',trim(input('param.value')));
+                $condition[]=array('ac_parent_id','=',$class_array['ac_parent_id']);
+                $condition[]=array('ac_id','<>',intval(input('param.id')));
+                $class_list = $articleclass_model->getArticleclassList($condition);
+                if (empty($class_list)) {
+                    $update_array = array();
+                    $update_array['ac_name'] = trim(input('param.value'));
+                    $articleclass_model->editArticleclass($update_array,input('param.id'));
+                    echo 'true';
+                    exit;
+                } else {
+                    echo 'false';
+                    exit;
+                }
+                break;
+            /**
+             * 分类: 排序 显示 设置
+             */
+            case 'article_class_sort':
+                $articleclass_model = model('articleclass');
+                $update_array = array();
+                $update_array[input('param.column')] = trim(input('param.value'));
+                $result = $articleclass_model->editArticleclass($update_array,intval(input('param.id')));
+                echo 'true';
+                exit;
+                break;
+            /**
+             * 分类:添加、修改操作中 检测类别名称是否有重复
+             */
+            case 'check_class_name':
+                $articleclass_model = model('articleclass');
+                $condition[]=array('ac_name','=',trim(input('param.ac_name')));
+//                $condition[] = array('ac_parent_id','=',intval(input('param.ac_parent_id')));
+                $condition[]=array('ac_id','<>',intval(input('param.ac_id')));
+                $class_list = $articleclass_model->getArticleclassList($condition);
+                if (empty($class_list)) {
+                    echo 'true';
+                    exit;
+                } else {
+                    echo 'false';
+                    exit;
+                }
+                break;
+        }
+    }
+
+    protected function getAdminItemList() {
+        $menu_array = array(
+            array(
+                'name' => 'index',
+                'text' =>lang('ds_manage'),
+                'url' => (string)url('Articleclass/index')
+            ),
+            array(
+                'name' => 'add',
+                'text' => lang('ds_new'),
+                'url' =>"javascript:dsLayerOpen('".(string)url('Articleclass/article_class_add')."','".lang('article_class_add')."')",
+            )
+        );
+        return $menu_array;
+    }
+
+}

+ 552 - 0
app/admin/controller/Bill.php

@@ -0,0 +1,552 @@
+<?php
+
+namespace app\admin\controller;
+use think\facade\View;
+use think\facade\Db;
+use think\facade\Lang;
+use app\common\model\Storemoneylog;
+/**
+ * ============================================================================
+ * DSMall多用户商城
+ * ============================================================================
+ * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
+ * 网站地址: http://www.csdeshang.com
+ * ----------------------------------------------------------------------------
+ * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
+ * 不允许对程序代码以任何形式任何目的的再发布。
+ * ============================================================================
+ * 控制器
+ */
+class Bill extends AdminControl
+{
+    const EXPORT_SIZE = 1000;
+    public function initialize()
+    {
+        parent::initialize();
+        Lang::load(base_path() . 'admin/lang/'.config('lang.default_lang').'/bill.lang.php');
+    }
+
+    /**
+     * 所有月份销量账单
+     *
+     */
+    public function index()
+    {
+        //检查是否需要生成上月及更早结算单的程序不再执行,执行量较大,放到任务计划中触发
+        $condition = array();
+        $query_year = input('get.query_year');
+        if (preg_match('/^\d{4}$/', $query_year, $match)) {
+            $condition[]=array('os_month','like',$query_year.'%');
+        }
+        $bill_model = model('bill');
+        $bill_list = $bill_model->getOrderstatisList($condition, '*', 12, 'os_month desc');
+        View::assign('bill_list', $bill_list);
+        View::assign('show_page', $bill_model->page_info->render());
+
+        View::assign('filtered', $condition ? 1 : 0); //是否有查询条件
+
+        $this->setAdminCurItem('index');
+        return View::fetch('index');
+    }
+
+    /**
+     * 某月所有店铺销量账单
+     *
+     */
+    public function show_statis()
+    {
+
+        $bill_model = model('bill');
+        $condition = array();
+
+        $bill_state = input('get.bill_state');
+        if (is_numeric($bill_state)) {
+            $condition[] = array('ob_state','=',intval($bill_state));
+        }
+        $query_store = input('get.query_store');
+        if (preg_match('/^\d{1,8}$/', $query_store)) {
+            $condition[] = array('ob_store_id','=',$query_store);
+        } elseif ($query_store != '') {
+            $condition[] = array('ob_store_name','=',$query_store);
+        }
+        $os_month = input('get.os_month');
+        if($os_month){
+            $condition[]=array('ob_startdate','>=',strtotime($os_month.'01 0:0:0'));
+            $condition[]=array('ob_enddate','<',strtotime($os_month.'01 23:59:59 +1 month -1 day'));
+        }
+        $bill_list = $bill_model->getOrderbillList($condition, '*', 30, 'ob_no desc');
+        View::assign('bill_list', $bill_list);
+        View::assign('show_page', $bill_model->page_info->render());
+
+        $this->setAdminCurItem('show_statis');
+        return View::fetch('show_statis');
+    }
+
+    /**
+     * 某店铺某月订单列表
+     *
+     */
+    public function show_bill()
+    {
+        $ob_no = input('param.ob_no');
+        if (!$ob_no) {
+            $this->error(lang('param_error'));
+        }
+        $bill_model = model('bill');
+        $bill_info = $bill_model->getOrderbillInfo(array('ob_no' => $ob_no));
+        if (!$bill_info) {
+            $this->error(lang('param_error'));
+        }
+
+        $order_condition = array();
+        $order_condition[] = array('ob_no','=',$ob_no);
+        $order_condition[] = array('order_state','=',ORDER_STATE_SUCCESS);
+        $order_condition[] = array('store_id','=',$bill_info['ob_store_id']);
+
+        $query_start_date = input('get.query_start_date');
+        $query_end_date = input('get.query_end_date');
+        $if_start_date = preg_match('/^20\d{2}-\d{2}-\d{2}$/', $query_start_date);
+        $if_end_date = preg_match('/^20\d{2}-\d{2}-\d{2}$/', $query_end_date);
+        $start_unixtime = $if_start_date ? strtotime($query_start_date) : null;
+        $end_unixtime = $if_end_date ? strtotime($query_end_date) : null;
+
+        $end_unixtime = $if_end_date ? $end_unixtime + 86400 - 1 : null;
+        if ($if_start_date || $if_end_date) {
+            if($if_start_date){
+                $order_condition[]=array('finnshed_time','>=', $start_unixtime);
+            }
+            if($if_end_date){
+                $order_condition[]=array('finnshed_time','<=', $end_unixtime);
+            }
+        }
+
+        $query_type = input('param.query_type');
+        if ($query_type == 'cost') {
+
+            //店铺费用
+            $storecost_model = model('storecost');
+            $cost_condition = array();
+            $cost_condition[] = array('storecost_store_id','=',$bill_info['ob_store_id']);
+            $cost_condition[] = array('storecost_time','between',[$bill_info['ob_startdate'],$bill_info['ob_enddate']]);
+            $store_cost_list = $storecost_model->getStorecostList($cost_condition, 20);
+            //取得店铺名字
+            $store_info = model('store')->getStoreInfoByID($bill_info['ob_store_id']);
+            View::assign('cost_list', $store_cost_list);
+            View::assign('store_info', $store_info);
+            View::assign('show_page', $storecost_model->page_info->render());
+            $sub_tpl_name = 'show_cost_list';
+        }elseif ($query_type == 'vrorder') {
+
+            //店铺费用
+            $vrorder_model = model('vrorder');
+            $order_list = $vrorder_model->getVrorderList($order_condition, 20,'(ROUND(order_amount*commis_rate/100,2)) AS commis_amount,(ROUND(refund_amount*commis_rate/100,2)) AS return_commis_amount,order_amount,refund_amount,order_sn,buyer_name,add_time,finnshed_time,order_id');
+            foreach($order_list as $key => $val){
+                if(!$val['order_id']){
+                    $order_list=array();
+                    break;
+                }
+                //分销佣金
+                $inviter_info=Db::name('orderinviter')->where(array('orderinviter_order_id' => $key, 'orderinviter_valid' => 1, 'orderinviter_order_type' => 1))->field('SUM(orderinviter_money) AS ob_inviter_totals')->find();
+                $order_list[$key]['inviter_amount']= ds_price_format($inviter_info['ob_inviter_totals']);
+            }
+            View::assign('order_list', $order_list);
+            View::assign('show_page', $vrorder_model->page_info->render());
+            $sub_tpl_name = 'show_vrorder_list';
+        } else {
+
+            //订单列表
+            $order_model = model('order');
+            $order_list = $order_model->getOrderList($order_condition, 20);
+
+            //然后取订单商品佣金
+            $order_id_array = array();
+            if (is_array($order_list)) {
+                foreach ($order_list as $order_info) {
+                    $order_id_array[] = $order_info['order_id'];
+                }
+            }
+            $order_goods_condition = array();
+            $order_goods_condition[] = array('order_id','in',$order_id_array);
+            $field = 'SUM(ROUND(goods_pay_price*commis_rate/100,2)) as commis_amount,order_id';
+            $commis_list = $order_model->getOrdergoodsList($order_goods_condition, $field, 0, null, '', 'order_id', 'order_id');
+            foreach($commis_list as $key => $val){
+                $return_commis_amount=0;
+                $refund_info=Db::name('refundreturn')->alias('refundreturn')->join('ordergoods ordergoods', 'refundreturn.order_goods_id = ordergoods.rec_id')->where(array(array('refundreturn.order_id' ,'=', $key), array('refundreturn.refund_state' ,'=', 3), array('refundreturn.order_goods_id','>', 0)))->field('SUM(ROUND(refundreturn.refund_amount*ordergoods.commis_rate/100,2)) AS ob_commis_return_totals')->find();
+                $return_commis_amount=$refund_info['ob_commis_return_totals'];
+                $commis_list[$key]['return_commis_amount']=$return_commis_amount;
+                //分销佣金
+                $inviter_info=Db::name('orderinviter')->where(array('orderinviter_order_id' => $key, 'orderinviter_valid' => 1, 'orderinviter_order_type' => 0))->field('SUM(orderinviter_money) AS ob_inviter_totals')->find();
+                $commis_list[$key]['inviter_amount']=$inviter_info['ob_inviter_totals'];
+                //平台代金券
+                $mallvoucher_info=Db::name('ordercommon')->where(array('order_id' => $key))->field('mallvoucher_price')->find();
+                $commis_list[$key]['mall_voucher_totals']=number_format($mallvoucher_info['mallvoucher_price'], 2);
+            }
+            View::assign('commis_list', $commis_list);
+            View::assign('order_list', $order_list);
+            View::assign('show_page', $order_model->page_info->render());
+            $sub_tpl_name = 'show_order_list';
+        }
+        View::assign('bill_info', $bill_info);
+        return View::fetch($sub_tpl_name);
+    }
+
+    public function bill_check() {
+        $ob_no = input('param.ob_no');
+        if (!$ob_no) {
+            $this->error(lang('param_error'));
+        }
+        $bill_model = model('bill');
+        $condition = array();
+        $condition[] = array('ob_no','=',$ob_no);
+        $condition[] = array('ob_state','=',BILL_STATE_STORE_COFIRM);
+        $bill_info = $bill_model->getOrderbillInfo($condition);
+        if (!$bill_info) {
+            $this->error(lang('bill_is_not_exist'));
+        }
+        if (request()->isPost()) {
+            
+            Db::startTrans();
+            try {
+                if($bill_info['ob_result_totals']!=0){
+                    $storemoneylog_model=model('storemoneylog');
+                    $data=array(
+                        'store_id'=>$bill_info['ob_store_id'],
+                        'storemoneylog_type'=>Storemoneylog::TYPE_BILL,
+                        'storemoneylog_state'=>Storemoneylog::STATE_VALID,
+                        'storemoneylog_add_time'=>TIMESTAMP,
+                        'store_avaliable_money'=>$bill_info['ob_result_totals'],//如果是欠账则从店铺余额里扣除,否则增加
+                        'storemoneylog_desc'=>$ob_no.lang('bill_phase_numbers').lang('bill_state_success'),
+                    );
+
+                    $storemoneylog_model->changeStoremoney($data);
+                }
+                $update = $bill_model->editOrderbill(array('ob_state' => BILL_STATE_SUCCESS,'ob_admin_content'=>input('post.ob_admin_content')), $condition);
+                if (!$update) {
+                    throw new \think\Exception(lang('bill_audit_fail'), 10006);
+                }
+            } catch (\Exception $e) {
+                Db::rollback();
+                $this->log(lang('bill_audit_bill') . $ob_no, 0);
+                $this->error($e->getMessage());
+            }
+            Db::commit();
+            $this->log(lang('bill_audit_bill') . $ob_no, 1);
+            $this->success(lang('bill_audit_succ'),(string)url('Bill/show_bill',['ob_no'=>$ob_no]));
+        } else {
+            return View::fetch('bill_check');
+        }
+    }
+
+    /**
+     * 账单付款
+     *
+     */
+    public function bill_pay()
+    {
+        $ob_no = input('param.ob_no');
+        if (!preg_match('/^20\d{5,12}$/', $ob_no)) {
+            $this->error(lang('param_error'));
+        }
+        $bill_model = model('bill');
+        $condition = array();
+        $condition[] = array('ob_no','=',$ob_no);
+        $condition[] = array('ob_state','=',BILL_STATE_SYSTEM_CHECK);
+        $bill_info = $bill_model->getOrderbillInfo($condition);
+        if (!$bill_info) {
+            $this->error(lang('param_error'));
+        }
+        if (request()->isPost()) {
+            if (!preg_match('/^20\d{2}-\d{2}-\d{2}$/', input('param.pay_date'))) {
+                $this->error(lang('param_error'));
+            }
+            $input = array();
+            $input['ob_pay_content'] = input('pay_content');
+            $input['ob_paydate'] = strtotime(input('param.pay_date'));
+            $input['ob_state'] = BILL_STATE_SUCCESS;
+            $update = $bill_model->editOrderbill($input, $condition);
+            if ($update) {
+                $storecost_model = model('storecost');
+                $cost_condition = array();
+                $cost_condition[] = array('storecost_store_id','=',$bill_info['ob_store_id']);
+                $cost_condition[] = array('storecost_state','=',0);
+                $cost_condition[] = array('storecost_time','between', "{$bill_info['ob_startdate']},{$bill_info['ob_enddate']}");
+                $storecost_model->editStorecost(array('storecost_state' => 1), $cost_condition);
+
+                // 发送店铺消息
+                $param = array();
+                $param['code'] = 'store_bill_gathering';
+                $param['store_id'] = $bill_info['ob_store_id'];
+                $param['ali_param'] = array(
+                    'bill_no' => $bill_info['ob_no']
+                );
+                $param['ten_param'] = array(
+                    $bill_info['ob_no']
+                );
+                $param['param'] = $param['ali_param'];
+                //微信模板消息
+                $param['weixin_param'] = array(
+                    'url' => config('ds_config.h5_store_site_url').'/pages/seller/bill/BillList',
+                    'data'=>array(
+                        "keyword1" => array(
+                            "value" => date('Y-m-d', $bill_info['ob_startdate']).'~'.date('Y-m-d', $bill_info['ob_enddate']),
+                            "color" => "#333"
+                        ),
+                        "keyword2" => array(
+                            "value" => date('Y-m-d', $bill_info['ob_createdate']),
+                            "color" => "#333"
+                        ),
+                        "keyword3" => array(
+                            "value" => $bill_info['ob_result_totals'],
+                            "color" => "#333"
+                        )
+                    ),
+                );
+                model('cron')->addCron(array('cron_exetime'=>TIMESTAMP,'cron_type'=>'sendStoremsg','cron_value'=>serialize($param)));
+
+                $this->log(lang('bill_payment_audit_fail') . $ob_no, 1);
+                $this->success(lang('ds_common_save_succ'), 'bill/show_statis?os_month=' . $bill_info['os_month']);
+            } else {
+                $this->log(lang('bill_payment_audit_fail') . $ob_no, 1);
+                $this->error(lang('ds_common_save_fail'));
+            }
+        } else {
+            $this->setAdminCurItem('bill_pay');
+            return View::fetch('bill_pay');
+        }
+    }
+
+    /**
+     * 打印结算单
+     *
+     */
+    public function bill_print()
+    {
+        $ob_no = input('param.ob_no');
+        if (!$ob_no) {
+            $this->error(lang('param_error'));
+        }
+        $bill_model = model('bill');
+        $condition = array();
+        $condition[] = array('ob_no','=',$ob_no);
+        $condition[] = array('ob_state','=',BILL_STATE_SUCCESS);
+        $bill_info = $bill_model->getOrderbillInfo($condition);
+        if (!$bill_info) {
+            $this->error(lang('param_error'));
+        }
+
+        View::assign('bill_info', $bill_info);
+
+        return View::fetch('bill_print');
+    }
+
+    /**
+     * 导出 结算管理
+     *
+     */
+    public function export_js_step1() {
+        $bill_model = model('bill');
+        $condition = array();
+        $query_year = input('get.query_year');
+        if (preg_match('/^\d{4}$/', $query_year, $match)) {
+            $condition[]=array('os_month','like',$query_year.'%');
+        }
+        if (!is_numeric(input('param.page'))) {
+            $count = $bill_model->getOrderstatisCount($condition);
+            $export_list = array();
+            if ($count > self::EXPORT_SIZE) { //显示下载链接
+                $page = ceil($count / self::EXPORT_SIZE);
+                for ($i = 1; $i <= $page; $i++) {
+                    $limit1 = ($i - 1) * self::EXPORT_SIZE + 1;
+                    $limit2 = $i * self::EXPORT_SIZE > $count ? $count : $i * self::EXPORT_SIZE;
+                    $export_list[$i] = $limit1 . ' ~ ' . $limit2;
+                }
+                View::assign('export_list', $export_list);
+                return View::fetch('/public/excel');
+            } else { //如果数量小,直接下载
+                $data = $bill_model->getOrderstatisList($condition, '*', '', '', self::EXPORT_SIZE);
+                $this->createJsExcel($data);
+            }
+        } else { //下载
+            $limit1 = (input('param.page') - 1) * self::EXPORT_SIZE;
+            $limit2 = self::EXPORT_SIZE;
+            $data = $bill_model->getOrderstatisList($condition, '*', $limit2);
+            $this->createJsExcel($data);
+        }
+    }
+
+    /**
+     * 结算管理 生成excel
+     *
+     * @param array $data
+     */
+    private function createJsExcel($data = array()) {
+        Lang::load(base_path() .'admin/lang/'.config('lang.default_lang').'/export.lang.php');
+        $excel_obj = new \excel\Excel();
+        $excel_data = array();
+        //设置样式
+        $excel_obj->setStyle(array('id' => 's_title', 'Font' => array('FontName' => '宋体', 'Size' => '12', 'Bold' => '1')));
+        //header
+        $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_js_order_number_bill'));
+        $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_js_order_price_from'));
+        $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_js_order_total_transport'));
+        $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_js_os_commis_totals'));
+        $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_js_os_order_returntotals'));
+        $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_js_os_commis_returntotals'));
+        $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_js_os_store_costtotals'));
+        $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_js_ob_inviter_totals'));
+        $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_js_os_result_totals'));
+        $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_js_os_createdate'));
+        //data
+        foreach ((array) $data as $k => $v) {
+            $tmp = array();
+            $tmp[] = array('data' => substr($v['os_month'],0,4).'-'.substr($v['os_month'],4));
+            $tmp[] = array('format' => 'Number', 'data' => ds_price_format($v['os_order_totals']));
+            $tmp[] = array('format' => 'Number', 'data' => ds_price_format($v['os_shipping_totals']));
+            $tmp[] = array('format' => 'Number', 'data' => ds_price_format($v['os_commis_totals']));
+            $tmp[] = array('format' => 'Number', 'data' => ds_price_format($v['os_order_returntotals']));
+            $tmp[] = array('format' => 'Number', 'data' => ds_price_format($v['os_commis_returntotals']));
+            $tmp[] = array('format' => 'Number', 'data' => ds_price_format($v['os_store_costtotals']));
+            $tmp[] = array('format' => 'Number', 'data' => ds_price_format($v['os_inviter_totals']));
+            $tmp[] = array('format' => 'Number', 'data' => ds_price_format($v['os_result_totals']));
+            $tmp[] = array('data' => date('Y-m-d H:i:s', $v['os_createdate']));
+
+            $excel_data[] = $tmp;
+        }
+        $excel_data = $excel_obj->charset($excel_data, CHARSET);
+        $excel_obj->addArray($excel_data);
+        $excel_obj->addWorksheet($excel_obj->charset(lang('exp_js_list'), CHARSET));
+        $excel_obj->generateXML($excel_obj->charset(lang('exp_js_list'), CHARSET) . input('param.page') . '-' . date('Y-m-d-H', TIMESTAMP));
+    }
+
+    /**
+     * 商家账单列表 管理
+     *
+     */
+    public function export_zd_step1() {
+
+        $bill_model = model('bill');
+        $condition = array();
+
+        $bill_state = input('get.bill_state');
+        if (is_numeric($bill_state)) {
+            $condition[] = array('ob_state','=',intval($bill_state));
+        }
+        $query_store = input('get.query_store');
+        if (preg_match('/^\d{1,8}$/', $query_store)) {
+            $condition[] = array('ob_store_id','=',$query_store);
+        } elseif ($query_store != '') {
+            $condition[] = array('ob_store_name','=',$query_store);
+        }
+        $os_month = input('get.os_month');
+        if($os_month){
+            $condition[]=array('ob_startdate','>=',strtotime($os_month.'01 0:0:0'));
+            $condition[]=array('ob_enddate','<',strtotime($os_month.'01 23:59:59 +1 month -1 day'));
+        }
+        if (!is_numeric(input('param.page'))) {
+            $count = $bill_model->getOrderbillCount($condition);
+            $export_list = array();
+            if ($count > self::EXPORT_SIZE) { //显示下载链接
+                $page = ceil($count / self::EXPORT_SIZE);
+                for ($i = 1; $i <= $page; $i++) {
+                    $limit1 = ($i - 1) * self::EXPORT_SIZE + 1;
+                    $limit2 = $i * self::EXPORT_SIZE > $count ? $count : $i * self::EXPORT_SIZE;
+                    $export_list[$i] = $limit1 . ' ~ ' . $limit2;
+                }
+                View::assign('export_list', $export_list);
+                return View::fetch('/public/excel');
+            } else { //如果数量小,直接下载
+                $data = $bill_model->getOrderbillList($condition, '*', '', '', self::EXPORT_SIZE);
+                $this->createZdExcel($data);
+            }
+        } else { //下载
+            $limit1 = (input('param.page') - 1) * self::EXPORT_SIZE;
+            $limit2 = self::EXPORT_SIZE;
+            $data = $bill_model->getOrderbillList($condition, '*', $limit2);
+            $this->createZdExcel($data);
+        }
+    }
+
+    /**
+     * 商家账单列表 生成excel
+     *
+     * @param array $data
+     */
+    private function createZdExcel($data = array()) {
+        Lang::load(base_path() .'admin/lang/'.config('lang.default_lang').'/export.lang.php');
+        $excel_obj = new \excel\Excel();
+        $excel_data = array();
+        //设置样式
+        $excel_obj->setStyle(array('id' => 's_title', 'Font' => array('FontName' => '宋体', 'Size' => '12', 'Bold' => '1')));
+        //header
+        $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_zd_bill_ob_no'));
+        $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_zd_bill_os_startdate'));
+        $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_zd_bill_os_enddate'));
+        $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_zd_order_price_from'));
+        $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_zd_order_total_transport'));
+        $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_zd_bill_print_commision'));
+        $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_zd_bill_ob_order_return_totals'));
+        $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_zd_bill_os_commis_returntotals'));
+        $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_zd_ob_inviter_totals'));
+        $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_zd_bill_ob_vr_order_totals'));
+        $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_zd_bill_ob_vr_order_return_totals'));
+        $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_zd_bill_ob_vr_commis_totals'));
+        $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_zd_bill_ob_vr_commis_return_totals'));
+        $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_zd_bill_ob_vr_inviter_totals'));
+        $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_zd_bill_os_store_costtotals'));
+        $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_zd_bill_os_result_totals'));
+        $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_zd_bill_out_date'));
+        $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_zd_bill_state'));
+        $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('ds_store_name'));
+        //data
+        foreach ((array) $data as $k => $v) {
+            $tmp = array();
+            $tmp[] = array('data' => $v['ob_no']);
+            $tmp[] = array('data' => date('Y-m-d H:i:s', $v['ob_startdate']));
+            $tmp[] = array('data' => date('Y-m-d H:i:s', $v['ob_enddate']));
+            $tmp[] = array('format' => 'Number', 'data' => ds_price_format($v['ob_order_totals']));
+            $tmp[] = array('format' => 'Number', 'data' => ds_price_format($v['ob_shipping_totals']));
+            $tmp[] = array('format' => 'Number', 'data' => ds_price_format($v['ob_commis_totals']));
+            $tmp[] = array('format' => 'Number', 'data' => ds_price_format($v['ob_order_return_totals']));
+            $tmp[] = array('format' => 'Number', 'data' => ds_price_format($v['ob_commis_return_totals']));
+            $tmp[] = array('format' => 'Number', 'data' => ds_price_format($v['ob_inviter_totals']));
+            $tmp[] = array('format' => 'Number', 'data' => ds_price_format($v['ob_vr_order_totals']));
+            $tmp[] = array('format' => 'Number', 'data' => ds_price_format($v['ob_vr_order_return_totals']));
+            $tmp[] = array('format' => 'Number', 'data' => ds_price_format($v['ob_vr_commis_totals']));
+            $tmp[] = array('format' => 'Number', 'data' => ds_price_format($v['ob_vr_commis_return_totals']));
+            $tmp[] = array('format' => 'Number', 'data' => ds_price_format($v['ob_vr_inviter_totals']));
+            $tmp[] = array('format' => 'Number', 'data' => ds_price_format($v['ob_store_cost_totals']));
+            $tmp[] = array('format' => 'Number', 'data' => ds_price_format($v['ob_result_totals']));
+            $tmp[] = array('data' => date('Y-m-d H:i:s', $v['ob_createdate']));
+            $tmp[] = array('data' => get_bill_state($v['ob_state']));
+            $tmp[] = array('data' => $v['ob_store_name']);
+            $excel_data[] = $tmp;
+        }
+        $excel_data = $excel_obj->charset($excel_data, CHARSET);
+        $excel_obj->addArray($excel_data);
+        $excel_obj->addWorksheet($excel_obj->charset(lang('exp_zd_list'), CHARSET));
+        $excel_obj->generateXML($excel_obj->charset(lang('exp_zd_list'), CHARSET) . input('param.page') . '-' . date('Y-m-d-H', TIMESTAMP));
+    }
+
+    /**
+     * 获取卖家栏目列表,针对控制器下的栏目
+     */
+    protected function getAdminItemList()
+    {
+        $menu_array = array(
+            array(
+                'name' => 'index',
+                'text' => lang('ds_bill'),
+                'url' => (string)url('Bill/index')
+            ),
+        );
+            $title = !empty(input('param.os_month')) ? input('param.os_month') . lang('bill_period') : '';
+            $menu_array[] = array(
+                'name' => 'show_statis',
+                'text' => $title . lang('bill_billing_list'),
+                'url' => !empty($title) ? (string)url('Bill/show_statis', ['os_month' => input('param.os_month')]) : (string)url('Bill/show_statis'),
+            );
+        return $menu_array;
+    }
+}
+
+?>

+ 248 - 0
app/admin/controller/Bonus.php

@@ -0,0 +1,248 @@
+<?php
+
+namespace app\admin\controller;
+use think\facade\View;
+use think\facade\Db;
+use think\facade\Lang;
+
+/**
+ * ============================================================================
+ * DSMall多用户商城
+ * ============================================================================
+ * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
+ * 网站地址: http://www.csdeshang.com
+ * ----------------------------------------------------------------------------
+ * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
+ * 不允许对程序代码以任何形式任何目的的再发布。
+ * ============================================================================
+ * 平台红包 控制器
+ */
+class Bonus extends AdminControl {
+
+    public function initialize() {
+        parent::initialize();
+        Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/bonus.lang.php');
+    }
+
+    public function index() {
+        $condition = array();
+        $bonus_name = input('param.bonus_name');
+        if (!empty($bonus_name)) {
+            $condition[]=array('bonus_name','like', '%' . $bonus_name . '%');
+        }
+        //红包是否有效
+        $bonus_state = intval(input('get.bonus_state'));
+        if ($bonus_state) {
+            $condition[]=array('bonus_state','=',$bonus_state);
+        }
+        //红包类型
+        $bonus_type = intval(input('get.bonus_type'));
+        if ($bonus_type) {
+            $condition[]=array('bonus_type','=',$bonus_type);
+        }
+        $bonus_model = model('bonus');
+        $bonus_list = $bonus_model->getBonusList($condition, 10);
+
+        //红包类型
+        View::assign('bonus_type_list', $bonus_model->bonus_type_list());
+        //红包状态
+        View::assign('bonus_state_list', $bonus_model->bonus_state_list());
+
+        View::assign('bonus_list', $bonus_list);
+        View::assign('show_page', $bonus_model->page_info->render());
+        $this->setAdminCurItem('index');
+        return View::fetch();
+    }
+
+    /**
+     * 添加吸粉红包
+     */
+    public function add() {
+        $bonus_model = model('bonus');
+        if (!request()->isPost()) {
+            $bonus = array(
+                'bonus_type' => 1,
+                'bonus_begintime' => TIMESTAMP,
+                'bonus_endtime' => TIMESTAMP+3600*24*7,
+            );
+            //红包类型
+            View::assign('bonus_type_list', $bonus_model->bonus_type_list());
+            View::assign('bonus', $bonus);
+            return View::fetch('form');
+        } else {
+            $bonus_totalprice = floatval(input('param.bonus_totalprice'));
+            $bonus_pricetype = intval(input('param.bonus_pricetype'));
+            $bonus_fixedprice = floatval(input('param.bonus_fixedprice'));
+            $bonus_randomprice_start = floatval(input('param.bonus_randomprice_start'));
+            $bonus_randomprice_end = floatval(input('param.bonus_randomprice_end'));
+
+            //计算写入吸粉红包领取记录表
+            $data_bonusreceive = array(); //红包领取记录
+            if ($bonus_pricetype == 1) {
+                //固定金额
+                if ($bonus_fixedprice == 0 || $bonus_fixedprice > $bonus_totalprice) {
+                    $this->error(lang('bonus_fixedprice_error'));
+                }
+                if (($bonus_totalprice*100) % ($bonus_fixedprice*100) != 0) {
+                    $this->error(lang('bonus_fixedprice_error'));
+                }
+                //生成红包领取记录-固定金额
+                for ($i = 0; $i < $bonus_totalprice / $bonus_fixedprice; $i++) {
+                    $data_bonusreceive[] = array(
+                        'bonusreceive_price' => $bonus_fixedprice
+                    );
+                }
+                $bonus_randomprice_start = 0;
+                $bonus_randomprice_end = 0;
+            } else {
+                if ($bonus_randomprice_start == 0 || $bonus_randomprice_start > $bonus_totalprice || $bonus_randomprice_end > $bonus_totalprice || $bonus_randomprice_start >= $bonus_randomprice_end) {
+                    $this->error(lang('bonus_randomprice_error'));
+                }
+                //生成红包领取记录-随机金额
+                $surplus_price = $bonus_totalprice; //剩余未计算金额
+                while (true) {
+                    if ($surplus_price <= $bonus_randomprice_end) {
+                        $bonusreceive_price = $surplus_price;
+                    } else {
+                        $bonusreceive_price = rand($bonus_randomprice_start * 100, $bonus_randomprice_end * 100) / 100;
+                    }
+                    $surplus_price -= $bonusreceive_price;
+                    $data_bonusreceive[] = array(
+                        'bonusreceive_price' => $bonusreceive_price
+                    );
+                    if ($surplus_price == 0) {
+                        break;
+                    }
+                }
+                $bonus_fixedprice = 0;
+            }
+
+            $data_bonus = array(
+                'bonus_type' => input('param.bonus_type'),
+                'bonus_name' => input('param.bonus_name'),
+                'bonus_remark' => input('param.bonus_remark'),
+                'bonus_blessing' => input('param.bonus_blessing'),
+                'bonus_begintime' => strtotime(input('param.bonus_begintime')),
+                'bonus_endtime' => strtotime(input('param.bonus_endtime')),
+                'bonus_state' => 1,
+                'bonus_totalprice' => $bonus_totalprice,
+                'bonus_pricetype' => $bonus_pricetype,
+                'bonus_fixedprice' => $bonus_fixedprice,
+                'bonus_randomprice_start' => $bonus_randomprice_start,
+                'bonus_randomprice_end' => $bonus_randomprice_end,
+            );
+            $bonus_id = $bonus_model->addBonus($data_bonus);
+
+            if ($bonus_id > 0) {
+                foreach ($data_bonusreceive as $key => $bonusreceive) {
+                    $data_bonusreceive[$key]['bonus_id'] = $bonus_id;
+                }
+                Db::name('bonusreceive')->insertAll($data_bonusreceive);
+                $this->log(lang('ds_add') . lang('ds_bonus') . '[ID' . $bonus_id . ']', 1);
+                dsLayerOpenSuccess(lang('ds_common_save_succ'));
+            } else {
+                $this->error(lang('ds_common_save_fail'));
+            }
+        }
+    }
+
+    /**
+     * 编辑吸粉红包  不可以对金额以及红包类型进行编辑。
+     */
+    public function edit() {
+        $bonus_id = intval(input('param.bonus_id'));
+        if ($bonus_id < 0) {
+            ds_json_encode(10000, lang('param_error'));
+        }
+        $bonus_model = model('bonus');
+        $condition = array();
+        $condition[] = array('bonus_id','=',$bonus_id);
+        if (!request()->isPost()) {
+            $bonus = $bonus_model->getOneBonus($condition);
+            View::assign('bonus', $bonus);
+            //红包类型
+            View::assign('bonus_type_list', $bonus_model->bonus_type_list());
+            return View::fetch('form');
+        } else {
+            $data_bonus = array(
+                'bonus_name' => input('param.bonus_name'),
+                'bonus_remark' => input('param.bonus_remark'),
+                'bonus_blessing' => input('param.bonus_blessing'),
+                'bonus_begintime' => strtotime(input('param.bonus_begintime')),
+                'bonus_endtime' => strtotime(input('param.bonus_endtime')),
+            );
+            $bonus_model->editBonus($condition, $data_bonus);
+            $this->log(lang('ds_edit') . lang('ds_bonus') . '[ID' . $bonus_id . ']', 1);
+            dsLayerOpenSuccess(lang('ds_common_save_succ'));
+        }
+    }
+
+    /**
+     * 设置红包失效    1正在进行  2过期  3失效
+     */
+    public function invalid() {
+        $bonus_id = intval(input('param.bonus_id'));
+        if ($bonus_id < 0) {
+            ds_json_encode(10000, lang('param_error'));
+        }
+        $bonus_model = model('bonus');
+        $condition = array();
+        $condition[] = array('bonus_id','=',$bonus_id);
+        $data['bonus_state'] = 3;
+        $bonus_model->editBonus($condition, $data);
+        $this->log(lang('ds_edit') . lang('ds_bonus') . '[ID' . $bonus_id . ']', 1);
+        ds_json_encode(10000, lang('ds_common_op_succ'));
+    }
+
+    /**
+     * 领取列表
+     */
+    public function receive() {
+        $bonus_id = intval(input('param.bonus_id'));
+        if ($bonus_id < 0) {
+            $this->error(lang('param_error'));
+        }
+        $condition = array();
+        $condition[] = array('bonus_id','=',$bonus_id);
+        $bonus_model = model('bonus');
+        $bonusreceive_list = $bonus_model->getBonusreceiveList($condition, 10);
+        View::assign('bonusreceive_list', $bonusreceive_list);
+        View::assign('show_page', $bonus_model->page_info->render());
+        return View::fetch();
+    }
+    
+    //链接信息
+    public function link()
+    {
+        $bonus_id = intval(input('param.bonus_id'));
+        if ($bonus_id < 0) {
+            $this->error(lang('param_error'));
+        }
+        $condition = array();
+        $condition[] = array('bonus_id','=',$bonus_id);
+        $bonus_model = model('bonus');
+        $bonus = $bonus_model->getOneBonus($condition);
+        View::assign('bonus', $bonus);
+        $bonus_url = config('ds_config.h5_site_url')."/pages/home/bonus/Detail?bonus_id=".$bonus['bonus_id'];
+        View::assign('bonus_url', $bonus_url);
+        return View::fetch();
+    }
+    
+
+    protected function getAdminItemList() {
+        $menu_array = array(
+            array(
+                'name' => 'index',
+                'text' => lang('ds_manage'),
+                'url' => (string)url('Bonus/index')
+            ),
+            array(
+                'name' => 'add',
+                'text' => lang('ds_add'),
+                'url' => "javascript:dsLayerOpen('" . (string)url('Bonus/add') . "','".lang('ds_add')."')"
+            ),
+        );
+        return $menu_array;
+    }
+
+}

+ 457 - 0
app/admin/controller/Brand.php

@@ -0,0 +1,457 @@
+<?php
+
+namespace app\admin\controller;
+
+use think\facade\View;
+use think\facade\Lang;
+
+/**
+ * ============================================================================
+ * DSMall多用户商城
+ * ============================================================================
+ * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
+ * 网站地址: http://www.csdeshang.com
+ * ----------------------------------------------------------------------------
+ * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
+ * 不允许对程序代码以任何形式任何目的的再发布。
+ * ============================================================================
+ * 控制器
+ */
+class Brand extends AdminControl {
+
+    const EXPORT_SIZE = 1000;
+
+    public function initialize() {
+        parent::initialize();
+        Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/brand.lang.php');
+    }
+
+    /**
+     * 品牌列表
+     */
+    public function index() {
+        $brand_model = model('brand');
+        /**
+         * 检索条件
+         */
+        if (!empty(input('param.search_brand_name'))) {
+            $condition[] = array('brand_name', 'like', "%" . input('param.search_brand_name') . "%");
+        }
+        if (!empty(input('param.search_brand_class'))) {
+            $condition[] = array('brand_class', 'like', "%" . input('param.search_brand_class') . "%");
+        }
+        $condition[] = array('brand_apply', '=', '1');
+        $brand_list = $brand_model->getBrandList($condition, "*", 10);
+        View::assign('showpage', $brand_model->page_info->render());
+        View::assign('brand_list', $brand_list);
+        View::assign('search_brand_name', trim(input('param.search_brand_name')));
+        View::assign('search_brand_class', trim(input('param.search_brand_class')));
+        $this->setAdminCurItem('index');
+        return View::fetch();
+    }
+
+    /**
+     * 增加品牌
+     */
+    public function brand_add() {
+
+        $brand_model = model('brand');
+        if (request()->isPost()) {
+            $data = [
+                'brand_name' => input('post.brand_name'), 'brand_initial' => input('post.brand_initial'),
+                'brand_sort' => input('post.brand_sort')
+            ];
+            $brand_validate = ds_validate('brand');
+
+            if (!$brand_validate->scene('brand_add')->check($data)) {
+                $this->error($brand_validate->getError());
+            } else {
+                $insert_array = array();
+                if (!empty($_FILES['_pic']['name'])) {
+                    $res=ds_upload_pic(ATTACH_BRAND,'_pic');
+                    if($res['code']){
+                        $brand_pic=$res['data']['file_name'];
+                    }else{
+                        $this->error($res['msg']);
+                    }
+                }
+                $insert_array['brand_name'] = trim(input('post.brand_name'));
+                $insert_array['brand_initial'] = strtoupper(input('post.brand_initial'));
+                $insert_array['gc_id'] = input('post.class_id');
+                $insert_array['brand_class'] = trim(input('post.brand_class'));
+                if (!empty($brand_pic)) {
+                    $insert_array['brand_pic'] = $brand_pic;
+                }
+                $insert_array['brand_recommend'] = trim(input('post.brand_recommend'));
+                $insert_array['brand_sort'] = intval(input('post.brand_sort'));
+                $insert_array['brand_showtype'] = intval(input('post.brand_showtype')) == 1 ? 1 : 0;
+                $result = $brand_model->addBrand($insert_array);
+                if ($result) {
+                    $this->log(lang('ds_add') . lang('brand_index_brand') . '[' . input('post.brand_name') . ']', 1);
+                    dsLayerOpenSuccess(lang('ds_common_save_succ'));
+                } else {
+                    $this->error(lang('ds_common_save_fail'));
+                }
+            }
+        } else {
+            $brand_array = [
+                'brand_id' => '',
+                'brand_name' => '',
+                'brand_initial' => '',
+                'gc_id' => '',
+                'brand_class' => '',
+                'brand_pic' => '',
+                'brand_showtype' => '0',
+                'brand_recommend' => '1',
+                'brand_sort' => '0',
+            ];
+            View::assign('brand_array', $brand_array);
+
+            // 一级商品分类
+            $gc_list = model('goodsclass')->getGoodsclassListByParentId(0);
+            View::assign('gc_list', $gc_list);
+            return View::fetch('form');
+        }
+    }
+
+    /**
+     * 品牌编辑
+     */
+    public function brand_edit() {
+        $brand_model = model('brand');
+
+        if (request()->isPost()) {
+            $data = [
+                'brand_name' => input('post.brand_name'), 'brand_initial' => input('post.brand_initial'),
+                'brand_sort' => input('post.brand_sort')
+            ];
+            $brand_validate = ds_validate('brand');
+            if (!$brand_validate->scene('brand_edit')->check($data)) {
+                $this->error($brand_validate->getError());
+            } else {
+                if (!empty($_FILES['_pic']['name'])) {
+                    $res=ds_upload_pic(ATTACH_BRAND,'_pic');
+                    if($res['code']){
+                        $brand_pic=$res['data']['file_name'];
+                    }else{
+                        $this->error($res['msg']);
+                    }
+                }
+                $brand_info = $brand_model->getBrandInfo(array('brand_id' => intval(input('post.brand_id'))));
+                $condition = array();
+                $condition[] = array('brand_id', '=', intval(input('post.brand_id')));
+                $update_array = array();
+                $update_array['brand_name'] = trim(input('post.brand_name'));
+                $update_array['brand_initial'] = strtoupper(input('post.brand_initial'));
+                $update_array['gc_id'] = input('post.class_id');
+                $update_array['brand_class'] = trim(input('post.brand_class'));
+                if (!empty($brand_pic)) {
+                    $update_array['brand_pic'] = $brand_pic;
+                }
+                $update_array['brand_recommend'] = intval(input('post.brand_recommend'));
+                $update_array['brand_sort'] = intval(input('post.brand_sort'));
+                $update_array['brand_showtype'] = intval(input('post.brand_showtype')) == 1 ? 1 : 0;
+                $result = $brand_model->editBrand($condition, $update_array);
+                if ($result >= 0) {
+                    if (!empty(input('post.brand_pic')) && !empty($brand_info['brand_pic'])) {
+                        @unlink(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_BRAND . DIRECTORY_SEPARATOR . $brand_info['brand_pic']);
+                    }
+                    $this->log(lang('ds_edit') . lang('brand_index_brand') . '[' . input('post.brand_name') . ']', 1);
+                    dsLayerOpenSuccess(lang('ds_common_save_succ'));
+                } else {
+                    $this->log(lang('ds_edit') . lang('brand_index_brand') . '[' . input('post.brand_name') . ']', 0);
+                    $this->error(lang('ds_common_save_fail'));
+                }
+            }
+        } else {
+            $brand_info = $brand_model->getBrandInfo(array('brand_id' => intval(input('param.brand_id'))));
+            if (empty($brand_info)) {
+                $this->error(lang('param_error'));
+            }
+            View::assign('brand_array', $brand_info);
+            // 一级商品分类
+            $gc_list = model('goodsclass')->getGoodsclassListByParentId(0);
+            View::assign('gc_list', $gc_list);
+            return View::fetch('form');
+        }
+    }
+
+    /**
+     * 删除品牌
+     */
+    public function brand_del() {
+        $brand_id = input('param.brand_id');
+        $brand_id_array = ds_delete_param($brand_id);
+        if ($brand_id_array == FALSE) {
+            $this->log(lang('ds_del') . lang('brand_index_brand') . '[ID:' . $brand_id . ']', 0);
+            ds_json_encode(10001, lang('param_error'));
+        }
+        $brand_mod = model('brand');
+        $condition = array();
+        $condition[] = array('brand_id', 'in', $brand_id_array);
+        $brand_mod->delBrand($condition);
+        $this->log(lang('ds_del') . lang('brand_index_brand') . '[ID:' . $brand_id . ']', 1);
+        ds_json_encode(10000, lang('ds_common_del_succ'));
+    }
+
+    /**
+     * 品牌申请
+     */
+    public function brand_apply() {
+        $brand_model = model('brand');
+        /**
+         * 对申请品牌进行操作 通过,拒绝
+         */
+        if (request()->isPost()) {
+            $del_id_array = input('post.del_id/a'); #获取数组
+            if (!empty($del_id_array)) {
+                switch (input('post.type')) {
+                    case 'pass':
+                        //更新品牌 申请状态
+                        $brandid_array = array();
+                        foreach ($del_id_array as $v) {
+                            $brandid_array[] = intval($v);
+                        }
+                        $update_array = array();
+                        $update_array['brand_apply'] = 1;
+                        $condition = array();
+                        $condition[] = array('brand_id', 'in', $brandid_array);
+                        $brand_model->editBrand($condition, $update_array);
+                        $this->log(lang('brand_apply_pass') . '[ID:' . implode(',', $brandid_array) . ']', null);
+                        $this->success(lang('brand_apply_passed'));
+                        break;
+                    case 'refuse':
+                        //删除该品牌
+                        $brandid_array = array();
+                        foreach ($del_id_array as $v) {
+                            $brandid_array[] = intval($v);
+                        }
+                        $condition = array();
+                        $condition[] = array('brand_id', 'in', $brandid_array);
+                        $brand_model->delBrand($condition);
+                        $this->log(lang('ds_del') . lang('brand_index_brand') . '[ID:' . implode(',', $del_id_array) . ']', 1);
+                        $this->success(lang('ds_common_del_succ'));
+                        break;
+                    default:
+                        $this->success(lang('brand_apply_invalid_argument'));
+                }
+            } else {
+                $this->log(lang('ds_del') . lang('brand_index_brand'), 0);
+                $this->error(lang('ds_common_del_fail'));
+            }
+        } else {
+            /**
+             * 检索条件
+             */
+            $condition = array();
+            if (!empty(input('param.search_brand_name'))) {
+                $condition[] = array('brand_name', 'like', '%' . trim(input('param.search_brand_name')) . '%');
+            }
+            if (!empty(input('param.search_brand_class'))) {
+                $condition[] = array('brand_class', 'like', '%' . trim(input('param.search_brand_class')) . '%');
+            }
+            $brand_list = $brand_model->getBrandNoPassedList($condition, '*', 10);
+
+            View::assign('brand_list', $brand_list);
+            View::assign('show_page', $brand_model->page_info->render());
+            View::assign('search_brand_name', trim(input('param.search_brand_name')));
+            View::assign('search_brand_class', trim(input('param.search_brand_class')));
+            View::assign('filtered', $condition ? 1 : 0); //是否有查询条件
+            $this->setAdminCurItem('brand_apply');
+            return View::fetch('brand_apply');
+        }
+    }
+
+    /**
+     * 审核 申请品牌操作
+     */
+    public function brand_apply_set() {
+        $brand_model = model('brand');
+
+        if (intval(input('param.brand_id')) > 0) {
+            switch (input('param.state')) {
+                case 'pass':
+                    /**
+                     * 更新品牌 申请状态
+                     */
+                    $update_array = array();
+                    $update_array['brand_apply'] = 1;
+                    $result = $brand_model->editBrand(array('brand_id' => intval(input('param.brand_id'))), $update_array);
+                    if ($result) {
+                        $this->log(lang('brand_apply_pass') . '[ID:' . intval(input('param.brand_id')) . ']', null);
+                        $this->success(lang('brand_apply_pass'));
+                    } else {
+                        $this->log(lang('brand_apply_fail') . '[ID:' . intval(input('param.brand_id')) . ')', 0);
+                        $this->error(lang('brand_apply_fail'));
+                    }
+                    break;
+                case 'refuse':
+                    // 删除
+                    $brand_model->delBrand(array('brand_id' => intval(input('param.brand_id'))));
+                    $this->log(lang('ds_del') . lang('brand_index_brand') . '[ID:' . intval(input('param.brand_id')) . ']', 1);
+                    $this->success(lang('ds_common_del_succ'));
+                    break;
+                default:
+                    $this->error(lang('brand_apply_paramerror'));
+            }
+        } else {
+            $this->log(lang('ds_del') . lang('brand_index_brand') . '[ID:' . intval(input('param.brand_id')) . ']', 0);
+            $this->error(lang('brand_apply_brandparamerror'));
+        }
+    }
+
+    /**
+     * ajax操作
+     */
+    public function ajax() {
+        $brand_model = model('brand');
+        switch (input('param.branch')) {
+            /**
+             * 品牌名称
+             */
+            case 'brand_name':
+                /**
+                 * 判断是否有重复
+                 */
+                $condition[] = array('brand_name', '=', trim(input('param.value')));
+                $condition[] = array('brand_id', '<>', intval(input('param.id')));
+                $result = $brand_model->getBrandList($condition);
+                if (empty($result)) {
+                    $brand_model->editBrand(array('brand_id' => intval(input('param.id'))), array('brand_name' => trim(input('param.value'))));
+                    $this->log(lang('ds_edit') . lang('brand_index_name') . '[' . input('param.value') . ']', 1);
+                    echo 'true';
+                    exit;
+                } else {
+                    echo 'false';
+                    exit;
+                }
+                break;
+            /**
+             * 品牌类别,品牌排序,推荐
+             */
+            case 'brand_class':
+            case 'brand_sort':
+            case 'brand_recommend':
+                $brand_model->editBrand(array('brand_id' => intval(input('param.id'))), array(input('param.column') => trim(input('param.value'))));
+                $detail_log = str_replace(array(
+                    'brand_class', 'brand_sort', 'brand_recommend'
+                        ), array(
+                    lang('brand_index_class'), lang('ds_sort'), lang('ds_recommend')
+                        ), input('param.branch'));
+                $this->log(lang('ds_edit') . lang('brand_index_brand') . $detail_log . '[ID:' . intval(input('param.id')) . ')', 1);
+                echo 'true';
+                exit;
+                break;
+            /**
+             * 验证品牌名称是否有重复
+             */
+            case 'check_brand_name':
+                $condition[] = array('brand_name', '=', trim(input('param.brand_name')));
+                $condition[] = array('brand_id', '<>', intval(input('param.id')));
+                $result = $brand_model->getBrandList($condition);
+                if (empty($result)) {
+                    echo 'true';
+                    exit;
+                } else {
+                    echo 'false';
+                    exit;
+                }
+                break;
+        }
+    }
+
+    /**
+     * 品牌导出第一步
+     */
+    public function export_step1() {
+        $brand_model = model('brand');
+        $condition = array();
+        if ((input('param.search_brand_name'))) {
+            $condition[] = array('brand_name', 'like', "%{input('param.search_brand_name')}%");
+        }
+        if ((input('param.search_brand_class'))) {
+            $condition[] = array('brand_class', 'like', "%{input('param.search_brand_class')}%");
+        }
+        $condition[] = array('brand_apply', '=', '1');
+
+        if (!is_numeric(input('param.page'))) {
+            $count = $brand_model->getBrandCount($condition);
+            $export_list = array();
+            if ($count > self::EXPORT_SIZE) {    //显示下载链接
+                $page = ceil($count / self::EXPORT_SIZE);
+                for ($i = 1; $i <= $page; $i++) {
+                    $limit1 = ($i - 1) * self::EXPORT_SIZE + 1;
+                    $limit2 = $i * self::EXPORT_SIZE > $count ? $count : $i * self::EXPORT_SIZE;
+                    $export_list[$i] = $limit1 . ' ~ ' . $limit2;
+                }
+                View::assign('export_list', $export_list);
+                return View::fetch('export_excel');
+            } else {    //如果数量小,直接下载
+                $data = $brand_model->getBrandList($condition, '*', self::EXPORT_SIZE, 'brand_id desc');
+                $this->createExcel($data);
+            }
+        } else {    //下载
+            $limit1 = (input('param.page') - 1) * self::EXPORT_SIZE;
+            $limit2 = self::EXPORT_SIZE;
+            $data = $brand_model->getBrandList($condition, '*', $limit2, 'brand_id desc');
+            $this->createExcel($data);
+        }
+    }
+
+    /**
+     * 生成excel
+     *
+     * @param array $data
+     */
+    private function createExcel($data = array()) {
+        Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/export.lang.php');
+        $excel_obj = new \excel\Excel();
+        $excel_data = array();
+        //设置样式
+        $excel_obj->setStyle(array(
+            'id' => 's_title', 'Font' => array('FontName' => lang('ds_song_typeface'), 'Size' => '12', 'Bold' => '1')
+        ));
+        //header
+        $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_brandid'));
+        $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_brand'));
+        $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_brand_cate'));
+        $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_brand_img'));
+        foreach ((array) $data as $k => $v) {
+            $tmp = array();
+            $tmp[] = array('data' => $v['brand_id']);
+            $tmp[] = array('data' => $v['brand_name']);
+            $tmp[] = array('data' => $v['brand_class']);
+            $tmp[] = array('data' => $v['brand_pic']);
+            $excel_data[] = $tmp;
+        }
+        $excel_data = $excel_obj->charset($excel_data, CHARSET);
+        $excel_obj->addArray($excel_data);
+        $excel_obj->addWorksheet($excel_obj->charset(lang('exp_brand'), CHARSET));
+        $excel_obj->generateXML($excel_obj->charset(lang('exp_brand'), CHARSET) . input('param.page') . '-' . date('Y-m-d-H', TIMESTAMP));
+    }
+
+    /**
+     * 获取卖家栏目列表,针对控制器下的栏目
+     */
+    protected function getAdminItemList() {
+        $menu_array = array(
+            array(
+                'name' => 'index',
+                'text' => lang('ds_manage'),
+                'url' => (string) url('Brand/index'),
+            ),
+            array(
+                'name' => 'brand_add',
+                'text' => lang('ds_add'),
+                'url' => "javascript:dsLayerOpen('" . (string) url('Brand/brand_add') . "','" . lang('ds_add') . "')"
+            ),
+            array(
+                'name' => 'brand_apply',
+                'text' => lang('brand_index_to_audit'),
+                'url' => (string) url('Brand/brand_apply')
+            )
+        );
+        return $menu_array;
+    }
+
+}

+ 158 - 0
app/admin/controller/Chain.php

@@ -0,0 +1,158 @@
+<?php
+
+namespace app\admin\controller;
+use think\facade\View;
+use think\facade\Lang;
+
+/**
+ * ============================================================================
+ * DSMall多用户商城
+ * ============================================================================
+ * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
+ * 网站地址: http://www.csdeshang.com
+ * ----------------------------------------------------------------------------
+ * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
+ * 不允许对程序代码以任何形式任何目的的再发布。
+ * ============================================================================
+ * 控制器
+ */
+class Chain extends AdminControl
+{
+    public function initialize()
+    {
+        parent::initialize(); // TODO: Change the autogenerated stub
+        Lang::load(base_path() . 'admin/lang/'.config('lang.default_lang').'/chain.lang.php');
+    }
+
+    /**
+     * 门店列表
+     */
+    public function index()
+    {
+        $chain_model = model('chain');
+        $where = array();
+        if (input('param.search_name') != '') {
+            $where[]=array('chain_truename','like', '%' . input('param.search_name') . '%');
+            View::assign('search_name', input('param.search_name'));
+        }
+        if (input('param.sign') == 'verify') {
+            View::assign('sign', 'verify');
+            $dp_list = $chain_model->getChainWaitVerifyList($where, 10);
+            $this->setAdminCurItem('verify');
+        }
+        else {
+            $dp_list = $chain_model->getChainList($where, 10);
+            $this->setAdminCurItem('index');
+        }
+        View::assign('show_page', $chain_model->page_info->render());
+        View::assign('dp_list', $dp_list);
+
+        View::assign('chain_state', $chain_model->getChainState());
+        return View::fetch();
+    }
+
+    /**
+     * 门店设置
+     */
+    public function setting()
+    {
+        if (!request()->isPost()) {
+            $list_setting = rkcache('config', true);
+            View::assign('list_setting', $list_setting);
+            return View::fetch();
+        } else {
+            $update_array = array();
+            $update_array['chain_isuse'] = intval(input('post.chain_isuse'));
+            $result = model('config')->editConfig($update_array);
+            $log = lang('ds_open');
+            if ($result === true) {
+                if ($update_array['chain_isuse'] == 0) {
+                    $log = lang('ds_close');
+                    // 删除相关联的收货地址
+                    model('address')->delAddress(array(array('chain_id','<>', 0)));
+                }
+                $this->log($log . lang('chain_function'), 1);
+                dsLayerOpenSuccess(lang('ds_common_save_succ'));
+            } else {
+                $this->log($log . lang('chain_function'), 0);
+                $this->error(lang('ds_common_save_fail'));
+            }
+        }
+    }
+
+    /**
+     * 编辑门店信息
+     */
+    public function edit_chain()
+    {
+        $chain_id = intval(input('param.d_id'));
+        if ($chain_id <= 0) {
+            $this->error(lang('param_error'));
+        }
+        $chain_info = model('chain')->getChainInfo(array('chain_id' => $chain_id));
+        if (empty($chain_info)) {
+            $this->error(lang('param_error'));
+        }
+        View::assign('chain_info', $chain_info);
+        $this->setAdminCurItem('edit_chain');
+        return View::fetch();
+    }
+
+    /**
+     * 编辑保存
+     */
+    public function save_edit()
+    {
+        $chain_id = intval(input('param.did'));
+        if (!request()->isPost() || $chain_id <= 0) {
+            $this->error(lang('param_error'));
+        }
+        $where = array('chain_id' => $chain_id);
+        $update = array();
+        $update['chain_mobile'] = input('post.dmobile');
+        $update['chain_telephony'] = input('post.dtelephony');
+        $update['chain_addressname'] = input('post.daddressname');
+        $update['chain_address'] = input('post.daddress');
+        
+        $chain_passwd = input('post.dpasswd');
+        if (!empty($chain_passwd)) {
+            $update['chain_passwd'] = md5($chain_passwd);
+        }
+        $update['chain_state'] = intval(input('post.dstate'));
+        $update['chain_failreason'] = input('post.fail_reason');
+        $result = model('chain')->editChain($update, $where);
+        if ($result) {
+            // 删除相关联的收货地址
+            model('address')->delAddress(array('chain_id' => $chain_id));
+            $this->log(lang('ds_edit').lang('chain_function').',ID:' . $chain_id, 1);
+            $this->success(lang('ds_common_op_succ'), (string)url('Chain/index'));
+        }
+        else {
+            $this->log(lang('ds_edit').lang('chain_function').',ID:' . $chain_id, 0);
+            $this->error(lang('ds_common_op_fail'));
+        }
+    }
+
+    protected function getAdminItemList()
+    {
+        $menu_array = array(
+            array(
+                'name' => 'index',
+                'text' => lang('ds_manage'),
+                'url' => (string)url('Chain/index')
+            )/*, array(
+                'name' => 'verify',
+                'text' => lang('chain_verify'),
+                'url' => (string)url('Chain/index',['sign'=>'verify'])
+            ), array(
+                'name' => 'setting', 
+                'text' => lang('ds_set'),
+                'url' => "javascript:dsLayerOpen('".(string)url('Chain/setting')."','".lang('ds_set')."')"
+            ),*/
+        );
+        if (request()->action() == 'edit_chain') {
+            $menu_array[] = array('name' => 'edit_chain', 'text' => lang('ds_edit'), 'url' => (string)url('Chain/edit_chain'));
+        }
+        return $menu_array;
+    }
+}

+ 65 - 0
app/admin/controller/Common.php

@@ -0,0 +1,65 @@
+<?php
+
+namespace app\admin\controller;
+/**
+ * ============================================================================
+ * DSMall多用户商城
+ * ============================================================================
+ * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
+ * 网站地址: http://www.csdeshang.com
+ * ----------------------------------------------------------------------------
+ * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
+ * 不允许对程序代码以任何形式任何目的的再发布。
+ * ============================================================================
+ * 控制器
+ */
+class Common extends AdminControl {
+
+    public function initialize() {
+        parent::initialize(); // TODO: Change the autogenerated stub
+    }
+
+    /**
+     * 查询每月的周数组
+     */
+    public function getweekofmonth() {
+        include_once root_path(). 'extend/mall/datehelper.php';
+        $year = input('param.y');
+        $month = input('param.m');
+        $week_arr = getMonthWeekArr($year, $month);
+        echo json_encode($week_arr);
+        die;
+    }
+
+    public function ajax_get_brand() {
+        $initial = trim(input('param.letter'));
+        $keyword = trim(input('param.keyword'));
+        $type = trim(input('param.type'));
+        if (!in_array($type, array('letter', 'keyword')) || ($type == 'letter' && empty($initial)) || ($type == 'keyword' && empty($keyword))) {
+            echo json_encode(array());
+            die();
+        }
+
+        // 实例化模型
+        $where = array();
+        // 验证类型是否关联品牌
+        if ($type == 'letter') {
+            switch ($initial) {
+                case 'all':
+                    break;
+                case '0-9':
+                    $where[]=array('brand_initial','in', array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
+                    break;
+                default:
+                    $where[]=array('brand_initial','=',$initial);
+                    break;
+            }
+        } else {
+            $where[]=array('brand_name|brand_initial','like', '%' . $keyword . '%');
+        }
+        $brand_array = model('brand')->getBrandPassedList($where, 'brand_id,brand_name,brand_initial', 0, 'brand_initial asc, brand_sort asc');
+        echo json_encode($brand_array);
+        die();
+    }
+
+}

Some files were not shown because too many files changed in this diff