CoflController.php 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. <?php
  2. namespace App\Http\Controllers\Wap\User;
  3. use App\Modules\Activity\Services\ActivityService;
  4. use App\Modules\Book\Services\BookConfigService;
  5. use App\Modules\Channel\Models\Channel;
  6. use App\Modules\Subscribe\Models\BookOrder;
  7. use App\Modules\Subscribe\Models\YearOrder;
  8. use App\Modules\Subscribe\Services\YearOrderService;
  9. use App\Modules\User\Models\User;
  10. use App\Modules\User\Services\ReadRecordService;
  11. use App\Modules\User\Services\UserBindHkWelfareService;
  12. use App\Modules\User\Services\UserService;
  13. use Illuminate\Http\Request;
  14. use App\Http\Controllers\Controller;
  15. use Hashids;
  16. use EasyWeChat\Foundation\Application;
  17. use Log;
  18. use Exception;
  19. use Redis;
  20. use Cookie;
  21. use DB;
  22. /**
  23. * 朋友圈链接
  24. * Class CoflController
  25. * @package App\Http\Controllers\Wap\User
  26. */
  27. class CoflController extends Controller
  28. {
  29. public function index(Request $request){
  30. $bid = $request->get('bid');
  31. if(empty($bid)){
  32. $default_ink = $this->getLink();
  33. return redirect()->to($default_ink);
  34. }
  35. $openid = $request->get('openid');
  36. //授权
  37. $params = $request->except('_url');
  38. if(empty($openid)){
  39. //$url = str_replace('http://', env('PROTOCOL') . '://', url()->current() . '?' . http_build_query($params));
  40. $url = url()->current() . '?' . http_build_query($params);
  41. $params['redirect_url'] = urlencode($url);
  42. $app = new Application($this->auth($params));
  43. return $app->oauth->redirect();
  44. }
  45. try{
  46. if(is_numeric($bid) && $bid == 6500 ){
  47. $params['bid'] = Hashids::encode($bid);
  48. }else{
  49. $bid = Hashids::decode($bid)[0];
  50. }
  51. }catch (Exception $e){
  52. return redirect()->to($this->getLink());
  53. }
  54. $crm = $request->get('crm','book_'.$bid);
  55. if(!$bid) return redirect()->to($this->getLink());
  56. //获取用户
  57. //$user = $this->getUsers($openid);
  58. $test_param = $request->input('test',0);
  59. //if($test_param ==1 || $test_param =='1' ) {
  60. $user = $this->getUsersV2($openid);
  61. //}
  62. if(!$user[0]){
  63. $user[1] = 123;
  64. }
  65. //有阅读纪录的跳转
  66. $read_record = ReadRecordService::getByField($user[0],$bid);
  67. if($read_record){
  68. $cid = explode('_',$read_record)[0];
  69. }else{
  70. //没有阅读记录的跳转
  71. $book_info = BookConfigService::getBookById($bid);
  72. $cid = $book_info->first_cid;
  73. }
  74. $params['cid'] = $cid;
  75. $params['crm'] ='book_'.$bid;
  76. if(isset($params['openid'])) unset($params['openid']);
  77. if(isset($params['unionid'])) unset($params['unionid']);
  78. $url = $this->getLink($user[1]).'reader?'.http_build_query($params);
  79. $this->stats('book',$crm);
  80. $this->statsDetail($user[0],'book',$bid);
  81. return redirect()->to($url);
  82. }
  83. public function freeCurrencyView(Request $request){
  84. $openid = $request->get('openid');
  85. //授权
  86. $params = $request->except('_url');
  87. if(empty($openid)){
  88. //$url = str_replace('http://', env('PROTOCOL') . '://', url()->current() . '?' . http_build_query($params));
  89. $url = url()->current() . '?' . http_build_query($params);
  90. $params['redirect_url'] = urlencode($url);
  91. $app = new Application($this->auth($params));
  92. return $app->oauth->redirect();
  93. }
  94. $uri = $request->path();
  95. $token = $request->get('token');
  96. $rfee = $request->get('amount');
  97. $crm = $request->get('crm','reward');
  98. $source = $request->get('wx','none');
  99. $type = 'CRM';
  100. $user = $this->getUsersV2($openid);
  101. if($uri == 'freethrhcurrency'){
  102. //$fee = 300;
  103. $fee = 500;
  104. $this->stats('reward_300',$crm);
  105. }elseif($uri == 'wfreecurrency'){
  106. $fee = 200;
  107. $type = 'WCRM';
  108. $user = $this->getUsers($openid);
  109. }elseif ($uri == 'frocefreethrhcurrency'){
  110. //$fee = 200;
  111. $fee = 500;
  112. $type = 'CRM_FORCE';
  113. }
  114. else{
  115. $this->stats('reward_200',$crm);
  116. $fee = $this->freeCurrencyFee($token,$rfee);
  117. }
  118. //$user = $this->getUsers($openid);
  119. if(!$user || !$user[0] || !$user[1]) return redirect()->to($this->getLink().'?'.http_build_query($params));
  120. if($type == 'CRM'){
  121. $this->statsDetail($user[0],'reward',0);
  122. }elseif ($type == 'CRM_FORCE'){
  123. $this->statsDetail($user[0],'forcereward',0);
  124. } else{
  125. $this->statsDetail($user[0],'wreward',0);
  126. }
  127. $get_free_currency = UserBindHkWelfareService::isHasGet($user[0]);
  128. if($get_free_currency){
  129. //已经领过
  130. if(isset($user[2]) && !empty($user[2]) && isset($user[3]) && !empty($user[3])){
  131. $bid = Hashids::encode($user[2]);
  132. $cid = $user[3];
  133. $params['bid'] = $bid;
  134. $params['cid'] = $cid;
  135. $link = $this->getLink($get_free_currency->distribution_channel_id).'reader?'.http_build_query($params);
  136. }else{
  137. $link = $this->getLink($get_free_currency->distribution_channel_id).'recent?'.http_build_query($params);
  138. }
  139. return view('jump.bindHkFreeCurrency',['fee'=>$fee,'url'=>$link,'is_get'=>1,'uid'=>$get_free_currency->uid,'source'=>$source]);
  140. }else{
  141. if(isset($user[2]) && !empty($user[2]) && isset($user[3]) && !empty($user[3])){
  142. $bid = Hashids::encode($user[2]);
  143. $cid = $user[3];
  144. $params['bid'] = $bid;
  145. $params['cid'] = $cid;
  146. $link = $this->getLink($user[1]).'reader?'.http_build_query($params);
  147. }else{
  148. $link = $this->getLink($user[1]).'recent?'.http_build_query($params);
  149. }
  150. $this->getReward($user[0],$fee,$source,$type);
  151. return view('jump.bindHkFreeCurrency',['fee'=>$fee,'url'=>$link,'is_get'=>0,'uid'=>$user[0],'source'=>$source]);
  152. }
  153. }
  154. public function freeCurrencyViewOther(Request $request){
  155. $openid = $request->get('openid');
  156. //授权
  157. $params = $request->except('_url');
  158. if(empty($openid)){
  159. $url = url()->current() . '?' . http_build_query($params);
  160. $params['redirect_url'] = urlencode($url);
  161. $app = new Application($this->auth($params));
  162. return $app->oauth->redirect();
  163. }
  164. $token = $request->get('token');
  165. $fee = 0;
  166. $type = 'NCRM_'.$fee;
  167. if($token){
  168. $info = DB::table('crm_free_currency_token')->where('token',$token)->where('is_enable',1)->first();
  169. if($info) {
  170. $fee = $info->amount;
  171. $type = $info->type;
  172. }
  173. }
  174. $user = $this->getUsersV2($openid);
  175. if(!$user || !$user[0] || !$user[1] || !$fee) return redirect()->to($this->getLink().'?'.http_build_query($params));
  176. $crm = 'new_reward';
  177. $source = $request->get('wx','none');
  178. $this->stats($crm.'_'.$fee,$crm);
  179. $this->statsDetail($user[0],'new_reward_'.$fee,0);
  180. $get_free_currency = UserBindHkWelfareService::getByOpenidToken($openid,$token);
  181. if($get_free_currency){
  182. //已经领过
  183. $link = $this->getLink($user[1]).'recent?'.http_build_query($params);
  184. return view('crm.bindHkFreeCurrencyV2',['fee'=>$fee,'url'=>$link,'is_get'=>1,'uid'=>$get_free_currency->uid,'source'=>$source]);
  185. }else{
  186. $link = $this->getLink($user[1]).'recent?'.http_build_query($params);
  187. //$this->getReward($user[0],$fee,$source,$type,$token);
  188. $this->getRewardV2($user[0],$openid,$fee,$type,$source,$token);
  189. return view('crm.bindHkFreeCurrencyV2',['fee'=>$fee,'url'=>$link,'is_get'=>0,'uid'=>$user[0],'source'=>$source]);
  190. }
  191. }
  192. private function freeCurrencyFee($token,$fee){
  193. if(!$token || !$fee) return 200;
  194. $param['fee'] = $fee;
  195. if( _sign($param,env('SECRET_KEY')) == $token) return $fee;
  196. return 200;
  197. }
  198. public function freeCurrencyPost(Request $request){
  199. $uid = $request->post('uid');
  200. $fee = $request->post('fee',200);
  201. $source = $request->post('source','');
  202. $result = UserBindHkWelfareService::getfreeCurrency($uid,$fee,'CRM',$source);
  203. if($result){
  204. UserService::addBalance($uid,$fee,0,$fee);
  205. }
  206. return response()->success(['uid'=>$uid,'result'=>$result]);
  207. }
  208. private function getReward($uid,$fee,$source,$crm='CRM'){
  209. $result = UserBindHkWelfareService::getfreeCurrency($uid,$fee,$crm,$source);
  210. if($result){
  211. UserService::addBalance($uid,$fee,0,$fee);
  212. }
  213. }
  214. private function getRewardV2($uid,$openid,$fee,$type,$source,$token){
  215. $result = UserBindHkWelfareService::getfreeCurrencyV2($uid,$openid,$fee,$type,$source,$token);
  216. if($result){
  217. UserService::addBalance($uid,$fee,0,$fee);
  218. }
  219. }
  220. public function activity(Request $request){
  221. $token = $request->get('token');
  222. if(empty($token)){
  223. $default_ink = $this->getLink();
  224. return redirect()->to($default_ink);
  225. }
  226. $openid = $request->get('openid');
  227. //授权
  228. $params = $request->except('_url');
  229. if(empty($openid)){
  230. $url = url()->current() . '?' . http_build_query($params);
  231. $params['redirect_url'] = urlencode($url);
  232. $app = new Application($this->auth($params));
  233. return $app->oauth->redirect();
  234. }
  235. $crm = $request->get('crm');
  236. $this->stats('activity',$crm);
  237. $activity = ActivityService::getByToken($token);
  238. if($activity){
  239. $user = $this->getUsersV2($openid);
  240. $distribution_channel_id = (isset($user[1]) && !empty($user[1]))?$user[1]:123;
  241. $url_format = '%s://site%s.%s.com%s';
  242. $activity_page = $activity->activity_page ;
  243. if($activity->id == 6000){
  244. $activity_page = '/activity/common?token=LNyAqbFMgvkmvnHP8PXV3DYPIIhQm3oe';
  245. }
  246. $url = sprintf($url_format, env('PROTOCOL'), encodeDistributionChannelId($distribution_channel_id),
  247. env('CUSTOM_HOST'),
  248. $activity_page
  249. );
  250. return redirect()->to($url);
  251. }
  252. $default_ink = $this->getLink();
  253. return redirect()->to($default_ink);
  254. }
  255. public function indexRoot(Request $request){
  256. $openid = $request->get('openid');
  257. //授权
  258. $params = $request->except('_url');
  259. if(empty($openid)){
  260. $url = url()->current() . '?' . http_build_query($params);
  261. $params['redirect_url'] = urlencode($url);
  262. $app = new Application($this->auth($params));
  263. return $app->oauth->redirect();
  264. }
  265. list($uid,$distribution_channel_id) = $this->getUsersV2($openid);
  266. $link = $this->getLink($distribution_channel_id);
  267. $crm = $request->get('crm','index');
  268. $params['crm'] = $crm;
  269. $link = $link.'?'.http_build_query($params);
  270. $this->stats('index',$crm);
  271. $this->statsDetail($uid,'index',0);
  272. return redirect()->to($link);
  273. }
  274. public function recent(Request $request){
  275. $openid = $request->get('openid');
  276. //授权
  277. $params = $request->except('_url');
  278. if(empty($openid)){
  279. $url = url()->current() . '?' . http_build_query($params);
  280. $params['redirect_url'] = urlencode($url);
  281. $app = new Application($this->auth($params));
  282. return $app->oauth->redirect();
  283. }
  284. $user = $this->getUsersV2($openid);
  285. if(!$user[0]){
  286. $distribution_channel_id = 123;
  287. }else{
  288. $distribution_channel_id = $user[1];
  289. }
  290. $crm = $request->get('crm');
  291. $this->stats('recent',$crm);
  292. $this->statsDetail($user[0],'recent',0);
  293. $link = $this->getLink($distribution_channel_id).'recent?'.http_build_query($params);
  294. return redirect()->to($link);
  295. }
  296. public function person(Request $request){
  297. $openid = $request->get('openid');
  298. //授权
  299. $params = $request->except('_url');
  300. if(empty($openid)){
  301. $url = url()->current() . '?' . http_build_query($params);
  302. $params['redirect_url'] = urlencode($url);
  303. $app = new Application($this->auth($params));
  304. return $app->oauth->redirect();
  305. }
  306. $user = $this->getUsersV2($openid);
  307. if(!$user[0]){
  308. $distribution_channel_id = 123;
  309. }else{
  310. $distribution_channel_id = $user[1];
  311. }
  312. $crm = $request->get('crm');
  313. $this->stats('person',$crm);
  314. $this->statsDetail($user[0],'person',0);
  315. $link = $this->getLink($distribution_channel_id).'person?'.http_build_query($params);
  316. return redirect()->to($link);
  317. }
  318. public function sign(Request $request){
  319. $openid = $request->get('openid');
  320. //授权
  321. $params = $request->except('_url');
  322. if(empty($openid)){
  323. $url = url()->current() . '?' . http_build_query($params);
  324. $params['redirect_url'] = urlencode($url);
  325. $app = new Application($this->auth($params));
  326. return $app->oauth->redirect();
  327. }
  328. $user = $this->getUsersV2($openid);
  329. if(!$user[0]){
  330. $distribution_channel_id = 123;
  331. }else{
  332. $distribution_channel_id = $user[1];
  333. }
  334. $crm = $request->get('crm');
  335. $uri = $request->path();
  336. if($uri == 'sign'){
  337. $this->stats('sign',$crm);
  338. $this->statsDetail($user[0],'sign',0);
  339. }elseif($uri == 'wsign'){
  340. $this->stats('wsign',$crm);
  341. $this->statsDetail($user[0],'wsign',0);
  342. }
  343. $link = $this->getLink($distribution_channel_id).'sign?'.http_build_query($params);
  344. return redirect()->to($link);
  345. }
  346. private function getUsers($openid){
  347. $users = User::where('openid',$openid)->select('id','distribution_channel_id')->get();
  348. if($users->isEmpty()) return [0,0,0,0];
  349. $temp = null;
  350. $distribution_channel_id = 0;
  351. $uid = 0;
  352. $bid = 0;
  353. $cid = 0;
  354. foreach ($users as $user){
  355. $last_read = ReadRecordService::getByField($user->id,'last_read');
  356. if(!$last_read) continue;
  357. if(!$temp ){
  358. $distribution_channel_id = $user->distribution_channel_id;
  359. $uid = $user->id;
  360. $temp = $last_read;
  361. }else{
  362. $temp_last_read_info = explode('_',$temp);
  363. $last_read_info = explode('_',$last_read);
  364. if($last_read_info[2] > $temp_last_read_info[2]){
  365. $uid = $user->id;
  366. $distribution_channel_id = $user->distribution_channel_id;
  367. $temp = $last_read;
  368. $bid = $last_read_info[0];
  369. $cid = $last_read_info[1];
  370. }
  371. }
  372. }
  373. return [$uid,$distribution_channel_id,$bid,$cid];
  374. }
  375. /**
  376. * @param $openid
  377. * @return array
  378. */
  379. private function getUsersV2($openid){
  380. $friend_link_uid_bind = DB::table('friend_link_uid_bind')->where('openid',$openid)->orderBy('id','desc')->first();
  381. if($friend_link_uid_bind) {
  382. $user = User::where('id',$friend_link_uid_bind->uid)->select('id','distribution_channel_id')->first();
  383. if($user) {
  384. return [$user->id,$user->distribution_channel_id];
  385. }
  386. }
  387. //外部渠道导粉
  388. if(Redis::hget('crm:out_guide_exposure_user',$openid)){
  389. Log::info('------------getUsersV2-outer---start--------------------------------');
  390. Log::info('getUsersV2 $openid is:'.$openid);
  391. $user = $this->createUser($openid,$openid,5204,0,'');
  392. Log::info('$user = $this->createUser($openid,$openid,5204,0):');
  393. Log::info($user);
  394. $uid = $user->id;
  395. $distribution_channel_id = 5204;
  396. $ouid = Redis::hget('crm:out_guide_exposure_user',$openid);
  397. Log::info('$ouid is :'.$ouid);
  398. UserService::transfer($ouid,$uid,5204);
  399. $insert_data = ['uid'=>$uid,'openid'=>$openid ,'created_at'=>date('Y-m-d H:i:s'),'updated_at'=>date('Y-m-d H:i:s')];
  400. DB::table('friend_link_uid_bind')->insert($insert_data);
  401. Redis::hdel('crm:out_guide_exposure_user',$openid);
  402. Log::info([$uid,$distribution_channel_id]);
  403. Log::info('------------getUsersV2-outer---end--------------------------------');
  404. return [$uid,$distribution_channel_id];
  405. }
  406. $inner_channels = Channel::join('channel_users','channel_users.id','=','distribution_channels.channel_user_id')
  407. ->select('distribution_channels.id')
  408. ->whereIn('channel_users.id',explode(',',redisEnv('PROMOTION_GROUP_CHANNEL_USER_ID')))
  409. ->get()
  410. ->pluck('id')
  411. ->toArray();
  412. //\Log::info($inner_channels);
  413. $users = User::where('openid',$openid)->select('id','distribution_channel_id')->get();
  414. \Log::info('users:openid:'.$openid);
  415. if($users->isEmpty()) return [0,0];
  416. $temp = null;
  417. $distribution_channel_id = 0;
  418. $uid = 0;
  419. $inner_channel_uid_list = [];
  420. //先筛网站运营部UID
  421. foreach ($users as $user){
  422. if(in_array($user->distribution_channel_id,$inner_channels)) {
  423. $inner_channel_uid_list[] = $user;
  424. }
  425. }
  426. if(count($inner_channel_uid_list) == 1) {
  427. $insert_data = ['uid'=>$inner_channel_uid_list[0]->id,'openid'=>$openid ,'created_at'=>date('Y-m-d H:i:s'),'updated_at'=>date('Y-m-d H:i:s')];
  428. \Log::info('anchor:count $inner_channel_uid_list =1');
  429. DB::table('friend_link_uid_bind')->insert($insert_data);
  430. return [$inner_channel_uid_list[0]->id,$inner_channel_uid_list[0]->distribution_channel_id];
  431. }
  432. if(count($inner_channel_uid_list)>1) {
  433. //有多个网站运营部UID,针对网站运营部UID进入到下一步筛选
  434. $users = $inner_channel_uid_list;
  435. }
  436. //筛选包年UID
  437. $uid_list = [];
  438. foreach ($users as $item) {
  439. $uid_list[] =$item->id;
  440. }
  441. $year_order = YearOrder::whereIn('uid',$uid_list)->orderBy('end_time','desc')->first();
  442. if($year_order) {
  443. foreach ($users as $item) {
  444. if($item->id == $year_order->uid) {
  445. //筛选包年结束时间最后的UID
  446. $insert_data = ['uid'=>$item->id,'openid'=>$openid ,'created_at'=>date('Y-m-d H:i:s'),'updated_at'=>date('Y-m-d H:i:s')];
  447. \Log::info('anchor:year_order');
  448. DB::table('friend_link_uid_bind')->insert($insert_data);
  449. return [$item->id,$item->distribution_channel_id];
  450. }
  451. }
  452. }
  453. //按订阅章节数最多的筛选
  454. $users_select = ['id'=>0,'count'=>0,'user'=>null,'create_time'=>0];
  455. foreach ($users as $each){
  456. $table_prefix = ($each->id)%512;
  457. $chapter_count = DB::connection('chapter_order_mysql')
  458. ->table('chapter_orders'.$table_prefix)
  459. ->where('created_at','>=',date('Y-m-d H:i:s',strtotime('-3 day')))
  460. ->where('uid',$each->id)
  461. //->where('bid',$bid)
  462. ->count('id');
  463. $book_order = BookOrder::where('uid',$each->id)
  464. //->where('bid',$bid)
  465. ->where('created_at','>=',date('Y-m-d H:i:s',strtotime('-3 day')))
  466. ->first();
  467. if($book_order) {
  468. $chapter_count += 30;
  469. }
  470. //$each->chapter_count = $chapter_count;
  471. if($chapter_count>$users_select['count']) {
  472. $users_select['id'] = $each->id;
  473. $users_select['count'] = $chapter_count;
  474. $users_select['user'] = $each;
  475. }
  476. }
  477. if($users_select['id'] >0) {
  478. $insert_data = ['uid'=>$users_select['user']->id,'openid'=>$openid ,'created_at'=>date('Y-m-d H:i:s'),'updated_at'=>date('Y-m-d H:i:s')];
  479. \Log::info('anchor:subs_chapter');
  480. DB::table('friend_link_uid_bind')->insert($insert_data);
  481. return [$users_select['user']->id,$users_select['user']->distribution_channel_id];
  482. }
  483. //有订阅记录的
  484. foreach ($users as $value) {
  485. $table_prefix = ($value->id)%512;
  486. $chapter_order = DB::connection('chapter_order_mysql')
  487. ->table('chapter_orders'.$table_prefix)
  488. ->where('uid',$value->id)
  489. //->where('bid',$bid)
  490. ->orderBy('created_at','desc')
  491. ->first();
  492. $book_order = BookOrder::where('uid',$value->id)
  493. //->where('bid',$bid)
  494. ->first();
  495. if($chapter_order ){
  496. if(strtotime($chapter_order->created_at) > $users_select['create_time']) {
  497. $users_select['id'] = $value->id;
  498. $users_select['create_time'] = strtotime($chapter_order->created_at);
  499. $users_select['user'] = $value;
  500. }
  501. }
  502. if($book_order){
  503. if(strtotime($book_order->created_at) > $users_select['create_time']){
  504. $users_select['id']=$value->id;
  505. $users_select['create_time']= strtotime($book_order->created_at);
  506. $users_select['user']= $value;
  507. }
  508. }
  509. }
  510. if($users_select['id'] >0) {
  511. $insert_data = ['uid'=>$users_select['user']->id,'openid'=>$openid ,'created_at'=>date('Y-m-d H:i:s'),'updated_at'=>date('Y-m-d H:i:s')];
  512. \Log::info('anchor:subs_');
  513. DB::table('friend_link_uid_bind')->insert($insert_data);
  514. return [$users_select['user']->id,$users_select['user']->distribution_channel_id];
  515. }
  516. if(count($inner_channel_uid_list)>1) {
  517. //有多个网站运营部UID,针对网站运营部UID进入到下一步筛选
  518. $insert_data = ['uid'=>$inner_channel_uid_list[0]->id,'openid'=>$openid ,'created_at'=>date('Y-m-d H:i:s'),'updated_at'=>date('Y-m-d H:i:s')];
  519. \Log::info('anchor:count $inner_channel_uid_list >1');
  520. DB::table('friend_link_uid_bind')->insert($insert_data);
  521. return [$inner_channel_uid_list[0]['id'],$inner_channel_uid_list[0]['distribution_channel_id']];
  522. }
  523. \Log::info('anchor:over');
  524. return [0,0];
  525. }
  526. private function getUsersV3($openid){
  527. //5204
  528. //\Log::info($inner_channels);
  529. $users = User::where('openid',$openid)->select('id','distribution_channel_id')->get();
  530. if($users->isEmpty()) return [0,0];
  531. if(count($users) == 1){
  532. return [$users->first()->id,$users->first()->distribution_channel_id];
  533. }
  534. $temp = null;
  535. //筛选包年UID
  536. $uid_list = [];
  537. foreach ($users as $item) {
  538. $uid_list[] =$item->id;
  539. }
  540. $year_order = YearOrder::whereIn('uid',$uid_list)->orderBy('end_time','desc')->first();
  541. if($year_order) {
  542. foreach ($users as $item) {
  543. if($item->id == $year_order->uid) {
  544. //筛选包年结束时间最后的UID
  545. return [$item->id,$item->distribution_channel_id];
  546. }
  547. }
  548. }
  549. //按订阅章节数最多的筛选
  550. $users_select = ['id'=>0,'count'=>0,'user'=>null,'create_time'=>0];
  551. foreach ($users as $each){
  552. $table_prefix = ($each->id)%512;
  553. $chapter_count = DB::connection('chapter_order_mysql')
  554. ->table('chapter_orders'.$table_prefix)
  555. ->where('created_at','>=',date('Y-m-d H:i:s',strtotime('-3 day')))
  556. ->where('uid',$each->id)
  557. //->where('bid',$bid)
  558. ->count('id');
  559. $book_order = BookOrder::where('uid',$each->id)
  560. //->where('bid',$bid)
  561. ->where('created_at','>=',date('Y-m-d H:i:s',strtotime('-3 day')))
  562. ->first();
  563. if($book_order) {
  564. $chapter_count += 30;
  565. }
  566. //$each->chapter_count = $chapter_count;
  567. if($chapter_count>$users_select['count']) {
  568. $users_select['id'] = $each->id;
  569. $users_select['count'] = $chapter_count;
  570. $users_select['user'] = $each;
  571. }
  572. }
  573. if($users_select['id'] >0) {
  574. $insert_data = ['uid'=>$users_select['user']->id,'openid'=>$openid ,'created_at'=>date('Y-m-d H:i:s'),'updated_at'=>date('Y-m-d H:i:s')];
  575. return [$users_select['user']->id,$users_select['user']->distribution_channel_id];
  576. }
  577. //有订阅记录的
  578. foreach ($users as $value) {
  579. $table_prefix = ($value->id)%512;
  580. $chapter_order = DB::connection('chapter_order_mysql')
  581. ->table('chapter_orders'.$table_prefix)
  582. ->where('uid',$value->id)
  583. //->where('bid',$bid)
  584. ->orderBy('created_at','desc')
  585. ->first();
  586. $book_order = BookOrder::where('uid',$value->id)
  587. //->where('bid',$bid)
  588. ->first();
  589. if($chapter_order ){
  590. if(strtotime($chapter_order->created_at) > $users_select['create_time']) {
  591. $users_select['id'] = $value->id;
  592. $users_select['create_time'] = strtotime($chapter_order->created_at);
  593. $users_select['user'] = $value;
  594. }
  595. }
  596. if($book_order){
  597. if(strtotime($book_order->created_at) > $users_select['create_time']){
  598. $users_select['id']=$value->id;
  599. $users_select['create_time']= strtotime($book_order->created_at);
  600. $users_select['user']= $value;
  601. }
  602. }
  603. }
  604. if($users_select['id'] >0) {
  605. return [$users_select['user']->id,$users_select['user']->distribution_channel_id];
  606. }
  607. return [0,0];
  608. }
  609. private function getLink($distribution_channel_id=123){
  610. $url_format = '%s://site%s.%s.com/';
  611. return sprintf(
  612. $url_format,
  613. env('PROTOCOL'),
  614. encodeDistributionChannelId($distribution_channel_id),
  615. env('CUSTOM_HOST')
  616. );
  617. }
  618. private function auth($param){
  619. $param['appid'] = 'wx9d389a73b88bbeae';
  620. $options = [
  621. 'app_id' => 'wx9d389a73b88bbeae',
  622. 'secret' => '2f6594bb595dfa256b5512e43a32a3d3',
  623. 'oauth' => [
  624. 'scopes' => ['snsapi_base'],
  625. 'callback' => env('AUTH_CALLBACK_URL') . '?' . http_build_query($param),
  626. ],
  627. ];
  628. return $options;
  629. }
  630. private function stats($page,$flag){
  631. if(!$page || !$flag) return ;
  632. $flag = trim($flag);
  633. //pv
  634. $day = date('Y-m-d');
  635. $pv_key = sprintf('crm:pv:%s',$flag);
  636. Redis::hincrby($pv_key, $day, 1);
  637. //Redis::hincrby($pv_key, 'total', 1);
  638. Redis::sadd('crm_'.$day,$flag);
  639. //uv
  640. $cookie_flag = Cookie::get('crm_flag');
  641. if(!$cookie_flag || $cookie_flag != $flag){
  642. $uv_key = sprintf('crm:uv:%s',$flag);
  643. Redis::hincrby($uv_key, $day, 1);
  644. //Redis::hincrby($uv_key, 'total', 1);
  645. }
  646. Cookie::queue('crm_flag', $flag, env('U_COOKIE_EXPIRE'), null, null, false, false);
  647. }
  648. private function statsDetail($uid,$page,$bid=0)
  649. {
  650. try{
  651. DB::table('crm_visit_detail')->insert([
  652. 'uid'=>$uid,'page'=>$page,
  653. 'bid'=>$bid,'day'=>date('Y-m-d'),
  654. 'created_at'=>date('Y-m-d H:i:s'),
  655. 'updated_at'=>date('Y-m-d H:i:s')
  656. ]);
  657. }catch (\Exception $e){}
  658. }
  659. public function longActivity(Request $request)
  660. {
  661. $openid = $request->get('openid');
  662. //授权
  663. $params = $request->except('_url');
  664. if(empty($openid)){
  665. $url = url()->current() . '?' . http_build_query($params);
  666. $params['redirect_url'] = urlencode($url);
  667. $app = new Application($this->auth($params));
  668. return $app->oauth->redirect();
  669. }
  670. $user = $this->getUsersV2($openid);
  671. if(!$user[0]){
  672. $distribution_channel_id = 123;
  673. }else{
  674. $distribution_channel_id = $user[1];
  675. }
  676. $crm = $request->get('crm');
  677. $this->stats('activity',$crm);
  678. $baselink = $this->getLink($distribution_channel_id).'activity/crm?'.http_build_query($params);
  679. if(isset($params['token']) && !empty($params['token'])){
  680. return redirect()->to($baselink.'activity/crm?'.http_build_query($params));
  681. }
  682. return redirect()->to($baselink);
  683. }
  684. public function yearActivity(Request $request)
  685. {
  686. $openid = $request->get('openid');
  687. //授权
  688. $params = $request->except('_url');
  689. if(empty($openid)){
  690. $url = url()->current() . '?' . http_build_query($params);
  691. $params['redirect_url'] = urlencode($url);
  692. $app = new Application($this->auth($params));
  693. return $app->oauth->redirect();
  694. }
  695. $user = $this->getUsersV2($openid);
  696. if(!$user[0]){
  697. $distribution_channel_id = 123;
  698. }else{
  699. $distribution_channel_id = $user[1];
  700. }
  701. $crm = $request->get('crm','crm_year_activity');
  702. $params['crm'] = $crm;
  703. $this->stats('activity',$crm);
  704. $this->statsDetail($user[0],'year_activity',0);
  705. $baselink = $this->getLink($distribution_channel_id).'activity/crmy?'.http_build_query($params);
  706. return redirect()->to($baselink);
  707. }
  708. public function guidePersonal(Request $request){
  709. $openid = $request->get('openid');
  710. //授权
  711. $params = $request->except('_url');
  712. if(empty($openid)){
  713. $url = url()->current() . '?' . http_build_query($params);
  714. $params['redirect_url'] = urlencode($url);
  715. $app = new Application($this->auth($params));
  716. return $app->oauth->redirect();
  717. }
  718. list($uid,$distribution_channel_id) = $this->getUsersV2($openid);
  719. $link = $this->getLink($distribution_channel_id);
  720. $get_info = UserBindHkWelfareService::isHasGet($uid);
  721. if($get_info) redirect()->to($link);
  722. $crm = $request->get('crm','guide_personal');
  723. $params['crm'] = $crm;
  724. $params['uid'] = $uid;
  725. $link = $link.'guidestrem?'.http_build_query($params);
  726. $this->stats('index',$crm);
  727. $this->statsDetail($uid,'guidePersonal',0);
  728. return redirect()->to($link);
  729. }
  730. public function guidePersonalAccount(Request $request,$channel_id){
  731. $uid = $request->get('uid',0);
  732. //$user_cookie = Cookie::get(env('COOKIE_AUTH_WEB_WECHAT'));
  733. //$uid = $user_cookie ? decrypt($user_cookie) : null;
  734. $cookie_crm_img = Cookie::get('crm_person_img');
  735. $cookie_crm_name = Cookie::get('crm_person_name','');
  736. $request_img = $request->get('img');
  737. $fee = $request->get('fee',200);
  738. if($request_img){
  739. $name = $request_img = $request->get('name');
  740. return view('jump.guidePersonalAccountOurs',['img'=>$cookie_crm_img,'name'=>$name,'fee'=>$fee]);
  741. }
  742. $get_info = UserBindHkWelfareService::isHasGet($uid);
  743. if($get_info) return back();
  744. if($cookie_crm_img){
  745. return view('jump.guidePersonalAccountOurs',['img'=>$cookie_crm_img,'name'=>$cookie_crm_name,'fee'=>$fee]);
  746. }
  747. $distribution_channel_id = decodeDistributionChannelId($channel_id);
  748. //$distribution_channel_id = $channel_id;
  749. //if(in_array($distribution_channel_id,Redis::SISMEMBER('')));
  750. //$out = false;
  751. $group = 'ACTIVE';
  752. if(Redis::SISMEMBER('crm:out_channel_sites',$distribution_channel_id)){
  753. //$out = true;
  754. $group = 'OUT_ACTIVE';
  755. $env_config = redisEnvMulti('OUT_ACTIVE_GUIDE_PERSONAL_ACCOUNT_OURS_QRCODE',
  756. 'OUT_ACTIVE_GUIDE_PERSONAL_ACCOUNT_MAX_UV','OUT_ACTIVE_GUIDE_PERSONAL_ACCOUNT_ID',
  757. 'OUT_ACTIVE_GUIDE_PERSONAL_ACCOUNT_MAX_EVERY_UV');
  758. }else{
  759. $env_config = redisEnvMulti('ACTIVE_GUIDE_PERSONAL_ACCOUNT_OURS_QRCODE',
  760. 'ACTIVE_GUIDE_PERSONAL_ACCOUNT_MAX_UV','ACTIVE_GUIDE_PERSONAL_ACCOUNT_ID',
  761. 'ACTIVE_GUIDE_PERSONAL_ACCOUNT_MAX_EVERY_UV');
  762. }
  763. if(empty($env_config[0])){
  764. return back();
  765. }
  766. //$img = empty($env_config[0])?'https://cdn-novel.iycdm.com/h5/personal_account/chenchen.jpg':$env_config[0];
  767. $img =$env_config[0];
  768. $max = empty($env_config[1])?100:$env_config[1];
  769. $max = (int)$max;
  770. $one_loop_max = empty($env_config[3])?10:$env_config[3];
  771. $now_id = (int)$env_config[2];
  772. if($group == 'OUT_ACTIVE'){
  773. $uv_key = 'out_active_guide_personal_uv';
  774. }else{
  775. $uv_key = 'active_guide_personal_uv';
  776. }
  777. $uv = Redis::scard($uv_key);
  778. $personal_info = DB::table('personal_account_list')->where('id',$now_id)->select('count')->first();
  779. if(!$personal_info){
  780. return back();
  781. }
  782. $total_max = (int)($personal_info->count+$uv);
  783. //Log::info('$total_max is: '.);
  784. if($uv >= $one_loop_max || $total_max>=$max){
  785. if($total_max>=$max){
  786. DB::table('personal_account_list')->where('id',$now_id)->update([
  787. 'status'=>2,
  788. 'count'=>$total_max,
  789. 'updated_at'=>date('Y-m-d H:i:s')
  790. ]);
  791. }else{
  792. DB::table('personal_account_list')->where('id',$now_id)->increment('count',$uv,[
  793. 'updated_at'=>date('Y-m-d H:i:s')
  794. ]);
  795. }
  796. $account = DB::table('personal_account_list')
  797. ->where('is_enable',1)
  798. ->whereIn('status',[1,0])
  799. ->select('id','url')
  800. ->where('group',$group)
  801. ->where('count','<',$max)
  802. ->orderBy('count','asc')
  803. ->orderBy('id')
  804. ->first();
  805. Redis::del($uv_key);
  806. if($account){
  807. DB::table('personal_account_list')->where('id',$account->id)->update([
  808. 'status'=>1,
  809. 'updated_at'=>date('Y-m-d H:i:s')
  810. ]);
  811. $img = $account->url;
  812. $now_id = $account->id;
  813. Redis::Hmset('env',$group.'_GUIDE_PERSONAL_ACCOUNT_OURS_QRCODE',$account->url,$group.'_GUIDE_PERSONAL_ACCOUNT_ID',$account->id);
  814. }else{
  815. Redis::Hmset('env',$group.'_GUIDE_PERSONAL_ACCOUNT_OURS_QRCODE','',$group.'_GUIDE_PERSONAL_ACCOUNT_ID','');
  816. return back();
  817. }
  818. }
  819. Redis::sadd($uv_key,$uid);
  820. if($uid){
  821. if($group == 'OUT_ACTIVE'){
  822. DB::table('ad_pdd')->insert([
  823. 'uid'=>$uid,
  824. 'img'=>'OUT_GUIDE_PERSONAL_ACCOUNT_'.$now_id,
  825. 'date'=>date('Y-m-d'),
  826. 'created_at'=>date('Y-m-d H:i:s'),
  827. 'updated_at'=>date('Y-m-d H:i:s')
  828. ]);
  829. $user = UserService::getById($uid);
  830. Redis::hset('crm:out_guide_exposure_user',$user->openid,$uid);
  831. }else{
  832. DB::table('ad_pdd')->insert([
  833. 'uid'=>$uid,
  834. 'img'=>'GUIDE_PERSONAL_ACCOUNT_'.$now_id,
  835. 'date'=>date('Y-m-d'),
  836. 'created_at'=>date('Y-m-d H:i:s'),
  837. 'updated_at'=>date('Y-m-d H:i:s')
  838. ]);
  839. }
  840. }
  841. $name = '';
  842. $name_info = DB::table('personal_account_list')->where('id',$now_id)->select('name')->first();
  843. if($name_info){
  844. $name = $name_info->name;
  845. }
  846. /*if(in_array($channel_id,explode(',',redisEnv('GUIDE_PERSONAL_ACCOUNT_WEID_SITES')))){
  847. $imgs = redisEnv('GUIDE_PERSONAL_ACCOUNT_WEID_QRCODE',[]);
  848. if($imgs) $imgs = json_decode($imgs,1);
  849. $page = 'jump.guidePersonalAccount';
  850. }else{
  851. $imgs = redisEnv('GUIDE_PERSONAL_ACCOUNT_OURS_QRCODE',[]);
  852. if($imgs) $imgs = json_decode($imgs,1);
  853. $page = 'jump.guidePersonalAccountOurs';
  854. }*/
  855. $page = 'jump.guidePersonalAccountOurs';
  856. $time = strtotime(date('Y-m-d',time()+86400))-time();
  857. Cookie::queue('crm_person_img', $img,$time);
  858. Cookie::queue('crm_person_name', $name,$time);
  859. //$page = 'jump.guidePersonalAccount';
  860. //$img = collect($imgs)->random();
  861. return view($page,['img'=>$img,'name'=>$name,'fee'=>$fee]);
  862. }
  863. public function r(Request $request,$number){
  864. switch ($number){
  865. case 1:
  866. //首页
  867. break;
  868. case 2:
  869. //阅读器
  870. break;
  871. case 3:
  872. //阅读器
  873. break;
  874. case 4:
  875. //个人中心
  876. break;
  877. case 5:
  878. //最近阅读
  879. break;
  880. case 6:
  881. }
  882. }
  883. public function freeCurrencyViewForTransfer(Request $request){
  884. $openid = $request->get('openid');
  885. //授权
  886. $params = $request->except('_url');
  887. if(empty($openid)){
  888. //$url = str_replace('http://', env('PROTOCOL') . '://', url()->current() . '?' . http_build_query($params));
  889. $url = url()->current() . '?' . http_build_query($params);
  890. $params['redirect_url'] = urlencode($url);
  891. $app = new Application($this->auth($params));
  892. return $app->oauth->redirect();
  893. }
  894. $crm = $request->get('crm','treward');
  895. $source = $request->get('wx','none');
  896. $fee = 500;
  897. $type = 'TCRM';
  898. $this->stats('treward_500',$crm);
  899. $crm_transfer_uid_bind = DB::table('crm_transfer')->where('openid',$openid)->first();
  900. if($crm_transfer_uid_bind) {
  901. $user = User::where('id',$crm_transfer_uid_bind->to_uid)->select('id','distribution_channel_id')->first();
  902. $uid = $crm_transfer_uid_bind->to_uid;
  903. $distribution_channel_id = 5204;
  904. }else{
  905. list($ouid,$distribution_channel_id) = $this->getUsersV3($openid);
  906. $user = $this->createUser($openid,$openid,5204,0,'');
  907. $uid = $user->id;
  908. $distribution_channel_id = 5204;
  909. UserService::transfer($ouid,$uid,5204);
  910. $insert_data = ['uid'=>$uid,'openid'=>$openid ,'created_at'=>date('Y-m-d H:i:s'),'updated_at'=>date('Y-m-d H:i:s')];
  911. DB::table('friend_link_uid_bind')->insert($insert_data);
  912. }
  913. if(!$uid) return redirect()->to($this->getLink($distribution_channel_id).'?'.http_build_query($params));
  914. $this->statsDetail($uid,'treward',0);
  915. $get_free_currency = UserBindHkWelfareService::isHasGet($uid);
  916. if($get_free_currency){
  917. //已经领过
  918. $link = $this->getLink($distribution_channel_id).'recent?'.http_build_query($params);
  919. return view('jump.bindHkFreeCurrency',['fee'=>$fee,'url'=>$link,'is_get'=>1,'uid'=>$uid,'source'=>$source]);
  920. }else{
  921. $link = $this->getLink($distribution_channel_id).'recent?'.http_build_query($params);
  922. $this->getReward($uid,$fee,$source,$type);
  923. return view('jump.bindHkFreeCurrency',['fee'=>$fee,'url'=>$link,'is_get'=>0,'uid'=>$uid,'source'=>$source]);
  924. }
  925. }
  926. private function createUser($openid,$unionid,$distribution_channel_id,$send_order_id,$ip){
  927. $user = UserService::getUserByOpenidAndChannelId($openid,$distribution_channel_id);
  928. if($user){
  929. return $user;
  930. }
  931. $user = null;
  932. try{
  933. $user = UserService::addUser(
  934. ['openid' => $openid,
  935. 'unionid' => $unionid,
  936. 'distribution_channel_id' =>$distribution_channel_id,
  937. 'send_order_id'=>$send_order_id,
  938. 'is_new'=>1,
  939. 'register_ip'=>$ip
  940. ]);
  941. }catch (\Exception $e){
  942. myLog('user-error')->info($e);
  943. }
  944. // 注册动作
  945. $action_type = 'Register';
  946. $param = [
  947. 'openid' => $openid,
  948. 'uid' => isset($user->id)?$user->id:'0',
  949. ];
  950. UserService::PushUserActionToQueue($action_type,$distribution_channel_id,$param);
  951. return $user;
  952. }
  953. }