CoflController.php 15 KB

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