diff --git a/server/app/admin/view/goods/goods/import.html b/server/app/admin/view/goods/goods/import.html index 0c7a355d..535fd4f6 100644 --- a/server/app/admin/view/goods/goods/import.html +++ b/server/app/admin/view/goods/goods/import.html @@ -71,10 +71,10 @@ console.log(res,'res') layer.closeAll() //假设code=0代表上传成功 + layer.msg(res.msg) if(res.code === 1){ //do something (比如将res返回的图片链接保存到表单的隐藏域) } else { - layer.msg(res.msg) } //获取当前触发上传的元素,一般用于 elem 绑定 class 的情况,注意:此乃 layui 2.1.0 新增 diff --git a/server/app/common/model/goods/Goods.php b/server/app/common/model/goods/Goods.php index d21ff855..6b13c851 100644 --- a/server/app/common/model/goods/Goods.php +++ b/server/app/common/model/goods/Goods.php @@ -22,6 +22,7 @@ namespace app\common\model\goods; use app\common\basics\Models; use app\common\model\distribution\DistributionGoods; +use Overtrue\Pinyin\Pinyin; use think\facade\Db; @@ -209,14 +210,15 @@ class Goods extends Models // 处理组图 public static function handleImages() { - // 查询商品表images不为空的记录,批量插入到goods_image表,再将这些images字段置空 + // 查询商品表images不为空的记录,批量插入到goods_image表 $needHandleList = self::whereNotNull('images')->column('id,images'); - $goodsIds = []; $insertImage = []; foreach ($needHandleList as $item) { - $goodsIds[] = $item['id']; $imageArr = explode(',', $item['images']); foreach ($imageArr as $img) { + if (empty($img)) { + continue; + } $insertImage[] = [ 'goods_id' => $item['id'], 'uri' => $img, @@ -224,9 +226,6 @@ class Goods extends Models } } GoodsImage::limit(2000)->insertAll($insertImage); - self::whereIn('id', $goodsIds)->save([ - 'images' => null - ]); } // 批量插入没有的品牌 @@ -243,7 +242,9 @@ class Goods extends Models continue; } if (!in_array($item, $allBrandName)) { - $insert[] = ['name' =>$item, 'create_time'=> $now, 'image' => '', 'initial' => '']; + $word = Pinyin::abbr($item)->join(); + $word = substr($word, 0, 1); + $insert[] = ['name' =>$item, 'create_time'=> $now, 'image' => '', 'initial' => strtoupper($word)]; } } GoodsBrand::limit(2000)->insertAll($insert); @@ -281,7 +282,6 @@ class Goods extends Models $insert = []; $now = time(); - foreach ($needHandleList1 as $item) { if (empty($item)) { continue; @@ -296,7 +296,7 @@ class Goods extends Models continue; } if (!in_array($item['second_cate'], $allName)) { - $insert[] = ['name' =>$item['first_cate'], 'pid' => 0, 'level' => 2,'create_time'=> $now, 'parent_name' => $item['first_cate']]; + $insert[] = ['name' =>$item['second_cate'], 'pid' => 0, 'level' => 2,'create_time'=> $now, 'parent_name' => $item['first_cate']]; } } @@ -309,7 +309,9 @@ class Goods extends Models } } - GoodsCategory::limit(2000)->insertAll($insert); + if (!empty($insert)) { + GoodsCategory::limit(2000)->insertAll($insert); + } // 将level > 1但是pid=0且parent_name不为空的记录批量更新 GoodsCategory::alias('main') @@ -317,7 +319,7 @@ class Goods extends Models ->where('main.level', '>', 1) ->where('main.pid', 0) ->whereNotNull('main.parent_name') - ->update(['main.pid' => 'parent.id']); + ->update(['main.pid' => Db::raw('`parent`.id')]); } public static function handleSelfInfo() @@ -370,9 +372,9 @@ class Goods extends Models 'name' => $item['name'], 'shop_id' => $shopId, - 'first_cate' => $item['first_cate'], - 'second_cate' => $item['second_cate'], - 'third_cate' => $item['third_cate'], + 'first_cate' => $item['first_cate'] ?: null, + 'second_cate' => $item['second_cate'] ?: null, + 'third_cate' => $item['third_cate'] ?: null, 'brand_name' => $item['brand'], 'unit_name' => $item['unit'], 'images' => $item['images'], @@ -380,12 +382,12 @@ class Goods extends Models 'express_money' =>0, - 'image' => $item['image'], - 'content' => $item['content'], + 'image' => $item['image'] ?: '', + 'content' => $item['content'] ?: '', 'spec_type' => 1,//1 统一规格 - 'max_price' => $item['price'], - 'min_price' => $item['price'], - 'stock' => $item['stock'], + 'max_price' => $item['price'] ?: 0, + 'min_price' => $item['price'] ?: 0, + 'stock' => $item['stock'] ?: 1, 'create_time' => $now, 'update_time' => $now, 'custom_params' => $item['custom_params'],