<?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('加入企业成功'); } }