Index.php 6.8 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);
    }

    /**
     * 收藏操作
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    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');
    }

    /**
     * 判断距离是否满足条件
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function distance_where(){
        $param = $this->request->param();
        $validate = new \think\Validate([
            'user_id' => 'require',
            'longitude' => 'require',
            'latitude' => 'require',
            'goods_id' => 'require',
        ]);
        $validate->message([
            'user_id' => 'user_id参数错误',
            'longitude.require' => 'longitude参数错误!',
            'latitude.require' => 'latitude参数错误!',
            'goods_id.require' => 'goods_id参数错误!',
        ]);
        if (!$validate->check($param)) {
            $this->error($validate->getError());
        }
        $goods = Db::name('goods')->where(['id'=>$param['goods_id']])->find();
        $user = Db::name('user')->where(['id'=>$param['user_id']])->find();
        $user['age'] = getAge($user['birthday']);//年龄
        $distance = distance($goods,$param);//根据两地经纬度获取距离
        $is_distance = 1;
        //判断距离条件是否满足
        if($goods['distance'] != 0){
            if($distance > $goods['distance']){
                $is_distance = 0;
                $this->success('SUCCESS','',['is_where_satisfy'=>$is_distance]);
            }
        }
        //判断性别
        if($goods['sex'] != 0){
            if($user['gender'] == $goods['sex']){
                $is_distance = 0;
                $this->success('SUCCESS','',['is_where_satisfy'=>$is_distance]);
            }
        }
        //判断年龄是否满足
        if($goods['min_age'] != 0 || $goods['max_age'] != 0){
            if($goods['min_age'] <= $user['age'] && $user['age'] <= $goods['max_age']){
                $is_distance = 0;
                $this->success('SUCCESS','',['is_where_satisfy'=>$is_distance]);
            }
        }
        $this->success('SUCCESS','',['is_where_satisfy'=>$is_distance]);
    }



}