to_store_id; $from_store_id = $this->from_store_id; $diy_id = $this->diy_id; if (is_array($to_store_id)) { if (!is_array($diy_id)) { $diy_id = [$diy_id]; } foreach ($to_store_id as $store_id_item) { $this->copyHome($from_store_id, $store_id_item, $diy_id); } } } private function copyHome($from_store_id, $to_store_id, $diy_id) { try { foreach ($diy_id as $diy_item) { //判断是否为当前商城同步过此商品 $fromIndex_ = NewDiyTemplate::findOne([ 'id' => $diy_item, 'store_id' => $from_store_id, ]); if (intval($fromIndex_->is_index)) { NewDiyTemplate::updateAll(['is_index' => 0], ['is_delete' => 0, 'is_index' => 1, 'store_id' => $to_store_id]); } $template = \json_decode($fromIndex_->template, true); if ($template) { if ($fromIndex_->name === 'controls') { foreach ($template as &$controlsItem) { if (!empty($controlsItem['params'])) { if (!empty($controlsItem['params']['navList'])) { foreach ($controlsItem['params']['navList'] as &$nav_item) { $nav_item = $this->replaceLink($nav_item, $from_store_id, $to_store_id); } } } } } else { if (!empty($template['templates'])) { foreach ($template['templates'] as &$index_item) { $index_item = $this->diyHandleCommon($index_item, $from_store_id, $to_store_id); } } } //判断是否为当前商城同步过此商品 $storeSyncExtLog = StoreSyncExtLog::findOne([ 'from_store_id' => $from_store_id, 'to_store_id' => $to_store_id, 'type' => StoreSyncExtLog::TYPE_DIY, 'from_id' => $diy_item ]); $toIndex = NewDiyTemplate::findOne($storeSyncExtLog->to_id ?? 0); if ($fromIndex_->name === 'controls') { $toIndex = NewDiyTemplate::findOne(['name' => 'controls', 'is_delete' => 0, 'store_id' => $to_store_id]); } $toIndex = $toIndex ?: new NewDiyTemplate(); $toIndex->store_id = $to_store_id; $toIndex->template = json_encode($template); $toIndex->addtime = time(); $toIndex->name = $fromIndex_->name; $toIndex->is_index = $fromIndex_->is_index; $toIndex->type = $fromIndex_->type; $toIndex->is_delete = $fromIndex_->is_delete; if (!$toIndex->save()) { throw new \Exception(json_encode($toIndex->errors, JSON_UNESCAPED_UNICODE)); } (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $diy_item, $toIndex->id, StoreSyncExtLog::TYPE_DIY); } } } catch (\Exception $e) { } } //处理装修公用部分 public function diyHandleCommon($template, $from_store_id, $to_store_id) { // 头部导航 if ($template['type'] == 'fastNav') { $template = $this->replaceFastNav($template, $from_store_id, $to_store_id); } // 图片轮播 if ($template['type'] == 'banner') { $template = $this->replaceBanner($template, $from_store_id, $to_store_id); } // 魔方导航 if ($template['type'] == 'cubeNav') { $template = $this->replaceCubeNav($template, $from_store_id, $to_store_id); } // 魔方图片 if ($template['type'] == 'container') { $template = $this->replaceContainer($template, $from_store_id, $to_store_id); } // 产品展示 if ($template['type'] == 'productShow') { $template = $this->replaceProductShow($template, $from_store_id, $to_store_id); } //产品分类 if ($template['type'] == 'productClassify') { $template = $this->replaceProductClassify($template, $from_store_id, $to_store_id); } // 倒计时 if ($template['type'] == 'marketCountDown') { $template = $this->replaceMarketCountDown($template, $from_store_id, $to_store_id); } //图片 if ($template['type'] == 'images') { $template = $this->replaceImages($template, $from_store_id, $to_store_id); } // 首页推荐 if ($template['type'] == 'indexRecommend') { // Todo 该模块数据有误,后期解决 $template = $this->replaceIndexRecommend($template, $from_store_id, $to_store_id); } //秒杀 if ($template['type'] == 'productSeckill') { $template = $this->replaceProductSeckill($template, $from_store_id, $to_store_id); } //上门服务 if ($template['type'] == 'serviceHome') { $template = $this->replaceServiceHome($template, $from_store_id, $to_store_id); } //拼团 if ($template['type'] == 'ptProductShow') { $template = $this->replacePtProductShow($template, $from_store_id, $to_store_id); } //预约 if ($template['type'] == 'bookingHome') { $template = $this->replaceBookingHome($template, $from_store_id, $to_store_id); } return $template; } // 替换头部导航 private function replaceFastNav($data, $from_store_id, $to_store_id) { if (!empty($data['params']['list'])) { foreach ($data['params']['list'] as &$item) { $item = $this->replaceLink($item, $from_store_id, $to_store_id); } } return $data; } // 替换图片轮播 private function replaceBanner($data, $from_store_id, $to_store_id) { if (!empty($data['params']['bannerList'])) { foreach ($data['params']['bannerList'] as &$item) { $item = $this->replaceLink($item, $from_store_id, $to_store_id); } } return $data; } // 替换魔方导航 private function replaceCubeNav($data, $from_store_id, $to_store_id) { if (!empty($data['params']['list'])) { foreach ($data['params']['list'] as &$item) { $item = $this->replaceLink($item, $from_store_id, $to_store_id); } } return $data; } // 替换魔方图片 private function replaceContainer($data, $from_store_id, $to_store_id) { debug_log('——替换魔方图片','store_sync.log'); if (!empty($data['params']['cubeCoverList'])) { debug_log('替换魔方图片——','store_sync.log'); foreach ($data['params']['cubeCoverList'] as &$item) { $item = $this->replaceLink($item, $from_store_id, $to_store_id); } } return $data; } // 替换产品展示 private function replaceProductShow($data, $from_store_id, $to_store_id) { if (isset($data['params']['classifyData'])) { //判断是否为当前商城同步过此商品 $storeSyncExtLog = StoreSyncExtLog::findOne([ 'from_store_id' => $from_store_id, 'to_store_id' => $to_store_id, 'type' => StoreSyncExtLog::TYPE_PRODUCT_CAT, 'from_id' => $data['params']['classifyData'] ]); // 替换分类id if (isset($storeSyncExtLog->to_id)) { $data['params']['classifyData'] = $storeSyncExtLog->to_id; } } if (isset($data['params']['classifyArr'])) { foreach ($data['params']['classifyArr'] as &$item) { $storeSyncExtLog = StoreSyncExtLog::findOne([ 'from_store_id' => $from_store_id, 'to_store_id' => $to_store_id, 'type' => StoreSyncExtLog::TYPE_PRODUCT, 'from_id' => $item['id'] ]); // 替换分类id if (isset($storeSyncExtLog->to_id)) { $item['id'] = $storeSyncExtLog->to_id; } } } // 替换商品id if (isset($data['params']['selectData'])) { foreach ($data['params']['selectData'] as &$select_item) { //判断是否为当前商城同步过此商品 $storeSyncExtLog = StoreSyncExtLog::findOne([ 'from_store_id' => $from_store_id, 'to_store_id' => $to_store_id, 'type' => StoreSyncExtLog::TYPE_PRODUCT, 'from_id' => $select_item['id'] ]); if (isset($storeSyncExtLog->to_id)) { $select_item['id'] = $storeSyncExtLog->to_id; } } } $data['params'] = $this->replaceLink($data['params'], $from_store_id, $to_store_id); return $data; } // public function replaceIndexRecommend($template, $from_store_id, $to_store_id) { try { foreach ($template['params']['list'] as &$item) { foreach ($item['pics']['link'] as &$link_item) { if (!isset($link_item['link'])) { continue; } // 处理分类id if ( strpos($link_item['link'], '/pages/cat/cat?cat_id=') !== false && $link_item['params'][0]['value'] != '' ) { //判断是否为当前商城同步过此商品 $storeSyncExtLog = StoreSyncExtLog::findOne([ 'from_store_id' => $from_store_id, 'to_store_id' => $to_store_id, 'type' => StoreSyncExtLog::TYPE_PRODUCT_CAT, 'from_id' => $link_item['params'][0]['value'] ]); $cat_id = $link_item['params'][0]['value']; // 替换分类id if (isset($storeSyncExtLog->to_id)) { $cat_id = $storeSyncExtLog->to_id; } $link_item['link'] = '/pages/cat/cat?cat_id=' . $cat_id; $link_item['params'][0]['value'] = $cat_id; } // 处理商品列表 if ( strpos($link_item['link'], '/other/list/list?cat_id=') !== false && $link_item['params'][0]['value'] != '' ) { //判断是否为当前商城同步过此商品 $storeSyncExtLog = StoreSyncExtLog::findOne([ 'from_store_id' => $from_store_id, 'to_store_id' => $to_store_id, 'type' => StoreSyncExtLog::TYPE_PRODUCT_CAT, 'from_id' => $link_item['params'][0]['value'] ]); $cat_id = $link_item['params'][0]['value']; // 替换分类id if (isset($storeSyncExtLog->to_id)) { $cat_id = $storeSyncExtLog->to_id; } $link_item['link'] = '/other/list/list?cat_id=' . $cat_id; $link_item['params'][0]['value'] = $cat_id; } // 处理商品详情 if ( strpos($link_item['link'], '/goods/goods/goods?id=') !== false && $link_item['params'][0]['value'] != '' ) { //判断是否为当前商城同步过此商品 $storeSyncExtLog = StoreSyncExtLog::findOne([ 'from_store_id' => $from_store_id, 'to_store_id' => $to_store_id, 'type' => StoreSyncExtLog::TYPE_PRODUCT, 'from_id' => $link_item['params'][0]['value'] ]); $goods_id = $link_item['params'][0]['value']; // 替换分类id if (isset($storeSyncExtLog->to_id)) { $goods_id = $storeSyncExtLog->to_id; } $link_item['link'] = '/goods/goods/goods?id=' . $goods_id; $link_item['params'][0]['value'] = $goods_id; } if ( strpos($link_item['link'], '/pages/diy/diy?id=') !== false ) { //判断是否为当前商城同步过此商品 $storeSyncExtLog = StoreSyncExtLog::findOne([ 'from_store_id' => $from_store_id, 'to_store_id' => $to_store_id, 'type' => StoreSyncExtLog::TYPE_DIY, 'from_id' => $link_item['id'] ]); $diy_id = $link_item['id']; // 替换分类id if (isset($storeSyncExtLog->to_id)) { $diy_id = $storeSyncExtLog->to_id; } $link_item['link'] = '/pages/diy/diy?id=' . $diy_id; $link_item['id'] = $diy_id; } } } return $template; } catch (\Exception $e) { return $template; } } // 替换产品分类 private function replaceProductClassify($data, $from_store_id, $to_store_id) { if (!empty($data['params']['classifyList'])) { $data['params']['classifyList'] = (array)$data['params']['classifyList']; foreach ($data['params']['classifyList'] as &$item) { // 替换分类 if (isset($item['classifyId'])) { $storeSyncExtLog = StoreSyncExtLog::findOne([ 'from_store_id' => $from_store_id, 'to_store_id' => $to_store_id, 'type' => StoreSyncExtLog::TYPE_PRODUCT_CAT, 'from_id' => $item['classifyId'] ]); if (isset($storeSyncExtLog->to_id)) { $item['id'] = $storeSyncExtLog->to_id; $item['classifyId'] = $storeSyncExtLog->to_id; } } if (!empty($item['data'])) { // 替换商品id foreach ($item['data'] as &$value) { if (isset($value['id'])) { $storeSyncExtLog = StoreSyncExtLog::findOne([ 'from_store_id' => $from_store_id, 'to_store_id' => $to_store_id, 'type' => StoreSyncExtLog::TYPE_PRODUCT, 'from_id' => $value['id'] ]); if (isset($storeSyncExtLog->to_id)) { $value['id'] = $storeSyncExtLog->to_id; } } } } } } return $data; } //秒杀 private function replaceProductSeckill($template, $from_store_id, $to_store_id) { if (isset($template['params']['activeId'])) { $storeSyncExtLog = StoreSyncExtLog::findOne([ 'from_store_id' => $from_store_id, 'to_store_id' => $to_store_id, 'type' => StoreSyncExtLog::TYPE_SECKILL, 'from_id' => $template['params']['activeId'] ]); if (isset($storeSyncExtLog->to_id)) { $template['params']['activeId'] = (string)$storeSyncExtLog->to_id; } } return $template; } //上门服务 private function replaceServiceHome($template, $from_store_id, $to_store_id) { if (isset($template['params']['classifyData'])) { $storeSyncExtLog = StoreSyncExtLog::findOne([ 'from_store_id' => $from_store_id, 'to_store_id' => $to_store_id, 'type' => StoreSyncExtLog::TYPE_WORKER_CONFIG_CAT, 'from_id' => $template['params']['classifyData'] ]); if (isset($storeSyncExtLog->to_id)) { $template['params']['classifyData'] = $storeSyncExtLog->to_id; } } if (isset($template['params']['classifyArr'])) { foreach ($template['params']['classifyArr'] as &$worker_goods_item) { $storeSyncExtLog = StoreSyncExtLog::findOne([ 'from_store_id' => $from_store_id, 'to_store_id' => $to_store_id, 'type' => StoreSyncExtLog::TYPE_PRODUCT, 'from_id' => $worker_goods_item['id'] ]); if (isset($storeSyncExtLog->to_id)) { $worker_goods_item['id'] = $storeSyncExtLog->to_id; } } } return $template; } //拼团 private function replacePtProductShow($template, $from_store_id, $to_store_id) { if (!empty($template['params'])) { if (isset($template['params']['classifyData'])) { $storeSyncExtLog = StoreSyncExtLog::findOne([ 'from_store_id' => $from_store_id, 'to_store_id' => $to_store_id, 'type' => StoreSyncExtLog::TYPE_PT_GOODS_CAT, 'from_id' => $template['params']['classifyData'] ]); if (isset($storeSyncExtLog->to_id)) { $template['params']['classifyData'] = $storeSyncExtLog->to_id; } } if (!empty($template['params']['classifyArr'])) { foreach ($template['params']['classifyArr'] as &$pt_classify_goods_item) { $storeSyncExtLog = StoreSyncExtLog::findOne([ 'from_store_id' => $from_store_id, 'to_store_id' => $to_store_id, 'type' => StoreSyncExtLog::TYPE_PRODUCT, 'from_id' => $pt_classify_goods_item['id'] ]); if (isset($storeSyncExtLog->to_id)) { $pt_classify_goods_item['id'] = $storeSyncExtLog->to_id; } } } if (!empty($template['params']['selectData'])) { foreach ($template['params']['selectData'] as &$pt_goods_item) { $storeSyncExtLog = StoreSyncExtLog::findOne([ 'from_store_id' => $from_store_id, 'to_store_id' => $to_store_id, 'type' => StoreSyncExtLog::TYPE_PRODUCT, 'from_id' => $pt_goods_item['id'] ]); if (isset($storeSyncExtLog->to_id)) { $pt_goods_item['id'] = $storeSyncExtLog->to_id; } } } } return $template; } //装修预约商品 private function replaceBookingHome($template, $from_store_id, $to_store_id) { try { if (!empty($template['params'])) { if (isset($template['params']['classifyData'])) { $storeSyncExtLog = StoreSyncExtLog::findOne([ 'from_store_id' => $from_store_id, 'to_store_id' => $to_store_id, 'type' => StoreSyncExtLog::TYPE_BOOKING_GOODS_CAT, 'from_id' => $template['params']['classifyData'] ]); if (isset($storeSyncExtLog->to_id)) { $template['params']['classifyData'] = $storeSyncExtLog->to_id; } } if (!empty($template['params']['selectData'])) { foreach ($template['params']['selectData'] as &$booking_goods_item) { $storeSyncExtLog = StoreSyncExtLog::findOne([ 'from_store_id' => $from_store_id, 'to_store_id' => $to_store_id, 'type' => StoreSyncExtLog::TYPE_PRODUCT, 'from_id' => $booking_goods_item['id'] ]); if (isset($storeSyncExtLog->to_id)) { $booking_goods_item['id'] = $storeSyncExtLog->to_id; } } } } return $template; } catch (\Exception $e) { return $template; } } // 替换倒计时 private function replaceMarketCountDown($data, $from_store_id, $to_store_id) { $data['params'] = $this->replaceLink($data['params'], $from_store_id, $to_store_id); return $data; } // 替换图片链接 private function replaceImages($data, $from_store_id, $to_store_id) { if (isset($data['params']['current'])) { $data['params']['current'] = $this->replaceLink($data['params']['current'], $from_store_id, $to_store_id); } else { if (isset($data['params'])) { $data['params'] = $this->replaceLink($data['params'], $from_store_id, $to_store_id); } } return $data; } private function replaceLink($item, $from_store_id, $to_store_id) { try { if (!isset($item['link']['link'])) { return $item; } if (isset($item['link']['params'][0]['value'])) { //判断是否为当前商城同步过此商品 $storeSyncExtLog = StoreSyncExtLog::findOne([ 'from_store_id' => $from_store_id, 'to_store_id' => $to_store_id, 'type' => StoreSyncExtLog::TYPE_PRODUCT_CAT, 'from_id' => $item['link']['params'][0]['value'] ]); // 处理分类id if ( str_contains($item['link']['link'], '/pages/cat/cat?cat_id=') && $item['link']['params'][0]['value'] != '' && isset($storeSyncExtLog->to_id) ) { $item['link']['link'] = '/pages/cat/cat?cat_id=' . $storeSyncExtLog->to_id; $item['link']['params'][0]['value'] = $storeSyncExtLog->to_id; } } // 处理商品列表 if (strpos($item['link']['link'], '/other/list/list?cat_id=') !== false) { } if (isset($item['link']['params'][0]['value'])) { //判断是否为当前商城同步过此商品 $storeSyncExtLog = StoreSyncExtLog::findOne([ 'from_store_id' => $from_store_id, 'to_store_id' => $to_store_id, 'type' => StoreSyncExtLog::TYPE_PRODUCT_CAT, 'from_id' => $item['link']['params'][0]['value'] ]); if ( str_contains($item['link']['link'], '/other/list/list?cat_id=') && $item['link']['params'][0]['value'] != '' && isset($storeSyncExtLog->to_id) ) { $item['link']['link'] = '/other/list/list?cat_id=' . $storeSyncExtLog->to_id; $item['link']['params'][0]['value'] = $storeSyncExtLog->to_id; } } if (isset($item['link']['params'][0]['value'])) { //判断是否为当前商城同步过此商品 $storeSyncExtLog = StoreSyncExtLog::findOne([ 'from_store_id' => $from_store_id, 'to_store_id' => $to_store_id, 'type' => StoreSyncExtLog::TYPE_PRODUCT, 'from_id' => $item['link']['params'][0]['value'] ]); // 处理商品详情 if ( str_contains($item['link']['link'], '/goods/goods/goods?id=') && $item['link']['params'][0]['value'] != '' && isset($storeSyncExtLog->to_id) ) { $item['link']['link'] = '/goods/goods/goods?id=' . $storeSyncExtLog->to_id; $item['link']['params'][0]['value'] = $storeSyncExtLog->to_id; } } if (isset($item['link']['id'])) { //判断是否为当前商城同步过此商品 $storeSyncExtLog = StoreSyncExtLog::findOne([ 'from_store_id' => $from_store_id, 'to_store_id' => $to_store_id, 'type' => StoreSyncExtLog::TYPE_DIY, 'from_id' => $item['link']['id'] ]); if ( str_contains($item['link']['link'], '/pages/diy/diy?id=') && isset($storeSyncExtLog->to_id) ) { $item['link']['link'] = '/pages/diy/diy?id=' . $storeSyncExtLog->to_id; $item['link']['id'] = $storeSyncExtLog->to_id; } } } catch (\Exception $e) { debug_log([$e->getMessage()], 'syncDiy.log'); } return $item; } /** 同步DIY end */ }