Index.php 8.2 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\Config;
use think\Db;

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

    /**
     * @ApiTitle (获取状态)
     * @ApiMethod (POST)
     * @ApiReturn   ({"code":状态码,
    "msg":"提示信息"
    "time": "时间戳",
    "data": {
    "0": '个人排行榜已隐藏',
    "1": "个人排行榜已开启",
    }
    })
     */
    public function getStatus(){
        $config = Config::get('site')['status'];
        $this->success('是否隐藏个人排行榜',$config);
    }

    /**
     * @ApiTitle (首页)
     * @ApiMethod (POST)
     * @ApiReturn   ({"code":状态码,
    "msg":"提示信息"
    "time": "时间戳",
    "data": {
    "id": '学生id',
    "avatar": "头像",
    "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['avatar'] = cdnurl($sid['avatar'], true);
                $list['name'] = $sid['name'];
                $list['score_num'] = $v['sum_score'];
                $list['rank'] = $k + 1;
                break;
            }
        }
        if (empty($list)) {
            $res = \db('study')
                ->where('user_id', $userinfo['id'])
                ->field('avatar,name,grade,school,earn_score')
                ->find();
            $this->success('获取成功', $res);
        }
        $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]);
        }
        $model = new Banner();
        $data = collection($model->where('id', $id)
            ->order('createtime DESC')
            ->select())->toArray();
        $this->success('获取轮播图详情成功', $data);
    }

    /**
     * @ApiTitle (项目校区)
     * @ApiMethod (POST)
     * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码")
     * @ApiReturnParams   (name="msg", type="string", required=true, description="提示语")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ({
    "code":"状态码",
    "msg": "提示消息",
    "data": {}
    })
     */
    public function getSchoolItem()
    {
        $data = db('campus c')
            ->join('activity a','a.id = c.activity_id')
            ->join('school s','s.id = c.school_id')
            ->field('c.*,a.title as activity_name,s.title as school_name')
            ->select();
        if (empty($data)) {
            $this->error('当前还没有校区', ['status' => 2]);
        }
        $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": [
    {
    "title": "战队名称",
    "score": "战队总分",
    "team": 战队
    },
    ],
    "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')
            ->join('team t', 't.title = l.team')
            ->whereIn('l.campus_ids', $campus)
            ->field('t.title,t.score,l.team,l.campus_ids')
            ->order('t.score DESC')
            ->paginate($row, false, ['page' => $page])
            ->each(function ($item, $key) {
                $res = db('study_score_log')->where('campus_ids', $item['campus_ids'])->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')
            ->whereIn('campus_ids', $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);
    }
}