CoflController.php 7.0 KB

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