EvoplayController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <?php
  2. namespace App\Http\Controllers\Game;
  3. use App\Facade\TableName;
  4. use App\Game\GameCard;
  5. use App\Game\GlobalUserInfo;
  6. use App\Game\LogGamecardClick;
  7. use App\Game\Services\OuroGameService;
  8. use App\Game\Services\PlatformService;
  9. use App\Game\Services\ServerService;
  10. use App\Game\Services\EvoplayService;
  11. use App\Http\helper\NumConfig;
  12. use App\Models\AccountsInfo;
  13. use App\Notification\TelegramBot;
  14. use App\Util;
  15. use App\Utility\SetNXLock;
  16. use GuzzleHttp\Client;
  17. use GuzzleHttp\Exception\RequestException;
  18. use Illuminate\Http\Request;
  19. use Illuminate\Routing\Controller;
  20. use Illuminate\Support\Facades\DB;
  21. use Illuminate\Support\Facades\Redis;
  22. use Illuminate\Support\Facades\Log;
  23. class EvoplayController extends Controller
  24. {
  25. protected $evoplayService;
  26. protected $projectId;
  27. protected $secretKey;
  28. protected $currency;
  29. public function __construct(EvoplayService $evoplayService)
  30. {
  31. $this->evoplayService = $evoplayService;
  32. $this->projectId = env('EVOPLAY_PROJECT_ID', '');
  33. $this->secretKey = env('EVOPLAY_SECRET_KEY', '');
  34. $this->currency = env('CONFIG_24680_CURRENCY');
  35. }
  36. /**
  37. * 游戏启动页面
  38. *
  39. * @param Request $request
  40. * @return \Illuminate\Http\JsonResponse
  41. */
  42. public function gameLunch(Request $request)
  43. {
  44. // 获取请求参数
  45. $gid = $request->input('gid');
  46. $user = $request->user();
  47. $userId = $user->UserID;
  48. $globalUID = $user->GlobalUID;
  49. // 检查用户渠道权限 (如果需要)
  50. if ($user->Channel != 99 && $user->Channel != 44) {
  51. http_response_code(404);
  52. exit();
  53. }
  54. // 找到游戏卡片
  55. GameCard::$enableStateCheck = false;
  56. $gamecard = GameCard::where('gid', $gid)->first();
  57. // 检查用户当前游戏状态
  58. $inGameId = OuroGameService::getUserInGame($userId, $globalUID);
  59. if ($inGameId != intval($gamecard->id)) {
  60. Util::WriteLog('24680game', compact('inGameId', 'gamecard', 'user'));
  61. }
  62. // 记录游戏点击
  63. $gamecard = GameCard::where('gid', $gid)->where('brand', 'Evoplay')->first();
  64. $gamecard->increment('play_num', 1);
  65. LogGamecardClick::recordClick($gamecard->id, $userId);
  66. // 获取用户语言
  67. $lang = GlobalUserInfo::getLocale();
  68. $supportedLangs = ['en', 'zh', 'ru', 'ja', 'ko', 'th', 'vi', 'id', 'ms', 'fr', 'es', 'de', 'tr', 'pt', 'ar'];
  69. if (!in_array($lang, $supportedLangs)) {
  70. $lang = 'en';
  71. }
  72. // 关闭加载动画
  73. echo "<script>
  74. parent.postMessage({cmd:\"closeLoading\"},\"*\");
  75. </script>";
  76. // 设置缓存控制头
  77. header("Cache-Control: no-cache, no-store, must-revalidate");
  78. header("Expires: 0");
  79. try {
  80. // 使用服务获取游戏启动URL
  81. $gameUrl = $this->evoplayService->getGameUrl($gid, $globalUID, $lang);
  82. if ($gameUrl) {
  83. $htmlContent = "<script>window.location.href='".$gameUrl."';</script>";
  84. echo $htmlContent;
  85. exit();
  86. } else {
  87. echo "<script>alert('无法获取游戏URL,请稍后再试');</script>";
  88. exit();
  89. }
  90. } catch (\Exception $e) {
  91. Util::WriteLog('evoplay_error', $e->getMessage());
  92. echo "<script>alert('系统错误: " . $e->getMessage() . "');</script>";
  93. exit();
  94. }
  95. }
  96. /**
  97. * 获取游戏列表
  98. *
  99. * @param Request $request
  100. * @return \Illuminate\Http\JsonResponse
  101. */
  102. public function gameList(Request $request)
  103. {
  104. try {
  105. // 使用服务获取游戏列表
  106. $result = $this->evoplayService->getGamesList();
  107. return response()->json($result);
  108. } catch (\Exception $e) {
  109. Util::WriteLog('evoplay_error', $e->getMessage());
  110. return response()->json([
  111. 'error' => $e->getMessage()
  112. ], 500);
  113. }
  114. }
  115. /**
  116. * 回调接口 - 用户验证
  117. *
  118. * @param Request $request
  119. * @return \Illuminate\Http\JsonResponse
  120. */
  121. public function verifyPlayer(Request $request)
  122. {
  123. $data = $request->all();
  124. Util::WriteLog('evoplay', [$data, $request->header()]);
  125. // 验证签名
  126. if (!$this->verifyRequest($request)) {
  127. return response()->json([
  128. 'status' => 'error',
  129. 'error' => [
  130. 'code' => 'INVALID_SIGNATURE',
  131. 'message' => 'Invalid signature'
  132. ]
  133. ], 401);
  134. }
  135. $userId = $data['user_id'] ?? null;
  136. if (!$userId) {
  137. return response()->json([
  138. 'status' => 'error',
  139. 'error' => [
  140. 'code' => 'MISSING_PARAMETER',
  141. 'message' => 'Missing user_id parameter'
  142. ]
  143. ], 400);
  144. }
  145. if (!ServerService::IsLocalUser($userId)) {
  146. return $this->evoplayService->callSubApi($userId, $request);
  147. }
  148. // 验证用户
  149. return response()->json($this->evoplayService->validateUser($userId));
  150. }
  151. /**
  152. * 回调接口 - 获取余额
  153. *
  154. * @param Request $request
  155. * @return \Illuminate\Http\JsonResponse
  156. */
  157. public function getBalance(Request $request)
  158. {
  159. $data = $request->all();
  160. Util::WriteLog('evoplay', $data);
  161. // 验证签名
  162. if (!$this->verifyRequest($request)) {
  163. return response()->json([
  164. 'status' => 'error',
  165. 'error' => [
  166. 'code' => 'INVALID_SIGNATURE',
  167. 'message' => 'Invalid signature'
  168. ]
  169. ], 401);
  170. }
  171. $userId = $data['user_id'] ?? null;
  172. if (!$userId) {
  173. return response()->json([
  174. 'status' => 'error',
  175. 'error' => [
  176. 'code' => 'MISSING_PARAMETER',
  177. 'message' => 'Missing user_id parameter'
  178. ]
  179. ], 400);
  180. }
  181. if (!ServerService::IsLocalUser($userId)) {
  182. return $this->evoplayService->callSubApi($userId, $request);
  183. }
  184. // 获取余额
  185. return response()->json($this->evoplayService->getBalance($userId));
  186. }
  187. /**
  188. * 回调接口 - 下注
  189. *
  190. * @param Request $request
  191. * @return \Illuminate\Http\JsonResponse
  192. */
  193. public function bet(Request $request)
  194. {
  195. $data = $request->all();
  196. Util::WriteLog('evoplay', $data);
  197. // 验证签名
  198. if (!$this->verifyRequest($request)) {
  199. return response()->json([
  200. 'status' => 'error',
  201. 'error' => [
  202. 'code' => 'INVALID_SIGNATURE',
  203. 'message' => 'Invalid signature'
  204. ]
  205. ], 401);
  206. }
  207. $userId = $data['user_id'] ?? null;
  208. $gameId = $data['game_id'] ?? null;
  209. $amount = $data['amount'] ?? 0;
  210. $transactionId = $data['transaction_id'] ?? null;
  211. $roundId = $data['round_id'] ?? null;
  212. // 验证必须的参数
  213. if (!$userId || !$transactionId || !$gameId || $amount <= 0) {
  214. return response()->json([
  215. 'status' => 'error',
  216. 'error' => [
  217. 'code' => 'MISSING_PARAMETER',
  218. 'message' => 'Missing required parameters'
  219. ]
  220. ], 400);
  221. }
  222. if (!ServerService::IsLocalUser($userId)) {
  223. return $this->evoplayService->callSubApi($userId, $request);
  224. }
  225. // 处理下注
  226. return response()->json($this->evoplayService->bet($userId, $gameId, $amount, $transactionId, $roundId));
  227. }
  228. /**
  229. * 回调接口 - 赢钱
  230. *
  231. * @param Request $request
  232. * @return \Illuminate\Http\JsonResponse
  233. */
  234. public function win(Request $request)
  235. {
  236. $data = $request->all();
  237. Util::WriteLog('evoplay', $data);
  238. // 验证签名
  239. if (!$this->verifyRequest($request)) {
  240. return response()->json([
  241. 'status' => 'error',
  242. 'error' => [
  243. 'code' => 'INVALID_SIGNATURE',
  244. 'message' => 'Invalid signature'
  245. ]
  246. ], 401);
  247. }
  248. $userId = $data['user_id'] ?? null;
  249. $gameId = $data['game_id'] ?? null;
  250. $amount = $data['amount'] ?? 0;
  251. $transactionId = $data['transaction_id'] ?? null;
  252. $roundId = $data['round_id'] ?? null;
  253. $betTransactionId = $data['bet_transaction_id'] ?? null;
  254. // 验证必须的参数
  255. if (!$userId || !$transactionId || !$gameId || $amount <= 0) {
  256. return response()->json([
  257. 'status' => 'error',
  258. 'error' => [
  259. 'code' => 'MISSING_PARAMETER',
  260. 'message' => 'Missing required parameters'
  261. ]
  262. ], 400);
  263. }
  264. if (!ServerService::IsLocalUser($userId)) {
  265. return $this->evoplayService->callSubApi($userId, $request);
  266. }
  267. // 处理赢钱
  268. return response()->json($this->evoplayService->win($userId, $gameId, $amount, $transactionId, $roundId, $betTransactionId));
  269. }
  270. /**
  271. * 回调接口 - 回滚交易
  272. *
  273. * @param Request $request
  274. * @return \Illuminate\Http\JsonResponse
  275. */
  276. public function rollback(Request $request)
  277. {
  278. $data = $request->all();
  279. Util::WriteLog('evoplay', $data);
  280. // 验证签名
  281. if (!$this->verifyRequest($request)) {
  282. return response()->json([
  283. 'status' => 'error',
  284. 'error' => [
  285. 'code' => 'INVALID_SIGNATURE',
  286. 'message' => 'Invalid signature'
  287. ]
  288. ], 401);
  289. }
  290. $userId = $data['user_id'] ?? null;
  291. $transactionId = $data['transaction_id'] ?? null;
  292. $rollbackTransactionId = $data['rollback_transaction_id'] ?? null;
  293. // 验证必须的参数
  294. if (!$userId || !$transactionId || !$rollbackTransactionId) {
  295. return response()->json([
  296. 'status' => 'error',
  297. 'error' => [
  298. 'code' => 'MISSING_PARAMETER',
  299. 'message' => 'Missing required parameters'
  300. ]
  301. ], 400);
  302. }
  303. if (!ServerService::IsLocalUser($userId)) {
  304. return $this->evoplayService->callSubApi($userId, $request);
  305. }
  306. // 处理回滚
  307. return response()->json($this->evoplayService->rollback($userId, $transactionId, $rollbackTransactionId));
  308. }
  309. /**
  310. * 验证请求的真实性
  311. *
  312. * @param Request $request
  313. * @return bool
  314. */
  315. private function verifyRequest(Request $request)
  316. {
  317. // 在测试环境可以跳过验证
  318. if (env('APP_ENV') === 'local' || env('APP_ENV') === 'testing') {
  319. return true;
  320. }
  321. $signature = $request->header('X-Signature');
  322. if (empty($signature)) {
  323. Util::WriteLog('evoplay', 'Missing signature header');
  324. return false;
  325. }
  326. // 获取请求数据
  327. $data = $request->all();
  328. // 从请求中移除签名字段
  329. if (isset($data['signature'])) {
  330. unset($data['signature']);
  331. }
  332. // 生成预期的签名
  333. $expectedSignature = $this->evoplayService->generateSignature($data);
  334. // 比较签名
  335. $isValid = ($signature === $expectedSignature);
  336. Util::WriteLog('evoplay', [
  337. 'received_signature' => $signature,
  338. 'expected_signature' => $expectedSignature,
  339. 'is_valid' => $isValid
  340. ]);
  341. return $isValid;
  342. }
  343. }