Goods.php 3.5 KB
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/23
 * Time: 11:47
 */

namespace app\home\controller;


use app\common\controller\WechatBase;
use app\home\model\Question;
use app\home\model\User;
use think\Db;

class Goods extends WechatBase
{
    protected $user_id;
    function _initialize()
    {
        parent::_initialize();
        //判断是否授权
        $user_id = 3;//get_current_user_id();
        if(empty($user_id)){
            $goods_id = $this->request->param('goods_id',0,'intval');
            $target_url = rawurlencode(url('',array('goods_id'=>$goods_id),false,true));
            $this->redirect('user/authorization_view',array('target_url'=>$target_url));
        }
        $this->user_id = $user_id;
    }

    public function detail(){
        $goods_id = $this->request->param('goods_id',0,'intval');
        if(empty($goods_id)){
            $this->error('404');
        }
        $where['id'] = ['eq',$goods_id];
        $goodsModel = new \app\home\model\Goods();
        $data = $goodsModel->findData($where);
        if(empty($data)){
            $this->error('404');
        }
        if($data['status'] == 1){
            $this->error('广告审核中');
        }
        if($data['status'] == 3){
            $this->error('广告审核未通过');
        }
        if($data['admin_deletetime'] != 0){
            $this->error('已下架');
        }
        if($data['user_deletetime'] != 0){
            $this->error('已下架');
        }
        if($data['admin_is_up'] != 1){
            $this->error('已下架');
        }
        if($data['user_is_up'] != 1){
            $this->error('已下架');
        }
        //判断是否收藏
        $collect = Db::name('collect')->where(['user_id'=>$this->user_id,'goods_id'=>$goods_id])->find();
        if(!empty($collect)){
            $is_collect = 1;
        }else{
            $is_collect = 0;
        }
        $data['is_collect'] = $is_collect;
        $userModel = new User();
        $user = $userModel->findData(['id'=>$this->user_id]);
        if(empty($user)){
            $this->error('查无此人');
        }
        //判断用户是否答过题
        $is_answer = Db::name('user_question_answer')->where(['goods_id'=>$goods_id,'user_id'=>$this->user_id])->find();
        if(empty($is_answer)){
            $data['is_answer'] = 0;//未答过
        }else{
            $data['is_answer'] = 1;//答过题
        }
        //数据回显
        $question = json_decode($data['question'],true);
        foreach($question as $key => $q){
            $user_question_answer = Db::name('user_question_answer')->where(['user_id'=>$this->user_id,'goods_id'=>$goods_id,'question_key'=>$key])->find();
            $question[$key]['answer'] = $user_question_answer;
            //判断答题是否全部正确

        }
        $data['question'] = $question;
        //判断用户是否完成答题条件
        if(empty($user['mobile']) || empty($user['name']) || empty($user['card'])){
            $user['is_satisfy'] = 0;//不满足
        }else{
            $user['is_satisfy'] = 1;//满足
        }
        $this->assign('user',$user);
        $this->assign('data',$data);
        if($data['type'] == 1){
            return $this->fetch('video_detail');
        }else if($data['type'] == 2){
            return $this->fetch('audio_detail');
        }else if($data['type'] == 3){
            return $this->fetch('content_detail');
        }else{
            $this->error('未知类型');
        }
    }
}