0, 'getKandian'=>0, 'totalKandian'=>0, 'list'=>[], ]; $redisKey = $this->shareList.$userId; $redisData = Redis::instance()->hGetAll($redisKey); if ( $redisData == 'empty' ){//没有数据时 return $data; } if( !$redisData ){ $res = $this->getShareUserModel()->where('user_id',$userId); $data['count'] = $res->count(1); if ( $data['count'] < 1 ){ Redis::instance()->set($redisKey,'empty'); }else{ $data['getKandian'] = $res->sum('kandian'); $data['totalKandian'] = $data['count']*200;//默认书币是200 Redis::instance()->hMSet($redisKey,$data); Redis::instance()->expire($redisKey,3600*24); Redis::instance()->sadd($this->shareKey.$userId,$userName.'邀请了'.$data['count'].'人,收到'.$data['totalKandian'].'书币'); } }else{ $data = $redisData; } if ( $data['count'] > 0 ){ $data['list'] = $this->getShareUserModel()->limit($begin,$lenth)->select(); } return $data; } public function userList( $userId, $pageNum, $lenNum ){ $begin = $pageNum*$lenNum; $re = $this->getShareUserModel()->where('user_id',$userId)->limit($begin,$lenNum)->select(); return $re; } /** * 点击分享链接, * @param $userId 被分享者 * @param $fromUserId 分享者 * @return int */ public function ClickShareLink($userId, $fromUserId, $shareTime){ $time = time(); $userSource = [ 'user_id'=>$fromUserId, 'to_user_id'=>$userId ]; if( $this->checkBind($userId,$fromUserId) || (time()-$shareTime)>3600*24){ $this->updateUserSourceTime($fromUserId,$userId,$shareTime); LogService::info('分享活动-绑定成功01:'.json_encode($userSource).'分享时间:'.$shareTime); return 2; } $re = 0; try{ //再次检查数据库里是否存在 $re = $this->getShareUserModel()->where($userSource)->count(1); if ( $re == 0 ){//是第一次点看链接 //回写分享者的数据 $shareUser = [ 'user_id'=>$fromUserId, 'to_user_id'=>$userId, 'to_user_avatar'=>'/assets/img/frontend/share/default_people.png', 'to_user_nickname'=>'书友', 'share_time'=>$shareTime, 'created_at'=>date('Y-m-d H:i:s',$time), 'updated_at'=>date('Y-m-d H:i:s',$time) ]; $this->getShareUserModel()->save($shareUser); //将绑定关系存在redis里面 Redis::instance()->sadd($this->shareBind.$userId, $fromUserId); Redis::instance()->expire($this->shareBind.$userId, 3600*24); //清除统计redis Redis::instance()->del($this->shareList.$fromUserId); }else{ $this->updateUserSourceTime($fromUserId,$userId,$shareTime); } }catch(\Throwable $th){ LogService::error('分享活动-绑定error01'.json_encode($userSource).'分享时间:'.$shareTime); } return $re; } /** * 关注时回写分享者数据 * @param $userId * @param $fromUserId * @return mixed */ public function attentionUser($toUserId,$updateData){ LogService::info('分享活动begin'); $isOpenShare = Config::get('site.is_open_share'); if (!$isOpenShare){ return false; } //获取最早的链接分享者 try{ $userSource = [ 'to_user_id'=>$toUserId, ]; $users = $this->getShareUserModel() ->where($userSource) ->where('share_time','>',time()-3600*24) ->order('id asc')->limit(1)->find(); if (!empty($users)){ $id = $users['id']; $userId = $users['user_id']; $update = [ 'is_fan'=>1, 'kandian'=>200, 'to_user_nickname'=>$updateData['nickname'], 'to_user_avatar'=>$updateData['headimgurl'], ]; $re = $this->getShareUserModel()->where(['id'=>$id])->update($update); if ($re){ FinancialService::instance()->modifyUserKandian( $userId, 1, 1,0,200,'分享链接赠送',2); //清除统计redis Redis::instance()->del($this->shareList.$userId); } LogService::info('分享活动-更新数据成功 fromUserId:'.$userId.'res'.$re); } }catch ( \Throwable $th ){ LogService::error('分享活动-关注error02'.$th->getMessage()); } return ; } /** * 检查两个用户是否绑定 * @param $userId * @param $fromUserId * @return bool */ public function checkBind($userId, $fromUserId){ $redisKey = $this->shareBind.$userId; $bind = Redis::instance()->SISMEMBER($redisKey,$fromUserId); return $bind; } /** *重新邀请时,更新分享时间 */ public function updateUserSourceTime($userId,$toUserId,$shareTime){ $re = $this->getShareUserModel() ->where(['user_id'=>$userId]) ->where(['to_user_id'=>$toUserId]) ->update(['share_time'=>$shareTime]); return $re; } /** * 弹幕 * @return array|mixed */ public function getShareBarrage($userId){ $key = $this->shareKey.$userId; $data = Cache::get($key); if ( $data == false ){ $data = []; $nameInit = ['果果吧','我本善良','Bear.zheng','书友','幸福','可乐要加冰块','金芳','有些梦,忘了追','杨洪旭']; $len = Redis::instance()->sCard($key); if ( $len >= 20 ){ $data = Redis::instance()->spop($key, 10); }else{ $data = Redis::instance()->SRANDMEMBER($key, 10); } //不够十个填充 $diff = 10 - count($data); while ( $diff ){ $data[] = array_pop($nameInit).'邀请了1位好友,收到200书币'; $diff--; } Cache::set( $key, $data, 3600); } return $data; } }