Toutiao.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Bear
  5. * Date: 2020/2/5
  6. * Time: 下午12:36
  7. */
  8. namespace app\api\controller;
  9. use app\common\library\Ip;
  10. use app\common\library\Redis;
  11. use app\main\constants\CacheConstants;
  12. use app\main\service\LogService;
  13. use think\Request;
  14. class Toutiao
  15. {
  16. public function visitorsave($id)
  17. {
  18. $referral = Request::instance()->post('referral');
  19. if ($referral) {
  20. $ip = Ip::ip();
  21. $appid = Request::instance()->post('appid', '');
  22. $key = CacheConstants::getGuideWxSubscribe($ip, $_SERVER['HTTP_USER_AGENT'] ?? '');
  23. if ($appid) {
  24. Redis::instance()->set($key, $appid . '和' . $id . '和' . $referral, CacheConstants::TOUTIAO_SUB_WINDOW);
  25. } else {
  26. Redis::instance()->set($key, $id . '和' . $referral, CacheConstants::TOUTIAO_SUB_WINDOW);
  27. }
  28. LogService::info('REF:' . $id . ':' . $appid . ':' . $key . ':' . $_SERVER['HTTP_USER_AGENT']);
  29. } else {
  30. LogService::notice('NOREF:' . $id);
  31. }
  32. }
  33. public function visitorsave4join()
  34. {
  35. $ip = Request::instance()->param('ip', '');
  36. $ua = Request::instance()->param('ua', '');
  37. $url = Request::instance()->param('url', '');
  38. if ($ip && $ua && $url) {
  39. $key = CacheConstants::getGuideWxSubscribe($ip, $ua);
  40. $appid = Request::instance()->param('appid', '');
  41. if ($appid) {
  42. Redis::instance()->set($key, $appid . '和0和' . $url, CacheConstants::TOUTIAO_SUB_WINDOW);
  43. } else {
  44. Redis::instance()->set($key, '0和' . $url, CacheConstants::TOUTIAO_SUB_WINDOW);
  45. }
  46. LogService::info('REF:0:' . $appid . ':' . $key . ':' . $ua . ':' . $url);
  47. return json(['code' => 0, 'msg' => 'ok']);
  48. } else {
  49. return json(['code' => 100, 'msg' => 'params ip,ua,url is required, appid is recommended']);
  50. }
  51. }
  52. public function visitorsavefromjs()
  53. {
  54. $ua = $_SERVER['HTTP_USER_AGENT'] ?? '';
  55. $ip = Ip::ip();
  56. $url = Request::instance()->url();
  57. if ($ip && $ua && $url) {
  58. $key = CacheConstants::getGuideWxSubscribe($ip, $ua);
  59. $appid = Request::instance()->param('appid', '');
  60. if ($appid) {
  61. Redis::instance()->set($key, $appid . '和0和' . $url, CacheConstants::TOUTIAO_SUB_WINDOW);
  62. } else {
  63. Redis::instance()->set($key, '0和' . $url, CacheConstants::TOUTIAO_SUB_WINDOW);
  64. }
  65. LogService::info('REF:0:' . $appid . ':' . $key . ':' . $ua . ':' . $url);
  66. return json(['code' => 0, 'msg' => 'ok']);
  67. } else {
  68. return json(['code' => 100, 'msg' => 'params ip,ua,url is required, appid is recommended']);
  69. }
  70. }
  71. }