ClientService.php 50 KB

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