| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- <?php
- declare (strict_types = 1);
- namespace app\controller;
- use app\BaseController;
- use app\model\GameModel;
- use app\validate\GameValidate;
- use think\facade\Request;
- class Game extends BaseController
- {
- /**
- * 获取游戏列表
- */
- public function list()
- {
- // 获取当前用户信息
- $userInfo = $this->getUserInfo();
-
- // 获取查询参数
- $page = Request::get('page', 1, 'intval');
- $limit = Request::get('limit', 10, 'intval');
-
- // 过滤条件
- $filters = [
- 'title' => Request::get('title', '', 'trim'),
- 'game_id' => Request::get('game_id', 0, 'intval'),
- 'game_platform' => Request::get('game_platform', ''),
- 'status' => Request::get('status', ''),
- 'order' => Request::get('order', 'id', 'trim'),
- 'sort' => Request::get('sort', 'desc', 'trim'),
- ];
-
- try {
- // 获取游戏列表
- $result = GameModel::getGameList($userInfo['merchant_id'], $page, $limit, $filters);
-
- // 处理列表数据
- foreach ($result['list'] as &$game) {
- $game['status_text'] = GameModel::getStatusText($game['status']);
- $game['rtp_type_text'] = GameModel::getRtpTypeText($game['rtp_type']);
- }
-
- return json_success($result, '获取成功');
- } catch (\Exception $e) {
- return json_error([], '获取游戏列表失败:' . $e->getMessage());
- }
- }
-
- /**
- * 获取游戏详情
- */
- public function detail()
- {
- // 获取当前用户信息
- $userInfo = $this->getUserInfo();
-
- $id = Request::get('id', 0, 'intval');
- if (!$id) {
- return json_error([], '游戏ID不能为空');
- }
-
- try {
- $game = GameModel::getGameDetail($id, $userInfo['merchant_id']);
- if (!$game) {
- return json_error([], '游戏不存在');
- }
-
- $game['status_text'] = GameModel::getStatusText($game['status']);
- $game['rtp_type_text'] = GameModel::getRtpTypeText($game['rtp_type']);
-
- return json_success($game, '获取成功');
- } catch (\Exception $e) {
- return json_error([], '获取游戏详情失败:' . $e->getMessage());
- }
- }
-
- /**
- * 更新游戏
- */
- public function update()
- {
- // 获取当前用户信息
- $userInfo = $this->getUserInfo();
-
- $id = Request::post('id', 0, 'intval');
- if (!$id) {
- return json_error([], '游戏ID不能为空');
- }
-
- // 检查游戏是否存在且属于当前商户
- $game = GameModel::getGameDetail($id, $userInfo['merchant_id']);
- if (!$game) {
- return json_error([], '游戏不存在');
- }
-
- // 获取请求中提供的所有可更新字段
- $requestData = Request::post();
- $allowedFields = [
- 'rtp', 'rtp_type', 'free_game_status', 'bet_max_level', 'terminal_spin',
- 'max_multiple_count', 'deposit_list', 'default_deposit', 'default_deposit_level', 'min_deposit',
- ];
- $updateData = [];
-
- // 只处理请求中存在且允许更新的字段
- foreach ($allowedFields as $field) {
- if (array_key_exists($field, $requestData)) {
- // 转换数值类型
- if (in_array($field, ['rtp_type', 'free_game_status', 'bet_max_level', 'terminal_spin', 'max_multiple_count', 'default_deposit_level'])) {
- $updateData[$field] = intval($requestData[$field]);
- } elseif (in_array($field, ['rtp', 'default_deposit', 'min_deposit'])) {
- $updateData[$field] = floatval($requestData[$field]);
- } else {
- $updateData[$field] = $requestData[$field];
- }
- }
- }
-
- if (empty($updateData)) {
- return json_error([], '没有要更新的数据');
- }
-
- // 使用验证器进行字段验证
- $validate = new GameValidate();
-
- // 只验证传入的字段
- if (!$validate->only(array_keys($updateData))->check($updateData)) {
- return json_error([], $validate->getError());
- }
-
- try {
- GameModel::updateGame($id, $updateData, $userInfo['merchant_id']);
- return json_success([], '更新游戏成功');
- } catch (\Exception $e) {
- return json_error([], '更新游戏失败:' . $e->getMessage());
- }
- }
-
- /**
- * 更新游戏状态
- */
- public function updateStatus()
- {
- // 获取当前用户信息
- $userInfo = $this->getUserInfo();
-
- $ids = Request::post('ids', []);
- $status = Request::post('status', 0, 'intval');
-
- if (empty($ids) || !is_array($ids)) {
- return json_error([], '请选择要更新的游戏');
- }
-
- if (!in_array($status, [
- GameModel::STATUS_NORMAL,
- GameModel::STATUS_MAINTAIN,
- GameModel::STATUS_DISABLED
- ])) {
- return json_error([], '状态值无效');
- }
-
- try {
- $result = GameModel::updateGameStatus($ids, $userInfo['merchant_id'], $status);
- if ($result) {
- return json_success([], '状态更新成功');
- } else {
- return json_error([], '状态更新失败');
- }
- } catch (\Exception $e) {
- return json_error([], '更新游戏状态失败:' . $e->getMessage());
- }
- }
-
-
- /**
- * 获取游戏统计信息
- */
- public function statistics()
- {
- // 获取当前用户信息
- $userInfo = $this->getUserInfo();
-
- try {
- $statistics = GameModel::getGameStatistics($userInfo['merchant_id']);
- return json_success($statistics, '获取成功');
- } catch (\Exception $e) {
- return json_error([], '获取游戏统计失败:' . $e->getMessage());
- }
- }
-
- /**
- * 获取游戏平台列表
- */
- public function getPlatforms()
- {
- try {
- $platforms = GameModel::getAllPlatforms();
- return json_success($platforms, '获取成功');
- } catch (\Exception $e) {
- return json_error([], '获取游戏平台失败:' . $e->getMessage());
- }
- }
- /**
- * 获取所有游戏
- */
- public function getGames()
- {
- $userInfo = $this->getUserInfo();
- try {
- $platforms = GameModel::getGames($userInfo['merchant_id']);
- return json_success($platforms, '获取成功');
- } catch (\Exception $e) {
- return json_error([], '获取游戏平台失败:' . $e->getMessage());
- }
- }
-
- /**
- * 导出游戏列表
- */
- public function export()
- {
- // 获取当前用户信息
- $userInfo = $this->getUserInfo();
-
- // 获取所有过滤条件
- $filters = [
- 'title' => Request::get('title', '', 'trim'),
- 'game_id' => Request::get('game_id', 0, 'intval'),
- 'game_platform' => Request::get('game_platform', ''),
- 'status' => Request::get('status', ''),
- 'order' => Request::get('order', 'id', 'trim'),
- 'sort' => Request::get('sort', 'desc', 'trim'),
- ];
-
- try {
- // 获取所有数据
- $result = GameModel::getGameList($userInfo['merchant_id'], 1, 100000, $filters);
-
- // 生成CSV数据
- $csvData = "ID,游戏ID,游戏平台,中文名称,英文名称,RTP,RTP类型,免费游戏,下注线数,最高倍数,默认押注,最低押注,止损止赢,状态,创建时间\n";
-
- foreach ($result['list'] as $game) {
- $csvData .= sprintf(
- "%d,%d,%s,%s,%s,%.2f,%s,%s,%d,%d,%.2f,%.2f,%s,%s,%s\n",
- $game['id'],
- $game['game_id'],
- $game['game_platform'],
- $game['title'],
- $game['title_en'],
- $game['rtp'],
- GameModel::getRtpTypeText($game['rtp_type']),
- $game['free_game_status'] ? '开启' : '关闭',
- $game['bet_line_count'],
- $game['max_multiple_count'],
- $game['default_deposit'],
- $game['min_deposit'],
- $game['terminal_spin'] ? '开启' : '关闭',
- GameModel::getStatusText($game['status']),
- $game['create_time'],
- );
- }
-
- // 返回CSV数据
- return response($csvData)
- ->header(['Content-Type' => 'text/csv; charset=utf-8'])
- ->header(['Content-Disposition' => 'attachment; filename="games_' . date('YmdHis') . '.csv"'])
- ->header(['Cache-Control' => 'no-cache, must-revalidate']);
-
- } catch (\Exception $e) {
- return json_error([], '导出游戏列表失败:' . $e->getMessage());
- }
- }
- }
|