jh-admin/addon/ali1688/shop/controller/Goods.php

464 lines
24 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace addon\ali1688\shop\controller;
use addon\form\model\Form;
use addon\postertemplate\model\PosterTemplate as PosterTemplateModel;
use addon\supply\model\Supplier as SupplierModel;
use app\model\express\Config as ExpressConfig;
use app\model\express\ExpressTemplate as ExpressTemplateModel;
use app\model\goods\GoodsAttribute as GoodsAttributeModel;
use app\model\goods\GoodsBrand as GoodsBrandModel;
use app\model\goods\GoodsCategory as GoodsCategoryModel;
use app\model\goods\GoodsCommunityQrCode;
use app\model\goods\GoodsLabel as GoodsLabelModel;
use app\model\goods\GoodsService as GoodsServiceModel;
use app\model\web\Config as ConfigModel;
use app\shop\controller\BaseShop;
use addon\ali1688\model\Goods as goodsModel;
use addon\ali1688\model\Template as TemplateModel;
use addon\ali1688\model\Choice;
use addon\ali1688\model\CloudApi;
class Goods extends BaseShop
{
private $goodsModel;
private $Choice;
public function __construct()
{
parent::__construct();
$this->goodsModel = new GoodsModel($this->site_id);
$this->Choice = new Choice($this->site_id);
}
/**
* 查看已入库商品
* @return mixed
*/
public function lists()
{
if (request()->isAjax()) {
// 参数获取
$start_time = input('start_time', '');
$end_time = input('end_time', '');
$search = input('goods_name', '');
if ($start_time && $end_time) {
$condition[] = ['w.create_time', 'between', [date_to_time($start_time), date_to_time($end_time)]];
} elseif (!$start_time && $end_time) {
$condition[] = ['w.create_time', '<=', date_to_time($end_time)];
} elseif ($start_time && !$end_time) {
$condition[] = ['w.create_time', '>=', date_to_time($start_time)];
}
//筛选商品名称
if (!empty($search)) {
$condition[] = ['w.goods_name', 'like', '%' . $search . '%'];
}
$status = input('goods_state', '');
if (!empty($status)) {
$condition[] = ['w.goods_state', '=', $status];
}
$condition[] = ['w.site_id', '=', $this->site_id];
$page = input('page', 1);
$page_size = input('page_size', PAGE_LIST_ROWS);
return $this->goodsModel->getGoodsList($condition, $page, $page_size);
} else {
$this->forthMenu();
return $this->fetch("goods/lists");
}
}
/**
* @return void
* 选品入库
*/
public function servicegoods()
{
$search['site_id'] = $this->site_id;
$template_list = (new TemplateModel($this->site_id))->getTemplateList($search);
$CloudApi = new CloudApi($this->site_id);
$category = $CloudApi->getAlibabaCategory();
$ProductFilter = $CloudApi->getAlibabaProductFilter();
if ($ProductFilter) {
foreach ($ProductFilter as $key => $value) {
if (in_array($value['key'], ['dyCiphertext', 'ksCiphertext', 'jxhy'])) {
unset($ProductFilter[$key]);
}
}
}
$this->assign('ProductFilter', $ProductFilter);
$this->assign('category_list', $category);
$this->assign('template_list', $template_list['data']['list']);
return $this->fetch('goods/servicegoods');
}
/**
* @return void
* 选品入库
*/
public function putIn()
{
if (request()->isAjax()) {
$category_id = input("category_id", 0);// 分类id
$productID = input("productID");// 分类id
$category_json = json_encode($category_id);//分类字符串
$category_id = ',' . implode(',', $category_id) . ',';
$goods_id = input("goods_id", 0);// 商品id
$data = [
'goods_id' => $goods_id,// 商品id
'goods_name' => input("goods_name", ""),// 商品名称,
'goods_attr_class' => input("goods_attr_class", ""),// 商品类型id,
'goods_attr_name' => input("goods_attr_name", ""),// 商品类型名称,
'is_limit' => input("is_limit", "0"),// 商品是否限购,
'limit_type' => input("limit_type", "1"),// 商品限购类型,
'site_id' => $this->site_id,
'category_id' => $category_id,
'category_json' => $category_json,
'goods_image' => input("goods_image", ""),// 商品主图路径
'goods_content' => input("goods_content", ""),// 商品详情
'goods_state' => input("goods_state", ""),// 商品状态1.正常0下架
'price' => input("price", 0),// 商品价格取第一个sku
'market_price' => input("market_price", 0),// 市场价格取第一个sku
'cost_price' => input("cost_price", 0),// 成本价取第一个sku
'sku_no' => input("sku_no", ""),// 商品sku编码
'weight' => input("weight", ""),// 重量
'volume' => input("volume", ""),// 体积
'goods_stock' => input("goods_stock", 0),// 商品库存(总和)
'goods_stock_alarm' => input("goods_stock_alarm", 0),// 库存预警
'is_free_shipping' => input("is_free_shipping", 1),// 是否免邮
'shipping_template' => input("shipping_template", 0),// 指定运费模板
'goods_spec_format' => input("goods_spec_format", ""),// 商品规格格式
'goods_attr_format' => input("goods_attr_format", ""),// 商品参数格式
'introduction' => input("introduction", ""),// 促销语
'keywords' => input("keywords", ""),// 关键词
'unit' => input("unit", ""),// 单位
'sort' => input("sort", 0),// 排序,
'video_url' => input("video_url", ""),// 视频
'goods_sku_data' => input("goods_sku_data", ""),// SKU商品数据
'goods_service_ids' => input("goods_service_ids", ''),// 商品服务id集合
'label_id' => input("label_id", ''),// 商品分组id
'brand_id' => input("brand_id", 0),//品牌id
'virtual_sale' => input("virtual_sale", 0),// 虚拟销量
'max_buy' => input("max_buy", 0),// 限购
'min_buy' => input("min_buy", 0),// 起售
'recommend_way' => input('recommend_way', 0), // 推荐方式1新品2精品3推荐
'timer_on' => strtotime(input('timer_on', 0)),//定时上架
'timer_off' => strtotime(input('timer_off', 0)),//定时下架
'spec_type_status' => input('spec_type_status', 0),
'is_consume_discount' => input('is_consume_discount', 0),//是否参与会员折扣
'qr_id' => input('qr_id', 0),//社群二维码id
'sale_show' => input('sale_show', 0),//
'stock_show' => input('stock_show', 0),//
'market_price_show' => input('market_price_show', 0),//
'barrage_show' => input('barrage_show', 0),//
'template_id' => input('template_id', 0),//商品海报id
'form_id' => input('form_id', 0),
'support_trade_type' => input('support_trade_type', ''),
'sale_channel' => input('sale_channel', 'all'),
'sale_store' => input('sale_store', 'all'),
'is_unify_pirce' => input('is_unify_pirce', '1'),
'is_zmxx' => input('is_zmxx', 0),
'cat_id' => input('cat_id', ''),
];
if ($goods_id) {
$data['site_id'] = $this->site_id;
$goods_image = explode(',', $data['goods_image']);
$goods_sku_data = json_decode($data['goods_sku_data'], true);
$sku_image = array_column($goods_sku_data, 'sku_image');
$goods_images = array_unique(array_merge($goods_image, $sku_image));
$this->goodsModel->GooodsImgCheck($data['site_id'], $goods_images);//验证图片上传
$res = (new \app\model\goods\Goods())->editGoods($data);
return $res;
} else {
$template_id = input('template_id', 0);
return $this->goodsModel->addGoods($data, $this->site_id, $productID, $template_id);
}
} else {
$data = request()->all();
$data_list = $this->Choice->productInfoGet($data['itemId']);
if (!$data_list['result']['success']) {
return $this->error($data_list['result']['message'], '');
}
$aliGoodsInfo = $data_list['result']['result'][0]['productInfo'];
if ($aliGoodsInfo['bizType'] != 1) return $this->error('', '暂不支持非商品请选择其他商品');
// $freightTemplate = $aliGoodsInfo['shippingInfo'];
// //运费模板
// if(isset($freightTemplate['freightTemplateID']) && ($freightTemplate['freightTemplateID'] != 0 && $freightTemplate['freightTemplateID'] != 1)){
// $freight_detail = $this->Choice->productFreightTemplate($freightTemplate['freightTemplateID']);
// }
$price_template = input('template_id', 0);
$saleType = input('saleType', 'normal');
$sku_url = $this->goodsModel->getDefaultSkuImage();
$goods_info = $this->goodsModel->generateGoodsInfo($aliGoodsInfo, $price_template, $saleType);
if ($goods_info['goods_id']) {
$editGoodsInfo = event('editGoodsInfo', ['goods_id' => $goods_info['goods_id'], 'site_id' => $this->site_id]);
if ($editGoodsInfo) {
foreach ($editGoodsInfo as $item) {
if ($item['apptype'] == 'aliapp') {
$this->assign("alipay_category_id", $item['category_id'] ?? '');
}
}
}
}
$search['site_id'] = $this->site_id;
$template_list = (new TemplateModel($this->site_id))->getTemplateList($search);
$this->assign('categoryName', $aliGoodsInfo['categoryName']);
$this->assign('goods_id', $goods_info['goods_id']);
$this->assign('productID', $aliGoodsInfo['productID']);
$this->assign('template_id', $price_template);
$this->assign('goods_info', $goods_info);
$this->assign('sku_url', $sku_url);
$this->assign('template_list', $template_list['data']['list']);
// $this->assign('bizType', ['1' => '商品', '2' => '加工', '3' => '代理', '4' => '合作', '5' => '商务服务']);
// $productType = ['wholesale' => '在线批发商品', 'sourcing' => '询盘商品'];
// $this->assign('productType', $productType);
//获取一级商品分类
$goods_category_model = new GoodsCategoryModel();
$condition = [
['pid', '=', 0],
['site_id', '=', $this->site_id]
];
$goods_category_list = $goods_category_model->getCategoryList($condition, 'category_id,category_name,level,commission_rate')['data'];
$this->assign("goods_category_list", $goods_category_list);
//获取运费模板
$express_template_model = new ExpressTemplateModel();
$express_template_list = $express_template_model->getExpressTemplateList([['site_id', "=", $this->site_id]], 'template_id,template_name', 'is_default desc')['data'];
if (empty($express_template_list)) {
return $this->error("请设置运费模板");
}
$this->assign("express_template_list", $express_template_list);
//获取商品类型
$goods_attr_model = new GoodsAttributeModel();
$attr_class_list = $goods_attr_model->getAttrClassList([['site_id', '=', $this->site_id]], 'class_id,class_name')['data'];
$this->assign("attr_class_list", $attr_class_list);
// 商品服务
$goods_service_model = new GoodsServiceModel();
$service_list = $goods_service_model->getServiceList([['site_id', '=', $this->site_id]], 'id,service_name,icon')['data'];
$this->assign("service_list", $service_list);
// 商品分组
$goods_label_model = new GoodsLabelModel();
$label_list = $goods_label_model->getLabelList([['site_id', '=', $this->site_id]], 'id,label_name', 'sort asc')['data'];
$this->assign("label_list", $label_list);
//商品默认排序值
$config_model = new ConfigModel();
$sort_config = $config_model->getGoodsSort($this->site_id, $this->app_module);
$sort_config = $sort_config['data']['value'];
$this->assign("sort_config", $sort_config);
//获取品牌
$goods_brand_model = new GoodsBrandModel();
$brand_list = $goods_brand_model->getBrandList([['site_id', '=', $this->site_id]], "brand_id, brand_name")['data'];
$this->assign("brand_list", $brand_list);
//获取商品海报
$poster_template_model = new PosterTemplateModel();
$poster_list = $poster_template_model->getPosterTemplateList([['site_id', '=', $this->site_id], ['template_status', '=', 1]], 'template_id,poster_name,site_id');
$this->assign('poster_list', $poster_list['data']);
$this->assign('virtualcard_exit', addon_is_exit('virtualcard', $this->site_id));
//获取社群二维码
$goods_community_model = new GoodsCommunityQrCode();
$goods_community_qr_list = $goods_community_model->getQrList([['site_id', '=', $this->site_id], ['qr_state', '=', 1]], 'qr_id,qr_name,site_id');
$this->assign('goods_community_qr_list', $goods_community_qr_list['data']);
$form_is_exit = addon_is_exit('form', $this->site_id);
if ($form_is_exit) {
$form_list = (new Form())->getFormList([['site_id', '=', $this->site_id], ['form_type', '=', 'goods'], ['is_use', '=', 1]], 'id desc', 'id, form_name')['data'];
$this->assign('form_list', $form_list);
}
$this->assign('form_is_exit', $form_is_exit);
$express_type = (new ExpressConfig())->getEnabledExpressType($this->site_id);
$this->assign('express_type', $express_type);
$this->assign('all_goodsclass', event('GoodsClass'));
$this->assign('goods_class', (new \app\model\goods\Goods())->getGoodsClass());
$this->assign('store_is_exit', addon_is_exit('store', $this->site_id));
$is_install_supply = addon_is_exit("supply");
if ($is_install_supply) {
$supplier_model = new SupplierModel();
$supplier_list = $supplier_model->getSupplyList([['supplier_site_id', '=', $this->site_id]], 'supplier_id,title', 'supplier_id desc')['data'];
$this->assign("supplier_list", $supplier_list);
}
$this->assign("is_install_supply", $is_install_supply);
$config = new \addon\aliapp\model\Config();
$config_info = $config->getAppConfig($this->site_id)['data']['value'];
$is_alipay_goods = 0;
if ($config_info) {
$is_alipay_goods = 1;
}
$this->assign("is_alipay_goods", $is_alipay_goods);
return $this->fetch('goods/edit_goods');
}
}
/**
* @return void
* 批量入库
*/
public function putIns()
{
if (request()->isAjax()) {
$category_id = input("category_id", 0);// 分类id
$goods_id = input("item_id", 0);// 商品id
$category_json = json_encode($category_id);//分类字符串
$category_id = ',' . implode(',', $category_id) . ',';
$saleType = input("saleType", 'normal');
if (is_array($goods_id) && count($goods_id) > 0) {
$data_list = $this->Choice->productInfoGet($goods_id);
$aliGoodsInfo = $data_list['result']['result'];
$price_template = input('template_id', 0);
$success = $fail = 0;
$template_id = 0;
if ($saleType != 'normal') {
$template_id = model('express_template')->getValue(['site_id' => $this->site_id, 'is_default' => 1], 'template_id');
if (empty($template_id)) return $this->error('请先设置默认运费模板');
}
foreach ($aliGoodsInfo as $key => $val) {
if ($val['productInfo']['bizType'] == 1) {
$goods_info = $this->goodsModel->generateGoodsInfo($val['productInfo'], $price_template, $saleType);
$data = [
// 商品id
'goods_name' => $goods_info['goods_name'],
// 商品名称,
'goods_attr_class' => input("goods_attr_class", ""),
// 商品类型id,
'goods_attr_name' => input("goods_attr_name", ""),
// 商品类型名称,
'is_limit' => input("is_limit", "0"),
// 商品是否限购,
'limit_type' => input("limit_type", "1"),
// 商品限购类型,
'site_id' => $this->site_id,
'category_id' => $category_id,
'category_json' => $category_json,
'goods_image' => $goods_info['goods_image'],
// 商品主图路径
'goods_content' => $goods_info['goods_content'],
// 商品详情
'goods_state' => 0,
// 商品状态1.正常0下架
'price' => $goods_info['sku_list'][0]['price'],
// 商品价格取第一个sku
'market_price' => $goods_info['sku_list'][0]['market_price'],
// 市场价格取第一个sku
'cost_price' => $goods_info['sku_list'][0]['cost_price'],
// 成本价取第一个sku
'sku_no' => $goods_info['sku_list'][0]['sku_no'],
// 商品sku编码
'weight' => $goods_info['sku_list'][0]['weight'],
// 重量
'volume' => $goods_info['sku_list'][0]['volume'],
// 体积
'goods_stock' => array_sum(array_column($goods_info['sku_list'], 'stock')),
// 商品库存(总和)
'goods_stock_alarm' => input("goods_stock_alarm", 0),
// 库存预警
'is_free_shipping' => $goods_info['is_free_shipping'],//input("is_free_shipping",1),
'shipping_template' => $template_id,//运费模版 // 是否免邮
// 指定运费模板
'goods_spec_format' => $goods_info['goods_spec_format'],
// 商品规格格式
'goods_attr_format' => $goods_info['goods_attr_format'],
// 商品参数格式
'introduction' => $goods_info['introduction'],
// 促销语
'keywords' => $goods_info['keywords'],
// 关键词
'unit' => $goods_info['unit'],
// 单位
'sort' => $goods_info['sort'],
// 排序,
'video_url' => $goods_info['video_url'],
// 视频
'goods_sku_data' => $goods_info['goods_sku_data'],
// SKU商品数据
'goods_service_ids' => input("goods_service_ids", ''),
// 商品服务id集合
'label_id' => input("label_id", ''),
// 商品分组id
'brand_id' => input("brand_id", 0),
//品牌id
'virtual_sale' => $goods_info['virtual_sale'],
// 虚拟销量
'max_buy' => $goods_info['max_buy'],
// 限购
'min_buy' => $goods_info['min_buy'],
// 起售
'recommend_way' => input('recommend_way', 0),
// 推荐方式1新品2精品3推荐
'timer_on' => strtotime(input('timer_on', 0)),
//定时上架
'timer_off' => strtotime(input('timer_off', 0)),
//定时下架
'spec_type_status' => empty($goods_info['spec_type_status']) ? 0 : 1,
'is_consume_discount' => $goods_info['is_consume_discount'],
//是否参与会员折扣
'qr_id' => $goods_info['qr_id'],
//社群二维码id
'sale_show' => $goods_info['sale_show'],
'stock_show' => $goods_info['stock_show'],
'market_price_show' => $goods_info['market_price_show'],
'barrage_show' => $goods_info['barrage_show'],
'template_id' => 0,
//商品海报id
'form_id' => 0,
'support_trade_type' => $goods_info['support_trade_type'],
'sale_channel' => input('sale_channel', 'all'),
'sale_store' => input('sale_store', 'all'),
'is_unify_pirce' => $goods_info['is_unify_pirce'],
'is_zmxx' => $goods_info['is_zmxx'],
'cat_id' => '',
];
$res = $this->goodsModel->addGoods($data, $this->site_id, $val['productInfo']['productID'], $price_template);
if ($res['code'] === 0) {
$success++;
} else {
$fail++;
}
} else {
$fail++;
}
}
$msg = '入库成功' . $success . '件商品';
return ['code' => 0, 'data' => '入库成功', 'message' => $fail > 0 ? $msg . ',入库失败' . $fail . '件' : $msg];
}
} else {
$data = request()->all();
//获取一级商品分类
$goods_category_model = new GoodsCategoryModel();
$condition = [
['pid', '=', 0],
['site_id', '=', $this->site_id]
];
$template_id = input('template_id', 0);
$goods_category_list = $goods_category_model->getCategoryList($condition, 'category_id,category_name,level,commission_rate')['data'];
$this->assign("goods_category_list", $goods_category_list);
$this->assign("item_id", $data['itemId']);
$this->assign("template_id", $template_id);//单独选择模板
$search['site_id'] = $this->site_id;
$template_list = (new TemplateModel($this->site_id))->getTemplateList($search);
$this->assign('template_list', $template_list['data']['list']);
return $this->fetch('goods/putins');
}
}
/**
* @return void
* 从1688获取入库商品
*/
public function getGoodsList()
{
$param = request()->all();
return $this->Choice->productlist($param);
}
}