123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?php
- /**
- * 第三方接口
- * User: solu
- * Date: 2018/12/10
- * Time: 4:40 PM
- */
- class ThirdApi {
- /**
- * 上报游戏结果
- * @author solu
- * @param $gameId
- * @param $betAmount
- * @param $winAmount
- * @param string $appid
- * @return mixed
- */
- public static function reportGameResult($gameId, $betAmount, $winAmount, $appid = 'box') {
- $api = URL_SICBO . 'eos/gameResult';
- $data = [
- 'gameid' => $gameId,
- 'game_name' => $appid,
- 'bet_amount' => $betAmount,
- 'win_amount' => $winAmount,
- ];
- $objEosAES = new EosAES($GLOBALS['codeGtAdmin']);
- $objHttp = new dwHttp();
- $seed = "{$appid}|{$gameId}";
- $data['sign'] = $objEosAES->encode($seed);
- $resp = $objHttp->post2($api, $data);
- return json_decode($resp, true);
- }
- /**
- * 推送事件
- * @author solu
- * @param $channel
- * @param $data
- * @param null $objRedis
- * @return int
- */
- public static function event($channel, $data, $objRedis = null) {
- !$objRedis && $objRedis = dwRedis::initNewOne();
- if (is_array($data)) {
- $data = json_encode($data);
- }
- return $objRedis->publish($channel, $data);
- }
- public static function pushPersonEvent($to, array $data) {
- $channel = "chat:person:{$to}";
- !$data['timestamp'] && $data['timestamp'] = Session::getMS();
- return self::event($channel, $data);
- }
- public static function pushGroupEvent($groupId, array $data) {
- $channel = 'chat:group'; // 全局
- !$data['timestamp'] && $data['timestamp'] = Session::getMS();
- $data['group_id'] = (int) $groupId;
- $groupId > 0 && $channel .= ":{$groupId}";
- return self::event($channel, $data);
- }
- /**
- * 校验Telegram数据合法性
- * @param $auth_data
- * @return mixed
- * @throws Exception
- */
- public static function checkTelegramAuthorization($auth_data) {
- $check_hash = $auth_data['hash'];
- unset($auth_data['hash']);
- $hash = self::genTelegramHash($auth_data);
- if (strcmp($hash, $check_hash) !== 0) {
- throw new Exception('Data is NOT from Telegram');
- }
- if ((time() - $auth_data['auth_date']) > 86400) {
- throw new Exception('Data is outdated');
- }
- return $auth_data;
- }
- public static function genTelegramHash($auth_data) {
- $data_check_arr = [];
- foreach ($auth_data as $key => $value) {
- $data_check_arr[] = $key . '=' . $value;
- }
- sort($data_check_arr);
- $data_check_string = implode("\n", $data_check_arr);
- $secret_key = hash('sha256', BOT_TOKEN, true);
- $hash = hash_hmac('sha256', $data_check_string, $secret_key);
- return $hash;
- }
- /**
- * 获取群管理id
- * @author solu
- * @param $groupId
- * @return array
- */
- public static function getTelegramGroupAdministrators($groupId) {
- $admins = [];
- if (!$groupId) {
- return $admins;
- }
- $url = TELEGRAM_API_URL . 'getChatAdministrators?chat_id=' . $groupId;
- $resp = (new dwHttp())->get($url);
- $resp = json_decode($resp, true);
- $data = $resp['result'];
- foreach ($data as $u) {
- if (in_array($u['status'],['creator', 'administrator'])) {
- $admins[] = intval($u['user']['id']);
- }
- }
- return $admins;
- }
- /**
- * 聊天消息
- * @author solu
- * @param $groupId
- * @return array
- */
- public static function getTelegramChatInfo($groupId) {
- if (!$groupId) {
- return [];
- }
- $url = TELEGRAM_API_URL . 'getChat?chat_id=' . $groupId;
- $resp = (new dwHttp())->get($url);
- $resp = json_decode($resp, true);
- if (!$resp['ok']) {
- return [];
- }
- return $resp['result'];
- }
- }
|