IndexController.php 14.4 KB
<?php
namespace api\home\controller;

use api\portal\model\PortalPostModel;
use cmf\controller\RestUserBaseController;
use think\composer\Plugin;
use Think\Db;
use api\home\service\AnswerService;
/**
 * @title 答题小程序
 * @description 欢迎使用在线接口文档
 */
class IndexController extends RestUserBaseController
{

    /**
     * @title 判断用户是否保存手机接口
     * @description 手机判断
     * @author WeiQiang
     * @url /home/index/userMobile
     * @method POST
     * @header name:XX-Token require:1 default: desc:token
     * @header name:XX-Device-Type require:1 default:wxapp desc:固定参数
     *
     * @return type:以保存手机返回true,未保存返回false
     */
    public function userMobile(){
        $id = $this->getUserId();
        $mobile = Db::name('user')->where('id',$id)->value('mobile');
        if(empty($mobile)){
            $this->success('SUCCESS',['type'=>false]);
        }
        $this->success('SUCCESS',['type'=>true]);
    }

    /**
     * @title 用户保存手机接口
     * @description 手机保存
     * @author WeiQiang
     * @url /home/index/saveMobile
     * @method POST
     * @header name:XX-Token require:1 default: desc:token
     * @header name:XX-Device-Type require:1 default:wxapp desc:固定参数
     * @param name:mobile type:string require:1 other: desc:用户手机
     * @return type:保存手机成功返回true,未保存成功返回false
     */
    public function saveMobile(){
        $id = $this->getUserId();
        $mobile = input('mobile');
        if(mb_strlen($mobile,'utf8') != 11 ){
            $this->error(['code'=>'40001','msg'=>'手机号码位数错误']);
        }
        if(Db::name('user')->where('id',$id)->update(['mobile'=>$mobile]) == true){
            $this->success('SUCCESS',['type'=>true]);
        }
        $this->success('SUCCESS',['type'=>false]);
    }

    /**
     * @title 优惠券展示接口
     * @description 优惠券
     * @author WeiQiang
     * @url /home/index/coupon
     * @method POST
     * @header name:XX-Token require:1 default: desc:token
     * @header name:XX-Device-Type require:1 default:wxapp desc:固定参数
     * @return title:优惠券名称
     * @return money:优惠券金额
     * @return time:有效期
     * @return content:优惠券信息
     */
    public function coupon(){
        $coupon = Db::name('coupon')->where('type',1)->where('delete_time',0)->find();
        $array['title']   = $coupon['title'];
        $array['money']   = $coupon['money'];
        $array['time']    = date('Y-m-d',$coupon['start_time']).' 至 '.date('Y-m-d',$coupon['end_time']);
        $array['content'] = $coupon['content'];
        $this->success('SUCCESS',$array);
    }

    /**
     * @title 优惠券领取结果接口
     * @description 优惠券
     * @author WeiQiang
     * @url /home/index/couponPost
     * @method POST
     * @header name:XX-Token require:1 default: desc:token
     * @header name:XX-Device-Type require:1 default:wxapp desc:固定参数
     * @return type:领取成功返回true,返回40002为已经领取,40001数据库写入错误
     * @return coupon_text:成功返回成功消息语
     */
    public function couponPost(){
        $user_id = $this->getUserId();
        $coupon = Db::name('coupon')->where('type',1)->where('delete_time',0)->find();
        $user_coupon = Db::name('user_coupon')->where('user_id',$user_id)->where('coupon_id',$coupon['id'])->count();
        if($user_coupon > 0){
            $this->error(['code'=>40002,'msg'=>'该优惠券一个用户仅限领取一次']);
        }
        if(Db::name('user_coupon')->insert([
                'user_id'     => $user_id,
                'coupon_id'   => $coupon['id'],
                'create_time' => time(),
                'delete_time' => 0,
                'type'        => 0
            ])
        == true){
            $coupon_text = Db::name('public_config')->where('id',1)->value('coupon_text');
            $this->success('SUCCESS',['type'=>true,'coupon_text'=>$coupon_text]);
        } else {
            $this->error(['code'=>40001,'msg'=>'系统繁忙请重试']);
        }
    }

    /**
     * @title 前七题列表接口
     * @description 答题
     * @author WeiQiang
     * @url /home/index/qualification
     * @method POST
     * @header name:XX-Token require:1 default: desc:token
     * @header name:XX-Device-Type require:1 default:wxapp desc:固定参数
     * @return list:题目列表@
     * @list id:题目id title:题目 content:选错提示 type:正确选项1为是0为否 order_id:题目排序
     */
    public function qualification(){
        $list = Db::name('qualification')
            ->field('id,title,content,type ,order_id')
            ->where('delete_time',0)
            ->order('order_id')
            ->select();
        $this->success('SUCCESS',['list'=>$list]);

    }

