Index.php 7.1 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 think\Db;

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

    /**
     * @ApiTitle (首页)
     * @ApiMethod (POST)
     * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
     * @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": "学生姓名",
    "active_study_score": 当前登录用户绑定的学生总分,
    "active_study_info": "当前登录用户绑定的学生"
    },
    ]
    })
     */
    public function index()
    {
        $campus = $this->request->param('campus');
        $row = $this->request->param('row');
        $page = $this->request->param('page');
        $row = $row ? $row : 10;
        $page = $page ? $page : 1;
        if (empty($campus)) {
            $this->error('参数错误', ['status' => 2]);
        }
        $userinfo = $this->auth->getUserinfo();
        $sid = \db('study')->where('user_id', $userinfo['id'])->value('id');
        //个人排行榜
        $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'] = db('study')->where('id', $item['study_id'])->value('name');
                return $item;
            });
        $data['active_study_score'] = \db('study_score_log')
            ->where(['study_id' => $sid, 'campus_id' => $campus])
            ->sum('score');
        $data['active_study_info'] = \db('study')->where('id', $sid)->value('name');
        $this->success('获取成功', [$data, 'status' => 1]);
    }

    /**
     * @ApiTitle (轮播跳转)
     * @ApiMethod (POST)
     * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
     * @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);
            if ($data[$k]['is_url'] == 1) {
                $data[$k]['url'] = cdnurl($v['url'], true);
            } elseif ($data[$k]['is_url'] == 0) {
                $v['url'] = null;
            }
        }
        $this->success('获取轮播图成功', [$data, 'status' => 1]);
    }

    /**
     * @ApiTitle (战队排行榜)
     * @ApiMethod (POST)
     * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
     * @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')
            ->order('t.score DESC')
            ->paginate($row, false, ['page' => $page]);
        //最终显示
        $this->success('战队排行榜', [$data, 'status' => 1]);
    }

    /**
     * @ApiTitle (个人排行榜)
     * @ApiMethod (POST)
     * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
     * @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'] = db('study')->where('id', $item['study_id'])->value('name');
                return $item;
            });
        //最终显示
        $this->success('个人排行榜', [$data, 'status' => 1]);
    }
}