图片上传 和修复查询

This commit is contained in:
mkm 2023-11-23 16:39:20 +08:00
parent 6d387471fc
commit 9e70be804a
5 changed files with 315 additions and 90 deletions

View File

@ -2000,7 +2000,7 @@ class StoreOrderRepository extends BaseRepository
{ {
$order = $this->dao->getWhere(['order_id' => $id, 'mer_id' => $merId, 'order_type' => 1], '*', ['orderProduct']); $order = $this->dao->getWhere(['order_id' => $id, 'mer_id' => $merId, 'order_type' => 1], '*', ['orderProduct']);
if (!$order) throw new ValidateException('订单不存在'); if (!$order) throw new ValidateException('订单不存在');
if($order['verify_code']!=$data['verify_code']){ if ($order['verify_code'] != $data['verify_code']) {
throw new ValidateException('核销码不正确'); throw new ValidateException('核销码不正确');
} }
if (!$order->paid) throw new ValidateException('订单未支付'); if (!$order->paid) throw new ValidateException('订单未支付');
@ -2524,51 +2524,50 @@ class StoreOrderRepository extends BaseRepository
$find = Db::name('store_product')->where('mer_id', $merId)->where('store_name', $datum['value']['store_name'])->find(); $find = Db::name('store_product')->where('mer_id', $merId)->where('store_name', $datum['value']['store_name'])->find();
$attr_one = explode('|', $datum['value']['attr_one']); $attr_one = explode('|', $datum['value']['attr_one']);
$attr_two = explode('|', $datum['value']['attr_two']); $attr_two = explode('|', $datum['value']['attr_two']);
$attr=[]; $attr = [];
$arr = []; $arr = [];
foreach ($attr_one as $key => $value){ foreach ($attr_one as $key => $value) {
$attr[$value]=$attr_two[$key]; $attr[$value] = $attr_two[$key];
$arr[] = [ $arr[] = [
'value' => $value, 'value' => $value,
'detail' => [$attr_two[$key] ?? []], 'detail' => [$attr_two[$key] ?? []],
'inputVisible' => false 'inputVisible' => false
]; ];
} }
$array_values=array_values($attr); $array_values = array_values($attr);
if ($find) { if ($find) {
$store_product_attr_value = Db::name('store_product_attr_value')->where('product_id', $find['product_id'])->where('bar_code', $datum['value']['bar_code'])->find(); $store_product_attr_value = Db::name('store_product_attr_value')->where('product_id', $find['product_id'])->where('bar_code', $datum['value']['bar_code'])->find();
if ($store_product_attr_value) { if ($store_product_attr_value) {
Log::error('xls导入商品:已经导入过该规格了'); Log::error('xls导入商品:已经导入过该规格了');
continue; continue;
} }
$is_update=false; $is_update = false;
foreach ($attr as $kk => $vv) { foreach ($attr as $kk => $vv) {
/**查询当前规格是否存在 */ /**查询当前规格是否存在 */
$attr_values_find = Db::name('store_product_attr')->where('product_id', $find['product_id'])->where('attr_name', $kk) $attr_values_find = Db::name('store_product_attr')->where('product_id', $find['product_id'])->where('attr_name', $kk)
->find(); ->find();
if ($attr_values_find) { if ($attr_values_find) {
$attr_values= $attr_values_find['attr_values']; $attr_values = $attr_values_find['attr_values'];
$attr_values = explode('-!-', $attr_values); $attr_values = explode('-!-', $attr_values);
if (!in_array($vv, $attr_values)) { if (!in_array($vv, $attr_values)) {
$attr_values[] = $vv; $attr_values[] = $vv;
$attr_values = implode('-!-', $attr_values); $attr_values = implode('-!-', $attr_values);
Db::name('store_product_attr')->where('product_id', $find['product_id'])->where('attr_name', $kk)->update(['attr_values' => $attr_values]); Db::name('store_product_attr')->where('product_id', $find['product_id'])->where('attr_name', $kk)->update(['attr_values' => $attr_values]);
$this->create_product_attr_value($find,$datum,$product_type,$attr,$merId,implode(',',$array_values)); $this->create_product_attr_value($find, $datum, $product_type, $attr, $merId, implode(',', $array_values));
} }
$is_update=true; $is_update = true;
} else { } else {
$datas=[ $datas = [
'product_id' => $find['product_id'], 'product_id' => $find['product_id'],
'attr_name' => $kk, 'attr_values' => $vv, 'attr_name' => $kk, 'attr_values' => $vv,
]; ];
Db::name('store_product_attr')->insert($datas); Db::name('store_product_attr')->insert($datas);
} }
} }
if($is_update==false){ if ($is_update == false) {
$this->create_product_attr_value($find,$datum,$product_type,$attr,$merId,implode(',',$array_values)); $this->create_product_attr_value($find, $datum, $product_type, $attr, $merId, implode(',', $array_values));
} }
} else { } else {
$cate_id = 0; $cate_id = 0;
$spec_type = 1; $spec_type = 1;
@ -2581,16 +2580,45 @@ class StoreOrderRepository extends BaseRepository
$cate_id = $cate_id_two; $cate_id = $cate_id_two;
} }
} }
$bar_code = (int) $datum['value']['bar_code'];
$datas = [ $bar_code_where = ['is_used' => 1, 'status' => 1, 'is_del' => 0];
"image" => "https://lihai001.oss-cn-chengdu.aliyuncs.com/public/kk/luzhou/static4/oa_app/23565656.png", $bar_code_field = 'id,store_name,bar_code,manu_address,price,stock,image,slider_image,spec,trademark,manu_name,manu_address,note';
"slider_image" => [ if ($bar_code) {
$library = Db::name('product_library')->where('bar_code', $bar_code)->where($bar_code_where)->field($bar_code_field)
->withAttr('slider_image', function ($value, $data) {
return $value ? explode(',', $value) : [];
})
->find();
if (!$library) {
$ProductLibrary = app()->make(ProductLibrary::class);
try {
$ProductLibrary->caiji($bar_code);
$library = Db::name('product_library')->where('bar_code', $bar_code)->where($bar_code_where)->field($bar_code_field)
->withAttr('slider_image', function ($value, $data) {
return $value ? explode(',', $value) : [];
})
->find();
} catch (Exception $e) {
Log::error('导入采集编码错误:' . $bar_code . '。' . $e->getMessage());
}
}
}
if($library){
$image=$library['image'];
$slider_image=$library['slider_image'];
}else{
$image='https://lihai001.oss-cn-chengdu.aliyuncs.com/public/kk/luzhou/static4/oa_app/23565656.png';
$slider_image=[
0 => "https://lihai001.oss-cn-chengdu.aliyuncs.com/public/kk/luzhou/static4/oa_app/23565656.png", 0 => "https://lihai001.oss-cn-chengdu.aliyuncs.com/public/kk/luzhou/static4/oa_app/23565656.png",
], ];
}
$datas = [
"image" =>$image,
"slider_image" =>$slider_image,
"store_name" => $datum['value']['store_name'], "store_name" => $datum['value']['store_name'],
"store_info" => $datum['value']['store_name'], "store_info" => $datum['value']['store_name'],
"keyword" => '', "keyword" => '',
"bar_code" => (int) $datum['value']['bar_code'], "bar_code" => $bar_code,
"guarantee_template_id" => "", "guarantee_template_id" => "",
"cate_id" => $cate_id, "cate_id" => $cate_id,
"mer_cate_id" => [ "mer_cate_id" => [
@ -2623,7 +2651,7 @@ class StoreOrderRepository extends BaseRepository
"pay_limit" => 0, "pay_limit" => 0,
"attrValue" => [ "attrValue" => [
[ [
"image" => "https://lihai001.oss-cn-chengdu.aliyuncs.com/public/kk/luzhou/static4/oa_app/23565656.png", "image" => $image,
"price" => $datum['value']['price'], "price" => $datum['value']['price'],
"cost" => $datum['value']['cost'], "cost" => $datum['value']['cost'],
"ot_price" => $datum['value']['price'], "ot_price" => $datum['value']['price'],
@ -2646,19 +2674,20 @@ class StoreOrderRepository extends BaseRepository
"mer_status" => 1, "mer_status" => 1,
"rate" => 3, "rate" => 3,
]; ];
$data_list = [ // $data_list = [
'data' => $datas, // 'data' => $datas,
'product_type' => $product_type, // 'product_type' => $product_type,
]; // ];
$make = app()->make(ProductRepository::class); $make = app()->make(ProductRepository::class);
$id = $make->create($datas, 98); $make->create($datas, $product_type);
// Queue::push(ProductImportJob::class, $data_list); // Queue::push(ProductImportJob::class, $data_list);
} }
} }
return true; return true;
} }
public function create_product_attr_value($find,$datum,$product_type,$attr_values,$merId,$sku){ public function create_product_attr_value($find, $datum, $product_type, $attr_values, $merId, $sku)
{
$attrValue['stock'] = 1; $attrValue['stock'] = 1;
$unique = app(ProductRepository::class)->setUnique($find['product_id'], $datum['value']['bar_code'], $product_type); $unique = app(ProductRepository::class)->setUnique($find['product_id'], $datum['value']['bar_code'], $product_type);
$attrValue['unique'] = $unique; $attrValue['unique'] = $unique;
@ -2667,7 +2696,7 @@ class StoreOrderRepository extends BaseRepository
$attrValue['mer_id'] = $merId; $attrValue['mer_id'] = $merId;
$attrValue['sku'] = $sku; $attrValue['sku'] = $sku;
$attrValue['cost'] = $datum['value']['cost']; $attrValue['cost'] = $datum['value']['cost'];
$attrValue['ot_price'] =$datum['value']['price']; $attrValue['ot_price'] = $datum['value']['price'];
$attrValue['price'] = $datum['value']['price']; $attrValue['price'] = $datum['value']['price'];
$attrValue['procure_price'] = $datum['value']['procure_price']; $attrValue['procure_price'] = $datum['value']['procure_price'];
$attrValue["image"] = "https://lihai001.oss-cn-chengdu.aliyuncs.com/public/kk/luzhou/static4/oa_app/23565656.png"; $attrValue["image"] = "https://lihai001.oss-cn-chengdu.aliyuncs.com/public/kk/luzhou/static4/oa_app/23565656.png";

View File

@ -15,7 +15,10 @@ namespace app\controller\api;
use crmeb\basic\BaseController; use crmeb\basic\BaseController;
use think\facade\Db;
use crmeb\services\UploadService;
use Exception;
use ZipArchive;
/** /**
* Class Auth * Class Auth
@ -25,8 +28,116 @@ use crmeb\basic\BaseController;
*/ */
class Demo extends BaseController class Demo extends BaseController
{ {
public function index(){ 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;
}
$files_two = scandir($directory . '/' . $file);
$image_extensions = array('jpg', 'jpeg', 'png');
$image = '';
$slider_image = [];
$details = [];
$sku_arr = [];
/**清洗图片 */
foreach ($files_two as $file_two) {
$arr = explode('.', $file_two);
if (in_array($arr[1], $image_extensions)) {
/**首图 */
$images[] = $file_two;
if ($image == '' && is_int((int)$arr[0])) {
$image = $directory . '/' . $file . '/' . $file_two;
continue;
}
/**轮播图 */
if (is_int((int)$arr[0]) && count($slider_image) < 4) {
$slider_image[] = $directory . '/' . $file . '/' . $file_two;
continue;
}
/**详情图 */
if (is_int((int)$arr[0])) {
$details[] = $directory . '/' . $file_two;
continue;
}
/**sku图 */
$sku = explode('==', $arr[0]);
if ($sku) {
$sku = implode(',', $sku);
$sku_arr[$sku] = $directory . '/' . $file . '/' . $file_two;
}
}else{
throw new \think\exception\HttpException(404, '请上传jpg,jpeg,png的图片');
}
}
$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());
}
}
}
halt(1); halt(1);
} }
} }

