Index.php 4.1 KB
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/18
 * Time: 11:03
 */
namespace app\home\controller;

use app\common\controller\WechatBase;
use app\home\model\Goods;
use app\home\model\Slide;
use think\Db;

class Index extends WechatBase
{
    protected $user_id;
    function _initialize()
    {
        parent::_initialize();
        //判断是否授权
        $user_id = get_current_user_id();
        if(empty($user_id)){
            $this->redirect('user/authorization_view');
        }
        $this->user_id = $user_id;
    }
    public function index(){
        $domain_name = $this->request->domain();//域名
        //幻灯片
        $slideModel = new Slide();
        $slide = $slideModel->selectData([]);
        foreach($slide as $key => $s){
            $s[$key]['image'] = $domain_name.$s['image'];
        }
        //广告信息
        $user_id = get_current_user_id();
        $goods_ids = [];
        if(!empty($user_id)){
            //获取用户答对了哪些广告(拿过广告的积分就不要展示在列表里了)
            $user_exp_log = Db::name('user_exp_log')->where(['user_id'=>$user_id,'type'=>4])->select();
            foreach($user_exp_log as $key => $value){
                $goods_ids[] = $value['goods_id'];
            }
        }
        $goodsModel = new Goods();
        $data = $goodsModel->selectPageData(['id'=>['not in',$goods_ids]],1,5);
        foreach($data as $key => $vo){
            if(!empty($vo['video'])){
                $data[$key]['video'] = $domain_name.$vo['video'];
            }
            $voice = explode(',',$vo['voice']);
            if(!empty($voice)){
                foreach($voice as $key2 => $vo2){
                    $voice[$key2] = $domain_name.$vo2;
                }
                $data[$key]['voice'] = $voice;
            }
            $data[$key]['start_time'] = date('Y-m-d',$vo['start_time']);
            $data[$key]['end_time'] = date('Y-m-d',$vo['end_time']);
            $images = explode(',',$vo['images']);
            if(!empty($images)){
                foreach($images as $key3 => $vo3){
                    $images[$key3] = $domain_name.$vo3;
                }
                $data[$key]['images'] = $images;
            }
        }
        $this->assign('slide',$slide);
        $this->assign('data',$data);
        $this->assign('title','广告商城');
        return $this->fetch();
    }

    /**
     * 加载更多
     */
    public function more(){
        $domain_name = $this->request->domain();//域名
        $user_id = $this->request->param('user_id',0,'intval');
        $page = $this->request->param('page',1,'intval');
        $pageNum = $this->request->param('pageNum',5,'intval');
        $goods_ids = [];
        if(!empty($user_id)){
            //获取用户答对了哪些广告(拿过广告的积分就不要展示在列表里了)
            $user_exp_log = Db::name('user_exp_log')->where(['user_id'=>$user_id,'type'=>4])->select();
            foreach($user_exp_log as $key => $value){
                $goods_ids[] = $value['goods_id'];
            }
        }
        $goodsModel = new Goods();
        $data = $goodsModel->selectPageData(['goods_id'=>['not in',$goods_ids]],$page,$pageNum);
        foreach($data as $key => $vo){
            if(!empty($vo['video'])){
                $data[$key]['video'] = $domain_name.$vo['video'];
            }
            $voice = explode(',',$vo['voice']);
            if(!empty($voice)){
                foreach($voice as $key2 => $vo2){
                    $voice[$key2] = $domain_name.$vo2;
                }
                $data[$key]['voice'] = $voice;
            }
            $data[$key]['start_time'] = date('Y-m-d',$vo['start_time']);
            $data[$key]['end_time'] = date('Y-m-d',$vo['end_time']);
            $images = explode(',',$vo['images']);
            if(!empty($images)){
                foreach($images as $key3 => $vo3){
                    $images[$key3] = $domain_name.$vo3;
                }
                $data[$key]['images'] = $images;
            }
        }
        $this->success('SUCCESS','',$data);
    }



}