CoflController.php 41 KB

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