CoflController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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\User\Models\User;
  6. use App\Modules\User\Services\ReadRecordService;
  7. use App\Modules\User\Services\UserBindHkWelfareService;
  8. use App\Modules\User\Services\UserService;
  9. use Illuminate\Http\Request;
  10. use App\Http\Controllers\Controller;
  11. use Hashids;
  12. use EasyWeChat\Foundation\Application;
  13. use Log;
  14. use Exception;
  15. use Redis;
  16. use Cookie;
  17. use DB;
  18. /**
  19. * 朋友圈链接
  20. * Class CoflController
  21. * @package App\Http\Controllers\Wap\User
  22. */
  23. class CoflController extends Controller
  24. {
  25. public function index(Request $request){
  26. $bid = $request->get('bid');
  27. if(empty($bid)){
  28. $default_ink = $this->getLink();
  29. return redirect()->to($default_ink);
  30. }
  31. $openid = $request->get('openid');
  32. //授权
  33. $params = $request->except('_url');
  34. if(empty($openid)){
  35. //$url = str_replace('http://', env('PROTOCOL') . '://', url()->current() . '?' . http_build_query($params));
  36. $url = url()->current() . '?' . http_build_query($params);
  37. $params['redirect_url'] = urlencode($url);
  38. $app = new Application($this->auth($params));
  39. return $app->oauth->redirect();
  40. }
  41. try{
  42. $bid = Hashids::decode($bid)[0];
  43. }catch (Exception $e){
  44. return redirect()->to($this->getLink());
  45. }
  46. $crm = $request->get('crm');
  47. //获取用户
  48. $user = $this->getUsers($openid);
  49. if(!$user[0]){
  50. $user[1] = 123;
  51. }
  52. //有阅读纪录的跳转
  53. $read_record = ReadRecordService::getByField($user[0],$bid);
  54. if($read_record){
  55. $cid = explode('_',$read_record)[0];
  56. }else{
  57. //没有阅读记录的跳转
  58. $book_info = BookConfigService::getBookById($bid);
  59. $cid = $book_info->first_cid;
  60. }
  61. $params['cid'] = $cid;
  62. if(isset($params['openid'])) unset($params['openid']);
  63. if(isset($params['unionid'])) unset($params['unionid']);
  64. $url = $this->getLink($user[1]).'reader?'.http_build_query($params);
  65. $this->stats('book',$crm);
  66. $this->statsDetail($user[0],'book',$bid);
  67. return redirect()->to($url);
  68. }
  69. public function freeCurrencyView(Request $request){
  70. $openid = $request->get('openid');
  71. //授权
  72. $params = $request->except('_url');
  73. if(empty($openid)){
  74. //$url = str_replace('http://', env('PROTOCOL') . '://', url()->current() . '?' . http_build_query($params));
  75. $url = url()->current() . '?' . http_build_query($params);
  76. $params['redirect_url'] = urlencode($url);
  77. $app = new Application($this->auth($params));
  78. return $app->oauth->redirect();
  79. }
  80. $uri = $request->path();
  81. $token = $request->get('token');
  82. $rfee = $request->get('amount');
  83. $crm = $request->get('crm','reward');
  84. $source = $request->get('wx','none');
  85. if($uri == 'freethrhcurrency'){
  86. $fee = 300;
  87. $this->stats('reward_300',$crm);
  88. }else{
  89. $this->stats('reward_200',$crm);
  90. $fee = $this->freeCurrencyFee($token,$rfee);
  91. }
  92. $user = $this->getUsers($openid);
  93. if(!$user || !$user[0] || !$user[1]) return redirect()->to($this->getLink().'?'.http_build_query($params));
  94. $this->statsDetail($user[0],'reward',0);
  95. $get_free_currency = UserBindHkWelfareService::isHasGet($user[0]);
  96. if($get_free_currency){
  97. //已经领过
  98. if(isset($user[2]) && !empty($user[2]) && isset($user[3]) && !empty($user[3])){
  99. $bid = Hashids::encode($user[2]);
  100. $cid = $user[3];
  101. $params['bid'] = $bid;
  102. $params['cid'] = $cid;
  103. $link = $this->getLink($get_free_currency->distribution_channel_id).'reader?'.http_build_query($params);
  104. }else{
  105. $link = $this->getLink($get_free_currency->distribution_channel_id).'?'.http_build_query($params);
  106. }
  107. return view('jump.bindHkFreeCurrency',['fee'=>$fee,'url'=>$link,'is_get'=>1,'uid'=>$get_free_currency->uid,'source'=>$source]);
  108. }else{
  109. if(isset($user[2]) && !empty($user[2]) && isset($user[3]) && !empty($user[3])){
  110. $bid = Hashids::encode($user[2]);
  111. $cid = $user[3];
  112. $params['bid'] = $bid;
  113. $params['cid'] = $cid;
  114. $link = $this->getLink($user[1]).'reader?'.http_build_query($params);
  115. }else{
  116. $link = $this->getLink($user[1]).'?'.http_build_query($params);
  117. }
  118. $this->getReward($user[0],$fee,$source);
  119. return view('jump.bindHkFreeCurrency',['fee'=>$fee,'url'=>$link,'is_get'=>0,'uid'=>$user[0],'source'=>$source]);
  120. }
  121. }
  122. private function freeCurrencyFee($token,$fee){
  123. if(!$token || !$fee) return 200;
  124. $param['fee'] = $fee;
  125. if( _sign($param,env('SECRET_KEY')) == $token) return $fee;
  126. return 200;
  127. }
  128. public function freeCurrencyPost(Request $request){
  129. $uid = $request->post('uid');
  130. $fee = $request->post('fee',200);
  131. $source = $request->post('source','');
  132. $result = UserBindHkWelfareService::getfreeCurrency($uid,$fee,'CRM',$source);
  133. if($result){
  134. UserService::addBalance($uid,$fee,0,$fee);
  135. }
  136. return response()->success(['uid'=>$uid,'result'=>$result]);
  137. }
  138. private function getReward($uid,$fee,$source){
  139. $result = UserBindHkWelfareService::getfreeCurrency($uid,$fee,'CRM',$source);
  140. if($result){
  141. UserService::addBalance($uid,$fee,0,$fee);
  142. }
  143. }
  144. public function activity(Request $request){
  145. $token = $request->get('token');
  146. if(empty($token)){
  147. $default_ink = $this->getLink();
  148. return redirect()->to($default_ink);
  149. }
  150. $openid = $request->get('openid');
  151. //授权
  152. $params = $request->except('_url');
  153. if(empty($openid)){
  154. $url = url()->current() . '?' . http_build_query($params);
  155. $params['redirect_url'] = urlencode($url);
  156. $app = new Application($this->auth($params));
  157. return $app->oauth->redirect();
  158. }
  159. $crm = $request->get('crm');
  160. $this->stats('activity',$crm);
  161. $activity = ActivityService::getByToken($token);
  162. if($activity){
  163. $user = $this->getUsers($openid);
  164. $distribution_channel_id = (isset($user[1]) && !empty($user[1]))?$user[1]:123;
  165. $url_format = '%s://site%s.%s.com%s';
  166. $activity_page = $activity->activity_page ;
  167. if($activity->id == 6000){
  168. $activity_page = '/activity/common?token=LNyAqbFMgvkmvnHP8PXV3DYPIIhQm3oe';
  169. }
  170. $url = sprintf($url_format, env('PROTOCOL'), encodeDistributionChannelId($distribution_channel_id),
  171. env('CUSTOM_HOST'),
  172. $activity_page
  173. );
  174. return redirect()->to($url);
  175. }
  176. $default_ink = $this->getLink();
  177. return redirect()->to($default_ink);
  178. }
  179. public function recent(Request $request){
  180. $openid = $request->get('openid');
  181. //授权
  182. $params = $request->except('_url');
  183. if(empty($openid)){
  184. $url = url()->current() . '?' . http_build_query($params);
  185. $params['redirect_url'] = urlencode($url);
  186. $app = new Application($this->auth($params));
  187. return $app->oauth->redirect();
  188. }
  189. $user = $this->getUsers($openid);
  190. if(!$user[0]){
  191. $distribution_channel_id = 123;
  192. }else{
  193. $distribution_channel_id = $user[1];
  194. }
  195. $crm = $request->get('crm');
  196. $this->stats('recent',$crm);
  197. $this->statsDetail($user[0],'recent',0);
  198. $link = $this->getLink($distribution_channel_id).'recent?'.http_build_query($params);
  199. return redirect()->to($link);
  200. }
  201. public function person(Request $request){
  202. $openid = $request->get('openid');
  203. //授权
  204. $params = $request->except('_url');
  205. if(empty($openid)){
  206. $url = url()->current() . '?' . http_build_query($params);
  207. $params['redirect_url'] = urlencode($url);
  208. $app = new Application($this->auth($params));
  209. return $app->oauth->redirect();
  210. }
  211. $user = $this->getUsers($openid);
  212. if(!$user[0]){
  213. $distribution_channel_id = 123;
  214. }else{
  215. $distribution_channel_id = $user[1];
  216. }
  217. $crm = $request->get('crm');
  218. $this->stats('person',$crm);
  219. $this->statsDetail($user[0],'person',0);
  220. $link = $this->getLink($distribution_channel_id).'person?'.http_build_query($params);
  221. return redirect()->to($link);
  222. }
  223. public function sign(Request $request){
  224. $openid = $request->get('openid');
  225. //授权
  226. $params = $request->except('_url');
  227. if(empty($openid)){
  228. $url = url()->current() . '?' . http_build_query($params);
  229. $params['redirect_url'] = urlencode($url);
  230. $app = new Application($this->auth($params));
  231. return $app->oauth->redirect();
  232. }
  233. $user = $this->getUsers($openid);
  234. if(!$user[0]){
  235. $distribution_channel_id = 123;
  236. }else{
  237. $distribution_channel_id = $user[1];
  238. }
  239. $crm = $request->get('crm');
  240. $this->stats('sign',$crm);
  241. $this->statsDetail($user[0],'sign',0);
  242. $link = $this->getLink($distribution_channel_id).'sign?'.http_build_query($params);
  243. return redirect()->to($link);
  244. }
  245. private function getUsers($openid){
  246. $users = User::where('openid',$openid)->select('id','distribution_channel_id')->get();
  247. if($users->isEmpty()) return [0,0,0,0];
  248. $temp = null;
  249. $distribution_channel_id = 0;
  250. $uid = 0;
  251. $bid = 0;
  252. $cid = 0;
  253. foreach ($users as $user){
  254. $last_read = ReadRecordService::getByField($user->id,'last_read');
  255. if(!$last_read) continue;
  256. if(!$temp ){
  257. $distribution_channel_id = $user->distribution_channel_id;
  258. $uid = $user->id;
  259. $temp = $last_read;
  260. }else{
  261. $temp_last_read_info = explode('_',$temp);
  262. $last_read_info = explode('_',$last_read);
  263. if($last_read_info[2] > $temp_last_read_info[2]){
  264. $uid = $user->id;
  265. $distribution_channel_id = $user->distribution_channel_id;
  266. $temp = $last_read;
  267. $bid = $last_read_info[0];
  268. $cid = $last_read_info[1];
  269. }
  270. }
  271. }
  272. return [$uid,$distribution_channel_id,$bid,$cid];
  273. }
  274. private function getLink($distribution_channel_id=123){
  275. $url_format = '%s://site%s.%s.com/';
  276. return sprintf(
  277. $url_format,
  278. env('PROTOCOL'),
  279. encodeDistributionChannelId($distribution_channel_id),
  280. env('CUSTOM_HOST')
  281. );
  282. }
  283. private function auth($param){
  284. $param['appid'] = 'wx9d389a73b88bbeae';
  285. $options = [
  286. 'app_id' => 'wx9d389a73b88bbeae',
  287. 'secret' => '2f6594bb595dfa256b5512e43a32a3d3',
  288. 'oauth' => [
  289. 'scopes' => ['snsapi_base'],
  290. 'callback' => env('AUTH_CALLBACK_URL') . '?' . http_build_query($param),
  291. ],
  292. ];
  293. return $options;
  294. }
  295. private function stats($page,$flag){
  296. if(!$page || !$flag) return ;
  297. //pv
  298. $day = date('Y-m-d');
  299. $pv_key = sprintf('crm:pv:%s',$flag);
  300. Redis::hincrby($pv_key, $day, 1);
  301. //Redis::hincrby($pv_key, 'total', 1);
  302. Redis::sadd('crm_'.$day,$flag);
  303. //uv
  304. $cookie_flag = Cookie::get('crm_flag');
  305. if(!$cookie_flag || $cookie_flag != $flag){
  306. $uv_key = sprintf('crm:uv:%s',$flag);
  307. Redis::hincrby($uv_key, $day, 1);
  308. //Redis::hincrby($uv_key, 'total', 1);
  309. }
  310. Cookie::queue('crm_flag', $flag, env('U_COOKIE_EXPIRE'), null, null, false, false);
  311. }
  312. private function statsDetail($uid,$page,$bid=0)
  313. {
  314. try{
  315. DB::table('crm_visit_detail')->insert([
  316. 'uid'=>$uid,'page'=>$page,
  317. 'bid'=>$bid,'day'=>date('Y-m-d'),
  318. 'created_at'=>date('Y-m-d H:i:s'),
  319. 'updated_at'=>date('Y-m-d H:i:s')
  320. ]);
  321. }catch (\Exception $e){}
  322. }
  323. public function longActivity(Request $request)
  324. {
  325. $openid = $request->get('openid');
  326. //授权
  327. $params = $request->except('_url');
  328. if(empty($openid)){
  329. $url = url()->current() . '?' . http_build_query($params);
  330. $params['redirect_url'] = urlencode($url);
  331. $app = new Application($this->auth($params));
  332. return $app->oauth->redirect();
  333. }
  334. $user = $this->getUsers($openid);
  335. if(!$user[0]){
  336. $distribution_channel_id = 123;
  337. }else{
  338. $distribution_channel_id = $user[1];
  339. }
  340. $crm = $request->get('crm');
  341. $this->stats('activity',$crm);
  342. $baselink = $this->getLink($distribution_channel_id).'activity/crm?'.http_build_query($params);
  343. if(isset($params['token']) && !empty($params['token'])){
  344. return redirect()->to($baselink.'activity/crm?'.http_build_query($params));
  345. }
  346. return redirect()->to($baselink);
  347. }
  348. public function guidePersonalAccount(Request $request,$channel_id){
  349. $uid = $request->get('uid',0);
  350. $env_config = redisEnvMulti('GUIDE_PERSONAL_ACCOUNT_OURS_QRCODE','GUIDE_PERSONAL_ACCOUNT_MAX_USER','GUIDE_PERSONAL_ACCOUNT_ID','GUIDE_PERSONAL_ACCOUNT_ONE_LOOP_MAX_USER');
  351. $img = empty($env_config[0])?'https://cdn-novel.iycdm.com/h5/personal_account/chenchen.jpg':$env_config[0];
  352. $max = empty($env_config[1])?100:$env_config[1];
  353. $one_loop_max = empty($env_config[3])?10:$env_config[3];
  354. $now_id = (int)$env_config[2];
  355. $uv = Redis::scard('guide_personal_uv');
  356. $personal_info = DB::table('personal_account_list')->where('id',$now_id)->select('count')->first();
  357. $total_max = $personal_info->count+$uv;
  358. //Log::info('$total_max is: '.);
  359. if($uv >= $one_loop_max || $total_max>=$max){
  360. if($total_max>=$max){
  361. DB::table('personal_account_list')->where('id',$now_id)->update([
  362. 'status'=>2,
  363. 'count'=>$total_max,
  364. 'updated_at'=>date('Y-m-d H:i:s')
  365. ]);
  366. }else{
  367. DB::table('personal_account_list')->where('id',$now_id)->increment('count',$uv,[
  368. 'updated_at'=>date('Y-m-d H:i:s')
  369. ]);
  370. }
  371. $account = DB::table('personal_account_list')
  372. ->where('is_enable',1)
  373. ->whereIn('status',[1,0])
  374. ->select('id','url')
  375. ->where('count','<',$max)
  376. ->orderBy('count','asc')
  377. ->orderBy('id')
  378. ->first();
  379. Redis::del('guide_personal_uv');
  380. if($account){
  381. DB::table('personal_account_list')->where('id',$account->id)->update([
  382. 'status'=>1,
  383. 'updated_at'=>date('Y-m-d H:i:s')
  384. ]);
  385. $img = $account->url;
  386. $now_id = $account->id;
  387. Redis::Hmset('env','GUIDE_PERSONAL_ACCOUNT_OURS_QRCODE',$account->url,'GUIDE_PERSONAL_ACCOUNT_ID',$account->id);
  388. }else{
  389. Redis::Hmset('env','GUIDE_PERSONAL_ACCOUNT_OURS_QRCODE','','GUIDE_PERSONAL_ACCOUNT_ID','');
  390. return back();
  391. }
  392. }
  393. Redis::sadd('guide_personal_uv',$uid);
  394. if($uid){
  395. DB::table('ad_pdd')->insert([
  396. 'uid'=>$uid,
  397. 'img'=>'GUIDE_PERSONAL_ACCOUNT_'.$now_id,
  398. 'date'=>date('Y-m-d'),
  399. 'created_at'=>date('Y-m-d H:i:s'),
  400. 'updated_at'=>date('Y-m-d H:i:s')
  401. ]);
  402. }
  403. /*if(in_array($channel_id,explode(',',redisEnv('GUIDE_PERSONAL_ACCOUNT_WEID_SITES')))){
  404. $imgs = redisEnv('GUIDE_PERSONAL_ACCOUNT_WEID_QRCODE',[]);
  405. if($imgs) $imgs = json_decode($imgs,1);
  406. $page = 'jump.guidePersonalAccount';
  407. }else{
  408. $imgs = redisEnv('GUIDE_PERSONAL_ACCOUNT_OURS_QRCODE',[]);
  409. if($imgs) $imgs = json_decode($imgs,1);
  410. $page = 'jump.guidePersonalAccountOurs';
  411. }*/
  412. $page = 'jump.guidePersonalAccountOurs';
  413. //$page = 'jump.guidePersonalAccount';
  414. //$img = collect($imgs)->random();
  415. return view($page,['img'=>$img]);
  416. }
  417. }