12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046 |
- <?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;
- #[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);
- $data = json_decode($data['template'], true);
- $wetbsite_id = $data['base']['website_id'] ?? 2;
- //设置缓存
- $websiteInfoCacheKey = "awebsite:category:{$wetbsite_id}";
- $websiteInfo = $this->redis->get($websiteInfoCacheKey); //false;
- if ($websiteInfo === false) {
- // Redis 中没有缓存,从数据库中查询
- $websiteInfo = Db::table('website_category')->where('website_id', $wetbsite_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', $wetbsite_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) {
- // $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;
- }
- }
- $time2 = microtime(true);
- $time_ = ($time2 - $time1);
- var_dump($time2, $time1, $time_, '返回路由需要多少时间');
- //获取
- $getpage = $data['base']['getpage'] ?? 'index';
- // 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':
- $componetList = $data['template']['index'][0]['componentList'];
- //计算一级的所有子级;组成一个数据,key是一级的category_id,value是所有的子级数据,加上个二级的pinyin属性,放的是catiall的数据
- foreach ($componetList as $key => $value) {
- //取出componentData
- $componentData = $value['componentData'];
- // $page = $componentData['page'];
- // $pageSize = $componentData['pageSize'];
- $listType = $componentData['listType'];
- $category_id = $componentData['category_id'];
- $imgSize = $componentData['imgSize'];
- $textSize = $componentData['textSize'];
- $level = $componentData['level'];
- $child = $componentData['child'];
- //获取子级
- if ($child) {
- }
- $child_id = $child['id'];
- $child_imgSize = $child['imgSize'];
- $child_textSize = $child['textSize'];
- // 拼音就是路由----
- unset($listType['pinyin']);
- //查询查询数据,返回到data字段里,根据这几个
- if ($category_id > 0) {
- // 第一次查询:imgurl 不为空的数据
- $imgArticles = Db::table('article')
- ->leftJoin('article_ignore', function ($join) use ($wetbsite_id) {
- $join->on('article.id', '=', 'article_ignore.article_id')
- ->where('article_ignore.website_id', '=', $wetbsite_id);
- })
- ->whereNull('article_ignore.article_id')
- // ->whereIn('catid', [$category_id])
- ->whereIn('catid', $temp_arr = array_merge([$category_id], $cat_1st_arr[$category_id]))
- ->where('status', 1)
- ->whereNotNull('imgurl')
- // ignore_ids 是空 或者[],或者不存在website_id的数据 json
- // ->where('ignore_ids', 'not like', '%' . $wetbsite_id . '%')
- ->where('imgurl', '!=', '')
- ->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')
- // ->whereIn('catid', [$category_id])
- ->whereIn('catid', $temp_arr = array_merge([$category_id], $cat_1st_arr[$category_id]))
- ->where('status', 1)
- // ->where('ignore_ids', 'not like', '%' . $wetbsite_id . '%')
- // ->whereNull('imgurl')
- ->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 不为空的数据
- $imgArticles = Db::table('article')
- // ->where('level', 'like', "%$level%")
- // ->whereJsonContains('level', [$level])
- ->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 为空的数据
- $textArticles = Db::table('article')
- ->where('status', 1)
- // ->where('level', 'like', "%$level%")
- // ->whereJsonContains('level', [$level])
- ->whereRaw('MATCH(level_text) AGAINST(? IN NATURAL LANGUAGE MODE)', [$level])
- ->whereIn('catid', $categoryIds)
- ->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;
- //判断child
- if ($child_id > 0) {
- //取出子级的imgurl 不为空的数据
- $child_imgArticles = Db::table('article')
- ->where('catid', $child_id)
- ->whereNotNull('imgurl')
- ->limit($child_imgSize)
- ->orderBy('updated_at', 'desc')
- ->get()
- ->toArray();
- //取出子级的imgurl 为空的数据
- $child_textArticles = Db::table('article')
- ->where('catid', $child_id)
- ->whereNull('imgurl')
- ->limit($child_textSize)
- ->orderBy('updated_at', 'desc')
- ->get()
- ->toArray();
- //将查询到的数据放到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;
- //取出自己
- $all_childcat = $cat_1st_arr[$category_id];
- $processedChildCat = array_map(function ($v) use ($websiteInfoIndexed) {
- // 从 $websiteInfoIndexed 中获取对应的数据
- $info = $websiteInfoIndexed[$v];
- // 返回一个包含所需信息的数组
- 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 ?? ['出错'],
- ];
- }, $all_childcat);
- $componetList[$key]['componentData']['data']['child']['all_childcat'] = $processedChildCat ?? [];
- }
- }
- //将修改后的数据放到template里
- $data['template']['index'][0]['componentList'] = $componetList;
- 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不为空的数据
- $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为空的数据
- $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')
- ->get();
- if ($result) {
- return Result::success($result);
- } else {
- return Result::error('获取列表失败');
- }
- }
- public function updateWebConfig($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)
- {
- //根据源程序, 复制出来,更改配置文件,打包,
- $result = WebsiteTemplate::where('id', $data['id'])->update($data);
- 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('停止失败');
- }
- }
- }
|