ThirdApi.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. $data_check_arr = [];
  70. foreach ($auth_data as $key => $value) {
  71. $data_check_arr[] = $key . '=' . $value;
  72. }
  73. sort($data_check_arr);
  74. $data_check_string = implode("\n", $data_check_arr);
  75. $secret_key = hash('sha256', BOT_TOKEN, true);
  76. $hash = hash_hmac('sha256', $data_check_string, $secret_key);
  77. if (strcmp($hash, $check_hash) !== 0) {
  78. throw new Exception('Data is NOT from Telegram');
  79. }
  80. if ((time() - $auth_data['auth_date']) > 86400) {
  81. throw new Exception('Data is outdated');
  82. }
  83. return $auth_data;
  84. }
  85. }