cookieNoCrypt(); $test = 0; if ($request->has('test') && $request->input('test') == 'bonan') { $test = 1; } $uid_cookie = Cookie::get(env('COOKIE_AUTH_WEB_WECHAT')); if (!$uid_cookie) $uid_cookie = $this->authCallBack($request); $h5_scheme = env('H5_SCHEME', 'https'); $params = $request->except('_url'); $url = str_replace('http://', $h5_scheme . '://', url()->current() . '?' . http_build_query($params)); //Log::info('-----------------uid' . $uid_cookie); $send_order_id = $request->input('send_order_id'); if ($send_order_id) { Cookie::queue('send_order_id', $send_order_id, env('U_COOKIE_EXPIRE'), null, null, false, false); } $distribution_channel_id = str_replace('site', '', explode('.', _domain())[0]); $origin_distribution_channel_id = $distribution_channel_id; if (!is_numeric($distribution_channel_id)) { $distribution_channel_id = decodeDistributionChannelId($distribution_channel_id); } if (!$distribution_channel_id) { return response()->error('WAP_SYS_ERROR'); } $uri_send_order_id = 0; $uri = $request->input('_url'); if (strpos($uri, '/yun/') !== false) { $uri_send_order_id = (int)str_ireplace('/yun/', '', $uri); } $yun = $request->get('yun'); $yun && $uri_send_order_id = $yun; if (!$uid_cookie && $test == 0) { if ($uri_send_order_id) { Cookie::queue('send_order_id', $uri_send_order_id, env('U_COOKIE_EXPIRE'), null, null, false, false); } $this->share($request); $old_auth = false; if ($old_auth && !in_array($distribution_channel_id, explode(',', env('NEW_AUTH_CHANNEL_ID', 1)))) { //旧的授权 Cookie::queue('auth_redirect', urlencode($url), env('U_COOKIE_EXPIRE')); $auth_v2_url = $this->authV2($request, $distribution_channel_id); if ($auth_v2_url) { return redirect()->to($auth_v2_url); } $redirect_V2 = $this->auth($request, $distribution_channel_id); if ($redirect_V2) { return redirect()->to($redirect_V2); } } else { //新的授权 $options = $this->authReduceRedirect($request,$distribution_channel_id,$uri_send_order_id); $app = new Application($options); //Log::info($options); return $app->oauth->redirect(); } } //禁止访问的site $not_allow_access_site = env('NOT_ALLOW_ACCESS_SITE', ''); if ($not_allow_access_site) { $not_allow_access_site_arr = explode(',', $not_allow_access_site); if (in_array($distribution_channel_id, $not_allow_access_site_arr) && !OrderService::isPaidUser($uid_cookie)) { return response($this->notAccessPage())->header('Content-Type', 'text/html'); } } //个性化推送 //$this->stylePush($request, $uid_cookie, $distribution_channel_id); //禁止明文访问 if ($this->isForbidPrimaryNumberChannleID($origin_distribution_channel_id, $uid_cookie)) { return response('该网站地址暂停访问

该网站地址暂停访问,请联系客服微信获取新地址:wuxinchao12

