CoflController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 Illuminate\Http\Request;
  8. use App\Http\Controllers\Controller;
  9. use Hashids;
  10. use EasyWeChat\Foundation\Application;
  11. use Log;
  12. use Exception;
  13. /**
  14. * 朋友圈链接
  15. * Class CoflController
  16. * @package App\Http\Controllers\Wap\User
  17. */
  18. class CoflController extends Controller
  19. {
  20. public function index(Request $request){
  21. $bid = $request->get('bid');
  22. if(empty($bid)){
  23. $default_ink = $this->getLink();
  24. return redirect()->to($default_ink);
  25. }
  26. $openid = $request->get('openid');
  27. //授权
  28. $params = $request->except('_url');
  29. if(empty($openid)){
  30. //$url = str_replace('http://', env('PROTOCOL') . '://', url()->current() . '?' . http_build_query($params));
  31. $url = url()->current() . '?' . http_build_query($params);
  32. $params['redirect_url'] = urlencode($url);
  33. $app = new Application($this->auth($params));
  34. return $app->oauth->redirect();
  35. }
  36. try{
  37. $bid = Hashids::decode($bid)[0];
  38. }catch (Exception $e){
  39. return redirect()->to($this->getLink());
  40. }
  41. //获取用户
  42. $user = $this->getUsers($openid);
  43. if(!$user[0]){
  44. $user[1] = 123;
  45. }
  46. //有阅读纪录的跳转
  47. $read_record = ReadRecordService::getByField($user[0],$bid);
  48. if($read_record){
  49. $cid = explode('_',$read_record)[0];
  50. }else{
  51. //没有阅读记录的跳转
  52. $book_info = BookConfigService::getBookById($bid);
  53. $cid = $book_info->first_cid;
  54. }
  55. $params['cid'] = $cid;
  56. if(isset($params['openid'])) unset($params['openid']);
  57. if(isset($params['unionid'])) unset($params['unionid']);
  58. $url = $this->getLink($user[1]).'reader?'.http_build_query($params);
  59. return redirect()->to($url);
  60. }
  61. public function activity(Request $request){
  62. $token = $request->get('token');
  63. if(empty($token)){
  64. $default_ink = $this->getLink();
  65. return redirect()->to($default_ink);
  66. }
  67. $openid = $request->get('openid');
  68. //授权
  69. $params = $request->except('_url');
  70. if(empty($openid)){
  71. $url = url()->current() . '?' . http_build_query($params);
  72. $params['redirect_url'] = urlencode($url);
  73. $app = new Application($this->auth($params));
  74. return $app->oauth->redirect();
  75. }
  76. $activity = ActivityService::getByToken($token);
  77. if($activity){
  78. $user = $this->getUsers($openid);
  79. if($user[0]){
  80. $url_format = '%s://site%s.%s.com%s';
  81. $url = sprintf(
  82. $url_format,
  83. env('PROTOCOL'),
  84. encodeDistributionChannelId($user[1]),
  85. env('CUSTOM_HOST'),
  86. $activity->activity_page
  87. );
  88. return redirect()->to($url);
  89. }
  90. }
  91. $default_ink = $this->getLink();
  92. return redirect()->to($default_ink);
  93. }
  94. private function getUsers($openid){
  95. $users = User::where('openid',$openid)->select('id','distribution_channel_id')->get();
  96. if($users->isEmpty()) return [0,0];
  97. $temp = null;
  98. $distribution_channel_id = 0;
  99. $uid = 0;
  100. foreach ($users as $user){
  101. $last_read = ReadRecordService::getByField($user->id,'last_read');
  102. if(!$last_read) continue;
  103. if(!$temp ){
  104. $distribution_channel_id = $user->distribution_channel_id;
  105. $uid = $user->id;
  106. $temp = $last_read;
  107. }else{
  108. $temp_last_read_info = explode('_',$temp);
  109. $last_read_info = explode('_',$last_read);
  110. if($last_read_info[2] > $temp_last_read_info[2]){
  111. $uid = $user->id;
  112. $distribution_channel_id = $user->distribution_channel_id;
  113. $temp = $last_read;
  114. }
  115. }
  116. }
  117. return [$uid,$distribution_channel_id];
  118. }
  119. private function getLink($distribution_channel_id=123){
  120. $url_format = '%s://site%s.%s.com/';
  121. return sprintf(
  122. $url_format,
  123. env('PROTOCOL'),
  124. encodeDistributionChannelId($distribution_channel_id),
  125. env('CUSTOM_HOST')
  126. );
  127. }
  128. private function auth($param){
  129. $param['appid'] = 'wx9d389a73b88bbeae';
  130. $options = [
  131. 'app_id' => 'wx9d389a73b88bbeae',
  132. 'secret' => '2f6594bb595dfa256b5512e43a32a3d3',
  133. 'oauth' => [
  134. 'scopes' => ['snsapi_base'],
  135. 'callback' => env('AUTH_CALLBACK_URL') . '?' . http_build_query($param),
  136. ],
  137. ];
  138. return $options;
  139. }
  140. }