123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754 |
- <?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 Hyperf\Coroutine\Coroutine;
- use Hyperf\Redis\Redis;
- use Hyperf\Di\Annotation\Inject;
- #[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 {
- $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, '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')
- // ->whereIn('catid', [$category_id])
- ->whereIn('catid', $temp_arr = array_merge([$category_id], $cat_1st_arr[$category_id]))
- ->where('status', 1)
- ->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')
- // ->whereIn('catid', [$category_id])
- ->whereIn('catid', $temp_arr = array_merge([$category_id], $cat_1st_arr[$category_id]))
- ->where('status', 1)
- ->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')
- ->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;
- }
- }
|