pull/1/head
wmc 12 months ago
parent eaa994cc7f
commit 7aa09d8e4f
  1. 19
      app/api/controller/Goods.php
  2. 72
      app/api/model/Goods.php
  3. 7
      app/api/model/GoodsBrowseLog.php

@ -62,6 +62,25 @@ class Goods extends Controller
return $this->renderSuccess(['detail' => $goodsInfo]);
}
public function browseLog()
{
$param = $this->request->param();
$model = new GoodsModel;
$goodsInfo = $model->browseLog($param);
return $this->renderSuccess($goodsInfo);
}
public function browseDel()
{
$param = $this->request->param();
$model = new GoodsModel;
$goodsInfo = $model->browseDel($param);
if ($goodsInfo) {
return $this->renderSuccess('删除成功');
}
return $this->renderError('删除失败');
}
/**
* 获取商品详情(基础信息)
* @param int $goodsId 商品ID

@ -12,7 +12,9 @@ declare (strict_types=1);
namespace app\api\model;
use app\api\model\GoodsBrowseLog;
use app\api\service\Goods as GoodsService;
use app\api\service\User as UserService;
use app\api\service\user\Grade as UserGradeService;
use app\api\model\GoodsSku as GoodsSkuModel;
use app\api\model\store\Module as StoreModuleModel;
@ -113,6 +115,49 @@ class Goods extends GoodsModel
return $this->setGoodsListDataFromApi($list);
}
public function browseLog()
{
$userId = UserService::getCurrentLoginUserId(true);
$list = GoodsBrowseLog::where([
'store_id' => request()->header()['storeid'],
'user_id' => $userId,
])->order('id desc')->paginate(15)->column('ctime');
$list = array_unique($list);
$res = [];
foreach ($list as $k => $v) {
$all = GoodsBrowseLog::alias('a')
->join('goods b', 'a.goods_id = b.goods_id')
->join('goods_sku c', 'a.goods_id = c.goods_id')
->where([
'a.store_id' => request()->header()['storeid'],
'a.user_id' => $userId,
'a.ctime' => $v,
'b.is_delete' => 0,
'b.status' => 10
])->field('a.ctime,a.goods_id,c.goods_props')->order('a.id desc')->select()->toArray();
foreach ($all as $k2 => &$v2) {
$v2['goods_props'] = \Qiniu\json_decode($v2['goods_props'], 1)[0] ?? '';
$v2['image'] = $this->getDetails2($v2['goods_id'])->toArray()['goods_images'][0]->toArray()['preview_url'] ?? '';
}
$res[$v] = $all;
}
return $res;
}
public function browseDel()
{
$userId = UserService::getCurrentLoginUserId(true);
$info = GoodsBrowseLog::where([
'store_id' => request()->header()['storeid'],
'user_id' => $userId,
])->whereIn('id', $_POST['ids'])->delete();
return $info;
}
/**
* 获取商品详情 (详细数据用于页面展示)
* @param int $goodsId 商品ID
@ -162,10 +207,37 @@ class Goods extends GoodsModel
$goodsInfo->specifications = $newList;
}
//加入足迹
$userId = UserService::getCurrentLoginUserId(false) ?? '';
if ($userId) {
$info = GoodsBrowseLog::where([
'user_id' => $userId,
'goods_id' => $goodsId,
'store_id' => request()->header()['storeid'],
'ctime' => date('Y-m-d')
])->find();
if (!$info) {
GoodsBrowseLog::insert([
'user_id' => $userId,
'goods_id' => $goodsId,
'store_id' => request()->header()['storeid'],
'ctime' => date('Y-m-d')
]);
}
}
return $goodsInfo->hidden(static::getHidden(['images']));
}
public function getDetails2(int $goodsId, bool $verifyStatus = true)
{
// 关联查询(商品图片、sku列表)
$with = ['images.file'];
// 获取商品记录
$goodsInfo = $this->getGoodsMain($goodsId, $with, $verifyStatus);
return $goodsInfo;
}
/**
* 获取商品详情 (仅包含主商品信息和商品图片)
* @param int $goodsId 商品ID

@ -21,4 +21,11 @@ use app\common\model\GoodsSpecRel as GoodsSpecRelModel;
*/
class GoodsBrowseLog extends GoodsSpecRelModel
{
// 定义表名
protected $name = 'goods_browse_log';
// 定义主键
protected $pk = 'id';
protected $updateTime = false;
}

Loading…
Cancel
Save