CoflController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. $url = $this->getLink($user[1]).'reader?'.http_build_query($params);
  55. Log::info('$ur is: '.$url);
  56. return redirect()->to($url);
  57. }
  58. private function getUsers($openid){
  59. $users = User::where('openid',$openid)->select('id','distribution_channel_id')->get();
  60. Log::info('$openid is: '.$openid);
  61. Log::info('$users is: ');
  62. Log::info($users);
  63. if($users->isEmpty()) return [0,0];
  64. $temp = null;
  65. $distribution_channel_id = 0;
  66. $uid = 0;
  67. foreach ($users as $user){
  68. $last_read = ReadRecordService::getByField($user->id,'last_read');
  69. Log::info('foreach ($users as $user)');
  70. Log::info('$user');
  71. Log::info($user);
  72. Log::info('$last_read is');
  73. Log::info($last_read);
  74. if(!$last_read) continue;
  75. if(!$temp ){
  76. $distribution_channel_id = $user->distribution_channel_id;
  77. $uid = $user->id;
  78. $temp = $last_read;
  79. }else{
  80. $temp_last_read_info = explode('_',$temp);
  81. $last_read_info = explode('_',$last_read);
  82. if($last_read_info[2] > $temp_last_read_info[2]){
  83. $uid = $user->id;
  84. $distribution_channel_id = $user->distribution_channel_id;
  85. $temp = $last_read;
  86. }
  87. }
  88. }
  89. return [$uid,$distribution_channel_id];
  90. }
  91. private function getLink($distribution_channel_id=123){
  92. $url_format = '%s://site%s.%s.com/';
  93. return sprintf(
  94. $url_format,
  95. env('PROTOCOL'),
  96. encodeDistributionChannelId($distribution_channel_id),
  97. env('CUSTOM_HOST')
  98. );
  99. }
  100. private function auth($param){
  101. $param['appid'] = 'wx9d389a73b88bbeae';
  102. $options = [
  103. 'app_id' => 'wx9d389a73b88bbeae',
  104. 'secret' => '2f6594bb595dfa256b5512e43a32a3d3',
  105. 'oauth' => [
  106. 'scopes' => ['snsapi_base'],
  107. 'callback' => env('AUTH_CALLBACK_URL') . '?' . http_build_query($param),
  108. ],
  109. ];
  110. return $options;
  111. }
  112. }