Fuzhu.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace app\controller;
  3. use app\BaseController;
  4. use think\facade\Db;
  5. //辅助下载控制器
  6. class Fuzhu extends BaseController
  7. {
  8. protected $noNeedLogin = ['search', 'getFuzhu'];
  9. //搜索辅助
  10. public function search()
  11. {
  12. $keywords = $this->request->post('keywords'); //搜索关键字
  13. if (empty($keywords)) {
  14. $this->fail(500, '关键字不能为空');
  15. }
  16. $data = Db::table('tb_fuzhu')
  17. ->where('title', 'like', '%' . $keywords . '%')
  18. ->select();
  19. $this->success('success', $data);
  20. }
  21. //获取辅助,需要支持分页
  22. public function getFuzhu()
  23. {
  24. $data = Db::table('tb_fuzhu')
  25. ->order('priority ASC')
  26. ->select();
  27. $this->success('success', $data);
  28. }
  29. //获取辅助详细信息,需登录
  30. public function detail()
  31. {
  32. $fuzhuId = $this->request->post('fuzhuId'); //辅助ID
  33. if (!is_numeric($fuzhuId)) {
  34. $this->fail(500, 'fuzhuId参数校验错误');
  35. }
  36. $data = Db::table('tb_fuzhu')
  37. ->where(['id' => $fuzhuId])
  38. ->find();
  39. if (!$data) {
  40. $this->fail(501, '找不到fuzhuId对应的辅助信息');
  41. }
  42. //投票数据赞踩百分比
  43. $totalVoteCount = Db::table('tb_fuzhu_vote')
  44. ->where(['fuzhu_id' => $fuzhuId])
  45. ->count();
  46. $totalUpCount = Db::table('tb_fuzhu_vote')
  47. ->where(['fuzhu_id' => $fuzhuId])
  48. ->where(['value' => 1])
  49. ->count();
  50. $totalDownCount = Db::table('tb_fuzhu_vote')
  51. ->where(['fuzhu_id' => $fuzhuId])
  52. ->where(['value' => 0])
  53. ->count();
  54. $upPercent = 0;
  55. $downPercent = 0;
  56. //被除数总投票次数不能为0,如果为0,投票百分比应当均为0
  57. if ($totalVoteCount != 0) {
  58. //需要转换为float计算保留2位小数,否则整数/整数=整数
  59. $upPercent = number_format(floatval($totalUpCount) / floatval($totalVoteCount) * 100, 2);
  60. $downPercent = number_format(floatval($totalDownCount) / floatval($totalVoteCount) * 100, 2);
  61. }
  62. $data['vote'] = [
  63. 'upPercent' => $upPercent, //赞投票百分比
  64. 'downPercent' => $downPercent, //踩投票百分比
  65. ];
  66. //下载次数
  67. $data['download_count'] = Db::table('tb_fuzhu_download')
  68. ->where(['fuzhu_id' => $fuzhuId])
  69. ->count();
  70. $this->success('success', $data);
  71. }
  72. //推荐的猜你喜欢辅助,按排序优先级推荐最多5个,(后面优化到用户行为推荐),需登录
  73. public function recommendedFuzhu()
  74. {
  75. $fuzhuId = $this->request->post('fuzhuId'); //辅助ID
  76. if (!is_numeric($fuzhuId)) {
  77. $this->fail(500, 'fuzhuId参数校验错误');
  78. }
  79. $data = Db::table('tb_fuzhu')
  80. ->order('priority ASC')
  81. ->limit(5)
  82. ->select();
  83. $this->success('success', $data);
  84. }
  85. //赞踩投票,需登录
  86. public function vote()
  87. {
  88. $fuzhuId = $this->request->post('fuzhuId'); //辅助ID
  89. $value = $this->request->post('value'); //投票值 1:赞 0:踩
  90. if (!is_numeric($fuzhuId) || !is_numeric($value)) {
  91. $this->fail(500, '参数校验错误');
  92. }
  93. $value = intval($value);
  94. if (!in_array($value, [0, 1])) {
  95. $this->fail(500, '参数校验错误');
  96. }
  97. $fuzhu = Db::table('tb_fuzhu')
  98. ->where(['id' => $fuzhuId])
  99. ->find();
  100. if (!$fuzhu) {
  101. $this->fail(501, '辅助ID不存在');
  102. }
  103. //检测是否已经投过票
  104. $fuzhuVote = Db::table('tb_fuzhu_vote')
  105. ->where(['fuzhu_id' => $fuzhuId])
  106. ->where(['user_id' => $this->getUser()->user_id])
  107. ->find();
  108. if ($fuzhuVote) {
  109. $this->fail(502, '您已经投过票');
  110. }
  111. Db::table('tb_fuzhu_vote')
  112. ->insert([
  113. 'user_id' => $this->getUser()->user_id,
  114. 'fuzhu_id' => $fuzhuId,
  115. 'value' => $value,
  116. 'created_at' => time(),
  117. ]);
  118. $this->success('success', null);
  119. }
  120. //下载辅助
  121. public function download()
  122. {
  123. $gameId = $this->request->post('gameId');
  124. if (!is_numeric($gameId)) {
  125. $this->fail(500, 'gameId参数校验错误');
  126. }
  127. Db::table('tb_fuzhu_download')
  128. ->insert([
  129. 'fuzhu_id' => $fuzhuId,
  130. 'user_id' => $this->getUser()->user_id,
  131. 'created_at' => time(),
  132. ]);
  133. $this->success('success', null);
  134. }
  135. }