CoflController.php 14 KB

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