Index.php 6.8 KB
<?php

namespace app\api\controller;

use app\api\model\Banner;
use app\api\model\Study;
use app\api\model\StudyScore;
use app\common\controller\Api;
use app\common\library\Auth;
use app\common\model\ScoreLog;
use think\Db;

/**
 * 首页接口
 * @ApiWeigh    (98)
 */
class Index extends Api
{
    protected $noNeedLogin = ['teamRank', 'personRank'];
    protected $noNeedRight = ['*'];

    /**
     * @ApiTitle (首页)
     * @ApiMethod (POST)
     * @ApiReturn   ({"code":状态码,
    "msg":"提示信息"
    "time": "时间戳",
    "data": {
    "id": '学生id',
    "name": "学生姓名",
    "score_num": "学生分数",
    "rank": "排名"
    }
    })
     */
    public function index()
    {
        $userinfo = $this->auth->getUserinfo();
        $bind = \db('user')->where('id',$userinfo['id'])->value('bind_study');
        if ($bind == 0){
            $this->error('空', ['status' => 2]);
        }
        $sid = \db('study')->where('user_id', $userinfo['id'])->find();
        $data = \db('study_score_log')
            ->field('study_id, SUM(score) as sum_score')
            ->group('study_id')->order('sum_score', 'desc')
            ->select();
        $list = [];
        foreach ($data as $k => $v) {
            if ($v['study_id'] == $sid['id']) {
                $list['id'] = $sid['id'];
                $list['name'] = $sid['name'];
                $list['score_num'] = $v['sum_score'];
                $list['rank'] = $k + 1;
                break;
            }
        }
        $this->success('获取成功', $list);
    }


    /**
     * @ApiTitle (获取轮播图)
     * @ApiMethod (POST)
     * @ApiReturn   ({"code":状态码,
    "msg":"提示信息"
    "time": "时间戳",
    "data": [
    "id": 轮播图id,
    "title": "轮播图标题",
    "images": "轮播图地址",
    "is_url": "是否为外链",
    "url": "链接地址",
    "createtime": “创建时间”,
    "updatetime": "更新时间",
    "is_url_text": "否",
    "createtime_text": "2023-03-07"
    })
     */
    public function getBanner()
    {
        $data = \db('banner')
            ->order('createtime DESC')
            ->select();
        foreach ($data as $k => $v) {
            $data[$k]['images'] = cdnurl($v['images'], true);
        }
        if (!$data) {
            $this->error('当前还没有轮播图', ['status' => 2]);
        }
        $this->success('获取轮播图成功', $data);
    }

    /**
     * @ApiTitle (轮播跳转)
     * @ApiMethod (POST)
     * @ApiParams   (name="id", type="string", required=true, description="轮播图id")
     * @ApiReturn   ({"code":状态码,
    "msg":"提示信息"
    "time": "时间戳",
    "data": [
    "id": 轮播图id,
    "title": "轮播图标题",
    "images": "轮播图地址",
    "is_url": "是否为外链",
    "url": "链接地址",
    "createtime": “创建时间”,
    "updatetime": "更新时间",
    "is_url_text": "否",
    "createtime_text": "2023-03-07"
    })
     */
    public function banner()
    {
        $id = $this->request->param('id');
        if (empty($id)) {
            $this->error('参数错误', ['status' => 2]);
        }
        $data = Banner::where('id', $id)
            ->order('createtime DESC')
            ->select();
        foreach ($data as $k => $v) {
            $data[$k]['images'] = cdnurl($v['images'], true);
        }
        $this->success('获取轮播图详情成功', $data);
    }

    /**
     * @ApiTitle (战队排行榜)
     * @ApiMethod (POST)
     * @ApiParams   (name="campus", type="string", required=true, description="校区id[必填]")
     * @ApiParams   (name="row", type="string", required=false, description="每页显示行数[非必填]")
     * @ApiParams   (name="page", type="string", required=false, description="当前页[非必填]")
     * @ApiReturn   ({"code":状态码,
    "msg":"提示信息"
    "time": "时间戳",
    "data": [
    "total": 所有数据,
    "per_page": 每页几行,
    "current_page": 当前页,
    "last_page": 最后一页,
    "data": [
    {
    "title": "战队名称",
    "score": "战队总分",
    "team_id": 战队id
    },
    ]
    },
    "status": 1
    })
     */
    public function teamRank()
    {
        $row = $this->request->param('row');
        $page = $this->request->param('page');
        $row = $row ? $row : 10;
        $page = $page ? $page : 1;
        $campus = $this->request->param('campus');
        if (empty($campus)) {
            $this->error('参数错误', ['status' => 2]);
        }
        $data = \db('study_score_log l')
            ->distinct('l.team_id')
            ->join('team t', 't.id = l.team_id')
            ->where('l.campus_id', $campus)
            ->field('t.title,t.score,l.team_id,l.campus_id')
            ->order('t.score DESC')
            ->paginate($row, false, ['page' => $page])
            ->each(function ($item,$key){
                $res = db('study_score_log')->where('campus_id',$item['campus_id'])->sum('score');
                $item['score'] = floor($item['score']);
                $item['percent'] = round(($item['score']*0.6) / $res * 100,2);
                return $item;
            });
        //最终显示
        $this->success('战队排行榜', $data);
    }

    /**
     * @ApiTitle (个人排行榜)
     * @ApiMethod (POST)
     * @ApiParams   (name="campus", type="string", required=true, description="校区id[必填]")
     * @ApiParams   (name="row", type="string", required=false, description="每页显示行数[非必填]")
     * @ApiParams   (name="page", type="string", required=false, description="当前页[非必填]")
     * @ApiReturn   ({"code":状态码,
    "msg":"提示信息"
    "time": "时间戳",
    "data": [
    "total": 所有数据,
    "per_page": 每页几行,
    "current_page": 当前页,
    "last_page": 最后一页,
    "data": {
    "id": 分数id,
    "sum_score": "总分",
    "study_id": '学生id',
    "study": "学生姓名",
    },
    ]
    })
     */
    public function personRank()
    {
        $campus = $this->request->param('campus');
        $row = $this->request->param('row');
        $page = $this->request->param('page');
        $user = $this->auth->getUserinfo();
        $row = $row ? $row : 10;
        $page = $page ? $page : 1;
        if (empty($campus)) {
            $this->error('参数错误', ['status' => 2]);
        }
        //个人排行榜
        $data = \db('study_score_log')
            ->field('id,SUM(score) as sum_score,study_id')
            ->where('campus_id', $campus)
            ->group('study_id')
            ->order('sum_score DESC')
            ->paginate($row, false, ['page' => $page])
            ->each(function ($item, $key) {
                $item['study_name'] = db('study')->where('id', $item['study_id'])->value('name');
                return $item;
            });
        //最终显示
        $this->success('个人排行榜', $data);
    }
}