Index.php 10.9 KB
<?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\CompanyUser;
use app\mobile\model\CompanyUserLog;
use app\mobile\model\Message;
use app\mobile\model\Statistic;
use think\Db;

/**
 * 首页接口
 * @ApiWeigh (99)
 */
class Index extends Api
{
	protected $noNeedLogin = ['index','examList','newsList','newsInfo','startupPage','agreement','jobSwitch'];
    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 = $this->auth->getUser();
        if($user && !empty($user['mobile_exam_ids'])){
            // 别名的组合CONCAT(b.nickname,a.nickname) name
            $exam_list = Exam::alias('a')
                ->join('mobile_exam b','b.id = a.pid')
                ->where('a.id','in',$user['mobile_exam_ids'])
                ->field('a.id,CONCAT(b.nickname,a.nickname) name,a.nickname,a.exam_time')
                ->order('a.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 ?: (Object)[];
        $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 = $this->auth->getUser();
        $exam_ids = $this->request->param('exam_ids');
        empty($exam_ids) && $this->error('请选择考试');
        $user->mobile_exam_ids = $exam_ids;
        $user->save();
        $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 && $company_user['status'] == '1'){
            $this->error('加入企业成功,请勿重复提交申请');
        }
        Db::startTrans();
        try {
            if($company_user){
                $company_user->status = '1';
                $company_user->save();
            }
            CompanyUser::create([
                'user_id' => $user['id'],
                'company_id' => $company_id,
                'name' => $user['nickname'],
                'mobile' => $user['mobile'],
                'status' => '1'
            ]);
            // 新增员工记录
            CompanyUserLog::create([
                'company_id' => $company_id,
                'user_id' => $user['id'],
                'name' => $user['nickname'],
                'mobile' => $user['mobile'],
                'type' => '1'
            ]);
            Db::commit();
        } catch (PDOException $e) {
            Db::rollback();
            $this->error($e->getMessage());
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('加入企业成功');
    }

    /**
     * @ApiTitle    (启动页)
     * @ApiSummary  (启动页)
     * @ApiMethod   (POST)
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1606398386",
        "data": {
            "url": "http://qizhibang.brotop.cn/uploads/20200811/b9bbd60dcacbcb649579155f63b62558.png" //启动页图片地址
        }
    })
     */
    public function startupPage()
    {
        $url = Db::name('mobile_config')->where('id',1)->value('startup_page');
        $url = cdnurl($url,true);
        // 记录每日启动次数
        $statistic = Statistic::where('today',date('Y-m-d'))->find();
        if($statistic){
            $statistic->save(['startup_times' => $statistic->startup_times+1]);
        }else{
            (new Statistic)->save([
                'startup_times' => 1,
                'today' => date('Y-m-d')
            ]);
        }
        $this->success('成功',compact('url'));
    }

    /**
     * @ApiTitle    (启动页用户协议及隐私协议)
     * @ApiSummary  (启动页用户协议及隐私协议)
     * @ApiMethod   (POST)
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1599017563",
        "data": "启动页用户协议及隐私协议内容" //协议内容
    })
     */
    public function agreement()
    {
        $content = Db::name('mobile_config')->where('id',1)->value('agreement');
        $this->success('成功', $content);
    }

    /**
     * @ApiTitle    (职位开关)
     * @ApiSummary  (职位开关)
     * @ApiMethod   (POST)
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1599017563",
        "data": {
            "is_on": 0 //是否开启职位:0=否,1=是
        }
    })
     */
    public function jobSwitch()
    {
        $this->success('成功', ['is_on' => 0]);
    }
}