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.
yanzong/app/api/controller/Goods.php

375 lines
12 KiB

11 months ago
<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\api\controller;
9 months ago
use app\api\model\{Goods as GoodsModel, Store as StoreModel};
7 months ago
use app\api\model\Region;
10 months ago
use app\api\service\{Goods as GoodsService, User as UserService};
8 months ago
use app\common\model\GoodsImage as GoodsImageModel;
use app\common\model\UploadFile as UploadFileModel;
11 months ago
use app\common\service\qrcode\Goods as GoodsPoster;
use cores\exception\BaseException;
8 months ago
use think\db\exception\DbException;
10 months ago
use think\response\Json;
7 months ago
11 months ago
/**
* 商品控制器
* Class Goods
* @package app\api\controller
*/
class Goods extends Controller
{
/**
* 商品列表
* @return Json
8 months ago
* @throws DbException
11 months ago
*/
public function list(): Json
{
// 获取列表数据
$model = new GoodsModel;
10 months ago
$param = $this->request->param();
$list = $model->getList($param);
11 months ago
return $this->renderSuccess(compact('list'));
}
/**
* 获取商品详情(详细信息)
* @param int $goodsId 商品ID
* @param bool $verifyStatus 是否验证商品状态(上架)
* @return Json
* @throws \think\db\exception\DataNotFoundException
8 months ago
* @throws DbException
11 months ago
* @throws \think\db\exception\ModelNotFoundException
* @throws BaseException
*/
public function detail(int $goodsId, bool $verifyStatus = true): Json
{
10 months ago
// wmc商品详情
11 months ago
$model = new GoodsModel;
$goodsInfo = $model->getDetails($goodsId, $verifyStatus);
7 months ago
10 months ago
if (!empty($goodsInfo['content'])) {
$goodsInfo['content'] = str_ireplace('onload="if(this.width>750){this.height=this.height*(750.0/this.width); this.width = 750;}', '', $goodsInfo['content']);
}
11 months ago
return $this->renderSuccess(['detail' => $goodsInfo]);
}
7 months ago
8 months ago
/**
* 获取商品详情(详细信息)
* @param int $goodsId 商品ID
* @param bool $verifyStatus 是否验证商品状态(上架)
* @return Json
* @throws \think\db\exception\DataNotFoundException
* @throws DbException
* @throws \think\db\exception\ModelNotFoundException
* @throws BaseException
*/
public function getGoodsStock(): Json
{
// wmc商品详情
$params = $this->request->param();
8 months ago
if (!$params['list'] || !$params['province'] || !$params['city'] || !$params['district']) {
return $this->renderError('参数错误!');
8 months ago
}
8 months ago
$return = [];
foreach ($params['list'] as $value) {
$res = "无货";
$goods = GoodsModel::where('goods_id', $value['goods_id'])->find();
switch ($goods->channel) {
case 'sn':
7 months ago
$city = Region::withoutGlobalScope()->where('name', $params['city'])->where('level', 2)->find();
$district = Region::withoutGlobalScope()->where('name', $params['district'])->where('level', 3)->find();
8 months ago
$suning = new \app\common\service\Suning();
$goods->num = $param['num'] ?? 1;
$data = $suning->shoppingCartPrice([$goods->toArray()], $city->sn_code, $district->sn_code);
$data['goods_name'] = $goods['goods_name'];
$data['goods_id'] = $goods['goods_id'];
break;
case 'jd':
7 months ago
$province = Region::withoutGlobalScope()->where('name', $params['province'])->where('level', 1)->find();
$city = Region::withoutGlobalScope()->where('name', $params['city'])->where('level', 2)->find();
$district = Region::withoutGlobalScope()->where('name', $params['district'])->where('level', 3)->find();
8 months ago
$jd = new \app\common\service\Jd();
$arr = [];
7 months ago
8 months ago
$ret = $jd->getGoodsStock($province->jd_code, $city->jd_code, $district->jd_code, $goods['goods_no'], $value['num'] ?? 1);
if ($ret) {
$res = $ret['stockResp']['desc'];
if (strpos($res, "无货") !== false) {
$res = "无货";
} elseif (strpos($res, "有货") == false) {
$res = "有货";
}
}
$data = [
7 months ago
'state' => $res,
'goods_name' => $goods['goods_name'],
'goods_id' => $goods['goods_id'],
8 months ago
];
break;
default:
if ($goods->stock_total > ($value['num'] ?? 1)) {
$res = "有货";
}
$data = [
7 months ago
'state' => $res,
'goods_name' => $goods['goods_name'],
'goods_id' => $goods['goods_id'],
8 months ago
];
7 months ago
8 months ago
break;
}
$return[] = $data;
8 months ago
}
7 months ago
8 months ago
return $this->renderSuccess($return);
8 months ago
}
7 months ago
10 months ago
public function browseLog(): Json
10 months ago
{
$model = new GoodsModel;
10 months ago
$goodsInfo = $model->browseLogList();
10 months ago
return $this->renderSuccess($goodsInfo);
}
10 months ago
public function presale()
{
$param = $this->request->param();
$model = new GoodsModel;
$goodsInfo = $model->presale($param);
10 months ago
if ($goodsInfo) {
return $this->renderSuccess('预约成功');
}
return $this->renderError('您已经预约过了');
10 months ago
}
10 months ago
public function browseDel()
{
$param = $this->request->param();
$model = new GoodsModel;
$goodsInfo = $model->browseDel($param);
if ($goodsInfo) {
return $this->renderSuccess('删除成功');
}
return $this->renderError('删除失败');
}
10 months ago
public function presaleList()
{
$param = $this->request->param();
$model = new GoodsModel;
9 months ago
$list = $model->presaleList($param)->toArray();
if (!$list) {
return $this->renderSuccess(compact('list'));
}
foreach ($list['data'] as $key => &$value) {
$goods_image = GoodsImageModel::where('goods_id', $value['goods_id'])->find();
$file_path = UploadFileModel::where('file_id', $goods_image['image_id'])->find();
$value['image'] = getUrl($file_path['file_path'], $file_path['domain']);
}
10 months ago
return $this->renderSuccess(compact('list'));
}
10 months ago
public function presaleCateList()
{
$param = $this->request->param();
$model = new GoodsModel;
$list = $model->presaleCateList($param);
return $this->renderSuccess($list);
}
7 months ago
public function presaleGoodsList(): Json
10 months ago
{
$param = $this->request->param();
$model = new GoodsModel;
7 months ago
$list = $model->presaleGoods($param);
10 months ago
return $this->renderSuccess(compact('list'));
}
10 months ago
public function canlpresale()
{
$param = $this->request->param();
$model = new GoodsModel;
$goodsInfo = $model->canlpresale($param);
if ($goodsInfo) {
return $this->renderSuccess('成功');
}
return $this->renderError('失败');
}
11 months ago
/**
* 获取商品详情(基础信息)
* @param int $goodsId 商品ID
* @param bool $verifyStatus 是否验证商品状态(上架)
* @return Json
* @throws BaseException
*/
public function basic(int $goodsId, bool $verifyStatus = true): Json
{
// 获取商品详情
$model = new GoodsModel;
$detail = $model->getBasic($goodsId, $verifyStatus);
return $this->renderSuccess(compact('detail'));
}
/**
* 获取商品规格数据
* @param int $goodsId
* @return Json
* @throws \think\db\exception\DataNotFoundException
8 months ago
* @throws DbException
11 months ago
* @throws \think\db\exception\ModelNotFoundException
*/
public function specData(int $goodsId): Json
{
// 获取商品详情
$model = new GoodsModel;
$specData = $model->getSpecData($goodsId);
return $this->renderSuccess(compact('specData'));
}
/**
* 获取商品的指定SKU信息
* @param int $goodsId 商品ID
* @param string $goodsSkuId 商品SKU标识
* @return Json
*/
public function skuInfo(int $goodsId, string $goodsSkuId): Json
{
$skuInfo = GoodsService::getSkuInfo($goodsId, $goodsSkuId);
return $this->renderSuccess(compact('skuInfo'));
}
/**
* 推荐的商品列表
* @return Json
* @throws \think\db\exception\DataNotFoundException
8 months ago
* @throws DbException
11 months ago
* @throws \think\db\exception\ModelNotFoundException
*/
public function recommended(): Json
{
$service = new GoodsService;
9 months ago
// $cache_key = "goods_recommended".$this->storeId;
// if(Cache::has($cache_key)) {
// $goodsList = Cache::get($cache_key);
// return $this->renderSuccess(compact('goodsList'));
// }
11 months ago
$goodsList = $service->recommended();
9 months ago
// Cache::set($cache_key, $goodsList, 60*60);
11 months ago
return $this->renderSuccess(compact('goodsList'));
}
9 months ago
/**
* 商品大牌和新品
* @return Json
*/
10 months ago
public function brandList(): Json
{
$service = new GoodsService;
10 months ago
$storeid = request()->header()['storeid'];
$goodsList = $service->brandList($storeid);
10 months ago
return $this->renderSuccess($goodsList);
10 months ago
}
10 months ago
10 months ago
public function getCommonConfig(): Json
{
$service = new GoodsService;
7 months ago
$storeInfo = StoreModel::info();
8 months ago
if (empty($storeInfo)) {
9 months ago
return $this->renderError("商铺信息不存在");
}
7 months ago
$type = intval($this->request->get('type', 1));
$goodsList = $service->getCommonConfig($storeInfo, $type);
10 months ago
return $this->renderSuccess($goodsList);
}
10 months ago
public function charts(): Json
10 months ago
{
$service = new GoodsService;
10 months ago
$storeid = request()->header()['storeid'];
$goodsList = $service->charts($storeid);
10 months ago
return $this->renderSuccess($goodsList);
}
9 months ago
/**
* 排行行查看更多
* @return Json
*/
10 months ago
public function chartsGoodsList(): Json
{
$service = new GoodsService;
10 months ago
$storeid = request()->header()['storeid'];
$goodsList = $service->chartsGoodsList($storeid);
10 months ago
return $this->renderSuccess($goodsList);
}
9 months ago
/**
* 首页分类商品排行榜 默认返回3条
* @return Json
*/
10 months ago
public function chartsGoodsJing(): Json
{
$service = new GoodsService;
10 months ago
$storeid = request()->header()['storeid'];
$goodsList = $service->chartsGoodsJing($storeid);
10 months ago
return $this->renderSuccess($goodsList);
10 months ago
}
10 months ago
10 months ago
public function cityInfo(): Json
{
$service = new GoodsService;
8 months ago
$storeId = $this->request->storeId();
$goodsList = $service->cityInfo($storeId);
10 months ago
return $this->renderSuccess($goodsList);
}
9 months ago
public function hotWord(): Json
{
$service = new GoodsService;
$list = $service->hotWord($this->request->param('page'));
return $this->renderSuccess($list);
}
10 months ago
11 months ago
/**
* 生成商品海报
* @param int $goodsId 商品ID
* @param string $channel 二维码渠道(小程序码、h5码)
* @throws BaseException
* @throws \think\db\exception\DataNotFoundException
8 months ago
* @throws DbException
11 months ago
* @throws \think\db\exception\ModelNotFoundException
*/
public function poster(int $goodsId, string $channel = 'H5'): Json
{
// 获取商品详情
$model = new GoodsModel;
$goodsInfo = $model->setEnableGradeMoney(false)->getBasic($goodsId);
// 生成商品海报图
$userId = UserService::getCurrentLoginUserId();
$Qrcode = new GoodsPoster($goodsInfo, $userId, $channel);
return $this->renderSuccess(['imageUrl' => $Qrcode->getImage()]);
}
9 months ago
11 months ago
}