CoflController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 freeCurrencyView(Request $request){
  62. $openid = $request->get('openid');
  63. //授权
  64. $params = $request->except('_url');
  65. if(empty($openid)){
  66. //$url = str_replace('http://', env('PROTOCOL') . '://', url()->current() . '?' . http_build_query($params));
  67. $url = url()->current() . '?' . http_build_query($params);
  68. $params['redirect_url'] = urlencode($url);
  69. $app = new Application($this->auth($params));
  70. return $app->oauth->redirect();
  71. }
  72. }
  73. public function freeCurrencyPost(Request $request){
  74. }
  75. public function activity(Request $request){
  76. $token = $request->get('token');
  77. if(empty($token)){
  78. $default_ink = $this->getLink();
  79. return redirect()->to($default_ink);
  80. }
  81. $openid = $request->get('openid');
  82. //授权
  83. $params = $request->except('_url');
  84. if(empty($openid)){
  85. $url = url()->current() . '?' . http_build_query($params);
  86. $params['redirect_url'] = urlencode($url);
  87. $app = new Application($this->auth($params));
  88. return $app->oauth->redirect();
  89. }
  90. $activity = ActivityService::getByToken($token);
  91. if($activity){
  92. $user = $this->getUsers($openid);
  93. if($user[0]){
  94. $url_format = '%s://site%s.%s.com%s';
  95. $url = sprintf(
  96. $url_format,
  97. env('PROTOCOL'),
  98. encodeDistributionChannelId($user[1]),
  99. env('CUSTOM_HOST'),
  100. $activity->activity_page
  101. );
  102. return redirect()->to($url);
  103. }
  104. }
  105. $default_ink = $this->getLink();
  106. return redirect()->to($default_ink);
  107. }
  108. private function getUsers($openid){
  109. $users = User::where('openid',$openid)->select('id','distribution_channel_id')->get();
  110. if($users->isEmpty()) return [0,0];
  111. $temp = null;
  112. $distribution_channel_id = 0;
  113. $uid = 0;
  114. foreach ($users as $user){
  115. $last_read = ReadRecordService::getByField($user->id,'last_read');
  116. if(!$last_read) continue;
  117. if(!$temp ){
  118. $distribution_channel_id = $user->distribution_channel_id;
  119. $uid = $user->id;
  120. $temp = $last_read;
  121. }else{
  122. $temp_last_read_info = explode('_',$temp);
  123. $last_read_info = explode('_',$last_read);
  124. if($last_read_info[2] > $temp_last_read_info[2]){
  125. $uid = $user->id;
  126. $distribution_channel_id = $user->distribution_channel_id;
  127. $temp = $last_read;
  128. }
  129. }
  130. }
  131. return [$uid,$distribution_channel_id];
  132. }
  133. private function getLink($distribution_channel_id=123){
  134. $url_format = '%s://site%s.%s.com/';
  135. return sprintf(
  136. $url_format,
  137. env('PROTOCOL'),
  138. encodeDistributionChannelId($distribution_channel_id),
  139. env('CUSTOM_HOST')
  140. );
  141. }
  142. private function auth($param){
  143. $param['appid'] = 'wx9d389a73b88bbeae';
  144. $options = [
  145. 'app_id' => 'wx9d389a73b88bbeae',
  146. 'secret' => '2f6594bb595dfa256b5512e43a32a3d3',
  147. 'oauth' => [
  148. 'scopes' => ['snsapi_base'],
  149. 'callback' => env('AUTH_CALLBACK_URL') . '?' . http_build_query($param),
  150. ],
  151. ];
  152. return $options;
  153. }
  154. }