CoflController.php 15 KB

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