order('priority ASC') ->select(); $this->success('success', $data); } //根据分类获取游戏 public function getGamesByCategory() { $categoryId = $this->request->post('categoryId'); //分类ID $page = $this->request->post('page'); // 当前页码 $pageNum = $this->request->post('pageNum'); //每页显示的数据条数 if (!is_numeric($categoryId) || !is_numeric($pageNum)) { $this->fail(500, '参数校验错误'); } $games = Db::table('tb_game') ->field('id,title,image_url,description,buy_count,browse,price') ->where(['category' => $categoryId]) ->paginate([ 'page' => $page, 'list_rows' => $pageNum ]); $this->success('success', $games); } //根据游戏ID获取游戏 public function getGameById() { $gameId = $this->request->post('gameId'); if (!is_numeric($gameId)) { $this->fail(500, 'gameId参数校验错误'); } $data = Db::table('tb_game') ->where(['id' => $gameId]) ->find(); if (!$data) { $this->fail(500, 'gameId不存在'); } //浏览次数+1 Db::table('tb_game') ->where(['id' => $gameId]) ->update([ 'browse' => $data['browse'] + 1, ]); unset($data['download_url']); unset($data['extracted_code']); $this->success('success', $data); } //购买游戏 public function buyGame() { $gameId = $this->request->post('gameId'); if (!is_numeric($gameId)) { $this->fail(500, 'gameId参数校验错误'); } $game = Db::table('tb_game') ->where(['id' => $gameId]) ->find(); if (!$game) { $this->fail(501, 'gameId不存在'); } $user = Db::table('tb_user') ->where(['id' => $this->getUser()->user_id]) ->find(); if ($user['balance'] < $game['price']) { $this->fail(502, '余额不足无法购买'); } //避免重复购买同一款游戏 $userGame = Db::table('tb_user_game') ->where(['user_id' => $this->getUser()->user_id]) ->where(['game_id' => $gameId]) ->find(); if ($userGame) { $this->fail(503, '您已经购买过此款游戏了'); } //减少余额 Db::table('tb_user') ->where(['id' => $this->getUser()->user_id]) ->update(['balance' => $user['balance'] - $game['price']]); //购买到记录 Db::table('tb_user_game') ->insert([ 'user_id' => $this->getUser()->user_id, 'game_id' => $gameId, 'price' => $game['price'], 'created_at' => time(), 'channel' => 1, //站内直购 ]); $this->success('success', null); } //获取我已经购买的游戏,包括cdkey兑换 public function getMyBoughtGames() { $page = $this->request->post('page'); //页码 $pageNum = $this->request->post('pageNum'); //每页显示的数据条数 if (!is_numeric($pageNum) || !is_numeric($page)) { $this->fail(500, 'page或pageNum参数校验错误'); } $data = Db::table('tb_user_game') ->alias('ug') ->join('tb_game g', 'ug.game_id = g.id') ->where(['ug.user_id' => $this->getUser()->user_id]) ->field('ug.*,g.title as game_title,g.image_url as game_image_url,g.description as game_description') ->paginate([ 'page' => $page, 'list_rows' => $pageNum, ]); $this->success('success', $data); } //电商渠道购买,兑换购买的游戏 public function exchangeCdKeyGame() { $cdKey = $this->request->post('cdKey'); if (empty($cdKey)) { $this->fail(500, 'cdKey参数校验错误'); } $data = Db::table('tb_cdkey') ->where(['cdkey' => $cdKey]) ->find(); if (!$data) { $this->fail(501, 'cdKey不存在'); } if ($data['state'] == 1) { $this->fail(502, 'cdKey已经使用过'); } //如果过期了做一下更新 if ($data['expired_at'] < time()) { Db::table('tb_cdkey') ->where(['id' => $data['id']]) ->update(['state' => 2]); $data['state'] = 2; } if ($data['state'] == 2) { $this->fail(503, 'cdKey已经过期'); } //使用cdKey Db::table('tb_cdkey') ->where(['id' => $data['id']]) ->update([ 'state' => 1, 'use_at' => time(), ]); //兑换购买渠道选择 $channel = 2; if (str_starts_with($data['cdkey'], 'tb')) { $channel = 2; } elseif (str_starts_with($data['cdkey'], 'pdd')) { $channel = 3; } //获取游戏的价格 $game = Db::table('tb_game') ->where(['id' => $data['game_id']]) ->find(); $price = $game['price']; //购买游戏 Db::table('tb_user_game') ->insert([ 'user_id' => $this->getUser()->user_id, 'game_id' => $data['game_id'], 'price' => $price, 'channel' => $channel, 'created_at' => time(), ]); $this->success('success', [ 'gameId' => $data['game_id'], ]); } //获取我使用过的兑换码 public function getMyUsedCdKey() { $pageNum = $this->request->post('pageNum'); //每页显示的数据条数 if (!is_numeric($pageNum)) { $this->fail(500, 'pageNum参数校验错误'); } $data = Db::table('tb_cdkey') ->where(['user_id' => $this->getUser()->user_id]) ->where(['state' => 1]) ->paginate($pageNum); $this->success('success', $data); } //搜索游戏 public function searchGame() { $keywords = $this->request->post('keywords'); //关键字 if (empty($keywords)) { $this->fail(500, '关键字不能为空'); } if (strlen($keywords) >= 100) { $this->fail(501, '关键字长度超限'); } $data = Db::table('tb_game') ->where('title', 'like', '%' . $keywords . '%') ->select(); Db::table('tb_search_keywords') ->insert([ 'keywords' => $keywords, 'created_at' => time(), ]); $this->success('success', [ 'count' => count($data), 'data' => $data, ]); } //获取热搜关键字 public function getHotSearchKeywords() { $data = Db::table('tb_search_keywords') ->order('created_at DESC') ->limit(5) ->select(); $this->success('success', $data); } // 根据CDKey获取游戏下载地址以及提取码 public function getGameDownloadForCDkey() { $cdkey = $this->request->post('cdkey'); if (!$cdkey) { $this->fail('请填写正确的cdkey!', 500); } // 校验CDKey是否存在以及CDkey状态 $row = Db::table('tb_cdkey')->where('cdkey', $cdkey)->find(); if (!$row) { $this->fail('cdkey不存在!', 500); } // if ($row['state'] != 0) { // $this->fail('cdkey已使用或已过期!', 500); // } // 校验通过 返回对应游戏下载地址与提取码以及更新CDkey状态 // 查询对应游戏信息 $game = Db::table('tb_game')->where('id', $row['game_id'])->find(); if (!$game) { $this->fail('对应游戏不存在!请与管理员联系!', 500); } // Db::table('tb_cdkey')->where('cdkey', $cdkey)->update(['state' => 1]); $data = ['download_url' => $game['download_url'], 'extracted_code' => $game['extracted_code']]; $this->success('success', $data); } //推荐游戏(最多只显示5个) public function recommendedGames() { $gameId = $this->request->post('gameId'); if (!is_numeric($gameId)) { $this->fail(500, 'gameId参数校验错误'); } //优先推荐优先级靠前的同类型游戏 $game = Db::table('tb_game') ->where(['id' => $gameId]) ->find(); if (!$game) { $this->fail(501, '找不到对应的gameId'); } // $categoryId = $game['id']; $categoryId = $game['category']; //只返回推荐关键信息,不要返回下载链接,提取码等重要信息 $data = Db::table('tb_game') ->field('id,category,title,image_url,price,created_at,updated_at') ->where(['category' => $categoryId]) ->order('priority ASC') ->limit(5) ->select(); $this->success('success', $data); } // 获取指定用户的库存游戏 public function getGameByUserId() { $page = $this->request->post('page'); // 当前页数 $list = $this->request->post('list'); // 每页记录数 $user_id = $this->request->post('user_id'); // 用户ID if (!is_numeric($user_id)){ $this->fail(500, '用户ID未找到'); } // 用户验证 $user = Db::table('tb_user')->where('id', $user_id)->find(); if (!$user) { $this->fail(500, '用户不存在!'); } // 查找用户绑定的游戏 $data = Db::table('tb_game_bind_record') ->alias('tgbr') ->where('tgbr.user_id', $user_id) ->join('tb_game tg', 'tgbr.game_id = tg.id') ->field('tgbr.*, tg.title, tg.image_url, tg.game_version, tg.browse, tg.language') ->paginate([ 'page' => $page, 'list_rows' => $list, ]); // foreach($data->getCollection()->toArray() as &$item) { // exit($item['avatar']); // $item['avatar'] = $this->getUserAvatar($item['avatar']); // } $this->success('success', $data); } }