CoflController.php 15 KB

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