ActiveController.php 4.2 KB
<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
namespace app\portal\controller;

use app\portal\service\LikeService;
use cmf\controller\HomeBaseController;
use cmf\controller\WeChatBaseController;
use EasyWeChat\Foundation\Application;
use think\Db;

class ActiveController extends WeChatBaseController
{
    public function _initialize()
    {
        parent::_initialize();
        $this->checkWeChatUserLogin();

    }
    //活动首页
    public function active(){
        $banner=Db::name('active')->where('is_top',1)->field('thumbnail,id')->select();
        $this->assign('banner',$banner);
        $data=Db::name('active')->where(['status'=>1,'category_id'=>1])->select();
        $this->assign('list',$data);
        return $this->fetch();
    }
    //活动首页ajax
    public function activeAjax(){
        $where['status']=1;
        $status=$this->request->param('status');
        $where['category_id']=$status;
        $data=Db::name('active')->where($where)->select()->toArray();
        foreach ($data as $k => $v) {
            $data[$k]['url']=url('portal/active/activeDetail',array('id'=>$data[$k]['id']));
            $data[$k]['image'] = cmf_get_image_preview_url($data[$k]['thumbnail']);
            $data[$k]['active_time'] = date('Y-m-d',$data[$k]['active_time']);
            $data[$k]['end_time'] = date('Y-m-d',$data[$k]['end_time']);
        };
        return $data;
    }

    //活动详情
    public function activeDetail()
    {
        $re=Db::name('users')->where(['open_id'=>session('wechat_user')['id']])->find();
        if (!$re['mobile']){
            $like=0;
        }
        $id=$this->request->param('id');
        $data=Db::name('active')
            ->where('id',$id)
            ->find();
        $data['more']=json_decode($data['more'],true);
        $data['content']=cmf_replace_content_file_url(htmlspecialchars_decode($data['content']));
        if (!isset($like)) {
            $like = Db::name('like')->where(['cid' => $id, 'users_id' => $re['id'], 'status' => 2])->find();
            if ($like) {
                $like = 1;
            } else {
                $like = 0;
            }
        }
        $comment=Db::name('comment')
            ->alias('c')
            ->join('users u','c.users_id=u.id')
            ->join('brand b','c.cid=b.id')
            ->where(['b.id'=>$id,'c.status'=>2])
            ->field('c.*,u.*')
            ->select();
        $this->assign('comment',$comment);
        $this->assign('like',$like);
        $this->assign('list',$data);
        return $this->fetch('active_detail');
    }
    public function comment(){
        $id=$this->request->param('id');
        $this->assign('id',$id);
        return $this->fetch();
    }

    public function commentPost(){
        $re=Db::name('users')->where(['open_id'=>session('wechat_user')['id']])->find();
        if (!$re['mobile']){
            return 3;
        }
        $param=$this->request->param();
        $param['create_time']=time();
        $param['status']=2;
        $param['users_id']=$re['id'];
        $result=Db::name('comment')->where(['users_id'=>$param['users_id'],'status'=>2,'cid'=>$param['cid']])->find();
        if ($result){
            return 2;
        }
        $re1=Db::name('comment')->insert($param);
        $re2=Db::name('active')->where('id',$param['cid'])->setInc('comment',1);
        if ($re1 && $re2) {
            $url = url('portal/active/activeDetail', array('id' => $param['cid']));
            return $url;
        }else{
            return 0;
        }
    }

    //点赞ajax接口
    public function like(){
        $re=Db::name('users')->where(['open_id'=>session('wechat_user')['id']])->find();
        if (!$re['mobile']){
            return 3;
        }
        $param=$this->request->param();
        $like=new LikeService();
        return $like->like($param['id'],2,$re['id']);
    }


}