CoflController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. $token = $request->get('token');
  75. $rfee = $request->get('amount');
  76. $fee = $this->freeCurrencyFee($token,$rfee);
  77. $user = $this->getUsers($openid);
  78. if(!$user || !$user[0] || !$user[1]) return redirect()->to($this->getLink());
  79. $get_free_currency = UserBindHkWelfareService::isHasGet($user[0]);
  80. if($get_free_currency){
  81. //已经领过
  82. return view('jump.bindHkFreeCurrency',['fee'=>$fee,'url'=>$this->getLink($get_free_currency->distribution_channel_id),'is_get'=>1,'uid'=>$get_free_currency->uid]);
  83. }else{
  84. return view('jump.bindHkFreeCurrency',['fee'=>$fee,'url'=>$this->getLink($user[1]),'is_get'=>0,'uid'=>$user[0]]);
  85. }
  86. }
  87. private function freeCurrencyFee($token,$fee){
  88. if(!$token || !$fee) return 200;
  89. $param['fee'] = $fee;
  90. if( _sign($param,env('SECRET_KEY')) == $token) return $fee;
  91. return 200;
  92. }
  93. public function freeCurrencyPost(Request $request){
  94. $uid = $request->post('uid');
  95. $fee = $request->post('fee',200);
  96. $result = UserBindHkWelfareService::getfreeCurrency($uid,$fee);
  97. if($result){
  98. UserService::addBalance($uid,$fee,0,$fee);
  99. }
  100. return response()->success(['uid'=>$uid,'result'=>$result]);
  101. }
  102. public function activity(Request $request){
  103. $token = $request->get('token');
  104. if(empty($token)){
  105. $default_ink = $this->getLink();
  106. return redirect()->to($default_ink);
  107. }
  108. $openid = $request->get('openid');
  109. //授权
  110. $params = $request->except('_url');
  111. if(empty($openid)){
  112. $url = url()->current() . '?' . http_build_query($params);
  113. $params['redirect_url'] = urlencode($url);
  114. $app = new Application($this->auth($params));
  115. return $app->oauth->redirect();
  116. }
  117. $activity = ActivityService::getByToken($token);
  118. if($activity){
  119. $user = $this->getUsers($openid);
  120. if($user[0]){
  121. $url_format = '%s://site%s.%s.com%s';
  122. $url = sprintf(
  123. $url_format,
  124. env('PROTOCOL'),
  125. encodeDistributionChannelId($user[1]),
  126. env('CUSTOM_HOST'),
  127. $activity->activity_page
  128. );
  129. return redirect()->to($url);
  130. }
  131. }
  132. $default_ink = $this->getLink();
  133. return redirect()->to($default_ink);
  134. }
  135. private function getUsers($openid){
  136. $users = User::where('openid',$openid)->select('id','distribution_channel_id')->get();
  137. if($users->isEmpty()) return [0,0];
  138. $temp = null;
  139. $distribution_channel_id = 0;
  140. $uid = 0;
  141. foreach ($users as $user){
  142. $last_read = ReadRecordService::getByField($user->id,'last_read');
  143. if(!$last_read) continue;
  144. if(!$temp ){
  145. $distribution_channel_id = $user->distribution_channel_id;
  146. $uid = $user->id;
  147. $temp = $last_read;
  148. }else{
  149. $temp_last_read_info = explode('_',$temp);
  150. $last_read_info = explode('_',$last_read);
  151. if($last_read_info[2] > $temp_last_read_info[2]){
  152. $uid = $user->id;
  153. $distribution_channel_id = $user->distribution_channel_id;
  154. $temp = $last_read;
  155. }
  156. }
  157. }
  158. return [$uid,$distribution_channel_id];
  159. }
  160. private function getLink($distribution_channel_id=123){
  161. $url_format = '%s://site%s.%s.com/';
  162. return sprintf(
  163. $url_format,
  164. env('PROTOCOL'),
  165. encodeDistributionChannelId($distribution_channel_id),
  166. env('CUSTOM_HOST')
  167. );
  168. }
  169. private function auth($param){
  170. $param['appid'] = 'wx9d389a73b88bbeae';
  171. $options = [
  172. 'app_id' => 'wx9d389a73b88bbeae',
  173. 'secret' => '2f6594bb595dfa256b5512e43a32a3d3',
  174. 'oauth' => [
  175. 'scopes' => ['snsapi_base'],
  176. 'callback' => env('AUTH_CALLBACK_URL') . '?' . http_build_query($param),
  177. ],
  178. ];
  179. return $options;
  180. }
  181. }