|  | @@ -0,0 +1,509 @@
 | 
	
		
			
				|  |  | +<?php
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +namespace app\api\controller;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +use app\common\controller\Api;
 | 
	
		
			
				|  |  | +use think\Db;
 | 
	
		
			
				|  |  | +use app\common\library\Keyworld;
 | 
	
		
			
				|  |  | +/**
 | 
	
		
			
				|  |  | + * 圈子动态
 | 
	
		
			
				|  |  | + */
 | 
	
		
			
				|  |  | +class Topicdongtai extends Api
 | 
	
		
			
				|  |  | +{
 | 
	
		
			
				|  |  | +    protected $noNeedLogin = ['info','floor_info','topic_list'];
 | 
	
		
			
				|  |  | +    protected $noNeedRight = ['*'];
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    //发布动态
 | 
	
		
			
				|  |  | +    public function addone(){
 | 
	
		
			
				|  |  | +        $content = input('content','');
 | 
	
		
			
				|  |  | +        $images = input('images','');
 | 
	
		
			
				|  |  | +        $topic_id = input('topic_id','');
 | 
	
		
			
				|  |  | +        if(!$content && !$images){
 | 
	
		
			
				|  |  | +            $this->error(__('Invalid parameters'));
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        //关键字替换
 | 
	
		
			
				|  |  | +        //$content = Keyworld::sensitive($content);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        $data = [
 | 
	
		
			
				|  |  | +            'topic_id' => $topic_id,
 | 
	
		
			
				|  |  | +            'user_id' => $this->auth->id,
 | 
	
		
			
				|  |  | +            'content' => $content,
 | 
	
		
			
				|  |  | +            'images' => $images,
 | 
	
		
			
				|  |  | +            'type'  => input('type',1),
 | 
	
		
			
				|  |  | +            'createtime' => time(),
 | 
	
		
			
				|  |  | +            'updatetime' => time(),
 | 
	
		
			
				|  |  | +        ];
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        Db::startTrans();
 | 
	
		
			
				|  |  | +        $id = Db::name('topic_dongtai')->insertGetId($data);
 | 
	
		
			
				|  |  | +        //圈子新增一个贴
 | 
	
		
			
				|  |  | +        $rs = Db::name('topic_hub')->where('id',$topic_id)->setInc('t_number');
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        Db::commit();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        $this->success('发布成功',$id);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    //自己看列表
 | 
	
		
			
				|  |  | +    //某用户的帖子列表
 | 
	
		
			
				|  |  | +    public function my_lists(){
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        $uid = input('uid',$this->auth->id);
 | 
	
		
			
				|  |  | +        $list = Db::name('topic_dongtai')->alias('dt')
 | 
	
		
			
				|  |  | +            ->join('user','dt.user_id = user.id','LEFT')
 | 
	
		
			
				|  |  | +            ->field('dt.*,user.nickname,user.avatar')
 | 
	
		
			
				|  |  | +            ->where('dt.user_id',$uid)
 | 
	
		
			
				|  |  | +            ->order('dt.id desc')->autopage()->select();
 | 
	
		
			
				|  |  | +        $list = list_domain_image($list,['images','avatar']);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        if(!empty($list)){
 | 
	
		
			
				|  |  | +            foreach($list as $key => &$val){
 | 
	
		
			
				|  |  | +                $val['isgood'] = $this->is_good($val['id'],$this->auth->id);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                //创建视频缩略图
 | 
	
		
			
				|  |  | +                $val['images_thumb'] = '';
 | 
	
		
			
				|  |  | +                if ($val['type'] == 2) {
 | 
	
		
			
				|  |  | +                    $images_url = explode('.', $val['images']);
 | 
	
		
			
				|  |  | +                    unset($images_url[count($images_url) - 1]);
 | 
	
		
			
				|  |  | +                    $val['images_thumb'] = join('.', $images_url) . '_0.jpg';
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        $this->success('success',$list);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    //是否点赞
 | 
	
		
			
				|  |  | +    private function is_good($dt_id,$uid){
 | 
	
		
			
				|  |  | +        $where = [
 | 
	
		
			
				|  |  | +            'dt_id' => $dt_id,
 | 
	
		
			
				|  |  | +            'user_id'  => $uid,
 | 
	
		
			
				|  |  | +        ];
 | 
	
		
			
				|  |  | +        $check = Db::name('topic_dongtai_good')->where($where)->find();
 | 
	
		
			
				|  |  | +        if($check){
 | 
	
		
			
				|  |  | +            return 1;
 | 
	
		
			
				|  |  | +        }else{
 | 
	
		
			
				|  |  | +            return 0;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    //详情
 | 
	
		
			
				|  |  | +    public function info(){
 | 
	
		
			
				|  |  | +        $id = input('id');
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        $info = Db::name('topic_dongtai')->alias('dt')
 | 
	
		
			
				|  |  | +            ->join('user','dt.user_id = user.id','LEFT')
 | 
	
		
			
				|  |  | +            ->field('dt.*,user.nickname,user.avatar')
 | 
	
		
			
				|  |  | +            ->where('dt.id',$id)->find();
 | 
	
		
			
				|  |  | +        $info = info_domain_image($info,['images','avatar']);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        //是否点赞过
 | 
	
		
			
				|  |  | +        if($info){
 | 
	
		
			
				|  |  | +            $info['isgood'] = $this->is_good($id,$this->auth->id);
 | 
	
		
			
				|  |  | +            //创建视频缩略图
 | 
	
		
			
				|  |  | +            $info['images_thumb'] = '';
 | 
	
		
			
				|  |  | +            if ($info['type'] == 2) {
 | 
	
		
			
				|  |  | +                $images_url = explode('.', $info['images']);
 | 
	
		
			
				|  |  | +                unset($images_url[count($images_url) - 1]);
 | 
	
		
			
				|  |  | +                $info['images_thumb'] = join('.', $images_url) . '_0.jpg';
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        //评论
 | 
	
		
			
				|  |  | +        if($info){
 | 
	
		
			
				|  |  | +            $info['answer'] = $this->answer_list($id);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        $this->success('success',$info);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    //点赞
 | 
	
		
			
				|  |  | +    public function good(){
 | 
	
		
			
				|  |  | +        $id = input('id');
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        $where = [
 | 
	
		
			
				|  |  | +            'dt_id' => $id,
 | 
	
		
			
				|  |  | +            'user_id'  => $this->auth->id,
 | 
	
		
			
				|  |  | +        ];
 | 
	
		
			
				|  |  | +        $check = Db::name('topic_dongtai_good')->where($where)->find();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        if($check){
 | 
	
		
			
				|  |  | +            $this->error('已经赞过了');
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        Db::startTrans();
 | 
	
		
			
				|  |  | +        $rs = Db::name('topic_dongtai_good')->insertGetId($where);
 | 
	
		
			
				|  |  | +        $up = Db::name('topic_dongtai')->where('id',$id)->setInc('goodnum');
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        if($rs && $up !== false){
 | 
	
		
			
				|  |  | +            Db::commit();
 | 
	
		
			
				|  |  | +            $this->success('点赞成功');
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        Db::rollback();
 | 
	
		
			
				|  |  | +        $this->error('点赞失败');
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    //评论
 | 
	
		
			
				|  |  | +    public function answer(){
 | 
	
		
			
				|  |  | +        $id = input('id',0);
 | 
	
		
			
				|  |  | +        $content = input('content','');
 | 
	
		
			
				|  |  | +        $to_user_id = input('to_user_id',0);
 | 
	
		
			
				|  |  | +        $level = input('level',1);  //回复类型:1=层主回复楼主,2=层中回复
 | 
	
		
			
				|  |  | +        $floor = input('floor',0);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        if(empty($content) || empty($id)){
 | 
	
		
			
				|  |  | +            $this->error();
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        //关键字替换
 | 
	
		
			
				|  |  | +        //$content = Keyworld::sensitive($content);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        //判断
 | 
	
		
			
				|  |  | +        if($level == 2 && $floor == 0){
 | 
	
		
			
				|  |  | +            $this->error('楼层错误');
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        //回复楼主,最新楼层
 | 
	
		
			
				|  |  | +        if($level == 1 || $floor == 0){
 | 
	
		
			
				|  |  | +            $to_user_id = 0;
 | 
	
		
			
				|  |  | +            $floor = 1;  //默认一楼
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            $last_floor = Db::name('topic_dongtai_answer')->where(['dt_id'=>$id,'level'=>1])->order('floor desc')->value('floor');
 | 
	
		
			
				|  |  | +            if($last_floor){
 | 
	
		
			
				|  |  | +                $floor = $last_floor + 1;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        //判断user_id
 | 
	
		
			
				|  |  | +        if($to_user_id){
 | 
	
		
			
				|  |  | +            $to_user = Db::name('user')->where('id',$to_user_id)->value('id');
 | 
	
		
			
				|  |  | +            if(empty($to_user)){
 | 
	
		
			
				|  |  | +                $this->error('被回复的用户不存在');
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        //data
 | 
	
		
			
				|  |  | +        $data = [
 | 
	
		
			
				|  |  | +            'dt_id' => $id,
 | 
	
		
			
				|  |  | +            'floor' => $floor,
 | 
	
		
			
				|  |  | +            'user_id' => $this->auth->id,
 | 
	
		
			
				|  |  | +            'content' => $content,
 | 
	
		
			
				|  |  | +            'to_user_id' => $to_user_id,
 | 
	
		
			
				|  |  | +            'level' => $level,
 | 
	
		
			
				|  |  | +            'createtime' => time(),
 | 
	
		
			
				|  |  | +            'updatetime' => time(),
 | 
	
		
			
				|  |  | +        ];
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        Db::startTrans();
 | 
	
		
			
				|  |  | +        $rs = Db::name('topic_dongtai_answer')->insertGetId($data);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        Db::commit();
 | 
	
		
			
				|  |  | +        $this->success('评价成功');
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    //评论列表
 | 
	
		
			
				|  |  | +    private function answer_list($dt_id){
 | 
	
		
			
				|  |  | +        //楼
 | 
	
		
			
				|  |  | +        $floor_list = Db::name('topic_dongtai_answer')
 | 
	
		
			
				|  |  | +            ->alias('a')
 | 
	
		
			
				|  |  | +            ->field('a.*,user.nickname,user.avatar')
 | 
	
		
			
				|  |  | +            ->join('user','a.user_id = user.id','LEFT')
 | 
	
		
			
				|  |  | +            ->where(['a.dt_id'=>$dt_id,'a.level'=>1])->order('id asc')->autopage()->select();
 | 
	
		
			
				|  |  | +        $floor_list = list_domain_image($floor_list,['avatar']);
 | 
	
		
			
				|  |  | +        if(empty($floor_list)){
 | 
	
		
			
				|  |  | +            return [];
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        //层
 | 
	
		
			
				|  |  | +        $floors = array_column($floor_list,'floor');
 | 
	
		
			
				|  |  | +        $child_lists = Db::name('topic_dongtai_answer')->alias('a')
 | 
	
		
			
				|  |  | +            ->field('a.*,user.nickname,user.avatar,tuser.nickname as to_nickname,tuser.avatar as to_avatar')
 | 
	
		
			
				|  |  | +            ->join('user','a.user_id = user.id','LEFT')
 | 
	
		
			
				|  |  | +            ->join('user tuser','a.to_user_id = tuser.id','LEFT')
 | 
	
		
			
				|  |  | +            ->where(['a.dt_id'=>$dt_id,'a.floor'=>['IN',$floors],'a.level'=>2])->order('id asc')->select();
 | 
	
		
			
				|  |  | +        $child_lists = list_domain_image($child_lists,['avatar','to_avatar']);
 | 
	
		
			
				|  |  | +        /*if(empty($child_lists)){
 | 
	
		
			
				|  |  | +            return $floor_list;
 | 
	
		
			
				|  |  | +        }*/
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        //合并
 | 
	
		
			
				|  |  | +        foreach($floor_list as $key => $val){
 | 
	
		
			
				|  |  | +            $child = [];
 | 
	
		
			
				|  |  | +            foreach($child_lists as $k => $v){
 | 
	
		
			
				|  |  | +                if($val['floor'] == $v['floor']){
 | 
	
		
			
				|  |  | +                    $child[] = $v;
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            //追加到外循环
 | 
	
		
			
				|  |  | +            $floor_list[$key]['childcount'] = 0;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            if(count($child) > 4){
 | 
	
		
			
				|  |  | +                $floor_list[$key]['childcount'] = count($child) - 4;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            $floor_list[$key]['child'] = array_slice($child,0,4);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        return $floor_list;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    //单独某一层的详细
 | 
	
		
			
				|  |  | +    public function floor_info(){
 | 
	
		
			
				|  |  | +        $floor_id = input('floor_id');
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        //楼
 | 
	
		
			
				|  |  | +        $floor_info = Db::name('topic_dongtai_answer')
 | 
	
		
			
				|  |  | +            ->alias('a')
 | 
	
		
			
				|  |  | +            ->field('a.*,user.nickname,user.avatar')
 | 
	
		
			
				|  |  | +            ->join('user','a.user_id = user.id','LEFT')
 | 
	
		
			
				|  |  | +            ->where(['a.id'=>$floor_id])->find();
 | 
	
		
			
				|  |  | +        $floor_info = info_domain_image($floor_info,['avatar']);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        //层
 | 
	
		
			
				|  |  | +        $floors = $floor_info['floor'];
 | 
	
		
			
				|  |  | +        $child_lists = Db::name('topic_dongtai_answer')->alias('a')
 | 
	
		
			
				|  |  | +            ->field('a.*,user.nickname,user.avatar,tuser.nickname as to_nickname,tuser.avatar as to_avatar')
 | 
	
		
			
				|  |  | +            ->join('user','a.user_id = user.id','LEFT')
 | 
	
		
			
				|  |  | +            ->join('user tuser','a.to_user_id = tuser.id','LEFT')
 | 
	
		
			
				|  |  | +            ->where(['a.floor'=>$floors,'a.level'=>2])->order('id asc')->autopage()->select();
 | 
	
		
			
				|  |  | +        $child_lists = list_domain_image($child_lists,['avatar','to_avatar']);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        //合并
 | 
	
		
			
				|  |  | +        $floor_info['child'] = $child_lists;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        $this->success('success',$floor_info);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    //某个圈子里的动态列表,全部,最新,最热
 | 
	
		
			
				|  |  | +    public function topic_list(){
 | 
	
		
			
				|  |  | +        $topic_id = input('topic_id',0);
 | 
	
		
			
				|  |  | +        $order = input('orderby','new');
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        $orderby  = 'dt.id desc';
 | 
	
		
			
				|  |  | +        if($order == 'hot'){
 | 
	
		
			
				|  |  | +            $orderby = 'dt.goodnum desc';
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        $list = Db::name('topic_dongtai')->alias('dt')
 | 
	
		
			
				|  |  | +            ->join('user','dt.user_id = user.id','LEFT')
 | 
	
		
			
				|  |  | +            ->field('dt.*,user.nickname,user.avatar')
 | 
	
		
			
				|  |  | +            ->where('dt.topic_id',$topic_id)
 | 
	
		
			
				|  |  | +            ->order($orderby)->autopage()->select();
 | 
	
		
			
				|  |  | +        $list = list_domain_image($list,['images','avatar']);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        //追加是否点赞
 | 
	
		
			
				|  |  | +        if(!empty($list)){
 | 
	
		
			
				|  |  | +            $ids = array_column($list,'id');
 | 
	
		
			
				|  |  | +            $map = [
 | 
	
		
			
				|  |  | +                'dt_id' => ['IN',$ids],
 | 
	
		
			
				|  |  | +                'user_id'  => $this->auth->id,
 | 
	
		
			
				|  |  | +            ];
 | 
	
		
			
				|  |  | +            $good_list = Db::name('topic_dongtai_good')->where('dt_id','IN',$ids)->select();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            foreach($list as $key => $val){
 | 
	
		
			
				|  |  | +                $list[$key]['isgood'] = 0;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                foreach($good_list as $k => $v){
 | 
	
		
			
				|  |  | +                    if($val['id'] == $v['dt_id']){
 | 
	
		
			
				|  |  | +                        $list[$key]['isgood'] = 1;
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        $this->success('success',$list);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    //动态赠送礼物
 | 
	
		
			
				|  |  | +    public function givegiftdongtai() {
 | 
	
		
			
				|  |  | +        // 接口防并发
 | 
	
		
			
				|  |  | +        if (!$this->apiLimit(1, 1000)) {
 | 
	
		
			
				|  |  | +            $this->error(__('Operation frequently'));
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +//        $user_id = input('user_id');// 赠送对象
 | 
	
		
			
				|  |  | +        $dt_id = input('dt_id', 0, 'intval'); //动态id
 | 
	
		
			
				|  |  | +        $gift_id = input('gift_id');// 礼物ID
 | 
	
		
			
				|  |  | +        $number  = input('number',1,'intval');//数量
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        if (!$dt_id || !$gift_id || $number < 1) {
 | 
	
		
			
				|  |  | +            $this->error();
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        //查询动态
 | 
	
		
			
				|  |  | +        $dongtai_info = Db::name('topic_dongtai')->find($dt_id);
 | 
	
		
			
				|  |  | +        if (!$dongtai_info) {
 | 
	
		
			
				|  |  | +            $this->error('您的网络开小差啦~');
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        if ($dongtai_info['status'] != 0) {
 | 
	
		
			
				|  |  | +            $this->error('您的网络开小差啦~');
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        $user_id =  $dongtai_info['user_id'];
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // 不可以赠送给自己
 | 
	
		
			
				|  |  | +        if($this->auth->id == $user_id) {
 | 
	
		
			
				|  |  | +            $this->error("不可以赠送给自己");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // 获取礼物信息
 | 
	
		
			
				|  |  | +        $giftinfo = Db::name('gift')->where('id',$gift_id)->find();
 | 
	
		
			
				|  |  | +        if (!$giftinfo)
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            $this->error("请选择礼物");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        $giftvalue = bcmul($giftinfo['value'],$number);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        //被赠送人信息
 | 
	
		
			
				|  |  | +        $touserinfo = Db::name('user')->where('id',$user_id)->find();
 | 
	
		
			
				|  |  | +        if (!$touserinfo) {
 | 
	
		
			
				|  |  | +            $this->error("不存在的用户");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        // 判断当前用户余额
 | 
	
		
			
				|  |  | +        $user_gold = model('wallet')->getWallet($this->auth->id,'gold');
 | 
	
		
			
				|  |  | +        if($user_gold < $giftvalue) {
 | 
	
		
			
				|  |  | +            $this->error("您的金币余额不足");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        Db::startTrans();
 | 
	
		
			
				|  |  | +        // 添加礼物赠送记录表
 | 
	
		
			
				|  |  | +        $data = [
 | 
	
		
			
				|  |  | +            'user_id' => $this->auth->id,
 | 
	
		
			
				|  |  | +            'user_to_id' => $user_id,
 | 
	
		
			
				|  |  | +            'dt_id' => $dt_id,
 | 
	
		
			
				|  |  | +            'gift_id' => $giftinfo['id'],
 | 
	
		
			
				|  |  | +            'gift_name' => $giftinfo['name'],
 | 
	
		
			
				|  |  | +            'number' => $number,
 | 
	
		
			
				|  |  | +            'price' => $giftvalue,
 | 
	
		
			
				|  |  | +            'createtime' => time(),
 | 
	
		
			
				|  |  | +        ];
 | 
	
		
			
				|  |  | +        $log_id = Db::name('gift_user_dongtai')->insertGetId($data);
 | 
	
		
			
				|  |  | +        if(!$log_id){
 | 
	
		
			
				|  |  | +            Db::rollback();
 | 
	
		
			
				|  |  | +            $this->error('赠送失败');
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        if($giftvalue > 0){
 | 
	
		
			
				|  |  | +            // 扣除当前用户余额
 | 
	
		
			
				|  |  | +            $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,$user_id,'gold',-$giftvalue,59,'赠送礼物:'.$giftinfo["name"] . '*' . $number,'gift_user_dongtai',$log_id);
 | 
	
		
			
				|  |  | +            if($wallet_rs['status'] === false){
 | 
	
		
			
				|  |  | +                Db::rollback();
 | 
	
		
			
				|  |  | +                $this->error($wallet_rs['msg']);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            // 添加赠送用户余额
 | 
	
		
			
				|  |  | +            $money_to_gold = config('site.money_to_gold');
 | 
	
		
			
				|  |  | +            $gift_plat_scale = config('site.gift_plat_scale');
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            $giftmoney = bcdiv($giftvalue,$money_to_gold,2);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            $money = bcdiv(bcmul($giftmoney,100 - $gift_plat_scale,2),100,2);
 | 
	
		
			
				|  |  | +            $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,$this->auth->id,'money',$money,60,'获得礼物:'.$giftinfo["name"] . '*' . $number,'gift_user_dongtai',$log_id,2);
 | 
	
		
			
				|  |  | +            if($wallet_rs['status'] === false){
 | 
	
		
			
				|  |  | +                Db::rollback();
 | 
	
		
			
				|  |  | +                $this->error($wallet_rs['msg']);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            /*//增加赠送用户上级金币
 | 
	
		
			
				|  |  | +            if ($touserinfo['intro_uid']) {
 | 
	
		
			
				|  |  | +                //获取返利比率
 | 
	
		
			
				|  |  | +                $intro_gift_rebate_rate = (int)config('site.intro_gift_rebate_rate'); //邀请人收礼物返利比率
 | 
	
		
			
				|  |  | +                if ($intro_gift_rebate_rate > 0 && $intro_gift_rebate_rate <= 100) {
 | 
	
		
			
				|  |  | +                    //上级获得金币数量
 | 
	
		
			
				|  |  | +                    $intro_uid_gold = floor($giftvalue * $intro_gift_rebate_rate / 100);
 | 
	
		
			
				|  |  | +                    if ($intro_uid_gold > 0) {
 | 
	
		
			
				|  |  | +                        $intro_result = model('Wallet')->lockChangeAccountRemain($touserinfo['intro_uid'],$user_id,'gold',$intro_uid_gold,66, '动态礼物获赠奖励','gift_user_dongtai',$log_id);
 | 
	
		
			
				|  |  | +                        if($intro_result['status']===false)
 | 
	
		
			
				|  |  | +                        {
 | 
	
		
			
				|  |  | +                            Db::rollback();
 | 
	
		
			
				|  |  | +                            $this->error($intro_result['msg']);
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }*/
 | 
	
		
			
				|  |  | +            //增加赠送用户上级余额
 | 
	
		
			
				|  |  | +            if ($touserinfo['intro_uid']) {
 | 
	
		
			
				|  |  | +                //获取返利比率
 | 
	
		
			
				|  |  | +                $is_agent = Db::name('user')->where(['id' => $touserinfo['intro_uid']])->value('is_agent');
 | 
	
		
			
				|  |  | +                $intro_income_rebate_rate = $is_agent ? (int)config('site.h_intro_income_rebate_rate') : (int)config('site.intro_income_rebate_rate'); //邀请人收礼物返利比率
 | 
	
		
			
				|  |  | +                if ($intro_income_rebate_rate > 0 && $intro_income_rebate_rate <= 100) {
 | 
	
		
			
				|  |  | +                    //上级获得金额
 | 
	
		
			
				|  |  | +                    $intro_uid_money = number_format($money * $intro_income_rebate_rate / 100, 2, '.', '');
 | 
	
		
			
				|  |  | +                    if ($intro_uid_money > 0) {
 | 
	
		
			
				|  |  | +                        $intro_result = model('Wallet')->lockChangeAccountRemain($touserinfo['intro_uid'],$user_id,'money',$intro_uid_money,68, '邀请人动态礼物获赠奖励','gift_user_dongtai',$log_id);
 | 
	
		
			
				|  |  | +                        if($intro_result['status']===false)
 | 
	
		
			
				|  |  | +                        {
 | 
	
		
			
				|  |  | +                            Db::rollback();
 | 
	
		
			
				|  |  | +                            $this->error($intro_result['msg']);
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            if ($this->auth->gender == 1 && $touserinfo['gender'] == 0) {
 | 
	
		
			
				|  |  | +                //增加亲密度
 | 
	
		
			
				|  |  | +                $user_intimacy_rs = addintimacy($this->auth->id, $user_id, $giftvalue);
 | 
	
		
			
				|  |  | +                if (!$user_intimacy_rs['status']) {
 | 
	
		
			
				|  |  | +                    Db::rollback();
 | 
	
		
			
				|  |  | +                    $this->error('您的网络开小差啦~');
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        //tag任务赠送金币
 | 
	
		
			
				|  |  | +        //搭讪奖励
 | 
	
		
			
				|  |  | +//        $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,15);
 | 
	
		
			
				|  |  | +//        if($task_rs === false){
 | 
	
		
			
				|  |  | +//            Db::rollback();
 | 
	
		
			
				|  |  | +//            $this->error('完成任务赠送奖励失败');
 | 
	
		
			
				|  |  | +//        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        Db::commit();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        //发送消息
 | 
	
		
			
				|  |  | +        if (isset($user_intimacy_rs) && $user_intimacy_rs['level_remark']) {
 | 
	
		
			
				|  |  | +            $tenim = new \app\api\controller\Tenim;
 | 
	
		
			
				|  |  | +            $tenim->sendMessageToUser($this->auth->id, $user_id, $user_intimacy_rs['level_remark'], 1);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        $return_data['money'] = $money; //获得金额
 | 
	
		
			
				|  |  | +        $return_data['level_remark'] = isset($user_intimacy_rs) ? $user_intimacy_rs['level_remark'] : ''; //亲密度等级提示语
 | 
	
		
			
				|  |  | +        $this->success('赠送成功', $return_data);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    //动态收到礼物列表
 | 
	
		
			
				|  |  | +    public function dongtaigiftlist() {
 | 
	
		
			
				|  |  | +        $id = input('id', 0, 'intval');
 | 
	
		
			
				|  |  | +        if (!$id) {
 | 
	
		
			
				|  |  | +            $this->error('您的网络开小差啦~');
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        $list = Db::name('gift_user_dongtai')->field('user_id, count(id) count')->where(['dt_id' => $id])->group('user_id')->order('id desc')->autopage()->select();
 | 
	
		
			
				|  |  | +        if (!$list) {
 | 
	
		
			
				|  |  | +            $this->success('success', $list);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        $mt_user = Db::name('user');
 | 
	
		
			
				|  |  | +        foreach ($list as &$v) {
 | 
	
		
			
				|  |  | +            $user_info = $mt_user->field('nickname, avatar, gender, birthday')->where(['id' => $v['user_id']])->find();
 | 
	
		
			
				|  |  | +            $v['nickname'] = $user_info['nickname'];
 | 
	
		
			
				|  |  | +            $v['avatar'] = one_domain_image($user_info['avatar']);
 | 
	
		
			
				|  |  | +            $v['birthday'] = birthtime_to_age($user_info['birthday']);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        $this->success('success', $list);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +}
 |