Index.php
8.5 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
<?php
namespace app\mobile\controller;
use app\common\controller\Api;
use app\mobile\model\IndexBanner;
use app\mobile\model\News;
use app\mobile\model\Exam;
use app\mobile\model\UserExam;
use app\mobile\model\CompanyUser;
use app\mobile\model\Message;
/**
* 首页接口
* @ApiWeigh (99)
*/
class Index extends Api
{
protected $noNeedLogin = ['index','examList','newsList','newsInfo'];
protected $noNeedRight = ['*'];
public function _initialize()
{
parent::_initialize();
}
/**
* @ApiTitle (首页)
* @ApiSummary (首页)
* @ApiMethod (POST)
*
* @ApiHeaders (name=token, type=string, required=false, description="请求的Token")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1599026313",
"data": {
"exam_list": [{ //二级考试列表
"id": 3, //ID
"name": "", //考试名称
"nickname": "", //昵称
"countdown": 26 //距离考试倒计时天数
}],
"banner_list": [{ //轮播图列表
"id": 1, //ID
"image": "https://img02.mockplus.cn/idoc/sketch/2020-08-06/006324c3-e5d3-4206-be48-52a7c7468e74.7C1FC40C-CED5-4B08-9506-3E48A2732B2C.png", //图片地址
"link": "https://img02.mockplus.cn/idoc/sketch/2020-08-06/006324c3-e5d3-4206-be48-52a7c7468e74.7C1FC40C-CED5-4B08-9506-3E48A2732B2C.png" //链接地址
}],
"exam": { //考试
"id": 3, //ID
"name": "", //考试名称
"nickname": "", //昵称
"countdown": 26 //距离考试倒计时天数
},
"message": { //消息
"id": 7, //ID
"title": "这是个消息", //标题
}
}
})
*/
public function index()
{
$user_id = $this->auth->id;
$exam_id_arr = UserExam::where('user_id',$user_id)->column('exam_id');
if($user_id > 0 && !empty($exam_id_arr)){
$exam_list = Exam::where('id','in',$exam_id_arr)
->field('id,name,nickname,exam_time')
->order('exam_time asc')
->select();
foreach ($exam_list as &$v) {
$countdown = ceil(($v['exam_time']-time())/86400);
$v['countdown'] = $countdown > 0 ? $countdown : 0;
}
$exam = $exam_list[0];
}else{
$exam_list = [];
$exam = Exam::field('id,name,nickname,exam_time')
->order('exam_time asc')
->find();
$countdown = ceil(($exam['exam_time']-time())/86400);
$exam['countdown'] = $countdown > 0 ? $countdown : 0;
}
// 轮播图
$banner_list = IndexBanner::order('createtime desc')->field('id,image,url')->select();
// 消息
$message = Message::where('user_id',$this->auth->id)->where('is_read','0')->field('id,title')->find();
$message = $message ?: [];
$this->success('成功',compact('exam_list','banner_list','exam','message'));
}
/**
* @ApiWeigh (87)
* @ApiTitle (选择参加的考试)
* @ApiSummary (选择参加的考试)
* @ApiMethod (POST)
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1599018234",
"data": [{
"id": 1, //一级ID
"pid": 0,
"name": "建筑工程", //一级名称
"nickname": "", //一级昵称
"children": [{ //二级
"id": 3, //二级ID
"pid": 1, //父ID
"name": "一级建造师", //二级名称
"nickname": "" //二级昵称
}]
}]
})
*/
public function examList()
{
$list = Exam::where('pid',0)->field('id,pid,name,nickname')->select();
foreach($list as &$v){
$v['children'] = Exam::where('pid',$v['id'])->field('id,pid,name,nickname')->select();
}
$this->success('成功', $list);
}
/**
* @ApiTitle (选择考试)
* @ApiSummary (选择考试)
* @ApiMethod (POST)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="exam_ids", type="string", required=true, description="考试ID,多个用英文逗号隔开")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1599026313",
"data": null
})
*/
public function selectExam()
{
$user_id = $this->auth->id;
$exam_ids = $this->request->param('exam_ids');
empty($exam_ids) && $this->error('请选择考试');
UserExam::where('user_id',$user_id)->delete();
$add_data = [];
foreach (explode(',', $exam_ids) as $exam_id) {
$add_data[] = ['user_id'=>$user_id,'exam_id'=>$exam_id];
}
(new UserExam)->saveAll($add_data);
$this->success('选择考试成功');
}
/**
* @ApiTitle (报考资讯)
* @ApiSummary (报考资讯)
* @ApiMethod (POST)
*
* @ApiParams (name="page", type="inter", required=false, description="当前页(默认1)")
* @ApiParams (name="page_num", type="inter", required=false, description="每页显示数据个数(默认10)")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1599026840",
"data": {
"id": 1, //资讯ID
"title": "关于建造师", //资讯标题
"content": "关于建造师内容", //资讯内容
"createtime": "1970.01.01", //发布时间
"read_num": 120 //阅读量
}
})
*/
public function newsList()
{
$page = $this->request->param('page', 1, 'intval');
$page_num = $this->request->param('page_num', 10, 'intval');
$data = News::order('createtime desc')
->paginate($page_num,false,['page'=>$page])
->each(function($v){
$v->visible(['id','cover','title','read_num','createtime']);
})->toArray();
$this->success('成功',['total'=>$data['total'],'list'=>$data['data']]);
}
/**
* @ApiTitle (资讯详情)
* @ApiSummary (资讯详情)
* @ApiMethod (POST)
*
* @ApiParams (name="news_id", type="int", required=true, description="资讯ID")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1599026840",
"data": {
"id": 1, //资讯ID
"title": "关于建造师", //资讯标题
"content": "关于建造师内容", //资讯内容
"createtime": "1970.01.01", //发布时间
"read_num": 120 //阅读量
}
})
*/
public function newsInfo()
{
$news_id = $this->request->param('news_id');
empty($news_id) && $this->error('缺少必要参数');
$info = News::get($news_id);
empty($info) && $this->error('资讯信息不存在');
$info = $info->visible(['id','title','createtime','content']);
// 阅读量加1
News::where('id',$news_id)->setInc('read_num_real');
$this->success('成功',$info);
}
/**
* @ApiTitle (扫描企业二维码-加入企业)
* @ApiSummary (扫描企业二维码-加入企业)
* @ApiMethod (POST)
*
* @ApiParams (name="company_id", type="int", required=true, description="公司ID")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1599026840",
"data": null
})
*/
public function joinCompanyInviteQrcode()
{
$user = $this->auth->getUser();
$company_id = $this->request->param('company_id');
// 验证申请状态
$company_user = CompanyUser::where('company_id',$company_id)
->where('user_id',$user['id'])
->field('status')
->find();
if($company_user){
if($company_user['status'] == '0'){
$company_user->status = '1';
$company_user->save();
}
if($company_user['status'] == '1'){
$this->error('加入企业成功,请勿重复提交申请');
}
}
CompanyUser::create([
'user_id' => $this->auth->id,
'company_id' => $company_id,
'name' => $user['nickname'],
'mobile' => $user['mobile'],
'status' => '1'
]);
$this->success('加入企业成功');
}
}