200, 'msg' => 'success', 'data' => [ 'time' => $time, 'data' => $data, ] ]; //mysql直接查询连接数 $activeConnections = Db::select("SHOW STATUS LIKE 'Threads_connected'"); // $activeConnectionsCount = $activeConnections[0]->Value ?? 0; // $users = Db::select('SELECT * FROM article;'); return ['code' => 200, 'msg' => 'success', 'data' => $activeConnections]; } public function indexData(array $data): array { $time1 = microtime(true); // 调整获取方式 // 根据网站id获取,从website_template中获取 website_template $website_id = $data['website_id'] ?? 2; $getpage = $data['getpage'] ?? 'index'; $template = Db::table('website_template')->where('website_id', $website_id)->first(); var_dump("模板:", $template); $template = $template->template_data; $data = json_decode($template, true); // $data = json_decode($data['template'], true); // $website_id = $data['base']['website_id'] ?? 2; //设置缓存 $websiteInfoCacheKey = "awebsite:category:{$website_id}"; $websiteInfo = $this->redis->get($websiteInfoCacheKey); //false; if ($websiteInfo === false) { // Redis 中没有缓存,从数据库中查询 $websiteInfo = Db::table('website_category')->where('website_id', $website_id)->get()->toArray(); // 将查询结果存入 Redis,设置缓存时间为 3600 秒(1 小时) $this->redis->setex($websiteInfoCacheKey, 3600, json_encode($websiteInfo)); } else { // 从 Redis 中获取的数据需要反序列化 $websiteInfo = json_decode($websiteInfo, true); } $websiteInfo = Db::table('website_category')->where('website_id', $website_id)->get()->toArray(); //取出website var_dump($websiteInfo, 'websiteInfo'); //取出category_id 对应的数据 $websiteInfoIndexed = array_column($websiteInfo, null, 'category_id'); // 取出category_id 对应的aLIas_pinyin $categoryIds = array_column($websiteInfo, 'category_id'); $aliasPinyins = array_column($websiteInfo, 'aLIas_pinyin'); $cat_arr = array_combine($categoryIds, $aliasPinyins); // var_dump($cat_arr, 'cat_arr'); //根据 category_arr_id 组合出 aLIas_pinyin $catiall = []; //一级所有子级的记录 $cat_1st_arr = []; foreach ($websiteInfo as $key => $value) { //算出路由拼音 $category_arr_id = json_decode($value->category_arr_id); $pinyin_str = ''; foreach ($category_arr_id as $k => $v) { $pinyin_str .= isset($cat_arr[$v]) ? $cat_arr[$v] . '/' : ''; } $pinyin_str = rtrim($pinyin_str, '/'); $catiall[$value->category_id][] = $pinyin_str; // $cat_id = $value->category_id; $websiteInfoIndexed[$value->category_id]->pinyin = $pinyin_str; // 算出一级 并且算出子级 if ($value->pid == 0) { if (empty($cat_1st_arr[$value->category_id])) $cat_1st_arr[$value->category_id] = []; } else { if ($value->pid == 11) { var_dump($value->category_id, 'value->category_id'); } $cat_1st_arr[$value->pid][] = $value->category_id; if (empty($cat_1st_arr[$value->category_id])) { $cat_1st_arr[$value->category_id] = []; } } } var_dump($cat_1st_arr, 'cat_1st_arr'); $time2 = microtime(true); $time_ = ($time2 - $time1); var_dump($time2, $time1, $time_, '返回路由需要多少时间'); //获取 // $getpage = $data['base']['getpage'] ?? 'index'; var_dump($getpage, 'leixing '); // index = 首页 // class = 频道页 // list = 列表页 // article = 详情页 // search = 搜索页 // aboutList = 底部导航列表页 // aboutArticle = 底部导航详情页 // return $cat_1st_arr; // return $cat_1st_arr; // var_dump($cat_1st_arr[11], 'cat_1st_arr'); //一级所有子级的记录 // var_dump($catiall, 'catiall');//拼音 $templateData = []; switch ($getpage) { case 'index': $ceng = $data['template']['index']; var_dump($ceng, 'ceng================'); foreach ($ceng as $k_ceng => $v_ceng) { $componetList = $v_ceng['componentList']; //计算一级的所有子级;组成一个数据,key是一级的category_id,value是所有的子级数据,加上个二级的pinyin属性,放的是catiall的数据 foreach ($componetList as $key => $value) { //取出componentData $componentData = $value['componentData']; if (!$componentData) { continue; }; var_dump($componentData, '----------------componentData-------------'); if (is_array($componentData) && count($componentData) > 1 && !isset($componentData['level'])) { foreach ($componentData as $k1 => $v1) { // $v1['category_id'] = $v1['category_id']; $category_id = $v1['category_id'] ?? 0; //此处应该有值 $imgSize = $v1['imgSize']; $textSize = $v1['textSize']; $level = $v1['level']; // 暂不处理选项卡中带子级栏目数据 // $child = $v1['child']; //获取子级 // if ($child) { // $child_id = $child['id'] ?? 0; // $child_imgSize = $child['imgSize'] ?? 0; // $child_textSize = $child['textSize'] ?? 0; // } // 拼音就是路由---- // unset($listType['pinyin']); //查询查询数据,返回到data字段里,根据这几个 if ($category_id > 0) { // var_dump($category_id, '------------------category_id'); // var_dump($cat_1st_arr, '-----------1-------2'); // 第一次查询:imgurl 不为空的数据 if ($imgSize > 0) { $imgArticles = Db::table('article') ->leftJoin('article_ignore', function ($join) use ($website_id) { $join->on('article.id', '=', 'article_ignore.article_id') ->where('article_ignore.website_id', '=', $website_id); }) // ->whereNull('article_ignore.article_id') ->where(function ($query) { $query->whereNull('article_ignore.article_id') ->orWhere(function ($subQuery) { $subQuery->whereNotNull('article_ignore.article_id') ->where('article_ignore.is_ignore', 0) ->where(function ($subSubQuery) { $subSubQuery->where('article_ignore.c_show_time', '<=', date('Y-m-d H:i:s')) ->orWhereNull('article_ignore.c_show_time'); }); }); }) ->whereIn('catid', $temp_arr = array_merge([$category_id], $cat_1st_arr[$category_id]))->where('status', 1) ->whereNotNull('imgurl')->where('imgurl', '!=', '')->limit($imgSize)->orderBy('updated_at', 'desc')->get()->toArray(); //拼接上拼音 foreach ($imgArticles as $k => $v) { $imgArticles[$k]->pinyin = $catiall[$v->catid][0]; } } // 第二次查询:imgurl 为空的数据 if ($textSize > 0) { $textArticles = Db::table('article') ->leftJoin('article_ignore', function ($join) use ($website_id) { $join->on('article.id', '=', 'article_ignore.article_id')->where('article_ignore.website_id', '=', $website_id); }) // ->whereNull('article_ignore.article_id') ->where(function ($query) { $query->whereNull('article_ignore.article_id') ->orWhere(function ($subQuery) { $subQuery->whereNotNull('article_ignore.article_id') ->where('article_ignore.is_ignore', 0) ->where(function ($subSubQuery) { $subSubQuery->where('article_ignore.c_show_time', '<=', date('Y-m-d H:i:s')) ->orWhereNull('article_ignore.c_show_time'); }); }); }) ->whereIn('catid', $temp_arr = array_merge([$category_id], $cat_1st_arr[$category_id])) ->where('status', 1) ->where('status', 1)->limit($textSize)->orderBy('updated_at', 'desc')->get()->toArray(); //拼接上拼音 foreach ($textArticles as $k => $v) { $textArticles[$k]->pinyin = $catiall[$v->catid][0]; } } } // var_dump($level, 'level'); if ($level > 0) { // 取出一级的所有子级 //取出所有的子级的imgurl 不为空的数据 if ($imgSize > 0) { $imgArticles = Db::table('article') ->leftJoin('article_ignore', function ($join) use ($website_id) { $join->on('article.id', '=', 'article_ignore.article_id')->where('article_ignore.website_id', '=', $website_id); }) // ->whereNull('article_ignore.article_id') ->where(function ($query) { $query->whereNull('article_ignore.article_id') ->orWhere(function ($subQuery) { $subQuery->whereNotNull('article_ignore.article_id') ->where('article_ignore.is_ignore', 0) ->where(function ($subSubQuery) { $subSubQuery->where('article_ignore.c_show_time', '<=', date('Y-m-d H:i:s')) ->orWhereNull('article_ignore.c_show_time'); }); }); }) ->whereRaw('MATCH(level_text) AGAINST(? IN NATURAL LANGUAGE MODE)', [$level])->where('status', 1)->whereIn('catid', $categoryIds)->whereNotNull('imgurl')->where('imgurl', '!=', '')->limit($imgSize)->orderBy('updated_at', 'desc')->get()->toArray(); //拼接上拼音 foreach ($imgArticles as $k => $v) { $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '====='; } } //取出所有的子级的imgurl 为空的数据 ->whereNull('imgurl') if ($textSize > 0) { $textArticles = Db::table('article')->where('status', 1) ->leftJoin('article_ignore', function ($join) use ($website_id) { $join->on('article.id', '=', 'article_ignore.article_id')->where('article_ignore.website_id', '=', $website_id); }) // ->whereNull('article_ignore.article_id') ->where(function ($query) { $query->whereNull('article_ignore.article_id') ->orWhere(function ($subQuery) { $subQuery->whereNotNull('article_ignore.article_id') ->where('article_ignore.is_ignore', 0) ->where(function ($subSubQuery) { $subSubQuery->where('article_ignore.c_show_time', '<=', date('Y-m-d H:i:s')) ->orWhereNull('article_ignore.c_show_time'); }); }); }) ->whereRaw('MATCH(level_text) AGAINST(? IN NATURAL LANGUAGE MODE)', [$level])->whereIn('catid', $categoryIds)->limit($textSize)->orderBy('updated_at', 'desc')->get()->toArray(); //拼接上拼音 foreach ($textArticles as $k => $v) { $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '====='; } } } $componetList[$key]['componentData']['data'][$k1]['alias'] = $websiteInfoIndexed[$category_id]->alias ?? 'aliasnull' . $category_id; $componetList[$key]['componentData']['data'][$k1]['pinyin'] = $websiteInfoIndexed[$category_id]->pinyin ?? 'aliasnull' . $category_id; $componetList[$key]['componentData']['data'][$k1]['category_id'] = $category_id; $componetList[$key]['componentData']['data'][$k1]['textnum'] = $textArticles ?? []; $componetList[$key]['componentData']['data'][$k1]['imgnum'] = $imgArticles ?? []; } } else { $category_id = $componentData['category_id'] ?? 0; //此处应该有值 $imgSize = $componentData['imgSize']; $textSize = $componentData['textSize']; $level = $componentData['level']; $child = $componentData['child']; //获取子级 if ($child) { $child_id = $child['id'] ?? 0; $child_imgSize = $child['imgSize'] ?? 0; $child_textSize = $child['textSize'] ?? 0; } // 拼音就是路由---- // unset($listType['pinyin']); //查询查询数据,返回到data字段里,根据这几个 if ($category_id > 0) { // var_dump($category_id, '------------------category_id'); // var_dump($cat_1st_arr, '-----------1-------2'); // 第一次查询:imgurl 不为空的数据 if ($imgSize > 0) { $imgArticles = Db::table('article') ->leftJoin('article_ignore', function ($join) use ($website_id) { $join->on('article.id', '=', 'article_ignore.article_id') ->where('article_ignore.website_id', '=', $website_id); }) // ->whereNull('article_ignore.article_id') ->where(function ($query) { $query->whereNull('article_ignore.article_id') ->orWhere(function ($subQuery) { $subQuery->whereNotNull('article_ignore.article_id') ->where('article_ignore.is_ignore', 0) ->where(function ($subSubQuery) { $subSubQuery->where('article_ignore.c_show_time', '<=', date('Y-m-d H:i:s')) ->orWhereNull('article_ignore.c_show_time'); }); }); }) ->whereIn('catid', $temp_arr = array_merge([$category_id], $cat_1st_arr[$category_id]))->where('status', 1) ->whereNotNull('imgurl')->where('imgurl', '!=', '')->limit($imgSize)->orderBy('updated_at', 'desc')->get()->toArray(); //拼接上拼音 foreach ($imgArticles as $k => $v) { $imgArticles[$k]->pinyin = $catiall[$v->catid][0]; } } // 第二次查询:imgurl 为空的数据 if ($textSize > 0) { $textArticles = Db::table('article') ->leftJoin('article_ignore', function ($join) use ($website_id) { $join->on('article.id', '=', 'article_ignore.article_id')->where('article_ignore.website_id', '=', $website_id); }) // ->whereNull('article_ignore.article_id') ->where(function ($query) { $query->whereNull('article_ignore.article_id') ->orWhere(function ($subQuery) { $subQuery->whereNotNull('article_ignore.article_id') ->where('article_ignore.is_ignore', 0) ->where(function ($subSubQuery) { $subSubQuery->where('article_ignore.c_show_time', '<=', date('Y-m-d H:i:s')) ->orWhereNull('article_ignore.c_show_time'); }); }); }) ->whereIn('catid', $temp_arr = array_merge([$category_id], $cat_1st_arr[$category_id])) ->where('status', 1) ->where('status', 1)->limit($textSize)->orderBy('updated_at', 'desc')->get()->toArray(); //拼接上拼音 foreach ($textArticles as $k => $v) { $textArticles[$k]->pinyin = $catiall[$v->catid][0]; } } $componetList[$key]['componentData']['data']['alias'] = $websiteInfoIndexed[$category_id]->alias ?? 'aliasnull' . $category_id; $componetList[$key]['componentData']['data']['pinyin'] = $websiteInfoIndexed[$category_id]->pinyin ?? 'aliasnull' . $category_id; $componetList[$key]['componentData']['data']['category_id'] = $category_id; $componetList[$key]['componentData']['data']['textnum'] = $textArticles ?? []; $componetList[$key]['componentData']['data']['imgnum'] = $imgArticles ?? []; } // var_dump($level, 'level'); if ($level > 0) { // 取出一级的所有子级 //取出所有的子级的imgurl 不为空的数据 if ($imgSize > 0) { $imgArticles = Db::table('article') ->leftJoin('article_ignore', function ($join) use ($website_id) { $join->on('article.id', '=', 'article_ignore.article_id')->where('article_ignore.website_id', '=', $website_id); }) // ->whereNull('article_ignore.article_id') ->where(function ($query) { $query->whereNull('article_ignore.article_id') ->orWhere(function ($subQuery) { $subQuery->whereNotNull('article_ignore.article_id') ->where('article_ignore.is_ignore', 0) ->where(function ($subSubQuery) { $subSubQuery->where('article_ignore.c_show_time', '<=', date('Y-m-d H:i:s')) ->orWhereNull('article_ignore.c_show_time'); }); }); }) ->whereRaw('MATCH(level_text) AGAINST(? IN NATURAL LANGUAGE MODE)', [$level])->where('status', 1)->whereIn('catid', $categoryIds)->whereNotNull('imgurl')->where('imgurl', '!=', '')->limit($imgSize)->orderBy('updated_at', 'desc')->get()->toArray(); //拼接上拼音 foreach ($imgArticles as $k => $v) { $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '====='; } } //取出所有的子级的imgurl 为空的数据 ->whereNull('imgurl') if ($textSize > 0) { $textArticles = Db::table('article')->where('status', 1) ->leftJoin('article_ignore', function ($join) use ($website_id) { $join->on('article.id', '=', 'article_ignore.article_id')->where('article_ignore.website_id', '=', $website_id); }) // ->whereNull('article_ignore.article_id') ->where(function ($query) { $query->whereNull('article_ignore.article_id') ->orWhere(function ($subQuery) { $subQuery->whereNotNull('article_ignore.article_id') ->where('article_ignore.is_ignore', 0) ->where(function ($subSubQuery) { $subSubQuery->where('article_ignore.c_show_time', '<=', date('Y-m-d H:i:s')) ->orWhereNull('article_ignore.c_show_time'); }); }); }) ->whereRaw('MATCH(level_text) AGAINST(? IN NATURAL LANGUAGE MODE)', [$level])->whereIn('catid', $categoryIds)->limit($textSize)->orderBy('updated_at', 'desc')->get()->toArray(); //拼接上拼音 foreach ($textArticles as $k => $v) { $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '====='; } } } } // $page = $componentData['page']; // $pageSize = $componentData['pageSize']; // $listType = $componentData['listType']; //将查询到的数据放到componentData里 //判断child var_dump($child_id, '--------child_id---'); if ($child_id > 0) { //取出子级的imgurl 不为空的数据 if ($child_imgSize > 0) { $child_imgArticles = Db::table('article')->where('catid', $child_id) ->where('status', 1) ->whereNotNull('imgurl')->limit($child_imgSize)->orderBy('updated_at', 'desc')->get()->toArray(); } else { $child_imgArticles = []; } //取出子级的imgurl 为空的数据 if ($child_textSize > 0) { $child_textArticles = Db::table('article') ->where('status', 1) ->where('catid', $child_id)->whereNull('imgurl')->limit($child_textSize)->orderBy('updated_at', 'desc')->get()->toArray(); } else { $child_textArticles = []; } //将查询到的数据放到componentData里 $componetList[$key]['componentData']['data']['child']['alias'] = $websiteInfoIndexed[$child_id]->alias; $componetList[$key]['componentData']['data']['child']['pinyin'] = $websiteInfoIndexed[$child_id]->pinyin; $componetList[$key]['componentData']['data']['child']['category_id'] = $child_id; $componetList[$key]['componentData']['data']['child']['textnum'] = $child_textArticles; $componetList[$key]['componentData']['data']['child']['imgnum'] = $child_imgArticles; $componetList[$key]['componentData']['data']['child']['is_url'] = $websiteInfoIndexed[$child_id]->is_url ?? 0; //取出自己 $all_childcat = $cat_1st_arr[$category_id]; $componetList[$key]['componentData']['data']['child']['children_count'] = count($all_childcat); $processedChildCat = array_map(function ($v) use ($websiteInfoIndexed, $cat_1st_arr) { // 从 $websiteInfoIndexed 中获取对应的数据 $info = $websiteInfoIndexed[$v]; var_dump($info, $v, '-------child循环--------'); // 返回一个包含所需信息的数组 return [ 'pinyin' => $info->pinyin, 'alias' => $info->alias, 'category_id' => $info->category_id, 'aLIas_pinyin' => $info->aLIas_pinyin, 'pid' => $info->pid, 'cat_arr_id' => $info->category_arr_id ?? ['出错'], 'is_url' => $info->is_url ?? 0, 'children_count' => isset($cat_1st_arr[$v]) ? count($cat_1st_arr[$v]) : 0 ]; }, $all_childcat); $componetList[$key]['componentData']['data']['child']['all_childcat'] = $processedChildCat ?? []; } } $data['template']['index'][$k_ceng]['componentList'] = $componetList; } //将修改后的数据放到template里 break; case 'class': if (!isset($data['template']['class']['data'][0]['componentList'])) { break; } $componetList = $data['template']['class']['data'][0]['componentList']; $class_count = count($componetList); $parent_id = $data['template']['class']['parent_id'] ?? 0; //获取子级; $child = $cat_1st_arr[$parent_id]; //获取子级 // var_dump($child, 'child'); //如果$child的数量小于$class_count,就填充一样多的数据给child,如果大于就截取一样的数量给child if (count($child) < $class_count) { $child = array_pad($child, $class_count, end($child) ?? 0); } elseif (count($child) > $class_count) { $child = array_slice($child, 0, $class_count); } foreach ($componetList as $key => $value) { $componentData = $value['componentData']; //取出imgSize和textSize,获取相应的文章, $category_id = $child[$key] ?? 0; $imgSize = $componentData['imgSize'] ?? 0; $textSize = $componentData['textSize'] ?? 0; $listType = $componentData['listType'] ?? []; var_dump($listType, 'before unset listType'); // 定义要移除的元素数组 $valuesToRemove = ['category_name', 'pinyin']; // 循环移除每个元素 foreach ($valuesToRemove as $value) { while (($key = array_search($value, $listType)) !== false) { // 移除该元素 unset($listType[$key]); } } var_dump($listType, 'listType'); //child $child = $componentData['child'] ?? []; $data_class = []; //查询imgurl不为空的数据 $imgArticles = Db::table('article') ->where('catid', $category_id) ->whereNotNull('imgurl') ->limit($imgSize) ->leftJoin('article_ignore', function ($join) use ($website_id) { $join->on('article.id', '=', 'article_ignore.article_id')->where('article_ignore.website_id', '=', $website_id); }) ->where(function ($query) { $query->whereNull('article_ignore.article_id') ->orWhere(function ($subQuery) { $subQuery->whereNotNull('article_ignore.article_id') ->where('article_ignore.is_ignore', 0) ->where(function ($subSubQuery) { $subSubQuery->where('article_ignore.c_show_time', '<=', date('Y-m-d H:i:s')) ->orWhereNull('article_ignore.c_show_time'); }); }); }) // ->select($listType) ->orderBy('updated_at', 'desc') ->get() ->toArray(); //拼接上拼音 foreach ($imgArticles as $k => $v) { $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '====='; } //查询imgurl为空的数据 $textArticles = Db::table('article') ->where('catid', $category_id) ->whereNull('imgurl') ->limit($textSize) ->leftJoin('article_ignore', function ($join) use ($website_id) { $join->on('article.id', '=', 'article_ignore.article_id')->where('article_ignore.website_id', '=', $website_id); }) ->where(function ($query) { $query->whereNull('article_ignore.article_id') ->orWhere(function ($subQuery) { $subQuery->whereNotNull('article_ignore.article_id') ->where('article_ignore.is_ignore', 0) ->where(function ($subSubQuery) { $subSubQuery->where('article_ignore.c_show_time', '<=', date('Y-m-d H:i:s')) ->orWhereNull('article_ignore.c_show_time'); }); }); }) ->orderBy('updated_at', 'desc') ->get() ->toArray(); //拼接上拼音 foreach ($textArticles as $k => $v) { $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '====='; } $data_class['alias'] = $websiteInfoIndexed[$category_id]->alias ?? 'aliasnull' . $category_id; $data_class['pinyin'] = $websiteInfoIndexed[$category_id]->pinyin ?? 'aliasnull' . $category_id; $data_class['category_id'] = $category_id; $data_class['textnum'] = $textArticles ?? []; $data_class['imgnum'] = $imgArticles ?? []; $data['data'] = $data_class; } break; case 'list': if (!isset($data['template']['list']['data'][0]['componentList'])) { break; } $componetListdata = $data['template']['list']['data']; $category_id = $data['template']['list']['category_id'] ?? 0; foreach ($componetListdata as $k => $v) { if (!isset($v['componentList']) || !is_array($v['componentList'])) { continue; // 如果 componentList 不存在或不是数组,跳过当前循环 } $componentList = &$data['template']['list']['data'][$k]['componentList']; var_dump(count($componentList), 'componentList111'); foreach ($componentList as $key => $value) { $componentData = $value['componentData']; if (isset($value['componentData']['category_id'])) { $value['componentData']['category_id'] = $category_id; $page = $componentData['papageTypege']['page'] ?? 1; $pageSize = $componentData['papageTypege']['pageSize'] ?? 10; $listType = $componentData['listType']; $articals = Db::table('article') ->where('catid', $category_id) ->where('status', 1)->leftJoin('article_ignore', function ($join) use ($website_id) { $join->on('article.id', '=', 'article_ignore.article_id')->where('article_ignore.website_id', '=', $website_id); }) ->where(function ($query) { $query->whereNull('article_ignore.article_id') ->orWhere(function ($subQuery) { $subQuery->whereNotNull('article_ignore.article_id') ->where('article_ignore.is_ignore', 0) ->where(function ($subSubQuery) { $subSubQuery->where('article_ignore.c_show_time', '<=', date('Y-m-d H:i:s')) ->orWhereNull('article_ignore.c_show_time'); }); }); }) ->orderBy('updated_at', 'desc') ->get(); // ->paginate($pageSize, ['*'], 'page', $page); //拼接上拼音 foreach ($articals as $k => $v) { $articals[$k]->pinyin = $catiall[$v->catid][0] ?? '====='; } $value['componentData']['data'] = $articals; } var_dump(isset($value['componentData']['level']), $key, '---------------------------------------'); if (isset($value['componentData']['level'])) { //取出imgSize和textSize,获取相应的文章, $imgSize = $componentData['imgnum'] ?? 0; $textSize = $componentData['textnum'] ?? 0; $listType = $componentData['listType'] ?? []; // 定义要移除的元素数组 $valuesToRemove = ['category_name', 'pinyin']; // 循环移除每个元素 foreach ($valuesToRemove as $value) { while (($key = array_search($value, $listType)) !== false) { // 移除该元素 unset($listType[$key]); } } //查询imgurl不为空的数据 if ($imgSize > 0) { $imgArticles = Db::table('article') ->where('catid', $category_id) ->whereNotNull('imgurl') ->where('status', 1)->leftJoin('article_ignore', function ($join) use ($website_id) { $join->on('article.id', '=', 'article_ignore.article_id')->where('article_ignore.website_id', '=', $website_id); }) ->where(function ($query) { $query->whereNull('article_ignore.article_id') ->orWhere(function ($subQuery) { $subQuery->whereNotNull('article_ignore.article_id') ->where('article_ignore.is_ignore', 0) ->where(function ($subSubQuery) { $subSubQuery->where('article_ignore.c_show_time', '<=', date('Y-m-d H:i:s')) ->orWhereNull('article_ignore.c_show_time'); }); }); }) ->limit($imgSize) ->orderBy('updated_at', 'desc') ->get() ->toArray(); //拼接上拼音 foreach ($imgArticles as $k => $v) { $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '====='; } } //查询imgurl为空的数据 if ($textSize > 0) { $textArticles = Db::table('article') ->where('catid', $category_id) ->whereNull('imgurl') ->where('status', 1)->leftJoin('article_ignore', function ($join) use ($website_id) { $join->on('article.id', '=', 'article_ignore.article_id')->where('article_ignore.website_id', '=', $website_id); }) ->where(function ($query) { $query->whereNull('article_ignore.article_id') ->orWhere(function ($subQuery) { $subQuery->whereNotNull('article_ignore.article_id') ->where('article_ignore.is_ignore', 0) ->where(function ($subSubQuery) { $subSubQuery->where('article_ignore.c_show_time', '<=', date('Y-m-d H:i:s')) ->orWhereNull('article_ignore.c_show_time'); }); }); }) ->limit($textSize) ->orderBy('updated_at', 'desc') ->get() ->toArray(); //拼接上拼音 foreach ($textArticles as $k => $v) { $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '====='; } } //将查询到的数据放到componentData里 $componetList[$key]['componentData']['data']['alias'] = $websiteInfoIndexed[$category_id]->alias ?? 'aliasnull' . $category_id; $componetList[$key]['componentData']['data']['pinyin'] = $websiteInfoIndexed[$category_id]->pinyin ?? 'aliasnull' . $category_id; $componetList[$key]['componentData']['data']['category_id'] = $category_id; $componetList[$key]['componentData']['data']['textnum'] = $textArticles ?? []; $componetList[$key]['componentData']['data']['imgnum'] = $imgArticles ?? []; //将修改后的数据放到template里 // $data['template']['list']['data'][0]['componentList'] = $componetList; } } } break; case 'article': if (!isset($data['template']['article']['data'])) { break; } $article_id = 51434; //假设这是文章ID $componetListdata = &$data['template']['article']['data']; var_dump($categoryIds, '----------------'); // $category_id = $data['template']['article']['category_id'] ?? 0; foreach ($componetListdata as $k => $v) { foreach ($v['componentList'] as $key => $value) { $componentData = $value['componentData']; if (isset($value['componentData']['article_id'])) { $value['componentData']['article_id'] = $article_id; // $page = $componentData['papageTypege']['page'] ?? 1; // $pageSize = $componentData['papageTypege']['pageSize'] ?? 10; $listType = $componentData['listType']; //查询数据 $articals = Db::table('article') ->where('id', $article_id) ->where('status', 1) ->leftJoin('article_data', 'article.id', '=', 'article_data.article_id') ->select('article.*', 'article_data.content') ->orderBy('updated_at', 'desc') ->get(); //拼接上拼音 $value['componentData']['data'] = $articals; } if (isset($value['componentData']['level'])) { //:1:头条 2:轮播图 3:推荐图 4:最新新闻;5:推荐新闻;6:热点资讯 7:自动关联 //4 update 5 30天之内 6 库中 $level = $componentData['level'] ?? 0; $imgSize = $componentData['imgnum'] ?? 0; $textSize = $componentData['textnum'] ?? 0; if (in_array($level, [1, 2, 3, 6])) { //查询imgurl不为空的数据 $imgArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNotNull('imgurl') ->where('status', 1) ->whereRaw('MATCH(level_text) AGAINST(? IN NATURAL LANGUAGE MODE)', [$level]) ->limit($imgSize)->orderBy('updated_at', 'desc')->get()->toArray(); //拼接上拼音 foreach ($imgArticles as $k => $v) { $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '====='; } //查询imgurl为空的数据 $textArticles = Db::table('article')->where('catid', $categoryIds)->whereNull('imgurl') ->where('status', 1) ->whereRaw('MATCH(level_text) AGAINST(? IN NATURAL LANGUAGE MODE)', [$level]) ->limit($textSize)->orderBy('updated_at', 'desc')->get()->toArray(); //拼接上拼音 foreach ($textArticles as $k => $v) { $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '====='; } } else if ($level == 4) { //查询imgurl不为空的数据 $imgArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNotNull('imgurl') ->where('status', 1) ->limit($imgSize)->orderBy('updated_at', 'desc')->get()->toArray(); //拼接上拼音 if (!empty($imgArticles)) { foreach ($imgArticles as $k => $v) { $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '====='; } } //查询imgurl为空的数据 $textArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNull('imgurl') ->where('status', 1) ->limit($textSize)->orderBy('updated_at', 'desc')->get()->toArray(); //拼接上拼音 var_dump('---4------'); if (!empty($textArticles)) { foreach ($textArticles as $k => $v) { $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '====='; } } } else if ($level == 5) { var_dump($value['componentData']['level'], 'level'); //查询imgurl不为空的数据 $imgArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNotNull('imgurl') ->where('status', 1) //最近三十天 ->where('updated_at', '>=', date('Y-m-d H:i:s', strtotime('-30 days'))) ->limit($imgSize)->orderBy('updated_at', 'desc')->get()->toArray(); //拼接上拼音 if (!empty($imgArticles)) { foreach ($imgArticles as $k => $v) { $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '====='; } } //查询imgurl为空的数据 $textArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNull('imgurl') ->where('status', 1) ->where('updated_at', '>=', date('Y-m-d H:i:s', strtotime('-30 days'))) ->limit($textSize)->orderBy('updated_at', 'desc')->get()->toArray(); //拼接上拼音 var_dump('---5------'); if (!empty($textArticles)) { foreach ($textArticles as $k => $v) { $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '====='; } } } else if ($level == 7) { //获取artical的keyword $keword = DB::table('artical')->where('id', $article_id)->select('keyword')->get(); $kewordl = $keword[0]->keyword; $kewordl = explode(',', $kewordl); //筛选出符合条件的article来 $imgArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNotNull('imgurl') ->where('status', 1) // ->whereJsonContains('keyword', $kewordl) ->whereRaw('MATCH(keyword) AGAINST(? IN NATURAL LANGUAGE MODE)', [$kewordl]) ->limit($imgSize)->orderBy('updated_at', 'desc')->get()->toArray(); //拼接上拼音 foreach ($imgArticles as $k => $v) { $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '====='; } $textArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNull('imgurl') ->where('status', 1) // ->whereJsonContains('keyword', $kewordl) ->whereRaw('MATCH(keyword) AGAINST(? IN NATURAL LANGUAGE MODE)', [$kewordl]) ->limit($textSize)->orderBy('updated_at', 'desc')->get()->toArray(); //拼接上拼音 foreach ($textArticles as $k => $v) { $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '====='; } } } $value['componentData']['data']['imgnum'] = $imgArticles ?? []; $value['componentData']['data']['textnum'] = $textArticles ?? []; } } break; case 'search': break; case 'aboutList': $fact_id = 7; $componetList = &$data['template']['aboutList']['componentData']; $componetList['fact_id'] = $fact_id; $content = Db::table('footer_content') ->where('fcat_id', $fact_id) ->orderBy('updated_at', 'desc') ->get() ->toArray(); $componetList['data'] = $content; break; case 'aboutArticle': $fact_id = 7; $componetList = &$data['template']['aboutList']['componentData']; $componetList['fact_id'] = $fact_id; $content = Db::table('footer_content') ->where('id', $fact_id) ->orderBy('updated_at', 'desc') ->get() ->toArray(); $componetList['data'] = $content; break; default: $componetList = []; break; } return $data; } public function addWeb($data) { $result = WebsiteTemplate::insertGetId($data); if ($result) { return Result::success($result); } else { return Result::error('添加失败'); } } public function deleteWeb($data) { $result = WebsiteTemplate::where('id', $data['id'])->delete(); if ($result) { return Result::success($result); } else { return Result::error('删除失败'); } } public function updateWeb($data) { $result = WebsiteTemplate::where('id', $data['id'])->update($data); if ($result) { return Result::success($result); } else { return Result::error('更新失败'); } } public function getWebInfo($data) { $result = WebsiteTemplate::where('website_template.id', $data['id']) ->leftJoin('website', 'website.id', '=', 'website_template.website_id') ->select('website_template.*', 'website.website_name as website_name', 'website.website_url as website_url') ->first(); return Result::success($result); } public function getWebList($data) { var_dump($data, 'data'); $where = []; if (isset($data['website_name'])) { $where[] = ['website.website_name', 'like', '%' . $data['website_name'] . '%']; } if (isset($data['status'])) { $where[] = ['website_template.status', '=', $data['status']]; } $result = WebsiteTemplate::where([]) ->where($where) ->leftJoin('website', 'website.id', '=', 'website_template.website_id') ->select('website_template.*', 'website.website_name as website_name', 'website.website_url as website_url') ->paginate($data['pageSize'], ['*'], 'page', $data['page']); if ($result) { return Result::success($result); } else { return Result::error('获取列表失败'); } } public function updateWebConfig($data) { var_dump($data, 'data1'); // return Result::success($data); $where = []; if (isset($data['website_id'])) { $where['website_template.website_id'] = $data['website_id']; } $result = WebsiteTemplate::where([]) ->where($where) ->leftJoin('website', 'website.id', '=', 'website_template.website_id') ->select('website_template.id', 'website_template.port', 'website.website_name as website_name', 'website.website_url as website_url', 'website_template.siteId') ->first()->toArray(); var_dump($result, 'result'); $domain = json_decode($result['website_url'], true); $domainlist = $domain; var_dump(empty($result['port']), 'port'); $domain = $domain[3]; if ($domain == null || empty($domain) || !isset($domain)) { return Result::error('域名不存在'); } $port = $result['port']; if ($port == null || empty($port) || !isset($port)) { var_dump($port, 'youp0------port'); return Result::error('端口不存在'); } $postData = [ 'domain' => $domain, 'port' => $port ]; var_dump(empty($result['siteId']), 'siteId---'); if (empty($result['siteId'])) { //实例化对象 $api = new bt_api(); //获取面板日志 $r_data = $api->GetLogs(); //输出JSON数据到浏览器 echo json_encode($r_data); $webname = [ 'domain' => $domain, 'domainlist' => $domainlist, 'count' => count($domainlist) - 1 ]; $siteData = [ 'path' => '/www2/www/wwwroot/' . $domain, 'ftp' => 'false', 'type' => 'PHP', 'type_id' => 0, 'ps' => $domain, 'port' => 80, 'version' => '00', 'need_index' => 0, 'need_404' => 0, 'sql' => 'false', 'codeing' => 'utf8mb4', 'webname' => json_encode($webname), 'add_dns_record' => 'false' ]; $r_data = $api->addSite($siteData); /* { "siteStatus": true, "siteId": 20, "ftpStatus": false, "databaseStatus": false, "gitStatus": false } */ var_dump($r_data, '----------1--r_data--------'); if (isset($r_data['status']) && $r_data['status'] == false) { return Result::error('宝塔添加失败' . $r_data['msg']); } if (isset($r_data['siteStatus']) && $r_data['siteStatus'] == true) { //记录siteId $siteId = $r_data['siteId']; //更新网站模板的siteId WebsiteTemplate WebsiteTemplate::where('website_id', $data['website_id']) ->update([ 'siteId' => $siteId, 'dir' => '/www2/www/wwwroot/' . $domain, 'status' => 2 ]); } // return Result::success($r_data); } // 使用Guzzle HTTP客户端发送POST请求 try { $client = new \GuzzleHttp\Client(); $response = $client->post(env('CREATE_WEB', 'http://adminpre.bjzxtw.org.cn/create.php'), [ 'json' => $postData, 'timeout' => 30, 'headers' => [ 'Content-Type' => 'application/json', ] ]); // 获取响应内容 $responseBody = $response->getBody()->getContents(); $responseData = json_decode($responseBody, true); var_dump($responseData, '脚本返回数据------------------------'); // 检查响应是否为有效的JSON if (json_last_error() === JSON_ERROR_NONE) { // 如果远程服务器返回成功结果 if (isset($responseData['code']) && $responseData['code'] === 1) { return Result::success([ 'message' => '远程服务器处理成功' . $responseData['message'], 'output' => $responseData['output'], 'file' => $responseData['file'], 'code' => $responseData['code'], 'exec' => $responseData['exec'], 'domain' => $domain, 'port' => $port, 'remote_response' => $responseData ]); } else { // 远程服务器返回错误 return Result::error(500, '远程服务器处理失败', [ 'remote_response' => $responseData ]); } } else { // 响应不是有效的JSON return Result::success([ 'message' => '收到远程服务器响应(非JSON格式)', 'domain' => $domain, 'port' => $port, 'raw_response' => $responseBody ]); } } catch (\GuzzleHttp\Exception\RequestException $e) { // HTTP请求异常 $errorMessage = 'HTTP请求失败'; if ($e->hasResponse()) { $response = $e->getResponse(); $errorMessage .= ': ' . $response->getStatusCode() . ' ' . $response->getReasonPhrase(); } else { $errorMessage .= ': ' . $e->getMessage(); } return Result::error(500, $errorMessage, [ 'exception' => $e->getMessage() ]); } catch (\Exception $e) { // 其他异常 return Result::error(500, '请求处理异常: ' . $e->getMessage(), [ 'exception' => $e->getMessage() ]); } if ($result) { return Result::success($result); } else { return Result::error('获取列表失败'); } } private function bt($data) {} public function updateWebOutput($data) { // return Result::error('更新输出失败'); //根据源程序, 复制出来,更改配置文件,打包, // $result = 1; $projectRoot = dirname(dirname(__DIR__)); // 从当前文件向上两级目录 $projectDir = $projectRoot . '/../zizhujianzhan_web'; $command = "cd " . escapeshellarg($projectDir) . " && npm run build"; // 执行命令并获取输出 exec($command, $output, $returnCode); // 输出结果 echo "Return Code: " . $returnCode . "\n"; echo "Output:\n"; foreach ($output as $line) { echo $line . "\n"; } $result = $returnCode; if ($result) { return Result::success($result); } else { return Result::error('更新输出失败'); } } public function updateWebMove($data) { //更改到dir目录, $result = WebsiteTemplate::where('id', $data['id'])->update($data); if ($result) { return Result::success($result); } else { return Result::error('更新移动失败'); } } public function runWeb($data) { //运行网站, 先检查是否有配置文件, pm2启动 $result = WebsiteTemplate::where('id', $data['id'])->update($data); if ($result) { return Result::success($result); } else { return Result::error('运行失败'); } } public function stopWeb($data) { $result = WebsiteTemplate::where('id', $data['id'])->update($data); if ($result) { return Result::success($result); } else { return Result::error('停止失败'); } } } class bt_api { private $BT_KEY; // 只声明,不初始化 private $BT_PANEL; // 只声明,不初始化 //如果希望多台面板,可以在实例化对象时,将面板地址与密钥传入 public function __construct($bt_panel = null, $bt_key = null) { // 在构造函数中使用 env() 初始化 $this->BT_KEY = $bt_key ?? env('BT_KEY', 'RN0wTXLHpKKBXXxyDJoovHMpBhyLVFWZ'); $this->BT_PANEL = $bt_panel ?? env('BT_PANEL', 'http://192.168.1.234:18888'); } //示例取面板日志 public function GetLogs() { //拼接URL地址 $url = $this->BT_PANEL . '/data?action=getData'; //准备POST数据 $p_data = $this->GetKeyData(); //取签名 $p_data['table'] = 'logs'; $p_data['limit'] = 10; $p_data['tojs'] = 'test'; //请求面板接口 $result = $this->HttpPostCookie($url, $p_data); //解析JSON数据 $data = json_decode($result, true); return $data; } public function addSite(array $siteData): array { //拼接URL地址 $url = $this->BT_PANEL . '/site?action=AddSite'; //准备POST数据 $p_data = $this->GetKeyData(); //取签名 // Merge site data with key data $p_data = array_merge($p_data, $siteData); //请求面板接口 $result = $this->HttpPostCookie($url, $p_data); //解析JSON数据 $data = json_decode($result, true); return $data; } /** * 检查删除数据 * ids [21] */ public function checkDelSite(array $siteData): array { //拼接URL地址 $url = $this->BT_PANEL . '/site?action=check_del_data'; //准备POST数据 $p_data = $this->GetKeyData(); //取签名 // Merge site data with key data $p_data = array_merge($p_data, $siteData); //请求面板接口 $result = $this->HttpPostCookie($url, $p_data); //解析JSON数据 $data = json_decode($result, true); return $data; } /** * 删除网站 * id 21 * webname t2.lj * path 1 */ public function delSite(array $siteData): array { //拼接URL地址 $url = $this->BT_PANEL . '/site?action=DeleteSite'; //准备POST数据 $p_data = $this->GetKeyData(); //取签名 // Merge site data with key data $p_data = array_merge($p_data, $siteData); //请求面板接口 $result = $this->HttpPostCookie($url, $p_data); //解析JSON数据 $data = json_decode($result, true); return $data; } /** * 构造带有签名的关联数组 */ private function GetKeyData() { $now_time = time(); $p_data = array( 'request_token' => md5($now_time . '' . md5($this->BT_KEY)), 'request_time' => $now_time ); return $p_data; } /** * 发起POST请求 * @param String $url 目标网填,带http:// * @param Array|String $data 欲提交的数据 * @return string */ private function HttpPostCookie($url, $data, $timeout = 60) { //定义cookie保存位置 $cookie_file = './' . md5($this->BT_PANEL) . '.cookie'; if (!file_exists($cookie_file)) { $fp = fopen($cookie_file, 'w+'); fclose($fp); } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $output = curl_exec($ch); curl_close($ch); return $output; } }