CoflController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. $type = 'CRM';
  86. if($uri == 'freethrhcurrency'){
  87. $fee = 300;
  88. $this->stats('reward_300',$crm);
  89. }elseif($uri == 'wfreecurrency'){
  90. $fee = 200;
  91. $type = 'WCRM';
  92. }
  93. else{
  94. $this->stats('reward_200',$crm);
  95. $fee = $this->freeCurrencyFee($token,$rfee);
  96. }
  97. $user = $this->getUsers($openid);
  98. if(!$user || !$user[0] || !$user[1]) return redirect()->to($this->getLink().'?'.http_build_query($params));
  99. $this->statsDetail($user[0],'reward',0);
  100. $get_free_currency = UserBindHkWelfareService::isHasGet($user[0]);
  101. if($get_free_currency){
  102. //已经领过
  103. if(isset($user[2]) && !empty($user[2]) && isset($user[3]) && !empty($user[3])){
  104. $bid = Hashids::encode($user[2]);
  105. $cid = $user[3];
  106. $params['bid'] = $bid;
  107. $params['cid'] = $cid;
  108. $link = $this->getLink($get_free_currency->distribution_channel_id).'reader?'.http_build_query($params);
  109. }else{
  110. $link = $this->getLink($get_free_currency->distribution_channel_id).'?'.http_build_query($params);
  111. }
  112. return view('jump.bindHkFreeCurrency',['fee'=>$fee,'url'=>$link,'is_get'=>1,'uid'=>$get_free_currency->uid,'source'=>$source]);
  113. }else{
  114. if(isset($user[2]) && !empty($user[2]) && isset($user[3]) && !empty($user[3])){
  115. $bid = Hashids::encode($user[2]);
  116. $cid = $user[3];
  117. $params['bid'] = $bid;
  118. $params['cid'] = $cid;
  119. $link = $this->getLink($user[1]).'reader?'.http_build_query($params);
  120. }else{
  121. $link = $this->getLink($user[1]).'?'.http_build_query($params);
  122. }
  123. $this->getReward($user[0],$fee,$source,$type);
  124. return view('jump.bindHkFreeCurrency',['fee'=>$fee,'url'=>$link,'is_get'=>0,'uid'=>$user[0],'source'=>$source]);
  125. }
  126. }
  127. private function freeCurrencyFee($token,$fee){
  128. if(!$token || !$fee) return 200;
  129. $param['fee'] = $fee;
  130. if( _sign($param,env('SECRET_KEY')) == $token) return $fee;
  131. return 200;
  132. }
  133. public function freeCurrencyPost(Request $request){
  134. $uid = $request->post('uid');
  135. $fee = $request->post('fee',200);
  136. $source = $request->post('source','');
  137. $result = UserBindHkWelfareService::getfreeCurrency($uid,$fee,'CRM',$source);
  138. if($result){
  139. UserService::addBalance($uid,$fee,0,$fee);
  140. }
  141. return response()->success(['uid'=>$uid,'result'=>$result]);
  142. }
  143. private function getReward($uid,$fee,$source,$crm='CRM'){
  144. $result = UserBindHkWelfareService::getfreeCurrency($uid,$fee,$crm,$source);
  145. if($result){
  146. UserService::addBalance($uid,$fee,0,$fee);
  147. }
  148. }
  149. public function activity(Request $request){
  150. $token = $request->get('token');
  151. if(empty($token)){
  152. $default_ink = $this->getLink();
  153. return redirect()->to($default_ink);
  154. }
  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. $crm = $request->get('crm');
  165. $this->stats('activity',$crm);
  166. $activity = ActivityService::getByToken($token);
  167. if($activity){
  168. $user = $this->getUsers($openid);
  169. $distribution_channel_id = (isset($user[1]) && !empty($user[1]))?$user[1]:123;
  170. $url_format = '%s://site%s.%s.com%s';
  171. $activity_page = $activity->activity_page ;
  172. if($activity->id == 6000){
  173. $activity_page = '/activity/common?token=LNyAqbFMgvkmvnHP8PXV3DYPIIhQm3oe';
  174. }
  175. $url = sprintf($url_format, env('PROTOCOL'), encodeDistributionChannelId($distribution_channel_id),
  176. env('CUSTOM_HOST'),
  177. $activity_page
  178. );
  179. return redirect()->to($url);
  180. }
  181. $default_ink = $this->getLink();
  182. return redirect()->to($default_ink);
  183. }
  184. public function recent(Request $request){
  185. $openid = $request->get('openid');
  186. //授权
  187. $params = $request->except('_url');
  188. if(empty($openid)){
  189. $url = url()->current() . '?' . http_build_query($params);
  190. $params['redirect_url'] = urlencode($url);
  191. $app = new Application($this->auth($params));
  192. return $app->oauth->redirect();
  193. }
  194. $user = $this->getUsers($openid);
  195. if(!$user[0]){
  196. $distribution_channel_id = 123;
  197. }else{
  198. $distribution_channel_id = $user[1];
  199. }
  200. $crm = $request->get('crm');
  201. $this->stats('recent',$crm);
  202. $this->statsDetail($user[0],'recent',0);
  203. $link = $this->getLink($distribution_channel_id).'recent?'.http_build_query($params);
  204. return redirect()->to($link);
  205. }
  206. public function person(Request $request){
  207. $openid = $request->get('openid');
  208. //授权
  209. $params = $request->except('_url');
  210. if(empty($openid)){
  211. $url = url()->current() . '?' . http_build_query($params);
  212. $params['redirect_url'] = urlencode($url);
  213. $app = new Application($this->auth($params));
  214. return $app->oauth->redirect();
  215. }
  216. $user = $this->getUsers($openid);
  217. if(!$user[0]){
  218. $distribution_channel_id = 123;
  219. }else{
  220. $distribution_channel_id = $user[1];
  221. }
  222. $crm = $request->get('crm');
  223. $this->stats('person',$crm);
  224. $this->statsDetail($user[0],'person',0);
  225. $link = $this->getLink($distribution_channel_id).'person?'.http_build_query($params);
  226. return redirect()->to($link);
  227. }
  228. public function sign(Request $request){
  229. $openid = $request->get('openid');
  230. //授权
  231. $params = $request->except('_url');
  232. if(empty($openid)){
  233. $url = url()->current() . '?' . http_build_query($params);
  234. $params['redirect_url'] = urlencode($url);
  235. $app = new Application($this->auth($params));
  236. return $app->oauth->redirect();
  237. }
  238. $user = $this->getUsers($openid);
  239. if(!$user[0]){
  240. $distribution_channel_id = 123;
  241. }else{
  242. $distribution_channel_id = $user[1];
  243. }
  244. $crm = $request->get('crm');
  245. $this->stats('sign',$crm);
  246. $this->statsDetail($user[0],'sign',0);
  247. $link = $this->getLink($distribution_channel_id).'sign?'.http_build_query($params);
  248. return redirect()->to($link);
  249. }
  250. private function getUsers($openid){
  251. $users = User::where('openid',$openid)->select('id','distribution_channel_id')->get();
  252. if($users->isEmpty()) return [0,0,0,0];
  253. $temp = null;
  254. $distribution_channel_id = 0;
  255. $uid = 0;
  256. $bid = 0;
  257. $cid = 0;
  258. foreach ($users as $user){
  259. $last_read = ReadRecordService::getByField($user->id,'last_read');
  260. if(!$last_read) continue;
  261. if(!$temp ){
  262. $distribution_channel_id = $user->distribution_channel_id;
  263. $uid = $user->id;
  264. $temp = $last_read;
  265. }else{
  266. $temp_last_read_info = explode('_',$temp);
  267. $last_read_info = explode('_',$last_read);
  268. if($last_read_info[2] > $temp_last_read_info[2]){
  269. $uid = $user->id;
  270. $distribution_channel_id = $user->distribution_channel_id;
  271. $temp = $last_read;
  272. $bid = $last_read_info[0];
  273. $cid = $last_read_info[1];
  274. }
  275. }
  276. }
  277. return [$uid,$distribution_channel_id,$bid,$cid];
  278. }
  279. private function getLink($distribution_channel_id=123){
  280. $url_format = '%s://site%s.%s.com/';
  281. return sprintf(
  282. $url_format,
  283. env('PROTOCOL'),
  284. encodeDistributionChannelId($distribution_channel_id),
  285. env('CUSTOM_HOST')
  286. );
  287. }
  288. private function auth($param){
  289. $param['appid'] = 'wx9d389a73b88bbeae';
  290. $options = [
  291. 'app_id' => 'wx9d389a73b88bbeae',
  292. 'secret' => '2f6594bb595dfa256b5512e43a32a3d3',
  293. 'oauth' => [
  294. 'scopes' => ['snsapi_base'],
  295. 'callback' => env('AUTH_CALLBACK_URL') . '?' . http_build_query($param),
  296. ],
  297. ];
  298. return $options;
  299. }
  300. private function stats($page,$flag){
  301. if(!$page || !$flag) return ;
  302. //pv
  303. $day = date('Y-m-d');
  304. $pv_key = sprintf('crm:pv:%s',$flag);
  305. Redis::hincrby($pv_key, $day, 1);
  306. //Redis::hincrby($pv_key, 'total', 1);
  307. Redis::sadd('crm_'.$day,$flag);
  308. //uv
  309. $cookie_flag = Cookie::get('crm_flag');
  310. if(!$cookie_flag || $cookie_flag != $flag){
  311. $uv_key = sprintf('crm:uv:%s',$flag);
  312. Redis::hincrby($uv_key, $day, 1);
  313. //Redis::hincrby($uv_key, 'total', 1);
  314. }
  315. Cookie::queue('crm_flag', $flag, env('U_COOKIE_EXPIRE'), null, null, false, false);
  316. }
  317. private function statsDetail($uid,$page,$bid=0)
  318. {
  319. try{
  320. DB::table('crm_visit_detail')->insert([
  321. 'uid'=>$uid,'page'=>$page,
  322. 'bid'=>$bid,'day'=>date('Y-m-d'),
  323. 'created_at'=>date('Y-m-d H:i:s'),
  324. 'updated_at'=>date('Y-m-d H:i:s')
  325. ]);
  326. }catch (\Exception $e){}
  327. }
  328. public function longActivity(Request $request)
  329. {
  330. $openid = $request->get('openid');
  331. //授权
  332. $params = $request->except('_url');
  333. if(empty($openid)){
  334. $url = url()->current() . '?' . http_build_query($params);
  335. $params['redirect_url'] = urlencode($url);
  336. $app = new Application($this->auth($params));
  337. return $app->oauth->redirect();
  338. }
  339. $user = $this->getUsers($openid);
  340. if(!$user[0]){
  341. $distribution_channel_id = 123;
  342. }else{
  343. $distribution_channel_id = $user[1];
  344. }
  345. $crm = $request->get('crm');
  346. $this->stats('activity',$crm);
  347. $baselink = $this->getLink($distribution_channel_id).'activity/crm?'.http_build_query($params);
  348. if(isset($params['token']) && !empty($params['token'])){
  349. return redirect()->to($baselink.'activity/crm?'.http_build_query($params));
  350. }
  351. return redirect()->to($baselink);
  352. }
  353. public function guidePersonalAccount(Request $request,$channel_id){
  354. $uid = $request->get('uid',0);
  355. $env_config = redisEnvMulti('GUIDE_PERSONAL_ACCOUNT_OURS_QRCODE','GUIDE_PERSONAL_ACCOUNT_MAX_USER','GUIDE_PERSONAL_ACCOUNT_ID','GUIDE_PERSONAL_ACCOUNT_ONE_LOOP_MAX_USER');
  356. $img = empty($env_config[0])?'https://cdn-novel.iycdm.com/h5/personal_account/chenchen.jpg':$env_config[0];
  357. $max = empty($env_config[1])?100:$env_config[1];
  358. $one_loop_max = empty($env_config[3])?10:$env_config[3];
  359. $now_id = (int)$env_config[2];
  360. $uv = Redis::scard('guide_personal_uv');
  361. $personal_info = DB::table('personal_account_list')->where('id',$now_id)->select('count')->first();
  362. $total_max = $personal_info->count+$uv;
  363. //Log::info('$total_max is: '.);
  364. if($uv >= $one_loop_max || $total_max>=$max){
  365. if($total_max>=$max){
  366. DB::table('personal_account_list')->where('id',$now_id)->update([
  367. 'status'=>2,
  368. 'count'=>$total_max,
  369. 'updated_at'=>date('Y-m-d H:i:s')
  370. ]);
  371. }else{
  372. DB::table('personal_account_list')->where('id',$now_id)->increment('count',$uv,[
  373. 'updated_at'=>date('Y-m-d H:i:s')
  374. ]);
  375. }
  376. $account = DB::table('personal_account_list')
  377. ->where('is_enable',1)
  378. ->whereIn('status',[1,0])
  379. ->select('id','url')
  380. ->where('count','<',$max)
  381. ->orderBy('count','asc')
  382. ->orderBy('id')
  383. ->first();
  384. Redis::del('guide_personal_uv');
  385. if($account){
  386. DB::table('personal_account_list')->where('id',$account->id)->update([
  387. 'status'=>1,
  388. 'updated_at'=>date('Y-m-d H:i:s')
  389. ]);
  390. $img = $account->url;
  391. $now_id = $account->id;
  392. Redis::Hmset('env','GUIDE_PERSONAL_ACCOUNT_OURS_QRCODE',$account->url,'GUIDE_PERSONAL_ACCOUNT_ID',$account->id);
  393. }else{
  394. Redis::Hmset('env','GUIDE_PERSONAL_ACCOUNT_OURS_QRCODE','','GUIDE_PERSONAL_ACCOUNT_ID','');
  395. return back();
  396. }
  397. }
  398. Redis::sadd('guide_personal_uv',$uid);
  399. if($uid){
  400. DB::table('ad_pdd')->insert([
  401. 'uid'=>$uid,
  402. 'img'=>'GUIDE_PERSONAL_ACCOUNT_'.$now_id,
  403. 'date'=>date('Y-m-d'),
  404. 'created_at'=>date('Y-m-d H:i:s'),
  405. 'updated_at'=>date('Y-m-d H:i:s')
  406. ]);
  407. }
  408. /*if(in_array($channel_id,explode(',',redisEnv('GUIDE_PERSONAL_ACCOUNT_WEID_SITES')))){
  409. $imgs = redisEnv('GUIDE_PERSONAL_ACCOUNT_WEID_QRCODE',[]);
  410. if($imgs) $imgs = json_decode($imgs,1);
  411. $page = 'jump.guidePersonalAccount';
  412. }else{
  413. $imgs = redisEnv('GUIDE_PERSONAL_ACCOUNT_OURS_QRCODE',[]);
  414. if($imgs) $imgs = json_decode($imgs,1);
  415. $page = 'jump.guidePersonalAccountOurs';
  416. }*/
  417. $page = 'jump.guidePersonalAccountOurs';
  418. //$page = 'jump.guidePersonalAccount';
  419. //$img = collect($imgs)->random();
  420. return view($page,['img'=>$img]);
  421. }
  422. }