ClientService.php 49 KB

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