問題說明:商品采集參數未保存
修改文件:app\services\product\product\StoreProductServices
修改方法:save()
public function save(int $id, array $data, int $type = 0, int $relation_id = 0)
{
if (count($data['cate_id']) < 1) throw new AdminException('請選擇商品分類');
if ($type == 1 && $relation_id) {
/** @var SystemStoreServices $storeServices */
$storeServices = app()->make(SystemStoreServices::class);
$storeInfo = $storeServices->getStoreInfo($relation_id);
//有自建商品分類權限門店
if ($storeInfo['product_category_status'] && isset($data['store_cate_id']) && count($data['store_cate_id']) < 1) {
throw new AdminException('請選擇門店商品分類');
}
}
if (!$data['store_name']) throw new AdminException('請輸入商品名稱');
if (count($data['slider_image']) < 1) throw new AdminException('請上傳商品輪播圖');
if ($data['product_type'] == 0 && isset($data['delivery_type']) && count($data['delivery_type']) < 1) throw new AdminException('請選擇商品配送方式');
if (in_array($data['product_type'], [1, 2, 3, 4])) {//除普通商品外,都免運費
$data['freight'] = 1;
$data['temp_id'] = 0;
$data['postage'] = 0;
} else {
if ($data['freight'] == 1) {
$data['temp_id'] = 0;
$data['postage'] = 0;
} elseif ($data['freight'] == 2) {
$data['temp_id'] = 0;
} elseif ($data['freight'] == 3) {
$data['postage'] = 0;
}
if ($data['freight'] == 2 && !$data['postage']) {
throw new AdminException('請設置運費金額');
}
if ($data['freight'] == 3 && !$data['temp_id']) {
throw new AdminException('請選擇運費模版');
}
}
////供應商商品僅平臺
if (isset($data['type']) && $data['type'] == 2) {
$data['delivery_type'] = [1];
$data['applicable_type'] = 0;
}
// 開啟ERP后商品編碼驗證
$isOpen = sys_config('erp_open');
if ($isOpen && $data['product_type'] == 0 && empty($data['code'])) {
throw new AdminException('請輸入商品編碼');
}
$detail = $data['attrs'];
$attr = $data['items'];
$coupon_ids = $data['coupon_ids'];
//關聯補充信息
$relationData = [];
$relationData['cate_id'] = $data['cate_id'] ?? [];
if (isset($data['store_cate_id'])) {
$relationData['store_cate_id'] = $data['store_cate_id'] ?? [];
}
$relationData['brand_id'] = $data['brand_id'] ?? [];
$relationData['store_label_id'] = $data['store_label_id'] ?? [];
$relationData['label_id'] = $data['label_id'] ?? [];
$relationData['ensure_id'] = $data['ensure_id'] ?? [];
$relationData['specs_id'] = $data['specs_id'] ?? [];
$relationData['coupon_ids'] = $data['coupon_ids'] ?? [];
$description = $data['description'];
$data['type'] = $type;
$data['relation_id'] = $relation_id;
$supplier_id = $data['supplier_id'] ?? 0;
if ($supplier_id) {
$data['type'] = 2;
$data['relation_id'] = $supplier_id;
}
if ($data['type'] == 0) {//平臺商品不需要審核
$data['is_verify'] = 1;
}
$is_copy = $data['is_copy'];
unset($data['supplier_id'], $data['is_copy']);
//視頻
if ($data['video_link'] && strpos($data['video_link'], 'http') === false) {
$data['video_link'] = sys_config('site_url') . $data['video_link'];
}
//品牌
$data['brand_com'] = $data['brand_id'] ? implode(',', $data['brand_id']) : '';
$data['brand_id'] = $data['brand_id'] ? end($data['brand_id']) : 0;
$data['is_vip'] = $type == 0 && in_array(0, $data['is_sub']) ? 1 : 0;
$data['is_sub'] = in_array(1, $data['is_sub']) ? 1 : 0;
$data['product_type'] = intval($data['product_type']);
$data['is_vip_product'] = intval($type == 0 && $data['is_vip_product']);
if($type == 0) {
$data['is_presale_product'] = intval($data['is_presale_product']);
$data['presale_start_time'] = $data['is_presale_product'] ? strtotime($data['presale_time'][0]) : 0;
$data['presale_end_time'] = $data['is_presale_product'] ? strtotime($data['presale_time'][1]) : 0;
if ($data['presale_start_time'] && $data['presale_start_time'] < time()) {
throw new AdminException('預售開始時間不能小于當前時間');
}
if ($data['presale_end_time'] && $data['presale_end_time'] < time()) {
throw new AdminException('預售結束時間不能小于當前時間');
}
}
$data['auto_on_time'] = $data['auto_on_time'] ? strtotime($data['auto_on_time']) : 0;
$data['auto_off_time'] = $data['auto_off_time'] ? strtotime($data['auto_off_time']) : 0;
if ($data['auto_on_time']) {
$data['is_show'] = 0;
}
$data['is_limit'] = intval($data['is_limit']);
if (!$data['is_limit']) {
$data['limit_type'] = 0;
$data['limit_num'] = 0;
} else {
if (!in_array($data['limit_type'], [1, 2])) throw new AdminException('請選擇限購類型');
if ($data['limit_num'] <= 0) throw new AdminException('限購數量不能小于1');
}
$data['custom_form'] = json_encode($data['custom_form']);
$storeLabelId = $data['store_label_id'];
if ($data['store_label_id']) {
$data['store_label_id'] = is_array($data['store_label_id']) ? implode(',', $data['store_label_id']) : $data['store_label_id'];
} else {
$data['store_label_id'] = '';
}
if ($data['ensure_id']) {
$data['ensure_id'] = is_array($data['ensure_id']) ? implode(',', $data['ensure_id']) : $data['ensure_id'];
} else {
$data['ensure_id'] = '';
}
if (!$data['specs_id'] && !$data['specs']) {
$data['specs'] = '';
}
if ($data['specs']) {
$specs = [];
if (is_array($data['specs'])) {
/** @var StoreProductSpecsServices $storeProductSpecsServices */
$storeProductSpecsServices = app()->make(StoreProductSpecsServices::class);
foreach ($data['specs'] as $item) {
$specs[] = $storeProductSpecsServices->checkSpecsData($item);
}
$data['specs'] = json_encode($specs);
}
} else {
$data['specs'] = '';
}
if ($data['spec_type'] == 0) {
$attr = [
[
'value' => '規格',
'detailValue' => '',
'attrHidden' => '',
'detail' => ['默認']
]
];
$detail[0]['value1'] = '默認';
$detail[0]['detail'] = ['規格' => '默認'];
}
foreach ($detail as &$item) {
if ($isOpen && $data['product_type'] == 0 && (!isset($item['code']) || !$item['code'])) {
throw new AdminException('請輸入【' . ($item['values'] ?? '默認') . '】商品編碼');
}
if ($type == 2 && !$item['settle_price']) {
throw new AdminException('請輸入結算價');
}
if (isset($item['price']) && $item['price'] > 0 && $type == 2 && $item['settle_price'] > $item['price']) {
throw new AdminException('結算價不能超過商品售價');
}
$item['product_type'] = $data['product_type'];
if ($data['is_sub'] == 0) {
$item['brokerage'] = 0;
$item['brokerage_two'] = 0;
}
if (!isset($item['price'])) $item['price'] = 0;
if (!isset($item['ot_price'])) $item['ot_price'] = 0;
if (!isset($item['settle_price'])) $item['settle_price'] = 0;
if (($item['brokerage'] + $item['brokerage_two']) > $item['price']) {
throw new AdminException('一二級返傭相加不能大于商品售價');
}
//驗證次卡商品數據
if ($data['product_type'] == 4) {
if (!isset($item['write_times']) || !$item['write_times']) {
throw new AdminException('請輸入核銷次數');
}
if (!isset($item['write_valid'])) {
throw new AdminException('請選擇核銷時效類型');
}
switch ($item['write_valid']) {
case 1://永久
$item['days'] = 0;
$item['section_time'] = [0, 0];
break;
case 2://購買后n天
$item['section_time'] = [0, 0];
if (!isset($item['days']) || !$item['days']) {
throw new AdminException('填寫時效天數');
}
break;
case 3://固定時間
$item['days'] = 0;
if (!isset($item['section_time']) || !$item['section_time'] || !is_array($item['section_time']) || count($item['section_time']) != 2) {
throw new AdminException('請選擇固定有效時間段或時間格式錯誤');
}
[$start, $end] = $item['section_time'];
$data['start_time'] = $start ? strtotime($start) : 0;
$data['end_time'] = $end ? strtotime($end) : 0;
if ($data['start_time'] && $data['end_time'] && $data['end_time'] <= $data['start_time']) {
throw new AdminException('請重新選擇:結束時間必須大于開始時間');
}
break;
default:
throw new AdminException('請選擇核銷時效類型');
break;
}
}
}
foreach ($data['activity'] as $k => $v) {
if ($v == '秒殺') {
$data['activity'][$k] = 1;
} elseif ($v == '砍價') {
$data['activity'][$k] = 2;
} elseif ($v == '拼團') {
$data['activity'][$k] = 3;
} else {
$data['activity'][$k] = 0;
}
}
$data['activity'] = implode(',', $data['activity']);
$data['recommend_list'] = count($data['recommend_list']) ? implode(',', $data['recommend_list']) : '';
$data['price'] = min(array_column($detail, 'price'));
$data['settle_price'] = min(array_column($detail, 'settle_price'));
$data['ot_price'] = min(array_column($detail, 'ot_price'));
$data['cost'] = min(array_column($detail, 'cost'));
if (!$data['cost']) {
$data['cost'] = 0;
}
$data['cate_id'] = implode(',', $data['cate_id']);
if (isset($data['store_cate_id'])) {
$data['store_cate_id'] = implode(',', $data['store_cate_id']);
}
$data['label_id'] = implode(',', $data['label_id']);
$data['image'] = $data['slider_image'][0];//封面圖
$slider_image = $data['slider_image'];
$data['slider_image'] = json_encode($data['slider_image']);
$data['stock'] = array_sum(array_column($detail, 'stock'));
$stock = min(array_column($detail, 'stock'));
//是否售罄
$data['is_sold'] = $stock ? 0 : 1;
unset($data['description'], $data['coupon_ids'], $data['items'], $data['attrs'], $data['recommend']);
/** @var StoreDescriptionServices $storeDescriptionServices */
$storeDescriptionServices = app()->make(StoreDescriptionServices::class);
/** @var StoreProductAttrServices $storeProductAttrServices */
$storeProductAttrServices = app()->make(StoreProductAttrServices::class);
/** @var StoreProductCouponServices $storeProductCouponServices */
$storeProductCouponServices = app()->make(StoreProductCouponServices::class);
/** @var StoreDiscountsProductsServices $storeDiscountProduct */
$storeDiscountProduct = app()->make(StoreDiscountsProductsServices::class);
/** @var StoreProductVirtualServices $productVirtual */
$productVirtual = app()->make(StoreProductVirtualServices::class);
//同一鏈接不多次保存
if (!$id && $data['soure_link']) {
$productInfo = $this->dao->getOne(['soure_link' => $data['soure_link'], 'is_del' => 0], 'id');
if ($productInfo) $id = (int)$productInfo['id'];
}
[$skuList, $id, $is_new, $data] = $this->transaction(function () use ($id, $data, $description, $storeDescriptionServices, $storeProductAttrServices, $storeProductCouponServices, $detail, $attr, $coupon_ids, $storeDiscountProduct, $productVirtual, $slider_image) {
if ($id) {
//上下架處理
$this->setShow([$id], $data['is_show']);
$oldInfo = $this->get($id)->toArray();
if ($oldInfo['product_type'] != $data['product_type']) {
throw new AdminException('商品類型不能切換!');
}
//修改不改變商品來源
if ($oldInfo['type']) {
$data['type'] = $oldInfo['type'];
$data['relation_id'] = $oldInfo['relation_id'];
}
unset($data['sales']);
$res = $this->dao->update($id, $data);
if (!$res) throw new AdminException('修改失敗');
// 修改優惠套餐商品的運費模版id
$storeDiscountProduct->update(['product_id' => $id], ['temp_id' => $data['temp_id']]);
if (!empty($coupon_ids)) {
$storeProductCouponServices->setCoupon($id, $coupon_ids);
} else {
$storeProductCouponServices->delete(['product_id' => $id]);
}
if ($oldInfo['type'] == 1 && !$oldInfo['pid'] && $data['is_verify'] < 1) {
/** @var StoreCartServices $cartService */
$cartService = app()->make(StoreCartServices::class);
$cartService->batchUpdate([$id], ['status' => 0], 'product_id');
}
$is_new = 1;
} else {
//默認評分
$data['star'] = config('admin.product_default_star');
$data['add_time'] = time();
$data['code_path'] = '';
$data['spu'] = $this->createSpu();
$res = $this->dao->save($data);
if (!$res) throw new AdminException('添加失敗');
$id = (int)$res->id;
if (!empty($coupon_ids)) $storeProductCouponServices->setCoupon($id, $coupon_ids);
$is_new = 0;
}
//商品詳情
$storeDescriptionServices->saveDescription($id, $description);
$skuList = $storeProductAttrServices->validateProductAttr($attr, $detail, $id, 0, (int)$data['is_vip']);
foreach ($skuList['valueGroup'] as &$item) {
if (!isset($item['sum_stock']) || !$item['sum_stock']) $item['sum_stock'] = $item['stock'] ?? 0;
}
$proudctVipPrice = 0;
$detailTemp = array_column($skuList['valueGroup'], 'vip_price');
if ($detailTemp) {
$proudctVipPrice = min($detailTemp);
}
$this->dao->update($id, ['vip_price' => $proudctVipPrice]);
$valueGroup = $storeProductAttrServices->saveProductAttr($skuList, $id);
if (!$valueGroup) throw new AdminException('添加失??!');
if ($data['product_type'] == 1) {
$productVirtual->saveProductVirtual($id, $valueGroup);
}
return [$skuList, $id, $is_new, $data];
});
event('product.create', [$id, $data, $skuList, $is_new, $slider_image, $description, $is_copy, $relationData]);
}
后臺商品詳情頁:
src/pages/product/productAdd/index.vue,找到這個文件,搜索“添加參數”,修改這段代碼。
src/pages/product/components/productDetails.vue,找到這個文件,搜索“添加參數”,修改這段代碼。
后臺文件修改完成后需要重新打包,打包文檔: https://doc.www.2233z.cn/pro/crmebprov3_1/15816