CoflController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. if(!$user[0]){
  39. //用户不存在
  40. return redirect()->to($this->getLink());
  41. }
  42. //有阅读纪录的跳转
  43. $read_record = ReadRecordService::getByField($user[0],$bid);
  44. if($read_record){
  45. $cid = explode('_',$read_record)[0];
  46. }else{
  47. //没有阅读记录的跳转
  48. $book_info = BookConfigService::getBookById($bid);
  49. $cid = $book_info->first_cid;
  50. }
  51. $params['cid'] = $cid;
  52. $url = $this->getLink($user[1]).'/reader?'.http_build_query($params);
  53. return redirect()->to($url);
  54. }
  55. private function getUsers($openid){
  56. $users = User::where('openid',$openid)->select('id','distribution_channel_id')->get();
  57. Log::info('$openid is: '.$openid);
  58. Log::info('$users is: ');
  59. Log::info($users);
  60. if($users->isEmpty()) return [0,0];
  61. $temp = null;
  62. $distribution_channel_id = 0;
  63. $uid = 0;
  64. foreach ($users as $user){
  65. $last_read = ReadRecordService::getByField($user->id,'last_read');
  66. Log::info('foreach ($users as $user)');
  67. Log::info('$user');
  68. Log::info($user);
  69. Log::info('$last_read is');
  70. Log::info($last_read);
  71. if(!$last_read) continue;
  72. if(!$temp ){
  73. $distribution_channel_id = $user->distribution_channel_id;
  74. $uid = $user->id;
  75. $temp = $last_read;
  76. }else{
  77. $temp_last_read_info = explode('_',$temp);
  78. $last_read_info = explode('_',$last_read);
  79. if($last_read_info[2] > $temp_last_read_info[2]){
  80. $uid = $user->id;
  81. $distribution_channel_id = $user->distribution_channel_id;
  82. $temp = $last_read;
  83. }
  84. }
  85. }
  86. if($temp) [$uid,$distribution_channel_id];
  87. return [0,0];
  88. }
  89. private function getLink($distribution_channel_id=123){
  90. $url_format = '%s://site%s.%s.com/';
  91. return sprintf(
  92. $url_format,
  93. env('PROTOCOL'),
  94. encodeDistributionChannelId($distribution_channel_id),
  95. env('CUSTOM_HOST')
  96. );
  97. }
  98. private function auth($param){
  99. $param['appid'] = 'wx9d389a73b88bbeae';
  100. $options = [
  101. 'app_id' => 'wx9d389a73b88bbeae',
  102. 'secret' => '2f6594bb595dfa256b5512e43a32a3d3',
  103. 'oauth' => [
  104. 'scopes' => ['snsapi_base'],
  105. 'callback' => env('AUTH_CALLBACK_URL') . '?' . http_build_query($param),
  106. ],
  107. ];
  108. return $options;
  109. }
  110. }