ClientService.php 49 KB

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