Parcourir la source

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

15313670163 il y a 1 jour
Parent
commit
aa56bd160f
1 fichiers modifiés avec 63 ajouts et 12 suppressions
  1. 63 12
      app/JsonRpc/WebsiteService.php

+ 63 - 12
app/JsonRpc/WebsiteService.php

@@ -136,6 +136,10 @@ class WebsiteService implements WebsiteServiceInterface
      */
     public function updateWebsite(int $id, array $data): array
     {
+        $website = Website::where('id', $id)->first();
+        if (empty($website)) {
+            return Result::error("网站不存在", 0);
+        }
         $insertData = [
             'website_name' => $data['website_name'],
             'logo' => $data['logo'] ?? '',
@@ -157,20 +161,67 @@ 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();
+                if (!empty($ad_place)) {
+                    foreach($ad_place as $key => $val){
+                        $ad_keys = explode('_', $val['ad_tag']);
+                        $ad_keys[0] = $data['ad_key'];
+                        $ad_tags[$key] = implode('_', $ad_keys);
+                        $up_adtags = AdPlace::where('id', $val['id'])->update(['ad_tag' => $ad_tags[$key]]);
+                        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);
         }
+        
+        // var_dump("更新站点", $result);
+        // if (empty($result)) {
+        //     return Result::error("更新网站失败", 0);
+        // } else {
+        //     return Result::success("更新成功");
+        // }
     }
     /**
      * @param int $id