ClientService.php 48 KB

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