Game.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\controller;
  4. use app\BaseController;
  5. use app\model\MerchantGameModel;
  6. use think\facade\Request;
  7. class Game extends BaseController
  8. {
  9. /**
  10. * 获取游戏列表
  11. */
  12. public function list()
  13. {
  14. $loginInfo = checkUserLogin();
  15. if (!$loginInfo) {
  16. return json_error([], '请先登录');
  17. }
  18. if (!checkPermission($loginInfo, 'Game', 'list')) {
  19. return json_error([], '没有查看游戏列表的权限');
  20. }
  21. // 获取查询参数
  22. $page = Request::get('page', 1, 'intval');
  23. $limit = Request::get('limit', 10, 'intval');
  24. // 过滤条件
  25. $filters = [
  26. 'title' => Request::get('title', '', 'trim'),
  27. 'game_id' => Request::get('game_id', 0, 'intval'),
  28. 'game_platform' => Request::get('game_platform', ''),
  29. 'status' => Request::get('status', ''),
  30. 'rtp_type' => Request::get('rtp_type', ''),
  31. 'free_game_status' => Request::get('free_game_status', ''),
  32. 'terminal_spin' => Request::get('terminal_spin', ''),
  33. 'rtp_min' => Request::get('rtp_min', 0, 'floatval'),
  34. 'rtp_max' => Request::get('rtp_max', 0, 'floatval'),
  35. 'max_multiple_min' => Request::get('max_multiple_min', 0, 'intval'),
  36. 'max_multiple_max' => Request::get('max_multiple_max', 0, 'intval'),
  37. 'create_time_start' => Request::get('create_time_start', '', 'trim'),
  38. 'create_time_end' => Request::get('create_time_end', '', 'trim'),
  39. 'order' => Request::get('order', 'id', 'trim'),
  40. 'sort' => Request::get('sort', 'desc', 'trim'),
  41. ];
  42. try {
  43. // 获取游戏列表
  44. $result = MerchantGameModel::getGameList($page, $limit, $filters);
  45. // 处理列表数据
  46. foreach ($result['list'] as &$game) {
  47. $game['platform_text'] = MerchantGameModel::getPlatformText($game['game_platform']);
  48. $game['status_text'] = MerchantGameModel::getStatusText($game['status']);
  49. $game['rtp_type_text'] = MerchantGameModel::getRtpTypeText($game['rtp_type']);
  50. $game['free_game_status_text'] = $game['free_game_status'] ? '支持' : '不支持';
  51. $game['terminal_spin_text'] = $game['terminal_spin'] ? '开启' : '关闭';
  52. $game['create_time_text'] = date('Y-m-d H:i:s', $game['create_time']);
  53. $game['update_time_text'] = date('Y-m-d H:i:s', $game['update_time']);
  54. // 处理押注配置显示
  55. if (!empty($game['deposit_list'])) {
  56. $depositList = $game['deposit_list'];
  57. if (is_array($depositList)) {
  58. $game['deposit_list_text'] = implode(', ', $depositList);
  59. } else {
  60. $game['deposit_list_text'] = $depositList;
  61. }
  62. } else {
  63. $game['deposit_list_text'] = '未设置';
  64. }
  65. }
  66. return json_success($result, '获取成功');
  67. } catch (\Exception $e) {
  68. return json_error([], '获取游戏列表失败:' . $e->getMessage());
  69. }
  70. }
  71. /**
  72. * 获取游戏详情
  73. */
  74. public function detail()
  75. {
  76. $loginInfo = checkUserLogin();
  77. if (!$loginInfo) {
  78. return json_error([], '请先登录');
  79. }
  80. if (!checkPermission($loginInfo, 'Game', 'detail')) {
  81. return json_error([], '没有查看游戏详情的权限');
  82. }
  83. $id = Request::get('id', 0, 'intval');
  84. if (!$id) {
  85. return json_error([], '游戏ID不能为空');
  86. }
  87. try {
  88. $game = MerchantGameModel::getGameDetail($id);
  89. if (!$game) {
  90. return json_error([], '游戏不存在');
  91. }
  92. // 添加文本字段
  93. $game['platform_text'] = MerchantGameModel::getPlatformText($game['game_platform']);
  94. $game['status_text'] = MerchantGameModel::getStatusText($game['status']);
  95. $game['rtp_type_text'] = MerchantGameModel::getRtpTypeText($game['rtp_type']);
  96. $game['free_game_status_text'] = $game['free_game_status'] ? '支持' : '不支持';
  97. $game['terminal_spin_text'] = $game['terminal_spin'] ? '开启' : '关闭';
  98. $game['create_time_text'] = date('Y-m-d H:i:s', $game['create_time']);
  99. $game['update_time_text'] = date('Y-m-d H:i:s', $game['update_time']);
  100. return json_success($game, '获取成功');
  101. } catch (\Exception $e) {
  102. return json_error([], '获取游戏详情失败:' . $e->getMessage());
  103. }
  104. }
  105. /**
  106. * 创建游戏
  107. */
  108. public function create()
  109. {
  110. $loginInfo = checkUserLogin();
  111. if (!$loginInfo) {
  112. return json_error([], '请先登录');
  113. }
  114. if (!checkPermission($loginInfo, 'Game', 'create')) {
  115. return json_error([], '没有创建游戏的权限');
  116. }
  117. $data = Request::only([
  118. 'game_platform', 'game_id', 'title', 'title_en', 'image', 'image_en',
  119. 'rtp', 'rtp_type', 'free_game_status', 'bet_line_count', 'bet_max_level',
  120. 'max_multiple_count', 'deposit_list', 'default_deposit', 'default_deposit_level',
  121. 'min_deposit', 'terminal_spin', 'status'
  122. ]);
  123. // 验证必填字段
  124. if (empty($data['game_platform'])) {
  125. return json_error([], '游戏平台不能为空');
  126. }
  127. if (empty($data['game_id'])) {
  128. return json_error([], '游戏ID不能为空');
  129. }
  130. if (empty($data['title'])) {
  131. return json_error([], '游戏名称不能为空');
  132. }
  133. // 检查游戏ID是否已存在
  134. if (MerchantGameModel::checkGameIdExists($data['game_id'])) {
  135. return json_error([], '游戏ID已存在');
  136. }
  137. try {
  138. $game = MerchantGameModel::createGame($data);
  139. return json_success(['id' => $game->id], '创建游戏成功');
  140. } catch (\Exception $e) {
  141. return json_error([], '创建游戏失败:' . $e->getMessage());
  142. }
  143. }
  144. /**
  145. * 更新游戏
  146. */
  147. public function update()
  148. {
  149. $loginInfo = checkUserLogin();
  150. if (!$loginInfo) {
  151. return json_error([], '请先登录');
  152. }
  153. if (!checkPermission($loginInfo, 'Game', 'update')) {
  154. return json_error([], '没有更新游戏的权限');
  155. }
  156. $id = Request::post('id', 0, 'intval');
  157. if (!$id) {
  158. return json_error([], '游戏ID不能为空');
  159. }
  160. $data = Request::only([
  161. 'game_platform', 'game_id', 'title', 'title_en', 'image', 'image_en',
  162. 'rtp', 'rtp_type', 'free_game_status', 'bet_line_count', 'bet_max_level',
  163. 'max_multiple_count', 'deposit_list', 'default_deposit', 'default_deposit_level',
  164. 'min_deposit', 'terminal_spin', 'status'
  165. ]);
  166. // 验证必填字段
  167. if (empty($data['title'])) {
  168. return json_error([], '游戏名称不能为空');
  169. }
  170. // 检查游戏是否存在
  171. $game = MerchantGameModel::getGameDetail($id);
  172. if (!$game) {
  173. return json_error([], '游戏不存在');
  174. }
  175. // 如果修改了游戏ID,检查是否与其他游戏冲突
  176. if (isset($data['game_id']) && $data['game_id'] != $game['game_id']) {
  177. if (MerchantGameModel::checkGameIdExists($data['game_id'], $id)) {
  178. return json_error([], '游戏ID已存在');
  179. }
  180. }
  181. try {
  182. MerchantGameModel::updateGame($id, $data);
  183. return json_success([], '更新游戏成功');
  184. } catch (\Exception $e) {
  185. return json_error([], '更新游戏失败:' . $e->getMessage());
  186. }
  187. }
  188. /**
  189. * 更新游戏状态
  190. */
  191. public function updateStatus()
  192. {
  193. $loginInfo = checkUserLogin();
  194. if (!$loginInfo) {
  195. return json_error([], '请先登录');
  196. }
  197. if (!checkPermission($loginInfo, 'Game', 'updateStatus')) {
  198. return json_error([], '没有更新游戏状态的权限');
  199. }
  200. $id = Request::post('id', 0, 'intval');
  201. $status = Request::post('status', 0, 'intval');
  202. if (!$id) {
  203. return json_error([], '游戏ID不能为空');
  204. }
  205. if (!in_array($status, [
  206. MerchantGameModel::STATUS_NORMAL,
  207. MerchantGameModel::STATUS_MAINTAIN,
  208. MerchantGameModel::STATUS_DISABLED
  209. ])) {
  210. return json_error([], '状态值无效');
  211. }
  212. try {
  213. $result = MerchantGameModel::updateGameStatus($id, $status);
  214. if ($result) {
  215. return json_success([], '状态更新成功');
  216. } else {
  217. return json_error([], '状态更新失败');
  218. }
  219. } catch (\Exception $e) {
  220. return json_error([], '更新游戏状态失败:' . $e->getMessage());
  221. }
  222. }
  223. /**
  224. * 批量更新游戏状态
  225. */
  226. public function batchUpdateStatus()
  227. {
  228. $loginInfo = checkUserLogin();
  229. if (!$loginInfo) {
  230. return json_error([], '请先登录');
  231. }
  232. if (!checkPermission($loginInfo, 'Game', 'batchUpdate')) {
  233. return json_error([], '没有批量更新游戏状态的权限');
  234. }
  235. $ids = Request::post('ids', []);
  236. $status = Request::post('status', 0, 'intval');
  237. if (empty($ids) || !is_array($ids)) {
  238. return json_error([], '请选择要更新的游戏');
  239. }
  240. if (!in_array($status, [
  241. MerchantGameModel::STATUS_NORMAL,
  242. MerchantGameModel::STATUS_MAINTAIN,
  243. MerchantGameModel::STATUS_DISABLED
  244. ])) {
  245. return json_error([], '状态值无效');
  246. }
  247. try {
  248. $result = MerchantGameModel::batchUpdateStatus($ids, $status);
  249. if ($result > 0) {
  250. return json_success(['updated' => $result], '批量更新成功');
  251. } else {
  252. return json_error([], '批量更新失败');
  253. }
  254. } catch (\Exception $e) {
  255. return json_error([], '批量更新游戏状态失败:' . $e->getMessage());
  256. }
  257. }
  258. /**
  259. * 获取游戏统计信息
  260. */
  261. public function statistics()
  262. {
  263. $loginInfo = checkUserLogin();
  264. if (!$loginInfo) {
  265. return json_error([], '请先登录');
  266. }
  267. if (!checkPermission($loginInfo, 'Game', 'statistics')) {
  268. return json_error([], '没有查看游戏统计的权限');
  269. }
  270. try {
  271. $statistics = MerchantGameModel::getGameStatistics();
  272. return json_success($statistics, '获取成功');
  273. } catch (\Exception $e) {
  274. return json_error([], '获取游戏统计失败:' . $e->getMessage());
  275. }
  276. }
  277. /**
  278. * 获取游戏平台列表
  279. */
  280. public function getPlatforms()
  281. {
  282. $loginInfo = checkUserLogin();
  283. if (!$loginInfo) {
  284. return json_error([], '请先登录');
  285. }
  286. try {
  287. $platforms = MerchantGameModel::getAllPlatforms();
  288. return json_success($platforms, '获取成功');
  289. } catch (\Exception $e) {
  290. return json_error([], '获取游戏平台失败:' . $e->getMessage());
  291. }
  292. }
  293. /**
  294. * 删除游戏
  295. */
  296. public function delete()
  297. {
  298. $loginInfo = checkUserLogin();
  299. if (!$loginInfo) {
  300. return json_error([], '请先登录');
  301. }
  302. if (!checkPermission($loginInfo, 'Game', 'delete')) {
  303. return json_error([], '没有删除游戏的权限');
  304. }
  305. $id = Request::post('id', 0, 'intval');
  306. if (!$id) {
  307. return json_error([], '游戏ID不能为空');
  308. }
  309. try {
  310. // 检查游戏是否存在
  311. $game = MerchantGameModel::getGameDetail($id);
  312. if (!$game) {
  313. return json_error([], '游戏不存在');
  314. }
  315. // 删除游戏
  316. MerchantGameModel::where('id', $id)->delete();
  317. return json_success([], '删除成功');
  318. } catch (\Exception $e) {
  319. return json_error([], '删除游戏失败:' . $e->getMessage());
  320. }
  321. }
  322. /**
  323. * 导出游戏列表
  324. */
  325. public function export()
  326. {
  327. $loginInfo = checkUserLogin();
  328. if (!$loginInfo) {
  329. return json_error([], '请先登录');
  330. }
  331. if (!checkPermission($loginInfo, 'Game', 'export')) {
  332. return json_error([], '没有导出游戏列表的权限');
  333. }
  334. // 获取所有过滤条件
  335. $filters = [
  336. 'title' => Request::get('title', '', 'trim'),
  337. 'game_id' => Request::get('game_id', 0, 'intval'),
  338. 'game_platform' => Request::get('game_platform', ''),
  339. 'status' => Request::get('status', ''),
  340. 'rtp_type' => Request::get('rtp_type', ''),
  341. 'free_game_status' => Request::get('free_game_status', ''),
  342. 'terminal_spin' => Request::get('terminal_spin', ''),
  343. 'rtp_min' => Request::get('rtp_min', 0, 'floatval'),
  344. 'rtp_max' => Request::get('rtp_max', 0, 'floatval'),
  345. 'max_multiple_min' => Request::get('max_multiple_min', 0, 'intval'),
  346. 'max_multiple_max' => Request::get('max_multiple_max', 0, 'intval'),
  347. 'create_time_start' => Request::get('create_time_start', '', 'trim'),
  348. 'create_time_end' => Request::get('create_time_end', '', 'trim'),
  349. ];
  350. try {
  351. // 获取所有数据
  352. $result = MerchantGameModel::getGameList(1, 100000, $filters);
  353. // 生成CSV数据
  354. $csvData = "ID,游戏ID,游戏平台,中文名称,英文名称,RTP,RTP类型,免费游戏,下注线数,最高倍数,默认押注,最低押注,止损止赢,状态,创建时间\n";
  355. foreach ($result['list'] as $game) {
  356. $csvData .= sprintf(
  357. "%d,%d,%s,%s,%s,%.2f,%s,%s,%d,%d,%.2f,%.2f,%s,%s,%s\n",
  358. $game['id'],
  359. $game['game_id'],
  360. MerchantGameModel::getPlatformText($game['game_platform']),
  361. $game['title'],
  362. $game['title_en'],
  363. $game['rtp'],
  364. MerchantGameModel::getRtpTypeText($game['rtp_type']),
  365. $game['free_game_status'] ? '支持' : '不支持',
  366. $game['bet_line_count'],
  367. $game['max_multiple_count'],
  368. $game['default_deposit'],
  369. $game['min_deposit'],
  370. $game['terminal_spin'] ? '开启' : '关闭',
  371. MerchantGameModel::getStatusText($game['status']),
  372. date('Y-m-d H:i:s', $game['create_time'])
  373. );
  374. }
  375. // 返回CSV数据
  376. return response($csvData)
  377. ->header('Content-Type', 'text/csv; charset=utf-8')
  378. ->header('Content-Disposition', 'attachment; filename="games_' . date('YmdHis') . '.csv"')
  379. ->header('Cache-Control', 'no-cache, must-revalidate');
  380. } catch (\Exception $e) {
  381. return json_error([], '导出游戏列表失败:' . $e->getMessage());
  382. }
  383. }
  384. }