CoflController.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. use Redis;
  16. use Cookie;
  17. /**
  18. * 朋友圈链接
  19. * Class CoflController
  20. * @package App\Http\Controllers\Wap\User
  21. */
  22. class CoflController extends Controller
  23. {
  24. public function index(Request $request){
  25. $bid = $request->get('bid');
  26. if(empty($bid)){
  27. $default_ink = $this->getLink();
  28. return redirect()->to($default_ink);
  29. }
  30. $openid = $request->get('openid');
  31. //授权
  32. $params = $request->except('_url');
  33. if(empty($openid)){
  34. //$url = str_replace('http://', env('PROTOCOL') . '://', url()->current() . '?' . http_build_query($params));
  35. $url = url()->current() . '?' . http_build_query($params);
  36. $params['redirect_url'] = urlencode($url);
  37. $app = new Application($this->auth($params));
  38. return $app->oauth->redirect();
  39. }
  40. try{
  41. $bid = Hashids::decode($bid)[0];
  42. }catch (Exception $e){
  43. return redirect()->to($this->getLink());
  44. }
  45. $crm = $request->get('crm');
  46. //获取用户
  47. $user = $this->getUsers($openid);
  48. if(!$user[0]){
  49. $user[1] = 123;
  50. }
  51. //有阅读纪录的跳转
  52. $read_record = ReadRecordService::getByField($user[0],$bid);
  53. if($read_record){
  54. $cid = explode('_',$read_record)[0];
  55. }else{
  56. //没有阅读记录的跳转
  57. $book_info = BookConfigService::getBookById($bid);
  58. $cid = $book_info->first_cid;
  59. }
  60. $params['cid'] = $cid;
  61. if(isset($params['openid'])) unset($params['openid']);
  62. if(isset($params['unionid'])) unset($params['unionid']);
  63. $url = $this->getLink($user[1]).'reader?'.http_build_query($params);
  64. $this->stats('book',$crm);
  65. return redirect()->to($url);
  66. }
  67. public function freeCurrencyView(Request $request){
  68. $openid = $request->get('openid');
  69. //授权
  70. $params = $request->except('_url');
  71. if(empty($openid)){
  72. //$url = str_replace('http://', env('PROTOCOL') . '://', url()->current() . '?' . http_build_query($params));
  73. $url = url()->current() . '?' . http_build_query($params);
  74. $params['redirect_url'] = urlencode($url);
  75. $app = new Application($this->auth($params));
  76. return $app->oauth->redirect();
  77. }
  78. $uri = $request->path();
  79. $token = $request->get('token');
  80. $rfee = $request->get('amount');
  81. $crm = $request->get('crm');
  82. if($uri == 'freethrhcurrency'){
  83. $fee = 300;
  84. $this->stats('reward_300',$crm);
  85. }else{
  86. $this->stats('reward_200',$crm);
  87. $fee = $this->freeCurrencyFee($token,$rfee);
  88. }
  89. $user = $this->getUsers($openid);
  90. if(!$user || !$user[0] || !$user[1]) return redirect()->to($this->getLink().'?'.http_build_query($params));
  91. $get_free_currency = UserBindHkWelfareService::isHasGet($user[0]);
  92. if($get_free_currency){
  93. //已经领过
  94. return view('jump.bindHkFreeCurrency',['fee'=>$fee,'url'=>$this->getLink($get_free_currency->distribution_channel_id).'?'.http_build_query($params),'is_get'=>1,'uid'=>$get_free_currency->uid]);
  95. }else{
  96. return view('jump.bindHkFreeCurrency',['fee'=>$fee,'url'=>$this->getLink($user[1]).'?'.http_build_query($params),'is_get'=>0,'uid'=>$user[0]]);
  97. }
  98. }
  99. private function freeCurrencyFee($token,$fee){
  100. if(!$token || !$fee) return 200;
  101. $param['fee'] = $fee;
  102. if( _sign($param,env('SECRET_KEY')) == $token) return $fee;
  103. return 200;
  104. }
  105. public function freeCurrencyPost(Request $request){
  106. $uid = $request->post('uid');
  107. $fee = $request->post('fee',200);
  108. $result = UserBindHkWelfareService::getfreeCurrency($uid,$fee);
  109. if($result){
  110. UserService::addBalance($uid,$fee,0,$fee);
  111. }
  112. return response()->success(['uid'=>$uid,'result'=>$result]);
  113. }
  114. public function activity(Request $request){
  115. $token = $request->get('token');
  116. if(empty($token)){
  117. $default_ink = $this->getLink();
  118. return redirect()->to($default_ink);
  119. }
  120. $openid = $request->get('openid');
  121. //授权
  122. $params = $request->except('_url');
  123. if(empty($openid)){
  124. $url = url()->current() . '?' . http_build_query($params);
  125. $params['redirect_url'] = urlencode($url);
  126. $app = new Application($this->auth($params));
  127. return $app->oauth->redirect();
  128. }
  129. $crm = $request->get('crm');
  130. $this->stats('activity',$crm);
  131. $activity = ActivityService::getByToken($token);
  132. if($activity){
  133. $user = $this->getUsers($openid);
  134. if($user[0]){
  135. $url_format = '%s://site%s.%s.com%s';
  136. $url = sprintf($url_format, env('PROTOCOL'), encodeDistributionChannelId($user[1]),
  137. env('CUSTOM_HOST'),
  138. $activity->activity_page
  139. );
  140. return redirect()->to($url);
  141. }
  142. }
  143. $default_ink = $this->getLink();
  144. return redirect()->to($default_ink);
  145. }
  146. public function recent(Request $request){
  147. $openid = $request->get('openid');
  148. //授权
  149. $params = $request->except('_url');
  150. if(empty($openid)){
  151. $url = url()->current() . '?' . http_build_query($params);
  152. $params['redirect_url'] = urlencode($url);
  153. $app = new Application($this->auth($params));
  154. return $app->oauth->redirect();
  155. }
  156. $user = $this->getUsers($openid);
  157. if(!$user[0]){
  158. $distribution_channel_id = 123;
  159. }else{
  160. $distribution_channel_id = $user[1];
  161. }
  162. $crm = $request->get('crm');
  163. $this->stats('recent',$crm);
  164. $link = $this->getLink($distribution_channel_id).'recent?'.http_build_query($params);
  165. return redirect()->to($link);
  166. }
  167. public function person(Request $request){
  168. $openid = $request->get('openid');
  169. //授权
  170. $params = $request->except('_url');
  171. if(empty($openid)){
  172. $url = url()->current() . '?' . http_build_query($params);
  173. $params['redirect_url'] = urlencode($url);
  174. $app = new Application($this->auth($params));
  175. return $app->oauth->redirect();
  176. }
  177. $user = $this->getUsers($openid);
  178. if(!$user[0]){
  179. $distribution_channel_id = 123;
  180. }else{
  181. $distribution_channel_id = $user[1];
  182. }
  183. $crm = $request->get('crm');
  184. $this->stats('person',$crm);
  185. $link = $this->getLink($distribution_channel_id).'person?'.http_build_query($params);
  186. return redirect()->to($link);
  187. }
  188. private function getUsers($openid){
  189. $users = User::where('openid',$openid)->select('id','distribution_channel_id')->get();
  190. if($users->isEmpty()) return [0,0];
  191. $temp = null;
  192. $distribution_channel_id = 0;
  193. $uid = 0;
  194. foreach ($users as $user){
  195. $last_read = ReadRecordService::getByField($user->id,'last_read');
  196. if(!$last_read) continue;
  197. if(!$temp ){
  198. $distribution_channel_id = $user->distribution_channel_id;
  199. $uid = $user->id;
  200. $temp = $last_read;
  201. }else{
  202. $temp_last_read_info = explode('_',$temp);
  203. $last_read_info = explode('_',$last_read);
  204. if($last_read_info[2] > $temp_last_read_info[2]){
  205. $uid = $user->id;
  206. $distribution_channel_id = $user->distribution_channel_id;
  207. $temp = $last_read;
  208. }
  209. }
  210. }
  211. return [$uid,$distribution_channel_id];
  212. }
  213. private function getLink($distribution_channel_id=123){
  214. $url_format = '%s://site%s.%s.com/';
  215. return sprintf(
  216. $url_format,
  217. env('PROTOCOL'),
  218. encodeDistributionChannelId($distribution_channel_id),
  219. env('CUSTOM_HOST')
  220. );
  221. }
  222. private function auth($param){
  223. $param['appid'] = 'wx9d389a73b88bbeae';
  224. $options = [
  225. 'app_id' => 'wx9d389a73b88bbeae',
  226. 'secret' => '2f6594bb595dfa256b5512e43a32a3d3',
  227. 'oauth' => [
  228. 'scopes' => ['snsapi_base'],
  229. 'callback' => env('AUTH_CALLBACK_URL') . '?' . http_build_query($param),
  230. ],
  231. ];
  232. return $options;
  233. }
  234. private function stats($page,$flag){
  235. if(!$page || !$flag) return ;
  236. //pv
  237. $day = date('Y-m-d');
  238. $pv_key = sprintf('crm:pv:%s',$flag);
  239. Redis::hincrby($pv_key, $day, 1);
  240. //Redis::hincrby($pv_key, 'total', 1);
  241. Redis::sadd('crm_'.$day,$flag);
  242. //uv
  243. $cookie_flag = Cookie::get('crm_flag');
  244. if(!$cookie_flag || $cookie_flag != $flag){
  245. $uv_key = sprintf('crm:uv:%s',$flag);
  246. Redis::hincrby($uv_key, $day, 1);
  247. //Redis::hincrby($uv_key, 'total', 1);
  248. }
  249. Cookie::queue('crm_flag', $flag, env('U_COOKIE_EXPIRE'), null, null, false, false);
  250. }
  251. }