ClientService.php 50 KB

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