CoflController.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace App\Http\Controllers\Wap\User;
  3. use App\Modules\Activity\Services\ActivityService;
  4. use App\Modules\Book\Services\BookConfigService;
  5. use App\Modules\User\Models\User;
  6. use App\Modules\User\Services\ReadRecordService;
  7. use App\Modules\User\Services\UserBindHkWelfareService;
  8. use App\Modules\User\Services\UserService;
  9. use Illuminate\Http\Request;
  10. use App\Http\Controllers\Controller;
  11. use Hashids;
  12. use EasyWeChat\Foundation\Application;
  13. use Log;
  14. use Exception;
  15. /**
  16. * 朋友圈链接
  17. * Class CoflController
  18. * @package App\Http\Controllers\Wap\User
  19. */
  20. class CoflController extends Controller
  21. {
  22. public function index(Request $request){
  23. $bid = $request->get('bid');
  24. if(empty($bid)){
  25. $default_ink = $this->getLink();
  26. return redirect()->to($default_ink);
  27. }
  28. $openid = $request->get('openid');
  29. //授权
  30. $params = $request->except('_url');
  31. if(empty($openid)){
  32. //$url = str_replace('http://', env('PROTOCOL') . '://', url()->current() . '?' . http_build_query($params));
  33. $url = url()->current() . '?' . http_build_query($params);
  34. $params['redirect_url'] = urlencode($url);
  35. $app = new Application($this->auth($params));
  36. return $app->oauth->redirect();
  37. }
  38. try{
  39. $bid = Hashids::decode($bid)[0];
  40. }catch (Exception $e){
  41. return redirect()->to($this->getLink());
  42. }
  43. //获取用户
  44. $user = $this->getUsers($openid);
  45. if(!$user[0]){
  46. $user[1] = 123;
  47. }
  48. //有阅读纪录的跳转
  49. $read_record = ReadRecordService::getByField($user[0],$bid);
  50. if($read_record){
  51. $cid = explode('_',$read_record)[0];
  52. }else{
  53. //没有阅读记录的跳转
  54. $book_info = BookConfigService::getBookById($bid);
  55. $cid = $book_info->first_cid;
  56. }
  57. $params['cid'] = $cid;
  58. if(isset($params['openid'])) unset($params['openid']);
  59. if(isset($params['unionid'])) unset($params['unionid']);
  60. $url = $this->getLink($user[1]).'reader?'.http_build_query($params);
  61. return redirect()->to($url);
  62. }
  63. public function freeCurrencyView(Request $request){
  64. $openid = $request->get('openid');
  65. //授权
  66. $params = $request->except('_url');
  67. if(empty($openid)){
  68. //$url = str_replace('http://', env('PROTOCOL') . '://', url()->current() . '?' . http_build_query($params));
  69. $url = url()->current() . '?' . http_build_query($params);
  70. $params['redirect_url'] = urlencode($url);
  71. $app = new Application($this->auth($params));
  72. return $app->oauth->redirect();
  73. }
  74. $user = $this->getUsers($openid);
  75. if(!$user || !$user[0] || !$user[1]) return redirect()->to($this->getLink());
  76. $get_free_currency = UserBindHkWelfareService::isHasGet($user[0]);
  77. if($get_free_currency){
  78. //已经领过
  79. return view('jump.bindHkFreeCurrency',['url'=>$this->getLink($get_free_currency->distribution_channel_id),'is_get'=>1,'uid'=>$get_free_currency->uid]);
  80. }else{
  81. return view('jump.bindHkFreeCurrency',['url'=>$this->getLink($user[1]),'is_get'=>0,'uid'=>$user[0]]);
  82. }
  83. }
  84. public function freeCurrencyPost(Request $request){
  85. $uid = $request->post('uid');
  86. $result = UserBindHkWelfareService::getfreeCurrency($uid);
  87. if($result){
  88. UserService::addBalance($uid,200,0,200);
  89. }
  90. return response()->success(['uid'=>$uid,'result'=>$result]);
  91. }
  92. public function activity(Request $request){
  93. $token = $request->get('token');
  94. if(empty($token)){
  95. $default_ink = $this->getLink();
  96. return redirect()->to($default_ink);
  97. }
  98. $openid = $request->get('openid');
  99. //授权
  100. $params = $request->except('_url');
  101. if(empty($openid)){
  102. $url = url()->current() . '?' . http_build_query($params);
  103. $params['redirect_url'] = urlencode($url);
  104. $app = new Application($this->auth($params));
  105. return $app->oauth->redirect();
  106. }
  107. $activity = ActivityService::getByToken($token);
  108. if($activity){
  109. $user = $this->getUsers($openid);
  110. if($user[0]){
  111. $url_format = '%s://site%s.%s.com%s';
  112. $url = sprintf(
  113. $url_format,
  114. env('PROTOCOL'),
  115. encodeDistributionChannelId($user[1]),
  116. env('CUSTOM_HOST'),
  117. $activity->activity_page
  118. );
  119. return redirect()->to($url);
  120. }
  121. }
  122. $default_ink = $this->getLink();
  123. return redirect()->to($default_ink);
  124. }
  125. private function getUsers($openid){
  126. $users = User::where('openid',$openid)->select('id','distribution_channel_id')->get();
  127. if($users->isEmpty()) return [0,0];
  128. $temp = null;
  129. $distribution_channel_id = 0;
  130. $uid = 0;
  131. foreach ($users as $user){
  132. $last_read = ReadRecordService::getByField($user->id,'last_read');
  133. if(!$last_read) continue;
  134. if(!$temp ){
  135. $distribution_channel_id = $user->distribution_channel_id;
  136. $uid = $user->id;
  137. $temp = $last_read;
  138. }else{
  139. $temp_last_read_info = explode('_',$temp);
  140. $last_read_info = explode('_',$last_read);
  141. if($last_read_info[2] > $temp_last_read_info[2]){
  142. $uid = $user->id;
  143. $distribution_channel_id = $user->distribution_channel_id;
  144. $temp = $last_read;
  145. }
  146. }
  147. }
  148. return [$uid,$distribution_channel_id];
  149. }
  150. private function getLink($distribution_channel_id=123){
  151. $url_format = '%s://site%s.%s.com/';
  152. return sprintf(
  153. $url_format,
  154. env('PROTOCOL'),
  155. encodeDistributionChannelId($distribution_channel_id),
  156. env('CUSTOM_HOST')
  157. );
  158. }
  159. private function auth($param){
  160. $param['appid'] = 'wx9d389a73b88bbeae';
  161. $options = [
  162. 'app_id' => 'wx9d389a73b88bbeae',
  163. 'secret' => '2f6594bb595dfa256b5512e43a32a3d3',
  164. 'oauth' => [
  165. 'scopes' => ['snsapi_base'],
  166. 'callback' => env('AUTH_CALLBACK_URL') . '?' . http_build_query($param),
  167. ],
  168. ];
  169. return $options;
  170. }
  171. }