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