123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409 |
- <?php
- namespace App\Http\Controllers\Wap\User;
- use App\Modules\Activity\Services\ActivityService;
- use App\Modules\Book\Services\BookConfigService;
- use App\Modules\User\Models\User;
- use App\Modules\User\Services\ReadRecordService;
- use App\Modules\User\Services\UserBindHkWelfareService;
- use App\Modules\User\Services\UserService;
- use Illuminate\Http\Request;
- use App\Http\Controllers\Controller;
- use Hashids;
- use EasyWeChat\Foundation\Application;
- use Log;
- use Exception;
- use Redis;
- use Cookie;
- use DB;
- /**
- * 朋友圈链接
- * Class CoflController
- * @package App\Http\Controllers\Wap\User
- */
- class CoflController extends Controller
- {
- public function index(Request $request){
- $bid = $request->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','reward');
- $source = $request->get('wx','none');
- 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){
- //已经领过
- if(isset($user[2]) && !empty($user[2]) && isset($user[3]) && !empty($user[3])){
- $bid = Hashids::encode($user[2]);
- $cid = $user[3];
- $params['bid'] = $bid;
- $params['cid'] = $cid;
- $link = $this->getLink($get_free_currency->distribution_channel_id).'reader?'.http_build_query($params);
- }else{
- $link = $this->getLink($get_free_currency->distribution_channel_id).'?'.http_build_query($params);
- }
- return view('jump.bindHkFreeCurrency',['fee'=>$fee,'url'=>$link,'is_get'=>1,'uid'=>$get_free_currency->uid,'source'=>$source]);
- }else{
- if(isset($user[2]) && !empty($user[2]) && isset($user[3]) && !empty($user[3])){
- $bid = Hashids::encode($user[2]);
- $cid = $user[3];
- $params['bid'] = $bid;
- $params['cid'] = $cid;
- $link = $this->getLink($user[1]).'reader?'.http_build_query($params);
- }else{
- $link = $this->getLink($user[1]).'?'.http_build_query($params);
- }
- $this->getReward($user[0],$fee,$source);
- return view('jump.bindHkFreeCurrency',['fee'=>$fee,'url'=>$link,'is_get'=>0,'uid'=>$user[0],'source'=>$source]);
- }
- }
- 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);
- $source = $request->post('source','');
- $result = UserBindHkWelfareService::getfreeCurrency($uid,$fee,'CRM',$source);
- if($result){
- UserService::addBalance($uid,$fee,0,$fee);
- }
- return response()->success(['uid'=>$uid,'result'=>$result]);
- }
- private function getReward($uid,$fee,$source){
- $result = UserBindHkWelfareService::getfreeCurrency($uid,$fee,'CRM',$source);
- if($result){
- UserService::addBalance($uid,$fee,0,$fee);
- }
- }
- 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,0,0];
- $temp = null;
- $distribution_channel_id = 0;
- $uid = 0;
- $bid = 0;
- $cid = 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;
- $bid = $last_read_info[0];
- $cid = $last_read_info[1];
- }
- }
- }
- return [$uid,$distribution_channel_id,$bid,$cid];
- }
- 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);
- }
- public function longActivity(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('activity',$crm);
- $baselink = $this->getLink($distribution_channel_id).'activity/crm?'.http_build_query($params);
- if(isset($params['token']) && !empty($params['token'])){
- return redirect()->to($baselink.'activity/crm?'.http_build_query($params));
- }
- return redirect()->to($baselink);
- }
- public function guidePersonalAccount(Request $request,$channel_id){
- $uid = $request->get('uid',0);
- $env_config = redisEnvMulti('GUIDE_PERSONAL_ACCOUNT_OURS_QRCODE','GUIDE_PERSONAL_ACCOUNT_MAX_USER','GUIDE_PERSONAL_ACCOUNT_ID');
- $img = empty($env_config[0])?'https://cdn-novel.iycdm.com/h5/personal_account/chenchen.jpg':$env_config[0];
- $max = empty($env_config[1])?100:$env_config[1];
- $now_id = (int)$env_config[2];
- $uv = Redis::scard('guide_personal_uv');
- if($uv>=$max){
- DB::table('personal_account_list')->where('id',$now_id)->update([
- 'status'=>2,
- 'count'=>$uv,
- 'updated_at'=>date('Y-m-d H:i:s')
- ]);
- $account = DB::table('personal_account_list')
- ->where('is_enable',1)
- ->where('status',0)
- ->select('id','url')
- ->orderBy('id')
- ->first();
- Redis::del('guide_personal_uv');
- if($account){
- DB::table('personal_account_list')->where('id',$account->id)->update([
- 'status'=>1,
- 'updated_at'=>date('Y-m-d H:i:s')
- ]);
- $img = $account->url;
- $now_id = $account->id;
- Redis::Hmset('env','GUIDE_PERSONAL_ACCOUNT_OURS_QRCODE',$account->url,'GUIDE_PERSONAL_ACCOUNT_ID',$account->id);
- }else{
- return back();
- }
- }
- Redis::sadd('guide_personal_uv',$uid);
- if($uid){
- DB::table('ad_pdd')->insert([
- 'uid'=>$uid,
- 'img'=>'GUIDE_PERSONAL_ACCOUNT_'.$now_id,
- 'date'=>date('Y-m-d'),
- 'created_at'=>date('Y-m-d H:i:s'),
- 'updated_at'=>date('Y-m-d H:i:s')
- ]);
- }
- /*if(in_array($channel_id,explode(',',redisEnv('GUIDE_PERSONAL_ACCOUNT_WEID_SITES')))){
- $imgs = redisEnv('GUIDE_PERSONAL_ACCOUNT_WEID_QRCODE',[]);
- if($imgs) $imgs = json_decode($imgs,1);
- $page = 'jump.guidePersonalAccount';
- }else{
- $imgs = redisEnv('GUIDE_PERSONAL_ACCOUNT_OURS_QRCODE',[]);
- if($imgs) $imgs = json_decode($imgs,1);
- $page = 'jump.guidePersonalAccountOurs';
- }*/
- $page = 'jump.guidePersonalAccountOurs';
- //$img = collect($imgs)->random();
- return view($page,['img'=>$img]);
- }
- }
|