浏览代码

修改广告位管理-修改广告的接口

15313670163 5 天之前
父节点
当前提交
11fc07b600
共有 1 个文件被更改,包括 132 次插入31 次删除
  1. 132 31
      app/JsonRpc/AdService.php

+ 132 - 31
app/JsonRpc/AdService.php

@@ -6,6 +6,8 @@ use App\Model\AdSize;
 use Hyperf\RpcServer\Annotation\RpcService;
 use App\Tools\Result;
 use Hyperf\DbConnection\Db;
+use App\Model\WebsiteTemplate;
+
 #[RpcService(name: "AdService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
 class AdService implements AdServiceInterface
 {
@@ -178,43 +180,142 @@ class AdService implements AdServiceInterface
             return Result::error("创建广告位失败",500);
         }
     }
-
     /**
-     * 'name' => 'required', //广告位名称
-     * 'website_id' => 'required',//关联的网站id
-     * 'price' => 'required',//价格
-     * 'thumb'=>'required', //广告示意图
-     * 'typeid'=>'required', //广告类型
-     * 'ad_size_id'=>'required', //广告位大小
-     * 'status'=>'required', //状态
-     * @param array $data
-     * @return array
-     */
-    public function updateAdPlace(array $data): array
+     * @param array $data 
+     * @return array 
+     */ 
+    public function updateAdPlace(array $data): array 
     {
         $where = [
-            'id'=>$data['id']
-        ];
-        $Insdata = [
-            'website_id'=>$data['website_id']??'',
-            'typeid'=>$data['typeid']??1,
-            'status'=>$data['status']??2,
-            'name'=>$data['name']??'',
-            'thumb'=>$data['thumb']??'',
-            'introduce'=>$data['introduce']??'',
-            'code'=>$data['code']??'',
-            'price'=>$data['price']??0,
-            'ad_size_id'=>$data['ad_size_id']??0,
-                'ad_tag'=>$data['ad_tag']??'',
+            'ad_place.id'=>$data['id']
         ];
-        $result = AdPlace::where($where)->update($Insdata);
-        if($result){
-            return Result::success($result);
-        }else{
-            return Result::error("更新失败");
+        // $website_id = intval($data['website_id']);
+
+        // 原代码 leftJoin 条件使用错误,第三个参数应该是关联字段,这里修正为 "website.id" 关联 "ad_place.website_id"
+        $web = AdPlace::where($where)
+        ->leftJoin("website","website.id","ad_place.website_id")
+        ->select("website.*","ad_place.name",'ad_place.ad_tag')
+        ->first();
+        if(empty($web)){
+            return Result::error("广告位不存在");
         }
-    }
+        // 自助建站广告位-广告位编辑同步模板数据-------fr----start
 
+        $website_column_arr_id = json_decode($web['website_column_arr_id'],true);
+        Db::beginTransaction();
+        try{
+            // 判断网站的广告位是否通过自助建站创建的广告位(通过网系包含2来判断)
+            if(in_array(2,$website_column_arr_id)){
+                $template_data = ['ad_tag'=>$web['ad_tag']];
+                $template_data = array_merge($data,$template_data);
+                $template_module = WebsiteTemplate::where('website_id',$template_data['website_id'])->first();
+                $page_type = explode('_', $template_data['ad_tag']);
+                // 页面类型
+                $page = $page_type[1];
+                $ad_places = [];
+                // 模板数据及画布数据
+                $oldtemplate_data = json_decode($template_module['template_data'],true);
+                $canvas_data = json_decode($template_module['canvas_data'],true);
+                if(array_key_exists( $page,$oldtemplate_data['ad'])){
+                    $ad_places = $oldtemplate_data['ad'][$page];
+                }
+                foreach($ad_places as $key=>$val){
+                    if($template_data['ad_tag'] == $val['ad_tag']){
+                        $ad_place[$key] = [
+                            'width' => $val['width'],
+                            'height' => $val['height'],
+                            'name'=>$template_data['name'],   //广告位名称可更改
+                            'price'=>$template_data['price'],   //广告位价格可更改
+                            'introduce'=>$template_data['introduce'],   //广告位介绍可更改
+                            'website_id'=>$val['website_id'],
+                            'thumb'=>$template_data['thumb'],   //广告位默认图可更改
+                            'typeid'=>$val['typeid'],
+                            'ad_tag'=>$val['ad_tag'],
+                        ];
+                        $template_ad = $ad_place[$key];
+                    }else{
+                        $ad_place[$key] = $val;
+
+                    }
+                }
+                //  保存的模板中的广告位相关信息
+                $oldtemplate_data['ad'][$page] = $ad_place;
+
+                if(array_key_exists( $page,$oldtemplate_data['template'])){
+                    $ad_places_info = $oldtemplate_data['template'][$page];
+                }
+                foreach($ad_places_info as $key=>$val){
+                    $ad_info[$key] = $val;
+                    if(array_key_exists('ad',$val) && $val['ad']['ad_tag'] == $template_data['ad_tag']){
+                        $ad_info[$key]['ad'] = $template_ad;
+                        $num = $key;
+                    }
+                }
+                foreach($canvas_data['template'][$page] as $key=>$val){
+                    $canvas_ad[$key] = $val;
+                    if(array_key_exists('ad',$val['content']) && $key == $num){
+                        // $canvas_ad[$key]['ad'] = $template_ad;
+                        $canvas_ad[$num]['content']['ad'] = [
+                            'width' => $val['content']['ad']['width'],
+                            'height' => $val['content']['ad']['height'],
+                            'name'=>$template_data['name'],   //广告位名称可更改
+                            'price'=>$template_data['price'],   //广告位价格可更改
+                            'introduce'=>$template_data['introduce'],   //广告位介绍可更改
+                            'website_id'=>$val['content']['ad']['website_id'],
+                            'thumb'=>$val['content']['ad']['thumb'],   //广告位默认图可更改
+                            'typeid'=>$val['content']['ad']['typeid'],
+                            'ad_tag'=>$val['content']['ad']['ad_tag'],
+                        ];
+                    }
+                }
+                // Db::rollBack();
+            // }
+                // return Result::success($canvas_ad);
+                $oldtemplate_data['template'][$page] = $ad_info;    //模板广告位相关信息已修改
+                if($page_type[1] == 'top'){
+                    $canvas_data['topAd'] = $template_ad;
+                }else{
+                    // $canvas_ad = $ad_info;
+                    $canvas_data['template'][$page] = $canvas_ad;
+                }
+                $template_module['canvas_data'] = json_encode($canvas_data,JSON_UNESCAPED_UNICODE);
+                $template_module['template_data'] = json_encode($oldtemplate_data,JSON_UNESCAPED_UNICODE);
+                $template_result = WebsiteTemplate::where('website_id',$template_data['website_id'])->update([
+                    'canvas_data'=>$template_module['canvas_data'],
+                    'template_data'=>$template_module['template_data'], 
+                ]);
+                if(!$template_result){
+                    Db::rollBack();
+                    return Result::error('同步模板失败!');
+                }
+            }
+            // return Result::success($website_column_arr_id);
+            $Insdata = [
+                'website_id'=>$data['website_id']??'',
+                'typeid'=>$data['typeid']??1,
+                'status'=>$data['status']??2,
+                'name'=>$data['name']??'',
+                'thumb'=>$data['thumb']??'',
+                'introduce'=>$data['introduce']??'',
+                'code'=>$data['code']??'',
+                'price'=>$data['price']??0,
+                'ad_size_id'=>$data['ad_size_id']??0,
+                'ad_url'=>$data['ad_url']??'',
+
+            ];
+            $result = AdPlace::where($where)->update($Insdata);
+            if(!$result){
+                Db::rollBack();
+                return Result::error("更新失败");
+            }
+            Db::commit();
+            return Result::success($result);
+        }catch (\Exception $e) {
+            Db::rollBack();
+            return Result::error($e->getMessage());
+        }
+        // 自助建站广告位-广告位编辑同步模板数据-------fr----end
+    } 
     /**
      * 删除广告位
      * @param array $data