Game.php 9.3 KB

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