limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->orderBy("code", "asc")->get(); $count = District::where($where)->count(); $result = [ 'rows' => $rep, 'count' => $count ]; } else { $result = District::where($data)->orderBy("code", "asc")->get(); } return $result ? Result::success($result) : Result::error("没有查到数据"); } /** * @param array $data * @return array */ public function getUserLevelList(array $data): array { $where = []; if (isset($data['keyWord'])) { $where = [ ['name', 'like', '%' . $data['keyWord'] . '%'], ]; } $result = []; if (isset($data['pageSize'])) { $rep = UserLevel::where($where)->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->orderBy("id", "asc")->get(); $count = UserLevel::where($where)->count(); $result = [ 'rows' => $rep, 'count' => $count, ]; } else { $result = UserLevel::orderBy("id", "asc")->get(); } return $result ? Result::success($result) : Result::error("没有查到数据"); } /** * 添加用户等级 * @param array $data * @return array */ public function addUserLevel(array $data): array { LevelUser::insertGetId($data); return Result::success([]); } /** * 更新等级 * @param array $data * @return array */ public function updateUserLevel(array $data): array { $result = LevelUser::where(['id' => $data['id']])->update($data); if ($result) { return Result::success($result); } return Result::error("更新失败"); } /** * 删除等级 * @param array $data * @return array */ public function delUserLevel(array $data): array { $result = LevelUser::where(['id' => $data['id']])->delete(); if ($result) { return Result::success($result); } return Result::error("删除失败"); } /** * 查询投诉举报信息 * @param array $data * @return array */ public function getLetterOfComplaint(array $data = []): array { var_dump("===="); $where = []; if (isset($data['user_id']) && !empty($data['user_id'])) { array_push($where, ['letter_of_complaint.user_id', '=', $data['user_id']]); } if (isset($data['nature']) && !empty($data['nature'])) { array_push($where, ['letter_of_complaint.nature', '=', $data['nature']]); } if (isset($data['nature_level0']) && !empty($data['nature_level0'])) { array_push($where, ['letter_of_complaint.nature_level0', '=', $data['nature_level0']]); } if (isset($data['status']) && !empty($data['status'])) { array_push($where, ['letter_of_complaint.status', '=', $data['status']]); } $result = []; if (isset($data['pageSize'])) { $rep = LetterOfComplaint::where($where) ->leftJoin("letter_type as type_a", "letter_of_complaint.nature", "type_a.id") ->leftJoin("letter_type as type_c", "letter_of_complaint.nature_level0", "type_c.id") ->leftJoin("letter_type as type_b", "letter_of_complaint.nature_level1", "type_b.id") ->leftJoin("letter_type as type_e", "letter_of_complaint.nature_level3", "type_e.id") ->leftJoin("letter_type as type_d", "letter_of_complaint.status", "type_d.id") ->select( "letter_of_complaint.*", "type_a.type_name as nature_name", "type_b.type_name as nature_name1", "type_c.type_name as nature_name0", "type_d.type_name as status_name", "type_e.type_name as nature_name3" ) ->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->orderBy("letter_of_complaint.id", "desc")->get(); $count = LetterOfComplaint::where($where)->count(); if ($rep) { foreach ($rep as $val) { if ($val['judgment']) { $val['judgment'] = json_decode($val['judgment']); } if ($val['audio_and_video']) { $val['audio_and_video'] = json_decode($val['audio_and_video']); } if ($val['contract']) { $val['contract'] = json_decode($val['contract']); } if ($val['qualifications']) { $val['qualifications'] = json_decode($val['qualifications']); } } } $result = [ 'rows' => $rep, 'count' => $count, ]; } else { $result = LetterOfComplaint::where($where) ->leftJoin("letter_type as type_a", "letter_of_complaint.nature", "type_a.id") ->leftJoin("letter_type as type_c", "letter_of_complaint.nature_level0", "type_c.id") ->leftJoin("letter_type as type_b", "letter_of_complaint.nature_level1", "type_b.id") ->leftJoin("letter_type as type_e", "letter_of_complaint.nature_level3", "type_e.id") ->leftJoin("letter_type as type_d", "letter_of_complaint.status", "type_d.id") ->select( "letter_of_complaint.*", "type_a.type_name as nature_name", "type_b.type_name as nature_name1", "type_c.type_name as nature_name0", "type_d.type_name as status_name", "type_e.type_name as nature_name3" ) ->orderBy("letter_of_complaint.id", "desc")->get(); } return $result ? Result::success($result) : Result::error("没有查到数据"); } /** * 添加投诉举报信息 * @param array $data * @return array */ public function addLetterOfComplaint(array $data): array { $data['judgment'] = $data['judgment'] ? json_encode($data['judgment']) : ''; $data['audio_and_video'] = $data['audio_and_video'] ? json_encode($data['audio_and_video']) : ''; $data['contract'] = $data['contract'] ? json_encode($data['contract']) : ''; $data['qualifications'] = $data['qualifications'] ? json_encode($data['qualifications']) : ''; unset($data['id']); $result = LetterOfComplaint::insertGetId($data); if (empty($result)) { return Result::error("创建失败", 0); } else { return Result::success(["id" => $result]); } } /** * 用户端更新投诉举报 * @param array $data * @return array */ public function userUpLetterOfComplaint(array $data): array { $data['judgment'] = $data['judgment'] ? json_encode($data['judgment']) : ''; $data['audio_and_video'] = $data['audio_and_video'] ? json_encode($data['audio_and_video']) : ''; $data['contract'] = $data['contract'] ? json_encode($data['contract']) : ''; $data['qualifications'] = $data['qualifications'] ? json_encode($data['qualifications']) : ''; $result = LetterOfComplaint::where(['id' => $data['id']])->update($data); if (empty($result)) { return Result::error("创建失败", 0); } else { return Result::success(["id" => $result]); } } /** * 管理后台更新投诉举报信息 * @param array $data * @return array */ public function upLetterOfComplaint(array $data): array { var_dump("admin:", $data); $where = [ 'id' => $data['id'], ]; $filtered_array = array_filter($data, function ($value) { return $value !== "" && $value !== null && $value !== false && !is_array($value) || !empty($value); }); $filtered_array['judgment'] = isset($filtered_array['judgment']) ? json_encode($filtered_array['judgment']) : ''; $filtered_array['audio_and_video'] = isset($filtered_array['audio_and_video']) ? json_encode($filtered_array['audio_and_video']) : ''; $filtered_array['contract'] = isset($filtered_array['contract']) ? json_encode($filtered_array['contract']) : ''; $filtered_array['qualifications'] = isset($filtered_array['qualifications']) ? json_encode($filtered_array['qualifications']) : ''; unset($filtered_array['nature_name']); unset($filtered_array['type_name']); unset($filtered_array['nature_level_name']); unset($filtered_array['status_name']); unset($filtered_array['is_admin']); unset($filtered_array['type_level_name']); $result = LetterOfComplaint::where($where)->update($filtered_array); if ($result) { return Result::success($result); } return Result::error("更新失败", 0); } /** * 查询投诉举报记录 * @param array $data * @return array */ public function getLetterOfComplaintInfo(array $data): array { $where = [ 'letter_of_complaint.id' => $data['id'], ]; if (isset($data['user_id']) && !empty($data['user_id'])) { array_push($where, ['letter_of_complaint.user_id', '=', $data['user_id']]); } $result = LetterOfComplaint::where($where) ->leftJoin("letter_type as type_a", "letter_of_complaint.nature", "type_a.id") ->leftJoin("letter_type as type_c", "letter_of_complaint.nature_level0", "type_c.id") ->leftJoin("letter_type as type_b", "letter_of_complaint.nature_level1", "type_b.id") ->leftJoin("letter_type as type_e", "letter_of_complaint.nature_level3", "type_e.id") ->leftJoin("letter_type as type_d", "letter_of_complaint.status", "type_d.id") ->select( "letter_of_complaint.*", "type_a.type_name as nature_name", "type_b.type_name as nature_name1", "type_c.type_name as nature_name0", "type_d.type_name as status_name", "type_e.type_name as nature_name3" ) ->first(); return Result::success($result); } /** * 删除投诉举报信息 * @param array $data * @return array */ public function delLetterOfComplaint(array $data): array { $result = LetterOfComplaint::when($data, function ($query) use ($data) { if (isset($data['id']) && !empty($data['id'])) { $query->where(['id' => $data['id']]); } if (isset($data['user_id']) && !empty($data['user_id'])) { $query->where(['user_id' => $data['user_id']]); } })->delete(); if (empty($result)) { return Result::error("删除失败", 0); } else { return Result::success(); } } /** * 获取举报信息类型 * @param array $data * @return array */ public function getLetterType(array $data): array { $where = []; if (isset($data['type'])) { array_push($where, ['type', '=', $data['type']]); } if (isset($data['pid']) && $data['pid'] > 0) { array_push($where, ['pid', '=', $data['pid']]); } $result = LetterType::where($where)->orderBy('sort', 'asc')->get(); return $result ? Result::success($result) : Result::error("没有查到数据"); } /** * 更新举报类型 * @param array $data * @return array */ public function upLetterType(array $data): array { return []; } /** * 添加举报类型 * @param array $data * @return array */ public function addLetterType(array $data): array { $result = LetterType::insertGetId($data); if (empty($result)) { return Result::error("创建失败", 0); } else { return Result::success(["id" => $result]); } } /** * 删除举报类型 * @param array $data * @return array */ public function delLetterType(array $data): array { $result = LetterType::where('id', $data['id'])->delete(); if (empty($result)) { return Result::error("删除失败", 0); } else { return Result::success(); } } /** * 检测是否已经被接案 * @param array $data * @return array */ public function checkMeasure(array $data): array { $where = [ 'id' => $data['id'], ]; $letterOfComplaintInfo = LetterOfComplaint::where($where)->first(); var_dump("查询数据:", $letterOfComplaintInfo['admin_id'], $data['user_id']); //操作人和当前登陆用户id 相等说明是当前人接收的案件 if (($letterOfComplaintInfo['admin_id'] == $data['user_id']) || empty($letterOfComplaintInfo['admin_id'])) { return Result::success(); } else { return Result::error("您不能处理其他人已经接过的案件", 0); } } /** * 后台获取职能部门 * @param array $data * @return array */ public function getZhinengbumenList(array $data): array { // 获取分页参数,默认每页 10 条记录 $page = isset($data['page']) ? (int) $data['page'] : 1; $perPage = isset($data['pagesize']) ? (int) $data['pagesize'] : 10; // 查询数据并分页 $query = Department::query(); // 可以在这里添加更多的查询条件 if (isset($data['search'])) { $query->where('name', 'like', '%' . $data['search'] . '%'); } // 执行分页查询 $result = $query->paginate($perPage, ['*'], 'page', $page); // 返回分页结果 return Result::success([ 'count' => $result->total(), 'current_page' => $result->currentPage(), 'last_page' => $result->lastPage(), 'pagesize' => $result->perPage(), 'rows' => $result->items(), ]); } /** * 添加获取职能部门 * @param array $data * @return array */ public function addZhinengbumen(array $data): array { $result = Department::insertGetId($data); if (empty($result)) { return Result::error("创建失败", 0); } else { return Result::success(["id" => $result]); } } public function delZhinengbumen(array $data): array { $result = Department::where('id', $data['id'])->delete(); if (empty($result)) { return Result::error("删除失败", 0); } else { return Result::success(); } } public function getZhinengbumen(array $data): array { $result = Department::where('id', $data['id']) ->orderBy('department', 'desc') ->first(); return Result::success($result); } public function getPidZhinengbumen(array $data): array { if (empty($data['pid'])) { $data['pid'] = 0; } $result = Department::where('pid', $data['pid'])->get(); return Result::success($result); } public function modZhinengbumen(array $data): array { $result = Department::where('id', $data['id'])->update($data); if (empty($result)) { return Result::error("修改失败", 0); } else { return Result::success(); } } /** * 查询职能列表 * @param array $data * @return array */ public function getDepartment(array $data): array { $where = []; if (isset($data['pid'])) { $where = [ 'pid' => $data['pid'] ?? 0 ]; } $result = Department::when(!empty($where), function ($query) use ($where) { $query->where($where); })->orderBy("sort", "desc")->get(); if (empty($result)) { return Result::error("查询失败", 0); } else { return Result::success($result); } } /** * 获取所有的buckets * @param array $data * @return array */ public function getBuckets(array $data): array { $result = new MinioService(); // 调用服务层的方法获取存储桶列表 $bucketsResponse = $result->listBuckets(); // 直接返回服务层生成的响应 return Result::success($bucketsResponse['data']); } /** * 上传文件 * @param array $data * @return array */ public function uploadFile(array $data): array { $result = new MinioService(); $rep = $result->uploadFile($data); if ($rep['code'] == 200) { return Result::success($rep['data']); } else { return Result::error("上传失败!"); } } /** * 黑名单管理 * @param array $data * @return array */ public function getBlackWordList(array $data): array { $result = BlackWord::when($data, function ($query) use ($data) { if (isset($data['name']) && $data['name']) { $query->where('black_word.name', 'like', '%' . $data['name'] . '%'); } })->orderBy('black_word.id', 'desc') ->paginate( intval($data['pageSize']), [ 'black_word.*', ], 'page', intval($data['page']) ); $count = $result->total(); $returnData = [ 'rows' => $result->items(), 'count' => $count ]; return Result::success($returnData); } /** * 添加黑名单 * @param array $data * @return array */ public function addBlackWord(array $data): array { Db::beginTransaction(); try { $info = BlackWord::where(['name' => $data['name']])->first(); if ($info) { Db::rollBack(); return Result::error("该黑名单已存在", 0); } $data['type'] = 10; $result = BlackWord::insertGetId($data); $redisKey = 'black_word'; $this->redis->sAdd($redisKey, $data['name']); Db::commit(); return Result::success(["id" => $result]); } catch (\Exception $e) { Db::rollBack(); return Result::error("创建失败" . $e->getMessage(), 0); } } /** * 删除黑名单 * @param array $data * @return array */ public function delBlackWord(array $data): array { Db::beginTransaction(); try { BlackWord::where(['name' => $data['name']])->delete(); $redisKey = 'black_word'; $this->redis->sRem($redisKey, $data['name']); Db::commit(); return Result::success([]); } catch (\Exception $e) { Db::rollBack(); return Result::error("删除失败" . $e->getMessage(), 0); } } /** * 修改违禁词 * @param array $data * @return array */ public function upBlackWord(array $data): array { Db::beginTransaction(); try { $checkInfo = BlackWord::where(['name' => $data['name']])->first(); if ($checkInfo) { Db::rollBack(); return Result::error("该违禁词已经存在", 0); } $blackWordInfo = BlackWord::where(['id' => $data['id']])->first(); if ($blackWordInfo) { //先删除redis $blackWordInfo = $blackWordInfo->toArray(); $redisKey = 'black_word'; $this->redis->sRem($redisKey, $blackWordInfo['name']); $this->redis->sAdd($redisKey, $data['name']); BlackWord::where(['id' => $data['id']])->update(['name' => $data['name']]); Db::commit(); return Result::success([]); } else { Db::rollBack(); return Result::error("系统错误", 0); } } catch (\Exception $e) { Db::rollBack(); return Result::error("修改失败" . $e->getMessage(), 0); } } /** * 获取风格 * @return void */ public function getTemplateClassList(array $data): array { $where = []; if (isset($data['name']) && $data['name']) { array_push($where, ['template_class.name', 'like', '%' . $data['name'] . '%']); } if (isset($data['keyword']) && $data['keyword']) { array_push($where, ['template_class.keyword', 'like', '%' . $data['keyword'] . '%']); } $template = TemplateClass::when($where, function ($query) use ($where) { $query->where($where); }); $count = $template->count(); // $countQuery = clone $template; $row = $template ->leftJoin('template', function ($join) { $join->on('template_class.class_id', '=', 'template.template_class_id'); }) ->select('template_class.*', DB::raw('COUNT(template.template_class_id) as template_count')) ->groupBy('template_class.id') ->orderBy('template_class.id', 'desc') ->offset(($data['page'] - 1) * $data['pageSize']) ->limit($data['pageSize']) ->get(); $result = [ 'rows' => $row, 'count' => $count, ]; if ($row->isEmpty()) { return Result::error("暂无风格", 0); } else { return Result::success($result); } } /** * 添加风格 * @param * @return void */ public function addTemplateClass(array $data): array { $data['keyword'] = json_encode($data['keyword']); $template_class = TemplateClass::where('name', $data['name']) ->orWhere('class_id', $data['class_id']) ->first(); if ($template_class) { return Result::error("风格名称或者风格编号已存在,不可添加!", 0); } $result = TemplateClass::insertGetId($data); if (empty($result)) { return Result::error("创建风格失败", 0); } else { return Result::success(["id" => $result]); } } /** * 更新风格 * @param array $data * @return array */ public function upTemplateClass(array $data): array { $where = [ 'id' => $data['id'], ]; $template_class = TemplateClass::where($where)->first(); if (empty($template_class)) { return Result::error("未查询到风格", 0); } if ($template_class->type == 1) { return Result::error("默认风格不能修改", 0); } $template = TemplateClass::where('id', '!=', $data['id']) // ->where(['name' => $data['name']]) // ->orWhere(['class_id' => $data['class_id']]) ->where(function ($query) use ($data) { $query->where('name', $data['name']) ->orWhere('class_id', $data['class_id']); }) ->first(); if ($template) { return Result::error("风格名称或者风格编号已存在,不可编辑!", 0); } $updateData = [ 'name' => $data['name'], 'keyword' => json_encode($data['keyword']), 'class_id' => $data['class_id'], ]; $result = TemplateClass::where($where)->update($updateData); if (empty($result)) { return Result::error("更新失败", 0); } else { return Result::success($result); } } /** * 删除风格 * @param array $data * @return array */ public function delTemplateClass(array $data): array { $where = [ 'id' => $data['id'], ]; $template_class = TemplateClass::where($where)->first(); if (empty($template_class)) { return Result::error("未查询到风格", 0); } if ($template_class->type == 1) { return Result::error("默认风格不能删除", 0); } $template = Template::where('template_class_id', $template_class['class_id'])->get(); if (!empty($template->all())) { try { // 默认风格 1 Db::beginTransaction(); $template = Template::where('template_class_id', $template['class_id'])->update(['template_class_id' => 1]); if (empty($template)) { Db::rollBack(); return Result::error("删除失败", 0); } $result = TemplateClass::where($where)->delete(); } catch (\Exception $e) { return Result::error("删除失败" . $e->getMessage(), 0); } } else { $result = TemplateClass::where('id', $data['id'])->delete(); } if (empty($result)) { return Result::error("删除失败", 0); } return Result::success($result); } /** * 根据风格名称-获取获取所有风格 * @param array $data * @return array */ public function getTemplateClass(array $data): array { $where = []; if (!empty($data['template_name'])) { $where['name'] = $data['template_name']; } $result = TemplateClass::where($where)->get(); if (empty($result)) { return Result::error("暂无风格", 0); } return Result::success($result); } /** * 获取皮肤列表 * @param array $data * @return array */ public function getTemplateList(array $data): array { $where = []; if (!empty($data['template_class_id'])) { $where['template_class_id'] = $data['template_class_id']; } if (!empty($data['template_name'])) { array_push($where, ['template_name', 'like', '%' . $data['template_name'] . '%']); } if (!empty($data['template_keyword'])) { array_push($where, ['template_keyword', 'like', '%' . $data['template_keyword'] . '%']); } $result = Template::where($where) ->leftJoin('template_class', 'template.template_class_id', 'template_class.class_id') ->select('template.*', 'template_class.name as template_class_name') ->orderBy('template.template_id','desc') ->paginate($data['page_size'], ['*'], 'mypage_name', $data['page']); if (empty($result)) { return Result::error("暂无皮肤", 0); } return Result::success($result); } public function getTemplateInfo(array $data): array { $result = Template::where('template.id', $data['id']) ->leftJoin('template_class', 'template.template_class_id', 'template_class.class_id') ->select('template.*', 'template_class.name as template_class_name') ->first(); if (empty($result)) { return Result::error("暂无皮肤", 0); } return Result::success($result); } /** * 添加皮肤 * @param array $data * @return array */ public function addTemplate(array $data): array { var_dump($data); unset($data['user_id']); $template = Template::where('template_name', $data['template_name']) ->orWhere('template_id', $data['template_id']) ->first(); if ($template) { return Result::error("皮肤名称或者皮肤编号已存在,不可添加!", 0); } $data['template_keyword'] = json_encode($data['template_keyword']) ?? ''; $page_type = json_decode($data['page_type'], true); $data['page_type'] = json_encode(array_values(array_unique($page_type))); $result = Template::insertGetId($data); if (empty($result)) { return Result::error("创建失败", 0); } else { return Result::success($result); } } /** * 删除皮肤 * @param array $data * @return array */ public function delTemplate(array $data): array { $rector = Sector::where('template_id', $data['id'])->first(); if (!empty($rector)) { return Result::error("此皮肤已被绑定通栏,不可删除", 0); } $template = Template::where('id', $data['id'])->first(); if(empty($template)){ return Result::error("此皮肤不存在!", 0); } $web_template = WebsiteTemplateInfo::where('template_id', $template['template_id'])->first(); if (!empty($web_template)) { return Result::error("此皮肤已被绑定网站,不可删除", 0); } // if ($template->type == 1) { // return Result::error("默认皮肤不能删除", 0); // } $component_img = ComponentImg::where('template_id',$template['template_id'])->get(); if(!empty($component_img) && $component_img->count() > 0){ return Result::error('此皮肤已绑定组件预览图,不可删除!'.$component_img); } $result = Template::where('id', $data['id'])->delete(); var_dump($result, '-------------------delete'); if ($result) { return Result::success($result); } else { return Result::error("删除失败", 0); } } /** * 更新皮肤 * @param array $data * @return array */ public function updateTemplate(array $data): array { unset($data['user_id']); $template = Template::where('id', $data['id']) ->first(); if (empty($template)) { return Result::error("此皮肤不存在!", 0); } $template = Template::where('id', '!=', $data['id']) ->where(function ($query) use ($data) { $query->where('template_name', $data['template_name']) ->orWhere('template_id', $data['template_id']); }) ->first(); if ($template) { return Result::error("皮肤名称或者皮肤编号已存在,不可编辑!", 0); } $data['template_keyword'] = json_encode($data['template_keyword']) ?? ''; $page_type = json_decode($data['page_type'], true); $data['page_type'] = json_encode(array_values(array_unique($page_type))); $result = Template::where('id', $data['id'])->update($data); var_dump($result, '-------------------update'); if (!$result) { return Result::error("更新失败", 0); } else { return Result::success('更新成功'); } } /** * 获取所有尺寸 * @param array $data * @return array */ public function getAllSize(array $data): array { if(isset($data['id']) && !empty($data['id'])){ $result = Size::where('id', $data['id'])->get()->all(); }else{ $result = Size::get()->all(); } if(empty($result)){ return Result::error("暂无尺寸", 0); } return Result::success($result); } /** * 根据皮肤名称-获取所有皮肤 * @param array $data * @return array */ public function getAllTemplate(array $data): array { $where = []; if (isset($data['template_class_id']) && !empty($data['template_class_id'])) { $where[] = ['template_class_id', $data['template_class_id']]; } if (isset($data['template_name']) && !empty($data['template_name'])) { $where[] = ['template_name', 'like', '%' . $data['template_name'] . '%']; } $result = Template::where($where)->get()->all(); if (empty($result)) { return Result::error("暂无皮肤", 0); } return Result::success($result); } /** * 获取通栏列表 * @param array $data * @return array */ public function getSectorList(array $data): array { $where = []; if (isset($data['template_class_id']) && !empty($data['template_class_id'])) { $where['template.template_class_id'] = $data['template_class_id']; } if (isset($data['template_id']) && !empty($data['template_id'])) { array_push($where, ['template.template_id', $data['template_id']]); } if (isset($data['sector_name']) && !empty($data['sector_name'])) { array_push($where, ['sector.sector_name', 'like', '%' . $data['sector_name'] . '%']); } if (isset($data['width']) && !empty($data['width']) && isset($data['height']) && !empty($data['height'])) { $size_id = Size::where('width', 'like', '%' . $data['width'] . '%')->where('height', 'like', '%' . $data['height'] . '%')->pluck('id'); } else if (isset($data['width']) && !empty($data['width'])) { $size_id = Size::where('width', 'like', '%' . $data['width'] . '%')->pluck('id'); } else if (isset($data['height']) && !empty($data['height'])) { $size_id = Size::where('height', 'like', '%' . $data['height'] . '%')->pluck('id'); } else { $size_id = []; } if(isset($data['page_type']) && !empty($data['page_type'])){ array_push($where, ['sector.page_type', 'like', '%' . $data['page_type'] . '%']); } $result = Sector::when(!empty($size_id), function ($query) use ($size_id) { $query->whereIn('sector.size_id', $size_id); }) ->leftJoin('size', 'size.id', '=', 'sector.size_id') ->leftJoin('template', 'template.template_id', '=', 'sector.template_id') ->leftJoin('template_class', 'template_class.class_id', '=', 'template.template_class_id') ->leftJoin('sector_place', 'sector_place.sector_type', '=', 'sector.place_type') ->where($where) ->where('sector_place.type_id', 1) ->select('sector.*', 'size.width', 'size.height', 'template_class.name as class_name', 'template_class.class_id', 'template.template_name', 'sector_place.name as place_name', 'sector_place.sector_img as place_img') ->orderBy('sector.id', 'desc') ->paginate($data['page_size'], ['*'], 'page', $data['page']); if(empty($result)){ return Result::error('暂无通栏!'); } return Result::success($result); } /** * 获取通栏详情 * @param array $data * @return array */ public function getSectorInfo(array $data): array { // $where = []; $where[] = ['sector.id', '=', $data['id']]; $result = Sector::where($where) ->leftJoin('template', 'template.template_id', '=', 'sector.template_id') ->select('sector.*','template.template_name','template.template_id') ->orderBy('sector.id', 'desc') ->first(); $result['component'] = SectorComponent::where('sector_component.sector_id', $data['id']) ->leftJoin('component', 'component.component_type', '=', 'sector_component.component_id') ->select('component.component_type','sector_component.sort_id','component.component_name') ->get()->all(); // $sorts = array_values(array_unique(array_column($components, 'sort_id'))); // $result['component'] = $components; $result['place'] = SectorPlace::where('sector_place.sector_type', $result['place_type']) ->where('sector_place.type_id',1) ->select('sector_place.sector_img','sector_place.map') ->first(); // $key_num = 0; // foreach($result['place'] as $key => $value){ // if($value['type_id'] == 1){ // $sector_place['sector_plate'] = $value; // }else{ // $sector_place['component_place'][$key_num] = $value; // $key_num++; // } // } // $result['place'] = $sector_place; if (empty($result)) { return Result::error('此通栏不存在!'); } return Result::success($result); } /** * 获取经纬度信息 * @return void */ public function getIpInfo(array $data): array { if (empty($data['ip'])) { // 在RPC服务中,依赖$_SERVER是不可靠的,强制要求调用方传入IP return Result::error('IP地址不能为空!'); } $client_ip = $data['ip']; try { // 直接从容器获取 Guzzle 客户端实例 /** @var \GuzzleHttp\Client $client */ $client = \Hyperf\Context\ApplicationContext::getContainer()->get(Client::class); // 使用新的 GeoJS API 地址 $api_url = "https://get.geojs.io/v1/ip/geo/{$client_ip}.json"; // 将 timeout 作为请求选项传递 $response = $client->get($api_url, ['timeout' => 5]); if ($response->getStatusCode() !== 200) { return Result::error('IP查询接口请求失败,状态码:' . $response->getStatusCode()); } $ip_info = json_decode($response->getBody()->getContents(), true); // GeoJS 的成功响应不包含 'status' 字段,直接检查关键数据是否存在 if ($ip_info && isset($ip_info['latitude']) && isset($ip_info['longitude'])) { // GeoJS 返回的经纬度是字符串,转换为浮点数 $ip_info['latitude'] = floatval($ip_info['latitude']); $ip_info['longitude'] = floatval($ip_info['longitude']); return Result::success($ip_info); } // 如果返回的数据不符合预期,提供一个通用错误 $errorMessage = $ip_info['message'] ?? '获取IP地理位置信息失败'; return Result::error($errorMessage); } catch (GuzzleException $e) { // 这个异常会给出详细的网络错误信息,如超时、无法解析域名等 return Result::error('请求IP查询接口异常: ' . $e->getMessage()); } catch (\Throwable $e) { return Result::error('获取IP信息时发生未知错误: ' . $e->getMessage()); } } /** * 获取天气 * @param array $data * @return array */ public function getWeatherInfo(array $data): array { $month = $data['month'] ?? date('m'); $day = $data['day'] ?? date('d'); // 使用缓存键 $cacheKey = "tsbb_data_weather_{$month}_{$day}"; // 尝试从缓存获取数据 $container = \Hyperf\Context\ApplicationContext::getContainer(); $cache = $container->get(\Psr\SimpleCache\CacheInterface::class); if ($cachedData = $cache->get($cacheKey)) { return Result::success(unserialize($cachedData)); } $location = $data['latitude'] . ":" . $data['longitude']; $api_url = "https://api.seniverse.com/v3/weather/now.json?key=" . \Hyperf\Support\env('WEATHER_KEY') . "&location=" . $location . "&language=zh-Hans&unit=c"; $ch = curl_init($api_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch); // 解析 JSON 响应 $WeatherInfo = json_decode($response, true); // 缓存结果,设置1小时过期 $cache->set($cacheKey, serialize($WeatherInfo), 3600); if ($WeatherInfo) { return Result::success($WeatherInfo); } else { return Result::error("获取天气失败", 0); } } /** * 获取农历信息 * @return void */ public function getCalendar(array $data): array { $calendar = new Calendar(); $result = $calendar->solar($data['year'], $data['month'], $data['day'], $data['hour']); // 阳历 return Result::success($result); } /** * 添加通栏 * @param array $data * @return array */ public function addSector(array $data): array { unset($data['user_id']); // 对传入的 page_type 数组进行去重、转换为整数并重新索引 $data['page_type'] = array_values(array_unique(array_map('intval', $data['page_type']))); $data['page_type'] = json_encode($data['page_type']); $template = Template::where('template_id', $data['template_id']) ->first(); if (empty($template)) { return Result::error('皮肤不存在!'); } $sector_id = Sector::where('sector_id',$data['sector_id'])->where('template_id',$data['template_id'])->first(); if(!empty($sector_id)){ return Result::error('通栏编号已存在!'); } $sector_place = SectorPlace::where('sector_type',$data['place_type'])->get(); if(empty($sector_place)){ return Result::error('位置不存在!'); } $sort = json_decode($data['component_code'],true); $sector = 0; // $flattened = []; // $sector = []; $sector_component = []; foreach ($sort as $key => $subArray) { foreach ($subArray as $k => $item) { // // $component[] = $item; $sector_component[] = [ 'sector_id' => $data['sector_id'], 'component_id' => $subArray[$k], 'sort_id' => $key + 1, 'place_id' => $data['place_type'], // 'sectorid' => $data['id'], ]; // // // ---------同步通栏的sort_id到组件之中,目前已废弃,涉及到批量修改------ // // $componentTypes[] = $item; // // $components[] = [ // // 'sort_id' => $key + 1, // // 'component_type' => $item, // // ]; // // $place_ids[] = $data['place_type']; } } // $component_num = count($component); // $count = count(array_unique($component)); // 使用array_map批量替换sector_id // return Result::success($sector_component); if(count($sort) != $data['component_num']){ return Result::error('组件关联错误!'); } // 通栏分类:1:资讯类:2:通栏广告类;3:混合类;4:头条类;5:轮播图类; $sector_code = [ $data['sector_id'] => [ 'sectorName' => $data['sector_name'], 'sectorHeight' => $data['pic_height'], 'sectorImg' => $data['sector_img'], ] ]; // return Result::success($sector_code); $data['sector_code'] = json_encode($sector_code); Db::beginTransaction(); try { $sector = Sector::insertGetId($data); if (empty($sector)) { Db::rollBack(); return Result::error('添加失败'); } $sector_com = array_map(function ($item) use ($sector) { // 替换当前记录的sector_id为变量值(保留其他字段不变) $item['sectorid'] = $sector; return $item; }, $sector_component); // Db::rollBack(); // return Result::success($sector_com); $com_sector = SectorComponent::insert($sector_com); if(empty($com_sector)){ Db::rollBack(); return Result::error('通栏关联组件失败!'); } // ---------同步通栏的sort_id到组件之中,目前已废弃,涉及到批量修改------ // 查询数据库中已存在的component_type(只保留存在的记录) // $existingTypes = Component::whereIn('component_type', $componentTypes) // ->pluck('component_type') // 提取存在的component_type // ->toArray(); // if(empty($existingTypes)){ // Db::rollBack(); // return Result::error('组件不存在!'); // } // // var_dump($existingTypes); // // 3. 过滤$components数组,只保留数据库中已存在的记录(核心:避免新增) // $updateData = array_filter($components, function ($item) use ($existingTypes) { // return in_array($item['component_type'], $existingTypes); // }); // // 4. 执行upsert(此时$updateData中只有已存在的记录,不会新增) // if (!empty($updateData)) { // $component_sort = Component::upsert($updateData,['component_type'],['sort_id']); // } // if(empty($component_sort)){ // Db::rollBack(); // return Result::error('组件位置关联失败!'); // } Db::commit(); return Result::success($sector); } catch (\Exception $e) { Db::rollBack(); return Result::error($e->getMessage()); } } /** * 删除通栏 * @param array $data * @return array */ public function delSector(array $data): array { $sector = Sector::where('id', $data['id'])->first(); if (empty($sector)) { return Result::error('通栏不存在!'); } Db::beginTransaction(); try { $component_id = SectorComponent::where('sectorid', $data['id'])->delete(); if (empty($component_id)) { Db::rollBack(); return Result::error('解除相关组件关联关系失败!'); } $result = Sector::where('id', $data['id'])->delete(); if (empty($result)) { Db::rollBack(); return Result::error('通栏删除失败!'); } Db::commit(); return Result::success('通栏删除成功!'); } catch (\Exception $e) { Db::rollBack(); return Result::error($e->getMessage()); } } /** * 修改通栏 * @param array $data * @return array */ public function updateSector(array $data): array { unset($data['user_id']); $sector = Sector::where('id', $data['id'])->first(); if (empty($sector)) { return Result::error('通栏不存在!'); } if($sector['sector_id'] != $data['sector_id']){ $sector_id = Sector::where('sector_id',$data['sector_id'])->where('template_id',$data['template_id'])->first(); if(!empty($sector_id)){ return Result::error('通栏编号已存在!'); } } $sector_code = [ $data['sector_id'] => [ 'sectorName' => $data['sector_name'], 'sectorHeight' => $data['pic_height'], 'sectorImg' => $data['sector_img'], ] ]; // return Result::success($sector_code); $data['sector_code'] = json_encode($sector_code); // 对传入的 page_type 数组进行去重、转换为整数并重新索引 $data['page_type'] = array_values(array_unique(array_map('intval', $data['page_type']))); $data['page_type'] = json_encode($data['page_type']); $template = Template::where('template_id', $data['template_id']) ->whereRaw("JSON_CONTAINS(template.page_type, ?)", [$data['page_type']]) ->first(); if (empty($template)) { return Result::error('皮肤不存在!'); } $sort = json_decode($data['component_code'],true); $sector_component = []; foreach ($sort as $key => $subArray) { foreach ($subArray as $k => $item) { // // $component[] = $item; $sector_component[] = [ 'sector_id' => $data['sector_id'], 'component_id' => $subArray[$k], 'sort_id' => $key + 1, 'place_id' => $data['place_type'], 'sectorid' => $data['id'], ]; // // // ---------同步通栏的sort_id到组件之中,目前已废弃,涉及到批量修改------ // // $componentTypes[] = $item; // // $components[] = [ // // 'sort_id' => $key + 1, // // 'component_type' => $item, // // ]; // // $place_ids[] = $data['place_type']; } } if(count($sort) != $data['component_num']){ return Result::error('组件关联错误!'); } $data['sector_code'] = json_encode($sector_code); Db::beginTransaction(); try { $com_sector = SectorComponent::where('sectorid', $data['id'])->pluck('component_id'); if(empty($com_sector)){ Db::rollBack(); return Result::error('通栏解除组件关联失败!'); } $del_SectorComponent = SectorComponent::where('sectorid', $data['id'])->delete(); if(empty($del_SectorComponent)){ Db::rollBack(); return Result::error('通栏解除组件关联失败!1'); } $sector = Sector::where('id', $data['id'])->update($data); if (empty($sector)) { Db::rollBack(); return Result::error('修改失败'); } $sector_com = array_map(function ($item) use ($data) { // 替换当前记录的sector_id为变量值(保留其他字段不变) $item['sectorid'] = $data['id']; return $item; }, $sector_component); // var_dump($sector_com); $com_sector = SectorComponent::insert($sector_com); if(empty($com_sector)){ Db::rollBack(); return Result::error('通栏关联组件失败!'); } // ---------同步通栏的sort_id到组件之中,目前已废弃,涉及到批量修改------ // $up_component = Component::whereIn('component_id',$sector_component['component_type']) // ->update(['status' => 2,]); // if(empty($up_component)){ // Db::rollBack(); // return Result::error('通栏关联组件失败!'); // } // 2. 查询数据库中已存在的component_type(只保留存在的记录) // $existingTypes = Component::whereIn('component_type', $componentTypes) // ->pluck('component_type') // 提取存在的component_type // ->toArray(); // // 3. 过滤$components数组,只保留数据库中已存在的记录(核心:避免新增) // $updateData = array_filter($components, function ($item) use ($existingTypes) { // return in_array($item['component_type'], $existingTypes); // }); // // 4. 执行upsert(此时$updateData中只有已存在的记录,不会新增) // if (!empty($updateData)) { // $component_sort = Component::upsert($updateData,['component_type'],['sort_id']); // } // if(empty($component_sort)){ // Db::rollBack(); // return Result::error('组件位置关联失败!'); // } Db::commit(); return Result::success($sector); } catch (\Exception $e) { Db::rollBack(); return Result::error($e->getMessage()); } } public function getComponentList(array $data): array { $where = []; if (isset($data['component_name']) && !empty($data['component_name'])) { array_push($where, ['component.component_name', 'like', '%' . $data['component_name'] . '%']); } if (isset($data['width']) && !empty($data['width']) && isset($data['height']) && !empty($data['height'])) { array_push($where, ['component.component_width', 'like', '%' . $data['width'] . '%']); array_push($where, ['component.component_height', 'like', '%' . $data['height'] . '%']); } else if (isset($data['width']) && !empty($data['width'])) { array_push($where, ['component.component_width', 'like', '%' . $data['width'] . '%']); } else if (isset($data['height']) && !empty($data['height'])) { array_push($where, ['component.component_height', 'like', '%' . $data['height'] . '%']); } else { } if(isset($data['type_id']) && !empty($data['type_id'])){ $where['component.type_id'] = $data['type_id']; } $result = Component::where($where) ->leftJoin('component_type', 'component_type.id', '=', 'component.type_id') ->leftJoin('component_img', 'component.component_type', '=', 'component_img.component_id') ->select( 'component.*', 'component_type.com_typename', DB::raw('COUNT(component_img.id) as img_count') ) ->groupBy('component.id') // 按组件ID分组,确保统计每个组件的图片数量 ->orderBy('component.updated_at', 'desc') ->paginate($data['page_size'], ['*'], 'mypage_name', $data['page']); if(empty($result)){ return Result::error('暂无数据'); } return Result::success($result); } public function getComponentInfo(array $data): array { $where = ['component.id'=>$data['id']]; $result = Component::where($where) ->leftJoin('component_type','component_type.id','=','component.type_id') ->select( 'component.*','component_type.com_typename') ->get(); if(empty($result)){ return Result::error('暂无数据'); } return Result::success($result); } /* 新增组件 */ public function addComponent(array $data): array { unset($data['user_id']); $component_id = Component::where('component_type',$data['component_type'])->first(); if(!empty($component_id)){ return Result::error('组件编号已存在!'); } $component_type = ComponentType::where('id',$data['type_id'])->first(); if(empty($component_type)){ return Result::error('组件分类不存在!'); } // '组件分类:1:资讯-头条组件;2:资讯-轮播组件;3:资讯-推荐图类组件;4:资讯-最新类组件;5:资讯-推荐类;6:资讯-热点类组件;7:资讯-栏目类组件; // 8:列表类组件;9:详情类组件;10:二级导航栏类组件;11:广告类;12:静态资源类;13:底部导航类;14:广告资讯混合类 $data['type_id'] = intval($data['type_id']); $add_arr = [ // 'template_id' => intval($data['template_id']), 'component_type' => intval($data['component_type']), 'component_name' => $data['component_name'], 'component_img' => $data['component_img'], 'component_width' => $data['component_width'], 'component_height' => $data['component_height'], 'type_id' => intval($data['type_id']), 'component_keyword' => $data['component_keyword'], ]; $component_head_code = [ 'component_id' => $add_arr['component_type'], 'component_type' => $add_arr['type_id'], 'component_name' => $add_arr['component_name'], 'component_style' => 1, ]; $add_arr['component_code'] = json_encode($component_head_code,true); switch($data['type_id']){ case 1: //1:资讯-头条组件; case 2: //2:资讯-轮播组件; case 3: //3:资讯-推荐图类组件; case 4: //4:资讯-最新类组件; case 5: //5:资讯-推荐类组件; case 6: //6:资讯-热点类组件; // $component_data['componentData']['name'] = ''; $add_arr['level'] = $data['type_id']; $add_arr['img_num'] = intval($data['img_num']); $add_arr['text_num'] = intval($data['text_num']); $component_data['componentData'] = [ // 'category_id' => '', 'level' => $add_arr['level'], 'name' => '', // 'category_arr' => [], 'imgSize' => $data['img_num'] ?? '', 'textSize' => $data['text_num'] ?? '', 'child' => [ 'id' => '', 'imgSize' => '', 'textSize' => '' ] ]; if($data['type_id'] == 4){ $component_data['componentData']['name'] = '最新资讯'; } if($data['type_id'] == 5 || $data['type_id'] == 6){ $component_data['componentData']['name'] = '热点精选'; } $add_arr['component_data'] = json_encode($component_data, true); $add_arr['component_column'] = $component_type['com_code'] ?? ''; break; case 7: //7:资讯-栏目类组件; case 15: //15:滚动图文类; $add_arr['img_num'] = intval($data['img_num']); $add_arr['text_num'] = intval($data['text_num']); $add_arr['child_imgnum'] = intval($data['child_imgnum'] ?? null); $add_arr['child_textnum'] = intval($data['child_textnum'] ?? null); $component_data['componentData'] = [ 'category_id' => '', 'level' => '', 'name' => '', 'category_arr' => [], 'imgSize' => $add_arr['img_num'] ?? '', 'textSize' => $add_arr['text_num'] ?? '', 'child' => [ 'id' => '', 'imgSize' => $add_arr['child_imgnum'] ?? '', 'textSize' => $add_arr['child_textnum'] ?? '', ] ]; $add_arr['component_data'] = json_encode($component_data,true); $add_arr['component_column'] = $component_type['com_code'] ?? ''; break; case 8: //8:列表类组件; $add_arr['pageSize'] = intval($data['pageSize']); $component_data['componentData'] = [ 'category_id' => '', 'pageType' => [ 'page' => 1, 'pageSize' => $add_arr['pageSize'] ?? '', ] ]; $add_arr['component_data'] = json_encode($component_data,true); $add_arr['component_column'] = $component_type['com_code'] ?? ''; break; case 9: //9:详情类组件; $component_data['componentData'] = [ 'article_id' => '', ]; $add_arr['component_data'] = json_encode($component_data,true); $add_arr['component_column'] = $component_type['com_code'] ?? ''; break; case 10: //10:二级导航类组件; $add_arr['cate_place'] = 1; $add_arr['num'] = intval($data['num']); $component_data['componentData'] = [ 'pid' => '', 'placeid' => 1, 'num' => $add_arr['num'], ]; $add_arr['component_data'] = json_encode($component_data,true); $add_arr['component_column'] = $component_type['com_code'] ?? ''; break; case 11: //11:广告类; $add_arr['ad_width'] = intval($data['ad_width']); $add_arr['ad_height'] = intval($data['ad_height']); $add_arr['component_width'] = intval($data['ad_width']); $add_arr['component_height'] = intval($data['ad_height']); $add_arr['ad_type'] = intval($data['ad_type']); // $add_arr['ad_img'] = $data['ad_img']; $ad = [ 'width' => $add_arr['ad_width'], 'height' => $add_arr['ad_height'], 'name' => '', 'price' => '', 'introduce' => '', 'website_id' => '', // 'thumb' => $add_arr['ad_img'], 'typeid' => $add_arr['ad_type'], 'ad_tag' => '', ]; $add_arr['ad'] = json_encode($ad,true); $add_arr['component_column'] = $component_type['com_code'] ?? ''; // $add_arr['adimg_info'] = $data['img_info']; // $ad_imginfo = json_decode($data['img_info'],true); break; // case 13: //13:底部导航类; case 17: //17:单页列表类; case 18: //18:单页详情类; $component_data['componentData'] = [ 'fcat_id' => '', ]; $add_arr['component_data'] = json_encode($component_data,true); $add_arr['component_column'] = $component_type['com_code'] ?? ''; break; case 14: $add_arr['ad_width'] = intval($data['ad_width']); $add_arr['ad_height'] = intval($data['ad_height']); $add_arr['component_width'] = intval($data['component_width']); $add_arr['component_height'] = intval($data['component_height']); $add_arr['ad_type'] = intval($data['ad_type']); $add_arr['ad_img'] = $data['ad_img']; $add_arr['img_num'] = intval($data['img_num']); $add_arr['text_num'] = intval($data['text_num']); $add_arr['child_imgnum'] = intval($data['child_imgnum'] ?? null); $add_arr['child_textnum'] = intval($data['child_textnum'] ?? null); $component_data['componentData'] = [ 'category_id' => '', 'level' => '', 'name' => '', 'category_arr' => [], 'imgSize' => $data['img_num'] ?? '', 'textSize' => $data['text_num'] ?? '', 'child' => [ 'id' => '', 'imgSize' => $data['child_imgnum'] ?? '', 'textSize' => $data['child_textnum'] ?? '', ] ]; $ad = [ 'width' => $add_arr['ad_width'], 'height' => $add_arr['ad_height'], 'name' => '', 'price' => '', 'introduce' => '', 'website_id' => '', 'thumb' => $add_arr['ad_img'], 'typeid' => $add_arr['ad_type'], 'ad_tag' => '', ]; $add_arr['ad'] = json_encode($ad,true); $add_arr['component_data'] = json_encode($component_data,true); $add_arr['component_column'] = $component_type['com_code'] ?? ''; // $add_arr['adimg_info'] = $data['img_info']; // $ad_imginfo = json_decode($data['img_info'],true); break; default: break; } if($data['type_id'] == 11 || $data['type_id'] == 14){ Db::beginTransaction(); try{ // $website_img = WebsiteImg::insertGetId($ad_imginfo); // var_dump($website_img); // $add_arr['ad_imgid'] = $website_img; // if(empty($website_img)){ // Db::rollBack(); // return Result::error('广告默认图上传失败!'); // } $result = Component::insertGetId($add_arr); if(empty($result)){ Db::rollBack(); return Result::error('添加失败!'); } Db::commit(); }catch(\Exception $e){ Db::rollback(); return Result::error('添加失败!'); } }else{ $result = Component::insertGetId($add_arr); if(empty($result)){ return Result::error('添加组件失败!'); } } return Result::success($result); } public function delComponent(array $data): array { $component = Component::where('id', $data['id'])->first(); if(empty($component)){ return Result::error('组件不存在!'); } $component_id = SectorComponent::where('component_id', $component['component_type'])->first(); if (!empty($component_id)) { return Result::error('请先解除相关组件关联关系!'); } Db::beginTransaction(); try{ // if($component['type_id'] == 11 || $component['type_id'] == 14){ // $ad_img = WebsiteImg::where('id',$component['ad_imgid'])->first(); // if(!empty($ad_img)){ // $ad_img = WebsiteImg::where('id',$component['ad_imgid'])->delete(); // if(empty($ad_img)){ // Db::rollBack(); // return Result::error('删除广告默认图失败!'); // } // } // } $component_img = ComponentImg::where('component_id',$component['component_type'])->first(); if(!empty($component_img)){ $del_img = ComponentImg::where('component_id',$component['component_type'])->delete(); if(empty($del_img)){ Db::rollBack(); return Result::error('删除组件预览图失败!'); } } $result = Component::where('id',$data['id'])->delete(); if(empty($result)){ Db::rollBack(); return Result::error('删除失败!'); } Db::commit(); return Result::success($result); }catch(\Exception $e){ Db::rollback(); return Result::error('删除失败!'); } } public function updateComponent(array $data): array { unset($data['user_id']); $id = $data['id']; unset($data['id']); $component_id = Component::where('id','!=',$id)->where('component_type',$data['component_type'])->first(); if(!empty($component_id) || $component_id != null){ return Result::error('组件编号已存在!'); } $component_type = ComponentType::where('id',$data['type_id'])->first(); if(empty($component_type)){ return Result::error('组件分类不存在!'); } $component = Component::where('id',$id)->first(); if(empty($component)){ return Result::error('组件不存在!'); } // 组件分类:1:资讯-头条组件;2:资讯-轮播组件;3:资讯-推荐图类组件;4:资讯-最新类组件;5:资讯-推荐类;6:资讯-热点类组件; // 7:资讯-栏目类组件;8:列表类组件;9:详情类组件;10:二级导航栏类组件;11:广告类;12:静态资源类;13:底部导航类; $data['type_id'] = intval($data['type_id']); $add_arr = [ // 'template_id' => intval($data['template_id']), 'component_type' => intval($data['component_type']), 'component_name' => $data['component_name'], 'component_img' => $data['component_img'], 'component_width' => $data['component_width'], 'component_height' => $data['component_height'], 'type_id' => intval($data['type_id']), 'component_keyword' => $data['component_keyword'], ]; $component_head_code = [ 'component_id' => $add_arr['component_type'], 'component_type' => $add_arr['type_id'], 'component_name' => $add_arr['component_name'], 'component_style' => 1, ]; $add_arr['component_code'] = json_encode($component_head_code,true); switch($data['type_id']){ case 1: //1:资讯-头条组件; case 2: //2:资讯-轮播组件; case 3: //3:资讯-推荐图类组件; case 4: //4:资讯-最新类组件; case 5: //5:资讯-推荐类组件; case 6: //6:资讯-热点类组件; // $component_data['componentData']['name'] = ''; $add_arr['level'] = $data['type_id']; $add_arr['img_num'] = intval($data['img_num']); $add_arr['text_num'] = intval($data['text_num']); $component_data['componentData'] = [ // 'category_id' => '', 'level' => $add_arr['level'], // 'category_arr' => [], 'name' => '', 'imgSize' => $data['img_num'] ?? '', 'textSize' => $data['text_num'] ?? '', 'child' => [ 'id' => '', 'imgSize' => '', 'textSize' => '' ] ]; if($data['type_id'] == 4){ $component_data['componentData']['name'] = '最新资讯'; } if($data['type_id'] == 5 || $data['type_id'] == 6){ $component_data['componentData']['name'] = '热点精选'; } $add_arr['component_data'] = json_encode($component_data, true); $add_arr['component_column'] = $component_type['com_code'] ?? ''; break; case 7: //7:资讯-栏目类组件; case 15: //15:滚动图文类; $add_arr['img_num'] = intval($data['img_num']); $add_arr['text_num'] = intval($data['text_num']); $add_arr['child_imgnum'] = intval($data['child_imgnum'] ?? null); $add_arr['child_textnum'] = intval($data['child_textnum'] ?? null); $component_data['componentData'] = [ 'category_id' => '', 'level' => '', 'name' => '', 'category_arr' => [], 'imgSize' => $add_arr['img_num'] ?? '', 'textSize' => $add_arr['text_num'] ?? '', 'child' => [ 'id' => '', 'imgSize' => $add_arr['child_imgnum'] ?? '', 'textSize' => $add_arr['child_textnum'] ?? '', ] ]; $add_arr['component_data'] = json_encode($component_data,true); $add_arr['component_column'] = $component_type['com_code'] ?? ''; break; case 8: //8:列表类组件; $add_arr['pageSize'] = intval($data['pageSize']); $component_data['componentData'] = [ 'category_id' => '', 'pageType' => [ 'page' => 1, 'pageSize' => $add_arr['pageSize'] ?? '', ] ]; $add_arr['component_data'] = json_encode($component_data,true); $add_arr['component_column'] = $component_type['com_code'] ?? ''; break; case 9: //9:详情类组件; $component_data['componentData'] = [ 'article_id' => '', ]; $add_arr['component_data'] = json_encode($component_data,true); $add_arr['component_column'] = $component_type['com_code'] ?? ''; break; case 10: //10:二级导航类组件; $add_arr['cate_place'] = 1; $add_arr['num'] = intval($data['num']); $component_data['componentData'] = [ 'pid' => '', 'placeid' => 1, 'num' => $add_arr['num'], ]; $add_arr['component_data'] = json_encode($component_data,true); $add_arr['component_column'] = $component_type['com_code'] ?? ''; break; case 11: //11:广告类; $add_arr['ad_width'] = intval($data['ad_width']); $add_arr['ad_height'] = intval($data['ad_height']); $add_arr['component_width'] = intval($data['ad_width']); $add_arr['component_height'] = intval($data['ad_height']); $add_arr['ad_type'] = intval($data['ad_type']); // $add_arr['ad_img'] = $data['ad_img']; $ad = [ 'width' => $add_arr['ad_width'], 'height' => $add_arr['ad_height'], 'name' => '', 'price' => '', 'introduce' => '', 'website_id' => '', 'thumb' => $add_arr['ad_img'], // 'typeid' => $add_arr['ad_type'], 'ad_tag' => '', ]; $add_arr['ad'] = json_encode($ad,true); $add_arr['component_column'] = $component_type['com_code'] ?? ''; // $add_arr['adimg_info'] = $data['img_info']; // $ad_imginfo = json_decode($data['img_info'],true); break; // case 13: //13:底部导航类; case 17: //17:单页列表类; case 18: //18:单页详情类; $component_data['componentData'] = [ 'fcat_id' => '', ]; $add_arr['component_data'] = json_encode($component_data,true); $add_arr['component_column'] = $component_type['com_code'] ?? ''; break; case 14: $add_arr['ad_width'] = intval($data['ad_width']); $add_arr['ad_height'] = intval($data['ad_height']); $add_arr['component_width'] = intval($data['component_width']); $add_arr['component_height'] = intval($data['component_height']); $add_arr['ad_type'] = intval($data['ad_type']); $add_arr['ad_img'] = $data['ad_img']; $add_arr['img_num'] = intval($data['img_num']); $add_arr['text_num'] = intval($data['text_num']); $add_arr['child_imgnum'] = intval($data['child_imgnum'] ?? null); $add_arr['child_textnum'] = intval($data['child_textnum'] ?? null); $component_data['componentData'] = [ 'category_id' => '', 'level' => '', 'name' => '', 'category_arr' => [], 'imgSize' => $data['img_num'] ?? '', 'textSize' => $data['text_num'] ?? '', 'child' => [ 'id' => '', 'imgSize' => $data['child_imgnum'] ?? '', 'textSize' => $data['child_textnum'] ?? '', ] ]; $ad = [ 'width' => $add_arr['ad_width'], 'height' => $add_arr['ad_height'], 'name' => '', 'price' => '', 'introduce' => '', 'website_id' => '', 'thumb' => $add_arr['ad_img'], 'typeid' => $add_arr['ad_type'], 'ad_tag' => '', ]; $add_arr['ad'] = json_encode($ad,true); $add_arr['component_data'] = json_encode($component_data,true); $add_arr['component_column'] = $component_type['com_code'] ?? ''; // $add_arr['adimg_info'] = $data['img_info']; // $ad_imginfo = json_decode($data['img_info'],true); break; default: break; } // var_dump("ad_imginfo:",$ad_imginfo); $cll_column = [ 'img_num' => null, 'text_num' => null, 'child_imgnum' => null, 'child_textnum' => null, 'category_id' => null, 'child_id' => null, 'num' => null, 'level' => null, 'fcatid' => null, 'article_id' => null, 'cate_place' => null, 'pageSize' => null, // 'ad_img' => null, 'ad_width' => null, 'ad_height' => null, 'ad' => '', 'ad_type' => null, // 'ad_imgid' => null, 'adimg_info' => '[]', ]; Db::beginTransaction(); try{ // return Result::success($add_arr); // if(($component['type_id'] == 11 || $component['type_id'] == 14) && ($data['type_id'] != 11 && $data['type_id'] != 14)){ // // $website_img = WebsiteImg::where('id',$component['ad_imgid'])->delete(); // if(empty($website_img)){ // Db::rollBack(); // return Result::error('广告默认图删除失败!'); // } // var_dump("删除",$website_img); // } // if(($data['type_id'] == 11 || $data['type_id'] == 14) && ($component['type_id'] != 11 && $component['type_id'] != 14)){ // $website_img = WebsiteImg::insertGetId($ad_imginfo); // $add_arr['ad_imgid'] = $website_img; // if(empty($website_img)){ // Db::rollBack(); // return Result::error('广告默认图上传失败!'); // } // var_dump("上传",$website_img); // } // if(($component['type_id'] == 11 || $component['type_id'] == 14) && ($data['type_id'] == 11 || $data['type_id'] == 14)){ // $website_img = WebsiteImg::where('id',$component['ad_imgid'])->first(); // if(empty($website_img)){ // $website_img = WebsiteImg::insertGetId($ad_imginfo); // $add_arr['ad_imgid'] = $website_img; // }else{ // $website_img = WebsiteImg::where('id',$component['ad_imgid'])->update($ad_imginfo); // $add_arr['ad_imgid'] = $component['ad_imgid']; // } // if(empty($website_img)){ // Db::rollBack(); // return Result::error('广告默认图修改失败!'); // } // } $add_arr = array_merge($cll_column,$add_arr); $result = Component::where('id',$id)->update($add_arr); if(empty($result)){ Db::rollBack(); return Result::error('修改失败!'); } Db::commit(); }catch(\Exception $e){ Db::rollBack(); return Result::error('修改失败!'); } return Result::success($result); } public function getWebsiteTemplateInfo(array $data) { $where = []; if (isset($data['id'])) { $where[] = ['id', '=', $data['id']]; } $result = WebsiteTemplateInfo::where($where) ->leftJoin('website', 'website_template_info.website_id', '=', 'website.id') ->leftJoin('website_template', 'website_template_info.template_id', '=', 'website_template.id') ->select('website_template_info.*', 'website.website_name') ->get(); if ($result) { return Result::success($result); } else { return Result::error('暂无数据'); } } /** * 自助建站----1.获取页面类型回显 * @param array $data * @return array */ public function getWebPageType(array $data): array { // 1:图片 2:文字 3:底部 $friend_link = Link::where('website_id', $data['website_id'])->where('type', 3)->get(); if (empty($friend_link->toArray())) { return Result::error('暂无友情链接数据!'); } $footer_category = FooterCategory::where('website_id', $data['website_id'])->get(); if (empty($footer_category->toArray())) { return Result::error('暂无底部导航数据!'); } $page_type = WebsiteTemplateInfo::where('website_id', $data['website_id'])->value('page_type'); if (empty($page_type)) { $page_type = []; } else { $page_type = json_decode($page_type, true); } // '网站页面类型(存数组)1:首页 2:分类页 3:列表页 4:详情页 5:搜索页 6:特殊列表 7:特殊详情页 if (isset($page_type) && in_array(5, $page_type)) { $page_type = 1; } else { $page_type = 0; } $result = [ 'friend_link' => $friend_link, 'footer_category' => $footer_category, 'page_type' => $page_type, ]; return Result::success($result); } /** * 自助建站----2.添加页面类型 * @param array $data * @return array */ public function addWebPageType(array $data): array { // return Result::success($data); // 1:首页 2:频道页 3:列表页 4:详情页 5:搜索页 6:特殊列表 7:特殊详情页' if ($data['is_search'] == 1) { $page_type = json_encode([1,2,3,4,5,6,7]); } else { $page_type = json_encode([1,2,3,4,6,7]); } // 0:未构建;1:已填写基础信息;2:已选择模板; action_id $website_template_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first(); $website_template = WebsiteTemplate::where('website_id', $data['website_id'])->first(); Db::beginTransaction(); try{ if (empty($website_template_info)) { $result['template_info'] = WebsiteTemplateInfo::create([ 'website_id' => $data['website_id'], 'page_type' => $page_type, 'user_id' => $data['user_id'] ])->id; } else { if ($website_template_info['status'] == 2) { Db::rollBack(); return Result::error('网站已应用,不可再次修改!'); } $result['template_info'] = WebsiteTemplateInfo::where('website_id', $data['website_id'])->update(['page_type' => $page_type, 'action_id' => 1, 'user_id' => $data['user_id']]); } if (!$result['template_info']) { Db::rollBack(); return Result::error('网站所选页面添加失败'); } if(empty($website_template)){ $result['template'] = WebsiteTemplate::insertGetId([ 'website_id' => $data['website_id'], 'page_type' => $page_type, 'user_id' => $data['user_id'], ]); }else{ $result['template'] = WebsiteTemplate::where('website_id', $data['website_id'])->update([ 'page_type' => $page_type, ]); } if(!$result['template']){ Db::rollBack(); return Result::error('添加网站预制模版信息失败!'); } Db::commit(); return Result::success($result); }catch(\Exception $e){ Db::rollBack(); return Result::error('添加失败'); } } /** * 自助建站---流程---3.选择皮肤列表 * @param array $data * @return array */ public function getWebsiteTemplateList(array $data): array { $where = []; if (isset($data['template_class_id']) && $data['template_class_id']) { $where[] = ['template_class_id', '=', $data['template_class_id']]; } // 0:未构建 1:未应用 2:已应用 status $templste_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first(); if (empty($templste_info)) { return Result::error('请先填写网站基础信息!'); } if ($templste_info['status'] == 2) { return Result::error('网站已应用,不可再次修改!'); } if (isset($data['keyword']) && !empty($data['keyword'])) { if (is_array($data['keyword'])) { // 拼接多个模糊查询条件 $query = Template::query(); foreach ($data['keyword'] as $kw) { $escapedKw = addcslashes($kw, '%_'); // 转义通配符 $query->WhereRaw("JSON_EXTRACT(template_keyword, '$[*]') LIKE ?", ["%$escapedKw%"]); } } else { $escapedKeyword = addcslashes($data['keyword'], '%_'); // 转义通配符 $where[] = [DB::raw("JSON_EXTRACT(template_keyword, '$[*]') LIKE ?"), "%$escapedKeyword%"]; } } else { $query = Template::where($where); } $template_id = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first(['template_id', 'page_type']); // return Result::success($template_id); if (!empty($template_id['page_type']) && $templste_info['page_type'] != '[]' && is_string($templste_info['page_type'])) { $template_page = is_string($templste_info['page_type']) ? json_decode($templste_info['page_type'], true) : []; } else { $template_page = []; } if (!empty($template_id['template_id'])) { $result['template_id'] = $templste_info['template_id']; } // 确保 $template_page 为有效数据,避免 SQL 注入风险,这里使用参数绑定 $template_page_str = json_encode($template_page); $result['template'] = $query->where($where) ->whereRaw("JSON_CONTAINS(template.page_type, ?)", [$template_page_str]) ->orderBy('template.template_id','desc') ->paginate($data['page_size'], ['*'], 'page', $data['page']); if (empty($result['template'])) { $result['template'] = Template::orderBy('template.template_id','desc') ->paginate($data['page_size'], ['*'], 'page', $data['page']); } $template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->value('template_id'); if ($template) { $result['template_id'] = $template; } else { $result['template_id'] = 0; } return Result::success($result); } /** * 自助建站---流程---4.选择皮肤 * @param array $data * @return array */ public function chooseWebsiteTemplate(array $data): array { $website_template_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first(); if (empty($website_template_info)) { return Result::error('请先填写网站基础信息!'); } // 0:未构建 1:未应用 2:已应用 status if ($website_template_info['status'] == 2) { return Result::error('网站已应用,不可再次修改!'); } $template = Template::where('template_id', $data['template_id'])->first(); if (empty($template)) { return Result::error('未查询到皮肤!'); } // 0:未构建;1:已填写基础信息;2:已选择模板; action_id // 分别更新 WebsiteTemplateInfo 和 WebsiteTemplate 表 Db::beginTransaction(); try { $reuslt['template_info'] = WebsiteTemplateInfo::where('website_id', $data['website_id']) ->update(['template_id' => $data['template_id'],'action_id' => 2,'user_id'=> $data['user_id']]); if(empty($reuslt['template_info'])){ Db::rollBack(); return Result::error('选择皮肤失败!'); } $result['template'] = WebsiteTemplate::where('website_id', $data['website_id']) ->update(['template_id' => $data['template_id'],'user_id'=> $data['user_id']]); if(empty($result['template'])){ Db::rollBack(); return Result::error('网站模版选择皮肤失败!'); } Db::commit(); return Result::success($result); } catch (\Exception $e) { Db::rollBack(); return Result::error('选择皮肤失败!'); } } /** * 自助建站---流程---5.保存模版 * @param array $data * @return array */ public function saveWebsiteTemplate(array $data): array { // return Result::success($data); $website_template_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first(); if (empty($website_template_info) || empty($website_template_info->template_id) || empty($website_template_info->page_type)) { return Result::error('请先填写网站基础信息!'); } // 0:未构建 1:未应用 2:已应用 status if ($website_template_info['status'] == 2) { return Result::error('网站已应用,不可再次保存!'); } if ($website_template_info['action_id'] != 2) { return Result::error('请完成之前的步骤!'); } $template_info = WebsiteTemplate::where('website_id', $data['website_id'])->first(); // var_dump($data['template_data']); try { Db::beginTransaction(); if (empty($template_info)) { Db::rollback(); return Result::error('该网站不存在已保存的模版!'); } else { $template = WebsiteTemplate::where('website_id', $data['website_id'])->update( [ 'template_data' => $data['template_data'], 'user_id' => $data['user_id'], 'canvas_data' => $data['canvas_data'], ] ); } if (empty($template)) { Db::rollback(); return Result::error('保存失败!'); } else { $tempalte_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->update(['status' => 1]); if (empty($tempalte_info)) { Db::rollback(); return Result::error('网站搭建状态保存失败!'); } Db::commit(); } } catch (\Exception $e) { return Result::error('模版数据格式错误!'); } return Result::success($template); } /** * 自助建站---流程---6.返显保存的模版 * @param array $data * @return array */ public function getWebsiteTemplate(array $data): array { $web = Website::where('id', $data['website_id'])->first(); if (empty($web)) { return Result::error('未查询到网站信息!'); } $template = WebsiteTemplate::where('website_id', $data['website_id']) ->first(['website_id', 'template_id', 'page_type', 'template_data', 'canvas_data']); if (empty($template)) { return Result::error('未查询到模版!'); } $template['page_type'] = json_decode($template['page_type'],true); return Result::success($template); } /** * 通栏版式管理-获取通栏版式列表 */ public function getSectorPlaceList(array $data): array { $where['sector_place.type_id'] = $data['type_id']; if(isset($data['component_num']) && !empty($data['component_num'])){ $where['sector_place.component_num'] = $data['component_num']; } if(isset($data['sector_type']) && !empty($data['sector_type'])){ $where['sector_place.sector_type'] = $data['sector_type']; } // if(isset($data['component_type']) && !empty($data['component_type'])){ // $where['sector_place.component_type'] = $data['component_type']; // } if(isset($data['name']) && !empty($data['name'])){ array_push($where,['sector_place.name', 'like', '%'.$data['name'].'%']); } if (isset($data['width']) && !empty($data['width']) && isset($data['height']) && !empty($data['height'])){ array_push($where,['sector_place.width','like','%'.$data['width'].'%']); array_push($where,['sector_place.height','like','%'.$data['height'].'%']); } else if( isset($data['width']) && !empty($data['width'])){ array_push($where,['sector_place.width','like','%'.$data['width'].'%']); }else if( isset($data['height']) && !empty($data['height'])){ array_push($where,['sector_place.height','like','%'.$data['height'].'%']); }else{ $size_id = []; } $sector_place = SectorPlace::where($where) // ->when(!empty($size_id), function ($query) use ($size_id) { // $query->whereIn('sector_place.size_id', $size_id); // }) // ->leftJoin('size','sector_place.size_id','=','size.id') ->select('sector_place.*'); $result['count'] = $sector_place->count(); $result['row'] =$sector_place->paginate($data['page_size'], ['*'], 'page', $data['page']); if(empty($result['count'])){ return Result::error("暂无相关版式数据!"); } return Result::success($result); } /** * 通栏版式管理-添加通栏版式 */ public function addSectorPlace(array $data): array { // 1:通栏;2:组件; if($data['type_id'] == 1){ $sector_place = SectorPlace::where('sector_type', $data['type'])->first(); if(!empty($sector_place)){ return Result::error('通栏版式类别编号已存在!'); } $data = [ 'name' => $data['name'], 'component_num' => $data['component_num'], 'size_id' => $data['size_id'], 'sort_id' => 0, 'sector_img' => $data['sector_img'], 'sector_type' => $data['type'], 'type_id' => $data['type_id'], 'status' => 0, ]; }else{ $sector_type = SectorPlace::where('sector_type', $data['sector_type'])->where('type_id',1)->first(); if(empty($sector_type)){ return Result::error('通栏版式类别不存在!'); } // $com_query = SectorPlace::where('type_id',2); // ->query(function($query) use ($data){ // $query->where('component_type', $data['type']) // ->Subquery(function($subQuery) use ($data) { // $subQuery->where('sector_type', $data['sector_type']) // ->where('sort_id', $data['sort']); // }); // }) //若是组件版式编号或者组件版式位置重复用此语句筛选 // ->where('sector_type',$data['sector_type']) //若是组件版式编号在同一个通栏版式下不重复就行,则启用此语句 $component = SectorPlace::where('type_id',2)->where('component_type',$data['type'])->first(); if(!empty($component)){ return Result::error('组件版式类别编号已存在!'); } // $com_query = clone $com_query; $com_num = SectorPlace::where('type_id',2)->where('sector_type',$data['sector_type'])->count('sort_id'); // return Result::success($com_num); if($sector_type['component_num'] = $com_num){ $sector = SectorPlace::where('sector_type',$data['sector_type'])->where('type_id',1)->update(['status'=>1]); if(empty($sector)){ return Result::error('通栏版式状态修改失败!'); } } $data = [ 'name' => $data['name'], 'component_num' => 0, 'size_id' => $data['size_id'], 'sort_id' => $data['sort_id'], 'sector_img' => $data['sector_img'], 'component_type' => $data['type'], 'type_id' => $data['type_id'], 'sector_type' => $data['sector_type'], ]; } $result = SectorPlace::insertGetId($data); if(empty($result)){ return Result::error('添加失败!'); } return Result::success('添加成功!',$result); } /** * 通栏版式管理-修改通栏版式 */ public function upSectorPlace(array $data): array { $id = $data['id']; // 1:通栏;2:组件; if($data['type_id'] == 1){ $sector_place = SectorPlace::where('id','!=',$id)->where('sector_type', $data['type'])->where('type_id',1)->first(); if(!empty($sector_place)){ return Result::error('通栏版式类别编号已存在!'); } $data = [ 'name' => $data['name'], 'component_num' => $data['component_num'], 'size_id' => $data['size_id'], 'sort_id' => 0, 'sector_img' => $data['sector_img'], 'sector_type' => $data['type'], 'type_id' => $data['type_id'], ]; }else{ $sector_type = SectorPlace::where('sector_type', $data['sector_type'])->where('type_id',1)->first(); if(empty($sector_type)){ return Result::error('通栏版式类别不存在!'); } $component = SectorPlace::where('id','!=',$id) ->where('component_type', $data['type']) ->where('type_id',2) ->first(); if(!empty($component)){ return Result::error('组件版式类别编号已存在!'); } $data = [ 'name' => $data['name'], 'component_num' => 0, 'size_id' => $data['size_id'], 'sort_id' => $data['sort_id'], 'sector_img' => $data['sector_img'], 'component_type' => $data['type'], 'type_id' => $data['type_id'], 'sector_type' => $data['sector_type'], ]; } $result = SectorPlace::where('id',$id)->update($data); if(empty($result)){ return Result::error('修改失败!'); } return Result::success('修改成功!',$result); } /** * 通栏版式管理-删除通栏版式 */ public function delSectorPlace(array $data): array { $sector_place = SectorPlace::where('id',$data['id'])->first(); if(empty($sector_place)){ return Result::error('通栏版式不存在!'); } if($sector_place['type_id'] == 1){ $sector = Sector::where('place_type', $sector_place['sector_type'])->first(); $component = SectorPlace::where('sector_type', $sector_place['sector_type']) ->where('type_id',2) ->first(); if(!empty($sector) || !empty($component)){ return Result::error('通栏版式分类已应用,暂不可删除!'); } }else{ $sector = Component::where('place_type', $sector_place['component_type'])->first(); if(!empty($sector)){ return Result::error('组件版式分类已应用,暂不可删除!'); } $component_num = SectorPlace::where('sector_type', $sector_place['sector_type']) ->where('type_id',1)->update(['status'=> 0]); if(empty($component_num)){ return Result::error('通栏版式状态修改失败!'); } } $result = SectorPlace::where('id',$data['id'])->delete(); if(empty($result)){ return Result::error('删除失败!'); } return Result::success('删除成功!'); } /** * 自助建站-通栏版式管理-获取通栏版式详情 */ public function getSectorPlaceInfo(array $data): array { $result = SectorPlace::where('sector_place.id',$data['id']) ->leftJoin('size','sector_place.size_id','=','size.id') ->select('sector_place.*','size.width','size.height') ->first(); if(empty($result)){ return Result::error('通栏版式不存在!'); } return Result::success($result); } /** * 自助建站-通栏版式管理-获取通栏版式列表 */ public function getSectorPlaceSort(array $data): array { $sector = SectorPlace::where('sector_type',$data['sector_type'])->first(); if(empty($sector)){ return Result::error('通栏版式不存在!'); } $component = SectorPlace::where('type_id',2)->where('sector_type',$sector['sector_type'])->pluck('sort_id')->toArray(); if(count($component) < $sector['component_num']){ // 根据 $sector['component_num'] 的值生成对应元素数量的数组 if (isset($sector['component_num']) && is_numeric($sector['component_num'])) { $num = intval($sector['component_num']); if ($num > 0 && $num <= 10) { // 使用 range 函数生成一个从 1 到 $num 的连续整数数组,存储到 $sector_arr 中 $sector_arr = range(1, $num); } else { $sector_arr = []; } } else { $sector_arr = []; } // 获取 $sector_arr 中不在 $component 里的元素 $sort = array_diff($sector_arr, $component); // return Result::success($sort); } if(!empty($sort)){ $sort = array_values(array_map(function($value) { return ['id' => $value]; }, $sort)); }else{ return Result::error("组件版式数量已达上限!"); } return Result::success($sort); } /** * 获取所有通栏版式 */ public function getAllSectorPlace(array $data): array { $where['type_id'] = $data['type_id']; // if($data['type_id'] == 1){ // $where['status'] = 1; // } if(isset($data['sector_type']) && !empty($data['sector_type'])){ $where['sector_type'] = $data['sector_type']; } if(isset($data['component_num']) && !empty($data['component_num'])){ $where['component_num'] = $data['component_num']; } $sector_place = SectorPlace::where($where) ->when($data['type_id'] == 2, function($query) use($data){ $query->orderBy('sort_id','asc'); }) ->get()->all(); if($data['type_id'] == 2){ foreach($sector_place as $key => $value){ $sector_place[$key]['component'] = Component::where('type_id',$value['type']) ->where('component_width',$value['width']) ->where('component_height',$value['height']) ->get()->all(); } } if(empty($sector_place)){ return Result::error('通栏版式不存在!'); } return Result::success($sector_place); } /** * 组件管理-获取组件类型 */ public function getComponentType(array $data): array { $where = []; if(isset($data['id']) && !empty($data['id'])){ $where['id'] = $data['id']; } $component_type = ComponentType::where($where)->get()->all(); if(empty($component_type)){ return Result::error('组件类型不存在!'); } if(count($component_type) == 1){ $all_code = json_decode($component_type[0]['com_code'] ?? '',true); $component_type[0]['all_code'] = $all_code['listType'] ?? []; $result = $component_type[0]; }else{ $result = $component_type; } if(empty($component_type)){ return Result::error('组件类型不存在!'); } return Result::success($component_type); } /** * 组件管理-获取所有组件样式 */ public function getAllComponent(array $data): array { $where = []; if((isset($data['sector_id']) && !empty($data['sector_id'])) || (isset($data['sort_id']) && !empty($data['sort_id']))){ $sector_id['sector.sector_id'] = $data['sector_id']; $sector['sector_component.sort_id'] = $data['sort_id']; $sectors = Sector::where($sector_id) ->where('sector.template_id',$data['template_id']) ->leftJoin('sector_component','sector.id','sector_component.sectorid') ->where($sector) ->select('sector.template_id','sector_component.component_id') ->get()->all(); // return Result::success($sectors); $component_id = array_column($sectors,'component_id'); $template = ['template_id' => $sectors[0]['template_id']]; $component = ComponentImg::whereIn('component_id',$component_id) ->where($template) ->select('img_id','img_name','img_url','template_id') ->orderBy('component_img.img_id','asc') ->get()->all(); }else{ if(isset($data['type_id']) && !empty($data['type_id'])){ $where['component.type_id'] = $data['type_id']; } if(isset($data['width']) && !empty($data['width'])){ $where['component.component_width'] = $data['width']; } if(isset($data['height']) && !empty($data['height'])){ $where['component.component_height'] = $data['height']; } $component = Component::where($where) ->leftJoin('component_img','component.component_id','=','component_img.component_id') ->select('component.*','component_img.img_id','component_img.img_url','component_img.img_name') // ->orderBy('component_img.template_id') ->orderBy('component_img.img_id','asc') ->get()->all(); } if(empty($component)){ return Result::error('组件不存在!'); } return Result::success($component); } /** * 自助建站-流程管理-获取所有通栏 */ public function getAllSector(array $data): array { $where = []; if(isset($data['template_id']) && !empty($data['template_id'])){ $where['sector.template_id'] = $data['template_id']; } if(isset($data['page_type']) && !empty($data['page_type'])){ array_push($where,['sector.page_type', 'like', '%'.$data['page_type'].'%']); } $sector = Sector::where($where) ->leftJoin('sector_component', 'sector.id', '=', 'sector_component.sectorid') ->leftJoin('component', 'sector_component.component_id', '=', 'component.component_type') ->when(isset($data['template_id']), function ($query) use ($data) { // 当 type_id 等于 11 时,关联查询 component_img 表并添加 img_url 字段 return $query->leftJoin('component_img', function ($join) use ($data) { $join->on('component.component_type', '=', 'component_img.component_id') ->where('component_img.template_id', $data['template_id']) ->where('component.type_id', 11); }); }) ->select('sector.sector_name', 'sector.sector_id', 'component.*', 'component_img.img_url' ,'sector.pic_height','sector.sector_img','sector_component.sort_id') ->orderBy('sector_component.sort_id') ->orderBy('sector_component.sector_id') ->get()->all(); if(empty($sector)){ return Result::error('通栏不存在!'); } $com_num = array_count_values(array_column($sector,'sector_id')); // 使用 array_keys 和 array_diff 函数,不使用循环将值不为 1 的字段键提取到 $sectors_key 中 $sectors_key = array_keys(array_diff($com_num, [1])); // return Result::success($sector); $num = 0; $array_key = 0; foreach($sector as $key => $value){ $sector_id = $value['sector_id']; $sectors[$key] = $value['sector_id']; // if(isset($comlist_key) && !empty($comlist_key)){ // $sector_arr_key[$key] = array_keys($comlist_key); // } if(in_array($sector_id,$sectors_key) && in_array($sector_id,$sectors)){ $array_key = array_search($sector_id,$sectors); if(isset($sectors) && !in_array($sector_id,$sectors)){ $array_key++; } $num++; $array_key++; }else{ $array_key++; } // var_dump($array_key); // var_dump(in_array($sector_id,$sectors_key)); $comlist_key[$array_key][$sector_id]['sectorName'] = $value['sector_name']; $comlist_key[$array_key][$sector_id]['sectorHeight'] = $value['pic_height']; $comlist_key[$array_key][$sector_id]['sectorImg'] = $value['sector_img']; $componentList['component_style'] = 1; $componentList['sort'] = $value['sort_id']; // 1:资讯;2:广告;3:静态; $component_data = empty($value['component_data']) ? null : (json_decode($value['component_data']) ?? []); // 检查 $value['component_column'] 是否为非空字符串,避免传递 null 给 json_decode $listType = empty($value['component_column']) ? null : (json_decode($value['component_column']) ?? []);; // $page[$key][$setor_id]['componentList'] = // 组件分类:1:资讯-头条组件;2:资讯-轮播组件;3:资讯-推荐图类组件;4:资讯-最新类组件;5:资讯-推荐类;6:资讯-热点类组件;7:资讯-栏目类组件;8:列表类组件;9:详情类组件; // 10:二级导航栏类组件;11:广告类;12:静态资源类;13:单页导航类;14:广告资讯混合类;15:滚动图文;16:搜索框类;17:单页列表类;18:单页详情类; if(in_array($value['type_id'],[1,2,3,4,5,6,7,8,9,15,17,18])){ $type = 1; $componentList['component_type'] = $type; if(in_array($value['type_id'],[7,15]) && $data['page_type'] == 1){ $component_data->componentData->name = '请选择导航..'; }else if(in_array($value['type_id'],[7,15]) && $data['page_type'] == 2){ $component_data->componentData->name = '自动生成'; }else if($data['page_type'] == 3){ if($value['type_id'] == 4){ $component_data->componentData->name = '最新资讯'; } if($value['type_id'] == 6){ $component_data->componentData->name = '热点精选'; } }else{ unset($component_data->componentData->name); } $componentData = $component_data->componentData; $componentList['componentData'] = is_object($componentData) ? (array)$componentData ?? [] : $componentData ?? []; // $page_listType = is_object($listType) ? (array)$listType->listType ?? [] : $listType['listType'] ?? []; $componentList['componentData']['listType'] = $listType->listType; }else if(in_array($value['type_id'],[10,12,13,16])){ $type = 3; $componentList['component_type'] = $type; $componentList['componentData']=(object)[]; }else if($value['type_id']== 11){ $type = 2; $componentList['component_type'] = $type; $componentList['componentData']=(object)[]; $ad = json_decode($value['ad']) ?? []; if(is_object($ad)){ $ad->thumb = $value['img_url']; }else{ $ad['thumb'] = $value['img_url']; } $comlist_key[$array_key][$sector_id]['ad'] =is_object($ad) ? (array)$ad ?? [] : $ad ?? []; // 在 $page[$key][$setor_id] 中增加一个键值为 ad 的数组 // $page[$key][$setor_id]['ad'] = is_object($ad) ? (array)$ad->ad ?? [] : $ad['ad'] ?? []; }else if($value['type_id'] == 14){ $type = 1; $componentList['component_type'] = $type; if($data['page_type'] == 1){ $component_data->componentData->name = '请选择导航..'; } $componentData = $component_data->componentData; $componentList['componentData'] = is_object($componentData) ? (array)$componentData ?? [] : $componentData ?? []; $componentList['componentData']['listType'] = $listType->listType; $ad = json_decode($value['ad']) ?? []; $comlist_key[$array_key][$sector_id]['ad'] =is_object($ad) ? (array)$ad ?? [] : $ad ?? []; }else{ } if( in_array($sector_id,$sectors_key)){ $comlist_key[$array_key][$sector_id]['componentList'][$num-1] = $componentList; $comlist_key[$array_key][$sector_id]['componentList'] = array_values($comlist_key[$array_key][$sector_id]['componentList']); }else{ $comlist_key[$array_key][$sector_id]['componentList'] = $componentList; } $keys[$key] = $key; } // return Result::success($sector_arr_key); // $page[$keys] = array_column($page,$sectors_key[0]); $comlist_key = array_values($comlist_key); // $sector_page = json_encode($comlist_key); if(empty($comlist_key)){ return Result::error('通栏不存在!'); } return Result::success($comlist_key); // return Result::success(gettype($listType)); } /** * 自助建站-组件管理-获取组件预览图列表 */ public function getComponentImgList(array $data): array { $where['component_img.component_id'] = $data['component_id']; if(isset($data['template_id']) && !empty($data['template_id'])){ $where['component_img.template_id'] = $data['template_id']; } $component_img = ComponentImg::where($where) ->leftJoin('template','component_img.template_id','=','template.template_id') ->leftJoin('component','component_img.component_id','=','component.component_type') ->select('component_img.*','template.template_name','component.component_name') ->orderBy('component_img.template_id') ->orderBy('component_img.img_id') ->paginate($data['page_size'], ['*'], 'page', $data['page']); if($component_img->isEmpty()){ return Result::error('组件预览图不存在!'); } $component_img = [ 'data' => $component_img->items(), 'total' => $component_img->total(), ]; return Result::success($component_img); } /** * 自助建站-组件管理-添加组件预览图 */ public function addComponentImg(array $data): array { unset($data['user_id']); $component = Component::where('component_type',$data['component_id'])->first(); if(empty($component)){ return Result::error('组件不存在!'); } if($component['type_id'] == 11){ $img = ComponentImg::where('component_id',$data['component_id'])->where('template_id',$data['template_id'])->get()->all(); if(!empty($img)){ return Result::error('该组件已存在此皮肤的预览图!'); } } $component = ComponentImg::where('template_id',$data['template_id'])->where('component_id',$data['component_id'])->where('img_id',$data['img_id'])->first(); if(!empty($component)){ return Result::error('组件预览图编号已存在!'); } $component_img = ComponentImg::insertGetId($data); if(empty($component_img)){ return Result::error('添加失败!'); } return Result::success($component_img); } /** * 自助建站-组件管理-删除组件预览图 */ public function delComponentImg(array $data): array { $component_img = ComponentImg::where('id',$data['id'])->delete(); if(empty($component_img)){ return Result::error('删除失败!'); } return Result::success($component_img); } /** * 自助建站-组件管理-更新组件预览图 */ public function updateComponentImg(array $data): array { $component = Component::where('component_type',$data['component_id'])->first(); if(empty($component)){ return Result::error('组件不存在!'); } if($component['type_id'] == 11){ $img = ComponentImg::where('id','!=',$data['id'])->where('component_id',$data['component_id'])->where('template_id',$data['template_id'])->get()->all(); if(!empty($img)){ return Result::error('该组件已存在此皮肤的预览图!'); } } $component = ComponentImg::where('id','!=',$data['id'])->where('component_id',$data['component_id'])->where('template_id',$data['template_id'])->where('img_id',$data['img_id'])->first(); if(!empty($component)){ return Result::error('组件预览图编号已存在!'); } // return Result::success($component); unset($data['user_id']); // unset($data['updated_at']); $id = $data['id']; unset($data['id']); // return Result::success($data); $component_img = ComponentImg::where('id',$id)->update($data); if(empty($component_img)){ return Result::error('更新失败!'); } return Result::success($component_img); } /** * 自助建站-获取通栏类型 */ public function getSectorType(array $data): array { $sector = SectorType::get()->all(); if(empty($sector)){ return Result::error('通栏类型不存在!'); } return Result::success($sector); } /** * 自助建站-随机获取模板 */ public function randomWebTemplate(array $data): array { $web = Website::where('id',$data['website_id'])->first(); if(empty($web)){ return Result::error('此网站不存在!'); } $template = Template:: where('template_id',$data['template_id'])->first(); if(empty($template)){ return Result::error('此皮肤不存在!'); } $rule = TemplateRule::first(); switch ($data['page']) { case 1: $index_rule = json_decode($rule['index_rule']); break; case 2: $index_rule = json_decode($rule['class_rule']); break; case 3: $index_rule = json_decode($rule['list_rule']); break; case 4: $index_rule = json_decode($rule['info_rule']); break; case 5: $index_rule = json_decode($rule['search_rule']); break; case 6: $index_rule = json_decode($rule['footerlist_rule']); break; case 7: $index_rule = json_decode($rule['footerinfo_rule']); break; default: break; } // return Result::success($index_rule); $tempalte['index'] = $this->randomPage($data,$data['page'],$index_rule); // $tempalte['class'] = $this->randomPage($data,2,$class_rule); // $tempalte['list'] = $this->randomPage($data,3); // $tempalte['article'] = $this->randomPage($data,4); // $tempalte['search'] = $this->randomPage($data,5); // $tempalte['aloneList'] = $this->randomPage($data,6); // $tempalte['aloneArticle'] = $this->randomPage($data,7); // $index = $this->randomPage($data); return Result::success($tempalte); } public function randomPage($data,$page,$rule){ // return $rule; $sectors = Sector::where('sector.template_id',$data['template_id']) ->where('sector.page_type','like','%'.$page.'%') ->select('sector.sector_name', 'sector.sector_id','sector.pic_height','sector.sector_img', 'sector.sector_type','sector.sector_code','sector.id','sector.sector_width') ->orderBy('id') ->get()->all(); if(empty($sectors)){ return Result::error('通栏不存在!'); } // return $sectors; // 获取每个类别的通栏数量 $type_num = array_count_values(array_column($sectors,'sector_type')); // 此页面通栏的所有类别 $all_sector_types = array_keys($type_num); //获取每个通栏类别在其中的位置 -------暂时不考虑先,应该是必需通栏类型或者非必需通栏类型,每个类型应该取一个,其余取可重复的通栏 $sector_type_keys = array_column($sectors,'sector_type'); // 将数组处理为二维数组,值相同的合并为同一个数组,保持原有键值 $sector_types = array_reduce(array_keys($sector_type_keys), function ($result, $key) use ($sector_type_keys) { $value = $sector_type_keys[$key]; if (!isset($result[$value])) { $result[$value] = []; } $result[$value][$key] = $key; $result[$value] = array_values($result[$value]); return $result; }, []); // return $sector_types; $must_type = $rule->must_type; $sector_num = $rule->max_num; // 获取必需通栏类别与查询到的通栏类别的交集数量; $is_must = count(array_intersect($must_type, $all_sector_types)); // $is_must交集数量若是等于必需通栏类别的数量相等则不缺必需通栏 if( $is_must != count($must_type)){ return '此页面缺少必要通栏!'; } // var_dump("sector_types",$all_sector_types); // var_dump("must_type",$must_type); // return $must_type; // 非必须通栏的随机处理 $not_must_type = array_diff($all_sector_types,$must_type); $total = count($not_must_type); $randomCount = mt_rand(0, $total); // 可能为0(无元素)或任意数量,最多等于数组长度 $and_type = $rule->and_type ?? []; // return $not_must_type; if ($randomCount === 0) { $random_sector = []; $rand_count = 0; } else { // 随机获取指定数量的键名(保留原键) $randomKeys = array_rand($not_must_type, $randomCount); // 处理单元素情况(array_rand返回字符串,需转为数组) if (!is_array($randomKeys)) { $randomKeys = [$randomKeys]; } // 通栏关联性规则暂时无法实现--------------- // 假设 $oneDimensionalArray 是一维数组,$randomKeys 是随机数组 // 判断 $oneDimensionalArray 是否为一维数组 // if (isset($and_type) && is_array($and_type) && !array_is_list(array_filter($and_type, 'is_array'))) { // 判断是否存在有关联通栏的交集 // if (isset($and_type) && !empty($and_type) && count(array_intersect($and_type, $randomKeys)) > 0) { // // var_dump("11111",$and_type); // // var_dump("22222",$randomKeys); // // 将一维数组中的值都放到随机数组中 // $randomKeys = array_merge($randomKeys, $and_type); // } // var_dump(count(array_intersect($and_type, $randomKeys))); // } // ----------------------------- // 根据随机键名提取元素(保留原键值关系) // $randomKeys = array_intersect($sector_type_keys,$randomKeys); // var_dump("randomKeys",$randomKeys); $random_sector = array_values(array_intersect_key($sectors, array_flip($randomKeys))); $rand_count = count($random_sector); } // var_dump("randomKeys",$randomKeys); // var_dump("random_sector",$random_sector); var_dump("rand_count",$rand_count); // return $random_sector; // 必需通栏 $must_type = array_intersect($all_sector_types,$must_type); // return $must_type; // 此代码用于从 $sectors 数组中筛选出必需的通栏数据。 // 1. array_keys($must_type) 获取 $must_type 数组的所有键名 // 2. array_flip() 将这些键名作为值,生成一个新数组 // 3. array_intersect_key() 比较 $sectors 和上述新数组的键名,返回键名相同的元素 // 4. array_values() 重新索引返回的数组,使其键名从 0 开始连续递增 // 最终得到的 $must_sector 数组包含了 $sectors 中对应 $must_type 键名的所有必需通栏数据 $must_sector = array_values(array_intersect_key($sectors, array_flip(array_keys($must_type)))); $must_count = count($must_sector); // var_dump("must_sector",$must_sector); // var_dump("must_count",$must_count); // return $must_sector; // 计算随机之后减去必需的通栏,查询重复通栏可用数量;(目前的这些通栏没有重复的同类别通栏) $repeat_num = $sector_num-$rand_count-$must_count; // 组合起来通栏 $sector_zuhe1 = array_merge($must_sector,$random_sector); $sector_ids = array_column($sector_zuhe1, 'id'); if($repeat_num > 0){ $rule_repeat = (array)$rule->type_max; $sector_zuhe1_type = array_count_values(array_column($sector_zuhe1,'sector_type')); // $sector_arr = array_intersect_key($rule_repeat,$sector_zuhe1_type); foreach($rule_repeat as $key => $value){ if(in_array($key,array_keys($sector_zuhe1_type))){ $rule_repeat[$key] = $value-1; } } var_dump("repeat_num",$repeat_num); // return array_intersect($sector_zuhe1,$must_sector); if(count(array_intersect($sector_zuhe1,$must_sector)) != count($must_sector)){ return '此页面缺少必要通栏!'; } // 剩余通栏进行重复处理 // $rule_repeat = (array)$rule->type_max; // 取到可以重复的通栏类别 $rule_repaet_type = array_keys($rule_repeat); $repeat_type = array_intersect_key($sector_types, array_flip($rule_repaet_type)); // 获取可以随机重复的通栏 $repaet_key = 0; $num = 0; $type = []; // return $rule_repaet_type; // 防止无限循环,设置最大迭代次数 $max_iterations = 20; $iteration_count = 0; // return [ // 'repaet_key' => $repaet_key, // 'repeat_num' => $repeat_num, // 'rule_repeat' => $rule_repeat, // 'repeat_type' => $repeat_type, // 'must_type' => $must_type, // ]; while($repaet_key < $repeat_num && $iteration_count < $max_iterations){ $num++; foreach($repeat_type as $key => $value){ if(!isset($type[$key])){ $type[$key] = 0; }else{ $type[$key]++; } // $type代表每个类型的通栏数量,每个类型的通栏的数量应该≤规则限制的数量 if($repaet_key < $repeat_num && $type[$key] <= $rule_repeat[$key]){ // $max_repaert_num = $rule_repeat[$key]; // if($max_repaert_num < $num ){ // // $arr_key = $num+$key; if (count($value) > 1) { // 若 可以随机的通栏类型存在多个通栏,随机取其一 $repeat_sector_key[$repaet_key] = $value[array_rand($value)]; } else { $repeat_sector_key[$repaet_key] = $value[0]; } $type[$repaet_key] = $repeat_sector_key[$repaet_key]; $repaet_key++; // } } // else{ // break; // } } // var_dump("num", $type[$key]); // var_dump("repeat_sector_key", $repeat_sector_key); $iteration_count++; } // return $repeat_sector_key; // var_dump("num", $num); // var_dump("repeat_sector_key", $repeat_sector_key); // 随机的重复通栏键值转换成真正的通栏 $repeat_sector = []; if(isset($repeat_sector_key) && !empty($repeat_sector_key) && is_array($repeat_sector_key)){ foreach ($repeat_sector_key as $key => $value) { if (isset($sectors[$value])) { $repeat_sector[$key] = $sectors[$value]; } } } $repeat_count = count($repeat_sector); var_dump("repeat_count",$repeat_count); // var_dump("repeat_sector", $repeat_sector); // return ($data['page'] == '2' || $data['page'] == 2); $sector = array_merge($repeat_sector,$sector_zuhe1); }else{ $sector = $sector_zuhe1; } // var_dump("sector_ids",$sector_ids); // return [ // 'random_sector'=>$random_sector, // 'must_sector'=>$must_sector, // 'sector_ids'=>$sector_ids, // ]; // 各类型通栏最大数量数量限制 = 规则最大数量-已经存在的通栏数量(目前的通栏不会有重复的,所以只是-1) // var_dump("sector",$sector); // 处理头条及轮播图在随机通栏中的特殊位置特殊处理 $sector_specal_sort = array_column($sector,'sector_type'); if(in_array(6,$sector_specal_sort) || in_array(7,$sector_specal_sort) || in_array(5,$sector_specal_sort)){ $toutiao_key = array_search(6,$sector_specal_sort) ?? null; $pic_key = array_search(7,$sector_specal_sort) ?? null; $cat_key = array_search(5,$sector_specal_sort) ?? null; $toutiao = $sector[$toutiao_key]; $pic = $sector[$pic_key]; $cat = $sector[$cat_key]; $sort_1 = $sector[0]; $sort_2 = $sector[1]; $sort_3 = $sector[2]; // return $sector_specal_sort; // return [ // 'sector_ids' => array_column($sector_zuhe1,'id'), // // 'repeat_sector' => $repeat_sector, // 'sector_id' => array_column($sector,'id'), // 'array_intersect' => count(array_intersect($sector,$must_sector)), // 'old' => [ $toutiao_key,$pic_key,$cat_key], // 'new' => [ $sort_1,$sort_2,$sort_3] // ]; if($data['page'] == 1 || $data['page'] == '1'){ if($toutiao_key){ $sector[0] = $toutiao; $sector[$toutiao_key] = $sort_1; $sector[1] = $pic; $sector[$pic_key] = $sort_2; } if($cat_key){ $sector[0] = $pic; $sector[$pic_key] = $sort_1; } } if($data['page'] == '2' || $data['page'] == 2 || in_array(5,$sector_specal_sort)){ $sector[0] = $cat; $sector[$cat_key] = $sort_1; if($toutiao_key && $pic_key){ $sector[1] = $toutiao; $sector[$toutiao_key] = $sort_2; $sector[2] = $pic; $sector[$pic_key] = $sort_3; } if($pic_key){ $sector[1] = $pic; $sector[$pic_key] = $sort_2; } } } $sector_id = array_column($sector,'id'); // if(count(array_intersect($sector,$must_sector)) != count($must_sector)){ // return '此页面缺少必要通栏!'; // } $data['page'] = $page; $component[] = $this->randomComponent($sector_id,$data,$sector); var_dump("sector_id",$sector_id); return ['sector_id'=>$sector_id, // 'sector'=>$sector, 'component'=>$component, // 'sector'=>$sector, ]; } public function randomComponent($sector_id,$data,$sector){ $rawResults = SectorComponent::whereIn('sector_component.sectorid', $sector_id) ->leftJoin('component','component.component_type','sector_component.component_id') ->leftJoin('component_img','component_img.component_id','component.component_type') ->where('component_img.template_id','=',$data['template_id']) ->select('component.*','component_img.img_name','component_img.img_id','component_img.img_url', 'sector_component.sort_id','sector_component.sectorid') ->orderBy('sector_component.sort_id') ->orderBy('component.component_type') ->get(); // 按sectorid和sort_id分组数据 $groupedResults = []; $sort_ids = []; $sector_ids = []; $component_list = []; $component_ids = []; $img_ids = []; $sort_key = 0; // return $rawResults; // $groupedResults重构数组,将相同位置的组件合并,相同组件的样式合并(已经包括所有选择的组件及组件样式) // $sort_component = array_column($rawResults,'sort_id'); foreach ($rawResults as $key => $item) { $sectorId = $item->sectorid; $sortId = $item->sort_id; $componentId = $item->component_type; $component_data = $item->component_data; $component_column = $item->component_column; $component_type = $item->type_id; $component_ad = $item->ad; $imgId = $item->img_id; if (!isset($groupedResults[$sectorId])) { $groupedResults[$sectorId] = []; } $componentData = !empty($component_data) && is_string($component_data) ? json_decode($component_data, true) : []; $component_listtype = !empty($component_column) && is_string($component_column) ? json_decode($component_column, true) : []; $component_code = !empty($component_code) && is_string($component_code) ? json_decode($component_code, true) : []; $component_ad = !empty($component_ad) && is_string($component_ad) ? json_decode($component_ad, true) : []; // 确保 $componentData['componentData'] 是数组,避免 array_push 传入 null if (!isset($componentData['componentData']) || !is_array($componentData['componentData']) ) { $componentData['componentData'] = []; } $componentData['componentData'] += $component_listtype; $component_code = $componentData; $component_list[$key] = $component_code; // $component_list[$key]['listType'] = $component_listtype; // } $sort_id = intval($sortId-1); // if(!isset($groupedResults[$sectorId][$sort_id])) { // // $groupedResults[$sectorId][$sortId] = []; // $com_key = 0; // $groupedResults[$sectorId][$sort_id][$com_key] = [ // 'component_type' => $item->component_type, // 'type_id' => $component_type, // 'sort' => $sortId, // 'width' => $item->component_width, // 'height' => $item->component_height, // 'images' => [ // 'img_name' => $item->img_name, // 'img_id' => $item->img_id, // 'img_url' => $item->img_url, // 'component_type' => $component_type, // ] // ]; // // 检查 $sectorId 和 $sort_id 是否为合法的数组键类型(字符串或整数) // }else{ // if((isset($component_ids) && in_array($componentId,$component_ids))){ // if(isset($img_arr)){ // $img_arr++; // }else{ // $img_arr = 0; // if(isset($com_key)){ // $com_key++; // } // } // // $com_id_key = array_search($componentId,$component_ids); // $groupedResults[$sectorId][$sort_id][$com_key]['images'][$img_arr] = [ // 'img_name' => $item->img_name, // 'img_id' => $item->img_id, // 'img_url' => $item->img_url, // 'component_type' => $component_type, // ]; // }else{ // $img_arr = 0; // $com_key = $com_key ?? 0; // $groupedResults[$sectorId][$sort_id][$com_key] = [ // 'component_type' => $item->component_type, // 'type_id' => $component_type, // 'sort' => $sortId, // 'width' => $item->component_width, // 'height' => $item->component_height, // 'images' => // [ // $img_arr => // [ // 'img_name' => $item->img_name, // 'img_id' => $item->img_id, // 'img_url' => $item->img_url, // 'component_type' => $component_type, // ] // ] // ]; // } // } // $component = $groupedResults[$sectorId][$sort_id]; // if(count($component) > 1){ // $com_key = 0; // } // if(!in_array($sectorId,$sector_ids)){ // $sort_key = 0; // $sector_ids[$sectorId][$sort_key] = $sectorId; // }else{ // $sort_key++; // $sector_ids[$sectorId][$sort_key] = $sectorId; // } if(!isset($com_key) || !in_array($sectorId,$sector_ids) || !isset($sort_ids[$sectorId]) || !in_array($sortId, $sort_ids[$sectorId])){ $com_key = 0; // if(!in_array($componentId,$component_ids)){ // $img_key = 0; // } $img_key = 0; }else if(in_array($sortId,$sort_ids[$sectorId]) && in_array($sectorId, $sector_ids) && !in_array($componentId,$component_ids)){ $com_key++; } if(in_array($componentId,$component_ids) && !in_array($imgId,$img_ids[$componentId])){ $img_key++; }else{ $img_key=0; } if(!in_array($componentId,$component_ids)){ $groupedResults[$sectorId][$sort_id][$com_key] = [ 'component_type' => $item->component_type, 'type_id' => $component_type, 'sort' => $sortId, 'width' => $item->component_width, 'height' => $item->component_height, ]; } $groupedResults[$sectorId][$sort_id][$com_key]['images'][$img_key] = [ 'img_name' => $item->img_name, 'img_id' => $imgId, 'img_url' => $item->img_url, 'component_type' => $componentId, ]; // $component_type[$key] = $groupedResults[$sectorId][$sort_id]['component_type']; // $sort_com = $groupedResults[$sectorId][$sort_id]['component_type']; // if(count($sort_com) == 1){ // $com_key = 0; // } // // 组件分类:1.头条资讯;2.轮播图资讯;3.推荐图;4.最新资讯;5.推荐资讯;6.热点资讯;7.栏目资讯;8.列表类;9.详情类; // // 10.二级导航类;11.广告类;12.静态类;13.单页导航类;14.资讯广告混合类;15.滚动图文类;16.搜索框类;17.单页列表类;18.单页详情类; if (is_scalar($sectorId) && is_scalar($sort_id)) { if(!in_array($component_type,[10,12,16])){ if($component_type == 11 || $component_type == 14){ $groupedResults[(int)$sectorId][(int)$sort_id][$com_key]['ad'] = $component_ad; $groupedResults[(int)$sectorId][(int)$sort_id][$com_key]['ad']['website_id'] = $data['website_id']; } if($component_type != 11){ $groupedResults[(int)$sectorId][(int)$sort_id][$com_key]['componentData'] = $component_code['componentData']; } }else{ $groupedResults[(int)$sectorId][(int)$sort_id][$com_key]['componentData'] = []; } } else { // 处理非法键类型的情况,可根据实际需求修改错误处理逻辑 trigger_error('Illegal offset type for $sectorId or $sort_id', E_USER_WARNING); } // // 判断此组件是否存在数组中的依据(若是有重复的组件,一定是组件样式重复) $component_ids[$key] = $componentId; if (!isset($sort_ids[$sectorId])) { $sort_ids[$sectorId] = []; } if (!in_array($sortId, $sort_ids[$sectorId])) { $sort_ids[$sectorId][] = $sortId; } if (!isset($img_ids[$componentId])) { $img_ids[$componentId] = []; } if (!in_array($imgId, $img_ids[$componentId])) { $img_ids[$componentId][] = $imgId; } $sector_ids[$key] = $sectorId; // $img_ids[$componentId] = $imgId; } // return $groupedResults; $result = []; // return $sectors; $y_num = 0; foreach ($sector as $key => $value) { // array_push($value['sort_id'],$key); // $sector_id_ = $value['sector_id']; $sectorid = $value['id']; $template_data[$key] = [ 'sectorName' => $value['sector_id'], 'sort' => $key+1, ]; $date = date('mdHisu' ); // 获取时分秒微秒,转换为纯数字格式 // $date = 11010630021; if(substr($date,0,1) == 0){ $date = substr($date, 0, 10); // 截取前9位 }else{ $date = substr($date, 0, 9); // 截取前9位 } $random_num = intval(intval($date) . rand(1000, 9999)); // 拼接4位随机数,组成13位随机数 $y_num = $y_num + $value['pic_height']; $sector_width = $value['sector_width']/100; $canvas_data[$key] = [ 'i' => $random_num, 'x' => 0, 'y' => $y_num, 'w' => $sector_width, 'h' => $value['pic_height'], 'type' => $value['sector_id'], 'content' => [ 'sectorName' => $value['sector_id'], ] ]; // $sector_key[$key] = array_keys($value ); $sector_component = []; if(isset($groupedResults[$sectorid]) && !empty($groupedResults[$sectorid]) && is_array($groupedResults[$sectorid])){ foreach ($groupedResults[$sectorid] as $sortId => $components) { // 通栏某一位置的随机组件 if(count($components) > 1){ $componentIds = count($components); $selectedComponentId = rand(0, $componentIds - 1); }else{ $selectedComponentId = 0; } // var_dump($components[$selectedComponentId]); // $Component = $components[$selectedComponentId]; $Component['component_type'] = $components[$selectedComponentId]['component_type'] ?? 0; $Component['sort'] = $components[$selectedComponentId]['sort'] ?? 1; $Component['type_id'] = $components[$selectedComponentId]['type_id'] ?? 0; // 通栏某一位置的随机组件样式图 // 检查 $Component['images'] 是否存在且为数组 if (isset($components[$selectedComponentId]['images']) && is_array($components[$selectedComponentId]['images'])) { $images = count($components[$selectedComponentId]['images']); $selectedImage = 0; if ($images > 1) { $selectedImage = rand(0, $images - 1); } if (isset($components[$selectedComponentId]['images'][$selectedImage])) { $Component['component_style'] = is_object($components[$selectedComponentId]['images'][$selectedImage]) ? $components[$selectedComponentId]['images'][$selectedImage]->img_id : $components[$selectedComponentId]['images'][$selectedImage]['img_id']; } } $image_info[$key] = is_object($components[$selectedComponentId]['images'][$selectedImage]) ? $components[$selectedComponentId]['images'][$selectedImage] : $components[$selectedComponentId]['images'][$selectedImage]; // var_dump($images); $Component['componentData'] = $components[$selectedComponentId]['componentData'] ?? []; if($data['page'] == 1 &&(isset($components[$selectedComponentId]['type_id']) && in_array($components[$selectedComponentId]['type_id'],[7,14,15])) ){ $Component['componentData']['name'] = '请选择栏目'; } if($data['page'] == 2 && (isset($components[$selectedComponentId]['type_id']) && in_array($components[$selectedComponentId]['type_id'],[7,14,15]))){ $Component['componentData']['name'] = '自动生成'; } if(isset($components[$selectedComponentId]['type_id']) && $components[$selectedComponentId]['type_id'] == 11){ $ad = $components[$selectedComponentId]['ad']; $ad['thumb'] = $image_info[$key]['img_url']; $template_data[$key]['ad'] = $ad; $canvas_adinfo[$key] = $ad; $canvas_data[$key]['content']['ad'] = $canvas_adinfo[$key]; // // 确保 $ad 和 $canvas_adinfo[$key] 元素数量一致,直接组合 // // $canvas_data[$key]['ad'] = array_combine($ad, $canvas_adinfo[$key]); $Component['componentData'] = []; } if(isset($components[$selectedComponentId]['type_id']) && $components[$selectedComponentId]['type_id'] == 14){ $ad = $components[$selectedComponentId]['ad']; // $ad['thumb'] = $image_info[$key]['img_url']; $template_data[$key]['ad'] = $ad; $canvas_adinfo[$key] = $ad; $canvas_data[$key]['content']['ad'] = $canvas_adinfo[$key]; // // $canvas_data[$key]['ad'] = array_combine($ad, $canvas_adinfo[$key]); // // $Component['componentData'] = []; } // var_dump('---------------------------',$components[$selectedComponentId]); $sector_component[$sortId] = $Component; // $canvas_component[$sortId] = $Component; // $sector_component[$sortId] = $selectedComponentId; } // var_dump("image",$image); } $template_data[$key]['componentList'] = $sector_component; $canvas_data[$key]['content']['componentList'] = $sector_component; // $canvas_data[$key]['ad'] = $key; $canvas_data[$key]['dataSort'] = $key; $canvas_data[$key]['moved'] = false; // $page['template'][$key][$sector_key]['componentData'] = $result[$sectorId]; } // return $canvas_data; return [ 'template_data' => $template_data, 'canvas_data' => $canvas_data, ]; } }