| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?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;
- 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()
- {
- self::getDb();
- self::getRedis();
- self::getGlbData();
- }
- //workman 的共享数据插件
- public static function getGlbData()
- {
- if (empty(self::$global)) {
- self::$global = new \GlobalData\Client('127.0.0.1:2207');
- }
- return self::$global;
- }
- public static function getDb()
- {
- if (empty(self::$db)) {
- $mds = DIRECTORY_SEPARATOR;
- if (strtolower(substr(PHP_OS, 0, 3)) == 'win') {
- $dbcfg = realpath(dirname(__FILE__) . $mds . '..' . $mds . '..' . $mds . '..' . $mds . '..') . $mds . 'application' . $mds . 'database.php';
- } else {
- $dbcfg = realpath(dirname(__FILE__) . $mds . '..' . $mds . '..' . $mds . '..' . $mds . '..' . $mds . '..') . $mds . 'application' . $mds . 'database.php';
- }
- $conf = require($dbcfg);
- self::$db = new \Workerman\MySQL\Connection($conf['hostname'], $conf['hostport'], $conf['username'], $conf['password'], $conf['database']);
- }
- return self::$db;
- }
- //实例化redis
- public static function getRedis($force = 0)
- {
- if (!empty(self::$redis) && !$force) {
- return self::$redis;
- }
- $mds = DIRECTORY_SEPARATOR;
- if (strtolower(substr(PHP_OS, 0, 3)) == 'win') {
- $dbcfg = realpath(dirname(__FILE__) . $mds . '..' . $mds . '..' . $mds . '..' . $mds . '..') . $mds . 'application' . $mds . 'redis.php';
- } else {
- $dbcfg = realpath(dirname(__FILE__) . $mds . '..' . $mds . '..' . $mds . '..' . $mds . '..' . $mds . '..') . $mds . 'application' . $mds . 'redis.php';
- }
- $conf = require($dbcfg);
- $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();
- self::$redis = $redis;
- return self::$redis;
- }
- //redis ping ,看是否断线
- public static function RedisPing()
- {
- $ret = self::$redis->ping();
- if (strpos("PONG", strtolower($ret)) !== 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;
- }
- }
|