'); } //crm 跳转 if(($crm_link = $this->crmJump($request,$uid_cookie,$origin_distribution_channel_id))){ return redirect()->to($crm_link); } if ($request->has('appid') && $request->has('openid')) { Log::info('param is:'); Log::info($params); $uri = $request->input('_url'); $openid = $request->get('openid'); $appid = $request->get('appid'); if ($openid && $appid && $distribution_channel_id && $uid_cookie) { $this->apidAndOpenId($distribution_channel_id, $appid, $openid, $uid_cookie, $uri_send_order_id); } } $this->yun($request, $uid_cookie, $distribution_channel_id); //只能推送的统计 $this->smartPush($request, $uid_cookie); //from where的临时统计 $this->tempFromWhere($request, $uid_cookie); $response = $next($request); $stats = $request->get('stats', 0); if ($request->has('fromtype') && $request->input('fromtype')) { try { $from = $request->input('fromtype', 'main'); Cookie::queue('from', $from, env('U_COOKIE_EXPIRE'), null, null, false, false); if (!$stats) { Redis::sadd('push:distribution_channel_id:' . $distribution_channel_id . 'from:' . $from . ':date:' . date('Y-m-d'), $uid_cookie); Redis::sadd('push:distribution_channel_id:alluv:from:' . $from, $uid_cookie); Redis::hincrby('push:distribution_channel_id:allpv', $from, 1); Redis::hincrby('customer:push:click:distribution_channel_id:' . $distribution_channel_id . 'from:' . $from, date('Y-m-d'), 1); Redis::sadd('wap_from_stats' . date('Y-m-d'), $distribution_channel_id . '-' . $from); } } catch (\Exception $e) { Log::info($e); } } $from = $request->get('fromtype'); $inner_order_id = $request->get('inner_order_id'); if (!$from) { $from = Cookie::get('from'); $this->innerSendOrderIdFromFromtype($uid_cookie, 1, $from, $inner_order_id); } else { $this->innerSendOrderIdFromFromtype($uid_cookie, $stats, $from, $inner_order_id); } //内部派单 if ($inner_order_id) { $this->innerSendOrderIdStats($uid_cookie, $inner_order_id, $stats); } //crm $this->crmParam($request); //from_title_image $this->fromTitleImage($request, $uid_cookie); $this->InnerOuterWeixinStats($request,$uid_cookie,$distribution_channel_id); if ($test) { Cookie::queue(env('COOKIE_AUTH_WEB_WECHAT'), 9, env('U_COOKIE_EXPIRE'), null, null, false, false); } return $response; } private function share(Request $request) { if ($request->has('fromtype') && 'readershare' == $request->get('fromtype')) { $bid = $request->get('bid'); try { if ($bid) { $bid_arr = Hashids::decode($bid); isset($bid_arr[0]) && $bid = $bid_arr[0]; } } catch (\Exception $e) { } $fromflag = $request->get('fromflag', 0); $cid = $request->get('fromcid', 0); Cookie::queue('reader_share_bid', $bid . '-' . $fromflag, 60); Cookie::queue('reader_share_cid', $cid, 60); } } private function innerSendOrderIdFromFromtype($uid, $stats, $from, $inner_order_id) { //$inner_send_order_id = ReadRecordService::getInnerSendOrderId($uid); $inner_send_order_id = false; if (!$inner_send_order_id) { if ($from && (starts_with($from, 'custom') || starts_with($from, 'template')) && count(explode('_', $from)) == 2) { if (!$inner_order_id) { $this->innerSendOrderIdStats($uid, $from, $stats); } } } } private function innerSendOrderIdStats($uid, $inner_order_id, $stats) { try { ReadRecordService::setInnerSendOrderId($uid, $inner_order_id); if (!$stats) { Redis::sadd('push:inner_send_order_id:uv:' . $inner_order_id, $uid); Redis::hincrby('push:inner_send_order_id:pv', $inner_order_id, 1); } } catch (\Exception $e) { } } private function cookieNoCrypt() { $cookie_key = ['send_order_id', 'from', 'send_order_continue', 'send_order_flag']; foreach ($cookie_key as $v) { $temp = Cookie::get($v); if ($temp && strlen($temp) > 150) { try { $temp = decrypt($temp); } catch (\Exception $e) { } if ($v == 'from') { Cookie::queue('from', $temp, env('U_COOKIE_EXPIRE'), null, null, false, false); //Cookie::queue($v, $temp); } else { Cookie::queue($v, $temp, env('U_COOKIE_EXPIRE'), null, null, false, false); } } } } /** * 公众号签名@华灯初上 * @param $params * @return string */ protected function getSign($params, $key) { $url = $this->arr_to_url($params, false); $url = $url . '&key=' . $key; $sign = md5($url); return $sign; } /** * 公众号签名@华灯初上 * @param $array * @param bool $has_sign * @return string */ protected function arr_to_url($array, $has_sign = false) { ksort($array); reset($array); $arg = ""; while (list ($name, $val) = each($array)) { if ($name == 'sign' && !$has_sign) continue; if (strpos($name, "_") === 0) continue; if (is_array($val)) $val = join(',', $val); if ($val === "") continue; $arg .= $name . "=" . $val . "&"; } $arg = substr($arg, 0, count($arg) - 2); return $arg; } private function apidAndOpenId($distribution_channel_id, $appid, $openid, $uid, $send_order_id) { $appid_info = OfficialAccountService::officialAccountByAppid(['appid' => $appid]); if (!$appid_info || !isset($appid_info->distribution_channel_id)) return false; if ($appid_info->distribution_channel_id != $distribution_channel_id) { return false; } //ForceSubscribeService:: $res = ForceSubscribeService::forceSubscribeUsersByUidAndAppid($appid, $uid); if ($res) return false; $forcesubuser = ForceSubscribeService::forceSubscribeUsersByOpenid(['openid' => $openid]); if ($forcesubuser) { return false; } /*$send_order_id = ReadRecordService::getSendOrderId($uid); if ($send_order_id) { try { Redis::hset('force_subscribe_from_send_order_id', $appid . '_' . $uid, $send_order_id); } catch (\Exception $e) { } }*/ $user_info = ForceSubscribeService::getUserInfoAll($appid, $openid); $forcesubuserdata = [ 'appid' => $appid, 'uid' => $uid, 'distribution_channel_id' => $distribution_channel_id, 'last_interactive_time' => date('Y-m-d H:i:s'), 'openid' => $openid, 'official_account_id' => 1 ]; if ($send_order_id) { $forcesubuserdata['send_order_id'] = $send_order_id; } if ($user_info) { isset($user_info['province']) && !empty($user_info['province']) && $forcesubuserdata['province'] = $user_info['province']; isset($user_info['nickname']) && !empty($user_info['nickname']) && $forcesubuserdata['nick_name'] = $user_info['nickname']; isset($user_info['city']) && !empty($user_info['city']) && $forcesubuserdata['city'] = $user_info['city']; isset($user_info['country']) && !empty($user_info['country']) && $forcesubuserdata['country'] = $user_info['country']; isset($user_info['headimgurl']) && !empty($user_info['headimgurl']) && $forcesubuserdata['headimgurl'] = $user_info['headimgurl']; isset($user_info['sex']) && !empty($user_info['sex']) && $forcesubuserdata['sex'] = $user_info['sex']; } ForceSubscribeService::addForceSubscribeUsers($forcesubuserdata); ForceSubscribeService::tempForceSubscribeUsersLastTimeUpdate([ 'openid' => $openid, 'appid' => $appid, 'distribution_channel_id' => $distribution_channel_id, 'last_interactive_time' => date('Y-m-d H:i:s') ]); try { $send_data = array( 'type' => 'subscribe', 'data' => ['openid'=>'','uid'=>$uid] ); $send = new SendStatisticsList($send_data); $job = ($send)->onConnection('rabbitmq')->delay(0)->onQueue('send_statistics_list'); dispatch($job); } catch (\Exception $e) { Log::info($e); } return true; } private function smartPush(Request $request, $uid) { $bid = $request->get('bid'); $fromsource = $request->get('fromsource'); if (!$fromsource || !$bid) { return; } if ($fromsource != 'smart_push') { return; } $decode_bid_array = Hashids::decode($bid); if (!$decode_bid_array) return; $decode_bid = 0; if (isset($decode_bid_array[0]) && !empty($decode_bid_array[0])) { $decode_bid = $decode_bid_array[0]; } if (!$decode_bid) return; //记录pv uv try { $uv_key = sprintf('%s_%s_uv', $decode_bid, 'smart_push'); Redis::sadd($uv_key, $uid); Redis::HINCRBY('smart_push_test_book', $decode_bid, 1); ReadRecordService::setSmartPush($uid, $decode_bid); } catch (\Exception $e) { Log::info($e); } } protected function notAccessPage() { return '网站正在维护中

