222 lines
9.2 KiB
PHP
Raw Normal View History

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;
2024-01-21 19:21:38 +08:00
use think\facade\Queue;
use crmeb\jobs\ProductCopyJob;
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()
{
2024-01-21 22:51:04 +08:00
$arr=Db::name('store_product')->where('mer_id',208)->where('status',1)->where('is_show',0)->find();
$mer_id=239;//导入到的商户
$street_code=510521116;//导入到的商户
$type_id=17;//导入到的商户分类
foreach($arr as $k=>$v){
Queue::push(ProductCopyJob::class, ['product_id' => $v['product_id'], 'mer_id' =>$mer_id,'street_code'=>$street_code,'type_id'=>$type_id,'category_id'=>$v['category_id']]);//短信通知
}
2024-01-21 19:21:38 +08:00
2024-01-04 16:24:53 +08:00
halt(1);
2023-11-30 10:45:06 +08:00
$mer_id = 104;
2023-11-23 16:39:20 +08:00
$file = request()->file('file');
$zip_name = explode('.', $file->getOriginalName())[0];
// 上传到本地服务器
$savename = \think\facade\Filesystem::putFile('zippic', $file);
2023-11-30 09:15:35 +08:00
$dir = date('Y-m-d_H-i-s') . '_' . $mer_id;
2023-11-30 10:12:48 +08:00
$path = public_path('uploads/pic').$dir;
2023-11-30 09:15:35 +08:00
try {
2023-11-30 10:12:48 +08:00
$zip = new ZipArchive;
$filePath = public_path('uploads') . $savename;
$zip->open($filePath);
for ($i = 0; $i < $zip->numFiles; $i++) {
$statInfo = $zip->statIndex($i, ZipArchive::FL_ENC_RAW);
$filename = $this->transcoding($statInfo['name']);
$mkdFile = explode('/',$filename);
if ($statInfo['crc'] == 0) {
// 新建目录
if (!file_exists($path . '/' . $filename)) {
mkdir($path . '/' . $filename, 0777, true);
}
} else {
// 拷贝文件
if(count($mkdFile)==3){
if (!file_exists($path . '/' . $mkdFile[0].'/'.$mkdFile[1])) {
mkdir($path . '/' .$mkdFile[0].'/'.$mkdFile[1], 0777, true);
}
copy('zip://' . $file . '#' . $zip->getNameIndex($i), $path . '/' . $filename);
}
}
}
$zip->close();
} catch (\Exception $e) {
throw new \think\exception\HttpException(404, $e->getMessage() . '。line:' . $e->getLine());
2023-11-29 22:55:47 +08:00
}
2023-11-30 09:15:35 +08:00
2023-11-30 10:12:48 +08:00
$directory = $path.'/'.$zip_name;
2023-11-23 16:39:20 +08:00
$files = scandir($directory);
$dir = 'def/' . date('Y-m-d');
$upload = UploadService::create();
/**循环目录 */
foreach ($files as $file) {
2023-11-30 09:15:35 +08:00
if ($file === '.' || $file === '..' || $file === '__MACOSX') {
2023-11-23 16:39:20 +08:00
continue;
}
2023-11-30 09:15:35 +08:00
if (!is_dir($directory . '/' . $file)) {
2023-11-23 19:10:46 +08:00
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-30 09:15:35 +08:00
if ($file_two === '.' || $file_two === '..' || $file_two === '__MACOSX') {
2023-11-23 19:10:46 +08:00
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-30 10:12:48 +08:00
$details[] = $directory . '/' .$file.'/'. $file_two;
2023-11-23 16:39:20 +08:00
continue;
}
/**sku图 */
$sku = explode('==', $arr[0]);
if ($sku) {
$sku = implode(',', $sku);
$sku_arr[$sku] = $directory . '/' . $file . '/' . $file_two;
}
}
}
$where = ['mer_id' => $mer_id, 'is_del' => 0];
$update = [];
$update_content['title'] = '';
$update_content['image'] = [];
$update_content['type'] = 1;
2023-11-30 09:15:35 +08:00
$find = Db::name('store_product')->where($where)->where('store_name', $file)->find();
2023-11-23 16:39:20 +08:00
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;
}
2023-11-30 09:15:35 +08:00
if (isset($update['slider_image'])) {
2023-11-24 17:18:03 +08:00
$update['slider_image'] = implode(',', $update['slider_image']);
}
2023-11-30 09:15:35 +08:00
Db::name('store_product')->where('product_id', $find['product_id'])->update($update);
2023-11-23 16:39:20 +08:00
/**更新规格图片 */
foreach ($sku_arr as $k => $v) {
2023-11-30 11:43:13 +08:00
// $sku = explode(',', $k);
// if(count($sku)==2){
// $sku_name=$sku[0];
// }else{
// continue;
// }
$store_product_attr_value = Db::name('store_product_attr_value')->where(['mer_id' => $mer_id, 'product_id' => $find['product_id'], 'sku' => $k])->find();
2023-11-23 16:39:20 +08:00
if ($store_product_attr_value) {
$oss = $upload->to($dir)->stream(file_get_contents($v));
Db::name('store_product_attr_value')
2023-11-30 11:43:13 +08:00
->where(['mer_id' => $mer_id, 'product_id' => $find['product_id'], 'sku' => $k])
2023-11-23 16:39:20 +08:00
->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) {
2023-11-30 09:15:35 +08:00
if (isset($update_content['image']) && !empty($update_content['image'])) {
2023-11-24 17:18:03 +08:00
Db::name('store_product_content')
2023-11-30 09:15:35 +08:00
->where(['product_id' => $find['product_id']])
->update(['content' => json_encode($update_content)]);
2023-11-24 17:18:03 +08:00
}
2023-11-23 16:39:20 +08:00
} else {
$update_content['product_id'] = $find['product_id'];
2023-11-30 10:45:06 +08:00
Db::name('store_product_content')
2023-11-30 09:15:35 +08:00
->insert(['product_id' => $find['product_id'], 'type' => 1, 'content' => json_encode($update_content)]);
2023-11-23 16:39:20 +08:00
}
} catch (Exception $e) {
2023-11-30 09:15:35 +08:00
halt($e->getMessage(), $e->getLine());
2023-11-23 16:39:20 +08:00
}
}
}
2023-11-23 13:34:40 +08:00
halt(1);
}
2023-11-29 22:55:47 +08:00
public function transcoding($fileName)
{
$encoding = mb_detect_encoding($fileName, ['UTF-8', 'GBK', 'BIG5', 'CP936']);
if (DIRECTORY_SEPARATOR == '/') { // linux
$fileName = iconv($encoding, 'UTF-8', $fileName);
} else { // win
$fileName = iconv($encoding, 'GBK', $fileName);
}
return $fileName;
}
function createDirectories($path) {
if (is_dir($path)) {
return true ;
} else {
$parts = explode(DIRECTORY_SEPARATOR, $path);
$part = null;
foreach ($parts as $part) {
if ($part === '' || $part === '.' || $part === '..') {
continue;
}
$dir = dirname($path) . DIRECTORY_SEPARATOR . $part;
if (!file_exists($dir)) {
mkdir($dir, 0777, true);
}
}
return true ;
}
}
2023-11-23 13:34:40 +08:00
}