| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- namespace app\controller;
- use app\BaseController;
- use think\facade\Db;
- //辅助下载控制器
- class Fuzhu extends BaseController
- {
- protected $noNeedLogin = ['search', 'getFuzhu'];
-
- //搜索辅助
- public function search()
- {
- $keywords = $this->request->post('keywords'); //搜索关键字
- if (empty($keywords)) {
- $this->fail(500, '关键字不能为空');
- }
- $data = Db::table('tb_fuzhu')
- ->where('title', 'like', '%' . $keywords . '%')
- ->select();
- $this->success('success', $data);
- }
-
- //获取辅助,需要支持分页
- public function getFuzhu()
- {
- $data = Db::table('tb_fuzhu')
- ->order('priority ASC')
- ->select();
- $this->success('success', $data);
- }
-
- //获取辅助详细信息,需登录
- public function detail()
- {
- $fuzhuId = $this->request->post('fuzhuId'); //辅助ID
- if (!is_numeric($fuzhuId)) {
- $this->fail(500, 'fuzhuId参数校验错误');
- }
- $data = Db::table('tb_fuzhu')
- ->where(['id' => $fuzhuId])
- ->find();
- if (!$data) {
- $this->fail(501, '找不到fuzhuId对应的辅助信息');
- }
- //投票数据赞踩百分比
- $totalVoteCount = Db::table('tb_fuzhu_vote')
- ->where(['fuzhu_id' => $fuzhuId])
- ->count();
- $totalUpCount = Db::table('tb_fuzhu_vote')
- ->where(['fuzhu_id' => $fuzhuId])
- ->where(['value' => 1])
- ->count();
- $totalDownCount = Db::table('tb_fuzhu_vote')
- ->where(['fuzhu_id' => $fuzhuId])
- ->where(['value' => 0])
- ->count();
- $upPercent = 0;
- $downPercent = 0;
- //被除数总投票次数不能为0,如果为0,投票百分比应当均为0
- if ($totalVoteCount != 0) {
- //需要转换为float计算保留2位小数,否则整数/整数=整数
- $upPercent = number_format(floatval($totalUpCount) / floatval($totalVoteCount) * 100, 2);
- $downPercent = number_format(floatval($totalDownCount) / floatval($totalVoteCount) * 100, 2);
- }
- $data['vote'] = [
- 'upPercent' => $upPercent, //赞投票百分比
- 'downPercent' => $downPercent, //踩投票百分比
- ];
- //下载次数
- $data['download_count'] = Db::table('tb_fuzhu_download')
- ->where(['fuzhu_id' => $fuzhuId])
- ->count();
- $this->success('success', $data);
- }
-
- //推荐的猜你喜欢辅助,按排序优先级推荐最多5个,(后面优化到用户行为推荐),需登录
- public function recommendedFuzhu()
- {
- $fuzhuId = $this->request->post('fuzhuId'); //辅助ID
- if (!is_numeric($fuzhuId)) {
- $this->fail(500, 'fuzhuId参数校验错误');
- }
- $data = Db::table('tb_fuzhu')
- ->order('priority ASC')
- ->limit(5)
- ->select();
- $this->success('success', $data);
- }
-
- //赞踩投票,需登录
- public function vote()
- {
- $fuzhuId = $this->request->post('fuzhuId'); //辅助ID
- $value = $this->request->post('value'); //投票值 1:赞 0:踩
- if (!is_numeric($fuzhuId) || !is_numeric($value)) {
- $this->fail(500, '参数校验错误');
- }
- $value = intval($value);
- if (!in_array($value, [0, 1])) {
- $this->fail(500, '参数校验错误');
- }
- $fuzhu = Db::table('tb_fuzhu')
- ->where(['id' => $fuzhuId])
- ->find();
- if (!$fuzhu) {
- $this->fail(501, '辅助ID不存在');
- }
- //检测是否已经投过票
- $fuzhuVote = Db::table('tb_fuzhu_vote')
- ->where(['fuzhu_id' => $fuzhuId])
- ->where(['user_id' => $this->getUser()->user_id])
- ->find();
- if ($fuzhuVote) {
- $this->fail(502, '您已经投过票');
- }
- Db::table('tb_fuzhu_vote')
- ->insert([
- 'user_id' => $this->getUser()->user_id,
- 'fuzhu_id' => $fuzhuId,
- 'value' => $value,
- 'created_at' => time(),
- ]);
- $this->success('success', null);
- }
-
- //下载辅助
- public function download()
- {
- $gameId = $this->request->post('gameId');
- if (!is_numeric($gameId)) {
- $this->fail(500, 'gameId参数校验错误');
- }
- Db::table('tb_fuzhu_download')
- ->insert([
- 'fuzhu_id' => $fuzhuId,
- 'user_id' => $this->getUser()->user_id,
- 'created_at' => time(),
- ]);
- $this->success('success', null);
- }
- }
|