Mlogic.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. public static function GetInstance()
  18. {
  19. if (!empty(self::$Instance)) {
  20. return self::$Instance;
  21. }
  22. self::$Instance = new self();
  23. self::$Instance->Init();
  24. return self::$Instance;
  25. }
  26. private static function Init()
  27. {
  28. self::getDb();
  29. self::getRedis();
  30. self::getGlbData();
  31. }
  32. //workman 的共享数据插件
  33. public static function getGlbData()
  34. {
  35. if (empty(self::$global)) {
  36. self::$global = new \GlobalData\Client('127.0.0.1:2207');
  37. }
  38. return self::$global;
  39. }
  40. public static function getDb()
  41. {
  42. if (empty(self::$db)) {
  43. $mds = DIRECTORY_SEPARATOR;
  44. if (strtolower(substr(PHP_OS, 0, 3)) == 'win') {
  45. $dbcfg = realpath(dirname(__FILE__) . $mds . '..' . $mds . '..' . $mds . '..' . $mds . '..') . $mds . 'application' . $mds . 'database.php';
  46. } else {
  47. $dbcfg = realpath(dirname(__FILE__) . $mds . '..' . $mds . '..' . $mds . '..' . $mds . '..' . $mds . '..') . $mds . 'application' . $mds . 'database.php';
  48. }
  49. $conf = require($dbcfg);
  50. self::$db = new \Workerman\MySQL\Connection($conf['hostname'], $conf['hostport'], $conf['username'], $conf['password'], $conf['database']);
  51. }
  52. return self::$db;
  53. }
  54. //实例化redis
  55. public static function getRedis($force = 0)
  56. {
  57. if (!empty(self::$redis) && !$force) {
  58. return self::$redis;
  59. }
  60. $mds = DIRECTORY_SEPARATOR;
  61. if (strtolower(substr(PHP_OS, 0, 3)) == 'win') {
  62. $dbcfg = realpath(dirname(__FILE__) . $mds . '..' . $mds . '..' . $mds . '..' . $mds . '..') . $mds . 'application' . $mds . 'redis.php';
  63. } else {
  64. $dbcfg = realpath(dirname(__FILE__) . $mds . '..' . $mds . '..' . $mds . '..' . $mds . '..' . $mds . '..') . $mds . 'application' . $mds . 'redis.php';
  65. }
  66. $conf = require($dbcfg);
  67. $redis = new \Redis();
  68. $ret = $redis->connect($conf['host'], $conf['port']);
  69. if (!$ret) {
  70. return false;
  71. }
  72. if (!empty($conf['passwd'])) {
  73. $ret = $redis->auth($conf['passwd']);
  74. if (!$ret) {
  75. return false;
  76. }
  77. }
  78. $redis->select($conf['db']);
  79. self::$redisTime = time();
  80. self::$redis = $redis;
  81. return self::$redis;
  82. }
  83. //redis ping ,看是否断线
  84. public static function RedisPing()
  85. {
  86. $ret = self::$redis->ping();
  87. if (strpos("PONG", strtolower($ret)) !== false) {
  88. return true;
  89. }
  90. return false;
  91. }
  92. //发送消息的代理
  93. public static function MySendMsg($clientId, $msg)
  94. {
  95. Gateway::sendToClient($clientId, $msg);
  96. }
  97. //客服是否正在登陆,或已经登陆 0未登陆 1正在登陆 2已登陆成功
  98. public static function userIsLogin($client_id, $kfuid, $groupid)
  99. {
  100. $hakey = self::$redis->hget("loginTmp:" . $kfuid, $kfuid);
  101. if ($hakey) {
  102. return 1;
  103. }
  104. $cids = Gateway::getClientIdByUid($kfuid);
  105. if (count($cids) > 0) {
  106. return 2;
  107. }
  108. return 0;
  109. }
  110. }