CoflController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. /**
  12. * 朋友圈链接
  13. * Class CoflController
  14. * @package App\Http\Controllers\Wap\User
  15. */
  16. class CoflController extends Controller
  17. {
  18. public function index(Request $request){
  19. Log::info('index param is');
  20. Log::info($request->all());
  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. $params['redirect_url'] = urlencode($url);
  32. $app = new Application($this->auth($params));
  33. return $app->oauth->redirect();
  34. }
  35. $bid = Hashids::decode($bid)[0];
  36. //获取用户
  37. $user = $this->getUsers($openid);
  38. Log::info('$user = $this->getUsers($openid)');
  39. Log::info($user);
  40. if(!$user[0]){
  41. //用户不存在
  42. return redirect()->to($this->getLink());
  43. }
  44. //有阅读纪录的跳转
  45. $read_record = ReadRecordService::getByField($user[0],$bid);
  46. if($read_record){
  47. $cid = explode('_',$read_record)[0];
  48. }else{
  49. //没有阅读记录的跳转
  50. $book_info = BookConfigService::getBookById($bid);
  51. $cid = $book_info->first_cid;
  52. }
  53. $params['cid'] = $cid;
  54. if(isset($params['openid'])) unset($params['openid']);
  55. if(isset($params['unionid'])) unset($params['unionid']);
  56. $url = $this->getLink($user[1]).'reader?'.http_build_query($params);
  57. Log::info('$ur is: '.$url);
  58. return redirect()->to($url);
  59. }
  60. private function getUsers($openid){
  61. $users = User::where('openid',$openid)->select('id','distribution_channel_id')->get();
  62. Log::info('$openid is: '.$openid);
  63. Log::info('$users is: ');
  64. Log::info($users);
  65. if($users->isEmpty()) return [0,0];
  66. $temp = null;
  67. $distribution_channel_id = 0;
  68. $uid = 0;
  69. foreach ($users as $user){
  70. $last_read = ReadRecordService::getByField($user->id,'last_read');
  71. Log::info('foreach ($users as $user)');
  72. Log::info('$user');
  73. Log::info($user);
  74. Log::info('$last_read is');
  75. Log::info($last_read);
  76. if(!$last_read) continue;
  77. if(!$temp ){
  78. $distribution_channel_id = $user->distribution_channel_id;
  79. $uid = $user->id;
  80. $temp = $last_read;
  81. }else{
  82. $temp_last_read_info = explode('_',$temp);
  83. $last_read_info = explode('_',$last_read);
  84. if($last_read_info[2] > $temp_last_read_info[2]){
  85. $uid = $user->id;
  86. $distribution_channel_id = $user->distribution_channel_id;
  87. $temp = $last_read;
  88. }
  89. }
  90. }
  91. return [$uid,$distribution_channel_id];
  92. }
  93. private function getLink($distribution_channel_id=123){
  94. $url_format = '%s://site%s.%s.com/';
  95. return sprintf(
  96. $url_format,
  97. env('PROTOCOL'),
  98. encodeDistributionChannelId($distribution_channel_id),
  99. env('CUSTOM_HOST')
  100. );
  101. }
  102. private function auth($param){
  103. $param['appid'] = 'wx9d389a73b88bbeae';
  104. $options = [
  105. 'app_id' => 'wx9d389a73b88bbeae',
  106. 'secret' => '2f6594bb595dfa256b5512e43a32a3d3',
  107. 'oauth' => [
  108. 'scopes' => ['snsapi_base'],
  109. 'callback' => env('AUTH_CALLBACK_URL') . '?' . http_build_query($param),
  110. ],
  111. ];
  112. return $options;
  113. }
  114. }