ClientService.php 50 KB

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