CoflController.php 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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. $fee = $request->get('fee',200);
  733. $get_info = UserBindHkWelfareService::isHasGet($uid);
  734. if($get_info) return back();
  735. $old_id = ReadRecordService::getByField($uid,'person_account_id');
  736. if($old_id){
  737. $old_personal_info = DB::table('personal_account_list')->where('id',$old_id)->first();
  738. if($old_personal_info && $old_personal_info->is_enable == 1){
  739. return view('jump.guidePersonalAccountOurs',['img'=>$old_personal_info->url,'name'=>$old_personal_info->name,'fee'=>$fee]);
  740. }
  741. }
  742. $distribution_channel_id = decodeDistributionChannelId($channel_id);
  743. //$distribution_channel_id = $channel_id;
  744. //if(in_array($distribution_channel_id,Redis::SISMEMBER('')));
  745. //$out = false;
  746. $group = 'ACTIVE';
  747. if(Redis::SISMEMBER('crm:out_channel_sites',$distribution_channel_id)){
  748. //$out = true;
  749. $group = 'OUT_ACTIVE';
  750. $env_config = redisEnvMulti('OUT_ACTIVE_GUIDE_PERSONAL_ACCOUNT_OURS_QRCODE',
  751. 'OUT_ACTIVE_GUIDE_PERSONAL_ACCOUNT_MAX_UV','OUT_ACTIVE_GUIDE_PERSONAL_ACCOUNT_ID',
  752. 'OUT_ACTIVE_GUIDE_PERSONAL_ACCOUNT_MAX_EVERY_UV');
  753. }else{
  754. $env_config = redisEnvMulti('ACTIVE_GUIDE_PERSONAL_ACCOUNT_OURS_QRCODE',
  755. 'ACTIVE_GUIDE_PERSONAL_ACCOUNT_MAX_UV','ACTIVE_GUIDE_PERSONAL_ACCOUNT_ID',
  756. 'ACTIVE_GUIDE_PERSONAL_ACCOUNT_MAX_EVERY_UV');
  757. }
  758. if(empty($env_config[0])){
  759. return back();
  760. }
  761. //$img = empty($env_config[0])?'https://cdn-novel.iycdm.com/h5/personal_account/chenchen.jpg':$env_config[0];
  762. $img =$env_config[0];
  763. $max = empty($env_config[1])?100:$env_config[1];
  764. $max = (int)$max;
  765. $one_loop_max = empty($env_config[3])?10:$env_config[3];
  766. $now_id = (int)$env_config[2];
  767. if($group == 'OUT_ACTIVE'){
  768. $uv_key = 'out_active_guide_personal_uv';
  769. }else{
  770. $uv_key = 'active_guide_personal_uv';
  771. }
  772. $uv = Redis::scard($uv_key);
  773. $personal_info = DB::table('personal_account_list')->where('id',$now_id)->select('count')->first();
  774. if(!$personal_info){
  775. return back();
  776. }
  777. $total_max = (int)($personal_info->count+$uv);
  778. //Log::info('$total_max is: '.);
  779. if($uv >= $one_loop_max || $total_max>=$max){
  780. if($total_max>=$max){
  781. DB::table('personal_account_list')->where('id',$now_id)->update([
  782. 'status'=>2,
  783. 'count'=>$total_max,
  784. 'updated_at'=>date('Y-m-d H:i:s')
  785. ]);
  786. }else{
  787. DB::table('personal_account_list')->where('id',$now_id)->increment('count',$uv,[
  788. 'updated_at'=>date('Y-m-d H:i:s')
  789. ]);
  790. }
  791. $account = DB::table('personal_account_list')
  792. ->where('is_enable',1)
  793. ->whereIn('status',[1,0])
  794. ->select('id','url')
  795. ->where('group',$group)
  796. ->where('count','<',$max)
  797. ->orderBy('count','asc')
  798. ->orderBy('id')
  799. ->first();
  800. Redis::del($uv_key);
  801. if($account){
  802. DB::table('personal_account_list')->where('id',$account->id)->update([
  803. 'status'=>1,
  804. 'updated_at'=>date('Y-m-d H:i:s')
  805. ]);
  806. $img = $account->url;
  807. $now_id = $account->id;
  808. Redis::Hmset('env',$group.'_GUIDE_PERSONAL_ACCOUNT_OURS_QRCODE',$account->url,$group.'_GUIDE_PERSONAL_ACCOUNT_ID',$account->id);
  809. }else{
  810. Redis::Hmset('env',$group.'_GUIDE_PERSONAL_ACCOUNT_OURS_QRCODE','',$group.'_GUIDE_PERSONAL_ACCOUNT_ID','');
  811. return back();
  812. }
  813. }
  814. Redis::sadd($uv_key,$uid);
  815. if($uid){
  816. if($group == 'OUT_ACTIVE'){
  817. DB::table('ad_pdd')->insert([
  818. 'uid'=>$uid,
  819. 'img'=>'OUT_GUIDE_PERSONAL_ACCOUNT_'.$now_id,
  820. 'date'=>date('Y-m-d'),
  821. 'created_at'=>date('Y-m-d H:i:s'),
  822. 'updated_at'=>date('Y-m-d H:i:s')
  823. ]);
  824. $user = UserService::getById($uid);
  825. Redis::hset('crm:out_guide_exposure_user',$user->openid,$uid);
  826. }else{
  827. DB::table('ad_pdd')->insert([
  828. 'uid'=>$uid,
  829. 'img'=>'GUIDE_PERSONAL_ACCOUNT_'.$now_id,
  830. 'date'=>date('Y-m-d'),
  831. 'created_at'=>date('Y-m-d H:i:s'),
  832. 'updated_at'=>date('Y-m-d H:i:s')
  833. ]);
  834. }
  835. ReadRecordService::setByField($uid,'person_account_id',$now_id);
  836. }
  837. $name = '';
  838. $name_info = DB::table('personal_account_list')->where('id',$now_id)->select('name')->first();
  839. if($name_info){
  840. $name = $name_info->name;
  841. }
  842. /*if(in_array($channel_id,explode(',',redisEnv('GUIDE_PERSONAL_ACCOUNT_WEID_SITES')))){
  843. $imgs = redisEnv('GUIDE_PERSONAL_ACCOUNT_WEID_QRCODE',[]);
  844. if($imgs) $imgs = json_decode($imgs,1);
  845. $page = 'jump.guidePersonalAccount';
  846. }else{
  847. $imgs = redisEnv('GUIDE_PERSONAL_ACCOUNT_OURS_QRCODE',[]);
  848. if($imgs) $imgs = json_decode($imgs,1);
  849. $page = 'jump.guidePersonalAccountOurs';
  850. }*/
  851. $page = 'jump.guidePersonalAccountOurs';
  852. $time = strtotime(date('Y-m-d',time()+86400))-time();
  853. //Cookie::queue('crm_person_img', $img,$time);
  854. //Cookie::queue('crm_person_name', $name,$time);
  855. //$page = 'jump.guidePersonalAccount';
  856. //$img = collect($imgs)->random();
  857. return view($page,['img'=>$img,'name'=>$name,'fee'=>$fee]);
  858. }
  859. public function r(Request $request,$number){
  860. switch ($number){
  861. case 1:
  862. //首页
  863. break;
  864. case 2:
  865. //阅读器
  866. break;
  867. case 3:
  868. //阅读器
  869. break;
  870. case 4:
  871. //个人中心
  872. break;
  873. case 5:
  874. //最近阅读
  875. break;
  876. case 6:
  877. }
  878. }
  879. public function freeCurrencyViewForTransfer(Request $request){
  880. $openid = $request->get('openid');
  881. //授权
  882. $params = $request->except('_url');
  883. if(empty($openid)){
  884. //$url = str_replace('http://', env('PROTOCOL') . '://', url()->current() . '?' . http_build_query($params));
  885. $url = url()->current() . '?' . http_build_query($params);
  886. $params['redirect_url'] = urlencode($url);
  887. $app = new Application($this->auth($params));
  888. return $app->oauth->redirect();
  889. }
  890. $crm = $request->get('crm','treward');
  891. $source = $request->get('wx','none');
  892. $fee = 500;
  893. $type = 'TCRM';
  894. $this->stats('treward_500',$crm);
  895. $crm_transfer_uid_bind = DB::table('crm_transfer')->where('openid',$openid)->first();
  896. if($crm_transfer_uid_bind) {
  897. $user = User::where('id',$crm_transfer_uid_bind->to_uid)->select('id','distribution_channel_id')->first();
  898. $uid = $crm_transfer_uid_bind->to_uid;
  899. $distribution_channel_id = 5204;
  900. }else{
  901. list($ouid,$distribution_channel_id) = $this->getUsersV3($openid);
  902. $user = $this->createUser($openid,$openid,5204,0,'');
  903. $uid = $user->id;
  904. $distribution_channel_id = 5204;
  905. UserService::transfer($ouid,$uid,5204);
  906. $insert_data = ['uid'=>$uid,'openid'=>$openid ,'created_at'=>date('Y-m-d H:i:s'),'updated_at'=>date('Y-m-d H:i:s')];
  907. DB::table('friend_link_uid_bind')->insert($insert_data);
  908. }
  909. if(!$uid) return redirect()->to($this->getLink($distribution_channel_id).'?'.http_build_query($params));
  910. $this->statsDetail($uid,'treward',0);
  911. $get_free_currency = UserBindHkWelfareService::isHasGet($uid);
  912. if($get_free_currency){
  913. //已经领过
  914. $link = $this->getLink($distribution_channel_id).'recent?'.http_build_query($params);
  915. return view('jump.bindHkFreeCurrency',['fee'=>$fee,'url'=>$link,'is_get'=>1,'uid'=>$uid,'source'=>$source]);
  916. }else{
  917. $link = $this->getLink($distribution_channel_id).'recent?'.http_build_query($params);
  918. $this->getReward($uid,$fee,$source,$type);
  919. return view('jump.bindHkFreeCurrency',['fee'=>$fee,'url'=>$link,'is_get'=>0,'uid'=>$uid,'source'=>$source]);
  920. }
  921. }
  922. private function createUser($openid,$unionid,$distribution_channel_id,$send_order_id,$ip){
  923. $user = UserService::getUserByOpenidAndChannelId($openid,$distribution_channel_id);
  924. if($user){
  925. return $user;
  926. }
  927. $user = null;
  928. try{
  929. $user = UserService::addUser(
  930. ['openid' => $openid,
  931. 'unionid' => $unionid,
  932. 'distribution_channel_id' =>$distribution_channel_id,
  933. 'send_order_id'=>$send_order_id,
  934. 'is_new'=>1,
  935. 'register_ip'=>$ip
  936. ]);
  937. }catch (\Exception $e){
  938. myLog('user-error')->info($e);
  939. }
  940. // 注册动作
  941. $action_type = 'Register';
  942. $param = [
  943. 'openid' => $openid,
  944. 'uid' => isset($user->id)?$user->id:'0',
  945. ];
  946. UserService::PushUserActionToQueue($action_type,$distribution_channel_id,$param);
  947. return $user;
  948. }
  949. }