Mlogic.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/7/25
  6. * Time: 14:24
  7. */
  8. use \GatewayWorker\Lib\Gateway;
  9. use Workerman\Lib\Timer;
  10. class Mlogic
  11. {
  12. private static $Instance = null;
  13. private static $db = null;
  14. private static $redis = null;
  15. private static $redisTime = null;
  16. public static $global = null;
  17. private static $configPath = null;
  18. public static function GetInstance()
  19. {
  20. if (!empty(self::$Instance)) {
  21. return self::$Instance;
  22. }
  23. self::$Instance = new self();
  24. self::$Instance->Init();
  25. return self::$Instance;
  26. }
  27. private static function Init()
  28. {
  29. $mds = DIRECTORY_SEPARATOR;
  30. if (strtolower(substr(PHP_OS, 0, 3)) == 'win') {
  31. self::$configPath = realpath(dirname(__FILE__) . $mds . '..' . $mds . '..' . $mds . '..' . $mds . '..') . $mds . 'application' . $mds;
  32. } else {
  33. self::$configPath = realpath(dirname(__FILE__) . $mds . '..' . $mds . '..' . $mds . '..' . $mds . '..' . $mds . '..') . $mds . 'application' . $mds;
  34. }
  35. self::getDb();
  36. self::getRedis();
  37. self::getGlbData();
  38. }
  39. //workman 的共享数据插件
  40. public static function getGlbData($cache = 1)
  41. {
  42. if ($cache) {
  43. if (!empty(self::$global)) {
  44. return self::$global;
  45. }
  46. }
  47. self::$global = new \GlobalData\Client('127.0.0.1:2207');
  48. return self::$global;
  49. }
  50. public static function getDb($cache = 1)
  51. {
  52. if ($cache) {
  53. if (!empty(self::$db)) {
  54. return self::$db;
  55. }
  56. }
  57. $conf = require(self::$configPath . 'database.php');
  58. self::$db = new \Workerman\MySQL\Connection($conf['hostname'], $conf['hostport'], $conf['username'], $conf['password'], $conf['database'], $conf['charset']);
  59. return self::$db;
  60. }
  61. //实例化redis
  62. public static function getRedis($cache = 1)
  63. {
  64. if ($cache) {
  65. if (!empty(self::$redis)) {
  66. return self::$redis;
  67. }
  68. }
  69. $conf = require(self::$configPath . 'redis.php');
  70. $redis = new \Redis();
  71. $ret = $redis->connect($conf['host'], $conf['port']);
  72. if (!$ret) {
  73. return false;
  74. }
  75. if (!empty($conf['passwd'])) {
  76. $ret = $redis->auth($conf['passwd']);
  77. if (!$ret) {
  78. return false;
  79. }
  80. }
  81. $redis->select($conf['db']);
  82. self::$redisTime = time();
  83. echo "Redis Reconnect : " . date("Y-m-d H:i:s") . "\n";
  84. self::$redis = $redis;
  85. return self::$redis;
  86. }
  87. //redis ping ,看是否断线
  88. public static function RedisPing()
  89. {
  90. $ret = self::$redis->ping();
  91. //echo "redisPing: " . date("Y-m-d H:i:s") . ' - ' . $ret . "\n";
  92. if (strpos(strtoupper($ret), "PONG") !== false) {
  93. return true;
  94. }
  95. return false;
  96. }
  97. //发送消息的代理
  98. public static function MySendMsg($clientId, $msg)
  99. {
  100. Gateway::sendToClient($clientId, $msg);
  101. }
  102. //客服是否正在登陆,或已经登陆 0未登陆 1正在登陆 2已登陆成功
  103. public static function userIsLogin($client_id, $kfuid, $groupid)
  104. {
  105. $hakey = self::$redis->hget("loginTmp:" . $kfuid, $kfuid);
  106. if ($hakey) {
  107. return 1;
  108. }
  109. $cids = Gateway::getClientIdByUid($kfuid);
  110. if (count($cids) > 0) {
  111. return 2;
  112. }
  113. return 0;
  114. }
  115. }