ClientService.php 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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 App\Model\WebsiteTemplate;
  12. use Hyperf\Coroutine\Coroutine;
  13. use Hyperf\Redis\Redis;
  14. use Hyperf\Di\Annotation\Inject;
  15. use function Hyperf\Support\env;
  16. use Symfony\Component\Filesystem\Filesystem;
  17. #[RpcService(name: "ClientService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  18. class ClientService implements ClientServiceInterface
  19. {
  20. #[Inject]
  21. protected Redis $redis;
  22. /**
  23. * @param array $data
  24. * @return array
  25. */
  26. public function test(array $data): array
  27. {
  28. var_dump($data, '-------1--------');
  29. $time = date('Y-m-d H:i:s', time());
  30. $data = [
  31. 'code' => 200,
  32. 'msg' => 'success',
  33. 'data' => [
  34. 'time' => $time,
  35. 'data' => $data,
  36. ]
  37. ];
  38. //mysql直接查询连接数
  39. $activeConnections = Db::select("SHOW STATUS LIKE 'Threads_connected'");
  40. // $activeConnectionsCount = $activeConnections[0]->Value ?? 0;
  41. // $users = Db::select('SELECT * FROM article;');
  42. return ['code' => 200, 'msg' => 'success', 'data' => $activeConnections];
  43. }
  44. public function indexData(array $data): array
  45. {
  46. $time1 = microtime(true);
  47. // 调整获取方式
  48. // 根据网站id获取,从website_template中获取 website_template
  49. $website_id = $data['website_id'] ?? 2;
  50. var_dump("网站id:", $website_id);
  51. $template = Db::table('website_template')->where('website_id', $website_id)->first();
  52. var_dump("模板:", $template);
  53. $template = $template->template_data;
  54. $data = json_decode($template, true);
  55. // $data = json_decode($data['template'], true);
  56. // $website_id = $data['base']['website_id'] ?? 2;
  57. //设置缓存
  58. $websiteInfoCacheKey = "awebsite:category:{$website_id}";
  59. $websiteInfo = $this->redis->get($websiteInfoCacheKey); //false;
  60. if ($websiteInfo === false) {
  61. // Redis 中没有缓存,从数据库中查询
  62. $websiteInfo = Db::table('website_category')->where('website_id', $website_id)->get()->toArray();
  63. // 将查询结果存入 Redis,设置缓存时间为 3600 秒(1 小时)
  64. $this->redis->setex($websiteInfoCacheKey, 3600, json_encode($websiteInfo));
  65. } else {
  66. // 从 Redis 中获取的数据需要反序列化
  67. $websiteInfo = json_decode($websiteInfo, true);
  68. }
  69. $websiteInfo = Db::table('website_category')->where('website_id', $website_id)->get()->toArray();
  70. //取出website
  71. var_dump($websiteInfo, 'websiteInfo');
  72. //取出category_id 对应的数据
  73. $websiteInfoIndexed = array_column($websiteInfo, null, 'category_id');
  74. // 取出category_id 对应的aLIas_pinyin
  75. $categoryIds = array_column($websiteInfo, 'category_id');
  76. $aliasPinyins = array_column($websiteInfo, 'aLIas_pinyin');
  77. $cat_arr = array_combine($categoryIds, $aliasPinyins);
  78. // var_dump($cat_arr, 'cat_arr');
  79. //根据 category_arr_id 组合出 aLIas_pinyin
  80. $catiall = [];
  81. //一级所有子级的记录
  82. $cat_1st_arr = [];
  83. foreach ($websiteInfo as $key => $value) {
  84. //算出路由拼音
  85. $category_arr_id = json_decode($value->category_arr_id);
  86. $pinyin_str = '';
  87. foreach ($category_arr_id as $k => $v) {
  88. $pinyin_str .= $cat_arr[$v] . '/';
  89. }
  90. $pinyin_str = rtrim($pinyin_str, '/');
  91. $catiall[$value->category_id][] = $pinyin_str;
  92. // $cat_id = $value->category_id;
  93. $websiteInfoIndexed[$value->category_id]->pinyin = $pinyin_str;
  94. // 算出一级 并且算出子级
  95. if ($value->pid == 0) {
  96. if (empty($cat_1st_arr[$value->category_id]))
  97. $cat_1st_arr[$value->category_id] = [];
  98. } else {
  99. if ($value->pid == 11) {
  100. var_dump($value->category_id, 'value->category_id');
  101. }
  102. $cat_1st_arr[$value->pid][] = $value->category_id;
  103. }
  104. }
  105. var_dump($cat_1st_arr, 'cat_1st_arr');
  106. $time2 = microtime(true);
  107. $time_ = ($time2 - $time1);
  108. var_dump($time2, $time1, $time_, '返回路由需要多少时间');
  109. //获取
  110. // $getpage = $data['base']['getpage'] ?? 'index';
  111. $getpage = $data['getpage'] ?? 'index';
  112. var_dump($getpage, 'leixing ');
  113. // index = 首页
  114. // class = 频道页
  115. // list = 列表页
  116. // article = 详情页
  117. // search = 搜索页
  118. // aboutList = 底部导航列表页
  119. // aboutArticle = 底部导航详情页
  120. // return $cat_1st_arr;
  121. // return $cat_1st_arr;
  122. // var_dump($cat_1st_arr[11], 'cat_1st_arr'); //一级所有子级的记录
  123. // var_dump($catiall, 'catiall');//拼音
  124. $templateData = [];
  125. switch ($getpage) {
  126. case 'index':
  127. $ceng = $data['template']['index'];
  128. var_dump($ceng, 'ceng================');
  129. foreach ($ceng as $k_ceng => $v_ceng) {
  130. $componetList = $v_ceng['componentList'];
  131. //计算一级的所有子级;组成一个数据,key是一级的category_id,value是所有的子级数据,加上个二级的pinyin属性,放的是catiall的数据
  132. foreach ($componetList as $key => $value) {
  133. //取出componentData
  134. $componentData = $value['componentData'];
  135. if (!$componentData) {
  136. continue;
  137. };
  138. // $page = $componentData['page'];
  139. // $pageSize = $componentData['pageSize'];
  140. // $listType = $componentData['listType'];
  141. $category_id = $componentData['category_id'] ?? 0;
  142. //此处应该有值
  143. $imgSize = $componentData['imgSize'];
  144. $textSize = $componentData['textSize'];
  145. $level = $componentData['level'];
  146. $child = $componentData['child'];
  147. //获取子级
  148. if ($child) {
  149. $child_id = $componentData['category_id'] ?? 0;
  150. $child_imgSize = $child['imgSize'] ?? 0;
  151. $child_textSize = $child['textSize'] ?? 0;
  152. }
  153. // 拼音就是路由----
  154. // unset($listType['pinyin']);
  155. //查询查询数据,返回到data字段里,根据这几个
  156. if ($category_id > 0) {
  157. var_dump($category_id, '------------------category_id');
  158. var_dump($cat_1st_arr, '-----------1-------2');
  159. // 第一次查询:imgurl 不为空的数据
  160. if ($imgSize > 0) {
  161. $imgArticles = Db::table('article')
  162. ->leftJoin('article_ignore', function ($join) use ($website_id) {
  163. $join->on('article.id', '=', 'article_ignore.article_id')->where('article_ignore.website_id', '=', $website_id);
  164. })->whereNull('article_ignore.article_id')->whereIn('catid', $temp_arr = array_merge([$category_id], $cat_1st_arr[$category_id]))->where('status', 1)
  165. ->whereNotNull('imgurl')->where('imgurl', '!=', '')->limit($imgSize)->orderBy('updated_at', 'desc')->get()->toArray();
  166. //拼接上拼音
  167. foreach ($imgArticles as $k => $v) {
  168. $imgArticles[$k]->pinyin = $catiall[$v->catid][0];
  169. }
  170. }
  171. // 第二次查询:imgurl 为空的数据
  172. if ($textSize > 0) {
  173. $textArticles = Db::table('article')
  174. ->leftJoin('article_ignore', function ($join) use ($website_id) {
  175. $join->on('article.id', '=', 'article_ignore.article_id')->where('article_ignore.website_id', '=', $website_id);
  176. })->whereNull('article_ignore.article_id')->whereIn('catid', $temp_arr = array_merge([$category_id], $cat_1st_arr[$category_id]))->where('status', 1)
  177. ->where('status', 1)->limit($textSize)->orderBy('updated_at', 'desc')->get()->toArray();
  178. //拼接上拼音
  179. foreach ($textArticles as $k => $v) {
  180. $textArticles[$k]->pinyin = $catiall[$v->catid][0];
  181. }
  182. }
  183. }
  184. var_dump($level, 'level');
  185. if ($level > 0) {
  186. // 取出一级的所有子级
  187. //取出所有的子级的imgurl 不为空的数据
  188. if ($imgSize > 0) {
  189. $imgArticles = Db::table('article')
  190. ->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();
  191. //拼接上拼音
  192. foreach ($imgArticles as $k => $v) {
  193. $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  194. }
  195. }
  196. //取出所有的子级的imgurl 为空的数据 ->whereNull('imgurl')
  197. if ($textSize > 0) {
  198. $textArticles = Db::table('article')->where('status', 1)->whereRaw('MATCH(level_text) AGAINST(? IN NATURAL LANGUAGE MODE)', [$level])->whereIn('catid', $categoryIds)->limit($textSize)->orderBy('updated_at', 'desc')->get()->toArray();
  199. //拼接上拼音
  200. foreach ($textArticles as $k => $v) {
  201. $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  202. }
  203. }
  204. }
  205. //将查询到的数据放到componentData里
  206. $componetList[$key]['componentData']['data']['alias'] = $websiteInfoIndexed[$category_id]->alias ?? 'aliasnull' . $category_id;
  207. $componetList[$key]['componentData']['data']['pinyin'] = $websiteInfoIndexed[$category_id]->pinyin ?? 'aliasnull' . $category_id;
  208. $componetList[$key]['componentData']['data']['category_id'] = $category_id;
  209. $componetList[$key]['componentData']['data']['textnum'] = $textArticles ?? [];
  210. $componetList[$key]['componentData']['data']['imgnum'] = $imgArticles ?? [];
  211. //判断child
  212. var_dump($child_id, '--------child_id---');
  213. if ($child_id > 0) {
  214. //取出子级的imgurl 不为空的数据
  215. if ($child_imgSize > 0) {
  216. $child_imgArticles = Db::table('article')->where('catid', $child_id)
  217. ->where('status', 1)
  218. ->whereNotNull('imgurl')->limit($child_imgSize)->orderBy('updated_at', 'desc')->get()->toArray();
  219. } else {
  220. $child_imgArticles = [];
  221. }
  222. //取出子级的imgurl 为空的数据
  223. if ($child_textSize > 0) {
  224. $child_textArticles = Db::table('article')
  225. ->where('status', 1)
  226. ->where('catid', $child_id)->whereNull('imgurl')->limit($child_textSize)->orderBy('updated_at', 'desc')->get()->toArray();
  227. } else {
  228. $child_textArticles = [];
  229. }
  230. //将查询到的数据放到componentData里
  231. $componetList[$key]['componentData']['data']['child']['alias'] = $websiteInfoIndexed[$child_id]->alias;
  232. $componetList[$key]['componentData']['data']['child']['pinyin'] = $websiteInfoIndexed[$child_id]->pinyin;
  233. $componetList[$key]['componentData']['data']['child']['category_id'] = $child_id;
  234. $componetList[$key]['componentData']['data']['child']['textnum'] = $child_textArticles;
  235. $componetList[$key]['componentData']['data']['child']['imgnum'] = $child_imgArticles;
  236. $componetList[$key]['componentData']['data']['child']['is_url'] = $websiteInfoIndexed[$child_id]->is_url ?? 0;
  237. //取出自己
  238. $all_childcat = $cat_1st_arr[$category_id];
  239. $componetList[$key]['componentData']['data']['child']['children_count'] = count($all_childcat);
  240. $processedChildCat = array_map(function ($v) use ($websiteInfoIndexed, $cat_1st_arr) {
  241. // 从 $websiteInfoIndexed 中获取对应的数据
  242. $info = $websiteInfoIndexed[$v];
  243. var_dump($info, $v, '-------child循环--------');
  244. // 返回一个包含所需信息的数组
  245. return [
  246. 'pinyin' => $info->pinyin,
  247. 'alias' => $info->alias,
  248. 'category_id' => $info->category_id,
  249. 'aLIas_pinyin' => $info->aLIas_pinyin,
  250. 'pid' => $info->pid,
  251. 'cat_arr_id' => $info->category_arr_id ?? ['出错'],
  252. 'is_url' => $info->is_url ?? 0,
  253. 'children_count' => isset($cat_1st_arr[$v]) ? count($cat_1st_arr[$v]) : 0
  254. ];
  255. }, $all_childcat);
  256. $componetList[$key]['componentData']['data']['child']['all_childcat'] = $processedChildCat ?? [];
  257. }
  258. }
  259. $data['template']['index'][$k_ceng]['componentList'] = $componetList;
  260. }
  261. //将修改后的数据放到template里
  262. break;
  263. case 'class':
  264. $componetList = $data['template']['class']['data'][0]['componentList'];
  265. $class_count = count($componetList);
  266. $parent_id = $data['template']['class']['parent_id'] ?? 0;
  267. //获取子级;
  268. $child = $cat_1st_arr[$parent_id]; //获取子级
  269. // var_dump($child, 'child');
  270. //如果$child的数量小于$class_count,就填充一样多的数据给child,如果大于就截取一样的数量给child
  271. if (count($child) < $class_count) {
  272. $child = array_pad($child, $class_count, end($child) ?? 0);
  273. } elseif (count($child) > $class_count) {
  274. $child = array_slice($child, 0, $class_count);
  275. }
  276. foreach ($componetList as $key => $value) {
  277. $componentData = $value['componentData'];
  278. //取出imgSize和textSize,获取相应的文章,
  279. $category_id = $child[$key] ?? 0;
  280. $imgSize = $componentData['imgSize'] ?? 0;
  281. $textSize = $componentData['textSize'] ?? 0;
  282. $listType = $componentData['listType'] ?? [];
  283. var_dump($listType, 'before unset listType');
  284. // 定义要移除的元素数组
  285. $valuesToRemove = ['category_name', 'pinyin'];
  286. // 循环移除每个元素
  287. foreach ($valuesToRemove as $value) {
  288. while (($key = array_search($value, $listType)) !== false) {
  289. // 移除该元素
  290. unset($listType[$key]);
  291. }
  292. }
  293. var_dump($listType, 'listType');
  294. //child
  295. $child = $componentData['child'] ?? [];
  296. $data_class = [];
  297. //查询imgurl不为空的数据
  298. $imgArticles = Db::table('article')
  299. ->where('catid', $category_id)
  300. ->whereNotNull('imgurl')
  301. ->limit($imgSize)
  302. // ->select($listType)
  303. ->orderBy('updated_at', 'desc')
  304. ->get()
  305. ->toArray();
  306. //拼接上拼音
  307. foreach ($imgArticles as $k => $v) {
  308. $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  309. }
  310. //查询imgurl为空的数据
  311. $textArticles = Db::table('article')
  312. ->where('catid', $category_id)
  313. ->whereNull('imgurl')
  314. ->limit($textSize)
  315. ->orderBy('updated_at', 'desc')
  316. ->get()
  317. ->toArray();
  318. //拼接上拼音
  319. foreach ($textArticles as $k => $v) {
  320. $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  321. }
  322. $data_class['alias'] = $websiteInfoIndexed[$category_id]->alias ?? 'aliasnull' . $category_id;
  323. $data_class['pinyin'] = $websiteInfoIndexed[$category_id]->pinyin ?? 'aliasnull' . $category_id;
  324. $data_class['category_id'] = $category_id;
  325. $data_class['textnum'] = $textArticles ?? [];
  326. $data_class['imgnum'] = $imgArticles ?? [];
  327. $data['data'] = $data_class;
  328. }
  329. break;
  330. case 'list':
  331. $componetListdata = $data['template']['list']['data'];
  332. $category_id = $data['template']['list']['category_id'] ?? 0;
  333. foreach ($componetListdata as $k => $v) {
  334. if (!isset($v['componentList']) || !is_array($v['componentList'])) {
  335. continue; // 如果 componentList 不存在或不是数组,跳过当前循环
  336. }
  337. $componentList = &$data['template']['list']['data'][$k]['componentList'];
  338. var_dump(count($componentList), 'componentList111');
  339. foreach ($componentList as $key => $value) {
  340. $componentData = $value['componentData'];
  341. if (isset($value['componentData']['category_id'])) {
  342. $value['componentData']['category_id'] = $category_id;
  343. $page = $componentData['papageTypege']['page'] ?? 1;
  344. $pageSize = $componentData['papageTypege']['pageSize'] ?? 10;
  345. $listType = $componentData['listType'];
  346. $articals = Db::table('article')
  347. ->where('catid', $category_id)
  348. ->where('status', 1)
  349. ->orderBy('updated_at', 'desc')
  350. ->get();
  351. // ->paginate($pageSize, ['*'], 'page', $page);
  352. //拼接上拼音
  353. foreach ($articals as $k => $v) {
  354. $articals[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  355. }
  356. $value['componentData']['data'] = $articals;
  357. }
  358. var_dump(isset($value['componentData']['level']), $key, '---------------------------------------');
  359. if (isset($value['componentData']['level'])) {
  360. //取出imgSize和textSize,获取相应的文章,
  361. $imgSize = $componentData['imgnum'] ?? 0;
  362. $textSize = $componentData['textnum'] ?? 0;
  363. $listType = $componentData['listType'] ?? [];
  364. // 定义要移除的元素数组
  365. $valuesToRemove = ['category_name', 'pinyin'];
  366. // 循环移除每个元素
  367. foreach ($valuesToRemove as $value) {
  368. while (($key = array_search($value, $listType)) !== false) {
  369. // 移除该元素
  370. unset($listType[$key]);
  371. }
  372. }
  373. //查询imgurl不为空的数据
  374. if ($imgSize > 0) {
  375. $imgArticles = Db::table('article')
  376. ->where('catid', $category_id)
  377. ->whereNotNull('imgurl')
  378. ->limit($imgSize)
  379. ->orderBy('updated_at', 'desc')
  380. ->get()
  381. ->toArray();
  382. //拼接上拼音
  383. foreach ($imgArticles as $k => $v) {
  384. $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  385. }
  386. }
  387. //查询imgurl为空的数据
  388. if ($textSize > 0) {
  389. $textArticles = Db::table('article')
  390. ->where('catid', $category_id)
  391. ->whereNull('imgurl')
  392. ->limit($textSize)
  393. ->orderBy('updated_at', 'desc')
  394. ->get()
  395. ->toArray();
  396. //拼接上拼音
  397. foreach ($textArticles as $k => $v) {
  398. $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  399. }
  400. }
  401. //将查询到的数据放到componentData里
  402. $componetList[$key]['componentData']['data']['alias'] = $websiteInfoIndexed[$category_id]->alias ?? 'aliasnull' . $category_id;
  403. $componetList[$key]['componentData']['data']['pinyin'] = $websiteInfoIndexed[$category_id]->pinyin ?? 'aliasnull' . $category_id;
  404. $componetList[$key]['componentData']['data']['category_id'] = $category_id;
  405. $componetList[$key]['componentData']['data']['textnum'] = $textArticles ?? [];
  406. $componetList[$key]['componentData']['data']['imgnum'] = $imgArticles ?? [];
  407. //将修改后的数据放到template里
  408. // $data['template']['list']['data'][0]['componentList'] = $componetList;
  409. }
  410. }
  411. }
  412. break;
  413. case 'article':
  414. $article_id = 51434; //假设这是文章ID
  415. $componetListdata = &$data['template']['article']['data'];
  416. var_dump($categoryIds, '----------------');
  417. // $category_id = $data['template']['article']['category_id'] ?? 0;
  418. foreach ($componetListdata as $k => $v) {
  419. foreach ($v['componentList'] as $key => $value) {
  420. $componentData = $value['componentData'];
  421. if (isset($value['componentData']['article_id'])) {
  422. $value['componentData']['article_id'] = $article_id;
  423. // $page = $componentData['papageTypege']['page'] ?? 1;
  424. // $pageSize = $componentData['papageTypege']['pageSize'] ?? 10;
  425. $listType = $componentData['listType'];
  426. //查询数据
  427. $articals = Db::table('article')
  428. ->where('id', $article_id)
  429. ->where('status', 1)
  430. ->leftJoin('article_data', 'article.id', '=', 'article_data.article_id')
  431. ->select('article.*', 'article_data.content')
  432. ->orderBy('updated_at', 'desc')
  433. ->get();
  434. //拼接上拼音
  435. $value['componentData']['data'] = $articals;
  436. }
  437. if (isset($value['componentData']['level'])) {
  438. //:1:头条 2:轮播图 3:推荐图 4:最新新闻;5:推荐新闻;6:热点资讯 7:自动关联
  439. //4 update 5 30天之内 6 库中
  440. $level = $componentData['level'] ?? 0;
  441. $imgSize = $componentData['imgnum'] ?? 0;
  442. $textSize = $componentData['textnum'] ?? 0;
  443. if (in_array($level, [1, 2, 3, 6])) {
  444. //查询imgurl不为空的数据
  445. $imgArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNotNull('imgurl')
  446. ->where('status', 1)
  447. ->whereRaw('MATCH(level_text) AGAINST(? IN NATURAL LANGUAGE MODE)', [$level])
  448. ->limit($imgSize)->orderBy('updated_at', 'desc')->get()->toArray();
  449. //拼接上拼音
  450. foreach ($imgArticles as $k => $v) {
  451. $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  452. }
  453. //查询imgurl为空的数据
  454. $textArticles = Db::table('article')->where('catid', $categoryIds)->whereNull('imgurl')
  455. ->where('status', 1)
  456. ->whereRaw('MATCH(level_text) AGAINST(? IN NATURAL LANGUAGE MODE)', [$level])
  457. ->limit($textSize)->orderBy('updated_at', 'desc')->get()->toArray();
  458. //拼接上拼音
  459. foreach ($textArticles as $k => $v) {
  460. $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  461. }
  462. } else if ($level == 4) {
  463. //查询imgurl不为空的数据
  464. $imgArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNotNull('imgurl')
  465. ->where('status', 1)
  466. ->limit($imgSize)->orderBy('updated_at', 'desc')->get()->toArray();
  467. //拼接上拼音
  468. if (!empty($imgArticles)) {
  469. foreach ($imgArticles as $k => $v) {
  470. $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  471. }
  472. }
  473. //查询imgurl为空的数据
  474. $textArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNull('imgurl')
  475. ->where('status', 1)
  476. ->limit($textSize)->orderBy('updated_at', 'desc')->get()->toArray();
  477. //拼接上拼音
  478. var_dump('---4------');
  479. if (!empty($textArticles)) {
  480. foreach ($textArticles as $k => $v) {
  481. $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  482. }
  483. }
  484. } else if ($level == 5) {
  485. var_dump($value['componentData']['level'], 'level');
  486. //查询imgurl不为空的数据
  487. $imgArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNotNull('imgurl')
  488. ->where('status', 1)
  489. //最近三十天
  490. ->where('updated_at', '>=', date('Y-m-d H:i:s', strtotime('-30 days')))
  491. ->limit($imgSize)->orderBy('updated_at', 'desc')->get()->toArray();
  492. //拼接上拼音
  493. if (!empty($imgArticles)) {
  494. foreach ($imgArticles as $k => $v) {
  495. $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  496. }
  497. }
  498. //查询imgurl为空的数据
  499. $textArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNull('imgurl')
  500. ->where('status', 1)
  501. ->where('updated_at', '>=', date('Y-m-d H:i:s', strtotime('-30 days')))
  502. ->limit($textSize)->orderBy('updated_at', 'desc')->get()->toArray();
  503. //拼接上拼音
  504. var_dump('---5------');
  505. if (!empty($textArticles)) {
  506. foreach ($textArticles as $k => $v) {
  507. $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  508. }
  509. }
  510. } else if ($level == 7) {
  511. //获取artical的keyword
  512. $keword = DB::table('artical')->where('id', $article_id)->select('keyword')->get();
  513. $kewordl = $keword[0]->keyword;
  514. $kewordl = explode(',', $kewordl);
  515. //筛选出符合条件的article来
  516. $imgArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNotNull('imgurl')
  517. ->where('status', 1)
  518. // ->whereJsonContains('keyword', $kewordl)
  519. ->whereRaw('MATCH(keyword) AGAINST(? IN NATURAL LANGUAGE MODE)', [$kewordl])
  520. ->limit($imgSize)->orderBy('updated_at', 'desc')->get()->toArray();
  521. //拼接上拼音
  522. foreach ($imgArticles as $k => $v) {
  523. $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  524. }
  525. $textArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNull('imgurl')
  526. ->where('status', 1)
  527. // ->whereJsonContains('keyword', $kewordl)
  528. ->whereRaw('MATCH(keyword) AGAINST(? IN NATURAL LANGUAGE MODE)', [$kewordl])
  529. ->limit($textSize)->orderBy('updated_at', 'desc')->get()->toArray();
  530. //拼接上拼音
  531. foreach ($textArticles as $k => $v) {
  532. $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  533. }
  534. }
  535. }
  536. $value['componentData']['data']['imgnum'] = $imgArticles ?? [];
  537. $value['componentData']['data']['textnum'] = $textArticles ?? [];
  538. }
  539. }
  540. break;
  541. case 'search':
  542. break;
  543. case 'aboutList':
  544. $fact_id = 7;
  545. $componetList = &$data['template']['aboutList']['componentData'];
  546. $componetList['fact_id'] = $fact_id;
  547. $content = Db::table('footer_content')
  548. ->where('fcat_id', $fact_id)
  549. ->orderBy('updated_at', 'desc')
  550. ->get()
  551. ->toArray();
  552. $componetList['data'] = $content;
  553. break;
  554. case 'aboutArticle':
  555. $fact_id = 7;
  556. $componetList = &$data['template']['aboutList']['componentData'];
  557. $componetList['fact_id'] = $fact_id;
  558. $content = Db::table('footer_content')
  559. ->where('id', $fact_id)
  560. ->orderBy('updated_at', 'desc')
  561. ->get()
  562. ->toArray();
  563. $componetList['data'] = $content;
  564. break;
  565. default:
  566. $componetList = [];
  567. break;
  568. }
  569. return $data;
  570. }
  571. public function indexData1(array $data): array
  572. {
  573. $time1 = microtime(true);
  574. // 解析模板数据
  575. $templateData = json_decode($data['template'], true);
  576. $website_id = $templateData['base']['website_id'] ?? 2;
  577. $componentList = $templateData['template']['index'][0]['componentList'];
  578. // 1️⃣ 获取网站分类信息
  579. $websiteInfo = Db::table('website_category')
  580. ->where('website_id', $website_id)
  581. ->get()
  582. ->toArray();
  583. // 构建索引和拼音映射
  584. $websiteInfoIndexed = array_column($websiteInfo, null, 'category_id');
  585. $categoryIds = array_column($websiteInfo, 'category_id');
  586. $aliasPinyins = array_column($websiteInfo, 'aLIas_pinyin');
  587. $cat_arr = array_combine($categoryIds, $aliasPinyins);
  588. // 构建 catiall 和 cat_1st_arr
  589. $catiall = [];
  590. $cat_1st_arr = [];
  591. foreach ($websiteInfo as $item) {
  592. $arrId = json_decode($item->category_arr_id);
  593. $pinyin_str = implode('/', array_map(fn($id) => $cat_arr[$id], $arrId));
  594. $pinyin_str = rtrim($pinyin_str, '/');
  595. $catiall[$item->category_id][] = $pinyin_str;
  596. $websiteInfoIndexed[$item->category_id]->pinyin = $pinyin_str;
  597. if ($item->pid == 0) {
  598. $cat_1st_arr[$item->category_id] = [];
  599. } else {
  600. $cat_1st_arr[$item->pid][] = $item->category_id;
  601. }
  602. }
  603. // 2️⃣ 提前聚合所有需要查询的 category_id 和限制参数
  604. $allCatIds = [];
  605. $imgLimits = [];
  606. $txtLimits = [];
  607. $childImgLimits = [];
  608. $childTxtLimits = [];
  609. foreach ($componentList as $item) {
  610. $componentData = $item['componentData'];
  611. $category_id = $componentData['category_id'] ?? 0;
  612. if ($category_id > 0) {
  613. $allCatIds[] = $category_id;
  614. $imgLimits[$category_id] = $componentData['imgSize'] ?? 0;
  615. $txtLimits[$category_id] = $componentData['textSize'] ?? 0;
  616. if (!empty($cat_1st_arr[$category_id])) {
  617. $allCatIds = array_merge($allCatIds, $cat_1st_arr[$category_id]);
  618. }
  619. }
  620. // 子级
  621. $child = $componentData['child'] ?? null;
  622. if ($child && isset($child['id']) && $child['id'] > 0) {
  623. $child_id = $child['id'];
  624. $childImgLimits[$child_id] = $child['imgSize'] ?? 0;
  625. $childTxtLimits[$child_id] = $child['textSize'] ?? 0;
  626. }
  627. }
  628. $allCatIds = array_unique(array_filter($allCatIds));
  629. // 3️⃣ 一次性查询所有 article 数据
  630. $allArticles = Db::table('article')
  631. ->whereIn('catid', $allCatIds)
  632. ->where('status', 1)
  633. ->get()
  634. ->toArray();
  635. // 构建按 imgurl 分类的文章集合
  636. $articlesByCat = ['img' => [], 'txt' => []];
  637. foreach ($allArticles as $article) {
  638. if (!empty($article->imgurl)) {
  639. $articlesByCat['img'][$article->catid][] = $article;
  640. } else {
  641. $articlesByCat['txt'][$article->catid][] = $article;
  642. }
  643. }
  644. // 4️⃣ 使用协程并发处理每个组件
  645. $results = [];
  646. foreach ($componentList as $key => $value) {
  647. $componentKey = $key;
  648. $componentData = $value['componentData'];
  649. Coroutine::create(function () use (&$results, $componentKey, $componentData, $websiteInfoIndexed, $cat_1st_arr, $catiall, $articlesByCat, $imgLimits, $txtLimits, $childImgLimits, $childTxtLimits) {
  650. $result = [];
  651. $category_id = $componentData['category_id'] ?? 0;
  652. $imgLimit = $imgLimits[$category_id] ?? 0;
  653. $txtLimit = $txtLimits[$category_id] ?? 0;
  654. if ($category_id > 0) {
  655. // 图文数据
  656. $imgArticles = array_slice($articlesByCat['img'][$category_id] ?? [], 0, $imgLimit);
  657. foreach ($imgArticles as &$v) {
  658. $v->pinyin = $catiall[$v->catid][0] ?? '';
  659. }
  660. // 纯文字数据
  661. $textArticles = array_slice($articlesByCat['txt'][$category_id] ?? [], 0, $txtLimit);
  662. foreach ($textArticles as &$v) {
  663. $v->pinyin = $catiall[$v->catid][0] ?? '';
  664. }
  665. $result['imgnum'] = $imgArticles;
  666. $result['textnum'] = $textArticles;
  667. $result['alias'] = $websiteInfoIndexed[$category_id]['alias'] ?? '';
  668. $result['pinyin'] = $websiteInfoIndexed[$category_id]['pinyin'] ?? '';
  669. $result['category_id'] = $category_id;
  670. }
  671. // 子级处理
  672. $child = $componentData['child'] ?? null;
  673. if ($child && isset($child['id']) && $child['id'] > 0) {
  674. $child_id = $child['id'];
  675. $c_imgLimit = $childImgLimits[$child_id] ?? 0;
  676. $c_txtLimit = $childTxtLimits[$child_id] ?? 0;
  677. $childImg = array_slice($articlesByCat['img'][$child_id] ?? [], 0, $c_imgLimit);
  678. foreach ($childImg as &$v) {
  679. $v->pinyin = $catiall[$v->catid][0] ?? '';
  680. }
  681. $childText = array_slice($articlesByCat['txt'][$child_id] ?? [], 0, $c_txtLimit);
  682. foreach ($childText as &$v) {
  683. $v->pinyin = $catiall[$v->catid][0] ?? '';
  684. }
  685. $result['child'] = [
  686. 'alias' => $websiteInfoIndexed[$child_id]['alias'] ?? '',
  687. 'pinyin' => $websiteInfoIndexed[$child_id]['pinyin'] ?? '',
  688. 'category_id' => $child_id,
  689. 'textnum' => $childText,
  690. 'imgnum' => $childImg,
  691. 'all_childcat' => array_map(fn($v) => $websiteInfoIndexed[$v], $cat_1st_arr[$category_id] ?? []),
  692. ];
  693. }
  694. $results[$componentKey] = $result;
  695. });
  696. }
  697. // 等待所有协程完成(生产环境应使用 WaitGroup)
  698. usleep(200000); // 示例中简单等待
  699. // 5️⃣ 回填结果到 componentList
  700. foreach ($componentList as $key => &$component) {
  701. $componentData = $component['componentData'];
  702. $category_id = $componentData['category_id'] ?? 0;
  703. if (isset($results[$key])) {
  704. $component['componentData']['data'] = $results[$key];
  705. }
  706. }
  707. // 6️⃣ 返回最终数据
  708. $templateData['template']['index'][0]['componentList'] = $componentList;
  709. return $templateData;
  710. }
  711. public function addWeb($data)
  712. {
  713. $result = WebsiteTemplate::insertGetId($data);
  714. if ($result) {
  715. return Result::success($result);
  716. } else {
  717. return Result::error('添加失败');
  718. }
  719. }
  720. public function deleteWeb($data)
  721. {
  722. $result = WebsiteTemplate::where('id', $data['id'])->delete();
  723. if ($result) {
  724. return Result::success($result);
  725. } else {
  726. return Result::error('删除失败');
  727. }
  728. }
  729. public function updateWeb($data)
  730. {
  731. $result = WebsiteTemplate::where('id', $data['id'])->update($data);
  732. if ($result) {
  733. return Result::success($result);
  734. } else {
  735. return Result::error('更新失败');
  736. }
  737. }
  738. public function getWebInfo($data)
  739. {
  740. $result = WebsiteTemplate::where('website_template.id', $data['id'])
  741. ->leftJoin('website', 'website.id', '=', 'website_template.website_id')
  742. ->select('website_template.*', 'website.website_name as website_name', 'website.website_url as website_url')
  743. ->first();
  744. return Result::success($result);
  745. }
  746. public function getWebList($data)
  747. {
  748. var_dump($data, 'data');
  749. $where = [];
  750. if (isset($data['website_name'])) {
  751. $where[] = ['website.website_name', 'like', '%' . $data['website_name'] . '%'];
  752. }
  753. if (isset($data['status'])) {
  754. $where[] = ['website_template.status', '=', $data['status']];
  755. }
  756. $result = WebsiteTemplate::where([])
  757. ->where($where)
  758. ->leftJoin('website', 'website.id', '=', 'website_template.website_id')
  759. ->select('website_template.*', 'website.website_name as website_name', 'website.website_url as website_url')
  760. ->get();
  761. if ($result) {
  762. return Result::success($result);
  763. } else {
  764. return Result::error('获取列表失败');
  765. }
  766. }
  767. public function updateWebConfig($data)
  768. {
  769. // 先根据
  770. // 根据是否测试环境配置不同 的vue项目的 配置文件
  771. //获取根目录下 yuan 源代码,开始处理
  772. //获取目前目录,获取根目录
  773. $root = dirname(dirname(__DIR__));
  774. $yuan = $root . '/yuan';
  775. //取出envAPP_ENV=dev
  776. $APP_ENV = env('APP_ENV');
  777. switch ($APP_ENV) {
  778. case 'dev':
  779. $file = $yuan . '/plugins/category.ts';
  780. break;
  781. case 'prod':
  782. $file = $yuan . '/plugins/category.ts';
  783. break;
  784. default:
  785. $file = $yuan . '/plugins/category.ts';
  786. }
  787. //处理文件plugins\category.ts
  788. //找到 export default defineNuxtPlugin((nuxtApp) => {,处理到文件结尾
  789. /*
  790. export default defineNuxtPlugin((nuxtApp) => {
  791. //master 环境
  792. nuxtApp.provide('pageNav', navMaster)
  793. //pre 环境
  794. // nuxtApp.provide('pageNav', navPre)
  795. })
  796. */
  797. if (0 == 1) {
  798. $content = file_get_contents($file);
  799. $content = str_replace("nuxtApp.provide('pageNav', navPre)", "//nuxtApp.provide('pageNav', navPre)", $content);
  800. $content = str_replace("//nuxtApp.provide('pageNav', navMaster)", "nuxtApp.provide('pageNav', navMaster)", $content);
  801. //写入文件
  802. $res = file_put_contents($file, $content);
  803. var_dump('已经处理/plugins/category.ts');
  804. //处理文件\plugins\globals.js
  805. $file = $yuan . '/plugins/globals.js';
  806. $content = file_get_contents($file);
  807. // 1️⃣ 将没有注释的 nuxtApp.provide(...) 行加上注释
  808. $content = preg_replace('/^(?!\s*\/\/)(?=.*nuxtApp\.provide)/m', '// ', $content);
  809. // 2️⃣ 找到 "//乡村网正式环境" 下面的注释行并取消注释
  810. $lines = explode("\n", $content);
  811. $output = [];
  812. $foundSection = false;
  813. foreach ($lines as $line) {
  814. // 判断是否进入目标区域
  815. if (preg_match('/\/\/乡村网正式环境/', $line)) {
  816. $foundSection = true;
  817. $output[] = $line;
  818. continue;
  819. }
  820. // 在目标区域内,去掉注释
  821. if ($foundSection && preg_match('/^\s*\/\/\s*(nuxtApp\.provide)/', $line, $matches)) {
  822. $output[] = preg_replace('/^\s*\/\/\s*/', '', $line, 1);
  823. } else {
  824. // 如果遇到下一个注释块或空白行,结束当前处理
  825. if ($foundSection && preg_match('/^\s*$/', $line)) {
  826. $foundSection = false;
  827. }
  828. $output[] = $line;
  829. }
  830. }
  831. // 合并回字符串
  832. $newContent = implode("\n", $output);
  833. // 写入文件
  834. $res1 = file_put_contents($file, $newContent);
  835. var_dump($res1, '已经处理/plugins/globals.js');
  836. //处理文件\plugins\request.js
  837. $file = $yuan . '/plugins/request.js';
  838. $content = file_get_contents($file);
  839. $lines = explode("\n", $content);
  840. $in_prod = false;
  841. $in_pre = false;
  842. for ($i = 0; $i < count($lines); $i++) {
  843. $line = $lines[$i];
  844. // 匹配 "//正式环境"
  845. if (preg_match('/\/\/正式环境/', $line)) {
  846. $in_prod = true;
  847. $in_pre = false;
  848. continue;
  849. }
  850. // 匹配 "//pre环境"
  851. if (preg_match('/\/\/pre环境/', $line)) {
  852. $in_pre = true;
  853. $in_prod = false;
  854. continue;
  855. }
  856. // 处理正式环境下的两行:去注释
  857. if ($in_prod && $i + 0 <= count($lines) && $i + 1 <= count($lines)) {
  858. for ($j = $i + 0; $j <= $i + 1; $j++) {
  859. if (isset($lines[$j])) {
  860. $lines[$j] = preg_replace('/^\s*\/\/\s*/', '', $lines[$j]);
  861. }
  862. }
  863. $in_prod = false; // 只处理一次
  864. }
  865. // 处理 pre 环境下的两行:添加注释
  866. if ($in_pre && $i + 0 <= count($lines) && $i + 1 <= count($lines)) {
  867. for ($j = $i + 1; $j <= $i + 2; $j++) {
  868. if (isset($lines[$j]) && !preg_match('/^\s*\/\//', $lines[$j])) {
  869. // $lines[$j] = '// ' . $lines[$j];
  870. }
  871. }
  872. $in_pre = false; // 只处理一次
  873. }
  874. }
  875. // 写回文件
  876. $res2 = file_put_contents($file, implode("\n", $lines));
  877. var_dump($res2, '已经处理/plugins/request.js');
  878. echo "✅ 环境配置切换完成!";
  879. //第三步 定位到yuan目录,执行 npm run build 生成output目录
  880. chdir($yuan);
  881. $APP_ENV = env('APP_ENV');
  882. $res3 = exec("npm run build");
  883. var_dump($res, '执行 npm run build');
  884. echo "✅ 构建完成!";
  885. //第四步
  886. //修改 .output\server\chunks\routes\index.mjs ,
  887. // 根据网站的id的port更改端口
  888. //第五步
  889. //x
  890. $file4 = $yuan . '/.output/server/chunks/routes/index.mjs';
  891. $content = file_get_contents($file4);
  892. // 使用正则将 "destr(...) || 3e3" 替换为 "destr(...) || 9000"
  893. $content = preg_replace(
  894. '/(destr$[^)]+$$\s*\|\|\s*)3e3/i',
  895. '$19000',
  896. $content
  897. );
  898. // 写回文件
  899. file_put_contents($file, $content);
  900. }
  901. //第六步 yuan压缩成压缩包,复制到/hyperf-skeleton/web1/,解压
  902. $dir_yuan = $yuan . '/.output/';
  903. // 移动到web 没有就创建
  904. $dir_web = '/hyperf-skeleton/' . '/web1/';
  905. if (!is_dir($dir_web)) {
  906. mkdir($dir_web, 0755, true);
  907. }
  908. $fs = new Filesystem();
  909. $fs->rename($dir_yuan, $dir_web . 'output', true);
  910. var_dump('已经移动到/hyperf-skeleton/web1/output');
  911. $result = [
  912. 'res' => $res,
  913. 'res1' => $res1,
  914. 'res2' => $res2,
  915. 'res3' => $res3,
  916. 'root' => $root,
  917. 'yuan' => $yuan,
  918. 'APP_ENV' => $APP_ENV,
  919. 'website_id' => $data['website_id'],
  920. ];
  921. if ($result) {
  922. return Result::success($result);
  923. } else {
  924. return Result::error('更新配置失败');
  925. }
  926. }
  927. public function updateWebOutput($data)
  928. {
  929. //根据源程序, 复制出来,更改配置文件,打包,
  930. $result = WebsiteTemplate::where('id', $data['id'])->update($data);
  931. if ($result) {
  932. return Result::success($result);
  933. } else {
  934. return Result::error('更新输出失败');
  935. }
  936. }
  937. public function updateWebMove($data)
  938. {
  939. //更改到dir目录,
  940. $result = WebsiteTemplate::where('id', $data['id'])->update($data);
  941. if ($result) {
  942. return Result::success($result);
  943. } else {
  944. return Result::error('更新移动失败');
  945. }
  946. }
  947. public function runWeb($data)
  948. {
  949. //运行网站, 先检查是否有配置文件, pm2启动
  950. $result = WebsiteTemplate::where('id', $data['id'])->update($data);
  951. if ($result) {
  952. return Result::success($result);
  953. } else {
  954. return Result::error('运行失败');
  955. }
  956. }
  957. public function stopWeb($data)
  958. {
  959. $result = WebsiteTemplate::where('id', $data['id'])->update($data);
  960. if ($result) {
  961. return Result::success($result);
  962. } else {
  963. return Result::error('停止失败');
  964. }
  965. }
  966. }