CoflController.php 3.8 KB

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