cmsContentTemplateSet = $cmsContentTemplateSet; $this->cmsContentTemplate = $cmsContentTemplate; $this->cmsSubjectProduct = $cmsSubjectProduct; $this->cmsSubject = $cmsSubject; } /** * 点击内容配置 */ public function set($request) { $template = $this->cmsContentTemplate->select('id','city_name','city_id','title','apply_type','is_open','status')->where('id',$request['tpl_id'])->first(); $templates = $template->toArray(); $where = [ 'city_id'=>$templates['city_id'], 'title'=>$templates['title'], 'apply_type'=>$templates['apply_type'], ]; $copys_template = $this->cmsContentTemplate->where($where)->get(); $request = []; $need_create=true; foreach ($copys_template as $k=>$v) { if ($v['status'] == 0) { $need_create = false; $request['type'] = $v['apply_type']; $request['tpl_id'] = $v['id']; return $this->preview($request); } } if ($need_create) { $new_template = [ 'title' => $templates['title'], 'city_id' => $templates['city_id'], 'city_name' => $templates['city_name'], 'apply_type' => $templates['apply_type'], 'status' => 0, 'is_open' => $templates['is_open'], ]; DB::beginTransaction(); try { $res = $this->cmsContentTemplate->create($new_template); if (!$res) { throw new HttpException(500, '添加草稿模板失败'); } $template_set = $this->cmsContentTemplateSet->where('tpl_id', $templates['id'])->get(); if (count($template_set) > 0) { foreach ($template_set->toArray() as $key => $val) { $copy_template = [ 'rule' => $val['rule'], 'tpl_id' => $res->id, 'area_type' => $val['area_type'], 'status' => $val['status'], ]; $result = $this->cmsContentTemplateSet->create($copy_template); if (!$result) { throw new HttpException(500, '生成模板内容失败'); } $request['type'] = $res->apply_type; $request['tpl_id'] = $result->tpl_id; } } DB::commit(); return $this->preview($request); //return Response::create(); } catch (QueryException $exception) { DB::rollBack(); return Response::create([ 'message' => '生成模板内容失败,请重试', 'error' => $exception->getMessage(), 'status_code' => 500 ]); } } } /** * banner配置 */ public function bannerSet($request) { $banner_id = $request['id'] ?? ''; $rules = $request['rule']; if (is_array($rules)){ if (count($rules)>10) { throw new HttpException(500, '最多只能添加10个banner海报'); } $subject = [ 'rule' => json_encode($rules), 'tpl_id' => $request['tpl_id'], 'area_type' => $request['area_type'], 'status' => 0, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s') ]; if ($banner_id){ $banners = $this->cmsContentTemplateSet->where('id',$banner_id)->update($subject); if (!$banners) { throw new HttpException(500, '更新失败'); } }else{ if (!$this->cmsContentTemplateSet->create($subject)) { throw new HttpException(500, '添加失败'); } } }else{ throw new HttpException(500, '参数格式有误'); } } /** * 专题广告配置 */ public function advertisementSet($request) { $advertisement_id = $request['id'] ?? ''; $subject = [ 'tpl_id' => $request['tpl_id'], 'rule' => json_encode($request['rule']), 'area_type' => $request['area_type'], 'status' => 0, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s') ]; if (empty($advertisement_id)){ if (!$this->cmsContentTemplateSet->create($subject)) { throw new HttpException(500, '添加失败'); } }else{ $advertisement_ids = $this->cmsContentTemplateSet->where('id',$advertisement_id)->update($subject); if (!$advertisement_ids) { throw new HttpException(500, '更新失败'); } } } /** * 商品楼层配置 */ public function floorSet($request) { $floor_id = $request['id'] ?? ''; $subject = [ 'tpl_id' => $request['tpl_id'], 'rule' => json_encode($request['rule']), 'area_type' => $request['area_type'], 'status' => 0, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s') ]; if (empty($floor_id)){ if (!$this->cmsContentTemplateSet->create($subject)) { throw new HttpException(500, '添加失败'); } }else { $floor_ids = $this->cmsContentTemplateSet->where('id',$floor_id)->update($subject); if (!$floor_ids) { throw new HttpException(500, '更新失败'); } } } /** * 分类专题配置 */ public function categorySet($request) { $category_id = $request['id'] ?? ''; $subject = [ 'tpl_id' => $request['tpl_id'], 'rule' => json_encode($request['rule']), 'area_type' => $request['area_type'], 'status' => 0, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s') ]; if (empty($category_id)){ if (!$this->cmsContentTemplateSet->create($subject)) { throw new HttpException(500, '添加失败'); } }else { $category_ids = $this->cmsContentTemplateSet->where('id',$category_id)->update($subject); if (!$category_ids) { throw new HttpException(500, '更新失败'); } } } /** * 内容发布 */ public function release($request) { //同一个城市同一个模板(团购/菜市场) 只能有一种状态(草稿/发布),已发布的一旦被编辑把之前的直接删掉 $templateSet = $this->cmsContentTemplateSet->where('tpl_id',$request['tpl_id'])->select('id','status')->get(); $tem_array = $templateSet->toArray(); $template = $this->cmsContentTemplate->select('id','city_name','city_id','title','apply_type','is_open','status')->where('id',$request['tpl_id'])->first(); // if ($template->is_open == 0){ // throw new HttpException(500, '请先将状态开启之后再发布'); // } $where = [ 'city_id'=>$template->city_id, 'title'=>$template->title, 'apply_type'=>$template->apply_type, ]; $templates = $this->cmsContentTemplate->where($where)->get(); $update_is_open_ids=[]; foreach ($templates->toArray() as $k=>$v) { if ($v['status'] == 1) { $update_is_open_ids[]=$v['id']; } } if ($update_is_open_ids){ $update_open = $this->cmsContentTemplate->whereIn('id', $update_is_open_ids)->delete(); if (!$update_open) { throw new HttpException(500, '删除失败,请重试'); } $update_open = $this->cmsContentTemplateSet->whereIn('tpl_id', $update_is_open_ids)->update(['status' => 0]); if (!$update_open) { throw new HttpException(500, '修改失败,请重试'); } } $update_tpl = $this->cmsContentTemplate->where('id', $request['tpl_id'])->update(['status' => 1]); if (!$update_tpl) { throw new HttpException(500, '修改失败,请重试'); } $res = $this->cmsContentTemplateSet->where('tpl_id',$request['tpl_id'])->update(['status'=>1]); if (!$res){ throw new HttpException(500, '修改失败,请重试'); } foreach ($templates->toArray() as $k=>$v) { $result = $this->cmsContentTemplateSet->where(['status'=>0,'tpl_id'=>$v['id']])->delete(); if ($result) { return Response::create(); } } } /** * 内容预览 */ public function preview($request) { $temalates = $this->cmsContentTemplate->select('title')->where(['id'=>$request['tpl_id']])->first(); if (!$temalates){ throw new HttpException(500, '没有找到对应模板'); } //团购首页 $group_array = []; if ($request['type'] == 0){ $group_array['apply_type'] = 0; $group_array['tpl_id'] = $request['tpl_id']; $group_array['title'] = $temalates->title; $group_array['content'] =[]; }else{//菜市场首页 $group_array['apply_type'] = 1; $group_array['tpl_id'] = $request['tpl_id']; $group_array['title'] = $temalates->title; $group_array['content'] =[]; } $group_array['content'][0]['area_type'] = 0; $banner_rule = $this->cmsContentTemplateSet->select('id','rule')->where(['tpl_id'=>$request['tpl_id'],'area_type'=>0])->orderBy('id', 'desc')->limit(1)->get(); $new_rule = []; foreach ($banner_rule->toArray() as $k=>$v){ $new_rule[$k]['id'] = $v['id']; $new_rule[$k]['rule'] = json_decode($v['rule'],true); } $group_array['content'][0]['rule'] = $new_rule; $group_array['content'][1]['area_type'] = 1; $subject_rule = $this->cmsContentTemplateSet->select('id','rule')->where(['tpl_id'=>$request['tpl_id'],'area_type'=>1])->get(); $new_rule = []; foreach ($subject_rule->toArray() as $k=>$v){ $new_rule[$k]['id'] = $v['id']; $new_rule[$k]['rule'] = json_decode($v['rule'],true); } $group_array['content'][1]['rule'] = $new_rule; $group_array['content'][2]['area_type'] = 2; $floor_rule = $this->cmsContentTemplateSet->select('id','rule')->where(['tpl_id'=>$request['tpl_id'],'area_type'=>2])->get(); $new_rule = []; foreach ($floor_rule->toArray() as $k=>$v){ $new_rule[$k]['id'] = $v['id']; $new_rule[$k]['rule'] = json_decode($v['rule'],true); } foreach ($new_rule as $k=>$v){ if ($v){ $rules = $v['rule']; $show_num = intval($rules['show_num']); $show_type = $this->cmsSubject->select('show_type')->where('id', $rules['link_url'])->first(); $product = $this->cmsSubjectProduct->where('subject_id', $rules['link_url'])->orderBy('sort', 'asc')->limit($show_num)->get(); $pro_array = $product->toArray(); $res_id = implode(",", array_column($pro_array, 'product_id')); $new_rule[$k]['product_id'] = $res_id; $new_rule[$k]['subject_id'] = $rules['link_url']; $new_rule[$k]['show_type'] = $show_type->show_type ?? ''; unset($v['url']); unset($v['show_num']); unset($rules['link_url']); unset($v['link_type']); } } $group_array['content'][2]['rule'] = $new_rule; if ($request['type'] == 1){ $group_array['content'][3]['area_type'] = 3; $category_rule = $this->cmsContentTemplateSet->select('id','rule')->where(['tpl_id'=>$request['tpl_id'],'area_type'=>3])->get(); $new_rule = []; foreach ($category_rule->toArray() as $k=>$v){ $new_rule[$k]['id'] = $v['id']; $new_rule[$k]['rule'] = json_decode($v['rule'],true); } $group_array['content'][3]['rule'] = $new_rule; } return $group_array; } public function getTemplate($cityId) { return $this->cmsContentTemplate->select('id','title','apply_type')->where(['city_id'=>$cityId])->where('deleted_at', null)->orderBy('apply_type','asc')->get(); } public function templateSetDelete($request) { $advertisement = $this->cmsContentTemplateSet->where('id', $request['id'])->first(); $res = $advertisement->delete(); if (!$res){ return Response::create([ 'message' => '删除失败,请重试', 'status_code' => 500 ]); } } }