ThirdApi.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * 第三方接口
  4. * User: solu
  5. * Date: 2018/12/10
  6. * Time: 4:40 PM
  7. */
  8. class ThirdApi {
  9. /**
  10. * 上报游戏结果
  11. * @author solu
  12. * @param $gameId
  13. * @param $betAmount
  14. * @param $winAmount
  15. * @param string $appid
  16. * @return mixed
  17. */
  18. public static function reportGameResult($gameId, $betAmount, $winAmount, $appid = 'box') {
  19. $api = URL_SICBO . 'eos/gameResult';
  20. $data = [
  21. 'gameid' => $gameId,
  22. 'game_name' => $appid,
  23. 'bet_amount' => $betAmount,
  24. 'win_amount' => $winAmount,
  25. ];
  26. $objEosAES = new EosAES($GLOBALS['codeGtAdmin']);
  27. $objHttp = new dwHttp();
  28. $seed = "{$appid}|{$gameId}";
  29. $data['sign'] = $objEosAES->encode($seed);
  30. $resp = $objHttp->post2($api, $data);
  31. return json_decode($resp, true);
  32. }
  33. /**
  34. * 推送事件
  35. * @author solu
  36. * @param $channel
  37. * @param $data
  38. * @param null $objRedis
  39. * @return int
  40. */
  41. public static function event($channel, $data, $objRedis = null) {
  42. !$objRedis && $objRedis = dwRedis::initNewOne();
  43. if (is_array($data)) {
  44. $data = json_encode($data);
  45. }
  46. return $objRedis->publish($channel, $data);
  47. }
  48. public static function pushPersonEvent($to, array $data) {
  49. $channel = "chat:person:{$to}";
  50. !$data['timestamp'] && $data['timestamp'] = Session::getMS();
  51. return self::event($channel, $data);
  52. }
  53. public static function pushGroupEvent($groupId, array $data) {
  54. $channel = 'chat:group'; // 全局
  55. !$data['timestamp'] && $data['timestamp'] = Session::getMS();
  56. $data['group_id'] = (int) $groupId;
  57. $groupId > 0 && $channel .= ":{$groupId}";
  58. return self::event($channel, $data);
  59. }
  60. /**
  61. * 校验Telegram数据合法性
  62. * @param $auth_data
  63. * @return mixed
  64. * @throws Exception
  65. */
  66. public static function checkTelegramAuthorization($auth_data) {
  67. $check_hash = $auth_data['hash'];
  68. unset($auth_data['hash']);
  69. $hash = self::genTelegramHash($auth_data);
  70. if (strcmp($hash, $check_hash) !== 0) {
  71. throw new Exception('Data is NOT from Telegram');
  72. }
  73. if ((time() - $auth_data['auth_date']) > 86400) {
  74. throw new Exception('Data is outdated');
  75. }
  76. return $auth_data;
  77. }
  78. public static function genTelegramHash($auth_data) {
  79. $data_check_arr = [];
  80. foreach ($auth_data as $key => $value) {
  81. $data_check_arr[] = $key . '=' . $value;
  82. }
  83. sort($data_check_arr);
  84. $data_check_string = implode("\n", $data_check_arr);
  85. $secret_key = hash('sha256', BOT_TOKEN, true);
  86. $hash = hash_hmac('sha256', $data_check_string, $secret_key);
  87. return $hash;
  88. }
  89. /**
  90. * 获取群管理id
  91. * @author solu
  92. * @param $groupId
  93. * @return array
  94. */
  95. public static function getTelegramGroupAdministrators($groupId) {
  96. $admins = [];
  97. if (!$groupId) {
  98. return $admins;
  99. }
  100. $url = TELEGRAM_API_URL . 'getChatAdministrators?chat_id=' . $groupId;
  101. $resp = (new dwHttp())->get($url);
  102. $resp = json_decode($resp, true);
  103. $data = $resp['result'];
  104. foreach ($data as $u) {
  105. if (in_array($u['status'],['creator', 'administrator'])) {
  106. $admins[] = intval($u['user']['id']);
  107. }
  108. }
  109. return $admins;
  110. }
  111. /**
  112. * 聊天消息
  113. * @author solu
  114. * @param $groupId
  115. * @return array
  116. */
  117. public static function getTelegramChatInfo($groupId) {
  118. if (!$groupId) {
  119. return [];
  120. }
  121. $url = TELEGRAM_API_URL . 'getChat?chat_id=' . $groupId;
  122. $resp = (new dwHttp())->get($url);
  123. $resp = json_decode($resp, true);
  124. if (!$resp['ok']) {
  125. return [];
  126. }
  127. return $resp['result'];
  128. }
  129. }