ClientService.php 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  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. use Hyperf\Paginator\Paginator;
  18. #[RpcService(name: "ClientService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  19. class ClientService implements ClientServiceInterface
  20. {
  21. #[Inject]
  22. protected Redis $redis;
  23. /**
  24. * @param array $data
  25. * @return array
  26. */
  27. public function test(array $data): array
  28. {
  29. var_dump($data, '-------1--------');
  30. $time = date('Y-m-d H:i:s', time());
  31. $data = [
  32. 'code' => 200,
  33. 'msg' => 'success',
  34. 'data' => [
  35. 'time' => $time,
  36. 'data' => $data,
  37. ]
  38. ];
  39. //mysql直接查询连接数
  40. $activeConnections = Db::select("SHOW STATUS LIKE 'Threads_connected'");
  41. // $activeConnectionsCount = $activeConnections[0]->Value ?? 0;
  42. // $users = Db::select('SELECT * FROM article;');
  43. return ['code' => 200, 'msg' => 'success', 'data' => $activeConnections];
  44. }
  45. public function indexData(array $data): array
  46. {
  47. $time1 = microtime(true);
  48. // 调整获取方式
  49. // 根据网站id获取,从website_template中获取 website_template
  50. $website_id = $data['website_id'] ?? 2;
  51. var_dump("网站id:", $website_id);
  52. $template = Db::table('website_template')->where('website_id', $website_id)->first();
  53. var_dump("模板:", $template);
  54. $template = $template->template_data;
  55. $data = json_decode($template, true);
  56. // $data = json_decode($data['template'], true);
  57. // $website_id = $data['base']['website_id'] ?? 2;
  58. //设置缓存
  59. $websiteInfoCacheKey = "awebsite:category:{$website_id}";
  60. $websiteInfo = $this->redis->get($websiteInfoCacheKey); //false;
  61. if ($websiteInfo === false) {
  62. // Redis 中没有缓存,从数据库中查询
  63. $websiteInfo = Db::table('website_category')->where('website_id', $website_id)->get()->toArray();
  64. // 将查询结果存入 Redis,设置缓存时间为 3600 秒(1 小时)
  65. $this->redis->setex($websiteInfoCacheKey, 3600, json_encode($websiteInfo));
  66. } else {
  67. // 从 Redis 中获取的数据需要反序列化
  68. $websiteInfo = json_decode($websiteInfo, true);
  69. }
  70. $websiteInfo = Db::table('website_category')->where('website_id', $website_id)->get()->toArray();
  71. //取出website
  72. var_dump($websiteInfo, 'websiteInfo');
  73. //取出category_id 对应的数据
  74. $websiteInfoIndexed = array_column($websiteInfo, null, 'category_id');
  75. // 取出category_id 对应的aLIas_pinyin
  76. $categoryIds = array_column($websiteInfo, 'category_id');
  77. $aliasPinyins = array_column($websiteInfo, 'aLIas_pinyin');
  78. $cat_arr = array_combine($categoryIds, $aliasPinyins);
  79. // var_dump($cat_arr, 'cat_arr');
  80. //根据 category_arr_id 组合出 aLIas_pinyin
  81. $catiall = [];
  82. //一级所有子级的记录
  83. $cat_1st_arr = [];
  84. foreach ($websiteInfo as $key => $value) {
  85. //算出路由拼音
  86. $category_arr_id = json_decode($value->category_arr_id);
  87. $pinyin_str = '';
  88. foreach ($category_arr_id as $k => $v) {
  89. $pinyin_str .= isset($cat_arr[$v]) ? $cat_arr[$v] . '/' : '';
  90. }
  91. $pinyin_str = rtrim($pinyin_str, '/');
  92. $catiall[$value->category_id][] = $pinyin_str;
  93. // $cat_id = $value->category_id;
  94. $websiteInfoIndexed[$value->category_id]->pinyin = $pinyin_str;
  95. // 算出一级 并且算出子级
  96. if ($value->pid == 0) {
  97. if (empty($cat_1st_arr[$value->category_id]))
  98. $cat_1st_arr[$value->category_id] = [];
  99. } else {
  100. if ($value->pid == 11) {
  101. var_dump($value->category_id, 'value->category_id');
  102. }
  103. $cat_1st_arr[$value->pid][] = $value->category_id;
  104. if (empty($cat_1st_arr[$value->category_id])) {
  105. $cat_1st_arr[$value->category_id] = [];
  106. }
  107. }
  108. }
  109. var_dump($cat_1st_arr, 'cat_1st_arr');
  110. $time2 = microtime(true);
  111. $time_ = ($time2 - $time1);
  112. var_dump($time2, $time1, $time_, '返回路由需要多少时间');
  113. //获取
  114. // $getpage = $data['base']['getpage'] ?? 'index';
  115. $getpage = $data['getpage'] ?? 'index';
  116. var_dump($getpage, 'leixing ');
  117. // index = 首页
  118. // class = 频道页
  119. // list = 列表页
  120. // article = 详情页
  121. // search = 搜索页
  122. // aboutList = 底部导航列表页
  123. // aboutArticle = 底部导航详情页
  124. // return $cat_1st_arr;
  125. // return $cat_1st_arr;
  126. // var_dump($cat_1st_arr[11], 'cat_1st_arr'); //一级所有子级的记录
  127. // var_dump($catiall, 'catiall');//拼音
  128. $templateData = [];
  129. switch ($getpage) {
  130. case 'index':
  131. $ceng = $data['template']['index'];
  132. var_dump($ceng, 'ceng================');
  133. foreach ($ceng as $k_ceng => $v_ceng) {
  134. $componetList = $v_ceng['componentList'];
  135. //计算一级的所有子级;组成一个数据,key是一级的category_id,value是所有的子级数据,加上个二级的pinyin属性,放的是catiall的数据
  136. foreach ($componetList as $key => $value) {
  137. //取出componentData
  138. $componentData = $value['componentData'];
  139. if (!$componentData) {
  140. continue;
  141. };
  142. // $page = $componentData['page'];
  143. // $pageSize = $componentData['pageSize'];
  144. // $listType = $componentData['listType'];
  145. $category_id = $componentData['category_id'] ?? 0;
  146. //此处应该有值
  147. $imgSize = $componentData['imgSize'];
  148. $textSize = $componentData['textSize'];
  149. $level = $componentData['level'];
  150. $child = $componentData['child'];
  151. //获取子级
  152. if ($child) {
  153. $child_id = $componentData['category_id'] ?? 0;
  154. $child_imgSize = $child['imgSize'] ?? 0;
  155. $child_textSize = $child['textSize'] ?? 0;
  156. }
  157. // 拼音就是路由----
  158. // unset($listType['pinyin']);
  159. //查询查询数据,返回到data字段里,根据这几个
  160. if ($category_id > 0) {
  161. var_dump($category_id, '------------------category_id');
  162. var_dump($cat_1st_arr, '-----------1-------2');
  163. // 第一次查询:imgurl 不为空的数据
  164. if ($imgSize > 0) {
  165. $imgArticles = Db::table('article')
  166. ->leftJoin('article_ignore', function ($join) use ($website_id) {
  167. $join->on('article.id', '=', 'article_ignore.article_id')->where('article_ignore.website_id', '=', $website_id);
  168. })->whereNull('article_ignore.article_id')->whereIn('catid', $temp_arr = array_merge([$category_id], $cat_1st_arr[$category_id]))->where('status', 1)
  169. ->whereNotNull('imgurl')->where('imgurl', '!=', '')->limit($imgSize)->orderBy('updated_at', 'desc')->get()->toArray();
  170. //拼接上拼音
  171. foreach ($imgArticles as $k => $v) {
  172. $imgArticles[$k]->pinyin = $catiall[$v->catid][0];
  173. }
  174. }
  175. // 第二次查询:imgurl 为空的数据
  176. if ($textSize > 0) {
  177. $textArticles = Db::table('article')
  178. ->leftJoin('article_ignore', function ($join) use ($website_id) {
  179. $join->on('article.id', '=', 'article_ignore.article_id')->where('article_ignore.website_id', '=', $website_id);
  180. })->whereNull('article_ignore.article_id')->whereIn('catid', $temp_arr = array_merge([$category_id], $cat_1st_arr[$category_id]))->where('status', 1)
  181. ->where('status', 1)->limit($textSize)->orderBy('updated_at', 'desc')->get()->toArray();
  182. //拼接上拼音
  183. foreach ($textArticles as $k => $v) {
  184. $textArticles[$k]->pinyin = $catiall[$v->catid][0];
  185. }
  186. }
  187. }
  188. var_dump($level, 'level');
  189. if ($level > 0) {
  190. // 取出一级的所有子级
  191. //取出所有的子级的imgurl 不为空的数据
  192. if ($imgSize > 0) {
  193. $imgArticles = Db::table('article')
  194. ->leftJoin('article_ignore', function ($join) use ($website_id) {
  195. $join->on('article.id', '=', 'article_ignore.article_id')->where('article_ignore.website_id', '=', $website_id);
  196. })->whereNull('article_ignore.article_id')
  197. ->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();
  198. //拼接上拼音
  199. foreach ($imgArticles as $k => $v) {
  200. $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  201. }
  202. }
  203. //取出所有的子级的imgurl 为空的数据 ->whereNull('imgurl')
  204. if ($textSize > 0) {
  205. $textArticles = Db::table('article')->where('status', 1)
  206. ->leftJoin('article_ignore', function ($join) use ($website_id) {
  207. $join->on('article.id', '=', 'article_ignore.article_id')->where('article_ignore.website_id', '=', $website_id);
  208. })->whereNull('article_ignore.article_id')
  209. ->whereRaw('MATCH(level_text) AGAINST(? IN NATURAL LANGUAGE MODE)', [$level])->whereIn('catid', $categoryIds)->limit($textSize)->orderBy('updated_at', 'desc')->get()->toArray();
  210. //拼接上拼音
  211. foreach ($textArticles as $k => $v) {
  212. $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  213. }
  214. }
  215. }
  216. //将查询到的数据放到componentData里
  217. $componetList[$key]['componentData']['data']['alias'] = $websiteInfoIndexed[$category_id]->alias ?? 'aliasnull' . $category_id;
  218. $componetList[$key]['componentData']['data']['pinyin'] = $websiteInfoIndexed[$category_id]->pinyin ?? 'aliasnull' . $category_id;
  219. $componetList[$key]['componentData']['data']['category_id'] = $category_id;
  220. $componetList[$key]['componentData']['data']['textnum'] = $textArticles ?? [];
  221. $componetList[$key]['componentData']['data']['imgnum'] = $imgArticles ?? [];
  222. //判断child
  223. var_dump($child_id, '--------child_id---');
  224. if ($child_id > 0) {
  225. //取出子级的imgurl 不为空的数据
  226. if ($child_imgSize > 0) {
  227. $child_imgArticles = Db::table('article')->where('catid', $child_id)
  228. ->where('status', 1)
  229. ->whereNotNull('imgurl')->limit($child_imgSize)->orderBy('updated_at', 'desc')->get()->toArray();
  230. } else {
  231. $child_imgArticles = [];
  232. }
  233. //取出子级的imgurl 为空的数据
  234. if ($child_textSize > 0) {
  235. $child_textArticles = Db::table('article')
  236. ->where('status', 1)
  237. ->where('catid', $child_id)->whereNull('imgurl')->limit($child_textSize)->orderBy('updated_at', 'desc')->get()->toArray();
  238. } else {
  239. $child_textArticles = [];
  240. }
  241. //将查询到的数据放到componentData里
  242. $componetList[$key]['componentData']['data']['child']['alias'] = $websiteInfoIndexed[$child_id]->alias;
  243. $componetList[$key]['componentData']['data']['child']['pinyin'] = $websiteInfoIndexed[$child_id]->pinyin;
  244. $componetList[$key]['componentData']['data']['child']['category_id'] = $child_id;
  245. $componetList[$key]['componentData']['data']['child']['textnum'] = $child_textArticles;
  246. $componetList[$key]['componentData']['data']['child']['imgnum'] = $child_imgArticles;
  247. $componetList[$key]['componentData']['data']['child']['is_url'] = $websiteInfoIndexed[$child_id]->is_url ?? 0;
  248. //取出自己
  249. $all_childcat = $cat_1st_arr[$category_id];
  250. $componetList[$key]['componentData']['data']['child']['children_count'] = count($all_childcat);
  251. $processedChildCat = array_map(function ($v) use ($websiteInfoIndexed, $cat_1st_arr) {
  252. // 从 $websiteInfoIndexed 中获取对应的数据
  253. $info = $websiteInfoIndexed[$v];
  254. var_dump($info, $v, '-------child循环--------');
  255. // 返回一个包含所需信息的数组
  256. return [
  257. 'pinyin' => $info->pinyin,
  258. 'alias' => $info->alias,
  259. 'category_id' => $info->category_id,
  260. 'aLIas_pinyin' => $info->aLIas_pinyin,
  261. 'pid' => $info->pid,
  262. 'cat_arr_id' => $info->category_arr_id ?? ['出错'],
  263. 'is_url' => $info->is_url ?? 0,
  264. 'children_count' => isset($cat_1st_arr[$v]) ? count($cat_1st_arr[$v]) : 0
  265. ];
  266. }, $all_childcat);
  267. $componetList[$key]['componentData']['data']['child']['all_childcat'] = $processedChildCat ?? [];
  268. }
  269. }
  270. $data['template']['index'][$k_ceng]['componentList'] = $componetList;
  271. }
  272. //将修改后的数据放到template里
  273. break;
  274. case 'class':
  275. $componetList = $data['template']['class']['data'][0]['componentList'];
  276. $class_count = count($componetList);
  277. $parent_id = $data['template']['class']['parent_id'] ?? 0;
  278. //获取子级;
  279. $child = $cat_1st_arr[$parent_id]; //获取子级
  280. // var_dump($child, 'child');
  281. //如果$child的数量小于$class_count,就填充一样多的数据给child,如果大于就截取一样的数量给child
  282. if (count($child) < $class_count) {
  283. $child = array_pad($child, $class_count, end($child) ?? 0);
  284. } elseif (count($child) > $class_count) {
  285. $child = array_slice($child, 0, $class_count);
  286. }
  287. foreach ($componetList as $key => $value) {
  288. $componentData = $value['componentData'];
  289. //取出imgSize和textSize,获取相应的文章,
  290. $category_id = $child[$key] ?? 0;
  291. $imgSize = $componentData['imgSize'] ?? 0;
  292. $textSize = $componentData['textSize'] ?? 0;
  293. $listType = $componentData['listType'] ?? [];
  294. var_dump($listType, 'before unset listType');
  295. // 定义要移除的元素数组
  296. $valuesToRemove = ['category_name', 'pinyin'];
  297. // 循环移除每个元素
  298. foreach ($valuesToRemove as $value) {
  299. while (($key = array_search($value, $listType)) !== false) {
  300. // 移除该元素
  301. unset($listType[$key]);
  302. }
  303. }
  304. var_dump($listType, 'listType');
  305. //child
  306. $child = $componentData['child'] ?? [];
  307. $data_class = [];
  308. //查询imgurl不为空的数据
  309. $imgArticles = Db::table('article')
  310. ->where('catid', $category_id)
  311. ->whereNotNull('imgurl')
  312. ->limit($imgSize)
  313. // ->select($listType)
  314. ->orderBy('updated_at', 'desc')
  315. ->get()
  316. ->toArray();
  317. //拼接上拼音
  318. foreach ($imgArticles as $k => $v) {
  319. $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  320. }
  321. //查询imgurl为空的数据
  322. $textArticles = Db::table('article')
  323. ->where('catid', $category_id)
  324. ->whereNull('imgurl')
  325. ->limit($textSize)
  326. ->orderBy('updated_at', 'desc')
  327. ->get()
  328. ->toArray();
  329. //拼接上拼音
  330. foreach ($textArticles as $k => $v) {
  331. $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  332. }
  333. $data_class['alias'] = $websiteInfoIndexed[$category_id]->alias ?? 'aliasnull' . $category_id;
  334. $data_class['pinyin'] = $websiteInfoIndexed[$category_id]->pinyin ?? 'aliasnull' . $category_id;
  335. $data_class['category_id'] = $category_id;
  336. $data_class['textnum'] = $textArticles ?? [];
  337. $data_class['imgnum'] = $imgArticles ?? [];
  338. $data['data'] = $data_class;
  339. }
  340. break;
  341. case 'list':
  342. $componetListdata = $data['template']['list']['data'];
  343. $category_id = $data['template']['list']['category_id'] ?? 0;
  344. foreach ($componetListdata as $k => $v) {
  345. if (!isset($v['componentList']) || !is_array($v['componentList'])) {
  346. continue; // 如果 componentList 不存在或不是数组,跳过当前循环
  347. }
  348. $componentList = &$data['template']['list']['data'][$k]['componentList'];
  349. var_dump(count($componentList), 'componentList111');
  350. foreach ($componentList as $key => $value) {
  351. $componentData = $value['componentData'];
  352. if (isset($value['componentData']['category_id'])) {
  353. $value['componentData']['category_id'] = $category_id;
  354. $page = $componentData['papageTypege']['page'] ?? 1;
  355. $pageSize = $componentData['papageTypege']['pageSize'] ?? 10;
  356. $listType = $componentData['listType'];
  357. $articals = Db::table('article')
  358. ->where('catid', $category_id)
  359. ->where('status', 1)
  360. ->orderBy('updated_at', 'desc')
  361. ->get();
  362. // ->paginate($pageSize, ['*'], 'page', $page);
  363. //拼接上拼音
  364. foreach ($articals as $k => $v) {
  365. $articals[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  366. }
  367. $value['componentData']['data'] = $articals;
  368. }
  369. var_dump(isset($value['componentData']['level']), $key, '---------------------------------------');
  370. if (isset($value['componentData']['level'])) {
  371. //取出imgSize和textSize,获取相应的文章,
  372. $imgSize = $componentData['imgnum'] ?? 0;
  373. $textSize = $componentData['textnum'] ?? 0;
  374. $listType = $componentData['listType'] ?? [];
  375. // 定义要移除的元素数组
  376. $valuesToRemove = ['category_name', 'pinyin'];
  377. // 循环移除每个元素
  378. foreach ($valuesToRemove as $value) {
  379. while (($key = array_search($value, $listType)) !== false) {
  380. // 移除该元素
  381. unset($listType[$key]);
  382. }
  383. }
  384. //查询imgurl不为空的数据
  385. if ($imgSize > 0) {
  386. $imgArticles = Db::table('article')
  387. ->where('catid', $category_id)
  388. ->whereNotNull('imgurl')
  389. ->limit($imgSize)
  390. ->orderBy('updated_at', 'desc')
  391. ->get()
  392. ->toArray();
  393. //拼接上拼音
  394. foreach ($imgArticles as $k => $v) {
  395. $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  396. }
  397. }
  398. //查询imgurl为空的数据
  399. if ($textSize > 0) {
  400. $textArticles = Db::table('article')
  401. ->where('catid', $category_id)
  402. ->whereNull('imgurl')
  403. ->limit($textSize)
  404. ->orderBy('updated_at', 'desc')
  405. ->get()
  406. ->toArray();
  407. //拼接上拼音
  408. foreach ($textArticles as $k => $v) {
  409. $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  410. }
  411. }
  412. //将查询到的数据放到componentData里
  413. $componetList[$key]['componentData']['data']['alias'] = $websiteInfoIndexed[$category_id]->alias ?? 'aliasnull' . $category_id;
  414. $componetList[$key]['componentData']['data']['pinyin'] = $websiteInfoIndexed[$category_id]->pinyin ?? 'aliasnull' . $category_id;
  415. $componetList[$key]['componentData']['data']['category_id'] = $category_id;
  416. $componetList[$key]['componentData']['data']['textnum'] = $textArticles ?? [];
  417. $componetList[$key]['componentData']['data']['imgnum'] = $imgArticles ?? [];
  418. //将修改后的数据放到template里
  419. // $data['template']['list']['data'][0]['componentList'] = $componetList;
  420. }
  421. }
  422. }
  423. break;
  424. case 'article':
  425. $article_id = 51434; //假设这是文章ID
  426. $componetListdata = &$data['template']['article']['data'];
  427. var_dump($categoryIds, '----------------');
  428. // $category_id = $data['template']['article']['category_id'] ?? 0;
  429. foreach ($componetListdata as $k => $v) {
  430. foreach ($v['componentList'] as $key => $value) {
  431. $componentData = $value['componentData'];
  432. if (isset($value['componentData']['article_id'])) {
  433. $value['componentData']['article_id'] = $article_id;
  434. // $page = $componentData['papageTypege']['page'] ?? 1;
  435. // $pageSize = $componentData['papageTypege']['pageSize'] ?? 10;
  436. $listType = $componentData['listType'];
  437. //查询数据
  438. $articals = Db::table('article')
  439. ->where('id', $article_id)
  440. ->where('status', 1)
  441. ->leftJoin('article_data', 'article.id', '=', 'article_data.article_id')
  442. ->select('article.*', 'article_data.content')
  443. ->orderBy('updated_at', 'desc')
  444. ->get();
  445. //拼接上拼音
  446. $value['componentData']['data'] = $articals;
  447. }
  448. if (isset($value['componentData']['level'])) {
  449. //:1:头条 2:轮播图 3:推荐图 4:最新新闻;5:推荐新闻;6:热点资讯 7:自动关联
  450. //4 update 5 30天之内 6 库中
  451. $level = $componentData['level'] ?? 0;
  452. $imgSize = $componentData['imgnum'] ?? 0;
  453. $textSize = $componentData['textnum'] ?? 0;
  454. if (in_array($level, [1, 2, 3, 6])) {
  455. //查询imgurl不为空的数据
  456. $imgArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNotNull('imgurl')
  457. ->where('status', 1)
  458. ->whereRaw('MATCH(level_text) AGAINST(? IN NATURAL LANGUAGE MODE)', [$level])
  459. ->limit($imgSize)->orderBy('updated_at', 'desc')->get()->toArray();
  460. //拼接上拼音
  461. foreach ($imgArticles as $k => $v) {
  462. $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  463. }
  464. //查询imgurl为空的数据
  465. $textArticles = Db::table('article')->where('catid', $categoryIds)->whereNull('imgurl')
  466. ->where('status', 1)
  467. ->whereRaw('MATCH(level_text) AGAINST(? IN NATURAL LANGUAGE MODE)', [$level])
  468. ->limit($textSize)->orderBy('updated_at', 'desc')->get()->toArray();
  469. //拼接上拼音
  470. foreach ($textArticles as $k => $v) {
  471. $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  472. }
  473. } else if ($level == 4) {
  474. //查询imgurl不为空的数据
  475. $imgArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNotNull('imgurl')
  476. ->where('status', 1)
  477. ->limit($imgSize)->orderBy('updated_at', 'desc')->get()->toArray();
  478. //拼接上拼音
  479. if (!empty($imgArticles)) {
  480. foreach ($imgArticles as $k => $v) {
  481. $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  482. }
  483. }
  484. //查询imgurl为空的数据
  485. $textArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNull('imgurl')
  486. ->where('status', 1)
  487. ->limit($textSize)->orderBy('updated_at', 'desc')->get()->toArray();
  488. //拼接上拼音
  489. var_dump('---4------');
  490. if (!empty($textArticles)) {
  491. foreach ($textArticles as $k => $v) {
  492. $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  493. }
  494. }
  495. } else if ($level == 5) {
  496. var_dump($value['componentData']['level'], 'level');
  497. //查询imgurl不为空的数据
  498. $imgArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNotNull('imgurl')
  499. ->where('status', 1)
  500. //最近三十天
  501. ->where('updated_at', '>=', date('Y-m-d H:i:s', strtotime('-30 days')))
  502. ->limit($imgSize)->orderBy('updated_at', 'desc')->get()->toArray();
  503. //拼接上拼音
  504. if (!empty($imgArticles)) {
  505. foreach ($imgArticles as $k => $v) {
  506. $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  507. }
  508. }
  509. //查询imgurl为空的数据
  510. $textArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNull('imgurl')
  511. ->where('status', 1)
  512. ->where('updated_at', '>=', date('Y-m-d H:i:s', strtotime('-30 days')))
  513. ->limit($textSize)->orderBy('updated_at', 'desc')->get()->toArray();
  514. //拼接上拼音
  515. var_dump('---5------');
  516. if (!empty($textArticles)) {
  517. foreach ($textArticles as $k => $v) {
  518. $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  519. }
  520. }
  521. } else if ($level == 7) {
  522. //获取artical的keyword
  523. $keword = DB::table('artical')->where('id', $article_id)->select('keyword')->get();
  524. $kewordl = $keword[0]->keyword;
  525. $kewordl = explode(',', $kewordl);
  526. //筛选出符合条件的article来
  527. $imgArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNotNull('imgurl')
  528. ->where('status', 1)
  529. // ->whereJsonContains('keyword', $kewordl)
  530. ->whereRaw('MATCH(keyword) AGAINST(? IN NATURAL LANGUAGE MODE)', [$kewordl])
  531. ->limit($imgSize)->orderBy('updated_at', 'desc')->get()->toArray();
  532. //拼接上拼音
  533. foreach ($imgArticles as $k => $v) {
  534. $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  535. }
  536. $textArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNull('imgurl')
  537. ->where('status', 1)
  538. // ->whereJsonContains('keyword', $kewordl)
  539. ->whereRaw('MATCH(keyword) AGAINST(? IN NATURAL LANGUAGE MODE)', [$kewordl])
  540. ->limit($textSize)->orderBy('updated_at', 'desc')->get()->toArray();
  541. //拼接上拼音
  542. foreach ($textArticles as $k => $v) {
  543. $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  544. }
  545. }
  546. }
  547. $value['componentData']['data']['imgnum'] = $imgArticles ?? [];
  548. $value['componentData']['data']['textnum'] = $textArticles ?? [];
  549. }
  550. }
  551. break;
  552. case 'search':
  553. break;
  554. case 'aboutList':
  555. $fact_id = 7;
  556. $componetList = &$data['template']['aboutList']['componentData'];
  557. $componetList['fact_id'] = $fact_id;
  558. $content = Db::table('footer_content')
  559. ->where('fcat_id', $fact_id)
  560. ->orderBy('updated_at', 'desc')
  561. ->get()
  562. ->toArray();
  563. $componetList['data'] = $content;
  564. break;
  565. case 'aboutArticle':
  566. $fact_id = 7;
  567. $componetList = &$data['template']['aboutList']['componentData'];
  568. $componetList['fact_id'] = $fact_id;
  569. $content = Db::table('footer_content')
  570. ->where('id', $fact_id)
  571. ->orderBy('updated_at', 'desc')
  572. ->get()
  573. ->toArray();
  574. $componetList['data'] = $content;
  575. break;
  576. default:
  577. $componetList = [];
  578. break;
  579. }
  580. return $data;
  581. }
  582. public function addWeb($data)
  583. {
  584. $result = WebsiteTemplate::insertGetId($data);
  585. if ($result) {
  586. return Result::success($result);
  587. } else {
  588. return Result::error('添加失败');
  589. }
  590. }
  591. public function deleteWeb($data)
  592. {
  593. $result = WebsiteTemplate::where('id', $data['id'])->delete();
  594. if ($result) {
  595. return Result::success($result);
  596. } else {
  597. return Result::error('删除失败');
  598. }
  599. }
  600. public function updateWeb($data)
  601. {
  602. $result = WebsiteTemplate::where('id', $data['id'])->update($data);
  603. if ($result) {
  604. return Result::success($result);
  605. } else {
  606. return Result::error('更新失败');
  607. }
  608. }
  609. public function getWebInfo($data)
  610. {
  611. $result = WebsiteTemplate::where('website_template.id', $data['id'])
  612. ->leftJoin('website', 'website.id', '=', 'website_template.website_id')
  613. ->select('website_template.*', 'website.website_name as website_name', 'website.website_url as website_url')
  614. ->first();
  615. return Result::success($result);
  616. }
  617. public function getWebList($data)
  618. {
  619. var_dump($data, 'data');
  620. $where = [];
  621. if (isset($data['website_name'])) {
  622. $where[] = ['website.website_name', 'like', '%' . $data['website_name'] . '%'];
  623. }
  624. if (isset($data['status'])) {
  625. $where[] = ['website_template.status', '=', $data['status']];
  626. }
  627. $result = WebsiteTemplate::where([])
  628. ->where($where)
  629. ->leftJoin('website', 'website.id', '=', 'website_template.website_id')
  630. ->select('website_template.*', 'website.website_name as website_name', 'website.website_url as website_url')
  631. ->paginate($data['pageSize'], ['*'], 'page', $data['page']);
  632. if ($result) {
  633. return Result::success($result);
  634. } else {
  635. return Result::error('获取列表失败');
  636. }
  637. }
  638. public function updateWebConfig($data)
  639. {
  640. var_dump($data, 'data1');
  641. // return Result::success($data);
  642. $where = [];
  643. if (isset($data['website_id'])) {
  644. $where['website_template.website_id'] = $data['website_id'];
  645. }
  646. $result = WebsiteTemplate::where([])
  647. ->where($where)
  648. ->leftJoin('website', 'website.id', '=', 'website_template.website_id')
  649. ->select('website_template.id', 'website_template.port', 'website.website_name as website_name', 'website.website_url as website_url', 'website_template.siteId')
  650. ->first()->toArray();
  651. var_dump($result, 'result');
  652. $domain = json_decode($result['website_url'], true);
  653. $domainlist = $domain;
  654. var_dump(empty($result['port']), 'port');
  655. $domain = $domain[3];
  656. if ($domain == null || empty($domain) || !isset($domain)) {
  657. return Result::error('域名不存在');
  658. }
  659. $port = $result['port'];
  660. if ($port == null || empty($port) || !isset($port)) {
  661. var_dump($port, 'youp0------port');
  662. return Result::error('端口不存在');
  663. }
  664. $postData = [
  665. 'domain' => $domain,
  666. 'port' => $port
  667. ];
  668. var_dump(empty($result['siteId']), 'siteId---');
  669. if (empty($result['siteId'])) {
  670. //实例化对象
  671. $api = new bt_api();
  672. //获取面板日志
  673. $r_data = $api->GetLogs();
  674. //输出JSON数据到浏览器
  675. echo json_encode($r_data);
  676. $webname = [
  677. 'domain' => $domain,
  678. 'domainlist' => $domainlist,
  679. 'count' => count($domainlist) - 1
  680. ];
  681. $siteData = [
  682. 'path' => '/www2/www/wwwroot/' . $domain,
  683. 'ftp' => 'false',
  684. 'type' => 'PHP',
  685. 'type_id' => 0,
  686. 'ps' => $domain,
  687. 'port' => 80,
  688. 'version' => '00',
  689. 'need_index' => 0,
  690. 'need_404' => 0,
  691. 'sql' => 'false',
  692. 'codeing' => 'utf8mb4',
  693. 'webname' => json_encode($webname),
  694. 'add_dns_record' => 'false'
  695. ];
  696. $r_data = $api->addSite($siteData);
  697. /*
  698. {
  699. "siteStatus": true,
  700. "siteId": 20,
  701. "ftpStatus": false,
  702. "databaseStatus": false,
  703. "gitStatus": false
  704. }
  705. */
  706. var_dump($r_data, '----------1--r_data--------');
  707. if (isset($r_data['status']) && $r_data['status'] == false) {
  708. return Result::error('宝塔添加失败' . $r_data['msg']);
  709. }
  710. if (isset($r_data['siteStatus']) && $r_data['siteStatus'] == true) {
  711. //记录siteId
  712. $siteId = $r_data['siteId'];
  713. //更新网站模板的siteId WebsiteTemplate
  714. WebsiteTemplate::where('website_id', $data['website_id'])
  715. ->update([
  716. 'siteId' => $siteId,
  717. 'dir' => '/www2/www/wwwroot/' . $domain,
  718. 'status' => 2
  719. ]);
  720. }
  721. // return Result::success($r_data);
  722. }
  723. // 使用Guzzle HTTP客户端发送POST请求
  724. try {
  725. $client = new \GuzzleHttp\Client();
  726. $response = $client->post('http://adminpre.bjzxtw.org.cn/create.php', [
  727. 'json' => $postData,
  728. 'timeout' => 30,
  729. 'headers' => [
  730. 'Content-Type' => 'application/json',
  731. ]
  732. ]);
  733. // 获取响应内容
  734. $responseBody = $response->getBody()->getContents();
  735. $responseData = json_decode($responseBody, true);
  736. var_dump($responseData, '脚本返回数据------------------------');
  737. // 检查响应是否为有效的JSON
  738. if (json_last_error() === JSON_ERROR_NONE) {
  739. // 如果远程服务器返回成功结果
  740. if (isset($responseData['code']) && $responseData['code'] === 1) {
  741. return Result::success([
  742. 'message' => '远程服务器处理成功' . $responseData['message'],
  743. 'output' => $responseData['output'],
  744. 'file' => $responseData['file'],
  745. 'code' => $responseData['code'],
  746. 'exec' => $responseData['exec'],
  747. 'domain' => $domain,
  748. 'port' => $port,
  749. 'remote_response' => $responseData
  750. ]);
  751. } else {
  752. // 远程服务器返回错误
  753. return Result::error(500, '远程服务器处理失败', [
  754. 'remote_response' => $responseData
  755. ]);
  756. }
  757. } else {
  758. // 响应不是有效的JSON
  759. return Result::success([
  760. 'message' => '收到远程服务器响应(非JSON格式)',
  761. 'domain' => $domain,
  762. 'port' => $port,
  763. 'raw_response' => $responseBody
  764. ]);
  765. }
  766. } catch (\GuzzleHttp\Exception\RequestException $e) {
  767. // HTTP请求异常
  768. $errorMessage = 'HTTP请求失败';
  769. if ($e->hasResponse()) {
  770. $response = $e->getResponse();
  771. $errorMessage .= ': ' . $response->getStatusCode() . ' ' . $response->getReasonPhrase();
  772. } else {
  773. $errorMessage .= ': ' . $e->getMessage();
  774. }
  775. return Result::error(500, $errorMessage, [
  776. 'exception' => $e->getMessage()
  777. ]);
  778. } catch (\Exception $e) {
  779. // 其他异常
  780. return Result::error(500, '请求处理异常: ' . $e->getMessage(), [
  781. 'exception' => $e->getMessage()
  782. ]);
  783. }
  784. if ($result) {
  785. return Result::success($result);
  786. } else {
  787. return Result::error('获取列表失败');
  788. }
  789. }
  790. private function bt($data) {}
  791. public function updateWebOutput($data)
  792. {
  793. // return Result::error('更新输出失败');
  794. //根据源程序, 复制出来,更改配置文件,打包,
  795. // $result = 1;
  796. $projectRoot = dirname(dirname(__DIR__)); // 从当前文件向上两级目录
  797. $projectDir = $projectRoot . '/../zizhujianzhan_web';
  798. $command = "cd " . escapeshellarg($projectDir) . " && npm run build";
  799. // 执行命令并获取输出
  800. exec($command, $output, $returnCode);
  801. // 输出结果
  802. echo "Return Code: " . $returnCode . "\n";
  803. echo "Output:\n";
  804. foreach ($output as $line) {
  805. echo $line . "\n";
  806. }
  807. $result = $returnCode;
  808. if ($result) {
  809. return Result::success($result);
  810. } else {
  811. return Result::error('更新输出失败');
  812. }
  813. }
  814. public function updateWebMove($data)
  815. {
  816. //更改到dir目录,
  817. $result = WebsiteTemplate::where('id', $data['id'])->update($data);
  818. if ($result) {
  819. return Result::success($result);
  820. } else {
  821. return Result::error('更新移动失败');
  822. }
  823. }
  824. public function runWeb($data)
  825. {
  826. //运行网站, 先检查是否有配置文件, pm2启动
  827. $result = WebsiteTemplate::where('id', $data['id'])->update($data);
  828. if ($result) {
  829. return Result::success($result);
  830. } else {
  831. return Result::error('运行失败');
  832. }
  833. }
  834. public function stopWeb($data)
  835. {
  836. $result = WebsiteTemplate::where('id', $data['id'])->update($data);
  837. if ($result) {
  838. return Result::success($result);
  839. } else {
  840. return Result::error('停止失败');
  841. }
  842. }
  843. }
  844. class bt_api
  845. {
  846. private $BT_KEY = "RN0wTXLHpKKBXXxyDJoovHMpBhyLVFWZ"; //接口密钥
  847. private $BT_PANEL = "http://192.168.1.234:18888"; //面板地址
  848. //如果希望多台面板,可以在实例化对象时,将面板地址与密钥传入
  849. public function __construct($bt_panel = null, $bt_key = null)
  850. {
  851. if ($bt_panel) $this->BT_PANEL = $bt_panel;
  852. if ($bt_key) $this->BT_KEY = $bt_key;
  853. }
  854. //示例取面板日志
  855. public function GetLogs()
  856. {
  857. //拼接URL地址
  858. $url = $this->BT_PANEL . '/data?action=getData';
  859. //准备POST数据
  860. $p_data = $this->GetKeyData(); //取签名
  861. $p_data['table'] = 'logs';
  862. $p_data['limit'] = 10;
  863. $p_data['tojs'] = 'test';
  864. //请求面板接口
  865. $result = $this->HttpPostCookie($url, $p_data);
  866. //解析JSON数据
  867. $data = json_decode($result, true);
  868. return $data;
  869. }
  870. public function addSite(array $siteData): array
  871. {
  872. //拼接URL地址
  873. $url = $this->BT_PANEL . '/site?action=AddSite';
  874. //准备POST数据
  875. $p_data = $this->GetKeyData(); //取签名
  876. // Merge site data with key data
  877. $p_data = array_merge($p_data, $siteData);
  878. //请求面板接口
  879. $result = $this->HttpPostCookie($url, $p_data);
  880. //解析JSON数据
  881. $data = json_decode($result, true);
  882. return $data;
  883. }
  884. /**
  885. * 检查删除数据
  886. * ids [21]
  887. */
  888. public function checkDelSite(array $siteData): array
  889. {
  890. //拼接URL地址
  891. $url = $this->BT_PANEL . '/site?action=check_del_data';
  892. //准备POST数据
  893. $p_data = $this->GetKeyData(); //取签名
  894. // Merge site data with key data
  895. $p_data = array_merge($p_data, $siteData);
  896. //请求面板接口
  897. $result = $this->HttpPostCookie($url, $p_data);
  898. //解析JSON数据
  899. $data = json_decode($result, true);
  900. return $data;
  901. }
  902. /**
  903. * 删除网站
  904. * id 21
  905. * webname t2.lj
  906. * path 1
  907. */
  908. public function delSite(array $siteData): array
  909. {
  910. //拼接URL地址
  911. $url = $this->BT_PANEL . '/site?action=DeleteSite';
  912. //准备POST数据
  913. $p_data = $this->GetKeyData(); //取签名
  914. // Merge site data with key data
  915. $p_data = array_merge($p_data, $siteData);
  916. //请求面板接口
  917. $result = $this->HttpPostCookie($url, $p_data);
  918. //解析JSON数据
  919. $data = json_decode($result, true);
  920. return $data;
  921. }
  922. /**
  923. * 构造带有签名的关联数组
  924. */
  925. private function GetKeyData()
  926. {
  927. $now_time = time();
  928. $p_data = array(
  929. 'request_token' => md5($now_time . '' . md5($this->BT_KEY)),
  930. 'request_time' => $now_time
  931. );
  932. return $p_data;
  933. }
  934. /**
  935. * 发起POST请求
  936. * @param String $url 目标网填,带http://
  937. * @param Array|String $data 欲提交的数据
  938. * @return string
  939. */
  940. private function HttpPostCookie($url, $data, $timeout = 60)
  941. {
  942. //定义cookie保存位置
  943. $cookie_file = './' . md5($this->BT_PANEL) . '.cookie';
  944. if (!file_exists($cookie_file)) {
  945. $fp = fopen($cookie_file, 'w+');
  946. fclose($fp);
  947. }
  948. $ch = curl_init();
  949. curl_setopt($ch, CURLOPT_URL, $url);
  950. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  951. curl_setopt($ch, CURLOPT_POST, 1);
  952. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  953. curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
  954. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
  955. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  956. curl_setopt($ch, CURLOPT_HEADER, 0);
  957. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  958. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  959. $output = curl_exec($ch);
  960. curl_close($ch);
  961. return $output;
  962. }
  963. }