CoflController.php 12 KB

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