CoflController.php 14 KB

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