用户访问路径时间接口

This commit is contained in:
liu 2024-06-05 15:42:20 +08:00
parent 337873c199
commit 04c9f7b580
3 changed files with 78 additions and 1 deletions
app/api

@ -6,6 +6,10 @@ use app\api\controller\BaseApiController;
use app\api\logic\user\UserVisitLogic;
use app\api\validate\VisitValidate;
use hg\apidoc\annotation as ApiDoc;
use Illuminate\Support\Facades\Request;
use support\Response;
#[ApiDoc\title('用户访问')]
class UserVisitController extends BaseApiController
{
@ -20,7 +24,7 @@ class UserVisitController extends BaseApiController
ApiDoc\Header(name: "token", type: "string", require: true, desc: "token"),
ApiDoc\ResponseSuccess("data", type: "array"),
]
public function productLog()
public function productLog(): Response
{
$params = (new VisitValidate())->post()->goCheck('add');
@ -32,4 +36,30 @@ class UserVisitController extends BaseApiController
}
#[
ApiDoc\Title('用户访问页面记录'),
ApiDoc\url('/api/user/UserVisit/htmlLog'),
ApiDoc\Method('POST'),
ApiDoc\Param(name: "url", type: "string", require: true, desc: "路径"),
ApiDoc\Param(name: "stay_time", type: "int", require: true, desc: "停留时间"),
ApiDoc\NotHeaders(),
ApiDoc\Header(name: "token", type: "string", require: true, desc: "token"),
ApiDoc\ResponseSuccess("data", type: "array"),
]
public function htmlLog()
{
$ip = $this->request->getRealIp();
$params = (new VisitValidate())->post()->goCheck('userAdd');
$params['ip'] = $ip;
$result = UserVisitLogic::visitAdd($params,$this->userInfo);
if (true === $result) {
return $this->success('添加成功', [], 1, 1);
}
return $this->fail(UserVisitLogic::getError());
}
}

@ -44,4 +44,41 @@
return false;
}
}
public static function visitAdd(array $params,$userInfo): bool
{
Db::startTrans();
try {
$check = UserVisit::where(['url'=>$params['url'],
'uid'=>$userInfo['user_id']
])
->whereDay('create_time')
->find();
if($check){
UserVisit::where('id',$check['id'])->inc('stay_time',$params['stay_time'])->update();
}else{
UserVisit::create([
'uid' => $userInfo['user_id'],
'url' => $params['url'],
'channel_type' =>$userInfo['terminal'],
'ip' => $params['ip'],
'stay_time' => $params['stay_time'],
'create_time' => time(),
]);
}
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
}

@ -21,6 +21,8 @@ class VisitValidate extends BaseValidate
protected $rule = [
'product_id' => 'require|number',
'cate_id' => 'require|number',
'url' => 'require',
'stay_time' => 'require|number',
];
@ -31,6 +33,8 @@ class VisitValidate extends BaseValidate
protected $field = [
'product_id' => '商品id',
'cate_id' => '分类',
'url' => '路径',
'stay_time' => '页面停留时间(秒)',
];
@ -46,5 +50,11 @@ class VisitValidate extends BaseValidate
}
public function sceneUserAdd()
{
return $this->only(['url','stay_time']);
}
}