This commit is contained in:
liu 2024-06-05 14:54:25 +08:00
parent de3d6658d8
commit 5b9c50b689
5 changed files with 176 additions and 0 deletions

View File

@ -0,0 +1,35 @@
<?php
namespace app\api\controller\user;
use app\api\controller\BaseApiController;
use app\api\logic\user\UserVisitLogic;
use app\api\validate\VisitValidate;
use hg\apidoc\annotation as ApiDoc;
#[ApiDoc\title('用户访问')]
class UserVisitController extends BaseApiController
{
#[
ApiDoc\Title('添加访问商品记录'),
ApiDoc\url('/api/user/UserVisit/productLog'),
ApiDoc\Method('POST'),
ApiDoc\Param(name: "product_id", type: "int", require: true, desc: "product_id商品id"),
ApiDoc\Param(name: "cate_id", type: "int", require: true, desc: "分类id"),
ApiDoc\NotHeaders(),
ApiDoc\Header(name: "token", type: "string", require: true, desc: "token"),
ApiDoc\ResponseSuccess("data", type: "array"),
]
public function productLog()
{
$params = (new VisitValidate())->post()->goCheck('add');
$result = UserVisitLogic::add($params,$this->userId);
if (true === $result) {
return $this->success('添加成功', [], 1, 1);
}
return $this->fail(UserVisitLogic::getError());
}
}

View File

@ -0,0 +1,47 @@
<?php
namespace app\api\logic\user;
use app\common\logic\BaseLogic;
use app\common\model\store_visit\StoreVisit;
use app\common\model\user\UserVisit;
use think\facade\Db;
class UserVisitLogic extends BaseLogic
{
/**
* @notes 添加用户访问
* @param array $params
* @return bool
* @author likeadmin
* @date 2024/05/13 16:56
*/
public static function add(array $params,$uid): bool
{
Db::startTrans();
try {
$check = StoreVisit::where(['product_id'=>$params['product_id'],
'uid'=>$uid
])
->whereDay('create_time')
->find();
if($check){
StoreVisit::where('id',$check['id'])->inc('count')->update();
}else{
StoreVisit::create([
'uid' => $uid,
'product_id' => $params['product_id'],
'cate_id' => $params['cate_id'],
'count' => 1,
'create_time' => time(),
]);
}
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
}

View File

@ -0,0 +1,50 @@
<?php
namespace app\api\validate;
use app\common\validate\BaseValidate;
/**
* 访问表验证器
* Class VisitValidate
* @package app\admin\validate\order
*/
class VisitValidate extends BaseValidate
{
/**
* 设置校验规则
* @var string[]
*/
protected $rule = [
'product_id' => 'require|number',
'cate_id' => 'require|number',
];
/**
* 参数描述
* @var string[]
*/
protected $field = [
'product_id' => '商品id',
'cate_id' => '分类',
];
/**
* @notes 添加场景
* @return VisitValidate
* @author likeadmin
* @date 2024/04/24 10:37
*/
public function sceneAdd()
{
return $this->only(['product_id','cate_id']);
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace app\common\model\store_visit;
use app\common\model\BaseModel;
use think\model\concern\SoftDelete;
/**
* 计量单位
* Class StoreProductUnit
* @package app\common\model\store_product_unit
*/
class StoreVisit extends BaseModel
{
use SoftDelete;
protected $name = 'store_visit';
protected $deleteTime = 'delete_time';
}

View File

@ -0,0 +1,22 @@
<?php
namespace app\common\model\user;
use app\common\model\BaseModel;
use think\model\concern\SoftDelete;
/**
* 用户访问表
* Class UserVisit
* @package app\common\model
*/
class UserVisit extends BaseModel
{
use SoftDelete;
protected $pk = 'id';
protected $deleteTime = 'delete_time';
}