Index.php
8.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?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);
}
}