CoflController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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\Channel\Models\Channel;
  6. use App\Modules\Subscribe\Models\BookOrder;
  7. use App\Modules\Subscribe\Models\YearOrder;
  8. use App\Modules\Subscribe\Services\YearOrderService;
  9. use App\Modules\User\Models\User;
  10. use App\Modules\User\Services\ReadRecordService;
  11. use App\Modules\User\Services\UserBindHkWelfareService;
  12. use App\Modules\User\Services\UserService;
  13. use Illuminate\Http\Request;
  14. use App\Http\Controllers\Controller;
  15. use Hashids;
  16. use EasyWeChat\Foundation\Application;
  17. use Log;
  18. use Exception;
  19. use Redis;
  20. use Cookie;
  21. /**
  22. * 朋友圈链接
  23. * Class CoflController
  24. * @package App\Http\Controllers\Wap\User
  25. */
  26. class CoflController extends Controller
  27. {
  28. public function index(Request $request){
  29. $bid = $request->get('bid');
  30. if(empty($bid)){
  31. $default_ink = $this->getLink();
  32. return redirect()->to($default_ink);
  33. }
  34. $openid = $request->get('openid');
  35. //授权
  36. $params = $request->except('_url');
  37. if(empty($openid)){
  38. //$url = str_replace('http://', env('PROTOCOL') . '://', url()->current() . '?' . http_build_query($params));
  39. $url = url()->current() . '?' . http_build_query($params);
  40. $params['redirect_url'] = urlencode($url);
  41. $app = new Application($this->auth($params));
  42. return $app->oauth->redirect();
  43. }
  44. try{
  45. $bid = Hashids::decode($bid)[0];
  46. }catch (Exception $e){
  47. return redirect()->to($this->getLink());
  48. }
  49. $crm = $request->get('crm');
  50. //获取用户
  51. $user = $this->getUsers($openid);
  52. $test_param = $request->input('test',0);
  53. if($test_param ==1 || $test_param =='1' ) {
  54. $user = $this->getUsersV2($openid,$bid);
  55. }
  56. if(!$user[0]){
  57. $user[1] = 123;
  58. }
  59. //有阅读纪录的跳转
  60. $read_record = ReadRecordService::getByField($user[0],$bid);
  61. if($read_record){
  62. $cid = explode('_',$read_record)[0];
  63. }else{
  64. //没有阅读记录的跳转
  65. $book_info = BookConfigService::getBookById($bid);
  66. $cid = $book_info->first_cid;
  67. }
  68. $params['cid'] = $cid;
  69. if(isset($params['openid'])) unset($params['openid']);
  70. if(isset($params['unionid'])) unset($params['unionid']);
  71. $url = $this->getLink($user[1]).'reader?'.http_build_query($params);
  72. $this->stats('book',$crm);
  73. return redirect()->to($url);
  74. }
  75. public function freeCurrencyView(Request $request){
  76. $openid = $request->get('openid');
  77. //授权
  78. $params = $request->except('_url');
  79. if(empty($openid)){
  80. //$url = str_replace('http://', env('PROTOCOL') . '://', url()->current() . '?' . http_build_query($params));
  81. $url = url()->current() . '?' . http_build_query($params);
  82. $params['redirect_url'] = urlencode($url);
  83. $app = new Application($this->auth($params));
  84. return $app->oauth->redirect();
  85. }
  86. $uri = $request->path();
  87. $token = $request->get('token');
  88. $rfee = $request->get('amount');
  89. $crm = $request->get('crm');
  90. if($uri == 'freethrhcurrency'){
  91. $fee = 300;
  92. $this->stats('reward_300',$crm);
  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. $get_free_currency = UserBindHkWelfareService::isHasGet($user[0]);
  100. if($get_free_currency){
  101. //已经领过
  102. return view('jump.bindHkFreeCurrency',['fee'=>$fee,'url'=>$this->getLink($get_free_currency->distribution_channel_id).'?'.http_build_query($params),'is_get'=>1,'uid'=>$get_free_currency->uid]);
  103. }else{
  104. return view('jump.bindHkFreeCurrency',['fee'=>$fee,'url'=>$this->getLink($user[1]).'?'.http_build_query($params),'is_get'=>0,'uid'=>$user[0]]);
  105. }
  106. }
  107. private function freeCurrencyFee($token,$fee){
  108. if(!$token || !$fee) return 200;
  109. $param['fee'] = $fee;
  110. if( _sign($param,env('SECRET_KEY')) == $token) return $fee;
  111. return 200;
  112. }
  113. public function freeCurrencyPost(Request $request){
  114. $uid = $request->post('uid');
  115. $fee = $request->post('fee',200);
  116. $result = UserBindHkWelfareService::getfreeCurrency($uid,$fee);
  117. if($result){
  118. UserService::addBalance($uid,$fee,0,$fee);
  119. }
  120. return response()->success(['uid'=>$uid,'result'=>$result]);
  121. }
  122. public function activity(Request $request){
  123. $token = $request->get('token');
  124. if(empty($token)){
  125. $default_ink = $this->getLink();
  126. return redirect()->to($default_ink);
  127. }
  128. $openid = $request->get('openid');
  129. //授权
  130. $params = $request->except('_url');
  131. if(empty($openid)){
  132. $url = url()->current() . '?' . http_build_query($params);
  133. $params['redirect_url'] = urlencode($url);
  134. $app = new Application($this->auth($params));
  135. return $app->oauth->redirect();
  136. }
  137. $crm = $request->get('crm');
  138. $this->stats('activity',$crm);
  139. $activity = ActivityService::getByToken($token);
  140. if($activity){
  141. $user = $this->getUsers($openid);
  142. if($user[0]){
  143. $url_format = '%s://site%s.%s.com%s';
  144. $url = sprintf($url_format, env('PROTOCOL'), encodeDistributionChannelId($user[1]),
  145. env('CUSTOM_HOST'),
  146. $activity->activity_page
  147. );
  148. return redirect()->to($url);
  149. }
  150. }
  151. $default_ink = $this->getLink();
  152. return redirect()->to($default_ink);
  153. }
  154. public function recent(Request $request){
  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. $user = $this->getUsers($openid);
  165. if(!$user[0]){
  166. $distribution_channel_id = 123;
  167. }else{
  168. $distribution_channel_id = $user[1];
  169. }
  170. $crm = $request->get('crm');
  171. $this->stats('recent',$crm);
  172. $link = $this->getLink($distribution_channel_id).'recent?'.http_build_query($params);
  173. return redirect()->to($link);
  174. }
  175. public function person(Request $request){
  176. $openid = $request->get('openid');
  177. //授权
  178. $params = $request->except('_url');
  179. if(empty($openid)){
  180. $url = url()->current() . '?' . http_build_query($params);
  181. $params['redirect_url'] = urlencode($url);
  182. $app = new Application($this->auth($params));
  183. return $app->oauth->redirect();
  184. }
  185. $user = $this->getUsers($openid);
  186. if(!$user[0]){
  187. $distribution_channel_id = 123;
  188. }else{
  189. $distribution_channel_id = $user[1];
  190. }
  191. $crm = $request->get('crm');
  192. $this->stats('person',$crm);
  193. $link = $this->getLink($distribution_channel_id).'person?'.http_build_query($params);
  194. return redirect()->to($link);
  195. }
  196. private function getUsers($openid){
  197. $users = User::where('openid',$openid)->select('id','distribution_channel_id')->get();
  198. if($users->isEmpty()) return [0,0];
  199. $temp = null;
  200. $distribution_channel_id = 0;
  201. $uid = 0;
  202. foreach ($users as $user){
  203. $last_read = ReadRecordService::getByField($user->id,'last_read');
  204. if(!$last_read) continue;
  205. if(!$temp ){
  206. $distribution_channel_id = $user->distribution_channel_id;
  207. $uid = $user->id;
  208. $temp = $last_read;
  209. }else{
  210. $temp_last_read_info = explode('_',$temp);
  211. $last_read_info = explode('_',$last_read);
  212. if($last_read_info[2] > $temp_last_read_info[2]){
  213. $uid = $user->id;
  214. $distribution_channel_id = $user->distribution_channel_id;
  215. $temp = $last_read;
  216. }
  217. }
  218. }
  219. return [$uid,$distribution_channel_id];
  220. }
  221. /**
  222. * @param $openid
  223. * @return array
  224. */
  225. private function getUsersV2($openid,$bid){
  226. $friend_link_uid_bind = DB::table('friend_link_uid_bind')->where('openid',$openid)->where('bid',$bid)->first();
  227. if($friend_link_uid_bind) {
  228. $user = User::where('id',$friend_link_uid_bind->uid)->select('id','distribution_channel_id')->get();
  229. if($user) {
  230. return [$user->id,$user->distribution_channel_id];
  231. }
  232. }
  233. $inner_channels = Channel::join('channel_users','channel_users.id','=','distribution_channels.channel_user_id')
  234. ->select('distribution_channls.id')
  235. ->whereIn('channel_users.id',explode(',',redisEnv('PROMOTION_GROUP_CHANNEL_USER_ID')))
  236. ->get()
  237. ->pluck('id');
  238. $users = User::where('openid',$openid)->select('id','distribution_channel_id')->get();
  239. if($users->isEmpty()) return [0,0];
  240. $temp = null;
  241. $distribution_channel_id = 0;
  242. $uid = 0;
  243. $inner_channel_uid_list = [];
  244. //先筛网站运营部UID
  245. foreach ($users as $user){
  246. if(in_array($user->distribution_channel_id,$inner_channels)) {
  247. $inner_channel_uid_list[] = $user;
  248. }
  249. }
  250. if(count($inner_channel_uid_list) == 1) {
  251. return [$inner_channel_uid_list[0]->id,$inner_channel_uid_list[0]->distribution_channel_id];
  252. }
  253. if(count($inner_channel_uid_list)>1) {
  254. //有多个网站运营部UID,针对网站运营部UID进入到下一步筛选
  255. $users = $inner_channel_uid_list;
  256. }
  257. //筛选包年UID
  258. $uid_list = [];
  259. foreach ($users as $item) {
  260. $uid_list[] =$item->id;
  261. }
  262. $year_order = YearOrder::whereIn('uid',$uid_list)->where('bid')->orderBy('end_time','desc')->first();
  263. if($year_order) {
  264. foreach ($users as $item) {
  265. if($item->id == $year_order->uid) {
  266. //筛选包年结束时间最后的UID
  267. return [$item->id,$item->distribution_channel_id];
  268. }
  269. }
  270. }
  271. //按订阅章节数最多的筛选
  272. $users_select = ['id'=>0,'count'=>0,'user'=>null];
  273. foreach ($users as $each){
  274. $table_prefix = ($each->id)%512;
  275. $chapter_count = DB::connection('chapter_order_mysql')
  276. ->table('chapter_orders'.$table_prefix)
  277. ->where('created_at','>=',date('Y-m-d H:i:s',strtotime('-3 day')))
  278. ->where('uid',$each->id)
  279. ->where('bid',$bid)
  280. ->count('id');
  281. $book_order = BookOrder::where('uid',$each->id)
  282. ->where('bid',$bid)
  283. ->where('created_at','>=',date('Y-m-d H:i:s',strtotime('-3 day')))
  284. ->first();
  285. if($book_order) {
  286. $chapter_count += 30;
  287. }
  288. //$each->chapter_count = $chapter_count;
  289. if($chapter_count>$users_select['count']) {
  290. $users_select['id'] = $each->id;
  291. $users_select['count'] = $chapter_count;
  292. $users_select['user'] = $each;
  293. }
  294. }
  295. if($users_select['id'] >0) {
  296. return [$users_select['user']->id,$users_select['user']->distribution_channel_id];
  297. }
  298. //有订阅记录的
  299. foreach ($users as $value) {
  300. $table_prefix = ($value->id)%512;
  301. $chapter_order = DB::connection('chapter_order_mysql')
  302. ->table('chapter_orders'.$table_prefix)
  303. ->where('uid',$value->id)
  304. ->where('bid',$bid)
  305. ->orderBy('created_at','desc')
  306. ->first();
  307. $book_order = BookOrder::where('uid',$value->id)
  308. ->where('bid',$bid)
  309. ->first();
  310. if($chapter_order ){
  311. if(strtotime($chapter_order->created_at) > $users_select['create_time']) {
  312. $users_select['id'] = $value->id;
  313. $users_select['create_time'] = strtotime($chapter_order->created_at);
  314. $users_select['user'] = $value;
  315. }
  316. }
  317. if($book_order){
  318. if(strtotime($book_order->created_at) > $users_select['create_time']){
  319. $users_select['id']=$value->id;
  320. $users_select['create_time']= strtotime($book_order->created_at);
  321. $users_select['user']= $value;
  322. }
  323. }
  324. }
  325. if($users_select['id'] >0) {
  326. return [$users_select['user']->id,$users_select['user']->distribution_channel_id];
  327. }
  328. return [0,0];
  329. }
  330. private function getLink($distribution_channel_id=123){
  331. $url_format = '%s://site%s.%s.com/';
  332. return sprintf(
  333. $url_format,
  334. env('PROTOCOL'),
  335. encodeDistributionChannelId($distribution_channel_id),
  336. env('CUSTOM_HOST')
  337. );
  338. }
  339. private function auth($param){
  340. $param['appid'] = 'wx9d389a73b88bbeae';
  341. $options = [
  342. 'app_id' => 'wx9d389a73b88bbeae',
  343. 'secret' => '2f6594bb595dfa256b5512e43a32a3d3',
  344. 'oauth' => [
  345. 'scopes' => ['snsapi_base'],
  346. 'callback' => env('AUTH_CALLBACK_URL') . '?' . http_build_query($param),
  347. ],
  348. ];
  349. return $options;
  350. }
  351. private function stats($page,$flag){
  352. if(!$page || !$flag) return ;
  353. //pv
  354. $day = date('Y-m-d');
  355. $pv_key = sprintf('crm:pv:%s',$flag);
  356. Redis::hincrby($pv_key, $day, 1);
  357. //Redis::hincrby($pv_key, 'total', 1);
  358. Redis::sadd('crm_'.$day,$flag);
  359. //uv
  360. $cookie_flag = Cookie::get('crm_flag');
  361. if(!$cookie_flag || $cookie_flag != $flag){
  362. $uv_key = sprintf('crm:uv:%s',$flag);
  363. Redis::hincrby($uv_key, $day, 1);
  364. //Redis::hincrby($uv_key, 'total', 1);
  365. }
  366. Cookie::queue('crm_flag', $flag, env('U_COOKIE_EXPIRE'), null, null, false, false);
  367. }
  368. }