You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.2 KiB

<?php
namespace app\mall\logic;
use app\mall\model\MallCollect;
class Collect extends Base
{
/**
* 商品收藏
* @date 2021-03-01
*/
public function collect()
{
$param = input('post.');
$collect_model = new MallCollect();
if (!$param['collect']) { //取消收藏
$res = $collect_model->dataDel([
['uid', '=', UID],
['product_id', '=', $param['product_id']],
['user_id', '=', USER_ID]
]);
if (!$res) {
return sendErrorMessage(4001, '取消收藏失败');
}
$msg = '取消收藏成功';
$collect = false;
} else { //收藏
$data = [
'uid' => UID,
'user_id' => USER_ID,
'user_agent' => USER_AGENT,
'product_id' => $param['product_id']
];
$res = $collect_model->dataUpdate($data);
if (!$res) {
return sendErrorMessage(4002, '收藏失败');
}
$msg = '收藏成功';
$collect = true;
}
return sendSuccessMessage([
'collect' => $collect
], $msg);
}
/**
* 商品是否收藏
* @date 2021-03-01
*/
public function hasCollect()
{
$param = input('post.');
$collect_model = new MallCollect();
//判断是否收藏
$collect_id = $collect_model->getOneData([
['uid', '=', UID],
['product_id', '=', $param['product_id']],
['user_id', '=', USER_ID]
], 'id');
$collect = $collect_id ? true : false;
return sendSuccessMessage([
'collect' => $collect
]);
}
/**
* 我的收藏
* @param string $keyword 关键字
* @date 2022-12-01
*/
public function listMyCollect($keyword)
{
$collect_model = new MallCollect();
$where = [
['user_id', '=', $this->userId],
];
$where_mall_product = [];
if (!empty($keyword)) {
$where_mall_product = [
['name', 'like', '%' . $keyword . '%']
];
}
$field = 'id,product_id';
$my_collect_list = $collect_model->listAllCollect($where, $where_mall_product, $field);
$my_collect_list = empty($my_collect_list) ? [] : $my_collect_list->toArray();
foreach ($my_collect_list as $k => $v) {
$my_collect_list[$k]['is_select'] = 0;
}
return sendSuccessArray([
'my_collect_list' => $my_collect_list
]);
}
/**
* 删除我的收藏
* @param array $collect_ids 需要删除的收藏记录ID
* @date 2022-12-01
*/
public function deleteMyCollect($collect_ids)
{
$collect_model = new MallCollect();
$where = [
['uid', '=', $this->mid],
['user_id', '=', $this->userId],
['id', 'in', $collect_ids]
];
$res = $collect_model->dataDel($where);
if (!$res) {
return sendErrorArray(3001, '取消收藏失败');
}
return sendSuccessArray([], '取消收藏成功');
}
}