网站正在维护中,请联系客服

'; } private function fromTitleImage(Request $request, $uid) { $from_title_image = $request->get('from_title_image'); if ($from_title_image) { $key = 'smart_push_from_title_image:' . $from_title_image; try { Redis::hincrby($key, 'click_num', 1); Redis::sadd('smart_push_from_title_image_click_uv:' . $from_title_image, $uid); } catch (\Exception $e) { } } } /** * 临时统计 * @param Request $request * @param $uid */ private function tempFromWhere(Request $request, $uid) { $temp_from_where = env('TEMP_FROM_WHERE'); if (!$temp_from_where) { return; } $from_where = $request->get('fromwhere'); if (!$from_where) { return; } $url = $request->get('_url', '0'); if (stripos($url, 'reader') !== false) { $temp_from_where_start_end = explode(',', $temp_from_where); if (count($temp_from_where_start_end) == 2) { if (time() > $temp_from_where_start_end[0] && time() < $temp_from_where_start_end[1]) { try { Redis::sadd('temp_from_where:' . $from_where, $uid); } catch (\Exception $e) { } return; } } } return; } /** * 禁止site+数字站点的访问 保留已经充值过的用户和4267站点所有用户 * true禁止 false 不禁止 * @param $distribution_channel_id * @return bool */ private function isForbidPrimaryNumberChannleID($distribution_channel_id, $uid) { $forbid_primary_number_channel_id = env('FORBID_PRIMARY_NUMBER_CHANNEL_ID', '14,2,4267'); if (is_numeric($distribution_channel_id) && !in_array($distribution_channel_id, explode(',', $forbid_primary_number_channel_id)) && !DB::table('orders')->where('uid', $uid)->where('status', 'PAID')->first() ) { return true; } return false; } /** * 特殊渠道的授权登陆 * @param Request $request * @return string */ private function authV2(Request $request, $channel_id) { if (!$request->has('auth')) return ''; $auth = $request->get('auth'); if ($auth != 'v2') { return ''; } $h5_scheme = env('PROTOCOL', 'https'); $domain = _domain(); $params['redirect_url'] = urlencode($h5_scheme . '://' . $domain . '/oauthCallBackV2'); $params['timestamp'] = time(); $info = specialChannelAuthInfo(); if (!$info || !isset($info[$channel_id])) { return ''; } $appid = $info[$channel_id]; if (!$appid) { return ''; } $params['gzh_app_id'] = $appid; $params['sign'] = $this->getSign($params, env('OAUTH_KEY')); $redirect = env('AUTH_URL') . '?' . http_build_query($params); return $redirect; } /** * @param Request $request * @param $channel_id * @return string */ private function auth(Request $request, $channel_id) { $appid = env('WECHAT_AUTH_APPID'); $h5_scheme = env('PROTOCOL', 'https'); $domain = _domain(); $params['redirect_url'] = urlencode($h5_scheme . '://' . $domain . '/oauthCallBack'); $params['timestamp'] = time(); $info = specialChannelAuthInfoV2($channel_id); if ($info && isset($info[$channel_id]) && !empty($info[$channel_id])) { $appid = $info[$channel_id]; } $params['gzh_app_id'] = $appid; $params['sign'] = $this->getSign($params, env('OAUTH_KEY')); $redirect = env('AUTH_URL') . '?' . http_build_query($params); return $redirect; } //个性化推送 private function stylePush(Request $request, $uid, $distribution_channel_id) { $gxhp = $request->get('gxhp'); if (!$gxhp || !$uid || !$distribution_channel_id) return; $date = date('Y-m-d'); $uv_key = sprintf('push:stylepush:uv:%s:gxhp:%s:date:%s', $distribution_channel_id, $gxhp, $date); $pv_key = sprintf('push:stylepush:pv:%s:gxhp:%s:date:%s', $distribution_channel_id, $gxhp, $date); try { Redis::hset('book_read:' . $uid, 'gxhp', $gxhp); Redis::sadd($uv_key, $uid); Redis::incr($pv_key); Redis::sadd(sprintf('push:stylepush:date:%s', $date), sprintf('%s_%s', $distribution_channel_id, $gxhp)); } catch (\Exception $e) { } } private function isLimit($area) { if ($area == 'unknown') { return 'DISABLED'; } $limit_area = ['上海', '北京', '杭州', '南京', '广州']; foreach ($limit_area as $a) { Log::info('$area: ' . $area . ',$a: ' . $a . PHP_EOL); if (mb_strpos($area, $a) !== false) { return 'DISABLED'; } } return 'ENABLE'; } private function getIpArea($ip) { $client = new Client(['timeout' => 3.0]); $url = 'http://apis.juhe.cn/ip/ip2addr?key=5cb679e201b214cf70106e4bf09ab576&ip=' . $ip; $result = null; try { $result = $client->request('get', $url)->getBody()->getContents(); } catch (\Exception $e) { return ''; } if (!$result) return ''; $result = json_decode($result, 1); $area = ''; if (isset($result['result']) && !empty($result['result']) && isset($result['result']['area'])) { $area = $result['result']['area']; } return $area; } private function yun(Request $request, $uid, $distribution_channel_id) { $uri = $request->input('_url'); if ($uri && strpos($uri, '/yun/') !== false) { return; } $param = $request->get('yun'); //Log::info('$param is : '.$param); if (!$param) return; $bid = $request->get('bid'); //Log::info('$bid = $request is : '.$bid); $send_order_info = SendOrderService::getRedirectUrlById($param); //Log::info('$send_order_info is : '); //Log::info($send_order_info); if ($bid) { $bid_arr = Hashids::decode($bid); isset($bid_arr[0]) && $bid = $bid_arr[0]; } //Log::info('bid is'.$bid); if (!$send_order_info) return; if ($send_order_info->book_id && $send_order_info->book_id != $bid) return; if ($send_order_info->distribution_channel_id != $distribution_channel_id) return; if($uid){ try{ Redis::hset('book_read:' . $uid, 'send_order_id', $param); }catch (\Exception $e){} } is_numeric($bid) && $this->specialChannelIdStats($param, $distribution_channel_id, $uid, $bid); $key = date('Y-m-d'); Cookie::queue('send_order_id', $param, env('U_COOKIE_EXPIRE'), null, null, false, false); $send_order_flag = Cookie::get('send_order_flag'); $send_orders = explode(',', $send_order_flag); //uv if (!Cookie::get('send_order_flag_' . $param) && !in_array($param, $send_orders)) { Redis::hincrby('send_order_uv_' . $param, $key, 1); Redis::hincrby('send_order_uv_' . $param, 'total', 1); //Cookie::queue('send_order_flag_'.$param,$param, env('U_COOKIE_EXPIRE'), null, null, false, false); array_push($send_orders, $param); $str = implode(',', $send_orders); Cookie::queue('send_order_flag', $str, env('U_COOKIE_EXPIRE'), null, null, false, false); } if (Cookie::get('send_order_flag_' . $param)) { array_push($send_orders, $param); $str = implode(',', $send_orders); Cookie::queue('send_order_flag', $str, env('U_COOKIE_EXPIRE'), null, null, false, false); Cookie::queue('send_order_flag_' . $param, null, -1); } //pv Redis::hincrby('send_order_pv_' . $param, $key, 1);//每天 Redis::hincrby('send_order_pv_' . $param, 'total', 1);//汇总 Redis::sadd('send_order' . $key, $param); $uv = Redis::hget('send_order_uv_' . $param, $key); $uv && $uv >= 20 && SendOrderService::updateSendOrderTime($param); } /** * 用户从A连接推送的书,进入后,被标记bookid+派单id,之后所有的统计计算到该派单下 * @param int $send_order_id * @param int $distribution_channel_id * @param int $uid * @param int $bid */ private function specialChannelIdStats( int $send_order_id, int $distribution_channel_id, int $uid, int $bid ):void { if (!$bid || !$distribution_channel_id || !$uid || !$send_order_id) return; $specialChannelIdStats = env('SPECIAL_CHANNEL_STATS', 211); if (!in_array($distribution_channel_id, explode(',', $specialChannelIdStats))) { return; } WapVisitStatService::specialChannelIdStatsMarkUser($uid, $bid, $send_order_id); return; } private function authCallBack(Request $request) { $auth_uid = $request->get('auth_uid'); $atime = $request->get('atime'); $sign = $request->get('sign'); if (!$auth_uid || !$atime || !$sign) return 0; if (!is_numeric($auth_uid) || !is_numeric($atime)) return 0; if ((time() - $atime) > 5) return 0; if (get_sign(compact('auth_uid', 'atime')) !== $sign) return 0; Cookie::queue(env('COOKIE_AUTH_WEB_WECHAT'), $auth_uid, env('U_COOKIE_EXPIRE'), null, null, false, false); return $auth_uid; } private function getAppSecretByAppId($appId) { $info = DB::table('official_setting')->where('appid', $appId)->select('secret')->first(); if ($info) { return $info->secret; } return ''; } private function authReduceRedirect(Request $request,$distribution_channel_id,$send_order_id){ $h5_scheme = env('H5_SCHEME', 'https'); $params = $request->except('_url'); if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR']) { $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip = get_client_ip(); } $params['ip'] = $ip; if(isset($params['auth_uid'])) unset($params['auth_uid']); if(isset($params['atime'])) unset($params['atime']); if(isset($params['sign'])) unset($params['sign']); $url = str_replace('http://', $h5_scheme . '://', url()->current() . '?' . http_build_query($params)); unset($params['ip']); $params['redirect_url'] = urlencode($url); $params['timestamp'] = time(); $params['channel_id'] = $distribution_channel_id; $params['sid'] = $send_order_id; $params['sign'] = $this->getSign($params, env('OAUTH_KEY')); //新的授权 $auth_redirect_one_appids = specialChannelAuthInfoV2($distribution_channel_id); $auth_redirect_two_appids = specialChannelAuthInfo(); if($auth_redirect_one_appids && isset($auth_redirect_one_appids[$distribution_channel_id])){ //授权一次 $callback = env('AUTH_CALLBACK_URL_V2'); $appid = $auth_redirect_one_appids[$distribution_channel_id]; }elseif($auth_redirect_two_appids && isset($auth_redirect_two_appids[$distribution_channel_id]) ){ //授权二次 $callback = env('AUTH_CALLBACK_URL_V2_2'); $appid = env('WECHAT_AUTH_APPID'); }else{ //默认授权 $callback = env('AUTH_CALLBACK_URL_V2'); $appid = env('WECHAT_AUTH_APPID'); } $params['appid'] = $appid; $secret = $this->getAppSecretByAppId($appid); $options = [ 'app_id' => $appid, 'secret' => $secret, 'oauth' => [ 'scopes' => ['snsapi_base'], 'callback' => $callback . '?' . http_build_query($params), ], ]; return $options; } private function crmParam(Request $request){ $crm_param = $request->get('crm'); if($crm_param){ Cookie::queue('crm', $crm_param); } } //内外部模板客服统计埋点 custom_379989 template_75113 private function InnerOuterWeixinStats(Request $request,$uid,$distribution_channel_id){ if($request->has('jump_from') || !$request->has('fromtype')) return ; $from = $request->get('fromtype'); if(!str_contains($from,['custom','template'])){ return ; } $openChannel = redisEnv('OPEN_INNER_OUTER_WEIXIN_STATS'); if(!$openChannel || !in_array($distribution_channel_id,explode(',',$openChannel))) return ; ReadRecordService::setByField($uid,'new_total',$from); $day = date('Y-m-d'); $pv_key = sprintf('InnerOuterWeixinStats:pv:%s:%s',$from,$distribution_channel_id); $uv_key = sprintf('InnerOuterWeixinStats:uv:%s:%s:%s',$from,$distribution_channel_id,$day); Redis::hincrby($pv_key,$day,1); Redis::sadd($uv_key,$uid); Redis::sadd('InnerOuterWeixinStats:'.$day,$from.':'.$distribution_channel_id); } private function crmJump(Request $request,$uid,$en_distribution_channel_id){ $info = DB::table('crm_transfer')->where('from_uid',$uid)->where('is_enable',1)->select('to_uid')->first(); if(!$info)return ''; $uri = $request->input('_url'); if (strpos($uri, '/yun/') !== false) { return ''; } $params = []; $s_param = $request->except('_url'); $filter_key = ['id','bid','cid']; foreach ($s_param as $k=>$v){ if(in_array($k,$filter_key)){ $params[$k] = $v; } } $nowurl = str_replace('http://', env('PROTOCOL') . '://', url()->current() . '?' . http_build_query($params)); $url_info = parse_url($nowurl); $domain = str_replace($en_distribution_channel_id, decodeDistributionChannelId(5204),$url_info['host']); !isset($url_info['path'])&&$url_info['path'] = '/'; if($params){ $return_url = sprintf('%s://%s%s?%s', env('PROTOCOL'), $domain, $url_info['path'], http_build_query($params) ); }else{ $return_url = sprintf('%s://%s%s', env('PROTOCOL'), $domain, $url_info['path'] ); } return $return_url; } }