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('updated_at', '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); } if (is_string($data['keyword'])) { $keyword = json_decode($data['keyword'], true) ?? $data['keyword']; } if (is_array($data['keyword'])) { $keyword = json_encode($data['keyword']); } $updateData = [ 'name' => $data['name'], 'keyword' => $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'] = array_map('trim', $data['template_keyword']); $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'] = array_map('trim', $data['template_keyword']); $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['height']) && !empty($data['height'])) { array_push($where, ['sector.pic_height', 'like', '%' . $data['height'] . '%']); } if (isset($data['page_type']) && !empty($data['page_type'])) { array_push($where, ['sector.page_type', 'like', '%' . $data['page_type'] . '%']); } $result = Sector::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(function ($query) { $query->where('sector_place.type_id', 1) ->orWhereNull('sector_place.type_id'); }) ->select( 'sector.*', '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(); 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_query = Sector::where('template_id', $data['template_id']); if (!empty($sector_query->where('sector_id', $data['sector_id'])->first())) { return Result::error('通栏编号已存在!'); } if (!empty($sector_query->where('sector_type', $data['place_type'])->where('component_code', $data['component_code'])->first())) { 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 = []; $component = Component::whereIn('component_type', $sort) ->leftJoin('component_type', 'component.type_id', 'component_type.id') ->select('component.*', 'component_type.cat_num') ->get()->all(); // return Result::success($data); if (empty($component)) { return Result::error('组件不存在!'); } $data['cat_num'] = array_sum(array_column($component, 'cat_num')); $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); $component_id = array_values(array_unique(array_merge(...$sort))); $component = Component::whereIn('component_type', $component_id) ->leftJoin('component_type', 'component.type_id', 'component_type.id') ->select('component.*', 'component_type.cat_num') ->get()->all(); // return Result::success($data); if (empty($component)) { return Result::error('组件不存在!'); } $data['cat_num'] = array_sum(array_column($component, 'cat_num')); $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') ) ->orderBy('component.component_type', 'desc') ->groupBy('component.id') // 按组件ID分组,确保统计每个组件的图片数量 ->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); $type = $data['type_id']; switch ($type) { case 19: //19:选项卡-资讯-推荐图类组件; $type = 3; break; case 20: //20:选项卡-资讯-最新类组件; $type = 4; break; case 21: //21:选项卡-资讯-推荐类组件; $type = 5; break; case 22: //22:选项卡-资讯-热点类组件; $type = 6; break; case 23: //23:选项卡-资讯-栏目类组件; $type = 7; break; case 24: //24:选项卡-资讯-广告混合类组件; $type = 14; break; } switch ($type) { 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: //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'] == 24) { unset($add_arr['ad']); $add_arr['ad'][0] = $ad; $add_arr['ad'][1] = $ad; $add_arr['ad'] = json_encode($add_arr['ad'], true); } // if----带广告的特殊处理(广告位默认图上传静态资源的处理---已废弃) // 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 { // return Result::success($add_arr); $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); $type = $data['type_id']; switch ($type) { case 19: //19:选项卡-资讯-推荐图类组件; $type = 3; break; case 20: //20:选项卡-资讯-最新类组件; $type = 4; break; case 21: //21:选项卡-资讯-推荐类组件; $type = 5; break; case 22: //22:选项卡-资讯-热点类组件; $type = 6; break; case 23: //23:选项卡-资讯-栏目类组件; $type = 7; break; case 24: //24:选项卡-资讯-广告混合类组件; $type = 14; break; } switch ($type) { 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']); if(empty($data['ad_img'])){ $data['ad_img'] = $data['component_img']; } $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' => '[]', ]; if ($data['type_id'] == 24) { unset($add_arr['ad']); $add_arr['ad'][0] = $ad; $add_arr['ad'][1] = $ad; $add_arr['ad'] = json_encode($add_arr['ad'], true); } 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::insertGetId([ 'website_id' => $data['website_id'], 'page_type' => $page_type, 'user_id' => $data['user_id'], 'status' => 0, 'action_id' => 1, ]); } 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 { if ($data['template_id'] != $website_template_info['template_id']) { $info_data = [ 'template_id' => $data['template_id'], 'status' => 0, 'action_id' => 2, 'user_id' => $data['user_id'] ]; $template = [ 'template_id' => $data['template_id'], 'user_id' => $data['user_id'], 'template_data' => null, 'canvas_data' => null ]; } else { $info_data = [ 'template_id' => $data['template_id'], 'user_id' => $data['user_id'], 'action_id' => 2, ]; $template = [ 'template_id' => $data['template_id'], 'user_id' => $data['user_id'] ]; } $result['template_info'] = WebsiteTemplateInfo::where('website_id', $data['website_id']) ->update($info_data); if (empty($result['template_info'])) { Db::rollBack(); return Result::error('选择皮肤失败!'); } $result['template'] = WebsiteTemplate::where('website_id', $data['website_id']) ->update($template); 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 { $data['template_id'] = $data['template_id']?? $website_template_info->template_id; $template = WebsiteTemplate::where('website_id', $data['website_id'])->update( [ 'template_data' => $data['template_data'], 'user_id' => $data['user_id'], 'canvas_data' => $data['canvas_data'], 'template_id' => $data['template_id'] ] ); } 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) ->select('sector_place.*') ->orderBy('sector_type', 'desc'); $result['count'] = $sector_place->count(); $result['row'] = $sector_place->paginate($data['page_size'], ['*'], 'page', $data['page']); if ($data['type_id'] == 2) { $result['map_json'] = SectorPlace::where('sector_type', $data['sector_type'])->where('type_id', 1)->pluck('map'); } if (empty($result['count'])) { return Result::error("暂无相关版式数据!"); } return Result::success($result); } /** * 通栏版式管理-添加通栏版式 */ public function addSectorPlace(array $data): array { unset($data['user_id']); // 1:通栏;2:组件; if ($data['type_id'] == 1) { $sector_place = SectorPlace::where('sector_type', $data['sector_type'])->first(); if (!empty($sector_place)) { return Result::error('通栏版式类别编号已存在!'); } $data['sort_id'] = 0; } 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('type_id', 2)->where('sector_type', $data['sector_type'])->get()->all(); if (!empty($component)) { if (count($component) >= $sector_type['component_num']) { return Result::error('通栏版式组件数量已达上限!'); } if (in_array($data['sort_id'], array_column($component, 'sort_id')) && in_array($data['line_num'], array_column($component, 'line_num')) && in_array($data['column_num'], array_column($component, 'column_num'))) { return Result::error('组件版式位置已存在!'); } if (in_array($data['component_type'], array_column($component, 'component_type'))) { return Result::error('组件版式编号已存在!'); } $data['component_num'] = 0; } if (in_array($data['type'], [23, 24])) { $data['cat_num'] = 2; } else if (in_array($data['type'], [7, 14])) { $data['cat_num'] = 1; } else { $data['cat_num'] = 0; } } Db::beginTransaction(); try { $result = SectorPlace::insertGetId($data); if (empty($result)) { Db::rollBack(); return Result::error('添加失败!'); } if ($data['type_id'] == 2 && count($component) + 1 == $sector_type['component_num']) { $map = $this->changeSectorPlaceJson($sector_type['sector_type']); $sector_place_map = SectorPlace::where('sector_type', $sector_type['sector_type']) ->where('type_id', 1)->update(['map' => json_encode($map['map_json']), 'cat_num' => $map['cat_num']]); if ($sector_place_map === false) { Db::rollBack(); return Result::error('通栏版式画布数据更新失败!'); } } Db::commit(); return Result::success($result); } catch (\Exception $e) { Db::rollBack(); return Result::error($e->getMessage()); } } /** * 通栏版式管理-版式画布 * @param array $data * @return array */ public function changeSectorPlaceJson($sector_type) { $component = SectorPlace::where('type_id', 2)->where('sector_type', $sector_type)->get()->all(); // $component[count($component)] = $data; $sector_type_info = SectorPlace::where('sector_type', $sector_type)->where('type_id', 1)->first(); if (empty($sector_type_info)) { return Result::error('通栏版式类别不存在!'); } $allcomponent_type = array_column($component, 'type'); if ($sector_type_info['column_num'] > 1 && in_array(13, $allcomponent_type)) { $aboutArtcle_catkey = array_search(13, $allcomponent_type); // 确保取到的是数字类型的 width,避免与模型对象相乘 $width = (int) ($component[$aboutArtcle_catkey]['width'] ?? 1); $sector_type_info['width'] = $sector_type_info['width'] + $width; } $row_num = 1; $cat_num = 0; $row_width = []; $map = []; // 按 column_num 分组并取每组最小 width $col_width = []; foreach ($component as $item) { $col = $item['column_num'] - 1; if (!isset($grouped[$col]) || $item['width'] < $grouped[$col]['width']) { $column_width[$col] = $item['width']; // $column_width[$col][$item['line_num']-1] = $item['line_num']; } } // $grouped 即为分组后各组 width 最小的记录 // return Result::success($column_width); if (count($component) == $sector_type_info['component_num']) { foreach ($component as $key => $val) { if (in_array($val['type'], [23, 24])) { $cat_num = $cat_num + 2; } if (in_array($val['type'], [7, 14])) { $cat_num = $cat_num + 1; } if (!isset($map[$val['column_num'] - 1]['col_data']['row_num'])) { $row_num = 1; $map[$val['column_num'] - 1]['col_data']['row_num'] = $row_num; } else { $map[$val['column_num'] - 1]['col_data']['row_num'] = $row_num + 1; } // 原代码使用 $sector_type['width'] 有误,应该使用 $sector_type_info // $width = number_format((($val['width'] / $sector_type_info['width']) * 100 - 1.5), 2); $height = number_format((($val['height'] / $sector_type_info['height']) * 100 - 2.5), 2); $col_width = $column_width[$val['column_num'] - 1]; $width = number_format((($val['width'] / $col_width) * 100 - 1.5), 2); $col_width = number_format((($col_width / $sector_type_info['width']) * 100 - 1.5), 2); $row_key = intval($val['line_num'] - 1); $map[$val['column_num'] - 1]['col_data']['row_data'][$row_key] = [ 'row_width' => $width . '%', 'row_height' => $height . '%', 'component_sort' => $val['sort_id'], ]; $map[$val['column_num'] - 1]['col_data']['col_width'] = $col_width . '%'; } } // 补全缺失的行,确保row_data是连续的索引数组 if (!empty($map)) { // 为每一列补全缺失的行 for ($col_index = 0; $col_index < count($map); $col_index++) { if (isset($map[$col_index]['col_data']['row_data'])) { $row_data = &$map[$col_index]['col_data']['row_data']; // 检查当前行数据是否是连续的索引数组 $rows = array_keys($row_data); if (empty($rows)) { continue; } // 检查是否存在断连 $min_row = min($rows); $max_row = max($rows); $is_continuous = (count($rows) === $max_row - $min_row + 1); // 只有当行号不连续时才进行补全 if (!$is_continuous || $min_row > 0) { $temp_row_data = []; // 遍历从0到最大行号的所有行 for ($row_index = 0; $row_index <= $max_row; $row_index++) { if (isset($row_data[$row_index])) { // 保留原有行数据 $temp_row_data[$row_index] = $row_data[$row_index]; } else { // 补全新行数据 $temp_row_data[$row_index] = [ 'row_width' => '1%', // 固定为1% 'row_height' => '30.83%', // 默认高度 'component_sort' => null // 补全的行没有component_sort ]; // 如果不是第一列,尝试从上一列获取高度 if ($col_index > 0 && isset($map[$col_index - 1]['col_data']['row_data'][$row_index])) { $temp_row_data[$row_index]['row_height'] = $map[$col_index - 1]['col_data']['row_data'][$row_index]['row_height']; } } } // 更新为连续的索引数组 $map[$col_index]['col_data']['row_data'] = array_values($temp_row_data); // 更新row_num $map[$col_index]['col_data']['row_num'] = count($temp_row_data); } elseif (!is_array(reset($row_data))) { // 如果是关联数组但键是连续的,转换为索引数组 ksort($row_data); $map[$col_index]['col_data']['row_data'] = array_values($row_data); } } } } $map_data = [ 'map_json' => $map ?? [], 'component' => count($component), 'row_num' => $row_num ?? 0, 'cat_num' => $cat_num ?? 0, 'component_info' => $component, ]; return $map_data; } /** * 通栏版式管理-修改通栏版式 */ public function upSectorPlace($data): array { unset($data['user_id']); // return Result::success($data); // $id = $data['id']; // 1:通栏;2:组件; if ($data['type_id'] == 1) { $sector_type = SectorPlace::where('id', $data['id']) ->where('type_id', 1) ->first(); if (empty($sector_type)) { return Result::error('通栏版式不存在!'); } unset($data['sort_id']); unset($data['component_type']); unset($data['type']); // return Result::success($map); $sector_place_id = SectorPlace::where('id', '!=', $data['id'])->where('sector_type', $data['sector_type'])->where('type_id', 1)->first(); if (!empty($sector_place_id)) { return Result::error('通栏版式编号已存在!'); } $map = $this->changeSectorPlaceJson($sector_type['sector_type']) ?? []; if ($data['component_num'] == $map['component']) { $data['cat_num'] = $map['cat_num']; $data['map'] = json_encode($map['map_json']); } else { $data['cat_num'] = $map['cat_num']; $data['map'] = ''; } if ($data['sector_type'] != $sector_type['sector_type'] && $map['component'] != 0) { $com_place = SectorPlace::where('sector_type', $sector_type['sector_type']) ->where('type_id', 2) ->update(['sector_type' => $data['sector_type']]); if (empty($com_place)) { return Result::error('组件版式编号同步更新失败!'); } } $map = $this->changeSectorPlaceJson($data['sector_type']) ?? []; $data['cat_num'] = $map['cat_num'] ?? 0; $map = json_encode($map['map_json'] ?? []); $result = SectorPlace::where('id', $data['id'])->update($data); } else { $component_type = SectorPlace::where('id', $data['id'])->where('type_id', 2)->first(); if (empty($component_type)) { return Result::error('组件版式不存在!'); } unset($data['component_num']); unset($data['map']); $component = SectorPlace::where('sector_type', $data['sector_type']) ->where(function ($query) use ($data) { $query->where('component_type', $data['component_type']) ->Orwhere('sort_id', $data['sort_id']); }) ->where('id', '!=', $data['id']) ->where('type_id', 2) ->get()->all(); if (!empty($component)) { return Result::error('组件版式编号或位置已存在!'); } if (in_array($data['type'], [23, 24])) { $data['cat_num'] = 2; } else if (in_array($data['type'], [7, 14])) { $data['cat_num'] = 1; } else { $data['cat_num'] = 0; } $result['component'] = SectorPlace::where('id', $data['id'])->update($data); $map = $this->changeSectorPlaceJson($data['sector_type']) ?? []; $data['cat_num'] = $map['cat_num'] ?? 0; $map = json_encode($map['map_json'] ?? []); $result['map'] = SectorPlace::where('sector_type', $data['sector_type']) ->where('type_id', 1) ->update(['map' => $map, 'cat_num' => $data['cat_num']]); } if (empty($result)) { return Result::error('修改失败!'); } // $update_result['sector_place'] = SectorPlace::where('id',$data['id'])->first(); // $update_result['map'] = $map; return Result::success([ 'result' => $result, ]); } /** * 通栏版式管理-删除通栏版式 */ public function delSectorPlace(array $data): array { $sector_place = SectorPlace::where('id', $data['id'])->first(); if (empty($sector_place)) { return Result::error('通栏版式不存在!'); } $sector = Sector::where('place_type', $sector_place['sector_type'])->first(); if (!empty($sector)) { return Result::error('通栏版式分类已应用,暂不可删除!'); } Db::beginTransaction(); try { if ($sector_place['type_id'] == 1) { $where = ['sector_type' => $sector_place['sector_type']]; } else { $update_result = SectorPlace::where('sector_type', $sector_place['sector_type']) ->where('type_id', 1) ->whereNotNull('map') ->update(['map' => null]); if ($update_result === false) { Db::rollBack(); return Result::error('通栏版式画布数据清空失败!'); } $where = ['id' => $data['id']]; } $result = SectorPlace::where($where)->delete(); if (empty($result)) { Db::rollBack(); return Result::error('删除失败!'); } Db::commit(); return Result::success('删除成功!'); } catch (\Exception $e) { Db::rollBack(); return Result::error($e->getMessage()); } } /** * 自助建站-通栏版式管理-获取通栏版式详情 */ 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; // } $component_num = null; 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'); }) ->when(isset($data['component_num']) && !empty($data['component_num']), function ($query) use ($data) { // 子查询:统计每个 sector_type 下 type_id=2 的子级数量 $sub = SectorPlace::selectRaw('sector_type, COUNT(*) as child_count') ->where('type_id', 2) ->groupBy('sector_type'); // 关联子查询,并保证子级数量等于传入的 component_num $query->leftJoinSub($sub, 'component_place', function ($join) { $join->on('sector_place.sector_type', '=', 'component_place.sector_type'); })->whereRaw('component_place.child_count = ?', [$data['component_num']]); }) ->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']); if (!empty($component->where('img_id', $data['img_id'])->first())) { return Result::error('组件预览图编号已存在!'); } if (!empty($component->where('img_name', $data['img_name'])->first())) { 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']); if (!empty($component->where('img_id', $data['img_id'])->first())) { return Result::error('组件预览图编号已存在!'); } if (!empty($component->where('img_name', $data['img_name'])->first())) { 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(); if (isset($data['page']) && !empty($data['page'])) { switch ($data['page']) { case 1: $rule = json_decode($rule['index_rule']); break; case 2: $rule = json_decode($rule['class_rule']); break; case 3: $rule = json_decode($rule['list_rule']); break; case 4: $rule = json_decode($rule['info_rule']); break; case 5: $rule = json_decode($rule['search_rule']); break; case 6: $rule = json_decode($rule['footerlist_rule']); break; case 7: $rule = json_decode($rule['footerinfo_rule']); break; default: break; } } $query = WebsiteCategory::where('website_id', $data['website_id']); $cate_num = $query->where('is_show', 1)->count(); if ($cate_num < 5) { return '此网站栏目过少,无法进行随机模板!'; } $cate_num = min($cate_num, 24); // return Result::success($data); // $tempalte['index'] = $this->randomWeb($data,$rule); // return Result::success($tempalte); if (isset($data['page']) && !empty($data['page'])) { $tempalte['index'] = $this->randomWeb($data, $rule); } else { $data['page'] = 1; $ruleIndex = json_decode($rule->index_rule); $tempalte['index'] = $this->randomWeb($data, $ruleIndex); $data['page'] = 2; $ruleClass = json_decode($rule->class_rule); // $class_cate_num = $query->where('pid','!=',0)->groupBy('pid')->selectRaw('pid, count(*) as count')->get()->pluck('count','pid'); // $cate_num = max($class_cate_num); $tempalte['class'] = $this->randomWeb($data, $ruleClass); $data['page'] = 3; $ruleList = json_decode($rule->list_rule); $tempalte['list'] = $this->randomWeb($data, $ruleList); $data['page'] = 4; $ruleInfo = json_decode($rule->info_rule); $tempalte['info'] = $this->randomWeb($data, $ruleInfo); $data['page'] = 5; $ruleSearch = json_decode($rule->search_rule); $tempalte['search'] = $this->randomWeb($data, $ruleSearch); $data['page'] = 6; $ruleFooterlist = json_decode($rule->footerlist_rule); $tempalte['footerlist'] = $this->randomWeb($data, $ruleFooterlist); $data['page'] = 7; $ruleFooterinfo = json_decode($rule->footerinfo_rule); $tempalte['footerinfo'] = $this->randomWeb($data, $ruleFooterinfo); } // $index = $this->randomPage($data); return Result::success($tempalte); } // public function randomPage($data, $cate_num, $rule) // { // // return $rule; // $page = $data['page']; // $sectors = Sector::where('sector.template_id', $data['template_id']) // ->where('sector.page_type', 'like', '%' . $page . '%') // ->leftJoin('sector_place', function ($join) { // $join->on('sector.place_type', '=', 'sector_place.sector_type') // ->where('sector_place.type_id', 1); // }) // ->whereNotNull('sector_place.sector_type') // ->select( // 'sector.sector_name', // 'sector.sector_id', // 'sector.pic_height', // 'sector.sector_img', // 'sector.sector_type', // 'sector.sector_code', // 'sector.id', // 'sector.sector_width', // 'sector.component_num', // 'sector.cat_num', // ) // ->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); // //获取每个通栏类别在其中的位置 -------暂时不考虑先,应该是必需通栏类型或者非必需通栏类型,每个类型应该取一个,其余取可重复的通栏 // $all_sector_type = array_column($sectors, 'sector_type'); // $grouped = array_reduce( // array_keys($sectors), // function ($carry, $idx) use ($sectors) { // $carry[$sectors[$idx]['sector_type']][] = $idx; // return $carry; // }, // ); // // 对 all_sector_type 去重后,每个类型随机挑一个 sector 索引 // $unipue_sector_keys = array_combine( // array_keys($grouped), // array_map( // function ($type) use ($grouped) { // // 随机取一个索引 // return $grouped[$type][array_rand($grouped[$type])]; // }, // array_keys($grouped) // ) // ); // // 1. 必需通栏:同一类型只随机保留一条 // $rule_must_type = $rule->must_type; // $sector_num = $rule->max_num; // $unique_sector_type = array_flip($unipue_sector_keys); // $must_type = array_unique(array_intersect($unique_sector_type,$rule_must_type)); // // 先求交集,去重后统计 // $is_must = count($must_type); // if ($is_must != count(array_unique($must_type))) { // return '此页面缺少必要通栏!'; // } // // 先按类型把 sectors 分组,key 为 sector_type,value 为对应 sector 数组 // // return [ // // 'unipue_sector_keys'=>$unipue_sector_keys, // // 'grouped'=>$grouped, // // ]; // // 最终保留的 must_type 数组(去重且随机保留一个) // // return [ // // 'must_key'=>$must_key,'must_type'=>$must_type, // // 'must_sector_keys'=>$must_sector_keys // // ]; // // $must_sector_keys = array_intersect_key($unipue_sector_keys,$must_type); // $must_sector = array_values(array_intersect_key($sectors,$must_type)); // $must_count = count($must_sector); // // return $sector_comnum; // // $must_sector = array_column($must_sector,'sector_type'); // // 目前按照首页必需通栏的数量进行处理,若是存在必需通栏的数量大于查询到的通栏数量,则返回必需通栏的数量 // if ($must_count > $sector_num) { // return $must_sector; // } // // 必需通栏中的栏目数量 // if($page == 1){ // $must_cat_num = array_sum(array_column($must_sector,'cat_num')); // if($must_cat_num>$cate_num){ // return '栏目数量过少,无法完成必需通栏随机!'; // } // } // // var_dump(['must_cat_num'=>$must_cat_num,'cate_num'=>$cate_num]); // // 2.非必须通栏的随机处理 // $not_must_type = array_unique(array_diff($unique_sector_type, $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); // } // if($page == 1){ // $random_cat_num = array_sum(array_column($random_sector,'cat_num')); // $random_cat_num += $must_cat_num; // if($random_cat_num>$cate_num){ // return '栏目数量过少,无法完成随机!'; // } // } // // var_dump("randomKeys",$randomKeys); // // var_dump("random_sector",$random_sector); // // var_dump("rand_count", $rand_count); // // var_dump("random_cat_num", $random_cat_num); // // return $random_sector; // // var_dump("must_sector",$must_sector); // // var_dump("sector_num",$sector_num); // // var_dump("must_count",$must_count); // // return ['random_sector'=>$random_sector,'random_cat_num'=>$random_cat_num,'cate_num'=>$cate_num]; // // 3.计算随机之后减去必需的通栏,查询重复通栏可用数量;(目前的这些通栏没有重复的同类别通栏) // $repeat_num = $sector_num - $rand_count - $must_count; // // 组合起来通栏 // $sector_zuhe1 = array_merge($must_sector, $random_sector); // $sector_zuhe1_type = array_column($sector_zuhe1, 'sector_type'); // $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); // // 剩余通栏进行重复处理 // $rule_repeat = (array)$rule->type_max; // // 取到可以重复的通栏类别 // $rule_repaet_type = array_keys($rule_repeat); // $repeat_type = array_intersect($rule_repaet_type, $unique_sector_type); // // 获取可以随机重复的通栏 // $repaet_key = 0; // $type = []; // // return $rule_repaet_type; // // 防止无限循环,设置最大迭代次数 // $max_iterations = 10; // $iteration_count = 0; // // 剩余的栏目数量 // if($page == 1){ // $sector_cate_comnum = $cate_num - $random_cat_num; // if($sector_cate_comnum < 0){ // return '栏目数量过少,无法完成随机!'; // } // } // // return [ // // // 'repeat_type'=>$repeat_type, // // // 'repeat_num'=>$repeat_num, // // 'must_count'=>$must_count, // // 'rand_count'=>$rand_count, // // 'repeat_num'=>$repeat_num, // // 'sector_num'=>$sector_num, // // 'cate_num'=>$cate_num, // // 'sector_comnum'=>$sector_cate_comnum, // // 'random_cat_num'=>$random_cat_num, // // // 'rule_repeat'=>$rule_repeat, // // // 'unique_sector_type'=>$unique_sector_type, // // // 'all_sector_type'=>$all_sector_type, // // // 'sector_zuhe1' =>$sector_zuhe1 // // ]; // // -----新添加的代码----- // $re_sector = array_intersect($all_sector_type,$repeat_type); // $re_sector_key = array_keys($re_sector); // $every_repeattype_num = array_count_values($re_sector); // // $re_sector_key = array_keys($re_sector,1); // $repaet_key = 0; // $re_type_keys = []; // $one_sector_key = array_search(1,$re_sector); // $two_sector_key = array_search(2,$re_sector); // $three_sector_key = array_search(3,$re_sector); // $all_cat_num = array_column($sectors,'cat_num'); // $repeat_sector_key = []; // // $rule_repeat['1'] = 1; // // return $rule_repeat; // // $three_sector_key = array_search(3,$re_sector); // // $min_one_sector = max(array_column('cat_num',$all_sector)); // // 随机重复通栏----- // // $rule_repeat['1'] = 1; // // // 找出 all_sector_type 中值为 1 的键 // while ($repaet_key <= $repeat_num-1 && $iteration_count < $max_iterations) { // // 剩余通栏数量>0 若不是首页则继续;若是首页则需要判断剩余栏目数量是否>3 // if ($repeat_num-$repaet_key > 0 && ($page != 1 || (isset($sector_cate_comnum) && $sector_cate_comnum > 3))) { // foreach($repeat_type as $key => $value){ // $type_key = strval($value); // if(!isset($re_type_keys[$value])){ // $re_type_keys[$value] = []; // } // // 剩余的通栏数量>0 并且 剩余的此类通栏数量>0 // if($repeat_num-$repaet_key > 0 && $rule_repeat[$type_key]- count($re_type_keys[$value]) > 0 && ($page != 1 || (isset($sector_cate_comnum) && $sector_cate_comnum > 3))){ // // var_dump("0000",array_keys($re_sector,$value)); // $one_sector_type = array_keys($re_sector,$value); // // 若是此类通栏的数量>1 则随机取一个对应的键值 // if(count($one_sector_type) > 1){ // // 从 $one_sector_type 数组中随机取一个键名,并取其对应值 // $cat_key = $one_sector_type[array_rand($one_sector_type)]; // }else{ // $cat_key = $one_sector_type[0]; // } // $repeat_sector_key[$repaet_key] = $cat_key; // $repaet_key++; // // var_dump("1115----------cat_key",$cat_key); // if($page == 1){ // $sector_cate_comnum -= $all_cat_num[$cat_key]; // } // if(!in_array($repaet_key,$re_type_keys[$value])){ // $re_type_keys[$value][] = $repaet_key; // // var_dump("1117----------re_type_keys",$re_type_keys); // } // }else{ // break; // } // } // } else { // break; // } // $iteration_count++; // } // // var_dump("num", $num); // // var_dump("repeat_sector_key", $repeat_sector_key); // // 若是首页的通栏没有到10个,栏目还有剩余1~3个,则继续随机(已知当剩余栏目为4还会随机) // if($page == 1 && count($repeat_sector_key) < $repeat_num && $sector_cate_comnum > 0){ // if($sector_cate_comnum == 1){ // array_push($repeat_sector_key,$one_sector_key); // }else if($sector_cate_comnum == 2){ // array_push($repeat_sector_key,$two_sector_key); // }else{ // array_push($repeat_sector_key,$three_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]; // }else{ // continue; // } // } // } // $repeat_count = count($repeat_sector); // // var_dump("repeat_count", $repeat_count); // // return [ // // 'not_repeat_num'=>$rand_count + $must_count, // // // 'repeat_count'=>$repeat_count, // // 'repeat_num'=>$repeat_num, // // 'repeat_sector'=>$repeat_sector, // // 'repeat_type'=>$repeat_type, // // '$repaet_key'=>$repaet_key, // // 'all_sector_type'=>$all_sector_type, // // 're_sector'=>$re_sector, // // 're_sector_key'=>$re_sector_key, // // 'resdfddd'=>$every_repeattype_num, // // 'repeat_sector_key'=>$repeat_sector_key, // // // 'ddd'=>$ddd, // // // 'sector_num'=>$sector_num, // // // 'sector_zuhe1'=>count($sector_zuhe1), // // // // 'repeat_sector'=>$repeat_sector, // // // 'cate_num'=>$cate_num, // // // 'random_cat_num'=>$random_cat_num, // // 'rule_repeat' => $rule_repeat // // ]; // // // var_dump("repeat_sector", $repeat_sector); // // return $repeat_sector; // $sector = array_merge($repeat_sector,$sector_zuhe1); // }else{ // $sector = $sector_zuhe1; // } // // var_dump("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 && $pic_key){ // $sector[0] = $toutiao; // $sector[$toutiao_key] = $sort_1; // $sector[1] = $pic; // $sector[$pic_key] = $sort_2; // }else if($pic_key){ // $sector[0] = $pic; // $sector[$pic_key] = $sort_1; // }else{ // $sector[0] = $toutiao; // $sector[$toutiao_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; // }else if ($pic_key) { // $sector[1] = $pic; // $sector[$pic_key] = $sort_2; // }else if($toutiao_key){ // $sector[1] = $toutiao; // $sector[$toutiao_key] = $sort_2; // } // } // } // $sector_id = array_column($sector,'id'); // // if(count(array_intersect($sector,$must_sector)) != count($must_sector)){ // // return '此页面缺少必要通栏!'; // // } // // return Result::success([ // // // 'sector_id'=>$sector_id, // // 'sector'=>$sector, // // // 'cate_num'=>$cate_num, // // ]); // // $data['page'] = $page; // $component[] = $this->randomComponent($sector_id,$data,$sector); // var_dump("sector_id",$sector_id); // return [ // // 'sector'=>$sector, // // 'must_sector'=>$must_sector, // // 'sector_count'=>count($sector), // // 'sectors'=>$sectors, // // 'sector_comnum'=>$sector_comnum, // // 'com_num'=>$com_num, // // 'cate_num'=>$cate_num, // // '$sector_zuhe1_comnum'=>$sector_zuhe1_comnum+$com_num, // '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', function ($join) use ($data) { // $join->on('component_img.component_id', '=', 'component.component_type') // ->where('component_img.template_id', '=', $data['template_id'] ?? 0); // }) // ->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()->all(); // // return $rawResults; // $img_key = 0; // $com_key = 0; // foreach( $rawResults as $key => $value){ // $component_id = $value['component_type']; // $sort_id = $value['sort_id']-1; // $img_id = intval($value['img_id']); // $sector_id = $value['sectorid']; // $component_data = json_decode($value['component_data'] ?? '[]',true); // $component_column = json_decode($value['component_column'] ?? '[]',true); // $component_json['componentData'] = $component_data['componentData'] ?? []; // // 组件类型:1:头条;2:轮播图;3:推荐图;4:最新资讯;5:推荐资讯;6:热点资讯;7:栏目资讯类; // // 8:列表类;9:详情类;10:二级导航栏类;11:广告类;12:静态类;13:单页导航类;14:资讯广告混合类; // // 15:滚动图文类;16:搜索框类;17:单页列表类;18:单页详情类;19-24:选项卡3-7 // if(!in_array($value['type_id'],[10,11,12,16,])){ // $component_json['componentData']['listType'] = $component_column['listType'] ?? ''; // } // if(!isset($component_img[$component_id])){ // $component_img[$component_id] = []; // $com_key = 0; // }else{ // $com_key++; // } // if(!isset($component_img[$component_id][$sort_id])){ // $component_img[$component_id][$sort_id] = []; // $img_key = 0; // }else{ // $img_key++; // } // if(!empty($img_id)){ // $component_img[$component_id][$sort_id][$img_key] = // [ // 'component_id' => $value['component_type'], // 'img_id' => $value['img_id'], // 'component_imgurl' => $value['img_url'], // ]; // } // // else{ // // $component_img[$component_id][$sort_id][$img_key] = []; // // } // // $component_img = array_values($component_img[$component_id][$sort_id]); // if(in_array($value['type_id'],[19, 20, 21, 22, 23, 24])){ // $groupedResults[$sector_id][$sort_id] = // [ // 'component_type' => $component_id, // 'sort' => $value['sort_id'], // 'type_id' => $value['type_id'], // 'componentData' => [ // $component_json['componentData'], // $component_json['componentData'], // ], // 'images' => $component_img[$component_id][$sort_id] // ]; // }else{ // $groupedResults[$sector_id][$sort_id] = // [ // 'component_type' => $component_id, // 'sort' => $value['sort_id'], // 'type_id' => $value['type_id'], // 'componentData' => $component_json['componentData'], // 'images' => $component_img[$component_id][$sort_id] // ]; // } // if(in_array($value['type_id'],[11,14,24])){ // $groupedResults[$sector_id][$sort_id]['ad'] = json_decode($value['ad'],JSON_UNESCAPED_UNICODE); // } // // [$img_id-1] = $value['img_id']; // } // // return ['groupedResults'=>$groupedResults, // // 'component_data'=>$component_data]; // $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])) { // $componet_key = 0; // 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; // $Component['component_type'] = $components['component_type'] ?? 0; // $Component['sort'] = $components['sort'] ?? 1; // $Component['type_id'] = $components['type_id'] ?? 0; // // // 通栏某一位置的随机组件样式图 // // // 检查 $Component['images'] 是否存在且为数组 // if (isset($components['images']) && is_array($components['images'])) { // // var_dump('action1-------------------------------------sortId',$sortId); // $images = count($components['images']); // $selectedImage = 0; // if ($images > 1) { // $selectedImage = rand(0, $images - 1); // } // if (isset($components['images'][$selectedImage])) { // $Component['component_style'] = is_object($components['images'][$selectedImage]) ? $components['images'][$selectedImage]->img_id : $components['images'][$selectedImage]['img_id']; // } // } // if(isset($components['images'][$selectedImage])){ // $image_info[$key] = is_object($components['images'][$selectedImage]) ? $components['images'][$selectedImage] : $components['images'][$selectedImage]; // } // // var_dump($images); // // if(isset($components['type_id']) && in_array($components['type_id'], [19, 20, 21, 22, 23, 24])){ // // // $Component['componentData'][0] = $components['componentData'] ?? []; // // // $Component['componentData'][1] = $components['componentData'] ?? []; // // }else{ // $Component['componentData'] = $components['componentData'] ?? []; // // } // if ($data['page'] == 1 && (isset($components['type_id']) && in_array($components['type_id'], [7, 14, 15, 23, 24]))) { // if(in_array($components['type_id'], [23, 24])){ // $Component['componentData'][0]['name'] = '请选择栏目'; // $Component['componentData'][1]['name'] = '请选择栏目'; // }else{ // $Component['componentData']['name'] = '请选择栏目'; // } // } // if ($data['page'] == 2 && (isset($components['type_id']) && in_array($components['type_id'], [7, 14, 15, 23, 24]))) { // if(in_array($components['type_id'], [23, 24])){ // $Component['componentData'][0]['name'] = '自动生成'; // $Component['componentData'][1]['name'] = '自动生成'; // }else{ // $Component['componentData']['name'] = '自动生成'; // } // } // if (isset($components['type_id']) && $components['type_id'] == 11) { // $ad = $components['ad']; // $ad['thumb'] = $image_info[$key]['component_imgurl'] ?? ''; // $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['type_id']) && $components['type_id'] == 14) { // $ad = $components['ad']; // $ad['thumb'] = $image_info[$key]['component_imgurl'] ?? ''; // $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'] = []; // } // if (isset($components['type_id']) && $components['type_id'] == 24) { // $ad = $components['ad'][0]; // // $ad['thumb'] = $image_info[$key]['component_imgurl'] ?? ''; // $template_data[$key]['ad'][0] = $ad; // $template_data[$key]['ad'][1] = $ad; // $canvas_adinfo[$key] = $ad; // $canvas_data[$key]['content']['ad'][0] = $canvas_adinfo[$key]; // $canvas_data[$key]['content']['ad'][1] = $canvas_adinfo[$key]; // // $canvas_data[$key]['ad'] = array_combine($ad, $canvas_adinfo[$key]); // // // $Component['componentData'] = []; // } // // // var_dump('---------------------------',$components[$selectedComponentId]); // $sector_component[$componet_key] = $Component; // $componet_key++; // } // // 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, // ]; // } public function randomWeb($data, $rule) { $page = $data['page']; // 在使用 with 方法关联查询其他表时限制查询字段,不需要修改 Sector 的 model 文件, // 可以直接在查询时通过 select 方法指定要查询的字段。以下是修改后的代码: // 修正:去掉多余的英文分号,保持链式调用完整 $sectors = Sector::select( 'sector.id', 'sector.template_id', 'sector.page_type', 'sector.sector_id', 'sector.cat_num', 'sector.sector_name', 'sector.sector_type', 'sector.sector_width', 'sector.sector_height', 'sector.pic_height', 'sector.component_num' ) ->where('sector.template_id', $data['template_id']) ->where('sector.page_type', 'like', '%' . $data['page'] . '%') ->with([ 'sectorComponents' => function ($query) use ($data) { $query->select('sectorid', 'sort_id', 'component_id') ->with([ 'component' => function ($q) use ($data) { $q->select('id', 'component_type', 'component_data', 'component_code', 'component_column', 'ad', 'type_id') ->with([ 'componentImgs' => function ($subQ) use ($data) { $subQ->where('template_id', $data['template_id']) // ->select('id', 'img_id', 'img_name', 'img_url', 'template_id') ; } ]); } ]); } ]) ->get(); // 将模型对象转换为数组,确保关系字段正确命名 $sectors = $sectors->toArray(); if(empty($sectors)){ return '此网站没有 通栏!'; } // 提取并返回 component_imgs 内容,添加空安全检查 // return $sectors[0]['sector_components'][0]['component']['component_imgs'] ?? []; // return $sectors; $cat_column = [ 'website_id' => $data['website_id'], 'is_show' => 1, 'placeid' => 1, 'num' => 23, 'pid' => 0, ]; // 临时注入 WebsiteService,后续请改为属性注入 // 本项目中已存在 WebsiteServer 及其 getWebsiteModelCategory 方法,直接调用 $websiteServer = new \App\JsonRpc\WebsiteService(); $all_cate_data = $websiteServer->getWebsiteModelCategory($cat_column); $all_cate = $all_cate_data['data'] ?? []; $cate_num = count($all_cate); // return $all_cate; if ($cate_num < 5) { return 此网站栏目过少,无法进行随机模板!; } $all_cate = $all_cate->toArray(); $del_catid = 0; foreach ($all_cate as $k => $v) { if (!empty($v['web_url']) || $v['type'] != 1 || $v['pid'] == $del_catid) { $del_catid = $v['category_id'] ?? ''; unset($all_cate[$k]); } } $all_cate = array_values($all_cate); $cate_num = min($cate_num, 24); if(isset($sectors[0]['sector_type'])){ $all_sector_type = array_column($sectors, 'sector_type'); }else{ return '此网站没有 通栏类型!'; } // return $all_cate; $grouped = array_reduce(array_keys($sectors), function ($carry, $idx) use ($sectors) { $carry[$sectors[$idx]['sector_type']][] = $idx; return $carry; }, []); // 以通栏分类为索引 随机取一个通栏索引作为其值 $unipue_sector_keys = array_combine( array_keys($grouped), array_map(function ($type) use ($grouped) { // 随机取一个索引 return $grouped[$type][array_rand($grouped[$type])]; }, array_keys($grouped)) ); // 通栏类别:1:资讯类:2:广告类;3:混合类;4:搜索框类;5:导航类;6:头条类; // 7:轮播图类;8:静态资源类;9:单页(列表)类;10:单页(详情)类;11:列表类;12:详情类;13:栏目轮播图类;', // {"max_num": 10, "min_num": 6, "and_type": [6, 7], // "type_max": {"1": 20, "2": 4, "3": 3}, "must_type": [1, 2]} // 1. 必需通栏:同一类型只随机保留一条 $rule_must_type = $rule->must_type; $sector_num = $rule->max_num; $unique_sector_type = array_flip($unipue_sector_keys); // 必须通栏的类型 $must_type = array_unique(array_intersect($unique_sector_type, $rule_must_type)); // 先求交集,去重后统计 $is_must = count($must_type); if ($is_must != count(array_unique($must_type))) { return '此页面缺少必要通栏!'; } $must_sector = array_values(array_intersect_key($sectors, $must_type)); $must_count = count($must_sector); // 目前按照首页必需通栏的数量进行处理,若是存在必需通栏的数量大于查询到的通栏数量,则返回必需通栏的数量 if ($must_count > $sector_num) { return $must_sector; } // 必需通栏中的栏目数量 if ($page == 1) { $must_cat_num = array_sum(array_column($must_sector, 'cat_num')); if ($must_cat_num > $cate_num) { return '栏目数量过少,无法完成必需通栏随机!'; } } // 2.非必须通栏的随机处理 // 非必需通栏的类型 $not_must_type = array_unique(array_diff($unique_sector_type, $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]; } $random_sector = array_values(array_intersect_key($sectors, array_flip($randomKeys))); $rand_count = count($random_sector); } if ($page == 1 && $rand_count > 0) { $random_cat_num = array_sum(array_column($random_sector, 'cat_num')); $random_cat_num += $must_cat_num; if ($random_cat_num > $cate_num) { return '栏目数量过少,无法完成随机!'; } } // 3.计算随机之后减去必需的通栏,查询重复通栏可用数量;(目前的这些通栏没有重复的同类别通栏) $repeat_num = $sector_num - $rand_count - $must_count; // 组合起来通栏 if($rand_count > 0){ $sector_zuhe1 = array_merge($must_sector, $random_sector); }else{ $sector_zuhe1 = $must_sector; } $sector_zuhe1_type = array_column($sector_zuhe1, 'sector_type'); $sector_ids = array_column($sector_zuhe1, 'id'); if ($repeat_num > 0) { $rule_repeat = (array)$rule->type_max; 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); // 剩余通栏进行重复处理 $rule_repeat = (array)$rule->type_max; // 取到可以重复的通栏类别 $rule_repaet_type = array_keys($rule_repeat); $repeat_type = array_intersect($rule_repaet_type, $unique_sector_type); // 获取可以随机重复的通栏 $repaet_key = 0; $type = []; // return $rule_repaet_type; // 防止无限循环,设置最大迭代次数 $max_iterations = 10; $iteration_count = 0; // 剩余的栏目数量 if ($page == 1 && $rand_count > 0) { $sector_cate_comnum = $cate_num - $random_cat_num; if ($sector_cate_comnum < 0) { return '栏目数量过少,无法完成随机!'; } } // -----新添加的代码----- $re_sector = array_intersect($all_sector_type, $repeat_type); $re_sector_key = array_keys($re_sector); $every_repeattype_num = count($re_sector); // $re_sector_key = array_keys($re_sector,1); $repaet_key = 0; $re_type_keys = []; $all_cat_num = array_column($sectors, 'cat_num'); $repeat_sector_key = []; // $rule_repeat['1'] = 1; // return $rule_repeat; // $three_sector_key = array_search(3,$re_sector); // $min_one_sector = max(array_column('cat_num',$all_sector)); // 随机重复通栏----- // $rule_repeat['1'] = 1; // // 找出 all_sector_type 中值为 1 的键 while ($repaet_key <= $repeat_num - 1 && $iteration_count < $max_iterations) { // 剩余通栏数量>0 若不是首页则继续;若是首页则需要判断剩余栏目数量是否>3 if ($repeat_num - $repaet_key > 0 && ($page != 1 || (isset($sector_cate_comnum) && $sector_cate_comnum > 3))) { foreach ($repeat_type as $key => $value) { $type_key = strval($value); if (!isset($re_type_keys[$value])) { $re_type_keys[$value] = []; } // 剩余的通栏数量>0 并且 剩余的此类通栏数量>0 if ($repeat_num - $repaet_key > 0 && $rule_repeat[$type_key] - count($re_type_keys[$value])-1 > 0 && ($page != 1 || (isset($sector_cate_comnum) && $sector_cate_comnum > 3))) { // var_dump("0000",array_keys($re_sector,$value)); $one_sector_type = array_keys($re_sector, $value); // 若是此类通栏的数量>1 则随机取一个对应的键值 if (count($one_sector_type) > 1) { // 从 $one_sector_type 数组中随机取一个键名,并取其对应值 $cat_key = $one_sector_type[array_rand($one_sector_type)]; } else { $cat_key = $one_sector_type[0]; } $repeat_sector_key[$repaet_key] = $cat_key; $repaet_key++; // var_dump("1115----------cat_key",$cat_key); if ($page == 1) { $sector_cate_comnum -= $all_cat_num[$cat_key]; } if (!in_array($repaet_key, $re_type_keys[$value])) { $re_type_keys[$value][] = $repaet_key; // var_dump("1117----------re_type_keys",$re_type_keys); } } else { break; } } } else { break; } $iteration_count++; } // 随机的重复通栏键值转换成真正的通栏 $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]; } else { continue; } } } $repeat_count = count($repeat_sector); $sector = array_merge($repeat_sector, $sector_zuhe1); } else { $sector = $sector_zuhe1; } // var_dump("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]; if ($data['page'] == 1 || $data['page'] == '1') { if ($toutiao_key && $pic_key) { $sector[0] = $toutiao; $sector[$toutiao_key] = $sort_1; $sector[1] = $pic; $sector[$pic_key] = $sort_2; $place_count = 2; } else if ($pic_key) { $sector[0] = $pic; $sector[$pic_key] = $sort_1; $place_count = 1; } else { $sector[0] = $toutiao; $sector[$toutiao_key] = $sort_1; $place_count = 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; $place_count = 2; } else if ($pic_key) { $sector[1] = $pic; $sector[$pic_key] = $sort_2; $place_count = 1; } else if ($toutiao_key) { $sector[1] = $toutiao; $sector[$toutiao_key] = $sort_2; $place_count = 1; } } } //涉及广告的位置及数量处理 // 提取所有 sector_type 值 $sector_finsh_type = array_column($sector, 'sector_type'); // 统计值为 2 的数量 // 使用 array_filter 过滤出值为 2 的元素,再用 count 计算数量 $count_type_2 = count(array_filter($sector_finsh_type, fn($v) => $v == 2)); $count_not_type_2 = count($sector_finsh_type) - $count_type_2; // 查找所有值为 2 的键名(可能多个) $type_2_keys = array_keys($sector_finsh_type, 2); $ad_sector = $sector[$type_2_keys[0]]; foreach ($type_2_keys as $k => $v) { unset($sector[$v]); } $all_sector = []; $idx = 0; $ad = 0; // return $rule; // 将 $rule->type_max 转为数组后再取值,避免把 stdClass 当数组用 $typeMax = (array)($rule->type_max ?? []); $rull_adtype = $typeMax[2] ?? 0; $sector_num = count($sector); $sectorMax = $rule->max_num ?? []; foreach ($sector as $key => $value) { // 若是 6、7 直接放入 if (in_array($value['sector_type'], [6, 7])) { $all_sector[$idx++] = $value; } else { // 其余通栏:隔一个加一个广告通栏 if ($idx % 2 == 1 && isset($ad_sector) && $ad < $rull_adtype && $sector_num < $sectorMax) { $all_sector[$idx++] = $ad_sector; $sector_num++; $ad++; } $all_sector[$idx++] = $value; } if(empty($sector) && in_array(2, $rule_must_type)){ $all_sector[0] = $ad_sector; } } // -------------未完待续----------------- $sector_id = array_column($sector, 'id'); $y_num = 0; $cat_key = 0; $ad_key = 0; // return $all_sector; foreach ($all_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, // 'com_num' => $value['component_num'], ]; $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位随机数 $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'], ] ]; $y_num = $y_num + $value['pic_height']; // $componentData = []; $sector_component = []; for ($i = 0; $i < $value['component_num']; $i++) { // $component_data = []; $componentData = []; $component_imgs = $value['sector_components'][$i]['component']['component_imgs'] ?? []; $component_type = $value['sector_components'][$i]['component']['type_id'] ?? 0; $component_data = json_decode($value['sector_components'][$i]['component']['component_data'] ?? '[]', true) ?? []; $list_type = json_decode($value['sector_components'][$i]['component']['component_column'] ?? '[]', true) ?? []; if (!empty($component_data) && !empty($list_type)) { if (in_array($component_type, [23, 24])) { $componentData[0] = $component_data['componentData']; $componentData[1] = $component_data['componentData']; }else{ $componentData = $component_data['componentData']; } if ($component_type != 10) { if (in_array($component_type, [23, 24])) { $componentData[0]['listType'] = $list_type['listType']; $componentData[1]['listType'] = $list_type['listType']; }else{ $componentData['listType'] = $list_type['listType']; } } } if (in_array($component_type, [7, 14, 15, 23, 24]) && ($page == 1 || $page == 2)) { if ($page == 1) { if(!empty($all_cate[$cat_key]['real_category_arr_id'])){ $cat_arr[$cat_key] = json_decode($all_cate[$cat_key]['real_category_arr_id'],true); }else{ $cat_arr[$cat_key] = json_decode($all_cate[$cat_key]['category_arr_id'],true); } if(!empty($all_cate[$cat_key+1]['real_category_arr_id'])){ $cat_arr[$cat_key+1] = json_decode($all_cate[$cat_key+1]['real_category_arr_id'],true); }else{ $cat_arr[$cat_key+1] = json_decode($all_cate[$cat_key+1]['category_arr_id'],true); } if (in_array($component_type, [23, 24])) { $componentData[0]['name'] = $all_cate[$cat_key]['alias']; $componentData[0]['category_id'] = $all_cate[$cat_key]['category_id']; $componentData[0]['category_arr'] = $cat_arr[$cat_key]; $componentData[1]['name'] = $all_cate[$cat_key + 1]['alias']; $componentData[1]['category_id'] = $all_cate[$cat_key + 1]['category_id']; $componentData[1]['category_arr'] = $cat_arr[$cat_key + 1]; $cat_key += 2; } else { $componentData['name'] = $all_cate[$cat_key]['alias']; $componentData['category_arr'] = $cat_arr[$cat_key]; $componentData['category_id'] = $all_cate[$cat_key]['category_id']; $cat_key++; } } if ($page == 2) { $componentData['name'] = '自动生成'; } } if (!empty($component_imgs)) { $component_imgids = array_column($component_imgs, 'img_id'); $component_imgid = array_rand($component_imgids); } if (in_array($component_type, [11, 14, 24])) { $ad_data = json_decode($value['sector_components'][$i]['component']['ad'] ?? '[]', true) ?? []; $component_imgkey = array_search($component_imgid, $component_imgids); $ad_data['thumb'] = $component_imgs[$component_imgkey]['img_url']; if ($component_type == 24) { $ad[0] = $ad_data; $ad[1] = $ad_data; } else { $ad = $ad_data; } $template_data[$key]['ad'] = $ad; $canvas_data[$key]['content']['ad'] = $ad; } $sector_component[$i] = [ 'component_type' => $value['sector_components'][$i]['component']['component_type'], 'sort' => $value['sector_components'][$i]['sort_id'], 'type_id' => $component_type, // 'page' => $page, 'component_style' => $component_imgids[array_rand($component_imgids)], // 若是上个数组为[1]随机为0,原因:array_rand 在单元素数组时必然返回 0 'componentData' => $componentData, // 'type_id' => $value['sector_components'][$i]['component']['type_id'], ]; } $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]; } $datas['component'][0] = [ 'template_data' => $template_data, 'canvas_data' => $canvas_data, ]; return $datas; } // return $component_img; // } //level public function addLevel(array $data) { var_dump($data, 'request data'); $level = new Level(); $level->fill($data); $level->save(); return Result::success($level->toArray()); } public function updateLevel(array $data) { $level = Level::find($data['id']); $level->fill($data); $level->save(); return Result::success($level->toArray()); } public function deleteLevel(array $data) { $level = Level::find($data['id']); $level->delete(); return Result::success(); } public function getLevelInfo(array $data) { $level = Level::find($data['id']); return Result::success($level); } public function getLevelList(array $data) { $where = []; if (isset($data['name'])) { $where[] = ['name', 'like', '%' . $data['name'] . '%']; } $level = Level::where($where) ->orderBy('updated_at', 'desc') ->paginate(($data['page_size'] ?? 10) * ($data['page'] ?? 1), ['*'], 'page', $data['page'] ?? 1); return Result::success([ 'data' => $level->items(), 'total' => $level->total(), ]); } public function getLevel(array $data) { $where = []; if (isset($data['pid'])) { $where = [ 'pid' => $data['pid'] ?? 0 ]; } $result = level::when(!empty($where), function ($query) use ($where) { $query->where($where); })->orderBy("sort", "desc")->get(); var_dump($result); if (empty($result)) { return Result::error("查询失败", 0); } else { return Result::success($result); } } /** * 自助建站-添加样式代码 */ public function addStyleCode(array $data): array { unset($data['status']); $style_code = StyleCode::insertGetId($data); if (empty($style_code)) { return Result::error('添加失败!'); } return Result::success($style_code); } /** * 自助建站-获取样式代码 */ public function getStyleCode(array $data): array { unset($data['status']); $style_code = StyleCode::get()->all(); if (empty($style_code)) { return Result::error('样式代码不存在!'); } return Result::success($style_code); } /** * 自助建站-获取所有通栏、组件、组件样式 */ public function getAllSectorComponentStyle(array $data): array { $sector_component_style = Sector::where('sector.template_id', $data['template_id']) ->leftJoin('sector_component', 'sector.id', 'sector_component.sectorid') ->leftJoin('component', 'component.component_type', 'sector_component.component_id') ->leftJoin('component_img', 'component_img.component_id', 'component.component_type') // ->leftJoin('sector', 'sector.id', 'sector_component.sectorid') ->where('component_img.template_id', '=', $data['template_id']) ->select( 'sector.sector_id', 'sector.sector_keyword', 'sector.component_num', 'sector.sector_width', 'sector.sector_height', 'sector.pic_height', 'component.component_type', 'component.component_keyword', 'component.component_data', 'component.component_column', 'component.ad', 'component.type_id', '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()->all(); $groupedResults = []; $sort_ids = []; $sector_ids = []; $component_list = []; $component_ids = []; $img_ids = []; // return Result::success($sector_component_style); foreach ($sector_component_style as $key => $item) { $sectorId = $item->sectorid; $sortId = $item->sort_id; $sector_type = $item->sector_type; $component_num = $item->component_num; $componentId = $item->component_type; $component_data = $item->component_data; $component_column = $item->component_column; $component_keyword = $item->component_keyword; $component_type = $item->type_id; $component_ad = $item->ad; $imgId = $item->img_id; $sector_height = $item->sector_height; $pic_height = $item->pic_height; $sectorName = $item->sector_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; // } if (!in_array($sectorId, $sector_ids)) { $groupedResults[$sectorId] = [ 'sector_id' => $sectorName, 'sector_keyword' => $item->sector_keyword, 'component_num' => $component_num, 'sector_width' => $item->sector_width, 'sector_height' => $sector_height, 'pic_height' => $pic_height, ]; } $sort_id = intval($sortId - 1); if (!isset($com_key) || !in_array($sectorId, $sector_ids) || !isset($sort_ids[$sectorId]) || !in_array($sortId, $sort_ids[$sectorId])) { $com_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]['component_list'][$sort_id][$com_key] = [ 'component_type' => $item->component_type, 'type_id' => $component_type, 'sort' => $sortId, ]; } if ($component_type == 11) { $groupedResults[$sectorId]['component_list'][$sort_id][$com_key]['component_style_data'][$img_key] = [ 'img_name' => $item->img_name, 'img_id' => $imgId, 'img_url' => $item->img_url, ]; } else { $groupedResults[$sectorId]['component_list'][$sort_id][$com_key]['component_style_data'][$img_key] = [ 'img_name' => $item->img_name, 'img_id' => $imgId, ]; } $com_key = 0; // } // 组件分类:1.头条资讯;2.轮播图资讯;3.推荐图;4.最新资讯;5.推荐资讯;6.热点资讯;7.栏目资讯;8.列表类;9.详情类; // 10.二级导航类;11.广告类;12.静态类;13.单页导航类;14.资讯广告混合类;15.滚动图文类;16.搜索框类;17.单页列表类;18.单页详情类; // 19:选项卡-推荐图类;20:选项卡-最新资讯类;21:选项卡-推荐资讯类;22:选项卡-热点资讯类;23:选项卡-栏目资讯类;24:选项卡-栏目广告混合类; if (is_scalar($sectorId) && is_scalar($sort_id)) { if (!in_array($component_type, [10, 12, 16])) { if ($component_type == 11 || $component_type == 14 || $component_type == 24) { $groupedResults[(int)$sectorId]['component_list'][(int)$sort_id][$com_key]['ad'] = $component_ad; $groupedResults[(int)$sectorId]['component_list'][(int)$sort_id][$com_key]['ad']['website_id'] = $data['website_id']; } if ($component_type != 11) { $groupedResults[(int)$sectorId]['component_list'][(int)$sort_id][$com_key]['componentData'] = $component_code['componentData']; } } else { $groupedResults[(int)$sectorId]['component_list'][(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; $sort_ids[$sectorId][$sort_id] = $sort_id; // $img_ids[$componentId] = $imgId; } // return Result::success($groupedResults); if (empty($groupedResults)) { return Result::error('通栏组件样式不存在!'); } $groupedResults = array_values($groupedResults); return Result::success($groupedResults); } /** * 自助建站-ai会话-获取会话列表 */ public function getAiSessionList(array $data): array { $where['user_id'] = $data['user_id']; if (isset($data['website_id']) && !empty($data['website_id'])) { $where['website_id'] = $data['website_id']; } if (isset($data['template_id']) && !empty($data['template_id'])) { $where['template_id'] = $data['template_id']; } $result = AichatSession::when(!empty($where), function ($query) use ($where) { $query->where($where); })->get()->all(); if (empty($result)) { return Result::error('会话不存在!'); } return Result::success($result); } /** * 自助建站-ai会话-创建会话 */ public function addAiSession(array $data): array { $website = Website::where('id', $data['website_id'])->first(); if (empty($website)) { return Result::error('网站不存在!'); } // $template = Template::where('template_id',$data['template_id'])->first(); // if(empty($template)){ // return Result::error('皮肤不存在!'); // } $date = date('Hisms'); // $date = 1631191019 $rand_num = rand(1000, 9999); $data['session_id'] = $date . $rand_num; // $data['session_id'] = 16320610069512 $ai_sessions = AichatSession::where('user_id', $data['user_id'])->pluck('session_id')->all(); if (count($ai_sessions) >= 5) { return Result::error('会话数量已达上限!'); } if (!empty($data['session_id']) && in_array($data['session_id'], $ai_sessions)) { $rand_num = rand(1000, 9999); $data['session_id'] = $date . $rand_num; } $result = AichatSession::insert($data); if (empty($result)) { return Result::error('创建会话失败!'); } return Result::success($data['session_id']); } /** * 自助建站-ai会话-修改会话 */ public function upAiSession(array $data): array { $where['user_id'] = $data['user_id']; $where['session_id'] = $data['session_id']; // return Result::success($where); $aichat_session = AichatSession::where($where)->first(); if (empty($aichat_session)) { return Result::error('会话不存在!'); } unset($data['session_id']); // 会话状态:1-开启,0-关闭 // if(isset($data['is_active']) && $aichat_session['is_active'] == $data['is_active']){ // return Result::success('会话状态已为'.$data['is_active'].'!'); // } // if(isset($data['is_active']) && ($data['is_active'] == 0 || $data['is_active'] == '0')){ // $data['end_time'] = date('Y-m-d H:i:s'); // } $result = AichatSession::where($where)->update($data); if (empty($result)) { return Result::error('修改会话失败!'); } return Result::success($result); } /** * 自助建站-ai会话-删除会话 */ public function delAiSession(array $data): array { $where['user_id'] = $data['user_id']; $where['session_id'] = $data['session_id']; // 删除会话 Db::beginTransaction(); try { $aichat_session = AichatSession::where($where)->get()->all(); if (!empty($aichat_session)) { $result['ai_chat_session'] = AichatSession::where($where)->delete(); if (empty($result['ai_chat_session'])) { Db::rollBack(); return Result::error('删除会话消息失败!'); } } // 删除会话消息 $aichat = Aichat::where($where)->get()->all(); if (!empty($aichat)) { $result['ai_chat_message'] = Aichat::where($where)->delete(); if (empty($result['ai_chat_message'])) { Db::rollBack(); return Result::error('删除会话消息失败!'); } } // 删除草稿模板 $template_draftbox = TemplateDraftbox::where($where)->get()->all(); if (!empty($template_draftbox)) { $result['template_draftbox'] = TemplateDraftbox::where($where)->delete(); if (empty($result['template_draftbox'])) { Db::rollBack(); return Result::error('删除草稿模板失败!'); } } Db::commit(); return Result::success($result); } catch (\Exception $e) { Db::rollBack(); return Result::error($e->getMessage()); } } /** * 自助建站-ai会话-发送消息 */ public function sendAiMessage(array $data): array { $where['user_id'] = $data['user_id']; $where['session_id'] = $data['session_id']; // $where['is_active'] = 1; $aichat_session = AichatSession::where($where)->first(); if (empty($aichat_session)) { return Result::error('会话不存在!'); } Db::beginTransaction(); try { $data['message_type'] = empty($data['message_type']) ? 'text' : $data['message_type']; $result['aichat'] = Aichat::insertGetId($data); if (empty($result)) { Db::rollBack(); return Result::error('发送消息失败!'); } $result['aichat_session'] = AichatSession::where($where)->update(['end_time' => date('Y-m-d H:i:s')]); if (empty($result['aichat_session'])) { Db::rollBack(); return Result::error('更新会话截止时间失败!'); } Db::commit(); return Result::success($result); } catch (\Exception $e) { Db::rollBack(); return Result::error($e->getMessage()); } } /** * 自助建站-ai会话-获取消息列表 */ public function getAiMessageList(array $data): array { // $where['user_id'] = $data['user_id']; $where['session_id'] = $data['session_id']; // $where['is_active'] = 1; $session = AichatSession::where($where)->first(); if (empty($session)) { return Result::error('会话不存在!'); } $aichat = Aichat::where('user_id', $data['user_id']) ->where('session_id', $data['session_id']) ->orderBy('send_time') ->get()->all(); if (empty($aichat)) { return Result::error('消息不存在!'); } return Result::success($aichat); } /** * 自助建站-ai会话-添加模板草稿 */ public function addTemplateDraftbox(array $data): array { $where['session_id'] = $data['session_id']; // $where['user_id'] = $data['user_id']; // 后续若是用来对应会话记录,则此项需要修改 $session = AichatSession::where($where)->first(); if (empty($session)) { return Result::error('会话不存在!'); } $data['website_id'] = $session['website_id']; // $data['template_id'] = $session['template_id']; $template_draftbox = TemplateDraftbox::where($where)->first(); if (!empty($template_draftbox)) { $result = TemplateDraftbox::where($where)->update(['template_data' => $data['template_data'], 'canvas_data' => $data['canvas_data'], 'template_id' => $data['template_id']]); } else { $result = TemplateDraftbox::insert($data); } if (empty($result)) { return Result::error('添加草稿模板失败!'); } return Result::success($result); } /** * 自助建站-ai会话-修改模板草稿 */ public function upTemplateDraftbox(array $data): array { $where['session_id'] = $data['session_id']; $where['user_id'] = $data['user_id']; $session = AichatSession::where($where)->first(); if (empty($session)) { return Result::error('会话不存在!'); } if (isset($data['chat_id']) && !empty($data['chat_id'])) { $chat = Aichat::where($where)->where('chat_id', $data['chat_id'])->first(); if (empty($chat)) { return Result::error('消息不存在!'); } } // 后续若是用来对应会话记录,则此项需要修改 $template_draftbox = TemplateDraftbox::where($where)->first(); if (empty($template_draftbox)) { return Result::error('草稿模板不存在!'); } unset($data['session_id']); $result = TemplateDraftbox::where($where)->update($data); if (empty($result)) { return Result::error('修改草稿模板失败!'); } return Result::success($result); } /** * 自助建站-ai会话-获取模板草稿列表 */ public function getTemplateDraftboxList(array $data): array { $where['session_id'] = $data['session_id']; // $where['user_id'] = $data['user_id']; if (isset($data['website_id']) && !empty($data['website_id'])) { $where['website_id'] = $data['website_id']; } if (isset($data['template_id']) && !empty($data['template_id'])) { $where['template_id'] = $data['template_id']; } if (isset($data['status'])) { $where['status'] = $data['status']; } if (isset($data['chat_id']) && !empty($data['chat_id'])) { $where['chat_id'] = $data['chat_id']; } $template_draftbox = TemplateDraftbox::where($where)->first(); if (empty($template_draftbox)) { return Result::error('草稿模板不存在!'); } return Result::success($template_draftbox); } /** * 自助建站-验证关键词填写重复问题 */ public function checkRepeatKeyword(array $data): array { if (empty($data['keyword']) || $data['keyword'] == '' || $data['keyword'] == '[]') { return Result::success('关键词为空'); } $arr_keyword = json_decode(stripslashes($data['keyword']), true) ?: []; if (is_array($arr_keyword) && count($arr_keyword) > 0) { $keyword = array_map('trim', $arr_keyword); if (count($keyword) != count(array_unique($keyword))) { return Result::error('关键词不能重复!'); } } $where['id'] = $data['id'] ?? null; if ($data['type'] == 'style') { $type = '风格'; $all_keywords = TemplateClass::when(!empty($where), function ($query) use ($where) { $query->where('id', '!=', $where['id']); })->pluck('keyword')->toArray(); } if ($data['type'] == 'skin') { $type = '皮肤'; $all_keywords = Template::when(!empty($where), function ($query) use ($where) { $query->where('id', '!=', $where['id']); })->pluck('template_keyword')->toArray(); } if ($data['type'] == 'sector') { $type = '通栏'; $all_keywords = Sector::when(!empty($where), function ($query) use ($where) { $query->where('id', '!=', $where['id']); })->where('template_id', $data['template_id'])->pluck('sector_keyword')->toArray(); } if ($data['type'] == 'component') { $type = '组件'; $all_keywords = Component::when(!empty($where), function ($query) use ($where) { $query->where('id', '!=', $where['id']); })->pluck('component_keyword')->toArray(); } foreach ($all_keywords as $key => $value) { $value_keyword = json_decode($value ?? '[]', true); if (is_array($value_keyword) && count($value_keyword) > 0) { $repeat_keywords = array_intersect($arr_keyword, $value_keyword); if (count($repeat_keywords) == count($keyword) && count($repeat_keywords) == count($value_keyword)) { return Result::error('此' . $type . '关键词已存在!'); } } } return Result::success(['验证通过']); } /** * 自助建站-验证网站模板相关类型 * 版本1----在构建网站首页之后使用 */ public function checkWebTemplate(array $data): array { // 网站id (是否存在该网站) // (若是只传这个字段会判断是否关联导航池;) // (再判断是否有三种类型的友情链接) // (头部信息是否填写) // (两种类型的底部导航) $website = Website::where('id', $data['website_id'])->first(); if (empty($website)) { return Result::error('网站不存在!'); } $template_data = json_decode($data['template_data'], true); if (empty($template_data)) { return Result::error('模板数据不能为空!'); } return Result::success($template_data); if (!isset($template_data['website_id']) || empty($template_data['website_id'])) { return Result::error('网站ID不能为空!'); } if (!isset($template_data['template_id']) || empty($template_data['template_id'])) { return Result::error('模板ID不能为空!'); } return Result::success($template_data); } public function getAITemplate(array $data): array { $result['template'] = Template::select( 'id', 'template_id', 'template_name', 'template_keyword', 'page_type', 'template_class_id', 'template_img', 'updated_at' ) ->get()->all(); if (empty($result)) { return Result::error("暂无皮肤", 0); } // $num = 1; // 确保 $result 是数组且包含数据 if (!is_array($result) || empty($result)) { return Result::error('模板数据异常'); } // 初始化最新更新时间 $latestUpdate = null; foreach ($result['template'] as $key => $value) { $templateImg = json_decode($value['template_img'], true); $result['template'][$key]['template_img'] = $templateImg; // 更新最新时间:取最大的 updated_at if ($latestUpdate === null || $value['updated_at'] > $latestUpdate) { $latestUpdate = $value['updated_at']; } // unset($result[$key]['updated_at']); } // 将最新时间放入返回结果,若无数据则返回当前时间 $result['update_time'] = date('Y-m-d H:i:s', strtotime($latestUpdate)); return Result::success($result); } /** * 自助建站-ai会话-删除聊天记录 */ public function delAiChatRecord(array $data): array { $session_id = $data['session_id']; $user_id = $data['user_id']; $chat = AiChat::where('session_id', $session_id)->where('user_id', $user_id)->orderBy('send_time', 'desc')->first(); if (empty($chat)) { return Result::error('此用户的聊天记录不存在!'); } $del_chat = AiChat::where('chat_id', $chat['chat_id'])->where('user_id', $user_id)->delete(); if (empty($del_chat)) { return Result::error('删除失败!'); } return Result::success('删除成功' . $chat['chat_id']); } /** * 自助建站-ai会话-给模板添加导航 */ public function addTemplateCates(array $data): array { $website_template = TemplateDraftbox::where('session_id', $data['session_id'])->first(); if (empty($website_template)) { return Result::error('此网站模板不存在!'); } // return Result::success($website_template); $template_data = json_decode($website_template['template_data'], true); $template_index = $template_data['template']['index']; if (empty($template_index)) { return Result::error('模板数据不能为空!'); } $canvas_data = json_decode($website_template['canvas_data'], true); $canvas_index = $canvas_data['template']['index']; if (empty($canvas_index)) { return Result::error('画布数据不能为空!'); } $cate_column = [ 'placeid' => 1, 'pid' => 0, 'num' => 23, 'website_id' => $website_template['website_id'], 'is_show' => 1, ]; $websiteServer = new \App\JsonRpc\WebsiteService(); $all_cate_data = $websiteServer->getWebsiteModelCategory($cate_column); $all_cate = $all_cate_data['data'] ?? []; // return Result::success($all_cate); if (count($all_cate) == 0) { return Result::error('栏目不存在!'); } // 将 Collection 转为数组 $all_cate = $all_cate->toArray(); $del_catid = 0; foreach ($all_cate as $k => $v) { $cat_arr_id = json_decode($v['category_arr_id'], true) ?? []; if (!empty($v['web_url']) || $v['type'] != 1 || $v['pid'] == $del_catid) { $del_catid = $v['category_id'] ?? ''; unset($all_cate[$k]); } } $all_cate = array_values($all_cate); // return Result::success($all_cate); $componentList = array_column($template_index, 'componentList'); $componentData = []; $cat_key = 0; foreach ($componentList as $key => $item) { $componentList[$key] = $item ?? []; if (count($item) == 1 && isset($item[0]['componentData']['level']) && (empty($v['componentData']['level']) || $v['componentData']['level'] == "")) { // if(isset($item[0]) && !empty($item[0])){ $componentList[$key][0]['componentData']['name'] = $all_cate[$cat_key]['alias'] ?? ''; $componentList[$key][0]['componentData']['category_arr'] = json_decode($all_cate[$cat_key]['category_arr_id'], true) ?? ''; $componentList[$key][0]['componentData']['category_id'] = $all_cate[$cat_key]['category_id'] ?? ''; $cat_key++; $template_index[$key]['componentList'] = $componentList[$key] ?? []; $canvas_index[$key]['content']['componentList'] = $componentList[$key] ?? []; // } } else { foreach ($item as $k => $v) { $componentList[$key][$k] = $v ?? []; if (isset($v['componentData']['level']) && (empty($v['componentData']['level']) || $v['componentData']['level'] == "")) { $componentList[$key][$k]['componentData']['name'] = $all_cate[$cat_key]['alias'] ?? ''; $componentList[$key][$k]['componentData']['category_arr'] = json_decode($all_cate[$cat_key]['category_arr_id'], true) ?? ''; $componentList[$key][$k]['componentData']['category_id'] = $all_cate[$cat_key]['category_id'] ?? ''; $cat_key++; $template_index[$key]['componentList'][$k] = $componentList[$key][$k] ?? []; $canvas_index[$key]['content']['componentList'][$k] = $componentList[$key][$k] ?? []; } } } } // 从 componentList[0] 中取出 componentData 字段 $template_data['template']['index'] = $template_index; $canvas_data['template']['index'] = $canvas_index; // $componentData = $componentList[0] ?? []; $result = TemplateDraftbox::where('session_id', $data['session_id'])->update([ 'template_data' => json_encode($template_data, JSON_UNESCAPED_UNICODE), 'canvas_data' => json_encode($canvas_data, JSON_UNESCAPED_UNICODE), ]); if (empty($result)) { return Result::error('更新失败!'); } return Result::success($result); } /** * @param array $data * @return array */ public function public_server(array $data): array { try { // 这里添加public_server方法的业务逻辑 // 由于没有找到原始实现,暂时返回一个基本的成功响应 return Result::success($data); } catch (\Exception $e) { return Result::error($e->getMessage(), 0); } } }