Index.php 4.6 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
{
    public function index(){
        $domain_name = $this->request->domain();//域名
        //幻灯片
        $slideModel = new Slide();
        $slide = $slideModel->selectData([]);
        $slide = collection($slide)->toArray();
        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(['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);
    }

    public function collect(){
        $type = $this->request->param('type',0,'intval');
        $goods_id = $this->request->param('goods_id',0,'intval');
        $user_id = $this->request->param('user_id',0,'intval');
        if(empty($type) || empty($goods_id) || empty($user_id)){
            $this->error('404');
        }
        if($type == 1){
            //收藏
            if(empty($collect)){
                $arr['user_id'] = $user_id;
                $arr['goods_id'] = $goods_id;
                $arr['createtime'] = time();
                Db::name('collect')->insert($arr);
            }
        }else if($type == 2){
            //取消收藏
            Db::name('collect')->where(['user_id'=>$user_id,'goods_id'=>$goods_id])->delete();
        }
        $this->success('SUCCESS');
    }



}