get('bid'); if(empty($bid)){ $default_ink = $this->getLink(); return redirect()->to($default_ink); } $openid = $request->get('openid'); //授权 $params = $request->except('_url'); if(empty($openid)){ //$url = str_replace('http://', env('PROTOCOL') . '://', url()->current() . '?' . http_build_query($params)); $url = url()->current() . '?' . http_build_query($params); $params['redirect_url'] = urlencode($url); $app = new Application($this->auth($params)); return $app->oauth->redirect(); } try{ $bid = Hashids::decode($bid)[0]; }catch (Exception $e){ return redirect()->to($this->getLink()); } $crm = $request->get('crm'); //获取用户 $user = $this->getUsers($openid); if(!$user[0]){ $user[1] = 123; } //有阅读纪录的跳转 $read_record = ReadRecordService::getByField($user[0],$bid); if($read_record){ $cid = explode('_',$read_record)[0]; }else{ //没有阅读记录的跳转 $book_info = BookConfigService::getBookById($bid); $cid = $book_info->first_cid; } $params['cid'] = $cid; if(isset($params['openid'])) unset($params['openid']); if(isset($params['unionid'])) unset($params['unionid']); $url = $this->getLink($user[1]).'reader?'.http_build_query($params); $this->stats('book',$crm); return redirect()->to($url); } public function freeCurrencyView(Request $request){ $openid = $request->get('openid'); //授权 $params = $request->except('_url'); if(empty($openid)){ //$url = str_replace('http://', env('PROTOCOL') . '://', url()->current() . '?' . http_build_query($params)); $url = url()->current() . '?' . http_build_query($params); $params['redirect_url'] = urlencode($url); $app = new Application($this->auth($params)); return $app->oauth->redirect(); } $uri = $request->path(); $token = $request->get('token'); $rfee = $request->get('amount'); $crm = $request->get('crm'); if($uri == 'freethrhcurrency'){ $fee = 300; $this->stats('reward_300',$crm); }else{ $this->stats('reward_200',$crm); $fee = $this->freeCurrencyFee($token,$rfee); } $user = $this->getUsers($openid); if(!$user || !$user[0] || !$user[1]) return redirect()->to($this->getLink().'?'.http_build_query($params)); $get_free_currency = UserBindHkWelfareService::isHasGet($user[0]); if($get_free_currency){ //已经领过 return view('jump.bindHkFreeCurrency',['fee'=>$fee,'url'=>$this->getLink($get_free_currency->distribution_channel_id).'?'.http_build_query($params),'is_get'=>1,'uid'=>$get_free_currency->uid]); }else{ return view('jump.bindHkFreeCurrency',['fee'=>$fee,'url'=>$this->getLink($user[1]).'?'.http_build_query($params),'is_get'=>0,'uid'=>$user[0]]); } } private function freeCurrencyFee($token,$fee){ if(!$token || !$fee) return 200; $param['fee'] = $fee; if( _sign($param,env('SECRET_KEY')) == $token) return $fee; return 200; } public function freeCurrencyPost(Request $request){ $uid = $request->post('uid'); $fee = $request->post('fee',200); $result = UserBindHkWelfareService::getfreeCurrency($uid,$fee); if($result){ UserService::addBalance($uid,$fee,0,$fee); } return response()->success(['uid'=>$uid,'result'=>$result]); } public function activity(Request $request){ $token = $request->get('token'); if(empty($token)){ $default_ink = $this->getLink(); return redirect()->to($default_ink); } $openid = $request->get('openid'); //授权 $params = $request->except('_url'); if(empty($openid)){ $url = url()->current() . '?' . http_build_query($params); $params['redirect_url'] = urlencode($url); $app = new Application($this->auth($params)); return $app->oauth->redirect(); } $crm = $request->get('crm'); $this->stats('activity',$crm); $activity = ActivityService::getByToken($token); if($activity){ $user = $this->getUsers($openid); $distribution_channel_id = (isset($user[1]) && !empty($user[1]))?$user[1]:123; $url_format = '%s://site%s.%s.com%s'; $activity_page = $activity->activity_page ; if($activity->id == 6000){ $activity_page = '/activity/common?token=LNyAqbFMgvkmvnHP8PXV3DYPIIhQm3oe'; } $url = sprintf($url_format, env('PROTOCOL'), encodeDistributionChannelId($distribution_channel_id), env('CUSTOM_HOST'), $activity_page ); return redirect()->to($url); } $default_ink = $this->getLink(); return redirect()->to($default_ink); } public function recent(Request $request){ $openid = $request->get('openid'); //授权 $params = $request->except('_url'); if(empty($openid)){ $url = url()->current() . '?' . http_build_query($params); $params['redirect_url'] = urlencode($url); $app = new Application($this->auth($params)); return $app->oauth->redirect(); } $user = $this->getUsers($openid); if(!$user[0]){ $distribution_channel_id = 123; }else{ $distribution_channel_id = $user[1]; } $crm = $request->get('crm'); $this->stats('recent',$crm); $link = $this->getLink($distribution_channel_id).'recent?'.http_build_query($params); return redirect()->to($link); } public function person(Request $request){ $openid = $request->get('openid'); //授权 $params = $request->except('_url'); if(empty($openid)){ $url = url()->current() . '?' . http_build_query($params); $params['redirect_url'] = urlencode($url); $app = new Application($this->auth($params)); return $app->oauth->redirect(); } $user = $this->getUsers($openid); if(!$user[0]){ $distribution_channel_id = 123; }else{ $distribution_channel_id = $user[1]; } $crm = $request->get('crm'); $this->stats('person',$crm); $link = $this->getLink($distribution_channel_id).'person?'.http_build_query($params); return redirect()->to($link); } public function sign(Request $request){ $openid = $request->get('openid'); //授权 $params = $request->except('_url'); if(empty($openid)){ $url = url()->current() . '?' . http_build_query($params); $params['redirect_url'] = urlencode($url); $app = new Application($this->auth($params)); return $app->oauth->redirect(); } $user = $this->getUsers($openid); if(!$user[0]){ $distribution_channel_id = 123; }else{ $distribution_channel_id = $user[1]; } $crm = $request->get('crm'); $this->stats('sign',$crm); $link = $this->getLink($distribution_channel_id).'sign?'.http_build_query($params); return redirect()->to($link); } private function getUsers($openid){ $users = User::where('openid',$openid)->select('id','distribution_channel_id')->get(); if($users->isEmpty()) return [0,0]; $temp = null; $distribution_channel_id = 0; $uid = 0; foreach ($users as $user){ $last_read = ReadRecordService::getByField($user->id,'last_read'); if(!$last_read) continue; if(!$temp ){ $distribution_channel_id = $user->distribution_channel_id; $uid = $user->id; $temp = $last_read; }else{ $temp_last_read_info = explode('_',$temp); $last_read_info = explode('_',$last_read); if($last_read_info[2] > $temp_last_read_info[2]){ $uid = $user->id; $distribution_channel_id = $user->distribution_channel_id; $temp = $last_read; } } } return [$uid,$distribution_channel_id]; } private function getLink($distribution_channel_id=123){ $url_format = '%s://site%s.%s.com/'; return sprintf( $url_format, env('PROTOCOL'), encodeDistributionChannelId($distribution_channel_id), env('CUSTOM_HOST') ); } private function auth($param){ $param['appid'] = 'wx9d389a73b88bbeae'; $options = [ 'app_id' => 'wx9d389a73b88bbeae', 'secret' => '2f6594bb595dfa256b5512e43a32a3d3', 'oauth' => [ 'scopes' => ['snsapi_base'], 'callback' => env('AUTH_CALLBACK_URL') . '?' . http_build_query($param), ], ]; return $options; } private function stats($page,$flag){ if(!$page || !$flag) return ; //pv $day = date('Y-m-d'); $pv_key = sprintf('crm:pv:%s',$flag); Redis::hincrby($pv_key, $day, 1); //Redis::hincrby($pv_key, 'total', 1); Redis::sadd('crm_'.$day,$flag); //uv $cookie_flag = Cookie::get('crm_flag'); if(!$cookie_flag || $cookie_flag != $flag){ $uv_key = sprintf('crm:uv:%s',$flag); Redis::hincrby($uv_key, $day, 1); //Redis::hincrby($uv_key, 'total', 1); } Cookie::queue('crm_flag', $flag, env('U_COOKIE_EXPIRE'), null, null, false, false); } }