    /**
     * @title 答题错误提示语
     * @description 优惠券
     * @author WeiQiang
     * @url /home/index/qualificationPost
     * @method POST
     * @header name:XX-Token require:1 default: desc:token
     * @header name:XX-Device-Type require:1 default:wxapp desc:固定参数
     * @return qualification:提示语内容
     */
    public function qualificationPost(){
        $qualification = Db::name('public_config')->where('id',1)->value('qualification');
        $this->success('SUCCESS',['qualification'=>$qualification]);
    }

    /**
     * @title 后八题列表接口
     * @description 答题
     * @author WeiQiang
     * @url /home/index/question
     * @method POST
     * @header name:XX-Token require:1 default: desc:token
     * @header name:XX-Device-Type require:1 default:wxapp desc:固定参数
     * @return list:题目列表@
     * @list id:题目id title:题目 hint_type:是否有答题提示1为有0为没有 hint_text:答题提示标注 hint_title:答题提示标题 hint_content:答题提示内容 question:选项列表@ order_id:问题排序
     * @question id:选项列表 title:选项内容 order_id:选项排序
     */
    public function question(){
        $list = Db::name('question')->where('delete_time',0)->where('parent_id',0)->order('order_id')->select();
        $array = [];
        foreach($list as $l){
            $arr                 = [];
            $arr['id']           = $l['id'];
            $arr['title']        = $l['title'];
            $arr['hint_type']    = $l['hint_type'];
            $arr['hint_title']   = $l['hint_title'];
            $arr['hint_content'] = $l['hint_content'];
            $arr['hint_text']    = $l['hint_text'];
            $arr['order_id']     = $l['order_id'];
            $question = Db::name('question')
                ->field('id ,title ,order_id')
                ->where('delete_time',0)
                ->where('parent_id',$l['id'])
                ->order('order_id')
                ->select();
            $arr['question']     = $question;
            array_push($array,$arr);
        }
        $this->success('SUCCESS',['list'=>$array]);
    }

    /**
     * @title 后八题答题结果接口
     * @description 答题
     * @author WeiQiang
     * @url /home/index/questionPost
     * @method POST
     * @header name:XX-Token require:1 default: desc:token
     * @header name:XX-Device-Type require:1 default:wxapp desc:固定参数
     * @param name:questionStr type:string require:1 other:问题id-选项id,问题id-选项id desc:答题结果字符串
     * @return fen:分数区间
     * @return content:分数评价
     * @return type:是否显示评星1为显示
     * @return start:评星数量@
     * @return question:题目单独评价列表@
     * @start text:类型内容 num:评星数量 title:解释文字 type:1为知识产权2为科技成果转化情况3为研发组织管理水平4为财务增长情况
     * @question title:题目编号 content:单独评价题目内容
     * @return all_fen:总分
     */
    public function questionPost(){
        $question_str = input('questionStr');
        //结果采集

        $Answer = new AnswerService();
        $array = $Answer->answer($question_str);
        if($array == false){
            $this->error(['code'=>40001,'msg'=>'请完成所有题目']);
        }
        //保存结果
        $user_id = $this->getUserId();
        $user_answer = Db::name('user_answer')->where('user_id',$user_id)->find();
        if($user_answer){
            Db::name('user_answer')
                ->where('user_id',$user_id)
                ->update(['user_id'=>$user_id,'str'=>$question_str,'create_time'=>time()]);
        } else {
            Db::name('user_answer')->insert(['user_id'=>$user_id,'str'=>$question_str,'create_time'=>time()]);
        }
        $this->success('SUCCESS',$array);

    }

    /**
     * @title 个人资料接口
     * @description 个人中心
     * @author WeiQiang
     * @url /home/index/MyMeans
     * @method POST
     * @header name:XX-Token require:1 default: desc:token
     * @header name:XX-Device-Type require:1 default:wxapp desc:固定参数
     * @return avatar:头像
     * @return name:昵称
     * @return mobile:电话
     */
    public function MyMeans(){
        $user_id = $this->getUserId();
        $array = Db::name('user')->field('user_nickname ,avatar ,mobile')->where('id',$user_id)->find();
        $array['avatar'] = cmf_get_asset_url($array['avatar']);
        $this->success('SUCCESS',$array);
    }

