CoflController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. $distribution_channel_id = (isset($user[1]) && !empty($user[1]))?$user[1]:123;
  135. $url_format = '%s://site%s.%s.com%s';
  136. $activity_page = $activity->activity_page ;
  137. if($activity->id == 6000){
  138. $activity_page = '/activity/common?token=LNyAqbFMgvkmvnHP8PXV3DYPIIhQm3oe';
  139. }
  140. $url = sprintf($url_format, env('PROTOCOL'), encodeDistributionChannelId($distribution_channel_id),
  141. env('CUSTOM_HOST'),
  142. $activity_page
  143. );
  144. return redirect()->to($url);
  145. }
  146. $default_ink = $this->getLink();
  147. return redirect()->to($default_ink);
  148. }
  149. public function recent(Request $request){
  150. $openid = $request->get('openid');
  151. //授权
  152. $params = $request->except('_url');
  153. if(empty($openid)){
  154. $url = url()->current() . '?' . http_build_query($params);
  155. $params['redirect_url'] = urlencode($url);
  156. $app = new Application($this->auth($params));
  157. return $app->oauth->redirect();
  158. }
  159. $user = $this->getUsers($openid);
  160. if(!$user[0]){
  161. $distribution_channel_id = 123;
  162. }else{
  163. $distribution_channel_id = $user[1];
  164. }
  165. $crm = $request->get('crm');
  166. $this->stats('recent',$crm);
  167. $link = $this->getLink($distribution_channel_id).'recent?'.http_build_query($params);
  168. return redirect()->to($link);
  169. }
  170. public function person(Request $request){
  171. $openid = $request->get('openid');
  172. //授权
  173. $params = $request->except('_url');
  174. if(empty($openid)){
  175. $url = url()->current() . '?' . http_build_query($params);
  176. $params['redirect_url'] = urlencode($url);
  177. $app = new Application($this->auth($params));
  178. return $app->oauth->redirect();
  179. }
  180. $user = $this->getUsers($openid);
  181. if(!$user[0]){
  182. $distribution_channel_id = 123;
  183. }else{
  184. $distribution_channel_id = $user[1];
  185. }
  186. $crm = $request->get('crm');
  187. $this->stats('person',$crm);
  188. $link = $this->getLink($distribution_channel_id).'person?'.http_build_query($params);
  189. return redirect()->to($link);
  190. }
  191. public function sign(Request $request){
  192. $openid = $request->get('openid');
  193. //授权
  194. $params = $request->except('_url');
  195. if(empty($openid)){
  196. $url = url()->current() . '?' . http_build_query($params);
  197. $params['redirect_url'] = urlencode($url);
  198. $app = new Application($this->auth($params));
  199. return $app->oauth->redirect();
  200. }
  201. $user = $this->getUsers($openid);
  202. if(!$user[0]){
  203. $distribution_channel_id = 123;
  204. }else{
  205. $distribution_channel_id = $user[1];
  206. }
  207. $crm = $request->get('crm');
  208. $this->stats('sign',$crm);
  209. $link = $this->getLink($distribution_channel_id).'sign?'.http_build_query($params);
  210. return redirect()->to($link);
  211. }
  212. private function getUsers($openid){
  213. $users = User::where('openid',$openid)->select('id','distribution_channel_id')->get();
  214. if($users->isEmpty()) return [0,0];
  215. $temp = null;
  216. $distribution_channel_id = 0;
  217. $uid = 0;
  218. foreach ($users as $user){
  219. $last_read = ReadRecordService::getByField($user->id,'last_read');
  220. if(!$last_read) continue;
  221. if(!$temp ){
  222. $distribution_channel_id = $user->distribution_channel_id;
  223. $uid = $user->id;
  224. $temp = $last_read;
  225. }else{
  226. $temp_last_read_info = explode('_',$temp);
  227. $last_read_info = explode('_',$last_read);
  228. if($last_read_info[2] > $temp_last_read_info[2]){
  229. $uid = $user->id;
  230. $distribution_channel_id = $user->distribution_channel_id;
  231. $temp = $last_read;
  232. }
  233. }
  234. }
  235. return [$uid,$distribution_channel_id];
  236. }
  237. private function getLink($distribution_channel_id=123){
  238. $url_format = '%s://site%s.%s.com/';
  239. return sprintf(
  240. $url_format,
  241. env('PROTOCOL'),
  242. encodeDistributionChannelId($distribution_channel_id),
  243. env('CUSTOM_HOST')
  244. );
  245. }
  246. private function auth($param){
  247. $param['appid'] = 'wx9d389a73b88bbeae';
  248. $options = [
  249. 'app_id' => 'wx9d389a73b88bbeae',
  250. 'secret' => '2f6594bb595dfa256b5512e43a32a3d3',
  251. 'oauth' => [
  252. 'scopes' => ['snsapi_base'],
  253. 'callback' => env('AUTH_CALLBACK_URL') . '?' . http_build_query($param),
  254. ],
  255. ];
  256. return $options;
  257. }
  258. private function stats($page,$flag){
  259. if(!$page || !$flag) return ;
  260. //pv
  261. $day = date('Y-m-d');
  262. $pv_key = sprintf('crm:pv:%s',$flag);
  263. Redis::hincrby($pv_key, $day, 1);
  264. //Redis::hincrby($pv_key, 'total', 1);
  265. Redis::sadd('crm_'.$day,$flag);
  266. //uv
  267. $cookie_flag = Cookie::get('crm_flag');
  268. if(!$cookie_flag || $cookie_flag != $flag){
  269. $uv_key = sprintf('crm:uv:%s',$flag);
  270. Redis::hincrby($uv_key, $day, 1);
  271. //Redis::hincrby($uv_key, 'total', 1);
  272. }
  273. Cookie::queue('crm_flag', $flag, env('U_COOKIE_EXPIRE'), null, null, false, false);
  274. }
  275. }