View File

@ -39,57 +39,6 @@ class CloudWarehouse extends BaseController
$this->spuRepository->userInfo = $this->request->isLogin() ? $this->request->userInfo() : null; $this->spuRepository->userInfo = $this->request->isLogin() ? $this->request->userInfo() : null;
} }
/**
* 指定类型的云仓商品列表
* @return mixed
*/
// public function index()
// {
// // 除了市级供应链都可以查询
// $typeIdArray = Db::name('MerchantType')->where('type_code', Merchant::TypeCode['TypePlatform'])->column('mer_type_id');
// $params = $this->request->params(['category_id', 'street_code', 'order', ['product_type', 0], 'keyword','page_num']);
// $search = [
// // 'street_id' => $params['street_code'],
// 'type_id' => $typeIdArray ?? [],
// 'status' => 1,
// 'is_del' => 0,
// 'mer_state' => 1,
// ];
// if (!empty($params['category_id'])) {
// $search['category_id'] = $params['category_id'];
// }
// $merchantIds = $this->merchantDao->search($search)->column('mer_id');
// [$page, $limit] = $this->getPage();
// if(isset($params['page_num'])&&$params['page_num']!=''){
// $page=$params['page_num'];
// }
// if (empty($merchantIds)) {
// return app('json')->success(['count' => 0, 'list' => []]);
// }
// // 隐藏镇级云仓
// // $typeCloudWarehouseId = Db::name('MerchantType')->where('type_code', Merchant::TypeCode['TypeCloudWarehouse'])->value('mer_type_id');
// // $entryWhere = [
// // 'street_id' => $params['street_code'],
// // 'type_id' => $typeCloudWarehouseId,
// // 'status' => 1,
// // 'is_del' => 0,
// // 'mer_state' => 1,
// // ];
// // if (!empty($params['category_id'])) {
// // $entryWhere['category_id'] = $params['category_id'];
// // }
// // $where['entry_mer_id'] = $this->merchantDao->search($entryWhere)->value('mer_id');
// $where['entry_mer_id'] = 0;
// $where['keyword'] = $params['keyword'];
// $where['mer_ids'] = $merchantIds;
// $where['product_type'] = $params['product_type'];
// $where['is_gift_bag'] = 0;
// $where['order'] = $params['order'] ?: 'sort';
// $products = $this->spuRepository->getApiCloudSearch($where, $page, $limit, false);
// return app('json')->success($products);
// }
/** /**
* type_id 13云仓商品列表 * type_id 13云仓商品列表
* @return mixed * @return mixed
@ -133,10 +82,9 @@ class CloudWarehouse extends BaseController
'is_del' => 0, 'is_del' => 0,
'mer_state' => 1, 'mer_state' => 1,
]; ];
if (!empty($params['category_id'])) {
$search['category_id'] = $params['category_id'];
}
$merchantIds = $this->merchantDao->search($search)->column('mer_id'); $merchantIds = $this->merchantDao->search($search)->column('mer_id');
[$page, $limit] = $this->getPage(); [$page, $limit] = $this->getPage();
if (isset($params['page']) && $params['page'] != '') { if (isset($params['page']) && $params['page'] != '') {
$page = $params['page']; $page = $params['page'];
@ -150,6 +98,9 @@ class CloudWarehouse extends BaseController
$where['product_type'] = $params['product_type']; $where['product_type'] = $params['product_type'];
$where['is_gift_bag'] = 0; $where['is_gift_bag'] = 0;
$where['order'] = $params['order'] ?: 'sort'; $where['order'] = $params['order'] ?: 'sort';
if (!empty($params['category_id'])) {
$where['category_id'] = $params['category_id'];
}
$products = $this->spuRepository->getApiSearch($where, $page, $limit, false,true); $products = $this->spuRepository->getApiSearch($where, $page, $limit, false,true);
return app('json')->success($products); return app('json')->success($products);
} }

View File

@ -15,13 +15,14 @@ use app\common\repositories\store\ExcelRepository;
use app\common\repositories\store\order\StoreImportDeliveryRepository; use app\common\repositories\store\order\StoreImportDeliveryRepository;
use app\common\repositories\store\order\StoreOrderRepository; use app\common\repositories\store\order\StoreOrderRepository;
use crmeb\jobs\ImportSpreadsheetExcelJob; use crmeb\jobs\ImportSpreadsheetExcelJob;
use crmeb\jobs\ImportPicJob;
use crmeb\services\ExcelService; use crmeb\services\ExcelService;
use crmeb\services\SpreadsheetExcelService; use crmeb\services\SpreadsheetExcelService;
use crmeb\services\UploadService; use crmeb\services\UploadService;
use think\App; use think\App;
use crmeb\basic\BaseController; use crmeb\basic\BaseController;
use app\common\repositories\store\order\StoreImportRepository; use app\common\repositories\store\order\StoreImportRepository;
use ZipArchive;
use think\facade\Db; use think\facade\Db;
use think\facade\Queue; use think\facade\Queue;
@ -72,7 +73,7 @@ class StoreImport extends BaseController
} }
/** /**
* TODO 导入excel信息 * TODO 导入excel商品信息
* @return \think\response\Json * @return \think\response\Json
* @author Qinii * @author Qinii
* @day 3/16/21 * @day 3/16/21
@ -159,6 +160,26 @@ class StoreImport extends BaseController
return app('json')->fail('数据类型错误'); return app('json')->fail('数据类型错误');
} }
/**
* 导入商品图片
*/
public function import_images(){
$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 = $this->request->merId();
Queue::push(ImportPicJob::class,['mer_id'=>$mer_id,'path'=>$destination_path]);
}
public function getXlsList($type_id,$path){ public function getXlsList($type_id,$path){
if($type_id==12){ if($type_id==12){
$check =[ $check =[

113
crmeb/jobs/ImportPicJob.php Normal file
View File

@ -0,0 +1,113 @@
<?php
namespace crmeb\jobs;
use think\facade\Db;
use crmeb\services\UploadService;
use Exception;
use crmeb\interfaces\JobInterface;
use think\facade\Log;
class ImportPicJob implements JobInterface
{
public function fire($job, $data)
{
$directory = $data['path'];
$mer_id = $data['mer_id'];
$files = scandir($directory);
$dir = 'def/' . date('Y-m-d');
$upload = UploadService::create();
/**循环目录 */
foreach ($files as $file) {
if ($file === '.' || $file === '..' ||$file ==='__MACOSX') {
continue;
}
$files_two = scandir($directory . '/' . $file);
$image_extensions = array('jpg', 'jpeg', 'png');
$image = '';
$slider_image = [];
$details = [];
$sku_arr = [];
/**清洗图片 */
foreach ($files_two as $file_two) {
$arr = explode('.', $file_two);
if (in_array($arr[1], $image_extensions)) {
/**首图 */
$images[] = $file_two;
if ($image == '' && is_int((int)$arr[0])) {
$image = $directory . '/' . $file . '/' . $file_two;
continue;
}
/**轮播图 */
if (is_int((int)$arr[0]) && count($slider_image) < 4) {
$slider_image[] = $directory . '/' . $file . '/' . $file_two;
continue;
}
/**详情图 */
if (is_int((int)$arr[0])) {
$details[] = $directory . '/' . $file_two;
continue;
}
/**sku图 */
$sku = explode('==', $arr[0]);
if ($sku) {
$sku = implode(',', $sku);
$sku_arr[$sku] = $directory . '/' . $file . '/' . $file_two;
}
}else{
throw new \think\exception\HttpException(404, '请上传jpg,jpeg,png的图片');
}
}
$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) {
Log::error('导入商品图片失败'.$e->getMessage().'line:'.$e->getLine());
}
}
}
}
}