ClientService.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. <?php
  2. namespace App\JsonRpc;
  3. use Hyperf\RpcServer\Annotation\RpcService;
  4. use App\Tools\Result;
  5. use Hyperf\DbConnection\Db;
  6. //mocle
  7. use App\Model\Complaint;
  8. use App\Model\JobEnum;
  9. use App\Model\User;
  10. use App\Model\webisitecategory;
  11. use Hyperf\Coroutine\Coroutine;
  12. use Hyperf\Redis\Redis;
  13. use Hyperf\Di\Annotation\Inject;
  14. #[RpcService(name: "ClientService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  15. class ClientService implements ClientServiceInterface
  16. {
  17. #[Inject]
  18. protected Redis $redis;
  19. /**
  20. * @param array $data
  21. * @return array
  22. */
  23. public function test(array $data): array
  24. {
  25. var_dump($data, '-------1--------');
  26. $time = date('Y-m-d H:i:s', time());
  27. $data = [
  28. 'code' => 200,
  29. 'msg' => 'success',
  30. 'data' => [
  31. 'time' => $time,
  32. 'data' => $data,
  33. ]
  34. ];
  35. //mysql直接查询连接数
  36. $activeConnections = Db::select("SHOW STATUS LIKE 'Threads_connected'");
  37. // $activeConnectionsCount = $activeConnections[0]->Value ?? 0;
  38. // $users = Db::select('SELECT * FROM article;');
  39. return ['code' => 200, 'msg' => 'success', 'data' => $activeConnections];
  40. }
  41. public function indexData(array $data): array
  42. {
  43. $time1 = microtime(true);
  44. $data = json_decode($data['template'], true);
  45. $wetbsite_id = $data['base']['website_id'] ?? 2;
  46. //设置缓存
  47. $websiteInfoCacheKey = "awebsite:category:{$wetbsite_id}";
  48. $websiteInfo = $this->redis->get($websiteInfoCacheKey); //false;
  49. if ($websiteInfo === false) {
  50. // Redis 中没有缓存,从数据库中查询
  51. $websiteInfo = Db::table('website_category')->where('website_id', $wetbsite_id)->get()->toArray();
  52. // 将查询结果存入 Redis,设置缓存时间为 3600 秒(1 小时)
  53. $this->redis->setex($websiteInfoCacheKey, 3600, json_encode($websiteInfo));
  54. } else {
  55. // 从 Redis 中获取的数据需要反序列化
  56. $websiteInfo = json_decode($websiteInfo, true);
  57. }
  58. $websiteInfo = Db::table('website_category')->where('website_id', $wetbsite_id)->get()->toArray();
  59. //取出website
  60. var_dump($websiteInfo, 'websiteInfo');
  61. //取出category_id 对应的数据
  62. $websiteInfoIndexed = array_column($websiteInfo, null, 'category_id');
  63. // 取出category_id 对应的aLIas_pinyin
  64. $categoryIds = array_column($websiteInfo, 'category_id');
  65. $aliasPinyins = array_column($websiteInfo, 'aLIas_pinyin');
  66. $cat_arr = array_combine($categoryIds, $aliasPinyins);
  67. // var_dump($cat_arr, 'cat_arr');
  68. //根据 category_arr_id 组合出 aLIas_pinyin
  69. $catiall = [];
  70. //一级所有子级的记录
  71. $cat_1st_arr = [];
  72. foreach ($websiteInfo as $key => $value) {
  73. //算出路由拼音
  74. $category_arr_id = json_decode($value->category_arr_id);
  75. $pinyin_str = '';
  76. foreach ($category_arr_id as $k => $v) {
  77. $pinyin_str .= $cat_arr[$v] . '/';
  78. }
  79. $pinyin_str = rtrim($pinyin_str, '/');
  80. $catiall[$value->category_id][] = $pinyin_str;
  81. // $cat_id = $value->category_id;
  82. $websiteInfoIndexed[$value->category_id]->pinyin = $pinyin_str;
  83. // 算出一级 并且算出子级
  84. if ($value->pid == 0) {
  85. $cat_1st_arr[$value->category_id] = [];
  86. } else {
  87. $cat_1st_arr[$value->pid][] = $value->category_id;
  88. }
  89. }
  90. $time2 = microtime(true);
  91. $time_ = ($time2 - $time1);
  92. var_dump($time2, $time1, $time_, '返回路由需要多少时间');
  93. //获取
  94. $getpage = $data['base']['getpage'] ?? 'index';
  95. // index = 首页
  96. // class = 频道页
  97. // list = 列表页
  98. // article = 详情页
  99. // search = 搜索页
  100. // aboutList = 底部导航列表页
  101. // aboutArticle = 底部导航详情页
  102. // return $cat_1st_arr;
  103. // return $cat_1st_arr;
  104. // var_dump($cat_1st_arr, 'cat_1st_arr');//一级所有子级的记录
  105. // var_dump($catiall, 'catiall');//拼音
  106. $templateData = [];
  107. switch ($getpage) {
  108. case 'index':
  109. $componetList = $data['template']['index'][0]['componentList'];
  110. //计算一级的所有子级;组成一个数据,key是一级的category_id,value是所有的子级数据,加上个二级的pinyin属性,放的是catiall的数据
  111. foreach ($componetList as $key => $value) {
  112. //取出componentData
  113. $componentData = $value['componentData'];
  114. // $page = $componentData['page'];
  115. // $pageSize = $componentData['pageSize'];
  116. $listType = $componentData['listType'];
  117. $category_id = $componentData['category_id'];
  118. $imgSize = $componentData['imgSize'];
  119. $textSize = $componentData['textSize'];
  120. $level = $componentData['level'];
  121. $child = $componentData['child'];
  122. //获取子级
  123. if ($child) {
  124. }
  125. $child_id = $child['id'];
  126. $child_imgSize = $child['imgSize'];
  127. $child_textSize = $child['textSize'];
  128. // 拼音就是路由----
  129. unset($listType['pinyin']);
  130. //查询查询数据,返回到data字段里,根据这几个
  131. if ($category_id > 0) {
  132. // 第一次查询:imgurl 不为空的数据
  133. $imgArticles = Db::table('article')
  134. // ->whereIn('catid', [$category_id])
  135. ->whereIn('catid', $temp_arr = array_merge([$category_id], $cat_1st_arr[$category_id]))
  136. ->where('status', 1)
  137. ->whereNotNull('imgurl')
  138. ->limit($imgSize)
  139. ->orderBy('updated_at', 'desc')
  140. ->get()
  141. ->toArray();
  142. //拼接上拼音
  143. foreach ($imgArticles as $k => $v) {
  144. $imgArticles[$k]->pinyin = $catiall[$v->catid][0];
  145. }
  146. // 第二次查询:imgurl 为空的数据
  147. $textArticles = Db::table('article')
  148. // ->whereIn('catid', [$category_id])
  149. ->whereIn('catid', $temp_arr = array_merge([$category_id], $cat_1st_arr[$category_id]))
  150. ->where('status', 1)
  151. ->whereNull('imgurl')
  152. ->limit($textSize)
  153. ->orderBy('updated_at', 'desc')
  154. ->get()
  155. ->toArray();
  156. //拼接上拼音
  157. foreach ($textArticles as $k => $v) {
  158. $textArticles[$k]->pinyin = $catiall[$v->catid][0];
  159. }
  160. }
  161. var_dump($level, 'level');
  162. if ($level > 0) {
  163. // 取出一级的所有子级
  164. //取出所有的子级的imgurl 不为空的数据
  165. $imgArticles = Db::table('article')
  166. // ->where('level', 'like', "%$level%")
  167. // ->whereJsonContains('level', [$level])
  168. ->whereRaw('MATCH(level_text) AGAINST(? IN NATURAL LANGUAGE MODE)', [$level])
  169. ->where('status', 1)
  170. ->whereIn('catid', $categoryIds)
  171. ->whereNotNull('imgurl')
  172. ->limit($imgSize)
  173. ->orderBy('updated_at', 'desc')
  174. ->get()
  175. ->toArray();
  176. //拼接上拼音
  177. foreach ($imgArticles as $k => $v) {
  178. $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  179. }
  180. //取出所有的子级的imgurl 为空的数据
  181. $textArticles = Db::table('article')
  182. ->where('status', 1)
  183. // ->where('level', 'like', "%$level%")
  184. // ->whereJsonContains('level', [$level])
  185. ->whereRaw('MATCH(level_text) AGAINST(? IN NATURAL LANGUAGE MODE)', [$level])
  186. ->whereIn('catid', $categoryIds)
  187. ->whereNull('imgurl')
  188. ->limit($textSize)
  189. ->orderBy('updated_at', 'desc')
  190. ->get()
  191. ->toArray();
  192. //拼接上拼音
  193. foreach ($textArticles as $k => $v) {
  194. $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  195. }
  196. }
  197. //将查询到的数据放到componentData里
  198. $componetList[$key]['componentData']['data']['alias'] = $websiteInfoIndexed[$category_id]->alias ?? 'aliasnull' . $category_id;
  199. $componetList[$key]['componentData']['data']['pinyin'] = $websiteInfoIndexed[$category_id]->pinyin ?? 'aliasnull' . $category_id;
  200. $componetList[$key]['componentData']['data']['category_id'] = $category_id;
  201. $componetList[$key]['componentData']['data']['textnum'] = $textArticles;
  202. $componetList[$key]['componentData']['data']['imgnum'] = $imgArticles;
  203. //判断child
  204. if ($child_id > 0) {
  205. //取出子级的imgurl 不为空的数据
  206. $child_imgArticles = Db::table('article')
  207. ->where('catid', $child_id)
  208. ->whereNotNull('imgurl')
  209. ->limit($child_imgSize)
  210. ->orderBy('updated_at', 'desc')
  211. ->get()
  212. ->toArray();
  213. //取出子级的imgurl 为空的数据
  214. $child_textArticles = Db::table('article')
  215. ->where('catid', $child_id)
  216. ->whereNull('imgurl')
  217. ->limit($child_textSize)
  218. ->orderBy('updated_at', 'desc')
  219. ->get()
  220. ->toArray();
  221. //将查询到的数据放到componentData里
  222. $componetList[$key]['componentData']['data']['child']['alias'] = $websiteInfoIndexed[$child_id]->alias;
  223. $componetList[$key]['componentData']['data']['child']['pinyin'] = $websiteInfoIndexed[$child_id]->pinyin;
  224. $componetList[$key]['componentData']['data']['child']['category_id'] = $child_id;
  225. $componetList[$key]['componentData']['data']['child']['textnum'] = $child_textArticles;
  226. $componetList[$key]['componentData']['data']['child']['imgnum'] = $child_imgArticles;
  227. //取出自己
  228. $all_childcat = $cat_1st_arr[$category_id];
  229. $processedChildCat = array_map(function ($v) use ($websiteInfoIndexed) {
  230. // 从 $websiteInfoIndexed 中获取对应的数据
  231. $info = $websiteInfoIndexed[$v];
  232. // 返回一个包含所需信息的数组
  233. return [
  234. 'pinyin' => $info->pinyin,
  235. 'alias' => $info->alias,
  236. 'category_id' => $info->category_id,
  237. 'aLIas_pinyin' => $info->aLIas_pinyin,
  238. 'pid' => $info->pid,
  239. 'cat_arr_id' => $info->category_arr_id ?? ['出错'],
  240. ];
  241. }, $all_childcat);
  242. $componetList[$key]['componentData']['data']['child']['all_childcat'] = $processedChildCat ?? [];
  243. }
  244. }
  245. //将修改后的数据放到template里
  246. $data['template']['index'][0]['componentList'] = $componetList;
  247. break;
  248. case 'class':
  249. $componetList = $data['template']['class']['data'][0]['componentList'];
  250. $class_count = count($componetList);
  251. $parent_id = $data['template']['class']['parent_id'] ?? 0;
  252. //获取子级;
  253. $child = $cat_1st_arr[$parent_id]; //获取子级
  254. // var_dump($child, 'child');
  255. //如果$child的数量小于$class_count,就填充一样多的数据给child,如果大于就截取一样的数量给child
  256. if (count($child) < $class_count) {
  257. $child = array_pad($child, $class_count, end($child) ?? 0);
  258. } elseif (count($child) > $class_count) {
  259. $child = array_slice($child, 0, $class_count);
  260. }
  261. foreach ($componetList as $key => $value) {
  262. $componentData = $value['componentData'];
  263. //取出imgSize和textSize,获取相应的文章,
  264. $category_id = $child[$key] ?? 0;
  265. $imgSize = $componentData['imgSize'] ?? 0;
  266. $textSize = $componentData['textSize'] ?? 0;
  267. $listType = $componentData['listType'] ?? [];
  268. var_dump($listType, 'before unset listType');
  269. // 定义要移除的元素数组
  270. $valuesToRemove = ['category_name', 'pinyin'];
  271. // 循环移除每个元素
  272. foreach ($valuesToRemove as $value) {
  273. while (($key = array_search($value, $listType)) !== false) {
  274. // 移除该元素
  275. unset($listType[$key]);
  276. }
  277. }
  278. var_dump($listType, 'listType');
  279. //child
  280. $child = $componentData['child'] ?? [];
  281. $data_class = [];
  282. //查询imgurl不为空的数据
  283. $imgArticles = Db::table('article')
  284. ->where('catid', $category_id)
  285. ->whereNotNull('imgurl')
  286. ->limit($imgSize)
  287. // ->select($listType)
  288. ->orderBy('updated_at', 'desc')
  289. ->get()
  290. ->toArray();
  291. //拼接上拼音
  292. foreach ($imgArticles as $k => $v) {
  293. $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  294. }
  295. //查询imgurl为空的数据
  296. $textArticles = Db::table('article')
  297. ->where('catid', $category_id)
  298. ->whereNull('imgurl')
  299. ->limit($textSize)
  300. ->orderBy('updated_at', 'desc')
  301. ->get()
  302. ->toArray();
  303. //拼接上拼音
  304. foreach ($textArticles as $k => $v) {
  305. $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  306. }
  307. $data_class['alias'] = $websiteInfoIndexed[$category_id]->alias ?? 'aliasnull' . $category_id;
  308. $data_class['pinyin'] = $websiteInfoIndexed[$category_id]->pinyin ?? 'aliasnull' . $category_id;
  309. $data_class['category_id'] = $category_id;
  310. $data_class['textnum'] = $textArticles;
  311. $data_class['imgnum'] = $imgArticles;
  312. $data['data'] = $data_class;
  313. }
  314. break;
  315. case 'list':
  316. $componetListdata = $data['template']['list']['data'];
  317. $category_id = $data['template']['list']['category_id'] ?? 0;
  318. foreach ($componetListdata as $k => $v) {
  319. if (!isset($v['componentList']) || !is_array($v['componentList'])) {
  320. continue; // 如果 componentList 不存在或不是数组,跳过当前循环
  321. }
  322. $componentList = &$data['template']['list']['data'][$k]['componentList'];
  323. var_dump(count($componentList), 'componentList111');
  324. foreach ($componentList as $key => $value) {
  325. $componentData = $value['componentData'];
  326. if (isset($value['componentData']['category_id'])) {
  327. $value['componentData']['category_id'] = $category_id;
  328. $page = $componentData['papageTypege']['page'] ?? 1;
  329. $pageSize = $componentData['papageTypege']['pageSize'] ?? 10;
  330. $listType = $componentData['listType'];
  331. $articals = Db::table('article')
  332. ->where('catid', $category_id)
  333. ->where('status', 1)
  334. ->orderBy('updated_at', 'desc')
  335. ->get();
  336. // ->paginate($pageSize, ['*'], 'page', $page);
  337. //拼接上拼音
  338. foreach ($articals as $k => $v) {
  339. $articals[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  340. }
  341. $value['componentData']['data'] = $articals;
  342. }
  343. var_dump(isset($value['componentData']['level']), $key, '---------------------------------------');
  344. if (isset($value['componentData']['level'])) {
  345. //取出imgSize和textSize,获取相应的文章,
  346. $imgSize = $componentData['imgnum'] ?? 0;
  347. $textSize = $componentData['textnum'] ?? 0;
  348. $listType = $componentData['listType'] ?? [];
  349. // 定义要移除的元素数组
  350. $valuesToRemove = ['category_name', 'pinyin'];
  351. // 循环移除每个元素
  352. foreach ($valuesToRemove as $value) {
  353. while (($key = array_search($value, $listType)) !== false) {
  354. // 移除该元素
  355. unset($listType[$key]);
  356. }
  357. }
  358. //查询imgurl不为空的数据
  359. $imgArticles = Db::table('article')
  360. ->where('catid', $category_id)
  361. ->whereNotNull('imgurl')
  362. ->limit($imgSize)
  363. ->orderBy('updated_at', 'desc')
  364. ->get()
  365. ->toArray();
  366. //拼接上拼音
  367. foreach ($imgArticles as $k => $v) {
  368. $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  369. }
  370. //查询imgurl为空的数据
  371. $textArticles = Db::table('article')
  372. ->where('catid', $category_id)
  373. ->whereNull('imgurl')
  374. ->limit($textSize)
  375. ->orderBy('updated_at', 'desc')
  376. ->get()
  377. ->toArray();
  378. //拼接上拼音
  379. foreach ($textArticles as $k => $v) {
  380. $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  381. }
  382. //将查询到的数据放到componentData里
  383. $componetList[$key]['componentData']['data']['alias'] = $websiteInfoIndexed[$category_id]->alias ?? 'aliasnull' . $category_id;
  384. $componetList[$key]['componentData']['data']['pinyin'] = $websiteInfoIndexed[$category_id]->pinyin ?? 'aliasnull' . $category_id;
  385. $componetList[$key]['componentData']['data']['category_id'] = $category_id;
  386. $componetList[$key]['componentData']['data']['textnum'] = $textArticles;
  387. $componetList[$key]['componentData']['data']['imgnum'] = $imgArticles;
  388. //将修改后的数据放到template里
  389. // $data['template']['list']['data'][0]['componentList'] = $componetList;
  390. }
  391. }
  392. }
  393. break;
  394. case 'article':
  395. $article_id = 51434; //假设这是文章ID
  396. $componetListdata = &$data['template']['article']['data'];
  397. var_dump($categoryIds, '----------------');
  398. // $category_id = $data['template']['article']['category_id'] ?? 0;
  399. foreach ($componetListdata as $k => $v) {
  400. foreach ($v['componentList'] as $key => $value) {
  401. $componentData = $value['componentData'];
  402. if (isset($value['componentData']['article_id'])) {
  403. $value['componentData']['article_id'] = $article_id;
  404. // $page = $componentData['papageTypege']['page'] ?? 1;
  405. // $pageSize = $componentData['papageTypege']['pageSize'] ?? 10;
  406. $listType = $componentData['listType'];
  407. //查询数据
  408. $articals = Db::table('article')
  409. ->where('id', $article_id)
  410. ->where('status', 1)
  411. ->leftJoin('article_data', 'article.id', '=', 'article_data.article_id')
  412. ->select('article.*', 'article_data.content')
  413. ->orderBy('updated_at', 'desc')
  414. ->get();
  415. //拼接上拼音
  416. $value['componentData']['data'] = $articals;
  417. }
  418. if (isset($value['componentData']['level'])) {
  419. //:1:头条 2:轮播图 3:推荐图 4:最新新闻;5:推荐新闻;6:热点资讯 7:自动关联
  420. //4 update 5 30天之内 6 库中
  421. $level = $componentData['level'] ?? 0;
  422. $imgSize = $componentData['imgnum'] ?? 0;
  423. $textSize = $componentData['textnum'] ?? 0;
  424. if (in_array($level, [1, 2, 3, 6])) {
  425. //查询imgurl不为空的数据
  426. $imgArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNotNull('imgurl')
  427. ->where('status', 1)
  428. ->whereRaw('MATCH(level_text) AGAINST(? IN NATURAL LANGUAGE MODE)', [$level])
  429. ->limit($imgSize)->orderBy('updated_at', 'desc')->get()->toArray();
  430. //拼接上拼音
  431. foreach ($imgArticles as $k => $v) {
  432. $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  433. }
  434. //查询imgurl为空的数据
  435. $textArticles = Db::table('article')->where('catid', $categoryIds)->whereNull('imgurl')
  436. ->where('status', 1)
  437. ->whereRaw('MATCH(level_text) AGAINST(? IN NATURAL LANGUAGE MODE)', [$level])
  438. ->limit($textSize)->orderBy('updated_at', 'desc')->get()->toArray();
  439. //拼接上拼音
  440. foreach ($textArticles as $k => $v) {
  441. $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  442. }
  443. } else if ($level == 4) {
  444. //查询imgurl不为空的数据
  445. $imgArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNotNull('imgurl')
  446. ->where('status', 1)
  447. ->limit($imgSize)->orderBy('updated_at', 'desc')->get()->toArray();
  448. //拼接上拼音
  449. if (!empty($imgArticles)) {
  450. foreach ($imgArticles as $k => $v) {
  451. $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  452. }
  453. }
  454. //查询imgurl为空的数据
  455. $textArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNull('imgurl')
  456. ->where('status', 1)
  457. ->limit($textSize)->orderBy('updated_at', 'desc')->get()->toArray();
  458. //拼接上拼音
  459. var_dump('---4------');
  460. if (!empty($textArticles)) {
  461. foreach ($textArticles as $k => $v) {
  462. $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  463. }
  464. }
  465. } else if ($level == 5) {
  466. var_dump($value['componentData']['level'], 'level');
  467. //查询imgurl不为空的数据
  468. $imgArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNotNull('imgurl')
  469. ->where('status', 1)
  470. //最近三十天
  471. ->where('updated_at', '>=', date('Y-m-d H:i:s', strtotime('-30 days')))
  472. ->limit($imgSize)->orderBy('updated_at', 'desc')->get()->toArray();
  473. //拼接上拼音
  474. if (!empty($imgArticles)) {
  475. foreach ($imgArticles as $k => $v) {
  476. $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  477. }
  478. }
  479. //查询imgurl为空的数据
  480. $textArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNull('imgurl')
  481. ->where('status', 1)
  482. ->where('updated_at', '>=', date('Y-m-d H:i:s', strtotime('-30 days')))
  483. ->limit($textSize)->orderBy('updated_at', 'desc')->get()->toArray();
  484. //拼接上拼音
  485. var_dump('---5------');
  486. if (!empty($textArticles)) {
  487. foreach ($textArticles as $k => $v) {
  488. $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  489. }
  490. }
  491. } else if ($level == 7) {
  492. //获取artical的keyword
  493. $keword = DB::table('artical')->where('id', $article_id)->select('keyword')->get();
  494. $kewordl = $keword[0]->keyword;
  495. $kewordl = explode(',', $kewordl);
  496. //筛选出符合条件的article来
  497. $imgArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNotNull('imgurl')
  498. ->where('status', 1)
  499. // ->whereJsonContains('keyword', $kewordl)
  500. ->whereRaw('MATCH(keyword) AGAINST(? IN NATURAL LANGUAGE MODE)', [$kewordl])
  501. ->limit($imgSize)->orderBy('updated_at', 'desc')->get()->toArray();
  502. //拼接上拼音
  503. foreach ($imgArticles as $k => $v) {
  504. $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  505. }
  506. $textArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNull('imgurl')
  507. ->where('status', 1)
  508. // ->whereJsonContains('keyword', $kewordl)
  509. ->whereRaw('MATCH(keyword) AGAINST(? IN NATURAL LANGUAGE MODE)', [$kewordl])
  510. ->limit($textSize)->orderBy('updated_at', 'desc')->get()->toArray();
  511. //拼接上拼音
  512. foreach ($textArticles as $k => $v) {
  513. $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  514. }
  515. }
  516. }
  517. $value['componentData']['data']['imgnum'] = $imgArticles ?? [];
  518. $value['componentData']['data']['textnum'] = $textArticles ?? [];
  519. }
  520. }
  521. break;
  522. case 'search':
  523. break;
  524. case 'aboutList':
  525. $fact_id = 7;
  526. $componetList = &$data['template']['aboutList']['componentData'];
  527. $componetList['fact_id'] = $fact_id;
  528. $content = Db::table('footer_content')
  529. ->where('fcat_id', $fact_id)
  530. ->orderBy('updated_at', 'desc')
  531. ->get()
  532. ->toArray();
  533. $componetList['data'] = $content;
  534. break;
  535. case 'aboutArticle':
  536. $fact_id = 7;
  537. $componetList = &$data['template']['aboutList']['componentData'];
  538. $componetList['fact_id'] = $fact_id;
  539. $content = Db::table('footer_content')
  540. ->where('id', $fact_id)
  541. ->orderBy('updated_at', 'desc')
  542. ->get()
  543. ->toArray();
  544. $componetList['data'] = $content;
  545. break;
  546. default:
  547. $componetList = [];
  548. break;
  549. }
  550. return $data;
  551. }
  552. public function indexData1(array $data): array
  553. {
  554. $time1 = microtime(true);
  555. // 解析模板数据
  556. $templateData = json_decode($data['template'], true);
  557. $website_id = $templateData['base']['website_id'] ?? 2;
  558. $componentList = $templateData['template']['index'][0]['componentList'];
  559. // 1️⃣ 获取网站分类信息
  560. $websiteInfo = Db::table('website_category')
  561. ->where('website_id', $website_id)
  562. ->get()
  563. ->toArray();
  564. // 构建索引和拼音映射
  565. $websiteInfoIndexed = array_column($websiteInfo, null, 'category_id');
  566. $categoryIds = array_column($websiteInfo, 'category_id');
  567. $aliasPinyins = array_column($websiteInfo, 'aLIas_pinyin');
  568. $cat_arr = array_combine($categoryIds, $aliasPinyins);
  569. // 构建 catiall 和 cat_1st_arr
  570. $catiall = [];
  571. $cat_1st_arr = [];
  572. foreach ($websiteInfo as $item) {
  573. $arrId = json_decode($item->category_arr_id);
  574. $pinyin_str = implode('/', array_map(fn($id) => $cat_arr[$id], $arrId));
  575. $pinyin_str = rtrim($pinyin_str, '/');
  576. $catiall[$item->category_id][] = $pinyin_str;
  577. $websiteInfoIndexed[$item->category_id]->pinyin = $pinyin_str;
  578. if ($item->pid == 0) {
  579. $cat_1st_arr[$item->category_id] = [];
  580. } else {
  581. $cat_1st_arr[$item->pid][] = $item->category_id;
  582. }
  583. }
  584. // 2️⃣ 提前聚合所有需要查询的 category_id 和限制参数
  585. $allCatIds = [];
  586. $imgLimits = [];
  587. $txtLimits = [];
  588. $childImgLimits = [];
  589. $childTxtLimits = [];
  590. foreach ($componentList as $item) {
  591. $componentData = $item['componentData'];
  592. $category_id = $componentData['category_id'] ?? 0;
  593. if ($category_id > 0) {
  594. $allCatIds[] = $category_id;
  595. $imgLimits[$category_id] = $componentData['imgSize'] ?? 0;
  596. $txtLimits[$category_id] = $componentData['textSize'] ?? 0;
  597. if (!empty($cat_1st_arr[$category_id])) {
  598. $allCatIds = array_merge($allCatIds, $cat_1st_arr[$category_id]);
  599. }
  600. }
  601. // 子级
  602. $child = $componentData['child'] ?? null;
  603. if ($child && isset($child['id']) && $child['id'] > 0) {
  604. $child_id = $child['id'];
  605. $childImgLimits[$child_id] = $child['imgSize'] ?? 0;
  606. $childTxtLimits[$child_id] = $child['textSize'] ?? 0;
  607. }
  608. }
  609. $allCatIds = array_unique(array_filter($allCatIds));
  610. // 3️⃣ 一次性查询所有 article 数据
  611. $allArticles = Db::table('article')
  612. ->whereIn('catid', $allCatIds)
  613. ->where('status', 1)
  614. ->get()
  615. ->toArray();
  616. // 构建按 imgurl 分类的文章集合
  617. $articlesByCat = ['img' => [], 'txt' => []];
  618. foreach ($allArticles as $article) {
  619. if (!empty($article->imgurl)) {
  620. $articlesByCat['img'][$article->catid][] = $article;
  621. } else {
  622. $articlesByCat['txt'][$article->catid][] = $article;
  623. }
  624. }
  625. // 4️⃣ 使用协程并发处理每个组件
  626. $results = [];
  627. foreach ($componentList as $key => $value) {
  628. $componentKey = $key;
  629. $componentData = $value['componentData'];
  630. Coroutine::create(function () use (&$results, $componentKey, $componentData, $websiteInfoIndexed, $cat_1st_arr, $catiall, $articlesByCat, $imgLimits, $txtLimits, $childImgLimits, $childTxtLimits) {
  631. $result = [];
  632. $category_id = $componentData['category_id'] ?? 0;
  633. $imgLimit = $imgLimits[$category_id] ?? 0;
  634. $txtLimit = $txtLimits[$category_id] ?? 0;
  635. if ($category_id > 0) {
  636. // 图文数据
  637. $imgArticles = array_slice($articlesByCat['img'][$category_id] ?? [], 0, $imgLimit);
  638. foreach ($imgArticles as &$v) {
  639. $v->pinyin = $catiall[$v->catid][0] ?? '';
  640. }
  641. // 纯文字数据
  642. $textArticles = array_slice($articlesByCat['txt'][$category_id] ?? [], 0, $txtLimit);
  643. foreach ($textArticles as &$v) {
  644. $v->pinyin = $catiall[$v->catid][0] ?? '';
  645. }
  646. $result['imgnum'] = $imgArticles;
  647. $result['textnum'] = $textArticles;
  648. $result['alias'] = $websiteInfoIndexed[$category_id]['alias'] ?? '';
  649. $result['pinyin'] = $websiteInfoIndexed[$category_id]['pinyin'] ?? '';
  650. $result['category_id'] = $category_id;
  651. }
  652. // 子级处理
  653. $child = $componentData['child'] ?? null;
  654. if ($child && isset($child['id']) && $child['id'] > 0) {
  655. $child_id = $child['id'];
  656. $c_imgLimit = $childImgLimits[$child_id] ?? 0;
  657. $c_txtLimit = $childTxtLimits[$child_id] ?? 0;
  658. $childImg = array_slice($articlesByCat['img'][$child_id] ?? [], 0, $c_imgLimit);
  659. foreach ($childImg as &$v) {
  660. $v->pinyin = $catiall[$v->catid][0] ?? '';
  661. }
  662. $childText = array_slice($articlesByCat['txt'][$child_id] ?? [], 0, $c_txtLimit);
  663. foreach ($childText as &$v) {
  664. $v->pinyin = $catiall[$v->catid][0] ?? '';
  665. }
  666. $result['child'] = [
  667. 'alias' => $websiteInfoIndexed[$child_id]['alias'] ?? '',
  668. 'pinyin' => $websiteInfoIndexed[$child_id]['pinyin'] ?? '',
  669. 'category_id' => $child_id,
  670. 'textnum' => $childText,
  671. 'imgnum' => $childImg,
  672. 'all_childcat' => array_map(fn($v) => $websiteInfoIndexed[$v], $cat_1st_arr[$category_id] ?? []),
  673. ];
  674. }
  675. $results[$componentKey] = $result;
  676. });
  677. }
  678. // 等待所有协程完成(生产环境应使用 WaitGroup)
  679. usleep(200000); // 示例中简单等待
  680. // 5️⃣ 回填结果到 componentList
  681. foreach ($componentList as $key => &$component) {
  682. $componentData = $component['componentData'];
  683. $category_id = $componentData['category_id'] ?? 0;
  684. if (isset($results[$key])) {
  685. $component['componentData']['data'] = $results[$key];
  686. }
  687. }
  688. // 6️⃣ 返回最终数据
  689. $templateData['template']['index'][0]['componentList'] = $componentList;
  690. return $templateData;
  691. }
  692. }