PrizeController.php 8.7 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 PrizeController extends WeChatBaseController
{
    protected $options;
    public function _initialize()
    {
        parent::_initialize();
        $this->checkWeChatUserLogin();
        $this->options = [
            'app_id'  => config('wechat_config.app_id'),
            'secret'  => config('wechat_config.secret'),
            'payment' => config('wechat_config.payment'),
        ];
    }
    //兑奖首页
    public function prize(){
        $re=$this->checkUser();
        $user=Db::name('users')->where('id',$re['id'])->find();
        $prize=Db::name('prize')->where('status',1)->select();
        $category=Db::name('prize_category')->select();
        $this->assign('user',$user);
        $this->assign('list',$prize);
        $this->assign('category',$category);
        return $this->fetch();
    }
    //兑奖首页ajax
    public function prizeAjax(){
        $this->checkUser();
        $category=$this->request->param('category');
        $where['status']=1;
        if (!empty($category)){
            $where['category_id']=$category;
        }
        $data=Db::name('prize')->where($where)->select()->toArray();
        foreach ($data as $k => $v) {
            $data[$k]['url']=url('portal/prize/prizeDetail',array('id'=>$data[$k]['id']));
            $data[$k]['image'] = cmf_get_image_preview_url($data[$k]['thumbnail']);
        };
        return $data;
    }

    //兑奖详情页
    public function prizeDetail(){
        $re=$this->checkUser();
        $id=$this->request->param('id');
        $data=Db::name('prize')
            ->where('id',$id)
            ->find();
        $data['more']=json_decode($data['more'],true);
        $data['content']=cmf_replace_content_file_url(htmlspecialchars_decode($data['content']));
        $like=Db::name('like')->where(['cid'=>$id,'users_id'=>$re['id'],'status'=>3])->find();
        if ($like){
            $like=1;
        }else{
            $like=0;
        }
        $order=Db::name('prize_order')->where(['prize_id'=>$id,'users_id'=>$re['id'],'status'=>0])->find();
        if ($order){
            $order=1;
        }else{
            $order=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'=>3])
            ->field('c.*,u.*')
            ->select();
        $this->assign('comment',$comment);
        $this->assign('order',$order);
        $this->assign('like',$like);
        $this->assign('list',$data);
        return $this->fetch('prize_detail');
    }
    //点赞ajax接口
    public function like(){
        $re=$this->checkUser();
        $param=$this->request->param();
        $like=new LikeService();
        return $like->like($param['id'],3,$re['id']);
    }
    public function comment(){
        $this->checkUser();
        $id=$this->request->param('id');
        $this->assign('id',$id);
        return $this->fetch();
    }

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

    //兑换奖品
    public function getPrize(){
        $re=$this->checkUser();
        $id=$this->request->param('id');
        if(Db::name('users')->where('id',$re['id'])->find()['score']<Db::name('prize')->where('id',$id)->find()['score']){
            return 0;
        }
        $data['prize_id']=$id;
        $data['users_id']=$re['id'];
        $data['create_time']=time();
        Db::name('prize_order')->insert($data);
        return 1;
    }

    public function getScore(){
        $app = new Application($this->options);
        $js = $app->js;
        $this->assign('js',$js);
        return $this->fetch('getScore');
    }


    //上传小票
    public function upload(){
        $options = [
            'app_id' => config('wechat_config.app_id'),
            'secret' => config('wechat_config.secret'),
            'payment' => config('wechat_config.payment'),
        ];
        $app = new Application($options);
        $js = $app->js;
        $jss = $js->config(['chooseImage', 'uploadImage', 'previewImage'], $debug = false, $beta = false, $json = true);
        $this->assign('js', $jss);
        return $this->fetch();
    }
    //上传小票提交
    public function uploadPost(){
        $param = $this->request->param();
        $options = [
            'app_id' => config('wechat_config.app_id'),
            'secret' => config('wechat_config.secret'),
            'payment' => config('wechat_config.payment'),
        ];
        $app = new Application($options);
     // 获取 access token 实例
        $access_token = $app->access_token;
        return $this->getmedia($access_token, $param['media'], date('Ymd'));
    }
    private function getmedia($access_token, $media_id, $foldername)
    {
        $url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=" . $access_token . "&media_id=" . $media_id;
        if (!file_exists("./upload/" . $foldername)) {
            mkdir("./upload/" . $foldername, 0777, true);
        }
        $file_name = date('YmdHis') . rand(1000, 9999) . '.jpg';
        $targetName = './upload/' . $foldername . '/' . $file_name;
        $saveName = $foldername . '/' . $file_name;
        $ch = curl_init($url); // 初始化
        $fp = fopen($targetName, 'wb'); // 打开写入
        curl_setopt($ch, CURLOPT_FILE, $fp); // 设置输出文件的位置,值是一个资源类型
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_exec($ch);
        curl_close($ch);
        fclose($fp);
        return $saveName;
    }

    //兑奖记录
    public function prizeRecord(){
        $status=$this->request->param('status');
        $where['p.status']=!empty($status)?$status:0;
        $data=Db::name('prize_order')
            ->alias('po')
            ->join('prize p'.'po.prize_id=p.id')
            ->where($where)
            ->select();
        $this->assign('list',$data);
        return $this->fetch();
    }




    //个人首页
    public function userIndex(){
        $last = Db::name('users_score_log')->where(['users_id' => session('wechat_user')['users_id'], 'action' => '签到'])->max('create_time');
        if (date('Ymd', time()) > date('Ymd', $last)) {
            $is_qiandao=0;
        }else{
            $is_qiandao=1;
        }
        $data=Db::name('users')->where('id',session('wechat_user')['users_id'])->find();
        $this->assign('list',$data);
        $this->assign('qiandao',$is_qiandao);
        return $this->fetch();
    }
    public function scoreRecord(){
        $score=Db::name('users')->where('id',session('wechat_user')['users_id'])->find()['score'];
        $data=Db::name('users_score_log')->where('users_id',session('wechat_user')['users_id'])->select();
        $this->assign('score',$score);
        $this->assign('list',$data);
        return $this->fetch();
    }
    //每日签到功能
    public  function  qiandao(){
        $last = Db::name('users_score_log')->where(['users_id' => session('wechat_user')['users_id'], 'action' => '签到'])->max('create_time');
        if (date('Ymd', time()) > date('Ymd', $last)) {
            $data['score'] = 10;
            $data['users_id'] = session('users_id');
            $data['create_time'] = time();
            $data['action'] = '签到';
            Db::name('users_score_log')->insert($data);
            Db::name('users')->where('id', session('wechat_user')['users_id'])->setInc('score', 10);
            return 1;
        } else {
            return 0;
        }
    }

}