| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2019/7/25
- * Time: 14:24
- */
- use \GatewayWorker\Lib\Gateway;
- use Workerman\Lib\Timer;
- class Mlogic
- {
- private static $Instance = null;
- private static $db = null;
- private static $redis = null;
- private static $redisTime = null;
- public static $global = null;
- private static $configPath = null;
- public static function GetInstance()
- {
- if (!empty(self::$Instance)) {
- return self::$Instance;
- }
- self::$Instance = new self();
- self::$Instance->Init();
- return self::$Instance;
- }
- private static function Init()
- {
- $mds = DIRECTORY_SEPARATOR;
- if (strtolower(substr(PHP_OS, 0, 3)) == 'win') {
- self::$configPath = realpath(dirname(__FILE__) . $mds . '..' . $mds . '..' . $mds . '..' . $mds . '..') . $mds . 'application' . $mds;
- } else {
- self::$configPath = realpath(dirname(__FILE__) . $mds . '..' . $mds . '..' . $mds . '..' . $mds . '..' . $mds . '..') . $mds . 'application' . $mds;
- }
- self::getDb();
- self::getRedis();
- self::getGlbData();
- }
- //workman 的共享数据插件
- public static function getGlbData($cache = 1)
- {
- if ($cache) {
- if (!empty(self::$global)) {
- return self::$global;
- }
- }
- self::$global = new \GlobalData\Client('127.0.0.1:2207');
- return self::$global;
- }
- public static function getDb($cache = 1)
- {
- if ($cache) {
- if (!empty(self::$db)) {
- return self::$db;
- }
- }
- $conf = require(self::$configPath . 'database.php');
- self::$db = new \Workerman\MySQL\Connection($conf['hostname'], $conf['hostport'], $conf['username'], $conf['password'], $conf['database'], $conf['charset']);
- return self::$db;
- }
- //实例化redis
- public static function getRedis($cache = 1)
- {
- if ($cache) {
- if (!empty(self::$redis)) {
- return self::$redis;
- }
- }
- $conf = require(self::$configPath . 'redis.php');
- $redis = new \Redis();
- $ret = $redis->connect($conf['host'], $conf['port']);
- if (!$ret) {
- return false;
- }
- if (!empty($conf['passwd'])) {
- $ret = $redis->auth($conf['passwd']);
- if (!$ret) {
- return false;
- }
- }
- $redis->select($conf['db']);
- self::$redisTime = time();
- echo "Redis Reconnect : " . date("Y-m-d H:i:s") . "\n";
- self::$redis = $redis;
- return self::$redis;
- }
- //redis ping ,看是否断线
- public static function RedisPing()
- {
- $ret = self::$redis->ping();
- //echo "redisPing: " . date("Y-m-d H:i:s") . ' - ' . $ret . "\n";
- if (strpos(strtoupper($ret), "PONG") !== false) {
- return true;
- }
- return false;
- }
- //发送消息的代理
- public static function MySendMsg($clientId, $msg)
- {
- Gateway::sendToClient($clientId, $msg);
- }
- //客服是否正在登陆,或已经登陆 0未登陆 1正在登陆 2已登陆成功
- public static function userIsLogin($client_id, $kfuid, $groupid)
- {
- $hakey = self::$redis->hget("loginTmp:" . $kfuid, $kfuid);
- if ($hakey) {
- return 1;
- }
- $cids = Gateway::getClientIdByUid($kfuid);
- if (count($cids) > 0) {
- return 2;
- }
- return 0;
- }
- }
|