访问
This commit is contained in:
parent
de3d6658d8
commit
5b9c50b689
35
app/api/controller/user/UserVisitController.php
Normal file
35
app/api/controller/user/UserVisitController.php
Normal 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());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
47
app/api/logic/user/UserVisitLogic.php
Normal file
47
app/api/logic/user/UserVisitLogic.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
50
app/api/validate/VisitValidate.php
Normal file
50
app/api/validate/VisitValidate.php
Normal 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']);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
22
app/common/model/store_visit/StoreVisit.php
Normal file
22
app/common/model/store_visit/StoreVisit.php
Normal 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';
|
||||||
|
|
||||||
|
|
||||||
|
}
|
22
app/common/model/user/UserVisit.php
Normal file
22
app/common/model/user/UserVisit.php
Normal 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';
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user