Index.php 12.9 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 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
<?php
namespace app\mobile\controller;

use app\common\controller\Api;
use app\mobile\model\IndexBanner;
use app\mobile\model\IndexCategory;
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','wxLoginSwitch','questionnaireSwitch'];
    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", //图片地址
                "type": 1, //跳转类型:1=无跳转,2=精品课程,3=报考咨询
                "course_id": 1, //精品课程ID,当type为 2 时,跳转到指定课程
                "news_id": 1, //报考咨询ID,当type为 3 时,跳转到指定报考咨询
            }],
            "exam": { //考试
                "id": 3, //ID
                "name": "", //考试名称
                "nickname": "", //昵称
                "countdown": 26 //距离考试倒计时天数
            },
            "message": { //消息
                "id": 7, //ID
                "title": "这是个消息", //标题
            },
            "category_list": [{ // 首页固定分类
                "id": 1, //ID
                "name": "全能题库", //名称
                "image": "https://img02.mockplus.cn/idoc/sketch/2020-08-06/04bee097-d5ae-4a43-9c69-339bb5157d37.0BC4FEFE-A2AB-4DAF-96BF-2DF245CD2173.png" //图片地址
            }]
        }
    })
     */
    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();
        }else{
            $exam_list = Exam::alias('a')
                ->join('mobile_exam b','b.id = a.pid')
                ->where('a.pid','>',0)
                ->field('a.id,CONCAT(b.nickname,a.nickname) name,a.nickname,a.exam_time')
                ->order('a.exam_time asc')
                ->limit(1)
                ->select();
        }
        foreach ($exam_list as &$v) {
            $countdown = strtotime(date('Y-m-d',$v['exam_time'])) - strtotime(date('Y-m-d'));
            $countdown = $countdown/86400;
            $v['countdown'] = $countdown > 0 ? $countdown : 0;
        }
        $exam = $exam_list[0];
        // 轮播图
        $banner_list = IndexBanner::order('createtime desc')->field('id,image,type,course_id,news_id')->select();
        // 消息
        $message = Message::where('user_id',$this->auth->id)->where('is_read','0')->field('id,title')->find();
        $message = $message ?: (Object)[];
        // 首页固定分类
        $category_list = IndexCategory::field('id,name,image')->select();
        $this->success('成功',compact('exam_list','banner_list','exam','message','category_list'));
    }

    /**
     * @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)")
     * @ApiParams   (name="type", type="string", required=false, description="类型:1=报考咨询,2=政策信息,3=通知公告,4=行业资讯")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1599026840",
        "data": {
            "id": 1, //资讯ID
            "title": "关于建造师", //资讯标题
            "content": "关于建造师内容", //资讯内容
            "createtime": "1970.01.01", //发布时间
            "read_num": 120 //阅读量
            "is_top": 1 //是否置顶:0=否,1=是
        }
    })
     */
    public function newsList()
    {
        $page = $this->request->param('page', 1, 'intval');
        $page_num = $this->request->param('page_num', 10, 'intval');
        $type = $this->request->param('type');
        $where = [];
        if($type){
            $where['type'] = $type;
        }
        $data = News::where($where)
            ->order(['is_top' => 'desc','top_time' => 'desc', 'createtime' => 'desc'])
            ->paginate($page_num,false,['page'=>$page])
            ->each(function($v){
                $v->visible(['id','cover','title','read_num','createtime','is_top']);
            })->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'],
                'phone' => $user['mobile'],
                'status' => '1'
            ]);
            // 新增员工记录
            CompanyUserLog::create([
                'company_id' => $company_id,
                'user_id' => $user['id'],
                'name' => $user['nickname'],
                'phone' => $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]);
    }

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

    /**
     * @ApiTitle    (问卷开关)
     * @ApiSummary  (问卷开关)
     * @ApiMethod   (POST)
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1599017563",
        "data": {
            "is_on": 0 //问卷开关:0=关闭,1=开启
        }
    })
     */
    public function questionnaireSwitch()
    {
        $this->success('成功', ['is_on' => Db::name('mobile_config')->where('id',1)->value('questionnaire_switch')]);
    }
}