AgentClickController.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Facade\TableName;
  4. use App\Http\Controllers\Controller;
  5. use App\IpLocation;
  6. use App\Models\AccountsInfo;
  7. use App\Models\AgentClickLog;
  8. use App\Models\AgentUserInfo;
  9. use App\Models\AgentUserRecord;
  10. use App\Services\IpCheck;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Facades\DB;
  13. use Illuminate\Support\Facades\Log;
  14. class AgentClickController extends Controller
  15. {
  16. /**
  17. * 记录链接点击信息
  18. */
  19. public function recordClickPage(Request $request)
  20. {
  21. // 获取邀请码
  22. $code = $request->query('code');
  23. if (empty($code)||strlen($code)>6) {
  24. return response()->json([
  25. 'success' => false,
  26. 'message' => 'Invitation code is required'
  27. ], 400);
  28. }
  29. $ip=IpLocation::getIP();
  30. $agent=$_SERVER['HTTP_USER_AGENT'];
  31. if(AgentClickLog::query()->where('client_ip',$ip)->where('user_agent',$agent)->exists()){
  32. }else{
  33. // 检查邀请码是否存在
  34. $agentInfo = AgentUserInfo::where('referral_code', $code)->first();
  35. if (!$agentInfo) {
  36. // 仍然记录点击,但标记为无效
  37. $isValid = false;
  38. } else {
  39. $isValid = true;
  40. }
  41. $res=IpCheck::getGeoLocation($ip);
  42. // 检测设备类型
  43. $deviceType = AgentClickLog::detectDeviceType($agent);
  44. $referrer = $_SERVER['HTTP_ORIGIN'] ??$_SERVER['HTTP_REFERER']?? '';
  45. // 记录点击信息
  46. $clickLog = new AgentClickLog();
  47. $clickLog->code = $code;
  48. $clickLog->country = $res['country'];
  49. $clickLog->city = $res['city'];
  50. $clickLog->user_agent = $agent;
  51. $clickLog->client_ip = $ip;
  52. $clickLog->referrer = $referrer;
  53. $clickLog->device_type = $deviceType;
  54. $clickLog->is_valid = $isValid ? 1 : 0;
  55. $clickLog->save();
  56. }
  57. return "OK";
  58. }
  59. /**
  60. * 记录链接点击信息
  61. */
  62. public function recordClick(Request $request)
  63. {
  64. // 获取邀请码
  65. $code = $request->query('code');
  66. if (empty($code)||strlen($code)>6) {
  67. return response()->json([
  68. 'success' => false,
  69. 'message' => 'Invitation code is required'
  70. ], 400);
  71. }
  72. // 检查邀请码是否存在
  73. $agentInfo = AgentUserInfo::where('referral_code', $code)->first();
  74. if (!$agentInfo) {
  75. // 仍然记录点击,但标记为无效
  76. $isValid = false;
  77. } else {
  78. $isValid = true;
  79. }
  80. // 解析add参数中的JSON数据
  81. $addParam = $request->query('add');
  82. $clickData = [];
  83. if (!empty($addParam)) {
  84. try {
  85. $clickData = json_decode(urldecode($addParam), true) ?: [];
  86. } catch (\Exception $e) {
  87. Log::error('Error decoding click data: ' . $e->getMessage());
  88. $clickData = [];
  89. }
  90. }
  91. // 获取客户端信息
  92. $userAgent = $clickData['userAgent'] ?? $request->header('User-Agent');
  93. $country = $clickData['country'] ?? null;
  94. $city = $clickData['city'] ?? null;
  95. $clientIp = $clickData['clientIp'] ?? $request->ip();
  96. $referrer = $request->header('referer');
  97. // 检测设备类型
  98. $deviceType = AgentClickLog::detectDeviceType($userAgent);
  99. // 记录点击信息
  100. $clickLog = new AgentClickLog();
  101. $clickLog->code = $code;
  102. $clickLog->country = $country;
  103. $clickLog->city = $city;
  104. $clickLog->user_agent = $userAgent;
  105. $clickLog->client_ip = $clientIp;
  106. $clickLog->referrer = $referrer;
  107. $clickLog->device_type = $deviceType;
  108. $clickLog->is_valid = $isValid ? 1 : 0;
  109. $clickLog->save();
  110. // 如果需要重定向到某个页面
  111. if ($request->has('redirect')) {
  112. return redirect($request->query('redirect'));
  113. }
  114. // 返回1x1透明GIF图像
  115. $gif = base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7');
  116. return response($gif, 200)
  117. ->header('Content-Type', 'image/gif')
  118. ->header('Content-Length', strlen($gif));
  119. }
  120. static function changePlatformData($orgChannel,$newChannel)
  121. {
  122. DB::connection('write')->table(TableName::QPRecordDB() . 'RecordPlatformData')
  123. ->where('DateID', date('Ymd'))
  124. ->where('Channel', $orgChannel)
  125. ->update(['RegPeple'=>DB::raw('RegPeple-1'),'ActivePeple'=>DB::raw('ActivePeple-1')]);
  126. DB::connection('write')->table(TableName::QPRecordDB() . 'RecordPlatformData')
  127. ->where('DateID', date('Ymd'))
  128. ->where('Channel', $newChannel)
  129. ->update(['RegPeple'=>DB::raw('RegPeple+1'),'ActivePeple'=>DB::raw('ActivePeple+1')]);
  130. }
  131. public static function checkClick($UserID)
  132. {
  133. // if(AccountsInfo::where('UserID', $UserID)->value('SpreaderID')!=0){
  134. // return;
  135. // }
  136. $SpreaderID=AgentUserRecord::where('UserID', $UserID)->value('SpreaderID');
  137. $UserSpreaderID=AccountsInfo::where('UserID', $UserID)->value('SpreaderID');
  138. if(intval($UserSpreaderID)){
  139. return;
  140. }
  141. if(intval($SpreaderID)){
  142. if($UserID!=$SpreaderID){
  143. $orgChannel=AccountsInfo::find($UserID)->Channel;
  144. $newChannel=AccountsInfo::find($SpreaderID)->Channel;
  145. $BlockInviteChannel=DB::table('QPPlatformDB.dbo.ChannelPackageName')->where('Channel',$newChannel)->value('BlockInviteChannel');
  146. if($BlockInviteChannel==1){
  147. AccountsInfo::where('UserID', $UserID)->update(['SpreaderID' => $SpreaderID]);
  148. }else {
  149. AccountsInfo::where('UserID', $UserID)->update(['SpreaderID' => $SpreaderID, 'Channel' => $newChannel]);
  150. self::changePlatformData($orgChannel, $newChannel);
  151. }
  152. }
  153. return;
  154. }
  155. $ip=IpLocation::getIP();
  156. $agent=$_SERVER['HTTP_USER_AGENT'];
  157. $clicks = AgentClickLog::where('client_ip', $ip)->where('is_valid', 1)->orderByDesc('id')->get();
  158. if($clicks) {
  159. $validClick = null;
  160. foreach ($clicks as $click) {
  161. if ($click->user_agent == $agent) {
  162. $validClick = $click;
  163. break;
  164. }
  165. }
  166. if (!$validClick) {
  167. foreach ($clicks as $click) {
  168. $validClick =$click;
  169. break;
  170. }
  171. }
  172. if($validClick) {
  173. $agentInfo=(new AgentController())->processReferral($UserID,$validClick->code);
  174. if($agentInfo) {
  175. //不能是自己
  176. if($agentInfo->UserID==$UserID)return;
  177. $click->is_valid = 2;
  178. $click->save();
  179. $orgChannel=AccountsInfo::find($UserID)->Channel;
  180. $newChannel=AccountsInfo::find($agentInfo->UserID)->Channel;
  181. $BlockInviteChannel=DB::table('QPPlatformDB.dbo.ChannelPackageName')->where('Channel',$newChannel)->value('BlockInviteChannel');
  182. if($BlockInviteChannel==1){
  183. AccountsInfo::where('UserID', $UserID)->update(['SpreaderID' => $agentInfo->UserID]);
  184. }else {
  185. AccountsInfo::where('UserID', $UserID)->update(['SpreaderID'=>$agentInfo->UserID,'Channel'=>$newChannel]);
  186. self::changePlatformData($orgChannel,$newChannel);
  187. }
  188. }
  189. }
  190. }
  191. }
  192. /**
  193. * 获取点击统计数据(可添加权限控制)
  194. */
  195. public function getClickStats(Request $request)
  196. {
  197. $userId = $request->UserID;
  198. // 获取用户的邀请码
  199. $agentInfo = AgentUserInfo::where('UserID', $userId)->first();
  200. if (!$agentInfo) {
  201. return apiReturnFail('Agent information not found');
  202. }
  203. $code = $agentInfo->referral_code;
  204. // 统计总点击数
  205. $totalClicks = AgentClickLog::where('code', $code)->count();
  206. // 按设备类型统计
  207. $deviceStats = AgentClickLog::where('code', $code)
  208. ->selectRaw('device_type, COUNT(*) as count')
  209. ->groupBy('device_type')
  210. ->get()
  211. ->pluck('count', 'device_type')
  212. ->toArray();
  213. // 按国家统计
  214. $countryStats = AgentClickLog::where('code', $code)
  215. ->whereNotNull('country')
  216. ->selectRaw('country, COUNT(*) as count')
  217. ->groupBy('country')
  218. ->orderByDesc('count')
  219. ->limit(10)
  220. ->get()
  221. ->toArray();
  222. // 按日期统计
  223. $dailyStats = AgentClickLog::where('code', $code)
  224. ->selectRaw('DATE(created_at) as date, COUNT(*) as count')
  225. ->groupBy('date')
  226. ->orderByDesc('date')
  227. ->limit(15)
  228. ->get()
  229. ->toArray();
  230. return apiReturnSuc([
  231. 'total_clicks' => $totalClicks,
  232. 'device_stats' => $deviceStats,
  233. 'country_stats' => $countryStats,
  234. 'daily_stats' => $dailyStats
  235. ]);
  236. }
  237. }