Goods.php 4.9 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 EasyWeChat\Foundation\Application;
use think\Db;

class Goods extends WechatBase
{
    protected $user_id;
    function _initialize()
    {
        parent::_initialize();
        //判断是否授权
        $user_id = 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(){
        $domain_name = $this->request->domain();//域名
        $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['video'])){
            $data['video'] = $domain_name.$data['video'];
        }
        if(!empty($data['voice'])){
            $voice = explode(',',$data['voice']);
            foreach($voice as $key => $v){
                $voice[$key] = $domain_name.$v;
            }
            $data['voice'] = $voice;
        }
        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;//未答过
            $is_correct = 0;
        }else{
            $data['is_answer'] = 1;//答过题
            //判断是否全部正确
            $correct = Db::name('user_question_answer')->where(['goods_id'=>$goods_id,'user_id'=>$this->user_id,'is_correct'=>"0"])->find();
            if(empty($correct)){
                $is_correct = 1;//全部正确
            }else{
                $is_correct = 0;//未全部正确
            }
        }
        $data['is_correct'] = $is_correct;
        //数据回显
        $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;
        }
        //判断是否处于重新答题倒计时状态
        $question_error = Db::name('user_question_answer')->where(['user_id'=>$this->user_id,'goods_id'=>$goods_id])->find();
        $time = !empty($question_error['resettime']) ? $question_error['resettime']-time() : 0;
        $data['time'] = $time;
        $data['question'] = $question;
        //判断用户是否完成答题条件
        if(empty($user['gender']) || empty($user['birthday']) || empty($user['latitude']) || empty($user['longitude'])){
            $user['is_satisfy'] = 0;//不满足
        }else{
            $user['is_satisfy'] = 1;//满足
        }
        $this->assign('user',$user);
        $this->assign('data',$data);
        $options = [
            'app_id' => config('wechat.app_id'),
            'secret' => config('wechat.secret'),
        ];
        $app = new Application($options);
        $js = $app->js;
        $jssdk = $js->config(['getLocation'], $debug = false, $beta = false, $json = true);
        $this->assign('jssdk',$jssdk);
        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('未知类型');
        }
    }
}