Game.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. namespace app\controller;
  3. use app\BaseController;
  4. use think\facade\Db;
  5. //游戏控制器
  6. class Game extends BaseController
  7. {
  8. protected $noNeedLogin = ['getCategory', 'getGamesByCategory', 'getGameById', 'searchGame', 'getHotSearchKeywords', 'recommendedGames'];
  9. //获取游戏分类
  10. public function getCategory()
  11. {
  12. $data = Db::table('tb_game_category')
  13. ->order('priority ASC')
  14. ->select();
  15. $this->success('success', $data);
  16. }
  17. //根据分类获取游戏
  18. public function getGamesByCategory()
  19. {
  20. $categoryId = $this->request->post('categoryId'); //分类ID
  21. $page = $this->request->post('page'); // 当前页码
  22. $pageNum = $this->request->post('pageNum'); //每页显示的数据条数
  23. if (!is_numeric($categoryId) || !is_numeric($pageNum)) {
  24. $this->fail(500, '参数校验错误');
  25. }
  26. $games = Db::table('tb_game')
  27. ->field('id,title,image_url,description,buy_count,browse,price')
  28. ->where(['category' => $categoryId])
  29. ->paginate([
  30. 'page' => $page,
  31. 'list_rows' => $pageNum
  32. ]);
  33. $this->success('success', $games);
  34. }
  35. //根据游戏ID获取游戏
  36. public function getGameById()
  37. {
  38. $gameId = $this->request->post('gameId');
  39. if (!is_numeric($gameId)) {
  40. $this->fail(500, 'gameId参数校验错误');
  41. }
  42. $data = Db::table('tb_game')
  43. ->where(['id' => $gameId])
  44. ->find();
  45. if (!$data) {
  46. $this->fail(500, 'gameId不存在');
  47. }
  48. //浏览次数+1
  49. Db::table('tb_game')
  50. ->where(['id' => $gameId])
  51. ->update([
  52. 'browse' => $data['browse'] + 1,
  53. ]);
  54. unset($data['download_url']);
  55. unset($data['extracted_code']);
  56. $this->success('success', $data);
  57. }
  58. //购买游戏
  59. public function buyGame()
  60. {
  61. $gameId = $this->request->post('gameId');
  62. if (!is_numeric($gameId)) {
  63. $this->fail(500, 'gameId参数校验错误');
  64. }
  65. $game = Db::table('tb_game')
  66. ->where(['id' => $gameId])
  67. ->find();
  68. if (!$game) {
  69. $this->fail(501, 'gameId不存在');
  70. }
  71. $user = Db::table('tb_user')
  72. ->where(['id' => $this->getUser()->user_id])
  73. ->find();
  74. if ($user['balance'] < $game['price']) {
  75. $this->fail(502, '余额不足无法购买');
  76. }
  77. //避免重复购买同一款游戏
  78. $userGame = Db::table('tb_user_game')
  79. ->where(['user_id' => $this->getUser()->user_id])
  80. ->where(['game_id' => $gameId])
  81. ->find();
  82. if ($userGame) {
  83. $this->fail(503, '您已经购买过此款游戏了');
  84. }
  85. //减少余额
  86. Db::table('tb_user')
  87. ->where(['id' => $this->getUser()->user_id])
  88. ->update(['balance' => $user['balance'] - $game['price']]);
  89. //购买到记录
  90. Db::table('tb_user_game')
  91. ->insert([
  92. 'user_id' => $this->getUser()->user_id,
  93. 'game_id' => $gameId,
  94. 'price' => $game['price'],
  95. 'created_at' => time(),
  96. 'channel' => 1, //站内直购
  97. ]);
  98. $this->success('success', null);
  99. }
  100. //获取我已经购买的游戏,包括cdkey兑换
  101. public function getMyBoughtGames()
  102. {
  103. $page = $this->request->post('page'); //页码
  104. $pageNum = $this->request->post('pageNum'); //每页显示的数据条数
  105. if (!is_numeric($pageNum) || !is_numeric($page)) {
  106. $this->fail(500, 'page或pageNum参数校验错误');
  107. }
  108. $data = Db::table('tb_user_game')
  109. ->alias('ug')
  110. ->join('tb_game g', 'ug.game_id = g.id')
  111. ->where(['ug.user_id' => $this->getUser()->user_id])
  112. ->field('ug.*,g.title as game_title,g.image_url as game_image_url,g.description as game_description')
  113. ->paginate([
  114. 'page' => $page,
  115. 'list_rows' => $pageNum,
  116. ]);
  117. $this->success('success', $data);
  118. }
  119. //电商渠道购买,兑换购买的游戏
  120. public function exchangeCdKeyGame()
  121. {
  122. $cdKey = $this->request->post('cdKey');
  123. if (empty($cdKey)) {
  124. $this->fail(500, 'cdKey参数校验错误');
  125. }
  126. $data = Db::table('tb_cdkey')
  127. ->where(['cdkey' => $cdKey])
  128. ->find();
  129. if (!$data) {
  130. $this->fail(501, 'cdKey不存在');
  131. }
  132. if ($data['state'] == 1) {
  133. $this->fail(502, 'cdKey已经使用过');
  134. }
  135. //如果过期了做一下更新
  136. if ($data['expired_at'] < time()) {
  137. Db::table('tb_cdkey')
  138. ->where(['id' => $data['id']])
  139. ->update(['state' => 2]);
  140. $data['state'] = 2;
  141. }
  142. if ($data['state'] == 2) {
  143. $this->fail(503, 'cdKey已经过期');
  144. }
  145. //使用cdKey
  146. Db::table('tb_cdkey')
  147. ->where(['id' => $data['id']])
  148. ->update([
  149. 'state' => 1,
  150. 'use_at' => time(),
  151. ]);
  152. //兑换购买渠道选择
  153. $channel = 2;
  154. if (str_starts_with($data['cdkey'], 'tb')) {
  155. $channel = 2;
  156. } elseif (str_starts_with($data['cdkey'], 'pdd')) {
  157. $channel = 3;
  158. }
  159. //获取游戏的价格
  160. $game = Db::table('tb_game')
  161. ->where(['id' => $data['game_id']])
  162. ->find();
  163. $price = $game['price'];
  164. //购买游戏
  165. Db::table('tb_user_game')
  166. ->insert([
  167. 'user_id' => $this->getUser()->user_id,
  168. 'game_id' => $data['game_id'],
  169. 'price' => $price,
  170. 'channel' => $channel,
  171. 'created_at' => time(),
  172. ]);
  173. $this->success('success', [
  174. 'gameId' => $data['game_id'],
  175. ]);
  176. }
  177. //获取我使用过的兑换码
  178. public function getMyUsedCdKey()
  179. {
  180. $pageNum = $this->request->post('pageNum'); //每页显示的数据条数
  181. if (!is_numeric($pageNum)) {
  182. $this->fail(500, 'pageNum参数校验错误');
  183. }
  184. $data = Db::table('tb_cdkey')
  185. ->where(['user_id' => $this->getUser()->user_id])
  186. ->where(['state' => 1])
  187. ->paginate($pageNum);
  188. $this->success('success', $data);
  189. }
  190. //搜索游戏
  191. public function searchGame()
  192. {
  193. $keywords = $this->request->post('keywords'); //关键字
  194. if (empty($keywords)) {
  195. $this->fail(500, '关键字不能为空');
  196. }
  197. if (strlen($keywords) >= 100) {
  198. $this->fail(501, '关键字长度超限');
  199. }
  200. $data = Db::table('tb_game')
  201. ->where('title', 'like', '%' . $keywords . '%')
  202. ->select();
  203. Db::table('tb_search_keywords')
  204. ->insert([
  205. 'keywords' => $keywords,
  206. 'created_at' => time(),
  207. ]);
  208. $this->success('success', [
  209. 'count' => count($data),
  210. 'data' => $data,
  211. ]);
  212. }
  213. //获取热搜关键字
  214. public function getHotSearchKeywords()
  215. {
  216. $data = Db::table('tb_search_keywords')
  217. ->order('created_at DESC')
  218. ->limit(5)
  219. ->select();
  220. $this->success('success', $data);
  221. }
  222. // 根据CDKey获取游戏下载地址以及提取码
  223. public function getGameDownloadForCDkey()
  224. {
  225. $cdkey = $this->request->post('cdkey');
  226. if (!$cdkey) {
  227. $this->fail('请填写正确的cdkey!', 500);
  228. }
  229. // 校验CDKey是否存在以及CDkey状态
  230. $row = Db::table('tb_cdkey')->where('cdkey', $cdkey)->find();
  231. if (!$row) {
  232. $this->fail('cdkey不存在!', 500);
  233. }
  234. // if ($row['state'] != 0) {
  235. // $this->fail('cdkey已使用或已过期!', 500);
  236. // }
  237. // 校验通过 返回对应游戏下载地址与提取码以及更新CDkey状态
  238. // 查询对应游戏信息
  239. $game = Db::table('tb_game')->where('id', $row['game_id'])->find();
  240. if (!$game) {
  241. $this->fail('对应游戏不存在!请与管理员联系!', 500);
  242. }
  243. // Db::table('tb_cdkey')->where('cdkey', $cdkey)->update(['state' => 1]);
  244. $data = ['download_url' => $game['download_url'], 'extracted_code' => $game['extracted_code']];
  245. $this->success('success', $data);
  246. }
  247. //推荐游戏(最多只显示5个)
  248. public function recommendedGames()
  249. {
  250. $gameId = $this->request->post('gameId');
  251. if (!is_numeric($gameId)) {
  252. $this->fail(500, 'gameId参数校验错误');
  253. }
  254. //优先推荐优先级靠前的同类型游戏
  255. $game = Db::table('tb_game')
  256. ->where(['id' => $gameId])
  257. ->find();
  258. if (!$game) {
  259. $this->fail(501, '找不到对应的gameId');
  260. }
  261. // $categoryId = $game['id'];
  262. $categoryId = $game['category'];
  263. //只返回推荐关键信息,不要返回下载链接,提取码等重要信息
  264. $data = Db::table('tb_game')
  265. ->field('id,category,title,image_url,price,created_at,updated_at')
  266. ->where(['category' => $categoryId])
  267. ->order('priority ASC')
  268. ->limit(5)
  269. ->select();
  270. $this->success('success', $data);
  271. }
  272. // 获取指定用户的库存游戏
  273. public function getGameByUserId()
  274. {
  275. $page = $this->request->post('page'); // 当前页数
  276. $list = $this->request->post('list'); // 每页记录数
  277. $user_id = $this->request->post('user_id'); // 用户ID
  278. if (!is_numeric($user_id)){
  279. $this->fail(500, '用户ID未找到');
  280. }
  281. // 用户验证
  282. $user = Db::table('tb_user')->where('id', $user_id)->find();
  283. if (!$user) {
  284. $this->fail(500, '用户不存在!');
  285. }
  286. // 查找用户绑定的游戏
  287. $data = Db::table('tb_game_bind_record')
  288. ->alias('tgbr')
  289. ->where('tgbr.user_id', $user_id)
  290. ->join('tb_game tg', 'tgbr.game_id = tg.id')
  291. ->field('tgbr.*, tg.title, tg.image_url, tg.game_version, tg.browse, tg.language')
  292. ->paginate([
  293. 'page' => $page,
  294. 'list_rows' => $list,
  295. ]);
  296. // foreach($data->getCollection()->toArray() as &$item) {
  297. // exit($item['avatar']);
  298. // $item['avatar'] = $this->getUserAvatar($item['avatar']);
  299. // }
  300. $this->success('success', $data);
  301. }
  302. }