    /**
     * @title 个人答题结果接口
     * @description 个人中心
     * @author WeiQiang
     * @url /home/index/MyAnswer
     * @method POST
     * @header name:XX-Token require:1 default: desc:token
     * @header name:XX-Device-Type require:1 default:wxapp desc:固定参数
     * @return answer:有结果返回true无结果返回false
     * @return fen:分数区间
     * @return content:分数评价
     * @return type:是否显示评星1为显示
     * @return start:评星数量@
     * @return question:题目单独评价列表@
     * @start text:类型内容 num:评星数量 title:解释文字 type:1为知识产权2为科技成果转化情况3为研发组织管理水平4为财务增长情况
     * @question title:题目编号 content:单独评价题目内容
     * @return all_fen:总分
     */
    public function MyAnswer(){
        $user_id = $this->getUserId();
        $str = Db::name('user_answer')->where('user_id',$user_id)->value('str');
        if(!$str){
            $this->success('SUCCESS',['type'=>false]);
        }
        //结果采集
        $Answer = new AnswerService();
        $array = $Answer->answer($str);
        $array['answer'] = true;
        $this->success('SUCCESS',$array);
    }

    /**
     * @title 我的优惠券接口
     * @description 个人中心
     * @author WeiQiang
     * @url /home/index/MyCoupon
     * @method POST
     * @header name:XX-Token require:1 default: desc:token
     * @header name:XX-Device-Type require:1 default:wxapp desc:固定参数
     *@param name:type type:int require:1 other:0为未使用,1为已使用,2为已过期 desc:优惠券类型
     * @return list:优惠券列表@
     * @list title:优惠券名称 start_time:开始时间 end_time:结束时间 money:金额 content:简介 type:优惠券状态0待使用1已使用2已过期
     */
    public function MyCoupon(){
        $type = input('type');
        $user_id = $this->getUserId();
        $list = Db::name('user_coupon')
            ->alias('uc')
            ->field('c.money ,c.title ,c.start_time ,c.end_time ,c.content ,uc.type')
            ->join('coupon c','uc.coupon_id = c.id','left')
            ->where('uc.user_id',$user_id)
            ->where('uc.delete_time',0)
            ->order('uc.id','DESC')
            ->select()
            ->each(function ($item){
                if($item['end_time'] < time()){
                    $item['type'] = 2;
                }
                $item['start_time'] = date('Y-m-d',$item['start_time']);
                $item['end_time']   = date('Y-m-d',$item['end_time']);
                return $item;
            });
        $this_list = [];
        foreach ($list as $l){
            if($l['type'] == $type){
                $this_list[] = $l;
            }
        }
        $this->success('SUCCESS',$this_list);
    }

    /**
     * @title 关于我们页面接口
     * @description 个人中心
     * @author WeiQiang
     * @url /home/index/AboutUs
     * @method POST
     * @header name:XX-Token require:1 default: desc:token
     * @header name:XX-Device-Type require:1 default:wxapp desc:固定参数
     * @return wechat_name:小程序名称
     * @return thumbnail:小程序头像
     * @return about:平台简介
     * @return qq:qq客服
     * @return mobile:客服电话
     * @return email:客服邮箱
     */
    public function AboutUs(){
        $list = Db::name('public_config')->where('id',1)->field('wechat_name,thumbnail,about,qq,mobile,email')->find();
        $list['thumbnail'] = cmf_get_asset_url($list['thumbnail']);
        $this->success('SUCCESS',$list);
    }

    /**
     * @title 意见反馈接口
     * @description 个人中心
     * @author WeiQiang
     * @url /home/index/tick
     * @method POST
     * @header name:XX-Token require:1 default: desc:token
     * @header name:XX-Device-Type require:1 default:wxapp desc:固定参数
     * @param name:content type:string require:1 other: desc:反馈内容
     * @return type:保存成功type返回1保存失败返回错误码40001
     */
    public function tick(){
        $content = input('content');
        $user_id = $this->getUserId();
        if(Db::name('tickling')->insert(['content'=>$content,'user_id'=>$user_id,'create_time'=>time(),'delete_time'=>0]) == true){
            $this->success('SUCCESS',['type'=>1]);
        } else {
            $this->error(['code'=>40001,'msg'=>'系统繁忙']);
        }
    }

    /**
     * @title 用户协议接口
     * @description 个人中心
     * @author WeiQiang
     * @url /home/index/category
     * @method POST
     * @header name:XX-Token require:1 default: desc:token
     * @header name:XX-Device-Type require:1 default:wxapp desc:固定参数
     * @return list:优惠券列表@
     * @list title:优惠券名称 start_time:开始时间 end_time:结束时间 money:金额 content:简介 status:优惠券状态0待使用1已使用2已过期
     */
    public function category(){
        $PortalPost = new PortalPostModel();
        $category = $PortalPost->where('id',32)->find();
        $content = $category['post_content'];
        $title   = $category['post_title'];
        $this->success('SUCCESS',['title'=>$title,'content'=>$content]);
    }


}