| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925 |
- <?php
- /**
- * 厦门云联储网络科技有限公司
- * https://www.baokuaiyun.com
- * Copyright (c) 2023 爆块云 All rights reserved.
- */
- namespace app\models;
- use app\modules\admin\models\GoodsForm;
- use app\modules\admin\models\MerchantForm;
- use Yii;
- use yii\behaviors\TimestampBehavior;
- use yii\db\ActiveRecord;
- /**
- * This is the model class for table "{{%store_cloud}}".
- *
- * @property integer $id
- */
- class StoreCopy extends \yii\db\ActiveRecord
- {
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return '{{%store_copy}}';
- }
- public function behaviors()
- {
- return [
- [
- 'class' => TimestampBehavior::class,
- 'attributes' => [
- ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
- ]
- ]
- ];
- }
- public static function doCopyStatus($id = 0, $doCopy = 0) {
- $model = self::findOne($id);
- $type = $model->type;
- $type = json_decode($type, true);
- if($model->finished != 1 && $doCopy){
- self::doCopy($model->from_store_id, $model->to_store_id, $type, $id);
- }
- $model = self::findOne($id);
- return [
- 'code' => 0,
- 'msg' => 'ok',
- 'data' => [
- 'model' => $model,
- ],
- ];
- }
- // type //home/category/product/center/all
- public static function doCopy($from_store_id = 0, $to_store_id = 0, $type = ['home'], $id = 0) {
- if($from_store_id == $to_store_id){
- return [
- 'code' => 1,
- 'msg' => '不能选择自己店铺的模板',
- ];
- }
- $res = [
- 'code' => 0,
- 'msg' => 'success',
- ];
- if($id){
- $model = self::findOne($id);
- }else{
- $type = json_encode($type);
- $model = new self();
- $model->from_store_id = $from_store_id;
- $model->to_store_id = $to_store_id;
- $model->type = $type;
- $model->save();
- }
- $res['data'] = $model;
- if(!$id){
- // return $res;
- }
- if($model->finished == 1){
- return $res;
- }
- $finished = 1;
- $type = $model->type;
- $type = json_decode($type, true);
- if(!$id){
- if(array_intersect(['delete_goods'], $type)){
- $copy = $model->doDelGoods($to_store_id);
- //debug_log(['doDelGoods' => $finished, 'copy' => $copy], 'store_copy.log');
- if(!$copy && $finished){
- $finished = 0;
- }
- }
- if(array_intersect(['delete_cat'], $type)){
- $copy = $model->doDelCat($to_store_id);
- //debug_log(['doDelCat' => $finished, 'copy' => $copy], 'store_copy.log');
- if(!$copy && $finished){
- $finished = 0;
- }
- }
- }
- if(array_intersect(['home', 'all'], $type)){
- $copy = $model->doCopyHome();
- //debug_log(['doCopyHome' => $finished, 'copy' => $copy], 'store_copy.log');
- if(!$copy && $finished){
- $finished = 0;
- }
- }
- if(array_intersect(['center', 'all'], $type)){
- $copy = $model->doCopyCenter();
- //debug_log(['doCopyCenter' => $finished, 'copy' => $copy], 'store_copy.log');
- if(!$copy && $finished){
- $finished = 0;
- }
- }
- if(array_intersect(['footnav', 'all'], $type)){
- $copy = $model->doCopyFootnav();
- //debug_log(['doCopyFootnav' => $finished, 'copy' => $copy], 'store_copy.log');
- if(!$copy && $finished){
- $finished = 0;
- }
- }
- if(array_intersect(['category', 'all'], $type)){
- $copy = $model->doCopyCategory();
- //debug_log(['doCopyCategory' => $finished, 'copy' => $copy], 'store_copy.log');
- if(!$copy && $finished){
- $finished = 0;
- }
- }
- if(array_intersect(['product', 'all'], $type)){
- $copy = $model->doCopyProduct();
- //debug_log(['doCopyProduct' => $finished, 'copy' => $copy], 'store_copy.log');
- if(!$copy && $finished){
- $finished = 0;
- }
- }
- $model->finished = $finished;
- $model->save();
- return $res;
- }
- public function doDelGoods($store_id) {
- Goods::updateAll(['is_delete' => 1], ['store_id' => $store_id]);
- return true;
- }
- public function doDelCat($store_id) {
- Cat::updateAll(['is_delete' => 1], ['store_id' => $store_id]);
- return true;
- }
- public function doCopyHome() {
- $home_list = json_decode($this->home_list, true);
- if(!empty($home_list['from']) && !empty($home_list['to'])){
- if(count($home_list['from']) == count($home_list['to'])){
- return true;
- }
- }
- $params = [
- 'is_delete' => 0,
- 'is_index' => 1,
- 'store_id' => $this->from_store_id,
- ];
- $template = NewDiyTemplate::findOne($params);
- $ids = [$template->id];
- // $controls = NewDiyTemplate::find()
- // ->where([
- // 'store_id' => $this->from_store_id,
- // 'is_delete' => 0,
- // 'type' => 100
- // ])->one();
- // $ids = [$controls->id];
- $home_list = [
- 'from' => $ids,
- ];
- $attr = $template->attributes;
- $params = [
- 'is_delete' => 0,
- 'is_index' => 1,
- 'store_id' => $this->to_store_id,
- ];
- $template = NewDiyTemplate::findOne($params);
- if ($template) {
- $template->is_index = 0;
- $template->save();
- }
-
- unset($attr['id']);
- $attr['store_id'] = $this->to_store_id;
- $model = new NewDiyTemplate();
- $model->attributes = $attr;
- $model->save();
- $ids = [$model->id];
- // $attr = $controls->attributes;
- // unset($attr['id']);
- // $attr['store_id'] = $this->to_store_id;
- // $model = new NewDiyTemplate();
- // $model->attributes = $attr;
- // $model->save();
- // $ids[] = $model->id;
- $home_list['to'] = $ids;
- $this->home_list = json_encode($home_list);
- $this->save();
- return true;
- }
- public function doCopyCenter() {
- $center_list = json_decode($this->center_list, true);
- if(!empty($center_list['from']) && !empty($center_list['to'])){
- if(count($center_list['from']) == count($center_list['to'])){
- return true;
- }
- }
- $list = NewDiyTemplate::find()
- ->where([
- 'store_id' => $this->from_store_id,
- 'is_delete' => 0,
- 'name' => 'system'
- ])->orderBy(['id' => SORT_ASC])->asArray()->all();
- $template = null;
- foreach ($list as $value) {
- $itemp = json_decode($value['template'], true);
- if($itemp['system_type'] == 'my'){
- $template = $value;
- break;
- }
- }
- $ids = [$template['id']];
- $center_list = [
- 'from' => $ids,
- ];
- $attr = $template;
- $list = NewDiyTemplate::find()
- ->where([
- 'store_id' => $this->to_store_id,
- 'is_delete' => 0,
- 'name' => 'system'
- ])->orderBy(['id' => SORT_ASC])->all();
- foreach ($list as $value) {
- $itemp = json_decode($value['template'], true);
- if($itemp['system_type'] == 'my'){
- $value->is_delete = 1;
- $value->save();
- }
- }
- unset($attr['id']);
- $attr['store_id'] = $this->to_store_id;
- $model = new NewDiyTemplate();
- $model->attributes = $attr;
- $model->save();
- $ids = [$model->id];
- $center_list['to'] = $ids;
- $this->center_list = json_encode($center_list);
- $this->save();
- return true;
- }
- public function doCopyFootnav() {
- $footnav_list = json_decode($this->footnav_list, true);
- if(!empty($footnav_list['from']) && !empty($footnav_list['to'])){
- if(count($footnav_list['from']) == count($footnav_list['to'])){
- return true;
- }
- }
- $template = NewDiyTemplate::find()
- ->where([
- 'store_id' => $this->from_store_id,
- 'is_delete' => 0,
- 'name' => 'controls'
- ])->orderBy(['id' => SORT_ASC])->asArray()->one();
- // $template = null;
- $copyItem = [];
- $itemp = json_decode($template['template'], true);
- if($itemp){
- foreach ($itemp as $widget) {
- if($widget['controlComName'] == 'widgetNavControl'){
- $copyItem = $widget;
- break;
- }
- }
- }
- $ids = [$template['id']];
- $footnav_list = [
- 'from' => $ids,
- ];
- $attr = $template;
- $template = NewDiyTemplate::find()
- ->where([
- 'store_id' => $this->to_store_id,
- 'is_delete' => 0,
- 'name' => 'controls'
- ])->orderBy(['id' => SORT_ASC])->one();
- $itemp = json_decode($template['template'], true);
- if($itemp){
- foreach ($itemp as &$widget) {
- if($widget['controlComName'] == 'widgetNavControl'){
- $widget = $copyItem;
- break;
- }
- }
- }
- $template->template = json_encode($itemp);
- $template->save();
- $ids = [$template->id];
- $footnav_list['to'] = $ids;
- $this->footnav_list = json_encode($footnav_list);
- $this->save();
- return true;
- }
- public function doCopyCategory() {
- $diy_category_list = json_decode($this->diy_category_list, true);
- if(!empty($diy_category_list['from']) && !empty($diy_category_list['to'])){
- if(count($diy_category_list['from']) == count($diy_category_list['to'])){
- return true;
- }
- }
- $list = NewDiyTemplate::find()
- ->where([
- 'store_id' => $this->from_store_id,
- 'is_delete' => 0,
- 'name' => 'system'
- ])->orderBy(['id' => SORT_ASC])->asArray()->all();
- $template = null;
- $goods_d_template = null;
- foreach ($list as $value) {
- $itemp = json_decode($value['template'], true);
- if($itemp['system_type'] == 'classify'){
- $template = $value;
- } elseif ($itemp['system_type'] == 'goodsDetail') {
- $goods_d_template = $value;
- }
- }
- $ids = [$template['id']];
- $diy_category_list = [
- 'from' => $ids,
- ];
- $attr = $template;
- $goods_d_attr = $goods_d_template;
- $list = NewDiyTemplate::find()
- ->where([
- 'store_id' => $this->to_store_id,
- 'is_delete' => 0,
- 'name' => 'system'
- ])->orderBy(['id' => SORT_ASC])->all();
- foreach ($list as $value) {
- $itemp = json_decode($value['template'], true);
- if($itemp['system_type'] == 'classify'){
- $value->is_delete = 1;
- $value->save();
- } elseif ($itemp['system_type'] == 'goodsDetail') {
- $value->is_delete = 1;
- $value->save();
- }
- }
- unset($attr['id']);
- unset($goods_d_attr['id']);
- $attr['store_id'] = $this->to_store_id;
- $goods_d_attr['store_id'] = $this->to_store_id;
- $model = new NewDiyTemplate();
- $model->attributes = $attr;
- $model->save();
- $ids = [$model->id];
- $model = new NewDiyTemplate();
- $model->attributes = $goods_d_attr;
- $model->save();
- $diy_category_list['to'] = $ids;
- $this->diy_category_list = json_encode($diy_category_list);
- $this->save();
- return true;
- }
- public function doCopyProductCate() {
- $key_value_id = [];
- try {
- $category_list = json_decode($this->category_list, true);
- if(!empty($category_list['from']) && !empty($category_list['to'])){
- if(count($category_list['from']) == count($category_list['to'])){
- return true;
- }
- }
- $list = Cat::find()
- ->where([
- 'store_id' => $this->from_store_id,
- 'is_delete' => 0,
- 'parent_id' => 0
- ])->orderBy(['id' => SORT_ASC])->all();
- // $category_list = [
- // 'from' => array_column($list, 'id'),
- // ];
- $ids = [];
- foreach ($list as $value) {
- $attr = $value->attributes;
- $from_attr_id = $attr['id'];
- $key_value_id[$from_attr_id] = 0;
- unset($attr['id']);
- $attr['store_id'] = $this->to_store_id;
- $attr['parent_id'] = 0;
- $model = new Cat();
- $model->attributes = $attr;
- $model->save();
- $ids[] = $model->id;
- $key_value_id[$from_attr_id] = $model->id;
- }
- /*debug_log([
- 'key_value_id' => $key_value_id,
- 'type' => 'category_list'
- ],'store_copy.log');*/
- $list = Cat::find()
- ->where([
- 'store_id' => $this->from_store_id,
- 'is_delete' => 0,
- ])->andWhere(['>', 'parent_id', 0])->orderBy(['id' => SORT_ASC])->all();
- // $category_list = [
- // 'from' => array_column($list, 'id'),
- // ];
- foreach ($list as $value) {
- $attr = $value->attributes;
- $from_attr_id = $attr['id'];
- $key_value_id[$from_attr_id] = 0;
- unset($attr['id']);
- $attr['store_id'] = $this->to_store_id;
- /*debug_log([
- 'from_attr_id' => $from_attr_id,
- 'parent_id' => $attr['parent_id'],
- 'real' => $key_value_id[$attr['parent_id']],
- 'type' => 'category_list'
- ],'store_copy.log');*/
- if ($attr['parent_id'] > 0) {
- $attr['parent_id'] = $key_value_id[$attr['parent_id']] ?? 0;
- }
- $model = new Cat();
- $model->attributes = $attr;
- $model->save();
- $ids[] = $model->id;
- $key_value_id[$from_attr_id] = $model->id;
- }
- $category_list['to'] = $ids;
- $this->category_list = json_encode($category_list);
- if (!$this->save()) {
- throw new \Exception(json_encode($this->errors, JSON_UNESCAPED_UNICODE));
- }
- } catch (\Exception $e) {
- /*debug_log([
- 'message' => $e->getMessage(),
- 'line' => $e->getLine(),
- 'file' => $e->getFile(),
- 'type' => 'category_list'
- ],'store_copy.log');*/
- $category_list = json_decode($this->category_list, true);
- $category_list['message'] = $e->getMessage();
- $category_list['line'] = $e->getLine();
- $category_list['file'] = $e->getFile();
- $this->category_list = json_encode($category_list, JSON_UNESCAPED_UNICODE);
- $this->save();
- }
- return $key_value_id;
- }
- public function doCopyProductAttr() {
- $attr_group_key_value_id = [];
- $attr_key_value_id = [];
- try {
- $attr_group_list = json_decode($this->attr_group_list, true);
- if(!empty($attr_group_list['from']) && !empty($attr_group_list['to'])){
- if(count($attr_group_list['from']) == count($attr_group_list['to'])){
- return true;
- }
- }
- $list = AttrGroup::find()
- ->where([
- 'store_id' => $this->from_store_id,
- 'is_delete' => 0,
- ])->orderBy(['id' => SORT_ASC])->all();
- $attr_group_list = [
- 'from' => array_column($list, 'id'),
- ];
- $ids = [];
- foreach ($list as $value) {
- $attr = $value->attributes;
- $old_attr_group_id = $attr['id'];
- unset($attr['id']);
- $attr['store_id'] = $this->to_store_id;
- $model = new AttrGroup();
- $model->attributes = $attr;
- $model->save();
- $ids[] = $model->id;
- $attr_group_key_value_id[$old_attr_group_id] = $model->id;
- }
- $attr_group_list['to'] = $ids;
- $this->attr_group_list = json_encode($attr_group_list);
- if (!$this->save()) {
- throw new \Exception(json_encode($this->errors, JSON_UNESCAPED_UNICODE));
- }
- } catch (\Exception $e) {
- /*debug_log([
- 'message' => $e->getMessage(),
- 'line' => $e->getLine(),
- 'file' => $e->getFile(),
- 'type' => 'attr_group_list'
- ],'store_copy.log');*/
- $attr_group_list = json_decode($this->attr_group_list, true);
- $attr_group_list['message'] = $e->getMessage();
- $attr_group_list['line'] = $e->getLine();
- $attr_group_list['file'] = $e->getFile();
- $this->attr_group_list = json_encode($attr_group_list, JSON_UNESCAPED_UNICODE);
- $this->save();
- }
- try {
- $attr_list = json_decode($this->attr_list, true);
- if(!empty($attr_list['from']) && !empty($attr_list['to'])){
- if(count($attr_list['from']) == count($attr_list['to'])){
- return;
- }
- }
- $list = Attr::find()
- ->where([
- 'attr_group_id' => $attr_group_list['from'],
- 'is_delete' => 0,
- ])->orderBy(['id' => SORT_ASC])->all();
- $ids = [];
- foreach ($list as $value) {
- $ids[] = $value->id;
- }
- $attr_list = [
- 'from' => $ids,
- ];
- $ids = [];
- foreach ($list as $value) {
- $attr = $value->attributes;
- $old_attr_id = $attr['id'];
- unset($attr['id']);
- $attr['attr_group_id'] = $attr_group_key_value_id[$attr['attr_group_id']] ?? 0;
- $model = new Attr();
- $model->attributes = $attr;
- $model->save();
- $ids[] = $model->id;
- $attr_key_value_id[$old_attr_id] = $model->id;
- }
- $attr_list['to'] = $ids;
- $this->attr_list = json_encode($attr_list);
- if (!$this->save()) {
- throw new \Exception(json_encode($this->errors, JSON_UNESCAPED_UNICODE));
- }
- } catch (\Exception $e) {
- /*debug_log([
- 'message' => $e->getMessage(),
- 'line' => $e->getLine(),
- 'file' => $e->getFile(),
- 'type' => 'attr_list'
- ],'store_copy.log');*/
- $attr_list = json_decode($this->attr_list, true);
- $attr_list['message'] = $e->getMessage();
- $attr_list['line'] = $e->getLine();
- $attr_list['file'] = $e->getFile();
- $this->attr_list = json_encode($attr_list, JSON_UNESCAPED_UNICODE);
- $this->save();
- }
- return [
- 'attr_group_key_value_id' => $attr_group_key_value_id,
- 'attr_key_value_id' => $attr_key_value_id,
- ];
- }
- public function doCopyProduct() {
- try {
- $product_cate = $this->doCopyProductCate();
- $product_attr = $this->doCopyProductAttr();
- $goods_ids = json_decode($this->goods_ids, true);
- if(!empty($goods_ids['from'])){
- if(empty($goods_ids['to'])){
- $goods_ids['to'] = [];
- }
- if(count($goods_ids['from']) <= count($goods_ids['to'])){
- return true;
- }
- }else{
- $list = Goods::find()
- ->where([
- 'store_id' => $this->from_store_id,
- 'is_delete' => 0,
- ])->andWhere(['<=', 'md_food_id', 0])->select('id')->orderBy(['id' => SORT_ASC])->all();
- $ids = [];
- foreach ($list as $value) {
- $ids[] = $value->id;
- }
- $goods_ids = [
- 'from' => $ids,
- ];
- if(empty($ids)){
- $goods_ids['to'] = [];
- }
- $this->goods_ids = json_encode($goods_ids);
- $this->save();
- if(empty($ids)){
- return true;
- }
- // $queue = queue_push(new \app\jobs\StoreCopyJob(['id' => $this->id]));
- // \Yii::error(['queue StoreCopyJob in', $queue]);
- // return;
- }
- $attr_list = json_decode($this->attr_list, true);
- $category_list = json_decode($this->category_list, true);
- $ids = [];
- $icount = 20;
- $store_cloud_id = StoreCloud::findOne(['store_id' => $this->to_store_id, 'is_delete' => 0]);
- foreach (array_slice($goods_ids['from'], count($goods_ids['to'] ?? [])) as $k => $value) {
- // if($k >= $icount){
- // $queue = queue_push(new \app\jobs\StoreCopyJob(['id' => $this->id]));
- // \Yii::error(['queue StoreCopyJob in', $queue]);
- // return;
- // }
- $goods = Goods::findOne($value)->toArray();
- $attr = $goods;
- $fromId = $attr['id'];
- unset($attr['id']);
- $attr['store_id'] = $this->to_store_id;
- $gattr = json_decode($attr['attr'], true);
- // var_dump($attr_list);
- foreach($gattr as &$gatt){
- foreach($gatt['attr_list'] as &$att){
- $att['attr_id'] = $product_attr['attr_key_value_id'][$att['attr_id']];
- $att['share_commission_first'] = 0;
- $att['share_commission_second'] = 0;
- $att['share_commission_third'] = 0;
- }
- }
- $attr['attr'] = json_encode($gattr);
- $attr['full_cut'] = '{"pieces":0,"forehead":0}';
- $attr['integral'] = '{"give":0,"forehead":0}';
- $attr['verify_card_id'] = '';
- $model = new Goods();
- // var_dump($value, $attr);
- $model->setAttributes($attr, false);
- // var_dump($model->attributes);die;
- $model->save();
- if ($model->cloud_goods_id > 0) {
- $form = new GoodsForm();
- $form->id = $model->cloud_goods_id;
- $form->store_id = $model->store_id;
- $goodsInfo = $form->saveCloudGoods();
- $merchantForm = new MerchantForm();
- $merchantForm->token_stroe_cloud_id = $store_cloud_id ?? 0;
- $result = $merchantForm->mchGoodsImport($goodsInfo['cloudBindInfo'],$goodsInfo['goods_id'], $model->store_id);
- //debug_log($result, 'store_copy.log');
- }
- // var_dump($model);die;
- $goods_ids['to'][] = $model->id;
- $this->goods_ids = json_encode($goods_ids);
- $this->save();
- $cats = GoodsCat::findAll(['goods_id' => $fromId, 'is_delete' => 0]);
- foreach ($cats as $cat) {
- $attr = $cat->attributes;
- unset($attr['id']);
- $attr['store_id'] = $this->to_store_id;
- $attr['goods_id'] = $model->id;
- $attr['cat_id'] = $product_cate[$attr['cat_id']];
- $modelCat = new GoodsCat();
- $modelCat->attributes = $attr;
- $modelCat->save();
- }
- $gpics = GoodsPic::findAll(['goods_id' => $fromId, 'is_delete' => 0]);
- foreach ($gpics as $gpic) {
- $attr = $gpic->attributes;
- unset($attr['id']);
- $attr['goods_id'] = $model->id;
- $modelPic = new GoodsPic();
- $modelPic->attributes = $attr;
- $modelPic->save();
- }
- }
- // if (empty($goods_cache) && empty($cate_cache)) {
- // return false;
- // } else {
- $cate_ = json_decode($this->category_list, true);
- $cate_cache = array_combine($cate_['from'], $cate_['to']);
- $goods_cache = array_combine($goods_ids['from'], $goods_ids['to']);
- $params = [
- 'is_delete' => 0,
- 'is_index' => 1,
- 'store_id' => $this->to_store_id,
- ];
- $template = NewDiyTemplate::findOne($params);
- $template_ = \json_decode($template->template, true);
- if (!$template_) {
- return;
- }
- foreach ($template_['templates'] as &$item) {
- // 头部导航
- if ($item['type'] == 'fastNav') {
- $item = $this->replaceFastNav($item, $goods_cache, $cate_cache);
- }
- // 图片轮播
- if ($item['type'] == 'banner') {
- $item = $this->replaceBanner($item, $goods_cache, $cate_cache);
- }
- // 魔方导航
- if ($item['type'] == 'cubeNav') {
- $item = $this->replaceCubeNav($item, $goods_cache, $cate_cache);
- }
- // 魔方图片
- if ($item['type'] == 'container') {
- $item = $this->replaceContainer($item, $goods_cache, $cate_cache);
- }
- // 产品展示
- if ($item['type'] == 'productShow') {
- //debug_log($item, 'template.log');
- $item = $this->replaceProductShow($item, $goods_cache, $cate_cache);
- //debug_log($item, 'template.log');
- }
- //产品分类
- if ($item['type'] == 'productClassify') {
- $item = $this->replaceProductClassify($item, $goods_cache, $cate_cache);
- }
- // 倒计时
- if ($item['type'] == 'marketCountDown') {
- $item = $this->replaceMarketCountDown($item, $goods_cache, $cate_cache);
- }
- // 首页推荐
- if ($item['type'] == 'indexRecommend') {
- // Todo 该模块数据有误,后期解决
- // $item = $this->replaceIndexRecommend($item, $goodsCache, $cacheCat);
- }
- }
- $template->template = json_encode($template_, JSON_UNESCAPED_UNICODE);
- $template->save();
- // }
- return true;
- } catch (\Exception $e) {
- /*debug_log([
- 'message' => $e->getMessage(),
- 'line' => $e->getLine(),
- 'file' => $e->getFile(),
- 'type' => 'product_list'
- ],'store_copy.log');*/
- $category_list = json_decode($this->category_list, true);
- $category_list['message'] = $e->getMessage();
- $category_list['line'] = $e->getLine();
- $category_list['file'] = $e->getFile();
- return true;
- }
- }
- // 替换头部导航
- private function replaceFastNav($data, $goodsCache, $cacheCat)
- {
- foreach ($data['params']['list'] as &$item) {
- $item = $this->replaceLink($item, $goodsCache, $cacheCat);
- }
- return $data;
- }
- // 替换图片轮播
- private function replaceBanner($data, $goodsCache, $cacheCat)
- {
- foreach ($data['params']['bannerList'] as &$item) {
- $item = $this->replaceLink($item, $goodsCache, $cacheCat);
- }
- return $data;
- }
- // 替换魔方导航
- private function replaceCubeNav($data, $goodsCache, $cacheCat)
- {
- foreach ($data['params']['list'] as &$item) {
- $item = $this->replaceLink($item, $goodsCache, $cacheCat);
- }
- return $data;
- }
- // 替换魔方图片
- private function replaceContainer($data, $goodsCache, $cacheCat)
- {
- foreach ($data['params']['cubeCoverList'] as &$item) {
- $item = $this->replaceLink($item, $goodsCache, $cacheCat);
- }
- return $data;
- }
- // 替换产品展示
- private function replaceProductShow($data, $goodsCache, $cacheCat)
- {
- // 替换分类id
- if (isset($data['params']['classifyData']) && isset($cacheCat[$data['params']['classifyData']])) {
- $data['params']['classifyData'] = $cacheCat[$data['params']['classifyData']];
- }
- // 替换分类商品id
- if (isset($data['params']['classifyArr'])) {
- foreach ($data['params']['classifyArr'] as &$item) {
- if (isset($goodsCache[$item['id']])) {
- $item['id'] = $goodsCache[$item['id']];
- }
- }
- }
- // 替换商品id
- if (isset($data['params']['selectData'])) {
- foreach ($data['params']['selectData'] as &$item) {
- if (isset($goodsCache[$item['id']])) {
- $item['id'] = $goodsCache[$item['id']];
- }
- }
- }
- return $data;
- }
- // 替换产品分类
- private function replaceProductClassify($data, $goodsCache, $cacheCat)
- {
- foreach ($data['params']['classifyList'] as &$item) {
- // 替换分类
- if (empty($item['classifyId']) && isset($cacheCat[$item['classifyId']])) {
- $item['id'] = $cacheCat[$item['classifyId']];
- $item['classifyId'] = $cacheCat[$item['classifyId']];
- }
- // 替换商品id
- foreach ($item['data'] as &$value) {
- if (isset($goodsCache[$value['id']])) {
- $value['id'] = $goodsCache[$value['id']];
- }
- }
- }
- return $data;
- }
- // 替换倒计时
- private function replaceMarketCountDown($data, $goodsCache, $cacheCat)
- {
- $data['params'] = $this->replaceLink($data['params'], $goodsCache, $cacheCat);
- return $data;
- }
- private function replaceLink($item, $goodsCache, $cacheCat)
- {
- if (!isset($item['link']['link'])) {
- return $item;
- }
- // 处理分类id
- if (
- strpos($item['link']['link'], '/pages/cat/cat?cat_id=') !== false &&
- $item['link']['params'][0]['value'] != '' &&
- isset($cacheCat[$item['link']['params'][0]['value']])
- ) {
- $item['link']['link'] = '/pages/cat/cat?cat_id=' . $cacheCat[$item['link']['params'][0]['value']];
- $item['link']['params'][0]['value'] = $cacheCat[$item['link']['params'][0]['value']];
- }
- // 处理商品列表
- if (
- strpos($item['link']['link'], '/other/list/list?cat_id=') !== false &&
- $item['link']['params'][0]['value'] != '' &&
- isset($cacheCat[$item['link']['params'][0]['value']])
- ) {
- $item['link']['link'] = '/other/list/list?cat_id=' . $cacheCat[$item['link']['params'][0]['value']];
- $item['link']['params'][0]['value'] = $cacheCat[$item['link']['params'][0]['value']];
- }
- // 处理商品详情
- if (
- strpos($item['link']['link'], '/goods/goods/goods?id=') !== false &&
- $item['link']['params'][0]['value'] != '' &&
- isset($goodsCache[$item['link']['params'][0]['value']])
- ) {
- $item['link']['link'] = '/goods/goods/goods?id=' . $goodsCache[$item['link']['params'][0]['value']];
- $item['link']['params'][0]['value'] = $goodsCache[$item['link']['params'][0]['value']];
- }
- return $item;
- }
- }
|