<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Cookie;
use app\common\library\Auth;
use think\Db;
use fast\Random;
use think\Session;
use think\Cache;
use think\Validate;

/**
 * 各种列表以及详情接口
 */
class Lists extends Api
{
    protected $noNeedLogin = ['get_list','type_title','type_list','ke_list','index','policy','index_info','policy_info','video_list','video_detail','study','password_login','find_pwd','getcode','verify'];
    protected $noNeedRight = ['*'];

    /**
     * @ApiTitle    (通知公告列表)
     * @ApiSummary  (通知公告列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/lists/index)
     *
     * @ApiParams   (name="page", type="inter", required=false, description="当前页(默认1)")
     * @ApiParams   (name="pageNum", type="inter", required=false, description="每页显示数据个数(默认10)")
     * @ApiParams   (name="keyword", type="string", required=false, description="搜索关键字")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
          "total_num"://总条数
          "info":[
                "id"://ID
                "title"://标题
                "is_up"://是否上架0否1是
                 "file_name"://附件名称
                "createtime"://创建时间
                 "desc"://简介
          ]

    }
    })
     */
    public function index()
    {
        $keyword = $this->request->param('keyword');
        if(empty($keyword)){
            $where = true;
        }else{
            $where['title'] = ['like',"%$keyword%"];
        }
        $page = $this->request->param('page', 1, 'intval');
        $pageNum = $this->request->param('pageNum', 10, 'intval');

        $data['total_num'] = Db::name('information')
            ->where($where)
            ->count();

        $data['info'] = Db::name('information')
            ->field('id,title,is_up,desc,createtime')
            ->where($where)
            ->order('is_up desc,createtime desc')
            ->page($page,$pageNum)
            ->select();
        foreach ($data['info'] as &$v){
            $v['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
        }
        $this->success('success',$data);
    }

    /**
     * @ApiTitle    (搜索)
     * @ApiSummary  (搜索)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/lists/sou)
     *
     * @ApiParams   (name="page", type="inter", required=false, description="当前页(默认1)")
     * @ApiParams   (name="pageNum", type="inter", required=false, description="每页显示数据个数(默认10)")
     * @ApiParams   (name="keyword", type="string", required=false, description="搜索关键字")
     * @ApiParams   (name="type", type="inter", required=false, description="类型1通知公告2优选课程3培训课程")
     * @ApiParams   (name="id", type="inter", required=false, description="学习系统id")
     * 
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
    "total_num"://总条数
    "info":[

    ]

    }
    })
     */
    public function sou()
    {
        $qiniu = get_addon_config('qiniu')['cdnurl'];
        $user_id = $this->auth->id;
        $page = $this->request->param('page', 1, 'intval');
        $pageNum = $this->request->param('pageNum', 10, 'intval');

        $type = $this->request->param('type');
        if(empty($type)){
            $this->error('缺少必要参数');
        }

        $keyword = $this->request->param('keyword');
        if(empty($keyword)){
            $this->error('请输入关键字');
        }

        if($type == 1){
            $data['total_num'] = Db::name('information')
                ->where('title','like',"%$keyword%")
                ->count();

            $data['info'] = Db::name('information')
                ->field('id,title,is_up,desc,createtime')
                ->where('title','like',"%$keyword%")
                ->order('is_up desc,createtime desc')
                ->page($page,$pageNum)
                ->select();
            foreach ($data['info'] as &$v){
                $v['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
            }
            $this->success('success',$data);
        }elseif ($type == 2){
            $data['total_num'] = Db::name('optimization')
                ->where('title','like',"%$keyword%")
                ->count();

            $data['info'] = Db::name('optimization')
                ->where('title','like',"%$keyword%")
                ->order('createtime desc')
                ->page($page,$pageNum)
                ->select();
            foreach ($data['info'] as &$v){
                // 获取视频时长
                $video_avinfo = json_decode(file_get_contents(cdnurl($v['video_file'], true) . '?avinfo'), true); // 获取视频元信息
                $duration = ceil($video_avinfo['format']['duration']);
                $v['duration'] = gmdate('H:i:s',$duration);
                $v['image'] = $qiniu.$v['image'];
                $v['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
            }
            $this->success('success',$data);
        }elseif ($type == 3){
            $id = $this->request->param('id');
            if(empty($id)){
                $this->error('学习系统id不能为空');
            }

            $data['total_num'] = Db::name('classes')
                ->where('name','like',"%$keyword%")
                ->count();
            $data['info'] = Db::name('classes')
                ->field('createtime,updatetime',true)
                ->where('name','like',"%$keyword%")
                ->page($page,$pageNum)
                ->order('id desc')
                ->select();
            foreach ($data['info'] as &$v){
                $v['video_file'] = $qiniu.$v['video_file'];
                $video_info = json_decode(file_get_contents($v['video_file'] . '?avinfo'), true);
                $v['video_image'] = $this->get_video_first_image( $v['video_file'], $video_info);
            }
            $this->success('success',$data);
        }else{
            $this->error('类型错误');
        }
    }
    public function get_video_first_image($video_url,$video_info){
        if(empty($video_info['streams'][0]['width'])) {
            $width = $video_info['streams'][1]['width'];
            $height = $video_info['streams'][1]['height'];
        } else {
            $width = $video_info['streams'][0]['width'];
            $height = $video_info['streams'][0]['height'];
        }
        return $video_url.'?vframe/jpg/offset/1/w/'.$width.'/h/'.$height;
    }

    /**
     * @ApiTitle    (政策动态列表)
     * @ApiSummary  (政策动态列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/lists/policy)
     *
     * @ApiParams   (name="page", type="inter", required=false, description="当前页(默认1)")
     * @ApiParams   (name="pageNum", type="inter", required=false, description="每页显示数据个数(默认10)")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
    "total_num"://总条数
    "info":[
    "id"://ID
    "file_name"://附件名称
    "title"://标题
    "is_up"://是否上架0否1是
    "createtime"://创建时间
    ]
    }
    })
     */
    public function policy()
    {
        $page = $this->request->param('page', 1, 'intval');
        $pageNum = $this->request->param('pageNum', 10, 'intval');
        $data['total_num'] = Db::name('policy')
            ->count();
        $data['info'] = Db::name('policy')
            ->field('id,title,is_up,desc,createtime')
            ->order('is_up desc,createtime desc')
            ->page($page,$pageNum)
            ->select();
        foreach ($data['info'] as &$v){
            $v['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
        }
        $this->success('success',$data);
    }

    /**
     * @ApiTitle    (通知公告详情)
     * @ApiSummary  (通知公告详情)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/lists/index_info)
     *
     * @ApiParams   (name="id", type="int", required=true, description="公告ID")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
    "id"://ID
    "title"://标题
    "from"://转载于
    "content"://内容详情
    "file_name"://附件名称
    "file"://附件
    "is_up"://是否上架0否1是
    "createtime"://创建时间
    }
    })
     */
    public function index_info()
    {
        $qiniu = get_addon_config('qiniu')['cdnurl'];
        $id = $this->request->param('id');
        if(empty($id)){
            $this->error('缺少必要参数');
        }
        $data = Db::name('information')
            ->where('id',$id)
            ->field('updatetime',true)
            ->find();
        if(!empty($data['file'])){
            $base_url = $data['file'];
            $qiniu_url = $qiniu.$data['file'];
            $a = file_get_contents($qiniu_url);
            $path = './uploads/'.explode('/',$base_url)[2].'/';
            if(!file_exists($path)) {
                mkdir($path,0777,true);
            }
            file_put_contents('.'.$base_url,$a);
            $data['file'] = request()->domain().$base_url;
        }else{
            $data['file'] = '';
        }
        $data['createtime'] = date('Y-m-d H:i:s',$data['createtime']);
        $this->success('success',$data);
    }

    /**
     * @ApiTitle    (政策动态详情)
     * @ApiSummary  (政策动态详情)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/lists/policy_info)
     *
     * @ApiParams   (name="id", type="int", required=true, description="政策动态ID")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
    "id"://ID
    "title"://标题
    "from"://转载于
    "content"://内容详情
    "file_name"://附件名称
    "file"://附件
    "is_up"://是否上架0否1是
    "createtime"://创建时间
    }
    })
     */
    public function policy_info()
    {
        $qiniu = get_addon_config('qiniu')['cdnurl'];
        $id = $this->request->param('id');
        if(empty($id)){
            $this->error('缺少必要参数');
        }
        $data = Db::name('policy')
            ->where('id',$id)
            ->field('updatetime',true)
            ->find();
        if(!empty($data['file'])){
            $base_url = $data['file'];
            $qiniu_url = $qiniu.$data['file'];
            $a = file_get_contents($qiniu_url);
            $path = './uploads/'.explode('/',$base_url)[2].'/';
            if(!file_exists($path)) {
                mkdir($path,0777,true);
            }
            file_put_contents('.'.$base_url,$a);
            $data['file'] = request()->domain().$base_url;
        }else{
            $data['file'] = '';
        }
        $data['createtime'] = date('Y-m-d H:i:s',$data['createtime']);
        $this->success('success',$data);
    }

    /**
     * @ApiTitle    (优选课程列表)
     * @ApiSummary  (优选课程列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/lists/video_list)
     *
     * @ApiParams   (name="page", type="inter", required=false, description="当前页(默认1)")
     * @ApiParams   (name="pageNum", type="inter", required=false, description="每页显示数据个数(默认10)")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
          "total_num"://总条数
          "info":[
                 "id"://ID
                "title"://标题
                "desc"://简介
                "content"://内容详情
                "image"://图片
                "createtime"://创建时间
          ]
    }
    })
     */
    public function video_list()
    {
        $page = $this->request->param('page', 1, 'intval');
        $pageNum = $this->request->param('pageNum', 10, 'intval');
        $qiniu = get_addon_config('qiniu')['cdnurl'];
        $data['total_num'] = Db::name('optimization')
            ->count();
        $data['info'] = Db::name('optimization')
            ->field('id,title,image,desc,createtime,video_file')
            ->order('createtime desc')
            ->page($page,$pageNum)
            ->select();
        foreach ($data['info'] as &$v){
            // 获取视频时长
            $video_avinfo = json_decode(file_get_contents(cdnurl($v['video_file'], true) . '?avinfo'), true); // 获取视频元信息
            $duration = ceil($video_avinfo['format']['duration']);
            $v['duration'] = gmdate('H:i:s',$duration);
            $v['image'] = $qiniu.$v['image'];
            $v['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
        }
        $this->success('success',$data);
    }

    /**
     * @ApiTitle    (优选课程详情)
     * @ApiSummary  (优选课程详情)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/lists/video_detail)
     *
     * @ApiParams   (name="id", type="int", required=true, description="优选课程ID")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
    "id"://ID
    "title"://标题
    "desc"://简介
    "content"://内容详情
    "video_file"://视频信息
    "image"://图片
    "createtime"://创建时间
    }
    })
     */
    public function video_detail()
    {
        $id = $this->request->param('id');
        if(empty($id)){
            $this->error('缺少必要参数');
        }
        $qiniu = get_addon_config('qiniu')['cdnurl'];
        $data = Db::name('optimization')
            ->where('id',$id)
            ->find();
        $data['image'] = $qiniu.$data['image'];
        $data['video_file'] = $qiniu.$data['video_file'];

        // 获取视频时长
        $video_avinfo = json_decode(file_get_contents(cdnurl($data['video_file'], true) . '?avinfo'), true); // 获取视频元信息
        $duration = ceil($video_avinfo['format']['duration']);
        $data['duration'] = gmdate('H:i:s',$duration);

        $data['createtime'] = date('Y-m-d H:i:s',$data['createtime']);
        $this->success('success',$data);
    }

    /**
     * @ApiTitle    (学习系统)
     * @ApiSummary  (学习系统)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/lists/study)
     *
     *
     * @ApiParams   (name="page", type="inter", required=false, description="当前页(默认1)")
     * @ApiParams   (name="pageNum", type="inter", required=false, description="每页显示数据个数(默认10)")
     * @ApiParams   (name="type_id", type="inter", required=false, description="分类id")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
          "total_num"://总条数
          "info":[
                "id"://ID
                "examname"://考试名称
                "class_hour"://所需课时
                "finish_hour"://完成课时
                "image"://图片
                "expirationtime"://截止日期
          ]

    }
    })
     */
    public function study()
    {
        $type_id = $this->request->param('type_id');
        $erzi = Db::name('type')->where('id',$type_id)->field('id,pid')->select();
        if(!empty($erzi)){
            $erzi_id = array_column($erzi,"id");
            $sunzi = Db::name('type')->where(['pid'=>['in',$erzi_id]])->field("id")->select();
            $sunzi_id = array_column($sunzi,"id");
            $son = array_merge($sunzi_id,$erzi_id);
            array_push($son,$type_id);
            $where['type_id'] = ['in',$son];
        }else{
            $where['type_id'] = ['eq',$type_id];
        }
        $page = $this->request->param('page', 1, 'intval');
        $pageNum = $this->request->param('pageNum', 10, 'intval');
        $qiniu = get_addon_config('qiniu')['cdnurl'];
        $user_id = Session::get('user_id');
        $where['expirationtime'] = ['>',time()];
        $data['total_num'] = Db::name('study')
            ->where($where)
            ->count();
        $data['info'] = Db::name('study')
            ->field('updatetime,createtime',true)
            ->where($where)
            ->order('id desc')
            ->page($page,$pageNum)
            ->select();
        foreach ($data['info'] as &$v){
            if(empty($user_id)){
                $v['finish_hour'] = 0;
            }else{
                $finish_hour = Db::name('study_class')
                    ->alias('a')
                    ->join('classes b','a.class_id = b.id')
                    ->where('a.third_id',$user_id)
                    ->where('a.study_id',$v['id'])
                    ->where('a.status',2)
                    ->field('sum(b.class_hour) as finsh_hour')
                    ->find();
                if(empty($finish_hour['finsh_hour'])){
                    $v['finish_hour'] = 0;
                }else{
                    $v['finish_hour'] = $finish_hour['finsh_hour'];
                }
                
            }
            $v['image'] = $qiniu.$v['image'];
            $v['expirationtime'] = date('Y-m-d H:i:s',$v['expirationtime']);
        }
        $this->success('success',$data);
    }

    /**
     * @ApiTitle    (查看用户是否有权限)
     * @ApiSummary  (查看用户是否有权限)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/lists/study_detail)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="int", required=true, description="学习系统id")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
    }
    })
     */
    public function study_detail()
    {
        $user_id = $this->auth->id;
        $id = $this->request->param('id');
        if(empty($id)){
            $this->error('缺少必要参数');
        }
        $third_study = Db::name('third_study')->where('third_id',$user_id)->where('study_id',$id)->find();
        if(empty($third_study)){
            $this->error('您没有该场考试的权限');
        }else{
            $this->success('success');
        }
    }



    /**
     * @ApiTitle    (账号密码登录)
     * @ApiSummary  (账号密码登录)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/lists/password_login)
     *
     * @ApiParams   (name="username", type="string", required=true, description="用户名")
     * @ApiParams   (name="password", type="string", required=true, description="密码")
     * @ApiParams   (name="type", type="inter", required=false, description="是否记住账号密码 为空或者1否2是")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
    ]
    }
    })
     */
    public function password_login(){

        $type = $this->request->param('type');

        $qiniu = get_addon_config('qiniu')['cdnurl'];
        $username = $this->request->param('username');
        $password = $this->request->param('password');
        if(empty($username) || empty($password)){
            $this->error('缺少必要参数');
        }
        $result = Db::name('user')->where('username',$username)->find();
        if(empty($result)){
            $this->error('账号错误');
        }
        if($result['password'] != $password){
            $this->error('密码错误');
        }
        $user = Db::name('user')->where(['id'=>$result['id']])->find();
        if($user['expirationtime'] < time()){
            $this->error('抱歉,有效期已过');
        }
        if(!empty($user['image'])){
            $user['image'] = $qiniu.$user['image'];
        }else{
            $user['image'] = '';
        }
        if(empty($user['nickname'])){
            $user['nickname'] = '';
        }

        $auth =  Auth::instance();
        $this->auth->direct($user['id']);

        $userInfo = $auth->getUserinfo();
        $userInfo['image'] = $user['image'];
        unset($userInfo['avatar']);
        unset($userInfo['score']);
        unset($userInfo['mobile']);
        $this->success('SUCCESS',$userInfo);
    }

    /**
     * @ApiTitle    (找回密码)
     * @ApiSummary  (找回密码)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/lists/find_pwd)
     *
     * @ApiParams   (name="phone", type="string", required=true, description="手机号")
     * @ApiParams   (name="code", type="string", required=true, description="验证码")
     * @ApiParams   (name="new_password", type="string", required=true, description="新密码")
     * @ApiParams   (name="re_pwd", type="string", required=true, description="确认密码")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
    ]
    }
    })
     */
    public function find_pwd()
    {
        $phone = $this->request->param('phone');
        $code = $this->request->param('code');
        $new_password = $this->request->param('new_password');
        $re_pwd = $this->request->param('re_pwd');
        if(empty($phone) || empty($code) || empty($new_password) || empty($re_pwd)){
            $this->error('缺少必要参数');
        }

        if($new_password != $re_pwd){
            $this->error('两次密码不一致,请重新输入');
        }

        $result1 = Db::name('user')->where('phone',$phone)->find();
        if(empty($result1)){
            $this->error('该手机号暂无绑定账号');
        }

        //验证手机号验证码是否正确
        $yuancode = Cache::get($phone);
        if($code != $yuancode){
            $this->error('验证码错误或者失效,请重新发送');
        }

        $data = Db::name('user')->where('id',$result1['id'])->update(['password'=>$new_password]);
        if(empty($data)){
            $this->error('修改失败');
        }else{
            $this->success('success');
        }
    }

    /**
     * @ApiTitle    (修改密码)
     * @ApiSummary  (修改密码)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/lists/edit_pwd)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="pwd", type="string", required=true, description="密码")
     * @ApiParams   (name="new_password", type="string", required=true, description="新密码")
     * @ApiParams   (name="re_pwd", type="string", required=true, description="确认密码")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
    ]
    }
    })
     */
    public function edit_pwd()
    {
        $user_id = $this->auth->id;
        $username = $this->request->param('username');
        $pwd = $this->request->param('pwd');
        $new_password = $this->request->param('new_password');
        $re_pwd = $this->request->param('re_pwd');

        if(empty($username) || empty($pwd) || empty($new_password) || empty($re_pwd)){
            $this->error('缺少必要参数');
        }

        $userinfo = Db::name('user')->where('id',$user_id)->find();
        if($userinfo['password'] != $pwd){
            $this->error('您的密码不正确');
        }

        if($new_password != $re_pwd){
            $this->error('两次密码不一致');
        }
        $data = Db::name('user')->where('id',$user_id)->update(['password'=>$new_password]);
        if(empty($data)){
            $this->error('修改失败');
        }else{
            $this->success('修改成功');
        }
    }


    /**
     * @ApiTitle    (获取验证码)
     * @ApiSummary  (获取验证码)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/lists/getcode)
     *
     * @ApiParams   (name="mobile", type="string", required=false, description="手机号")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1553839125",
    "data": {

    }
    })
     */
    public function getcode()
    {
        $phone = $this->request->param('mobile');
        if (empty($phone)) {
            $this->error(['code' => 40005, 'msg' => '手机号不能为空']);
        }
        if (!preg_match('/^1[0-9]{10}$/', $phone)) {
            $this->error(['code' => 40005, 'msg' => "请输入正确的手机格式!"]);
        }
        //生成验证码
        $number = generateCode();
        //发送验证码
        $data = array(
            'content' => "【企智邦】提醒您,您的验证码是:" .$number.",十分钟之内有效,请勿向他人泄漏您的验证码",//短信内容
            'mobile' => $phone,//手机号码
            'productid' => '676767',//产品id
            'xh' => ''//小号
        );
        $result = send_sms($data);
        if (substr($result, 0, strpos($result, ',')) != "1") {
            $this->error(['code' => 42001, 'msg' => $result]);
        }
        Cache::set($phone,$number,600);
        $this->success('SUCCESS');
    }

    /**
     * @ApiTitle    (验证验证码)
     * @ApiSummary  (验证验证码)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/lists/verify)
     *
     * @ApiParams   (name="mobile", type="string", required=true, description="手机号")
     * @ApiParams   (name="code", type="string", required=true, description="验证码")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1553839125",
    "data": {

    }
    })
     */
    public function verify()
    {
        $param = $this->request->param();
        $validate = new Validate([
            'mobile' => 'require|max:11',
            'code'=>'require'
        ]);
        if (!$validate->check($param)) {
            $this->error(['code'=>40005,'msg'=>$validate->getError()]);
        }
        $code = Cache::get($param['mobile']);
        if($param['code'] == $code){
            $this->success('SUCCESS');
        }else{
            $this->error('验证码错误或者失效,请重新发送');
        }

    }


    /**
     * @ApiTitle    (客服列表)
     * @ApiSummary  (客服列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/lists/ke_list)
     *
     * @ApiParams   (name="page", type="inter", required=false, description="当前页(默认1)")
     * @ApiParams   (name="pageNum", type="inter", required=false, description="每页显示数据个数(默认10)")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
    "total_num"://总条数
    "info":[
    "id"://ID
    "phone2"://手机号2
    "phone1"://手机号1
    "image"://二维码
    "createtime"://创建时间
    ]

    }
    })
     */
    public function ke_list()
    {
        $page = $this->request->param('page', 1, 'intval');
        $pageNum = $this->request->param('pageNum', 10, 'intval');
        $qiniu = get_addon_config('qiniu')['cdnurl'];
        $data['total_num'] = Db::name('kelist')->count();
        $data['info'] = Db::name('kelist')->order('id desc')->field('updatetime',true)->page($page,$pageNum)->select();
        foreach ($data['info'] as &$v){
            $v['image'] = $qiniu.$v['image'];
            $v['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
        }
        $this->success('success',$data);
    }

    /**
     * @ApiTitle    (客服详情)
     * @ApiSummary  (客服详情)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/lists/ke_list_detail)
     *
     * @ApiParams   (name="id", type="inter", required=false, description="客服id")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
    "total_num"://总条数
    "info":[
    "id"://ID
    "phone2"://手机号2
    "phone1"://手机号1
    "image"://二维码
    "createtime"://创建时间
    ]

    }
    })
     */
    public function ke_list_detail()
    {
        $qiniu = get_addon_config('qiniu')['cdnurl'];
        $id = $this->request->param('id');
        if(empty($id)){
            $this->error('缺少必要参数');
        }
        $data = Db::name('kelist')->where('id',$id)->find();
        $data['image'] = $qiniu.$data['image'];
        $this->success('success',$data);
    }


    /**
     * @ApiTitle    (分类列表)
     * @ApiSummary  (分类列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/lists/type_list)
     *
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
    "one": [
    {
    "id": 1,
    "name": "学习类型",
    "pid": 0,
    "level": 1,
    "createtime": 1597834718,
    "updatetime": 1597834718
    }
    ],
    "two": [
    {
    "id": 2,
    "name": "理科类型",
    "pid": 1,
    "level": 2,
    "createtime": 1597834738,
    "updatetime": 1597834738
    }
    ],
    "three": [
    {
    "id": 3,
    "name": "测试",
    "pid": 2,
    "level": 3,
    "createtime": 1597835130,
    "updatetime": 1597835130
    }
    ]

    }
    })
     */
    public function type_list()
    {
        $data = Db::name('type')->select();
        $info['one'] = [];
        $info['two'] = [];
        $info['three'] = [];
        foreach ($data as $k=>$v){
            if($v['level'] == 1){
                array_push($info['one'],$data[$k]);
            }elseif ($v['level'] == 2){
                array_push($info['two'],$data[$k]);
            }else{
                array_push($info['three'],$data[$k]);
            }
        }
        $this->success('success',$info);
    }

    /**
     * @ApiTitle    (通过id获取分类列表)
     * @ApiSummary  (通过id获取分类列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/lists/get_list)
     *
     * @ApiParams   (name="id", type="inter", required=false, description="分类id")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
    }
    })
     */
    public function get_list()
    {
        $id = $this->request->param('id');
        if(empty($id)){
            $this->error('缺少必要参数');
        }

        $data = Db::name('type')->where('pid',$id)->select();
        $this->success('success',$data);
    }

    /**
     * @ApiTitle    (获取分类标题)
     * @ApiSummary  (获取分类标题)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/lists/type_title)
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
          "title_one"://标题1
          "title_two"://标题2
          "title_three"://标题3
    }
    })
     */
    public function type_title()
    {
        $data = Db::name('type_title')
            ->where('id',1)
            ->field('title_one,title_two,title_three')
            ->find();
        $this->success('success',$data);
    }

}