Game.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\controller;
  4. use app\BaseController;
  5. use app\model\GameModel;
  6. use think\facade\Request;
  7. class Game extends BaseController
  8. {
  9. /**
  10. * 获取游戏列表
  11. */
  12. public function list()
  13. {
  14. // 获取当前用户信息
  15. $userInfo = $this->getUserInfo();
  16. // 获取查询参数
  17. $page = Request::get('page', 1, 'intval');
  18. $limit = Request::get('limit', 10, 'intval');
  19. // 过滤条件
  20. $filters = [
  21. 'title' => Request::get('title', '', 'trim'),
  22. 'game_id' => Request::get('game_id', 0, 'intval'),
  23. 'game_platform' => Request::get('game_platform', ''),
  24. 'status' => Request::get('status', ''),
  25. 'order' => Request::get('order', 'id', 'trim'),
  26. 'sort' => Request::get('sort', 'desc', 'trim'),
  27. ];
  28. try {
  29. // 获取游戏列表
  30. $result = GameModel::getGameList($userInfo['merchant_id'], $page, $limit, $filters);
  31. // 处理列表数据
  32. foreach ($result['list'] as &$game) {
  33. $game['status_text'] = GameModel::getStatusText($game['status']);
  34. $game['rtp_type_text'] = GameModel::getRtpTypeText($game['rtp_type']);
  35. }
  36. return json_success($result, '获取成功');
  37. } catch (\Exception $e) {
  38. return json_error([], '获取游戏列表失败:' . $e->getMessage());
  39. }
  40. }
  41. /**
  42. * 获取游戏详情
  43. */
  44. public function detail()
  45. {
  46. // 获取当前用户信息
  47. $userInfo = $this->getUserInfo();
  48. $id = Request::get('id', 0, 'intval');
  49. if (!$id) {
  50. return json_error([], '游戏ID不能为空');
  51. }
  52. try {
  53. $game = GameModel::getGameDetail($id, $userInfo['merchant_id']);
  54. if (!$game) {
  55. return json_error([], '游戏不存在');
  56. }
  57. $game['status_text'] = GameModel::getStatusText($game['status']);
  58. $game['rtp_type_text'] = GameModel::getRtpTypeText($game['rtp_type']);
  59. return json_success($game, '获取成功');
  60. } catch (\Exception $e) {
  61. return json_error([], '获取游戏详情失败:' . $e->getMessage());
  62. }
  63. }
  64. /**
  65. * 更新游戏
  66. */
  67. public function update()
  68. {
  69. // 获取当前用户信息
  70. $userInfo = $this->getUserInfo();
  71. $id = Request::post('id', 0, 'intval');
  72. if (!$id) {
  73. return json_error([], '游戏ID不能为空');
  74. }
  75. // 定义允许更新的字段
  76. $allowedFields = [
  77. 'rtp', 'rtp_type', 'free_game_status', 'bet_max_level', 'terminal_spin',
  78. 'max_multiple_count', 'deposit_list', 'default_deposit', 'default_deposit_level', 'min_deposit',
  79. ];
  80. // 获取请求参数
  81. $params = Request::param();
  82. $data = [];
  83. // 只获取实际传递的字段
  84. foreach ($allowedFields as $field) {
  85. if (isset($params[$field])) {
  86. $data[$field] = $params[$field];
  87. }
  88. }
  89. // 如果没有要更新的字段
  90. if (empty($data)) {
  91. return json_error([], '没有要更新的数据');
  92. }
  93. // 检查游戏是否存在且属于当前商户
  94. $game = GameModel::getGameDetail($id, $userInfo['merchant_id']);
  95. if (!$game) {
  96. return json_error([], '游戏不存在');
  97. }
  98. try {
  99. GameModel::updateGame($id, $data, $userInfo['merchant_id']);
  100. return json_success([], '更新游戏成功');
  101. } catch (\Exception $e) {
  102. return json_error([], '更新游戏失败:' . $e->getMessage());
  103. }
  104. }
  105. /**
  106. * 更新游戏状态
  107. */
  108. public function updateStatus()
  109. {
  110. // 获取当前用户信息
  111. $userInfo = $this->getUserInfo();
  112. $ids = Request::post('ids', []);
  113. $status = Request::post('status', 0, 'intval');
  114. if (empty($ids) || !is_array($ids)) {
  115. return json_error([], '请选择要更新的游戏');
  116. }
  117. if (!in_array($status, [
  118. GameModel::STATUS_NORMAL,
  119. GameModel::STATUS_MAINTAIN,
  120. GameModel::STATUS_DISABLED
  121. ])) {
  122. return json_error([], '状态值无效');
  123. }
  124. try {
  125. $result = GameModel::updateGameStatus($ids, $userInfo['merchant_id'], $status);
  126. if ($result) {
  127. return json_success([], '状态更新成功');
  128. } else {
  129. return json_error([], '状态更新失败');
  130. }
  131. } catch (\Exception $e) {
  132. return json_error([], '更新游戏状态失败:' . $e->getMessage());
  133. }
  134. }
  135. /**
  136. * 获取游戏统计信息
  137. */
  138. public function statistics()
  139. {
  140. // 获取当前用户信息
  141. $userInfo = $this->getUserInfo();
  142. try {
  143. $statistics = GameModel::getGameStatistics($userInfo['merchant_id']);
  144. return json_success($statistics, '获取成功');
  145. } catch (\Exception $e) {
  146. return json_error([], '获取游戏统计失败:' . $e->getMessage());
  147. }
  148. }
  149. /**
  150. * 获取游戏平台列表
  151. */
  152. public function getPlatforms()
  153. {
  154. try {
  155. $platforms = GameModel::getAllPlatforms();
  156. return json_success($platforms, '获取成功');
  157. } catch (\Exception $e) {
  158. return json_error([], '获取游戏平台失败:' . $e->getMessage());
  159. }
  160. }
  161. /**
  162. * 导出游戏列表
  163. */
  164. public function export()
  165. {
  166. // 获取当前用户信息
  167. $userInfo = $this->getUserInfo();
  168. // 获取所有过滤条件
  169. $filters = [
  170. 'title' => Request::get('title', '', 'trim'),
  171. 'game_id' => Request::get('game_id', 0, 'intval'),
  172. 'game_platform' => Request::get('game_platform', ''),
  173. 'status' => Request::get('status', ''),
  174. 'order' => Request::get('order', 'id', 'trim'),
  175. 'sort' => Request::get('sort', 'desc', 'trim'),
  176. ];
  177. try {
  178. // 获取所有数据
  179. $result = GameModel::getGameList($userInfo['merchant_id'], 1, 100000, $filters);
  180. // 生成CSV数据
  181. $csvData = "ID,游戏ID,游戏平台,中文名称,英文名称,RTP,RTP类型,免费游戏,下注线数,最高倍数,默认押注,最低押注,止损止赢,状态,创建时间\n";
  182. foreach ($result['list'] as $game) {
  183. $csvData .= sprintf(
  184. "%d,%d,%s,%s,%s,%.2f,%s,%s,%d,%d,%.2f,%.2f,%s,%s,%s\n",
  185. $game['id'],
  186. $game['game_id'],
  187. $game['game_platform'],
  188. $game['title'],
  189. $game['title_en'],
  190. $game['rtp'],
  191. GameModel::getRtpTypeText($game['rtp_type']),
  192. $game['free_game_status'] ? '开启' : '关闭',
  193. $game['bet_line_count'],
  194. $game['max_multiple_count'],
  195. $game['default_deposit'],
  196. $game['min_deposit'],
  197. $game['terminal_spin'] ? '开启' : '关闭',
  198. GameModel::getStatusText($game['status']),
  199. $game['create_time'],
  200. );
  201. }
  202. // 返回CSV数据
  203. return response($csvData)
  204. ->header(['Content-Type' => 'text/csv; charset=utf-8'])
  205. ->header(['Content-Disposition' => 'attachment; filename="games_' . date('YmdHis') . '.csv"'])
  206. ->header(['Cache-Control' => 'no-cache, must-revalidate']);
  207. } catch (\Exception $e) {
  208. return json_error([], '导出游戏列表失败:' . $e->getMessage());
  209. }
  210. }
  211. }