299 lines
14 KiB
PHP
299 lines
14 KiB
PHP
<?php
|
||
/**
|
||
* @package CipherSYS IM
|
||
* @author Xiaowei Wang <wangxiaowei@ciphersys.com>
|
||
* @copyright Copyright (c) 2022 - 2024, CipherSYS Co.,Ltd. (https://www.ciphersys.com/)
|
||
* @license CipherSYS Commercial License
|
||
* @link https://www.ciphersys.com
|
||
*/
|
||
|
||
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;
|
||
|
||
require "../vendor/doudian/src/autoload.php";
|
||
|
||
include "../vendor/doudian/src/open/api/materialgw/BinaryMaterialUploadAddressAuthParam.php";
|
||
include "../vendor/doudian/src/open/api/materialgw/BinaryMaterialUploadAddressAuthRequest.php";
|
||
include "../vendor/doudian/src/open/api/materialgw/BinaryMaterialUploadParam.php";
|
||
|
||
// ACCESS_TOKEN_CODE 工具型应用
|
||
const ACCESS_TOKEN_CODE = 1;
|
||
// ACCESS_TOKEN_SHOP_ID 自用型应用
|
||
const ACCESS_TOKEN_SHOP_ID = 2;
|
||
|
||
class DouDianServer
|
||
{
|
||
private static $cacheName = 'doudian@access_token';
|
||
private static $appKey = '7377352986936280614';
|
||
private static $appSecret = 'a4180703-743d-4dc0-a695-de213ee0d7b3';
|
||
private static $shopId = 4463798;
|
||
private static $specName = array(
|
||
'颜色' => '颜色分类',
|
||
'尺寸' => '尺码大小'
|
||
);
|
||
|
||
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)) {
|
||
$this->access_token = Cache::get(self::$cacheName);
|
||
} else {
|
||
$result = \AccessTokenBuilder::build(self::$shopId, ACCESS_TOKEN_SHOP_ID);
|
||
if ($result->isSuccess()) {
|
||
Cache::set(self::$cacheName, $result, 30 * 60);
|
||
$this->access_token = $result;
|
||
} else {
|
||
throw new \think\Exception('抖店token获取错误', $result->getCode());
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @note 发布商品,缺少一个统一规格的和多规格
|
||
*/
|
||
public function productAdd($goods_id = 0)
|
||
{
|
||
// $this->getShopCategory();
|
||
// $this->getCateProperty();
|
||
|
||
$goods = GoodsLogic::info($goods_id);
|
||
$request = new \ProductAddV2Request();
|
||
$param = new \ProductAddV2Param();
|
||
$request->setParam($param);
|
||
|
||
if (isset($goods['base']['code'])) {
|
||
// 商品编码
|
||
$param->outer_product_id = $goods['base']['code'];
|
||
}
|
||
|
||
// 0-普通,3-虚拟,6玉石闪购,7云闪购
|
||
$param->product_type = $goods['base']['type'] == 0 ? 0 : 3;
|
||
|
||
// 叶子类目ID通过/shop/getShopCategory接口获取
|
||
$param->category_leaf_id = 20267;
|
||
|
||
// 商品名称
|
||
$param->name = $goods['base']['name'] . 'pangge';
|
||
dump($param->name);
|
||
|
||
// 商家推荐语
|
||
if (isset($goods['recommend_remark'])) {
|
||
$param->recommend_remark = $goods['base']['remark'];
|
||
}
|
||
|
||
// 商品轮播图
|
||
$param->pic = implode('|', $this->batchUploadImage($goods['base']['goods_image']));
|
||
|
||
// 商品描述,目前只支持图片
|
||
$doc = new DOMDocument();
|
||
$doc->loadHTML($goods['base']['content']);
|
||
$images = $doc->getElementsByTagName('img');
|
||
$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;
|
||
}
|
||
$param->description = implode('|', $this->batchUploadImage($src, true));
|
||
|
||
// 支付方式,0货到付款 1在线支付,2,货到付款+在线支付
|
||
$param->pay_type = 1;
|
||
// 减库存类型:1-拍下减库存 2-付款减库存
|
||
$param->reduce_type = 1;
|
||
// 运费模板id,传0表示包邮,通过/freightTemplate/list接口获取
|
||
$param->freight_id = 0;
|
||
// 承诺发货时间,具体规则用/product/getProductUpdateRule获取
|
||
$param->delivery_delay_day = 2;
|
||
// 发货模式,0-现货发货,1-预售发货,2-阶梯发货,默认0
|
||
$param->presell_type = 0;
|
||
$param->after_sale_service = "{\"supply_day_return_selector\":\"7-1\"}";
|
||
// 客服号码
|
||
$param->mobile = "400123456";
|
||
$param->commit = true; #false仅保存草稿,true保存+提审
|
||
|
||
// $product_format_new = array(
|
||
// 697 => [
|
||
// 'diy_type' => 1,
|
||
// 'name' => '旅行箱捆绑带',
|
||
// 'value' => 27904
|
||
// ]
|
||
// );
|
||
// dump(json_encode($product_format_new, JSON_UNESCAPED_UNICODE));
|
||
// die;
|
||
$param->product_format_new = "{\"697\":[{\"diy_type\":1,\"name\":\"旅行箱捆绑带\",\"value\":27904}]}";
|
||
// $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->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']] ? 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) {
|
||
$spec['spec_values'][$k]['values'][$k2] = array(
|
||
'value_name' => str_replace('英寸', '码', $v2['value']),
|
||
'value_id' => 0
|
||
);
|
||
}
|
||
}
|
||
$param->spec_info = json_encode($spec, JSON_UNESCAPED_UNICODE);
|
||
|
||
$spec_list = [];
|
||
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' => $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'];
|
||
$spec_list[$k]['price'] = floatval($v['price']);
|
||
}
|
||
}
|
||
|
||
$param->spec_prices_v2 = json_encode($spec_list, JSON_UNESCAPED_UNICODE);
|
||
$response = $request->execute($this->access_token);
|
||
dump($response);
|
||
}
|
||
|
||
/**
|
||
* @note 批量上传图片, 图片需要按照 1:1 来上传,单张详情图高度不能超过 20000px
|
||
*/
|
||
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'] = pathinfo($v['uri'])['filename'];
|
||
$img[$k]['url'] = $origin ? $v['uri'] : self::$domain . '/' . $v['uri'];
|
||
$img[$k]['material_type'] = 'photo';
|
||
}
|
||
|
||
$request = new \MaterialBatchUploadImageSyncRequest();
|
||
$param = new \MaterialBatchUploadImageSyncParam();
|
||
$request->setParam($param);
|
||
$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;
|
||
}
|
||
|
||
/**
|
||
* @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) {
|
||
// 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;
|
||
// 女装 -> 半身裙
|
||
// $param->category_leaf_id = 20219;
|
||
$response = $request->execute($this->access_token);
|
||
dump($response);
|
||
}
|
||
|
||
public function testProductAdd()
|
||
{
|
||
$request = new \ProductAddV2Request();
|
||
$param = new \ProductAddV2Param();
|
||
$request->setParam($param);
|
||
$param->product_type = 0; #0-普通,3-虚拟
|
||
$param->category_leaf_id = 20219;
|
||
$param->name = "半身裙夏季时尚裙-标准sku-大田";
|
||
$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";
|
||
$param->description = "https://p3-aio.ecombdimg.com/obj/ecom-shop-material/v1_ALSUvYM_70526582776577067660798_51d5ff356da0f3664980f84a9029bedb_sx_24925_www800-800";
|
||
$param->pay_type = 1; #1在线支付
|
||
$param->reduce_type = 1;#减库存类型:1-拍下减库存 2-付款减库存
|
||
$param->freight_id = 0; #运费模板 通过/freightTemplate/list接口获取
|
||
$param->delivery_delay_day = 2; # 承诺发货时间,具体规则用/product/getProductUpdateRule获取
|
||
$param->presell_type = 0; #发货模式,0-现货发货,1-预售发货,2-阶梯发货,默认0
|
||
$param->after_sale_service = "{\"supply_day_return_selector\":\"7-1\"}";
|
||
$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->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
|
||
$param->spec_info = '{"spec_values":[{"property_name":"颜色分类","property_id":2752,"values":[{"value_name":"白色","value_id":35497}]},{"property_name":"尺码大小","property_id":4704,"values":[{"value_name":"M","value_id":0}]}]}';
|
||
$param->spec_prices_v2 ="[{\"sell_properties\":[{\"value_name\":\"白色\",\"property_name\":\"颜色分类\"},{\"value_name\":\"M\",\"property_name\":\"尺码大小\"}],\"stock_num\":11,\"price\":1,\"code\":\"这个就是抖店后台的sku商家编码\"}]";
|
||
$response = $request->execute($this->access_token);
|
||
dump($response);
|
||
}
|
||
|
||
/**
|
||
* @note 获取抖店access_token
|
||
*/
|
||
public function getToken()
|
||
{
|
||
return \AccessTokenBuilder::build(self::$shopId, ACCESS_TOKEN_SHOP_ID);
|
||
}
|
||
|
||
/**
|
||
* @note 刷新抖店access_token
|
||
*/
|
||
public function refreshToken($token) {
|
||
return \AccessTokenBuilder::refresh($token);
|
||
}
|
||
} |