| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690 |
- <?php
- namespace app\jobs\storeSync;
- use app\models\NewDiyTemplate;
- use app\models\StoreSyncExtLog;
- use yii\base\BaseObject;
- use yii\queue\JobInterface;
- //同步装修
- class DiyJob extends BaseObject implements JobInterface
- {
- public array $to_store_id;
- public int $from_store_id;
- public array $diy_id;
- public function execute($queue)
- {
- $to_store_id = $this->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 */
- }
|