浏览代码

修改b端接口:修改网站头部基础信息(将网站标识同步到广告位管理及自助建站模板数据)

15313670163 1 天之前
父节点
当前提交
bfb37ae8e0
共有 2 个文件被更改,包括 56 次插入13 次删除
  1. 55 12
      app/JsonRpc/WebsiteService.php
  2. 1 1
      app/Model/AdPlace.php

+ 55 - 12
app/JsonRpc/WebsiteService.php

@@ -168,20 +168,63 @@ class WebsiteService implements WebsiteServiceInterface
         if (!empty($web_templsate) && $web_templsate['status'] == 2) {
             return Result::error("此网站已应用,不可修改基本信息", 0);
         }
-        $web = Website::where(function ($query) use ($data) {
-            $query->where('id', '!=', $data['id'])
-                ->where('ad_key', $data['ad_key']);
-        })->first();
-        if (!empty($web)) {
-            return Result::error("网站名称简写重复,请填写", 0);
-        }
-        $result = Website::where('id', $data['id'])->update($insertData);
-        // var_dump("更新站点", $result);
-        if (empty($result)) {
-            return Result::error("更新网站失败", 0);
-        } else {
+       
+       Db::beginTransaction();
+       try{
+            if($data['ad_key'] !=  $website['ad_key']){
+                $web = Website::where(function ($query) use ($data) {
+                $query->where('id','!=', $data['id'])
+                    ->where('ad_key', $data['ad_key']);
+                })->first();
+                if (!empty($web)) {
+                    Db::rollBack();
+                    return Result::error("网站名称简写重复,请填写", 0);
+                }
+                $ad_place = AdPlace::where('website_id', $data['id'])->select('ad_tag','id')->get()->toArray();
+                if (!empty($ad_place)) {
+                    foreach($ad_place as $key => $val){
+                        $ad_keys = explode('_', $val['ad_tag']);
+                        $ad_keys[0] = $data['ad_key'];
+                        $ad_place[$key]['ad_tag'] = implode('_', $ad_keys);
+
+                        // $up_adtags = AdPlace::where('id', $val['id'])->update(['ad_tag' => $ad_tags[$key]]);
+                        
+                    }
+                    $up_adtags = AdPlace::upsert($ad_place, ['id'], ['ad_tag']);
+
+                    if(empty($up_adtags)){
+                        Db::rollBack();
+                        return Result::error('广告位标识同步失败', 0);
+                    }
+                }
+                $template = WebsiteTemplate::where('website_id', $data['id'])->first();
+                if (!empty($template)) {
+                    // Eloquent 没有 replace 方法,推测你可能是想替换模板中的字符串,假设是更新 content 字段
+                    $uptemplate = WebsiteTemplate::where('website_id', $data['id'])->update([
+                        'template_data' => DB::raw("REPLACE(template_data, '{$website['ad_key']}', '{$data['ad_key']}')")
+                    ]);
+                    if(empty($uptemplate)){
+                        Db::rollBack();
+                        return Result::error("更新模板失败!", 0);
+                    }
+
+                }
+
+
+            }
+            // return Result::success($UP_template);
+
+            $result = Website::where('id', $data['id'])->update($insertData);
+            if(empty($result)){
+                Db::rollBack();
+                return Result::error("更新网站失败!", 0);
+            }
+            Db::commit();
             return Result::success("更新成功");
 
+        }catch(\Exception $e){
+            Db::rollBack();
+            return Result::error($e->getMessage(), 0);
         }
     }
     /**

+ 1 - 1
app/Model/AdPlace.php

@@ -18,7 +18,7 @@ class AdPlace extends Model
     /**
      * The attributes that are mass assignable.
      */
-    protected array $fillable = [];
+    protected array $fillable = ['ad_tag'];
 
     /**
      * The attributes that should be cast to native types.