|
@@ -0,0 +1,120 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Http\Controllers\Wap\User;
|
|
|
+
|
|
|
+use App\Modules\Book\Services\BookConfigService;
|
|
|
+use App\Modules\User\Models\User;
|
|
|
+use App\Modules\User\Services\ReadRecordService;
|
|
|
+use Illuminate\Http\Request;
|
|
|
+use App\Http\Controllers\Controller;
|
|
|
+use Hashids;
|
|
|
+use EasyWeChat\Foundation\Application;
|
|
|
+use Log;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 朋友圈链接
|
|
|
+ * Class CoflController
|
|
|
+ * @package App\Http\Controllers\Wap\User
|
|
|
+ */
|
|
|
+class CoflController extends Controller
|
|
|
+{
|
|
|
+ public function index(Request $request){
|
|
|
+ Log::info('index param is');
|
|
|
+ Log::info($request->all());
|
|
|
+ $bid = $request->get('bid');
|
|
|
+ if(empty($bid)){
|
|
|
+ $default_ink = $this->getLink();
|
|
|
+ return redirect()->to($default_ink);
|
|
|
+ }
|
|
|
+ $openid = $request->get('openid');
|
|
|
+ //授权
|
|
|
+ $params = $request->except('_url');
|
|
|
+ if(empty($openid)){
|
|
|
+ $url = str_replace('http://', env('PROTOCOL') . '://', url()->current() . '?' . http_build_query($params));
|
|
|
+ $params['redirect_url'] = urlencode($url);
|
|
|
+ $app = new Application($this->auth($params));
|
|
|
+ return $app->oauth->redirect();
|
|
|
+ }
|
|
|
+ $bid = Hashids::decode($bid)[0];
|
|
|
+ //获取用户
|
|
|
+ $user = $this->getUsers($openid);
|
|
|
+ if(!$user[0]){
|
|
|
+ //用户不存在
|
|
|
+ return redirect()->to($this->getLink());
|
|
|
+ }
|
|
|
+
|
|
|
+ //有阅读纪录的跳转
|
|
|
+ $read_record = ReadRecordService::getByField($user[0],$bid);
|
|
|
+ if($read_record){
|
|
|
+ $cid = explode('_',$read_record)[0];
|
|
|
+ }else{
|
|
|
+ //没有阅读记录的跳转
|
|
|
+ $book_info = BookConfigService::getBookById($bid);
|
|
|
+ $cid = $book_info->first_cid;
|
|
|
+ }
|
|
|
+ $params['cid'] = $cid;
|
|
|
+ $url = $this->getLink($user[1]).'/reader?'.http_build_query($params);
|
|
|
+ return redirect()->to($url);
|
|
|
+ }
|
|
|
+
|
|
|
+ private function getUsers($openid){
|
|
|
+ $users = User::where('openid',$openid)->select('id','distribution_channel_id')->get();
|
|
|
+ Log::info('$openid is: '.$openid);
|
|
|
+ Log::info('$users is: ');
|
|
|
+ Log::info($users);
|
|
|
+ if($users->isEmpty()) return [0,0];
|
|
|
+ $temp = null;
|
|
|
+ $distribution_channel_id = 0;
|
|
|
+ $uid = 0;
|
|
|
+ foreach ($users as $user){
|
|
|
+ $last_read = ReadRecordService::getByField($user->id,'last_read');
|
|
|
+ Log::info('foreach ($users as $user)');
|
|
|
+ Log::info('$user');
|
|
|
+ Log::info($user);
|
|
|
+ Log::info('$last_read is');
|
|
|
+ Log::info($last_read);
|
|
|
+ if(!$last_read) continue;
|
|
|
+ if(!$temp ){
|
|
|
+ $distribution_channel_id = $user->distribution_channel_id;
|
|
|
+ $uid = $user->id;
|
|
|
+ $temp = $last_read;
|
|
|
+ }else{
|
|
|
+ $temp_last_read_info = explode('_',$temp);
|
|
|
+ $last_read_info = explode('_',$last_read);
|
|
|
+ if($last_read_info[2] > $temp_last_read_info[2]){
|
|
|
+ $uid = $user->id;
|
|
|
+ $distribution_channel_id = $user->distribution_channel_id;
|
|
|
+ $temp = $last_read;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if($temp) [$uid,$distribution_channel_id];
|
|
|
+ return [0,0];
|
|
|
+ }
|
|
|
+
|
|
|
+ private function getLink($distribution_channel_id=123){
|
|
|
+ $url_format = '%s://site%s.%s.com/';
|
|
|
+ return sprintf(
|
|
|
+ $url_format,
|
|
|
+ env('PROTOCOL'),
|
|
|
+ encodeDistributionChannelId($distribution_channel_id),
|
|
|
+ env('CUSTOM_HOST')
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private function auth($param){
|
|
|
+ $param['appid'] = 'wx9d389a73b88bbeae';
|
|
|
+ $options = [
|
|
|
+ 'app_id' => 'wx9d389a73b88bbeae',
|
|
|
+ 'secret' => '2f6594bb595dfa256b5512e43a32a3d3',
|
|
|
+ 'oauth' => [
|
|
|
+ 'scopes' => ['snsapi_base'],
|
|
|
+ 'callback' => env('AUTH_CALLBACK_URL') . '?' . http_build_query($param),
|
|
|
+ ],
|
|
|
+ ];
|
|
|
+ return $options;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|