完善商品图片上传抖店功能

This commit is contained in:
wangxiaowei 2024-06-13 17:29:32 +08:00
parent 26e26bcd7e
commit 902ac6a4a4
2 changed files with 80 additions and 41 deletions

@ -11,6 +11,7 @@ namespace app\common\server\doudian;
use app\common\model\goods\GoodsSpec;
use app\shop\logic\goods\GoodsLogic;
use Nette\PhpGenerator\Dumper;
use \think\facade\Cache;
use DOMDocument;
@ -36,10 +37,14 @@ class DouDianServer
'尺寸' => '尺码大小'
);
private static $domain = '';
private $access_token = '';
public function __construct()
{
// $domain = input('server.REQUEST_SCHEME') . '://' . input('server.SERVER_NAME');
self::$domain = 'https://dlh.hainings.com';
\GlobalConfig::getGlobalConfig()->appKey = self::$appKey;
\GlobalConfig::getGlobalConfig()->appSecret = self::$appSecret;
if (Cache::get(self::$cacheName)) {
@ -56,10 +61,13 @@ class DouDianServer
}
/**
* @note 发布商品
* @note 发布商品,缺少一个统一规格的和多规格
*/
public function productAdd($goods_id = 0)
{
// $this->getShopCategory();
$this->getCateProperty();
// die;
$goods = GoodsLogic::info($goods_id);
$request = new \ProductAddV2Request();
$param = new \ProductAddV2Param();
@ -74,7 +82,7 @@ class DouDianServer
$param->product_type = $goods['base']['type'] == 0 ? 0 : 3;
// 叶子类目ID通过/shop/getShopCategory接口获取
$param->category_leaf_id = 20219;
$param->category_leaf_id = 20267;
// 商品名称
$param->name = $goods['base']['name'];
@ -86,29 +94,19 @@ class DouDianServer
}
// 商品轮播图
// $domain = input('server.REQUEST_SCHEME') . '://' . input('server.SERVER_NAME');
$domain = 'https://dlh.hainings.com';
$banner = '';
$img = [];
foreach ($goods['base']['goods_image'] as $v) {
$img['id'] = $v['id'];
$img['uri'] = $v['uri'];
$banner .= $domain . '/' . $v['uri'] . "|";
}
$param->pic = substr($banner, 0, -1);
// $param->pic = "https://p3-aio.ecombdimg.com/obj/ecom-shop-material/v1_ALSUvYM_70630259408755264040798_16991a88a61d1a1f4ab8bc526fcb7762_sx_388998_www800-800|https://p3-aio.ecombdimg.com/obj/ecom-shop-material/ALSUvYM_m_1ca7af9ff5adb5687879c18484d72777_sx_190683_www800-800|https://p3-aio.ecombdimg.com/obj/ecom-shop-material/v1_ALSUvYM_70526582776577067660798_51d5ff356da0f3664980f84a9029bedb_sx_24925_www800-800";
$this->batchUploadImage($img);
die;
$param->pic = implode('|', $this->batchUploadImage($goods['base']['goods_image']));
// 商品描述,目前只支持图片
$doc = new DOMDocument();
$doc->loadHTML($goods['base']['content']);
$images = $doc->getElementsByTagName('img');
$src = '';
foreach ($images as $image) {
$src .= $image->getAttribute('src') . '|';
$src = array();
foreach ($images as $k => $image) {
// $src[$k]['uri'] = str_replace('http://admin.dlh.com', 'https://dlh.hainings.com', $image->getAttribute('src'));
$src[$k]['uri'] = str_replace('http://admin.dlh.com', 'https://dlh.hainings.com', 'https://dlh.hainings.com/uploads/images/202311081656230c8308690.jpg');
$src[$k]['id'] = $goods_id . '_goods_detail_' . $k;
}
$src = str_replace('http://admin.dlh.com/', 'https://dlh.hainings.com/', $src);
$param->description = "https://p3-aio.ecombdimg.com/obj/ecom-shop-material/v1_ALSUvYM_70526582776577067660798_51d5ff356da0f3664980f84a9029bedb_sx_24925_www800-800";
$param->description = implode('|', $this->batchUploadImage($src, true));
// 支付方式,0货到付款 1在线支付,2,货到付款+在线支付
$param->pay_type = 1;
@ -124,14 +122,14 @@ class DouDianServer
// 客服号码
$param->mobile = "400123456";
$param->commit = true; #false仅保存草稿,true保存+提审
$param->product_format_new = "{\"241\":[{\"diy_type\":1,\"name\":\"常规款\",\"value\":17314}],\"631\":[{\"diy_type\":1,\"name\":\"东西南北风\",\"value\":0}],\"785\":[{\"measure_info\":{\"values\":[{\"module_id\":582,\"value\":\"亚麻\",\"unit_id\":0},{\"module_id\":583,\"unit_name\":\"%\",\"unit_id\":15,\"value\":\"100\"}],\"template_id\":252,\"value_name\":\"亚麻100%\"},\"diy_type\":1,\"name\":\"亚麻100%\",\"value\":0}],\"813\":[{\"diy_type\":1,\"name\":\"X型\",\"value\":8017}],\"981\":[{\"diy_type\":1,\"name\":\"蛋糕裙\",\"value\":22253}],\"1467\":[{\"diy_type\":0,\"name\":\"59%-69%(含)\",\"value\":168368}],\"1551\":[{\"diy_type\":1,\"name\":\"超短裙\",\"value\":22064}],\"1687\":[{\"diy_type\":0,\"name\":\"\",\"value\":0}],\"1878\":[{\"diy_type\":1,\"name\":\"高腰\",\"value\":16521}],\"2592\":[{\"diy_type\":1,\"name\":\"甜美风\",\"value\":192481}]}";
$param->product_format_new = "{\"1687\":[{\"diy_type\":1,\"name\":\"品牌\",\"value\":682910225}],\"631\":[{\"diy_type\":1,\"name\":\"东西南北风\",\"value\":0}],\"785\":[{\"measure_info\":{\"values\":[{\"module_id\":582,\"value\":\"亚麻\",\"unit_id\":0},{\"module_id\":583,\"unit_name\":\"%\",\"unit_id\":15,\"value\":\"100\"}],\"template_id\":252,\"value_name\":\"亚麻100%\"},\"diy_type\":1,\"name\":\"亚麻100%\",\"value\":0}],\"813\":[{\"diy_type\":1,\"name\":\"X型\",\"value\":8017}],\"981\":[{\"diy_type\":1,\"name\":\"蛋糕裙\",\"value\":22253}],\"1467\":[{\"diy_type\":0,\"name\":\"59%-69%(含)\",\"value\":168368}],\"1551\":[{\"diy_type\":1,\"name\":\"超短裙\",\"value\":22064}],\"1687\":[{\"diy_type\":0,\"name\":\"\",\"value\":0}],\"1878\":[{\"diy_type\":1,\"name\":\"高腰\",\"value\":16521}],\"2592\":[{\"diy_type\":1,\"name\":\"甜美风\",\"value\":192481}]}";
$param->standard_brand_id = 596120136; #品牌
# $param->presell_config_level = 2
//$param->size_info_template_id = 7119681499770667267; #尺码模板id https://op.jinritemai.com/docs/guide-docs/242/2094
$spec = [];
foreach ($goods['spec'] as $k => $v) {
$spec_text = self::$specName[$v['name']];
$spec_text = self::$specName[$v['name']] ? self::$specName[$v['name']] : $v['name'];
$spec['spec_values'][$k]['property_name'] = $spec_text;
$spec['spec_values'][$k]['property_id'] = 0;
foreach ($v['values'] as $k2 => $v2) {
@ -147,7 +145,7 @@ class DouDianServer
foreach ($goods['item'] as $k => $v) {
$spec_value_str = explode(',', $v['spec_value_str']);
foreach ($spec_value_str as $k2 => $v2) {
$res = GoodsSpec::where(['goods_id' => 733])->select();
$res = GoodsSpec::where(['goods_id' => $goods_id])->select();
$spec_list[$k]['sell_properties'][$k2]['value_name'] = str_replace('英寸', '码', $v2);
$spec_list[$k]['sell_properties'][$k2]['property_name'] = self::$specName[$res[$k2]['name']];
$spec_list[$k]['stock_num'] = $v['stock'];
@ -156,49 +154,90 @@ class DouDianServer
}
$param->spec_prices_v2 = json_encode($spec_list, JSON_UNESCAPED_UNICODE);
// dump($param->spec_prices_v2);
// $this->getShopCategory();
// $param->spec_prices_v2 = '[{"sell_properties":[{"value_name":"白色","property_name":"颜色分类"},{"value_name":"M","property_name":"尺码大小"}],"stock_num":11,"price":1,"code":"这个就是抖店后台的sku商家编码"}]';
// dump($param->spec_prices_v2);
$response = $request->execute($this->access_token);
dump($response);
}
/**
* @note 批量上传图片
* @note 批量上传图片, 图片需要按照 1:1 来上传,单张详情图高度不能超过 20000px
*/
public function batchUploadImage($data) {
dump($data);
public function batchUploadImage($data, $origin = false) {
$img = array();
foreach ($data as $k => $v) {
$img[$k]['request_id'] = $v['id'];
$img[$k]['folder_id'] = 0;
$img[$k]['name'] = '';
$img[$k]['name'] = pathinfo($v['uri'])['filename'];
$img[$k]['url'] = $origin ? $v['uri'] : self::$domain . '/' . $v['uri'];
$img[$k]['material_type'] = 'photo';
}
die;
$request = new \MaterialBatchUploadImageSyncRequest();
$param = new \MaterialBatchUploadImageSyncParam();
$request->setParam($param);
$param->materials = $data;
$param->need_distinct = false;
$response = $request->execute($this->access_token);
$param->materials = $img;
$param->need_distinct = true;
$result = $request->execute($this->access_token);
$upload_image = [];
// 获取 byte_url
if ($result->code == 10000) {
sleep(1);
$request = new \MaterialQueryMaterialDetailRequest();
$param = new \MaterialQueryMaterialDetailParam();
$request->setParam($param);
foreach ($result->data->success_map as $v) {
$param->material_id = $v->MaterialId;
$response = $request->execute($this->access_token);
if ($response->code == 10000 && $response->data->material_info->audit_status == 3) {
$upload_image[] = $response->data->material_info->byte_url;
} else {
throw new \think\Exception('图片审核失败');
}
}
} else {
throw new \think\Exception('图片上传失败');
}
return $upload_image;
}
private function getShopCategory()
/**
* @note 获取商品类目
*/
private function getShopCategory($category = '箱包')
{
$request = new \ShopGetShopCategoryRequest();
$param = new \ShopGetShopCategoryParam();
$request->setParam($param);
$param->cid = 0;
$response = $request->execute($this->access_token);
dump($response);
if ($response->code == 10000) {
// 暂时先测试箱包的类目,正常来说给到一个类目让用户选择
foreach ($response->data as $v) {
// dump($v);
// if ($v->name == $category) {
// $param->cid = $v->id;
// $response = $request->execute($this->access_token);
// dump($response);
// }
}
} else {
throw new \think\Exception('商品类目获取失败');
}
}
/**
* @note 获取商品类目属性列表
*/
private function getCateProperty()
{
$request = new \ProductGetCatePropertyV2Request();
$param = new \ProductGetCatePropertyV2Param();
$request->setParam($param);
$param->category_leaf_id = 20267;
$response = $request->execute($this->access_token);
dump($response);
}
public function testProductAdd()

@ -419,7 +419,7 @@ class Goods extends ShopBase
// dump($spec_list);
// die;
$doudian = new DouDianServer();
$doudian->productAdd(733);
$doudian->productAdd(736);
// $doudian->testProductAdd();
die;
$result = GoodsLogic::exportData($this->shop_id);