Files
amb_wechatapp/application/api/controller/data/Record.php
T

147 lines
3.7 KiB
PHP

<?php
namespace app\api\controller\data;
use app\common\controller\Api;
use Jenssegers\Agent\Agent;
use think\Cache;
/**
* 商品列表
* 区别于课程列表,这里展示的是处理课程外的所有商品,如会员卡、活动
*/
class Record extends Api
{
// 无需登录的接口,*表示全部
protected $noNeedLogin = ['*'];
protected $noNeedRight = '*';
public function _initialize()
{
parent::_initialize();
}
public function log(){
$url = input('url');
if(!$url){
$this->success("1");
}
// 过滤掉登录页和外部浏览器页面的记录
if(strpos($url, "login") !== false || strpos($url, "****") !== false){
$this->success("1");
}
$pageInfo = $this->getPageInfo($url);
$agent = new Agent();
$data = [
'uniacid' => UNIACID,
'ip' => $_SERVER['REMOTE_ADDR'],
'url' => $url,
'user_id' => 0,
'platform' => \app\common\library\Platform::getPlatform(),
'is_desktop' => $agent->isDesktop() ? 1 : 0,
'is_mobile' => $agent->isMobile() ? 1 : 0,
'browser' => $agent->browser(),
'is_android' => $agent->isAndroidOS() ? 1 : 0,
'is_ios' => $agent->isIOS() ? 1 : 0,
'device' => $agent->device(),
'page_type' => $pageInfo['type'],
'page_id' => $pageInfo['id'],
'createtime' => time()
];
if($this->auth->isLogin()){
$data['user_id'] = $this->auth->id;
}
try{
$cacheKey = \app\common\constant\cache\Keys::RECORD_LOG;
// 从缓存中获取当前队列
$queue = Cache::get($cacheKey) ?: [];
// 将新日志数据加入队列(从数组尾部添加)
$queue[] = $data;
// 将更新后的队列写回缓存
Cache::set($cacheKey, $queue);
}catch (\Exception $e){
}
$this->success("1");
}
/**
* 获取页面信息
* @param $url
* @return array
*/
public function getPageInfo($url){
$urlInfo = $this->parseUrl($url);
$data = [
'type'=>'other',
'id'=>0
];
if(!isset($urlInfo['url'])){
return $data;
}
switch ($urlInfo['url']){
case 'pages/app/exam/detail/detail':
$data['type'] = 'exercise';
break;
case 'pages/app/activity/detail/detail':
$data['type'] = 'activity';
break;
case 'pages/course/live/live':
case 'pages/course/detail/detail':
$data['type'] = 'course';
break;
case 'pages/app/vip/center/center':
$data['type'] = 'vipcard';
break;
}
if(isset($urlInfo['params']['id'])){
$data['id'] = $urlInfo['params']['id'];
}
return $data;
}
/**
* 解析路由
* @return void
*/
public function parseUrl($url){
// 删除URL前的'/'或'#/'前缀
$url = preg_replace('/^\/?#?\//', '', $url);
// 解析URL
$parts = parse_url($url);
// 提取路径部分
$result = [];
if (isset($parts['path'])) {
$result['url'] = $parts['path'];
}
// 解析查询字符串
$params = [];
if (isset($parts['query'])) {
parse_str($parts['query'], $params);
}
// 将参数数组添加到结果中
$result['params'] = $params;
return $result;
}
}