2023-11-23 13:34:40 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
|
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
|
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
|
// | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
|
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
|
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
|
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
|
// | Author: CRMEB Team <admin@crmeb.com>
|
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace app\controller\api;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use crmeb\basic\BaseController;
|
2023-11-23 16:39:20 +08:00
|
|
|
|
use think\facade\Db;
|
|
|
|
|
use crmeb\services\UploadService;
|
|
|
|
|
use Exception;
|
|
|
|
|
use ZipArchive;
|
2023-11-23 13:34:40 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class Auth
|
|
|
|
|
* @package app\controller\api
|
|
|
|
|
* @author xaboy
|
|
|
|
|
* @day 2020-05-06
|
|
|
|
|
*/
|
|
|
|
|
class Demo extends BaseController
|
|
|
|
|
{
|
2023-11-23 16:39:20 +08:00
|
|
|
|
public function index()
|
|
|
|
|
{
|
|
|
|
|
$file = request()->file('file');
|
|
|
|
|
$zip_name = explode('.', $file->getOriginalName())[0];
|
|
|
|
|
// 上传到本地服务器
|
|
|
|
|
$savename = \think\facade\Filesystem::putFile('zippic', $file);
|
|
|
|
|
$destination_path = public_path('uploads/pic') . $zip_name;
|
|
|
|
|
$zip = new ZipArchive;
|
|
|
|
|
if ($zip->open(public_path('uploads').$savename) === TRUE) {
|
|
|
|
|
$zip->extractTo($destination_path);
|
|
|
|
|
$zip->close();
|
|
|
|
|
} else {
|
|
|
|
|
throw new \think\exception\HttpException(404, '解压失败');
|
|
|
|
|
}
|
|
|
|
|
$mer_id = 35;
|
|
|
|
|
$directory = $destination_path;
|
|
|
|
|
$files = scandir($directory);
|
|
|
|
|
$dir = 'def/' . date('Y-m-d');
|
|
|
|
|
$upload = UploadService::create();
|
|
|
|
|
/**循环目录 */
|
|
|
|
|
foreach ($files as $file) {
|
|
|
|
|
if ($file === '.' || $file === '..' ||$file ==='__MACOSX') {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2023-11-23 19:10:46 +08:00
|
|
|
|
if(!is_dir($directory . '/' . $file)){
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2023-11-23 16:39:20 +08:00
|
|
|
|
$files_two = scandir($directory . '/' . $file);
|
|
|
|
|
|
|
|
|
|
$image_extensions = array('jpg', 'jpeg', 'png');
|
|
|
|
|
$image = '';
|
|
|
|
|
$slider_image = [];
|
|
|
|
|
$details = [];
|
|
|
|
|
$sku_arr = [];
|
2023-11-23 19:10:46 +08:00
|
|
|
|
/**清洗图片 */
|
2023-11-23 16:39:20 +08:00
|
|
|
|
foreach ($files_two as $file_two) {
|
2023-11-23 19:10:46 +08:00
|
|
|
|
if ($file_two === '.' || $file_two === '..' ||$file_two ==='__MACOSX') {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$arr = explode('.', $file_two);
|
|
|
|
|
if (in_array($arr[1], $image_extensions)) {
|
2023-11-23 16:39:20 +08:00
|
|
|
|
/**首图 */
|
|
|
|
|
$images[] = $file_two;
|
2023-11-23 19:10:46 +08:00
|
|
|
|
if ($image == '' && is_numeric($arr[0])) {
|
2023-11-23 16:39:20 +08:00
|
|
|
|
$image = $directory . '/' . $file . '/' . $file_two;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
/**轮播图 */
|
2023-11-23 19:10:46 +08:00
|
|
|
|
if (is_numeric($arr[0]) && count($slider_image) < 4) {
|
2023-11-23 16:39:20 +08:00
|
|
|
|
$slider_image[] = $directory . '/' . $file . '/' . $file_two;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
/**详情图 */
|
2023-11-23 19:10:46 +08:00
|
|
|
|
if (is_numeric($arr[0])) {
|
2023-11-23 16:39:20 +08:00
|
|
|
|
$details[] = $directory . '/' . $file_two;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
/**sku图 */
|
|
|
|
|
$sku = explode('==', $arr[0]);
|
|
|
|
|
if ($sku) {
|
|
|
|
|
$sku = implode(',', $sku);
|
|
|
|
|
$sku_arr[$sku] = $directory . '/' . $file . '/' . $file_two;
|
2023-11-23 19:10:46 +08:00
|
|
|
|
|
2023-11-23 16:39:20 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$where = ['mer_id' => $mer_id, 'is_del' => 0];
|
|
|
|
|
$update = [];
|
|
|
|
|
$update_content['title'] = '';
|
|
|
|
|
$update_content['image'] = [];
|
|
|
|
|
$update_content['type'] = 1;
|
|
|
|
|
$find = Db::name('store_product')->where($where)->where('store_name', 'like', '%' . $file . '%')->find();
|
|
|
|
|
if ($find) {
|
|
|
|
|
try {
|
|
|
|
|
/**更新商品图片 */
|
|
|
|
|
$image = $upload->to($dir)->stream(file_get_contents($image));
|
|
|
|
|
$update['image'] = $image->filePath;
|
|
|
|
|
foreach ($slider_image as $k => $v) {
|
|
|
|
|
$oss = $upload->to($dir)->stream(file_get_contents($v));
|
|
|
|
|
$update['slider_image'][] = $oss->filePath;
|
|
|
|
|
}
|
|
|
|
|
$update['slider_image'] = implode(',', $update['slider_image']);
|
|
|
|
|
Db::name('store_product')->where('product_id', $find['product_id'])->update($update);
|
|
|
|
|
/**更新规格图片 */
|
|
|
|
|
foreach ($sku_arr as $k => $v) {
|
|
|
|
|
$store_product_attr_value = Db::name('store_product_attr_value')->where(['mer_id' => $mer_id, 'product_id' => $find['product_id'], 'sku' => $k])->find();
|
|
|
|
|
if ($store_product_attr_value) {
|
|
|
|
|
$oss = $upload->to($dir)->stream(file_get_contents($v));
|
|
|
|
|
Db::name('store_product_attr_value')
|
|
|
|
|
->where(['mer_id' => $mer_id, 'product_id' => $find['product_id'], 'sku' => $k])
|
|
|
|
|
->update(['image' => $oss->filePath]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/**更新详情图片 */
|
|
|
|
|
$store_product_content = Db::name('store_product_content')->where(['product_id' => $find['product_id']])->find();
|
|
|
|
|
foreach ($details as $k => $v) {
|
|
|
|
|
$oss = $upload->to($dir)->stream(file_get_contents($v));
|
|
|
|
|
$update_content['image'][] = $oss->filePath;
|
|
|
|
|
}
|
|
|
|
|
if ($store_product_content) {
|
|
|
|
|
Db::name('store_product_content')
|
|
|
|
|
->where(['product_id' => $find['product_id']])
|
|
|
|
|
->update(json_encode($update_content));
|
|
|
|
|
} else {
|
|
|
|
|
$update_content['product_id'] = $find['product_id'];
|
|
|
|
|
Db::name('store_product_content')
|
|
|
|
|
->insert(json_encode($update_content));
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
|
halt($e->getMessage(), $e->getLine());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-23 13:34:40 +08:00
|
|
|
|
halt(1);
|
|
|
|
|
}
|
|
|
|
|
}
|