limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->orderBy("code", "asc")->get(); $count = District::where($where)->count(); $result = [ 'rows' => $rep, 'count' => $count ]; } else { $result = District::where($data)->orderBy("code", "asc")->get(); } return $result ? Result::success($result) : Result::error("没有查到数据"); } /** * @param array $data * @return array */ public function getUserLevelList(array $data): array { $where = []; if (isset($data['keyWord'])) { $where = [ ['name', 'like', '%' . $data['keyWord'] . '%'], ]; } $result = []; if (isset($data['pageSize'])) { $rep = UserLevel::where($where)->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->orderBy("id", "asc")->get(); $count = UserLevel::where($where)->count(); $result = [ 'rows' => $rep, 'count' => $count, ]; } else { $result = UserLevel::orderBy("id", "asc")->get(); } return $result ? Result::success($result) : Result::error("没有查到数据"); } /** * 添加用户等级 * @param array $data * @return array */ public function addUserLevel(array $data): array { LevelUser::insertGetId($data); return Result::success([]); } /** * 更新等级 * @param array $data * @return array */ public function updateUserLevel(array $data): array { $result = LevelUser::where(['id' => $data['id']])->update($data); if ($result) { return Result::success($result); } return Result::error("更新失败"); } /** * 删除等级 * @param array $data * @return array */ public function delUserLevel(array $data): array { $result = LevelUser::where(['id' => $data['id']])->delete(); if ($result) { return Result::success($result); } return Result::error("删除失败"); } /** * 查询投诉举报信息 * @param array $data * @return array */ public function getLetterOfComplaint(array $data = []): array { var_dump("===="); $where = []; if (isset($data['user_id']) && !empty($data['user_id'])) { array_push($where, ['letter_of_complaint.user_id', '=', $data['user_id']]); } if (isset($data['nature']) && !empty($data['nature'])) { array_push($where, ['letter_of_complaint.nature', '=', $data['nature']]); } if (isset($data['nature_level0']) && !empty($data['nature_level0'])) { array_push($where, ['letter_of_complaint.nature_level0', '=', $data['nature_level0']]); } if (isset($data['status']) && !empty($data['status'])) { array_push($where, ['letter_of_complaint.status', '=', $data['status']]); } $result = []; if (isset($data['pageSize'])) { $rep = LetterOfComplaint::where($where) ->leftJoin("letter_type as type_a", "letter_of_complaint.nature", "type_a.id") ->leftJoin("letter_type as type_c", "letter_of_complaint.nature_level0", "type_c.id") ->leftJoin("letter_type as type_b", "letter_of_complaint.nature_level1", "type_b.id") ->leftJoin("letter_type as type_e", "letter_of_complaint.nature_level3", "type_e.id") ->leftJoin("letter_type as type_d", "letter_of_complaint.status", "type_d.id") ->select( "letter_of_complaint.*", "type_a.type_name as nature_name", "type_b.type_name as nature_name1", "type_c.type_name as nature_name0", "type_d.type_name as status_name", "type_e.type_name as nature_name3" ) ->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->orderBy("letter_of_complaint.id", "desc")->get(); $count = LetterOfComplaint::where($where)->count(); if ($rep) { foreach ($rep as $val) { if ($val['judgment']) { $val['judgment'] = json_decode($val['judgment']); } if ($val['audio_and_video']) { $val['audio_and_video'] = json_decode($val['audio_and_video']); } if ($val['contract']) { $val['contract'] = json_decode($val['contract']); } if ($val['qualifications']) { $val['qualifications'] = json_decode($val['qualifications']); } } } $result = [ 'rows' => $rep, 'count' => $count, ]; } else { $result = LetterOfComplaint::where($where) ->leftJoin("letter_type as type_a", "letter_of_complaint.nature", "type_a.id") ->leftJoin("letter_type as type_c", "letter_of_complaint.nature_level0", "type_c.id") ->leftJoin("letter_type as type_b", "letter_of_complaint.nature_level1", "type_b.id") ->leftJoin("letter_type as type_e", "letter_of_complaint.nature_level3", "type_e.id") ->leftJoin("letter_type as type_d", "letter_of_complaint.status", "type_d.id") ->select( "letter_of_complaint.*", "type_a.type_name as nature_name", "type_b.type_name as nature_name1", "type_c.type_name as nature_name0", "type_d.type_name as status_name", "type_e.type_name as nature_name3" ) ->orderBy("letter_of_complaint.id", "desc")->get(); } return $result ? Result::success($result) : Result::error("没有查到数据"); } /** * 添加投诉举报信息 * @param array $data * @return array */ public function addLetterOfComplaint(array $data): array { $data['judgment'] = $data['judgment'] ? json_encode($data['judgment']) : ''; $data['audio_and_video'] = $data['audio_and_video'] ? json_encode($data['audio_and_video']) : ''; $data['contract'] = $data['contract'] ? json_encode($data['contract']) : ''; $data['qualifications'] = $data['qualifications'] ? json_encode($data['qualifications']) : ''; unset($data['id']); $result = LetterOfComplaint::insertGetId($data); if (empty($result)) { return Result::error("创建失败", 0); } else { return Result::success(["id" => $result]); } } /** * 用户端更新投诉举报 * @param array $data * @return array */ public function userUpLetterOfComplaint(array $data): array { $data['judgment'] = $data['judgment'] ? json_encode($data['judgment']) : ''; $data['audio_and_video'] = $data['audio_and_video'] ? json_encode($data['audio_and_video']) : ''; $data['contract'] = $data['contract'] ? json_encode($data['contract']) : ''; $data['qualifications'] = $data['qualifications'] ? json_encode($data['qualifications']) : ''; $result = LetterOfComplaint::where(['id' => $data['id']])->update($data); if (empty($result)) { return Result::error("创建失败", 0); } else { return Result::success(["id" => $result]); } } /** * 管理后台更新投诉举报信息 * @param array $data * @return array */ public function upLetterOfComplaint(array $data): array { var_dump("admin:", $data); $where = [ 'id' => $data['id'], ]; $filtered_array = array_filter($data, function ($value) { return $value !== "" && $value !== null && $value !== false && !is_array($value) || !empty($value); }); $filtered_array['judgment'] = isset($filtered_array['judgment']) ? json_encode($filtered_array['judgment']) : ''; $filtered_array['audio_and_video'] = isset($filtered_array['audio_and_video']) ? json_encode($filtered_array['audio_and_video']) : ''; $filtered_array['contract'] = isset($filtered_array['contract']) ? json_encode($filtered_array['contract']) : ''; $filtered_array['qualifications'] = isset($filtered_array['qualifications']) ? json_encode($filtered_array['qualifications']) : ''; unset($filtered_array['nature_name']); unset($filtered_array['type_name']); unset($filtered_array['nature_level_name']); unset($filtered_array['status_name']); unset($filtered_array['is_admin']); unset($filtered_array['type_level_name']); $result = LetterOfComplaint::where($where)->update($filtered_array); if ($result) { return Result::success($result); } return Result::error("更新失败", 0); } /** * 查询投诉举报记录 * @param array $data * @return array */ public function getLetterOfComplaintInfo(array $data): array { $where = [ 'letter_of_complaint.id' => $data['id'], ]; if (isset($data['user_id']) && !empty($data['user_id'])) { array_push($where, ['letter_of_complaint.user_id', '=', $data['user_id']]); } $result = LetterOfComplaint::where($where) ->leftJoin("letter_type as type_a", "letter_of_complaint.nature", "type_a.id") ->leftJoin("letter_type as type_c", "letter_of_complaint.nature_level0", "type_c.id") ->leftJoin("letter_type as type_b", "letter_of_complaint.nature_level1", "type_b.id") ->leftJoin("letter_type as type_e", "letter_of_complaint.nature_level3", "type_e.id") ->leftJoin("letter_type as type_d", "letter_of_complaint.status", "type_d.id") ->select( "letter_of_complaint.*", "type_a.type_name as nature_name", "type_b.type_name as nature_name1", "type_c.type_name as nature_name0", "type_d.type_name as status_name", "type_e.type_name as nature_name3" ) ->first(); return Result::success($result); } /** * 删除投诉举报信息 * @param array $data * @return array */ public function delLetterOfComplaint(array $data): array { $result = LetterOfComplaint::when($data, function ($query) use ($data) { if (isset($data['id']) && !empty($data['id'])) { $query->where(['id' => $data['id']]); } if (isset($data['user_id']) && !empty($data['user_id'])) { $query->where(['user_id' => $data['user_id']]); } })->delete(); if (empty($result)) { return Result::error("删除失败", 0); } else { return Result::success(); } } /** * 获取举报信息类型 * @param array $data * @return array */ public function getLetterType(array $data): array { $where = []; if (isset($data['type'])) { array_push($where, ['type', '=', $data['type']]); } if (isset($data['pid']) && $data['pid'] > 0) { array_push($where, ['pid', '=', $data['pid']]); } $result = LetterType::where($where)->orderBy('sort', 'asc')->get(); return $result ? Result::success($result) : Result::error("没有查到数据"); } /** * 更新举报类型 * @param array $data * @return array */ public function upLetterType(array $data): array { return []; } /** * 添加举报类型 * @param array $data * @return array */ public function addLetterType(array $data): array { $result = LetterType::insertGetId($data); if (empty($result)) { return Result::error("创建失败", 0); } else { return Result::success(["id" => $result]); } } /** * 删除举报类型 * @param array $data * @return array */ public function delLetterType(array $data): array { $result = LetterType::where('id', $data['id'])->delete(); if (empty($result)) { return Result::error("删除失败", 0); } else { return Result::success(); } } /** * 检测是否已经被接案 * @param array $data * @return array */ public function checkMeasure(array $data): array { $where = [ 'id' => $data['id'], ]; $letterOfComplaintInfo = LetterOfComplaint::where($where)->first(); var_dump("查询数据:", $letterOfComplaintInfo['admin_id'], $data['user_id']); //操作人和当前登陆用户id 相等说明是当前人接收的案件 if (($letterOfComplaintInfo['admin_id'] == $data['user_id']) || empty($letterOfComplaintInfo['admin_id'])) { return Result::success(); } else { return Result::error("您不能处理其他人已经接过的案件", 0); } } /** * 后台获取职能部门 * @param array $data * @return array */ public function getZhinengbumenList(array $data): array { // 获取分页参数,默认每页 10 条记录 $page = isset($data['page']) ? (int) $data['page'] : 1; $perPage = isset($data['pagesize']) ? (int) $data['pagesize'] : 10; // 查询数据并分页 $query = Department::query(); // 可以在这里添加更多的查询条件 if (isset($data['search'])) { $query->where('name', 'like', '%' . $data['search'] . '%'); } // 执行分页查询 $result = $query->paginate($perPage, ['*'], 'page', $page); // 返回分页结果 return Result::success([ 'count' => $result->total(), 'current_page' => $result->currentPage(), 'last_page' => $result->lastPage(), 'pagesize' => $result->perPage(), 'rows' => $result->items(), ]); } /** * 添加获取职能部门 * @param array $data * @return array */ public function addZhinengbumen(array $data): array { $result = Department::insertGetId($data); if (empty($result)) { return Result::error("创建失败", 0); } else { return Result::success(["id" => $result]); } } public function delZhinengbumen(array $data): array { $result = Department::where('id', $data['id'])->delete(); if (empty($result)) { return Result::error("删除失败", 0); } else { return Result::success(); } } public function getZhinengbumen(array $data): array { $result = Department::where('id', $data['id']) ->orderBy('department', 'desc') ->first(); return Result::success($result); } public function getPidZhinengbumen(array $data): array { if (empty($data['pid'])) { $data['pid'] = 0; } $result = Department::where('pid', $data['pid'])->get(); return Result::success($result); } public function modZhinengbumen(array $data): array { $result = Department::where('id', $data['id'])->update($data); if (empty($result)) { return Result::error("修改失败", 0); } else { return Result::success(); } } /** * 查询职能列表 * @param array $data * @return array */ public function getDepartment(array $data): array { $where = []; if (isset($data['pid'])) { $where = [ 'pid' => $data['pid'] ?? 0 ]; } $result = Department::when(!empty($where), function ($query) use ($where) { $query->where($where); })->orderBy("sort", "desc")->get(); if (empty($result)) { return Result::error("查询失败", 0); } else { return Result::success($result); } } /** * 获取所有的buckets * @param array $data * @return array */ public function getBuckets(array $data): array { $result = new MinioService(); // 调用服务层的方法获取存储桶列表 $bucketsResponse = $result->listBuckets(); // 直接返回服务层生成的响应 return Result::success($bucketsResponse['data']); } /** * 上传文件 * @param array $data * @return array */ public function uploadFile(array $data): array { $result = new MinioService(); $rep = $result->uploadFile($data); if ($rep['code'] == 200) { return Result::success($rep['data']); } else { return Result::error("上传失败!"); } } /** * 黑名单管理 * @param array $data * @return array */ public function getBlackWordList(array $data): array { $result = BlackWord::when($data, function ($query) use ($data) { if (isset($data['name']) && $data['name']) { $query->where('black_word.name', 'like', '%' . $data['name'] . '%'); } })->orderBy('black_word.id', 'desc') ->paginate( intval($data['pageSize']), [ 'black_word.*', ], 'page', intval($data['page']) ); $count = $result->total(); $returnData = [ 'rows' => $result->items(), 'count' => $count ]; return Result::success($returnData); } /** * 添加黑名单 * @param array $data * @return array */ public function addBlackWord(array $data): array { Db::beginTransaction(); try { $info = BlackWord::where(['name' => $data['name']])->first(); if ($info) { Db::rollBack(); return Result::error("该黑名单已存在", 0); } $data['type'] = 10; $result = BlackWord::insertGetId($data); $redisKey = 'black_word'; $this->redis->sAdd($redisKey, $data['name']); Db::commit(); return Result::success(["id" => $result]); } catch (\Exception $e) { Db::rollBack(); return Result::error("创建失败" . $e->getMessage(), 0); } } /** * 删除黑名单 * @param array $data * @return array */ public function delBlackWord(array $data): array { Db::beginTransaction(); try { BlackWord::where(['name' => $data['name']])->delete(); $redisKey = 'black_word'; $this->redis->sRem($redisKey, $data['name']); Db::commit(); return Result::success([]); } catch (\Exception $e) { Db::rollBack(); return Result::error("删除失败" . $e->getMessage(), 0); } } /** * 修改违禁词 * @param array $data * @return array */ public function upBlackWord(array $data): array { Db::beginTransaction(); try { $checkInfo = BlackWord::where(['name' => $data['name']])->first(); if ($checkInfo) { Db::rollBack(); return Result::error("该违禁词已经存在", 0); } $blackWordInfo = BlackWord::where(['id' => $data['id']])->first(); if ($blackWordInfo) { //先删除redis $blackWordInfo = $blackWordInfo->toArray(); $redisKey = 'black_word'; $this->redis->sRem($redisKey, $blackWordInfo['name']); $this->redis->sAdd($redisKey, $data['name']); BlackWord::where(['id' => $data['id']])->update(['name' => $data['name']]); Db::commit(); return Result::success([]); } else { Db::rollBack(); return Result::error("系统错误", 0); } } catch (\Exception $e) { Db::rollBack(); return Result::error("修改失败" . $e->getMessage(), 0); } } /** * 获取风格 * @return void */ public function getTemplateClassList(array $data): array { $where = []; if (isset($data['name']) && $data['name']) { array_push($where, ['template_class.name', 'like', '%' . $data['name'] . '%']); } if (isset($data['keyword']) && $data['keyword']) { array_push($where, ['template_class.keyword', 'like', '%' . $data['keyword'] . '%']); } $template = TemplateClass::when($where, function ($query) use ($where) { $query->where($where); }); $count = $template->count(); // $countQuery = clone $template; $row = $template ->leftJoin('template', function ($join) { $join->on('template_class.class_id', '=', 'template.template_class_id'); }) ->select('template_class.*', DB::raw('COUNT(template.template_class_id) as template_count')) ->groupBy('template_class.id') ->orderBy('template_class.id', 'desc') ->offset(($data['page'] - 1) * $data['pageSize']) ->limit($data['pageSize']) ->get(); $result = [ 'rows' => $row, 'count' => $count, ]; if ($row->isEmpty()) { return Result::error("暂无风格", 0); } else { return Result::success($result); } } /** * 添加风格 * @param * @return void */ public function addTemplateClass(array $data): array { $data['keyword'] = json_encode($data['keyword']); $template_class = TemplateClass::where('name', $data['name']) ->orWhere('class_id', $data['class_id']) ->first(); if ($template_class) { return Result::error("风格名称或者风格编号已存在,不可添加!", 0); } $result = TemplateClass::insertGetId($data); if (empty($result)) { return Result::error("创建风格失败", 0); } else { return Result::success(["id" => $result]); } } /** * 更新风格 * @param array $data * @return array */ public function upTemplateClass(array $data): array { $where = [ 'id' => $data['id'], ]; $template_class = TemplateClass::where($where)->first(); if (empty($template_class)) { return Result::error("未查询到风格", 0); } if ($template_class->type == 1) { return Result::error("默认风格不能修改", 0); } $template = TemplateClass::where('id', '!=', $data['id']) // ->where(['name' => $data['name']]) // ->orWhere(['class_id' => $data['class_id']]) ->where(function ($query) use ($data) { $query->where('name', $data['name']) ->orWhere('class_id', $data['class_id']); }) ->first(); if ($template) { return Result::error("风格名称或者风格编号已存在,不可编辑!", 0); } $updateData = [ 'name' => $data['name'], 'keyword' => json_encode($data['keyword']), 'class_id' => $data['class_id'], ]; $result = TemplateClass::where($where)->update($updateData); if (empty($result)) { return Result::error("更新失败", 0); } else { return Result::success($result); } } /** * 删除风格 * @param array $data * @return array */ public function delTemplateClass(array $data): array { $where = [ 'id' => $data['id'], ]; $template_class = TemplateClass::where($where)->first(); if (empty($template_class)) { return Result::error("未查询到风格", 0); } if ($template_class->type == 1) { return Result::error("默认风格不能删除", 0); } $template = Template::where('template_class_id', $template_class['class_id'])->get(); if (!empty($template->all())) { try { // 默认风格 1 Db::beginTransaction(); $template = Template::where('template_class_id', $template['class_id'])->update(['template_class_id' => 1]); if (empty($template)) { Db::rollBack(); return Result::error("删除失败", 0); } $result = TemplateClass::where($where)->delete(); } catch (\Exception $e) { return Result::error("删除失败" . $e->getMessage(), 0); } } else { $result = TemplateClass::where('id', $data['id'])->delete(); } if (empty($result)) { return Result::error("删除失败", 0); } return Result::success($result); } /** * 根据风格名称-获取获取所有风格 * @param array $data * @return array */ public function getTemplateClass(array $data): array { $where = []; if (!empty($data['template_name'])) { $where['name'] = $data['template_name']; } $result = TemplateClass::where($where)->get(); if (empty($result)) { return Result::error("暂无风格", 0); } return Result::success($result); } /** * 获取皮肤列表 * @param array $data * @return array */ public function getTemplateList(array $data): array { $where = []; if (!empty($data['template_class_id'])) { $where['template_class_id'] = $data['template_class_id']; } if (!empty($data['template_name'])) { array_push($where, ['template_name', 'like', '%' . $data['template_name'] . '%']); } if (!empty($data['template_keyword'])) { array_push($where, ['template_keyword', 'like', '%' . $data['template_keyword'] . '%']); } $result = Template::where($where) ->leftJoin('template_class', 'template.template_class_id', 'template_class.class_id') ->select('template.*', 'template_class.name as template_class_name') ->orderBy('template.template_id', 'desc') ->paginate($data['page_size'], ['*'], 'mypage_name', $data['page']); if (empty($result)) { return Result::error("暂无皮肤", 0); } return Result::success($result); } public function getTemplateInfo(array $data): array { $result = Template::where('template.id', $data['id']) ->leftJoin('template_class', 'template.template_class_id', 'template_class.class_id') ->select('template.*', 'template_class.name as template_class_name') ->first(); if (empty($result)) { return Result::error("暂无皮肤", 0); } return Result::success($result); } /** * 添加皮肤 * @param array $data * @return array */ public function addTemplate(array $data): array { var_dump($data); unset($data['user_id']); $template = Template::where('template_name', $data['template_name']) ->orWhere('template_id', $data['template_id']) ->first(); if ($template) { return Result::error("皮肤名称或者皮肤编号已存在,不可添加!", 0); } $data['template_keyword'] = 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') ) ->groupBy('component.id') // 按组件ID分组,确保统计每个组件的图片数量 ->orderBy('component.updated_at', 'desc') ->paginate($data['page_size'], ['*'], 'mypage_name', $data['page']); if(empty($result)){ return Result::error('暂无数据'); } return Result::success($result); } public function getComponentInfo(array $data): array { $where = ['component.id'=>$data['id']]; $result = Component::where($where) ->leftJoin('component_type','component_type.id','=','component.type_id') ->select( 'component.*','component_type.com_typename') ->get(); if(empty($result)){ return Result::error('暂无数据'); } return Result::success($result); } /* 新增组件 */ public function addComponent(array $data): array { unset($data['user_id']); $component_id = Component::where('component_type',$data['component_type'])->first(); if(!empty($component_id)){ return Result::error('组件编号已存在!'); } $component_type = ComponentType::where('id',$data['type_id'])->first(); if(empty($component_type)){ return Result::error('组件分类不存在!'); } // '组件分类:1:资讯-头条组件;2:资讯-轮播组件;3:资讯-推荐图类组件;4:资讯-最新类组件;5:资讯-推荐类;6:资讯-热点类组件;7:资讯-栏目类组件; // 8:列表类组件;9:详情类组件;10:二级导航栏类组件;11:广告类;12:静态资源类;13:底部导航类;14:广告资讯混合类 $data['type_id'] = intval($data['type_id']); $add_arr = [ // 'template_id' => intval($data['template_id']), 'component_type' => intval($data['component_type']), 'component_name' => $data['component_name'], 'component_img' => $data['component_img'], 'component_width' => $data['component_width'], 'component_height' => $data['component_height'], 'type_id' => intval($data['type_id']), 'component_keyword' => $data['component_keyword'], ]; $component_head_code = [ 'component_id' => $add_arr['component_type'], 'component_type' => $add_arr['type_id'], 'component_name' => $add_arr['component_name'], 'component_style' => 1, ]; $add_arr['component_code'] = json_encode($component_head_code, true); $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:资讯-热点类组件; $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----带广告的特殊处理(广告位默认图上传静态资源的处理---已废弃) // 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']); // $add_arr['ad_img'] = $data['ad_img']; $ad = [ 'width' => $add_arr['ad_width'], 'height' => $add_arr['ad_height'], 'name' => '', 'price' => '', 'introduce' => '', 'website_id' => '', 'thumb' => $add_arr['ad_img'], // 'typeid' => $add_arr['ad_type'], 'ad_tag' => '', ]; $add_arr['ad'] = json_encode($ad, true); $add_arr['component_column'] = $component_type['com_code'] ?? ''; // $add_arr['adimg_info'] = $data['img_info']; // $ad_imginfo = json_decode($data['img_info'],true); break; // case 13: //13:底部导航类; case 17: //17:单页列表类; case 18: //18:单页详情类; $component_data['componentData'] = [ 'fcat_id' => '', ]; $add_arr['component_data'] = json_encode($component_data,true); $add_arr['component_column'] = $component_type['com_code'] ?? ''; break; case 14: $add_arr['ad_width'] = intval($data['ad_width']); $add_arr['ad_height'] = intval($data['ad_height']); $add_arr['component_width'] = intval($data['component_width']); $add_arr['component_height'] = intval($data['component_height']); $add_arr['ad_type'] = intval($data['ad_type']); $add_arr['ad_img'] = $data['ad_img']; $add_arr['img_num'] = intval($data['img_num']); $add_arr['text_num'] = intval($data['text_num']); $add_arr['child_imgnum'] = intval($data['child_imgnum'] ?? null); $add_arr['child_textnum'] = intval($data['child_textnum'] ?? null); $component_data['componentData'] = [ 'category_id' => '', 'level' => '', 'name' => '', 'category_arr' => [], 'imgSize' => $data['img_num'] ?? '', 'textSize' => $data['text_num'] ?? '', 'child' => [ 'id' => '', 'imgSize' => $data['child_imgnum'] ?? '', 'textSize' => $data['child_textnum'] ?? '', ] ]; $ad = [ 'width' => $add_arr['ad_width'], 'height' => $add_arr['ad_height'], 'name' => '', 'price' => '', 'introduce' => '', 'website_id' => '', 'thumb' => $add_arr['ad_img'], 'typeid' => $add_arr['ad_type'], 'ad_tag' => '', ]; $add_arr['ad'] = json_encode($ad,true); $add_arr['component_data'] = json_encode($component_data,true); $add_arr['component_column'] = $component_type['com_code'] ?? ''; // $add_arr['adimg_info'] = $data['img_info']; // $ad_imginfo = json_decode($data['img_info'],true); break; default: break; } // var_dump("ad_imginfo:",$ad_imginfo); $cll_column = [ 'img_num' => null, 'text_num' => null, 'child_imgnum' => null, 'child_textnum' => null, 'category_id' => null, 'child_id' => null, 'num' => null, 'level' => null, 'fcatid' => null, 'article_id' => null, 'cate_place' => null, 'pageSize' => null, // 'ad_img' => null, 'ad_width' => null, 'ad_height' => null, 'ad' => '', 'ad_type' => null, // 'ad_imgid' => null, 'adimg_info' => '[]', ]; Db::beginTransaction(); try{ // return Result::success($add_arr); // if(($component['type_id'] == 11 || $component['type_id'] == 14) && ($data['type_id'] != 11 && $data['type_id'] != 14)){ // // $website_img = WebsiteImg::where('id',$component['ad_imgid'])->delete(); // if(empty($website_img)){ // Db::rollBack(); // return Result::error('广告默认图删除失败!'); // } // var_dump("删除",$website_img); // } // if(($data['type_id'] == 11 || $data['type_id'] == 14) && ($component['type_id'] != 11 && $component['type_id'] != 14)){ // $website_img = WebsiteImg::insertGetId($ad_imginfo); // $add_arr['ad_imgid'] = $website_img; // if(empty($website_img)){ // Db::rollBack(); // return Result::error('广告默认图上传失败!'); // } // var_dump("上传",$website_img); // } // if(($component['type_id'] == 11 || $component['type_id'] == 14) && ($data['type_id'] == 11 || $data['type_id'] == 14)){ // $website_img = WebsiteImg::where('id',$component['ad_imgid'])->first(); // if(empty($website_img)){ // $website_img = WebsiteImg::insertGetId($ad_imginfo); // $add_arr['ad_imgid'] = $website_img; // }else{ // $website_img = WebsiteImg::where('id',$component['ad_imgid'])->update($ad_imginfo); // $add_arr['ad_imgid'] = $component['ad_imgid']; // } // if(empty($website_img)){ // Db::rollBack(); // return Result::error('广告默认图修改失败!'); // } // } $add_arr = array_merge($cll_column,$add_arr); $result = Component::where('id',$id)->update($add_arr); if(empty($result)){ Db::rollBack(); return Result::error('修改失败!'); } Db::commit(); }catch(\Exception $e){ Db::rollBack(); return Result::error('修改失败!'); } return Result::success($result); } public function getWebsiteTemplateInfo(array $data) { $where = []; if (isset($data['id'])) { $where[] = ['id', '=', $data['id']]; } $result = WebsiteTemplateInfo::where($where) ->leftJoin('website', 'website_template_info.website_id', '=', 'website.id') ->leftJoin('website_template', 'website_template_info.template_id', '=', 'website_template.id') ->select('website_template_info.*', 'website.website_name') ->get(); if ($result) { return Result::success($result); } else { return Result::error('暂无数据'); } } /** * 自助建站----1.获取页面类型回显 * @param array $data * @return array */ public function getWebPageType(array $data): array { // 1:图片 2:文字 3:底部 $friend_link = Link::where('website_id', $data['website_id'])->where('type', 3)->get(); if (empty($friend_link->toArray())) { return Result::error('暂无友情链接数据!'); } $footer_category = FooterCategory::where('website_id', $data['website_id'])->get(); if (empty($footer_category->toArray())) { return Result::error('暂无底部导航数据!'); } $page_type = WebsiteTemplateInfo::where('website_id', $data['website_id'])->value('page_type'); if (empty($page_type)) { $page_type = []; } else { $page_type = json_decode($page_type, true); } // '网站页面类型(存数组)1:首页 2:分类页 3:列表页 4:详情页 5:搜索页 6:特殊列表 7:特殊详情页 if (isset($page_type) && in_array(5, $page_type)) { $page_type = 1; } else { $page_type = 0; } $result = [ 'friend_link' => $friend_link, 'footer_category' => $footer_category, 'page_type' => $page_type, ]; return Result::success($result); } /** * 自助建站----2.添加页面类型 * @param array $data * @return array */ public function addWebPageType(array $data): array { // return Result::success($data); // 1:首页 2:频道页 3:列表页 4:详情页 5:搜索页 6:特殊列表 7:特殊详情页' if ($data['is_search'] == 1) { $page_type = json_encode([1,2,3,4,5,6,7]); } else { $page_type = json_encode([1,2,3,4,6,7]); } // 0:未构建;1:已填写基础信息;2:已选择模板; action_id $website_template_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first(); $website_template = WebsiteTemplate::where('website_id', $data['website_id'])->first(); Db::beginTransaction(); try{ if (empty($website_template_info)) { $result['template_info'] = WebsiteTemplateInfo::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 { $reuslt['template_info'] = WebsiteTemplateInfo::where('website_id', $data['website_id']) ->update(['template_id' => $data['template_id'],'action_id' => 2,'user_id'=> $data['user_id']]); if(empty($reuslt['template_info'])){ Db::rollBack(); return Result::error('选择皮肤失败!'); } $result['template'] = WebsiteTemplate::where('website_id', $data['website_id']) ->update(['template_id' => $data['template_id'],'user_id'=> $data['user_id']]); if(empty($result['template'])){ Db::rollBack(); return Result::error('网站模版选择皮肤失败!'); } Db::commit(); return Result::success($result); } catch (\Exception $e) { Db::rollBack(); return Result::error('选择皮肤失败!'); } } /** * 自助建站---流程---5.保存模版 * @param array $data * @return array */ public function saveWebsiteTemplate(array $data): array { // return Result::success($data); $website_template_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first(); if (empty($website_template_info) || empty($website_template_info->template_id) || empty($website_template_info->page_type)) { return Result::error('请先填写网站基础信息!'); } // 0:未构建 1:未应用 2:已应用 status if ($website_template_info['status'] == 2) { return Result::error('网站已应用,不可再次保存!'); } if ($website_template_info['action_id'] != 2) { return Result::error('请完成之前的步骤!'); } $template_info = WebsiteTemplate::where('website_id', $data['website_id'])->first(); // var_dump($data['template_data']); try { Db::beginTransaction(); if (empty($template_info)) { Db::rollback(); return Result::error('该网站不存在已保存的模版!'); } else { $template = WebsiteTemplate::where('website_id', $data['website_id'])->update( [ 'template_data' => $data['template_data'], 'user_id' => $data['user_id'], 'canvas_data' => $data['canvas_data'], ] ); } if (empty($template)) { Db::rollback(); return Result::error('保存失败!'); } else { $tempalte_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->update(['status' => 1]); if (empty($tempalte_info)) { Db::rollback(); return Result::error('网站搭建状态保存失败!'); } Db::commit(); } } catch (\Exception $e) { return Result::error('模版数据格式错误!'); } return Result::success($template); } /** * 自助建站---流程---6.返显保存的模版 * @param array $data * @return array */ public function getWebsiteTemplate(array $data): array { $web = Website::where('id', $data['website_id'])->first(); if (empty($web)) { return Result::error('未查询到网站信息!'); } $template = WebsiteTemplate::where('website_id', $data['website_id']) ->first(['website_id', 'template_id', 'page_type', 'template_data', 'canvas_data']); if (empty($template)) { return Result::error('未查询到模版!'); } $template['page_type'] = json_decode($template['page_type'],true); return Result::success($template); } /** * 通栏版式管理-获取通栏版式列表 */ public function getSectorPlaceList(array $data): array { $where['sector_place.type_id'] = $data['type_id']; if(isset($data['component_num']) && !empty($data['component_num'])){ $where['sector_place.component_num'] = $data['component_num']; } if(isset($data['sector_type']) && !empty($data['sector_type'])){ $where['sector_place.sector_type'] = $data['sector_type']; } // if(isset($data['component_type']) && !empty($data['component_type'])){ // $where['sector_place.component_type'] = $data['component_type']; // } if(isset($data['name']) && !empty($data['name'])){ array_push($where,['sector_place.name', 'like', '%'.$data['name'].'%']); } if (isset($data['width']) && !empty($data['width']) && isset($data['height']) && !empty($data['height'])){ array_push($where,['sector_place.width','like','%'.$data['width'].'%']); array_push($where,['sector_place.height','like','%'.$data['height'].'%']); } else if( isset($data['width']) && !empty($data['width'])){ array_push($where,['sector_place.width','like','%'.$data['width'].'%']); }else if( isset($data['height']) && !empty($data['height'])){ array_push($where,['sector_place.height','like','%'.$data['height'].'%']); }else{ $size_id = []; } $sector_place = SectorPlace::where($where) ->select('sector_place.*'); $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; } } } 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('通栏版式类别不存在!'); } $row_num = 1; $cat_num = 0; $row_width = []; $map = []; 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); $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'] = $width.'%'; } } return ['map_json'=>$map ?? [], 'component'=>count($component), 'row_num'=>$row_num ?? 0, 'cat_num'=>$cat_num ?? 0, 'component_info'=>$component,]; } /** * 通栏版式管理-修改通栏版式 */ 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('组件版式编号或位置已存在!'); } $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: $index_rule = json_decode($rule['index_rule']); break; case 2: $index_rule = json_decode($rule['class_rule']); break; case 3: $index_rule = json_decode($rule['list_rule']); break; case 4: $index_rule = json_decode($rule['info_rule']); break; case 5: $index_rule = json_decode($rule['search_rule']); break; case 6: $index_rule = json_decode($rule['footerlist_rule']); break; case 7: $index_rule = json_decode($rule['footerinfo_rule']); break; default: break; } } $query = WebsiteCategory::where('website_id', $data['website_id']); $cate_num = $query->where('is_show', 1)->count(); if ($cate_num < 5) { return Result::error('此网站栏目过少,无法进行随机模板!'); } // return Result::success($data); // $tempalte['index'] = $this->randomWeb($data,$index_rule); if(isset($data['page']) && !empty($data['page'])){ $tempalte['index'] = $this->randomPage($data,$cate_num,$index_rule); }else{ $data['page'] = 1; $ruleIndex = json_decode($rule->index_rule); $tempalte['index'] = $this->randomPage($data, $cate_num, $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->randomPage($data, $cate_num, $ruleClass); $data['page'] = 3; $ruleList = json_decode($rule->list_rule); $tempalte['list'] = $this->randomPage($data,$cate_num,$ruleList); $data['page'] = 4; $ruleInfo = json_decode($rule->info_rule); $tempalte['info'] = $this->randomPage($data,$cate_num,$ruleInfo); $data['page'] = 5; $ruleSearch = json_decode($rule->search_rule); $tempalte['search'] = $this->randomPage($data,$cate_num,$ruleSearch); $data['page'] = 6; $ruleFooterlist = json_decode($rule->footerlist_rule); $tempalte['footerlist'] = $this->randomPage($data,$cate_num,$ruleFooterlist); $data['page'] = 7; $ruleFooterinfo = json_decode($rule->footerinfo_rule); $tempalte['footerinfo'] = $this->randomPage($data,$cate_num,$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; } // 必需通栏中的栏目数量 $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); } $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("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; $num = 0; $type = []; // return $rule_repaet_type; // 防止无限循环,设置最大迭代次数 $max_iterations = 10; $iteration_count = 0; // 剩余的栏目数量 $sector_cate_comnum = $cate_num - $random_cat_num; if($sector_cate_comnum < 0){ return '栏目数量过少,无法完成随机!'; } // var_dump("sector_comnum", $sector_cate_comnum); $com_num = 0; // return [ // 'repeat_type'=>$repeat_type, // 'repeat_num'=>$repeat_num, // 'sector_comnum'=>$sector_cate_comnum, // 'rule_repeat'=>$rule_repeat, // 'unique_sector_type'=>$unique_sector_type, // 'all_sector_type'=>$all_sector_type, // ]; $repaet_cat_num = 0; $sector_key = 0; $repeat_onenum = []; $repeat_type_sectornum = array_count_values( $all_sector_type); $repeat_type_sectorkey = array_keys($repeat_type_sectornum); // 随机重复通栏----- while ($repaet_key <= $repeat_num-1 && $iteration_count < $max_iterations) { if ($sector_cate_comnum - $repaet_cat_num > 0) { foreach ($repeat_type as $key => $value) { if(!isset($repeat_onenum[$value])){ $repeat_onenum[$value] = 0; } $sector_key = $repaet_key; // 确保 $value 是数组且不为空,再使用 $value[0] 或 array_rand if(in_array($value,$all_sector_type) && in_array($value,$repeat_type_sectorkey) ){ // $sector_key = $key+$num; // 可重复通栏,每一个可重复种类的通栏随机取1 if($repeat_type_sectornum[$value] > 1){ // 找出 all_sector_type 中值为 1 的键 $keys_equal_one = array_keys($all_sector_type,$value); // 随机取一个键 $random_key = $keys_equal_one[array_rand($keys_equal_one)]; $repeat_sector_key[$sector_key] = $random_key; }else{ $repeat_sector_key[$sector_key] = array_keys($all_sector_type,$value)[0]; } $sector_cat_key = $repeat_sector_key[$sector_key]; $repaet_cat_num += $sectors[$sector_cat_key]['cat_num']; // var_dump(['--------------------' => $sector_cate_comnum - $repaet_cat_num <= 0, // '************************' => $repaet_cat_num]); if($sector_cate_comnum - $repaet_cat_num <= 0){ unset($repeat_sector_key[$sector_key]); // $repaet_key--; // $sector_key = $sector_key-$repaet_key; $repaet_cat_num = $repaet_cat_num-$sectors[$sector_cat_key]['cat_num']; // var_dump(['33333333333333333' => $sector_key]); continue; }else{ $repeat_onenum[$value]++; $repaet_key++; } // var_dump(['22222222222222222' => $sector_key]); }else{ break; } // if (is_array($value) && count($value) > 0) { // if (in_array($value, $repeat_type_sectorkey) && in_array($value, $repeat_type_sectorkey) && // isset($rule_repeat[$value]) && $repeat_onenum[$value] <= $rule_repeat[$value]) { // $repeat_onenum[$value] = $key; // } // } } } else { break; } $num++; $iteration_count++; } // $value = 1; // return [ // 'repeat_num'=>$repeat_num, // 'sector_cate_comnum'=>$sector_cate_comnum, // 'repaet_cat_num'=>$repaet_cat_num, // 'repeat_onenum'=>$repeat_onenum, // 'repeat_sector_key'=>$repeat_sector_key, // // 'sector_cate_comnum'=>$sector_cate_comnum, // 'sector_cat_key'=>$sector_cat_key, // // 'all_sector_type'=>$all_sector_type, // // 'sectors'=>$sectors, // ]; // var_dump("num", $num); // var_dump("repeat_sector_key", $repeat_sector_key); // 随机的重复通栏键值转换成真正的通栏 $repeat_sector = []; if (isset($repeat_sector_key) && !empty($repeat_sector_key) && is_array($repeat_sector_key)) { foreach ($repeat_sector_key as $key => $value) { if (isset($sectors[$value])) { $repeat_sector[$key] = $sectors[$value]; } } } $repeat_count = count($repeat_sector); var_dump("repeat_count", $repeat_count); // return [ // 'sector_comnum'=>$sector_comnum, // 'com_num'=>$com_num, // 'repeat_count'=>$repeat_count, // 'repeat_sector'=>$repeat_sector, // ]; // // 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){ $sector[0] = $toutiao; $sector[$toutiao_key] = $sort_1; $sector[1] = $pic; $sector[$pic_key] = $sort_2; } if($cat_key){ $sector[0] = $pic; $sector[$pic_key] = $sort_1; } } if ($data['page'] == '2' || $data['page'] == 2 || in_array(5, $sector_specal_sort)) { $sector[0] = $cat; $sector[$cat_key] = $sort_1; if ($toutiao_key && $pic_key) { $sector[1] = $toutiao; $sector[$toutiao_key] = $sort_2; $sector[2] = $pic; $sector[$pic_key] = $sort_3; } if ($pic_key) { $sector[1] = $pic; $sector[$pic_key] = $sort_2; } } } $sector_id = array_column($sector,'id'); // if(count(array_intersect($sector,$must_sector)) != count($must_sector)){ // return '此页面缺少必要通栏!'; // } // 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_id'=>$sector_id, // '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', 'component_img.component_id', 'component.component_type') // ->leftJoin('sector', 'sector.id', 'sector_component.sectorid') ->where('component_img.template_id', '=', $data['template_id']) ->select( 'component.*', 'component_img.img_name', 'component_img.img_id', 'component_img.img_url', 'sector_component.sort_id', 'sector_component.sectorid', // 'sector.sector_type', ) ->orderBy('sector_component.sort_id') ->orderBy('component.component_type') ->get(); // 按sectorid和sort_id分组数据 $groupedResults = []; $sort_ids = []; $sector_ids = []; $component_list = []; $component_ids = []; $img_ids = []; $sort_key = 0; // return $rawResults; // $groupedResults重构数组,将相同位置的组件合并,相同组件的样式合并(已经包括所有选择的组件及组件样式) // $sort_component = array_column($rawResults,'sort_id'); foreach ($rawResults as $key => $item) { $sectorId = $item->sectorid; $sortId = $item->sort_id; $sector_type = $item->sector_type; $componentId = $item->component_type; $component_data = $item->component_data; $component_column = $item->component_column; $component_type = $item->type_id; $component_ad = $item->ad; $imgId = $item->img_id; if (!isset($groupedResults[$sectorId])) { $groupedResults[$sectorId] = []; } $componentData = !empty($component_data) && is_string($component_data) ? json_decode($component_data, true) : []; $component_listtype = !empty($component_column) && is_string($component_column) ? json_decode($component_column, true) : []; $component_code = !empty($component_code) && is_string($component_code) ? json_decode($component_code, true) : []; $component_ad = !empty($component_ad) && is_string($component_ad) ? json_decode($component_ad, true) : []; // 确保 $componentData['componentData'] 是数组,避免 array_push 传入 null if (!isset($componentData['componentData']) || !is_array($componentData['componentData'])) { $componentData['componentData'] = []; } $componentData['componentData'] += $component_listtype; $component_code = $componentData; $component_list[$key] = $component_code; // $component_list[$key]['listType'] = $component_listtype; // } if(in_array($component_type,[19,20,21,22,23,24])){ $sortId = $sortId*2-1; $sort_id = intval($sortId-1); }else{ $sort_id = intval($sortId - 1); } // if(!isset($groupedResults[$sectorId][$sort_id])) { // // $groupedResults[$sectorId][$sortId] = []; // $com_key = 0; // $groupedResults[$sectorId][$sort_id][$com_key] = [ // 'component_type' => $item->component_type, // 'type_id' => $component_type, // 'sort' => $sortId, // 'width' => $item->component_width, // 'height' => $item->component_height, // 'images' => [ // 'img_name' => $item->img_name, // 'img_id' => $item->img_id, // 'img_url' => $item->img_url, // 'component_type' => $component_type, // ] // ]; // // 检查 $sectorId 和 $sort_id 是否为合法的数组键类型(字符串或整数) // }else{ // if((isset($component_ids) && in_array($componentId,$component_ids))){ // if(isset($img_arr)){ // $img_arr++; // }else{ // $img_arr = 0; // if(isset($com_key)){ // $com_key++; // } // } // // $com_id_key = array_search($componentId,$component_ids); // $groupedResults[$sectorId][$sort_id][$com_key]['images'][$img_arr] = [ // 'img_name' => $item->img_name, // 'img_id' => $item->img_id, // 'img_url' => $item->img_url, // 'component_type' => $component_type, // ]; // }else{ // $img_arr = 0; // $com_key = $com_key ?? 0; // $groupedResults[$sectorId][$sort_id][$com_key] = [ // 'component_type' => $item->component_type, // 'type_id' => $component_type, // 'sort' => $sortId, // 'width' => $item->component_width, // 'height' => $item->component_height, // 'images' => // [ // $img_arr => // [ // 'img_name' => $item->img_name, // 'img_id' => $item->img_id, // 'img_url' => $item->img_url, // 'component_type' => $component_type, // ] // ] // ]; // } // } // $component = $groupedResults[$sectorId][$sort_id]; // if(count($component) > 1){ // $com_key = 0; // } // if(!in_array($sectorId,$sector_ids)){ // $sort_key = 0; // $sector_ids[$sectorId][$sort_key] = $sectorId; // }else{ // $sort_key++; // $sector_ids[$sectorId][$sort_key] = $sectorId; // } if (!isset($com_key) || !in_array($sectorId, $sector_ids) || !isset($sort_ids[$sectorId]) || !in_array($sortId, $sort_ids[$sectorId])) { $com_key = 0; // if(!in_array($componentId,$component_ids)){ // $img_key = 0; // } $img_key = 0; } else if (in_array($sortId, $sort_ids[$sectorId]) && in_array($sectorId, $sector_ids) && !in_array($componentId, $component_ids)) { $com_key++; } if (in_array($componentId, $component_ids) && !in_array($imgId, $img_ids[$componentId])) { $img_key++; } else { $img_key = 0; } if (!in_array($componentId, $component_ids)) { $groupedResults[$sectorId][$sort_id][$com_key] = [ 'component_type' => $item->component_type, 'type_id' => $component_type, 'sort' => $sortId, 'width' => $item->component_width, 'height' => $item->component_height, ]; if(in_array($component_type,[19,20,21,22,23,24])){ $groupedResults[$sectorId][$sort_id+1][$com_key] = [ 'component_type' => $item->component_type, 'type_id' => $component_type, 'sort' => $sortId+1, 'width' => $item->component_width, 'height' => $item->component_height, ]; } } $groupedResults[$sectorId][$sort_id][$com_key]['images'][$img_key] = [ 'img_name' => $item->img_name, 'img_id' => $imgId, 'img_url' => $item->img_url, 'component_type' => $componentId, ]; if(in_array($component_type,[19,20,21,22,23,24])){ $groupedResults[$sectorId][$sort_id+1][$com_key]['images'][$img_key] = [ 'img_name' => $item->img_name, 'img_id' => $imgId, 'img_url' => $item->img_url, 'component_type' => $componentId, ]; } // $groupedResults[$sectorId]['sector_type'] = $sector_type; // $component_type[$key] = $groupedResults[$sectorId][$sort_id]['component_type']; // $sort_com = $groupedResults[$sectorId][$sort_id]['component_type']; // if(count($sort_com) == 1){ // $com_key = 0; // } // // 组件分类:1.头条资讯;2.轮播图资讯;3.推荐图;4.最新资讯;5.推荐资讯;6.热点资讯;7.栏目资讯;8.列表类;9.详情类; // // 10.二级导航类;11.广告类;12.静态类;13.单页导航类;14.资讯广告混合类;15.滚动图文类;16.搜索框类;17.单页列表类;18.单页详情类; if (is_scalar($sectorId) && is_scalar($sort_id)) { if (!in_array($component_type, [10, 12, 16])) { if ($component_type == 11 || $component_type == 14 || $component_type == 24) { $groupedResults[(int)$sectorId][(int)$sort_id][$com_key]['ad'] = $component_ad; $groupedResults[(int)$sectorId][(int)$sort_id][$com_key]['ad']['website_id'] = $data['website_id']; } if ($component_type != 11) { $groupedResults[(int)$sectorId][(int)$sort_id][$com_key]['componentData'] = $component_code['componentData']; if(in_array($component_type,[19,20,21,22,23,24])){ $groupedResults[(int)$sectorId][(int)$sort_id+1][$com_key]['componentData'] = $component_code['componentData']; } } } else { $groupedResults[(int)$sectorId][(int)$sort_id][$com_key]['componentData'] = []; if(in_array($component_type,[19,20,21,22,23,24])){ $groupedResults[(int)$sectorId][(int)$sort_id+1][$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 $sort_ids; $result = []; // return $groupedResults; $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[$selectedComponentId]; $Component['component_type'] = $components[$selectedComponentId]['component_type'] ?? 0; $Component['sort'] = $components[$selectedComponentId]['sort'] ?? 1; $Component['type_id'] = $components[$selectedComponentId]['type_id'] ?? 0; // 通栏某一位置的随机组件样式图 // 检查 $Component['images'] 是否存在且为数组 if (isset($components[$selectedComponentId]['images']) && is_array($components[$selectedComponentId]['images'])) { $images = count($components[$selectedComponentId]['images']); $selectedImage = 0; if ($images > 1) { $selectedImage = rand(0, $images - 1); } if (isset($components[$selectedComponentId]['images'][$selectedImage])) { $Component['component_style'] = is_object($components[$selectedComponentId]['images'][$selectedImage]) ? $components[$selectedComponentId]['images'][$selectedImage]->img_id : $components[$selectedComponentId]['images'][$selectedImage]['img_id']; } } $image_info[$key] = is_object($components[$selectedComponentId]['images'][$selectedImage]) ? $components[$selectedComponentId]['images'][$selectedImage] : $components[$selectedComponentId]['images'][$selectedImage]; // var_dump($images); $Component['componentData'] = $components[$selectedComponentId]['componentData'] ?? []; if ($data['page'] == 1 && (isset($components[$selectedComponentId]['type_id']) && in_array($components[$selectedComponentId]['type_id'], [7, 14, 15]))) { $Component['componentData']['name'] = '请选择栏目'; } if ($data['page'] == 2 && (isset($components[$selectedComponentId]['type_id']) && in_array($components[$selectedComponentId]['type_id'], [7, 14, 15]))) { $Component['componentData']['name'] = '自动生成'; } if (isset($components[$selectedComponentId]['type_id']) && $components[$selectedComponentId]['type_id'] == 11) { $ad = $components[$selectedComponentId]['ad']; // $ad['thumb'] = $image_info[$key]['img_url']; $template_data[$key]['ad'] = $ad; $canvas_adinfo[$key] = $ad; $canvas_data[$key]['content']['ad'] = $canvas_adinfo[$key]; // // 确保 $ad 和 $canvas_adinfo[$key] 元素数量一致,直接组合 // // $canvas_data[$key]['ad'] = array_combine($ad, $canvas_adinfo[$key]); $Component['componentData'] = []; } if (isset($components[$selectedComponentId]['type_id']) && $components[$selectedComponentId]['type_id'] == 14) { $ad = $components[$selectedComponentId]['ad']; $ad['thumb'] = $image_info[$key]['img_url']; $template_data[$key]['ad'] = $ad; $canvas_adinfo[$key] = $ad; $canvas_data[$key]['content']['ad'] = $canvas_adinfo[$key]; // // $canvas_data[$key]['ad'] = array_combine($ad, $canvas_adinfo[$key]); // // $Component['componentData'] = []; } // var_dump('---------------------------',$components[$selectedComponentId]); $sector_component[$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,$index_rule) // { // // 在使用 with 方法关联查询其他表时限制查询字段,不需要修改 Sector 的 model 文件, // // 可以直接在查询时通过 select 方法指定要查询的字段。以下是修改后的代码: // $component_img = Sector::select('sector.id', 'sector.template_id', 'sector.page_type','sector.sector_id','sector.component_num' // ,'sector.sector_name','sector.sector_type') // ->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() // ->all(); // // 通栏类别: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]} // $rule = is_array($index_rule) ? $index_rule : (array)$index_rule; // // return gettype($rule); // $must_num = count($rule['must_type']); // $sector_type = array_column($component_img,'sector_type'); // $notmust_sectortype = count(array_diff(array_unique(array_column($component_img,'sector_type')),$rule['must_type'])); // $notmust_num = random_int(0,$notmust_sectortype); // $min_num = $notmust_sectortype+$must_num; // $max_num = $rule['max_num']; // $index_num = random_int($min_num, $max_num); // $repeat_num = $index_num - $must_num - $notmust_num; // $category_num = WebsiteCategory::where('website_id',$data['website_id']) // ->where('is_show',1) // ->get() // ->all(); // $num = 0; // $sector_ids = []; // $sector_sortids = []; // foreach($components as $key => $value){ // if(!in_array($value['sectorid'],$sector_ids)){ // $num = 0; // } // if($value['type_id'] == 7 && !in_array($value['sort_id'],$sector_sortids[$value['sectorid']])){ // $num++; // } // $sector_typenum[$value['sectorid']]['num'] = $num; // $sector_typenum[$value['sectorid']][$value['sort_id']] = $num; // $sector_ids[$key] = $value['sectorid']; // $sector_sortids[$value['sectorid']][$key] = $value['sort_id']; // // } // foreach($sector_type as $key => $value){ // if($value == 1){ // $sector_type[$key] = 1; // } // } // return [ // 'index_num' => $index_num, // 'min_num' => $min_num, // '$sector_type' => $sector_type, // 'notmust_sectortype' => $notmust_sectortype, // 'must_num' => $must_num, // 'notmust_num' => $notmust_num, // 'repeat_num' => $repeat_num, // // '$sector_typenum' => $sector_typenum, // ]; // return $component_img; // } /** * 自助建站-获取所有通栏、组件、组件样式 */ 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::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(!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($result); } /** * 自助建站-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{ $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('会话不存在!'); } $data['message_type'] = empty($data['message_type']) ? 'text' : $data['message_type']; $result = Aichat::insertGetId($data); if(empty($result)){ return Result::error('发送消息失败!'); } return Result::success($result); } /** * 自助建站-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','desc') ->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)){ return Result::error('草稿模板已存在!'); } $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)->get()->all(); if(empty($template_draftbox)){ return Result::error('草稿模板不存在!'); } return Result::success($template_draftbox); } /** * 自助建站-验证关键词填写重复问题 */ public function checkRepeatKeyword(array $data): array { if(empty($data['keyword'])){ return Result::error('关键词不能为空!'); } $keyword = json_decode($data['keyword'],true); $keyword = array_map('trim', $keyword); // $keyword_list = array_column($keyword,'keyword'); if(count($keyword) != count(array_unique($keyword))){ return Result::error('关键词不能重复!'); } // return Result::success(json_encode($keyword,JSON_UNESCAPED_UNICODE)); $keyword = json_encode($keyword, JSON_UNESCAPED_UNICODE); if($data['type'] == 'style'){ $template_class_keyword = TemplateClass::pluck('keyword')->toArray(); $keyword = preg_replace('/,/', ', ', $keyword); if(in_array($keyword,$template_class_keyword)){ return Result::error('此风格关键词已存在!'); } } if($data['type'] == 'skin'){ $template_keyword = Template::pluck('template_keyword')->toArray(); $keyword = preg_replace('/,/', ', ', $keyword); if(in_array($keyword,$template_keyword)){ return Result::error('此皮肤关键词已存在!'); } return Result::success(['关键词'=>$keyword,'type'=>$template_keyword]); } if($data['type'] == 'sector'){ $sector_keyword = Sector::where('template_id',$data['template_id'])->pluck('sector_keyword')->toArray(); if(in_array($keyword,$sector_keyword)){ return Result::error('此通栏关键词已存在!'); } } if($data['type'] == 'component'){ $component_keyword = Component::pluck('component_keyword')->toArray(); if(in_array($keyword,$component_keyword)){ return Result::error('此组件关键词已存在!'); } } 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); } }