| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393 |
- <?php
- namespace App\JsonRpc;
- use Hyperf\RpcServer\Annotation\RpcService;
- use App\Tools\Result;
- use Hyperf\DbConnection\Db;
- //mocle
- use App\Model\Complaint;
- use App\Model\JobEnum;
- use App\Model\User;
- use App\Model\webisitecategory;
- use App\Model\WebsiteTemplate;
- use Hyperf\Coroutine\Coroutine;
- use Hyperf\Redis\Redis;
- use Hyperf\Di\Annotation\Inject;
- use function Hyperf\Support\env;
- use Symfony\Component\Filesystem\Filesystem;
- use Hyperf\Paginator\Paginator;
- #[RpcService(name: "ClientService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
- class ClientService implements ClientServiceInterface
- {
- #[Inject]
- protected Redis $redis;
- /**
- * @param array $data
- * @return array
- */
- public function test(array $data): array
- {
- var_dump($data, '-------1--------');
- $time = date('Y-m-d H:i:s', time());
- $data = [
- 'code' => 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;
- var_dump("网站id:", $website_id);
- $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 .= $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;
- }
- }
- var_dump($cat_1st_arr, 'cat_1st_arr');
- $time2 = microtime(true);
- $time_ = ($time2 - $time1);
- var_dump($time2, $time1, $time_, '返回路由需要多少时间');
- //获取
- // $getpage = $data['base']['getpage'] ?? 'index';
- $getpage = $data['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;
- };
- // $page = $componentData['page'];
- // $pageSize = $componentData['pageSize'];
- // $listType = $componentData['listType'];
- $category_id = $componentData['category_id'] ?? 0;
- //此处应该有值
- $imgSize = $componentData['imgSize'];
- $textSize = $componentData['textSize'];
- $level = $componentData['level'];
- $child = $componentData['child'];
- //获取子级
- if ($child) {
- $child_id = $componentData['category_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')->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')->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')
- ->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')
- ->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] ?? '=====';
- }
- }
- }
- //将查询到的数据放到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 ?? [];
- //判断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':
- $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)
- // ->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)
- ->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':
- $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)
- ->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')
- ->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')
- ->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':
- $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 indexData1(array $data): array
- {
- $time1 = microtime(true);
- // 解析模板数据
- $templateData = json_decode($data['template'], true);
- $website_id = $templateData['base']['website_id'] ?? 2;
- $componentList = $templateData['template']['index'][0]['componentList'];
- // 1️⃣ 获取网站分类信息
- $websiteInfo = Db::table('website_category')
- ->where('website_id', $website_id)
- ->get()
- ->toArray();
- // 构建索引和拼音映射
- $websiteInfoIndexed = array_column($websiteInfo, null, 'category_id');
- $categoryIds = array_column($websiteInfo, 'category_id');
- $aliasPinyins = array_column($websiteInfo, 'aLIas_pinyin');
- $cat_arr = array_combine($categoryIds, $aliasPinyins);
- // 构建 catiall 和 cat_1st_arr
- $catiall = [];
- $cat_1st_arr = [];
- foreach ($websiteInfo as $item) {
- $arrId = json_decode($item->category_arr_id);
- $pinyin_str = implode('/', array_map(fn($id) => $cat_arr[$id], $arrId));
- $pinyin_str = rtrim($pinyin_str, '/');
- $catiall[$item->category_id][] = $pinyin_str;
- $websiteInfoIndexed[$item->category_id]->pinyin = $pinyin_str;
- if ($item->pid == 0) {
- $cat_1st_arr[$item->category_id] = [];
- } else {
- $cat_1st_arr[$item->pid][] = $item->category_id;
- }
- }
- // 2️⃣ 提前聚合所有需要查询的 category_id 和限制参数
- $allCatIds = [];
- $imgLimits = [];
- $txtLimits = [];
- $childImgLimits = [];
- $childTxtLimits = [];
- foreach ($componentList as $item) {
- $componentData = $item['componentData'];
- $category_id = $componentData['category_id'] ?? 0;
- if ($category_id > 0) {
- $allCatIds[] = $category_id;
- $imgLimits[$category_id] = $componentData['imgSize'] ?? 0;
- $txtLimits[$category_id] = $componentData['textSize'] ?? 0;
- if (!empty($cat_1st_arr[$category_id])) {
- $allCatIds = array_merge($allCatIds, $cat_1st_arr[$category_id]);
- }
- }
- // 子级
- $child = $componentData['child'] ?? null;
- if ($child && isset($child['id']) && $child['id'] > 0) {
- $child_id = $child['id'];
- $childImgLimits[$child_id] = $child['imgSize'] ?? 0;
- $childTxtLimits[$child_id] = $child['textSize'] ?? 0;
- }
- }
- $allCatIds = array_unique(array_filter($allCatIds));
- // 3️⃣ 一次性查询所有 article 数据
- $allArticles = Db::table('article')
- ->whereIn('catid', $allCatIds)
- ->where('status', 1)
- ->get()
- ->toArray();
- // 构建按 imgurl 分类的文章集合
- $articlesByCat = ['img' => [], 'txt' => []];
- foreach ($allArticles as $article) {
- if (!empty($article->imgurl)) {
- $articlesByCat['img'][$article->catid][] = $article;
- } else {
- $articlesByCat['txt'][$article->catid][] = $article;
- }
- }
- // 4️⃣ 使用协程并发处理每个组件
- $results = [];
- foreach ($componentList as $key => $value) {
- $componentKey = $key;
- $componentData = $value['componentData'];
- Coroutine::create(function () use (&$results, $componentKey, $componentData, $websiteInfoIndexed, $cat_1st_arr, $catiall, $articlesByCat, $imgLimits, $txtLimits, $childImgLimits, $childTxtLimits) {
- $result = [];
- $category_id = $componentData['category_id'] ?? 0;
- $imgLimit = $imgLimits[$category_id] ?? 0;
- $txtLimit = $txtLimits[$category_id] ?? 0;
- if ($category_id > 0) {
- // 图文数据
- $imgArticles = array_slice($articlesByCat['img'][$category_id] ?? [], 0, $imgLimit);
- foreach ($imgArticles as &$v) {
- $v->pinyin = $catiall[$v->catid][0] ?? '';
- }
- // 纯文字数据
- $textArticles = array_slice($articlesByCat['txt'][$category_id] ?? [], 0, $txtLimit);
- foreach ($textArticles as &$v) {
- $v->pinyin = $catiall[$v->catid][0] ?? '';
- }
- $result['imgnum'] = $imgArticles;
- $result['textnum'] = $textArticles;
- $result['alias'] = $websiteInfoIndexed[$category_id]['alias'] ?? '';
- $result['pinyin'] = $websiteInfoIndexed[$category_id]['pinyin'] ?? '';
- $result['category_id'] = $category_id;
- }
- // 子级处理
- $child = $componentData['child'] ?? null;
- if ($child && isset($child['id']) && $child['id'] > 0) {
- $child_id = $child['id'];
- $c_imgLimit = $childImgLimits[$child_id] ?? 0;
- $c_txtLimit = $childTxtLimits[$child_id] ?? 0;
- $childImg = array_slice($articlesByCat['img'][$child_id] ?? [], 0, $c_imgLimit);
- foreach ($childImg as &$v) {
- $v->pinyin = $catiall[$v->catid][0] ?? '';
- }
- $childText = array_slice($articlesByCat['txt'][$child_id] ?? [], 0, $c_txtLimit);
- foreach ($childText as &$v) {
- $v->pinyin = $catiall[$v->catid][0] ?? '';
- }
- $result['child'] = [
- 'alias' => $websiteInfoIndexed[$child_id]['alias'] ?? '',
- 'pinyin' => $websiteInfoIndexed[$child_id]['pinyin'] ?? '',
- 'category_id' => $child_id,
- 'textnum' => $childText,
- 'imgnum' => $childImg,
- 'all_childcat' => array_map(fn($v) => $websiteInfoIndexed[$v], $cat_1st_arr[$category_id] ?? []),
- ];
- }
- $results[$componentKey] = $result;
- });
- }
- // 等待所有协程完成(生产环境应使用 WaitGroup)
- usleep(200000); // 示例中简单等待
- // 5️⃣ 回填结果到 componentList
- foreach ($componentList as $key => &$component) {
- $componentData = $component['componentData'];
- $category_id = $componentData['category_id'] ?? 0;
- if (isset($results[$key])) {
- $component['componentData']['data'] = $results[$key];
- }
- }
- // 6️⃣ 返回最终数据
- $templateData['template']['index'][0]['componentList'] = $componentList;
- return $templateData;
- }
- 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('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 updateWebConfig1($data)
- {
- // 先根据
- // 根据是否测试环境配置不同 的vue项目的 配置文件
- //获取根目录下 yuan 源代码,开始处理
- //获取目前目录,获取根目录
- $root = dirname(dirname(__DIR__));
- $yuan = $root . '/yuan';
- //取出envAPP_ENV=dev
- $APP_ENV = env('APP_ENV');
- switch ($APP_ENV) {
- case 'dev':
- $file = $yuan . '/plugins/category.ts';
- break;
- case 'prod':
- $file = $yuan . '/plugins/category.ts';
- break;
- default:
- $file = $yuan . '/plugins/category.ts';
- }
- //处理文件plugins\category.ts
- //找到 export default defineNuxtPlugin((nuxtApp) => {,处理到文件结尾
- /*
- export default defineNuxtPlugin((nuxtApp) => {
- //master 环境
- nuxtApp.provide('pageNav', navMaster)
- //pre 环境
- // nuxtApp.provide('pageNav', navPre)
- })
- */
- if (0 == 1) {
- $content = file_get_contents($file);
- $content = str_replace("nuxtApp.provide('pageNav', navPre)", "//nuxtApp.provide('pageNav', navPre)", $content);
- $content = str_replace("//nuxtApp.provide('pageNav', navMaster)", "nuxtApp.provide('pageNav', navMaster)", $content);
- //写入文件
- $res = file_put_contents($file, $content);
- var_dump('已经处理/plugins/category.ts');
- //处理文件\plugins\globals.js
- $file = $yuan . '/plugins/globals.js';
- $content = file_get_contents($file);
- // 1️⃣ 将没有注释的 nuxtApp.provide(...) 行加上注释
- $content = preg_replace('/^(?!\s*\/\/)(?=.*nuxtApp\.provide)/m', '// ', $content);
- // 2️⃣ 找到 "//乡村网正式环境" 下面的注释行并取消注释
- $lines = explode("\n", $content);
- $output = [];
- $foundSection = false;
- foreach ($lines as $line) {
- // 判断是否进入目标区域
- if (preg_match('/\/\/乡村网正式环境/', $line)) {
- $foundSection = true;
- $output[] = $line;
- continue;
- }
- // 在目标区域内,去掉注释
- if ($foundSection && preg_match('/^\s*\/\/\s*(nuxtApp\.provide)/', $line, $matches)) {
- $output[] = preg_replace('/^\s*\/\/\s*/', '', $line, 1);
- } else {
- // 如果遇到下一个注释块或空白行,结束当前处理
- if ($foundSection && preg_match('/^\s*$/', $line)) {
- $foundSection = false;
- }
- $output[] = $line;
- }
- }
- // 合并回字符串
- $newContent = implode("\n", $output);
- // 写入文件
- $res1 = file_put_contents($file, $newContent);
- var_dump($res1, '已经处理/plugins/globals.js');
- //处理文件\plugins\request.js
- $file = $yuan . '/plugins/request.js';
- $content = file_get_contents($file);
- $lines = explode("\n", $content);
- $in_prod = false;
- $in_pre = false;
- for ($i = 0; $i < count($lines); $i++) {
- $line = $lines[$i];
- // 匹配 "//正式环境"
- if (preg_match('/\/\/正式环境/', $line)) {
- $in_prod = true;
- $in_pre = false;
- continue;
- }
- // 匹配 "//pre环境"
- if (preg_match('/\/\/pre环境/', $line)) {
- $in_pre = true;
- $in_prod = false;
- continue;
- }
- // 处理正式环境下的两行:去注释
- if ($in_prod && $i + 0 <= count($lines) && $i + 1 <= count($lines)) {
- for ($j = $i + 0; $j <= $i + 1; $j++) {
- if (isset($lines[$j])) {
- $lines[$j] = preg_replace('/^\s*\/\/\s*/', '', $lines[$j]);
- }
- }
- $in_prod = false; // 只处理一次
- }
- // 处理 pre 环境下的两行:添加注释
- if ($in_pre && $i + 0 <= count($lines) && $i + 1 <= count($lines)) {
- for ($j = $i + 1; $j <= $i + 2; $j++) {
- if (isset($lines[$j]) && !preg_match('/^\s*\/\//', $lines[$j])) {
- // $lines[$j] = '// ' . $lines[$j];
- }
- }
- $in_pre = false; // 只处理一次
- }
- }
- // 写回文件
- $res2 = file_put_contents($file, implode("\n", $lines));
- var_dump($res2, '已经处理/plugins/request.js');
- echo "✅ 环境配置切换完成!";
- //第三步 定位到yuan目录,执行 npm run build 生成output目录
- chdir($yuan);
- $APP_ENV = env('APP_ENV');
- $res3 = exec("npm run build");
- var_dump($res, '执行 npm run build');
- echo "✅ 构建完成!";
- //第四步
- //修改 .output\server\chunks\routes\index.mjs ,
- // 根据网站的id的port更改端口
- //第五步
- //x
- $file4 = $yuan . '/.output/server/chunks/routes/index.mjs';
- $content = file_get_contents($file4);
- // 使用正则将 "destr(...) || 3e3" 替换为 "destr(...) || 9000"
- $content = preg_replace(
- '/(destr$[^)]+$$\s*\|\|\s*)3e3/i',
- '$19000',
- $content
- );
- // 写回文件
- file_put_contents($file, $content);
- }
- //第六步 yuan压缩成压缩包,复制到/hyperf-skeleton/web1/,解压
- $dir_yuan = $yuan . '/.output/';
- // 移动到web 没有就创建
- $dir_web = '/hyperf-skeleton/' . '/web1/';
- if (!is_dir($dir_web)) {
- mkdir($dir_web, 0755, true);
- }
- $fs = new Filesystem();
- $fs->rename($dir_yuan, $dir_web . 'output', true);
- var_dump('已经移动到/hyperf-skeleton/web1/output');
- $result = [
- 'res' => $res,
- 'res1' => $res1,
- 'res2' => $res2,
- 'res3' => $res3,
- 'root' => $root,
- 'yuan' => $yuan,
- 'APP_ENV' => $APP_ENV,
- 'website_id' => $data['website_id'],
- ];
- if ($result) {
- return Result::success($result);
- } else {
- return Result::error('更新配置失败');
- }
- }
- 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 = "RN0wTXLHpKKBXXxyDJoovHMpBhyLVFWZ"; //接口密钥
- private $BT_PANEL = "http://192.168.1.234:18888"; //面板地址
- //如果希望多台面板,可以在实例化对象时,将面板地址与密钥传入
- public function __construct($bt_panel = null, $bt_key = null)
- {
- if ($bt_panel) $this->BT_PANEL = $bt_panel;
- if ($bt_key) $this->BT_KEY = $bt_key;
- }
- //示例取面板日志
- 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;
- }
- }
|