审查视图

application/home/controller/Goods.php 4.9 KB
王晓刚 authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?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;
王晓刚 authored
15
use EasyWeChat\Foundation\Application;
王晓刚 authored
16 17 18 19 20 21 22 23 24
use think\Db;

class Goods extends WechatBase
{
    protected $user_id;
    function _initialize()
    {
        parent::_initialize();
        //判断是否授权
王晓刚 authored
25
        $user_id = get_current_user_id();
王晓刚 authored
26
        if(empty($user_id)){
王晓刚 authored
27 28
            $goods_id = $this->request->param('goods_id',0,'intval');
            $target_url = rawurlencode(url('',array('goods_id'=>$goods_id),false,true));
王晓刚 authored
29
            $this->redirect('user/authorization_view',array('target_url'=>$target_url));
王晓刚 authored
30 31 32 33 34
        }
        $this->user_id = $user_id;
    }

    public function detail(){
王晓刚 authored
35
        $domain_name = $this->request->domain();//域名
王晓刚 authored
36 37 38 39 40 41 42
        $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);
王晓刚 authored
43 44 45
        if(!empty($data['video'])){
            $data['video'] = $domain_name.$data['video'];
        }
王晓刚 authored
46
        if(!empty($data['voice'])){
王晓刚 authored
47 48 49
            $voice = explode(',',$data['voice']);
            foreach($voice as $key => $v){
                $voice[$key] = $domain_name.$v;
王晓刚 authored
50
            }
王晓刚 authored
51
            $data['voice'] = $voice;
王晓刚 authored
52
        }
王晓刚 authored
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
        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('查无此人');
        }
王晓刚 authored
87 88 89 90
        //判断用户是否答过题
        $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;//未答过
王晓刚 authored
91
            $is_correct = 0;
王晓刚 authored
92 93
        }else{
            $data['is_answer'] = 1;//答过题
王晓刚 authored
94 95 96 97 98 99 100
            //判断是否全部正确
            $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;//未全部正确
            }
王晓刚 authored
101
        }
王晓刚 authored
102
        $data['is_correct'] = $is_correct;
王晓刚 authored
103 104 105 106 107 108
        //数据回显
        $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;
        }
王晓刚 authored
109 110 111 112
        //判断是否处于重新答题倒计时状态
        $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;
王晓刚 authored
113
        $data['question'] = $question;
王晓刚 authored
114 115
        //判断用户是否完成答题条件
        if(empty($user['mobile']) || empty($user['name']) || empty($user['card'])){
王晓刚 authored
116
            $user['is_satisfy'] = 0;//不满足
王晓刚 authored
117
        }else{
王晓刚 authored
118
            $user['is_satisfy'] = 1;//满足
王晓刚 authored
119 120
        }
        $this->assign('user',$user);
王晓刚 authored
121
        $this->assign('data',$data);
王晓刚 authored
122 123 124 125 126 127
        $options = [
            'app_id' => config('wechat.app_id'),
            'secret' => config('wechat.secret'),
        ];
        $app = new Application($options);
        $js = $app->js;
王晓刚 authored
128
        $jssdk = $js->config(['getLocation'], $debug = false, $beta = false, $json = true);
王晓刚 authored
129
        $this->assign('jssdk',$jssdk);
王晓刚 authored
130 131 132 133 134 135 136 137 138
        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('未知类型');
        }
王晓刚 authored
139 140
    }
}