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/service/Goods.php

456 lines
17 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\service;
9 months ago
use app\api\model\Category as CategoryModel;
11 months ago
use app\api\model\Goods as GoodsModel;
use app\api\model\GoodsSku as GoodsSkuModel;
9 months ago
use app\api\model\Setting as SettingModel;
10 months ago
use app\api\model\UploadFile;
10 months ago
use app\api\service\User as UserService;
9 months ago
use app\common\enum\Setting as SettingEnum;
10 months ago
use app\common\library\helper;
10 months ago
use app\common\model\goods\GoodsHotWord;
9 months ago
use app\common\model\store\Shop as ShopModel;
10 months ago
use app\common\model\store\shop\Comment;
11 months ago
use app\common\service\Goods as GoodsService;
/**
* 商品服务类
* Class Goods
* @package app\api\service
*/
class Goods extends GoodsService
{
/**
* 获取商品的指定的某个SKU信息
* @param int $goodsId
* @param string $goodsSkuId
* @return GoodsSkuModel|array|null
*/
public static function getSkuInfo(int $goodsId, string $goodsSkuId)
{
$detail = GoodsSkuModel::detail($goodsId, $goodsSkuId);
if (!empty($detail['image'])) {
$detail['goods_image'] = $detail['image']['preview_url'];
}
return $detail;
}
9 months ago
public function getCommonConfig($storeInfo)
11 months ago
{
9 months ago
//todo wmc1新品首发背景图 2限时秒杀背景图 3排行榜背景图 4大牌正品是轮播 5预售规则 6 新人首单
$type = request()->get('type');
switch ($type) {
11 months ago
case 1:
9 months ago
$data['imgurl'] = $this->getStoreImgUrl($storeInfo['new_product_img_id']);
//$data['imgurl'] = 'https://www.saas.njrenzhou.com/uploads/10001/20240131/bdc5f17f3ff8c53ce0853177bc4f1917.png';
11 months ago
return $data;
break;
case 2:
10 months ago
$data['imgurl'] = 'https://www.saas.njrenzhou.com/uploads/10001/20240131/e8e10653875e779bc72dbee65b8072c0.png';
11 months ago
return $data;
break;
case 3:
10 months ago
$data['imgurl'] = 'https://www.saas.njrenzhou.com/uploads/10001/20240131/5c4ec6ffeb23223936295e290dc3d1f9.png';
11 months ago
return $data;
break;
case 4:
9 months ago
$data['imgurl'] = $this->getStoreImgUrl($storeInfo['big_brand_img_id']);
// $data['imgurl'] = [
// 'https://www.saas.njrenzhou.com/uploads/10001/20240131/5c4ec6ffeb23223936295e290dc3d1f9.png',
// ];
11 months ago
return $data;
break;
10 months ago
case 5:
$data['imgurl'] = [
'http://qiniu.shop.royaum.com.cn/10001/20240201/13f2e675794e25176352fc039dfb6dcb.jpg',
];
return $data;
break;
9 months ago
case 6:
$data['imgurl'] = $this->getStoreImgUrl($storeInfo['new_people_order_img_id']);
// $data['imgurl'] = [
// 'http://qiniu.shop.royaum.com.cn/10001/20240201/13f2e675794e25176352fc039dfb6dcb.jpg',
// ];
return $data;
break;
11 months ago
}
11 months ago
return [];
}
9 months ago
/**
* 获取(新品首发,大牌正品,新人首单)商铺的url
*/
9 months ago
public function getStoreImgUrl($img_id)
{
9 months ago
$data = [];
9 months ago
if (!empty($img_id)) {
$img_ids = explode(",", $img_id);
$infos = \app\common\model\UploadFile::where('file_id', 'in', $img_ids)->field('file_path,domain')->select();
if (!empty($infos)) {
foreach ($infos as $info) {
9 months ago
$data[] = getUrl($info['file_path'], $info['domain']);
}
}
}
return $data;
}
11 months ago
/**
* 推荐的商品列表
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function recommended(): array
{
// 获取商品推荐数据
$setting = $this->getRecommendedSetting();
11 months ago
11 months ago
// 获取商品数据
$model = new GoodsModel;
if ($setting['params']['source'] === 'choice') {
// 数据来源:手动
if (empty($setting['params']['goodsIds'])) return [];
$goodsList = $model->getListByIdsFromApi($setting['params']['goodsIds']);
} else {
// 数据来源:自动
$goodsList = $model->getList([
'status' => 10,
'categoryId' => $setting['params']['auto']['category'],
'sortType' => $setting['params']['auto']['goodsSort'],
], $setting['params']['auto']['showNum']);
}
if (empty($goodsList) && $goodsList->isEmpty()) {
return [];
}
return $this->formatGoodsList($goodsList);
}
11 months ago
//大牌正品列表
11 months ago
public function brandList($storeid)
11 months ago
{
$model = new GoodsModel;
11 months ago
$goodsList = $model->getList([
11 months ago
'status' => 10,
11 months ago
'is_delete' => 0,
'is_brand' => $_GET['is_brand'],
'is_new' => $_GET['is_new'],
'is_in_store' => $_GET['is_in_store'] ?? '',//是否店内
11 months ago
'store_id' => $storeid,
11 months ago
'categoryId' => (int)$_GET['categoryId'],
], 15);
11 months ago
11 months ago
if (empty($goodsList) && $goodsList->isEmpty()) {
return [];
}
$list = $goodsList->toArray();
9 months ago
// echo "<pre>";
// print_r($list);
// exit()
$list['data'] = $this->newFormatGoodsList($list['data']);
11 months ago
return $list;
11 months ago
11 months ago
}
11 months ago
//排行榜分类列表 一级
11 months ago
public function charts($storeid): array
11 months ago
{
11 months ago
$model = new CategoryModel;
11 months ago
$list = $model->where([
11 months ago
'parent_id' => 0,
'status' => 1,
'is_paihang' => 1,
11 months ago
'store_id' => $storeid
11 months ago
])->order('category_id desc')->select()->toArray();
11 months ago
return $list;
}
11 months ago
11 months ago
public function chartsGoodsList($storeid)
11 months ago
{
$model = new GoodsModel;
11 months ago
$goodsList = $model->getList([
11 months ago
'status' => 10,
11 months ago
'is_delete' => 0,
'paihang' => 1,
11 months ago
'store_id' => $storeid,
11 months ago
'categoryId' => (int)$_GET['categoryId'],
], 15);
11 months ago
11 months ago
if (empty($goodsList) && $goodsList->isEmpty()) {
return [];
}
$list = $goodsList->toArray();
9 months ago
$list['data'] = $this->newFormatGoodsList($list['data']);
11 months ago
return $list;
11 months ago
11 months ago
}
11 months ago
public function chartsGoodsJing($storeid): array
11 months ago
{
11 months ago
$model = new CategoryModel;
$list = $model->where([
'parent_id' => 0,
'status' => 1,
'is_paihang' => 1,
11 months ago
'store_id' => $storeid
11 months ago
])->order('category_id desc')->select()->toArray();
$model = new GoodsModel;
11 months ago
foreach ($list as &$v) {
11 months ago
$goodsList = $model->getList([
'status' => 10,
'is_delete' => 0,
'paihang' => 1,
11 months ago
'store_id' => $storeid,
11 months ago
'categoryId' => $v['category_id'],
], 3);
$list2 = $goodsList->toArray()['data'];
9 months ago
$v['goods_list'] = $this->newFormatGoodsList($list2);
11 months ago
}
11 months ago
11 months ago
return $list;
}
11 months ago
//同城信息
11 months ago
public function cityInfo($storeid): array
11 months ago
{
$model = new ShopModel;
10 months ago
$info = $model->with(['wechatImage'])->where([
11 months ago
'is_delete' => 0,
'status' => 1,
11 months ago
'store_id' => $storeid,
11 months ago
'is_main' => 1,
11 months ago
])->field('shop_name,star,shop_hours,summary,phone,
9 months ago
province_id,city_id,region_id,shop_id,address,latitude,longitude,wechat_img_id,history1,history2,history3,remark,history,parking_name,parking_desc,parking_latitude,parking_longitude,shop_image_id,fuwu_hours,fuwu_num')->find();
if (!$info) {
return [];
}
$info = $info->toArray();
9 months ago
$info['remark'] = json_decode($info['remark'], true);
9 months ago
$info['is_open'] = 0;
9 months ago
9 months ago
$info['history'] = json_decode($info['history'], true);
10 months ago
$info['evaluate'] = Comment::with('file')
->field(['nickname,content,image_id'])
->hidden(['file'])
->where('is_delete', '=', 0)
9 months ago
->where('shop_id', '=', $info['shop_id'])
10 months ago
->select();
$file = UploadFile::whereIn('file_id', explode(',', $info['shop_image_id']))->select();
$info['imageList'] = helper::getArrayColumn($file, 'preview_url');
9 months ago
$info['service_num_for_me'] = 0;
//当前用户门店自提订单数
$userId = UserService::getCurrentLoginUserId(false);
if ($userId) {
$info['service_num_for_me'] = (new \app\api\model\Order())->where(['user_id' => $userId, 'extract_shop_id' => $info['shop_id']])->count();
}
11 months ago
return $info;
11 months ago
}
10 months ago
public function hotWord($page): array
{
9 months ago
if (empty($page) || $page == 1) {
10 months ago
$offset = 0;
9 months ago
} else {
10 months ago
$offset = $page * 5;
}
9 months ago
return GoodsHotWord::order('sort desc')->limit($offset, 5)->select()->toArray();
10 months ago
}
11 months ago
// //抢购会场列表
// public function panicList()
// {
// //只拉第一个活动
// $model = new SharpActiveModel;
//
// $info = $model->alias('a')
// ->where([
// 'a.is_delete' => 0,
// 'a.status' => 1,
// 'a.store_id' => $_GET['store_id'],
//// 'sharpActiveTime.status' => 1,
// ])->order(['a.active_id' => 'desc'])->find()->toArray();
// if (empty($info)) {
// return [];
// }
//
// $info['create_date'] = date('Y-m-d H:i:s', $info['active_date']);
// $aid = $info['active_id'];
// $time = ActiveTimeModel::where([
// 'status' => 1,
// 'active_id' => $aid,
// ])->order('active_time asc')->select()->toArray();
// if (empty($time)) {
// return [];
// }
//
// // 获取秒杀活动会场首页数据
// $service = new ActiveService;
//
//// $miaosha = $service->getGoodsListByActiveTimeId((int)$time[0]['active_time_id'])->toArray()['data'] ?? [];
//
// $info['create_time'] = date('Y-m-d', $info['active_date']) . ' ' . $time[0]['active_time'] . ':00';
// $info['lats_time'] = strtotime($info['create_time']) - time();
// $list['info'] = $info;
// $list['detail'] = $time;
//
// return $list;
// }
9 months ago
public static function getGoodsPrice($goods_id, $price, $cost_price)
{
9 months ago
$catService = new \app\store\model\GoodsCategoryRel();
$catIds = $catService->where(['goods_id' => $goods_id])->column('category_id');
if (UserService::isstore()) {
$price = $cost_price;
} elseif (UserService::isPlusMember()) {
$price = \app\common\model\PriceSet::membershipPrice($price, $cost_price, $catIds);
} elseif (UserService::isDealerMember()) {
$priceArr = \app\common\model\PriceSet::distributionPrice($price, $cost_price, $catIds);
9 months ago
$price = $priceArr['distributionPrice'];
9 months ago
}
return $price;
}
9 months ago
9 months ago
/**
* 格式化商品列表
* @param $goodsList
* @return array
*/
private function newFormatGoodsList($goodsList): array
{
9 months ago
9 months ago
$data = [];
foreach ($goodsList as $goods) {
$temp = [
'goods_id' => $goods['goods_id'],
'goods_name' => $goods['goods_name'],
'selling_point' => $goods['selling_point'],
'goods_image' => $goods['goods_image'],
'goods_price_min' => $goods['goods_price_min'],//商品价格
'goods_price_max' => $goods['goods_price_max'],
'line_price_min' => $goods['goods_price_min'],//划线价格等于市场价
'line_price_max' => $goods['line_price_max'],
'goods_sales' => $goods['goods_sales'],
'remaizhishu' => $goods['remaizhishu'],
];
9 months ago
9 months ago
if (UserService::isLogin()) {
$catService = new \app\store\model\GoodsCategoryRel();
$catIds = $catService->where(['goods_id' => $goods['goods_id']])->column('category_id');
//价格判断
if (UserService::isstore()) {
$temp['goods_price_min'] = $goods['cost_price_min'];
} elseif (UserService::isPlusMember()) {
9 months ago
$temp['goods_price_min'] = \app\common\model\PriceSet::membershipPrice($goods['goods_price_min'], $goods['cost_price_min'], $catIds);
9 months ago
} elseif (UserService::isDealerMember()) {
$priceArr = \app\common\model\PriceSet::distributionPrice($goods['goods_price_min'], $goods['cost_price_min'], $catIds);
9 months ago
$temp['goods_price_min'] = $priceArr['distributionPrice'];
9 months ago
}
}
$data[] = $temp;
}
return $data;
}
9 months ago
11 months ago
/**
* 格式化商品列表
* @param $goodsList
* @return array
*/
private function formatGoodsList($goodsList): array
{
9 months ago
// echo "<pre>";
// print_r($goodsList->toArray());
// exit();
// $data = [];
// foreach ($goodsList as $goods) {
// $temp = [
// 'goods_id' => $goods['goods_id'],
// 'goods_name' => $goods['goods_name'],
// 'selling_point' => $goods['selling_point'],
// 'goods_image' => $goods['goods_image'],
// 'goods_price_min' => $goods['goods_price_min'],//商品价格
// 'goods_price_max' => $goods['goods_price_max'],
// 'line_price_min' => $goods['goods_price_min'],//划线价格等于市场价
// 'line_price_max' => $goods['line_price_max'],
// 'goods_sales' => $goods['goods_sales'],
// 'remaizhishu' => $goods['remaizhishu'],
// ];
// var_dump($goods->toArray());
// var_dump($temp);
// exit();
// if (UserService::isLogin()) {
// $catService = new \app\store\model\GoodsCategoryRel();
// $catIds = $catService->where(['goods_id' => $goods['goods_id']])->column('category_id');
// // $price_list_plus = $price_list_dealer = [];
// // foreach ($catIds as $k => $v) {
// // $price_list_plus[] = GoodsPriceModel::getDiscountPrice($v, 1, $goods['goods_price_min']);
// // $price_list_dealer[] = GoodsPriceModel::getDiscountPrice($v, 2, $goods['goods_price_min']);
// // }
9 months ago
9 months ago
// //价格判断
// if (UserService::isstore()) {
9 months ago
9 months ago
// //$priceArr = \app\common\model\PriceSet::distributionPrice($goods['goods_price_min'], $goods['cost_price_min'], $catIds);
// // $temp['goods_price_min_plus'] = $priceArr['membershipPrice'];
// // $temp['goods_price_min_dealer'] = $priceArr['distributionPrice'];
// $temp['goods_price_min'] = $goods['cost_price_min'];
// // $temp['goods_price_min_plus'] = min($price_list_plus);
// // $temp['goods_price_min_dealer'] = min($price_list_dealer);
// } elseif (UserService::isPlusMember()) {
// //$temp['goods_price_min_plus'] = \app\common\model\PriceSet::membershipPrice($goods['goods_price_min'], $goods['cost_price_min'], $catIds);
// $temp['goods_price_min'] = \app\common\model\PriceSet::membershipPrice($goods['goods_price_min'], $goods['cost_price_min'], $catIds);
// // $temp['goods_price_min'] = $temp['goods_price_min_plus'];
// // $temp['goods_price_max'] = $temp['goods_price_min_plus'];
// //$temp['goods_price_min_plus'] = min($price_list_plus);
// } elseif (UserService::isDealerMember()) {
// $priceArr = \app\common\model\PriceSet::distributionPrice($goods['goods_price_min'], $goods['cost_price_min'], $catIds);
// //$temp['goods_price_min_dealer'] = $priceArr['distributionPrice'];
// $temp['goods_price_min'] = $priceArr['goods_price_min_dealer'];
// //$temp['goods_price_min_dealer'] = min($price_list_dealer);
// }
// }
// $data[] = $temp;
// }
return is_array($goodsList) ? $goodsList : $goodsList->toArray();
11 months ago
}
11 months ago
11 months ago
/**
* 获取商品推荐设置
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
private function getRecommendedSetting(): array
{
return SettingModel::getItem(SettingEnum::RECOMMENDED);
}
11 months ago
}