ClientService.php 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  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. $getpage = $data['getpage'] ?? 'index';
  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. var_dump($getpage, 'leixing ');
  116. // index = 首页
  117. // class = 频道页
  118. // list = 列表页
  119. // article = 详情页
  120. // search = 搜索页
  121. // aboutList = 底部导航列表页
  122. // aboutArticle = 底部导航详情页
  123. // return $cat_1st_arr;
  124. // return $cat_1st_arr;
  125. // var_dump($cat_1st_arr[11], 'cat_1st_arr'); //一级所有子级的记录
  126. // var_dump($catiall, 'catiall');//拼音
  127. $templateData = [];
  128. switch ($getpage) {
  129. case 'index':
  130. $ceng = $data['template']['index'];
  131. var_dump($ceng, 'ceng================');
  132. foreach ($ceng as $k_ceng => $v_ceng) {
  133. $componetList = $v_ceng['componentList'];
  134. //计算一级的所有子级;组成一个数据,key是一级的category_id,value是所有的子级数据,加上个二级的pinyin属性,放的是catiall的数据
  135. foreach ($componetList as $key => $value) {
  136. //取出componentData
  137. $componentData = $value['componentData'];
  138. if (!$componentData) {
  139. continue;
  140. };
  141. var_dump($componentData, '----------------componentData-------------');
  142. if (is_array($componentData) && count($componentData) > 1 && !isset($componentData['level'])) {
  143. foreach ($componentData as $k1 => $v1) {
  144. // $v1['category_id'] = $v1['category_id'];
  145. $category_id = $v1['category_id'] ?? 0;
  146. //此处应该有值
  147. $imgSize = $v1['imgSize'];
  148. $textSize = $v1['textSize'];
  149. $level = $v1['level'];
  150. // 暂不处理选项卡中带子级栏目数据
  151. // $child = $v1['child'];
  152. //获取子级
  153. // if ($child) {
  154. // $child_id = $child['id'] ?? 0;
  155. // $child_imgSize = $child['imgSize'] ?? 0;
  156. // $child_textSize = $child['textSize'] ?? 0;
  157. // }
  158. // 拼音就是路由----
  159. // unset($listType['pinyin']);
  160. //查询查询数据,返回到data字段里,根据这几个
  161. if ($category_id > 0) {
  162. // var_dump($category_id, '------------------category_id');
  163. // var_dump($cat_1st_arr, '-----------1-------2');
  164. // 第一次查询:imgurl 不为空的数据
  165. if ($imgSize > 0) {
  166. $imgArticles = Db::table('article')
  167. ->leftJoin('article_ignore', function ($join) use ($website_id) {
  168. $join->on('article.id', '=', 'article_ignore.article_id')
  169. ->where('article_ignore.website_id', '=', $website_id);
  170. })
  171. // ->whereNull('article_ignore.article_id')
  172. ->where(function ($query) {
  173. $query->whereNull('article_ignore.article_id')
  174. ->orWhere(function ($subQuery) {
  175. $subQuery->whereNotNull('article_ignore.article_id')
  176. ->where('article_ignore.is_ignore', 0)
  177. ->where(function ($subSubQuery) {
  178. $subSubQuery->where('article_ignore.c_show_time', '<=', date('Y-m-d H:i:s'))
  179. ->orWhereNull('article_ignore.c_show_time');
  180. });
  181. });
  182. })
  183. ->whereIn('catid', $temp_arr = array_merge([$category_id], $cat_1st_arr[$category_id]))->where('status', 1)
  184. ->whereNotNull('imgurl')->where('imgurl', '!=', '')->limit($imgSize)->orderBy('updated_at', 'desc')->get()->toArray();
  185. //拼接上拼音
  186. foreach ($imgArticles as $k => $v) {
  187. $imgArticles[$k]->pinyin = $catiall[$v->catid][0];
  188. }
  189. }
  190. // 第二次查询:imgurl 为空的数据
  191. if ($textSize > 0) {
  192. $textArticles = Db::table('article')
  193. ->leftJoin('article_ignore', function ($join) use ($website_id) {
  194. $join->on('article.id', '=', 'article_ignore.article_id')->where('article_ignore.website_id', '=', $website_id);
  195. })
  196. // ->whereNull('article_ignore.article_id')
  197. ->where(function ($query) {
  198. $query->whereNull('article_ignore.article_id')
  199. ->orWhere(function ($subQuery) {
  200. $subQuery->whereNotNull('article_ignore.article_id')
  201. ->where('article_ignore.is_ignore', 0)
  202. ->where(function ($subSubQuery) {
  203. $subSubQuery->where('article_ignore.c_show_time', '<=', date('Y-m-d H:i:s'))
  204. ->orWhereNull('article_ignore.c_show_time');
  205. });
  206. });
  207. })
  208. ->whereIn('catid', $temp_arr = array_merge([$category_id], $cat_1st_arr[$category_id]))
  209. ->where('status', 1)
  210. ->where('status', 1)->limit($textSize)->orderBy('updated_at', 'desc')->get()->toArray();
  211. //拼接上拼音
  212. foreach ($textArticles as $k => $v) {
  213. $textArticles[$k]->pinyin = $catiall[$v->catid][0];
  214. }
  215. }
  216. }
  217. // var_dump($level, 'level');
  218. if ($level > 0) {
  219. // 取出一级的所有子级
  220. //取出所有的子级的imgurl 不为空的数据
  221. if ($imgSize > 0) {
  222. $imgArticles = Db::table('article')
  223. ->leftJoin('article_ignore', function ($join) use ($website_id) {
  224. $join->on('article.id', '=', 'article_ignore.article_id')->where('article_ignore.website_id', '=', $website_id);
  225. })
  226. // ->whereNull('article_ignore.article_id')
  227. ->where(function ($query) {
  228. $query->whereNull('article_ignore.article_id')
  229. ->orWhere(function ($subQuery) {
  230. $subQuery->whereNotNull('article_ignore.article_id')
  231. ->where('article_ignore.is_ignore', 0)
  232. ->where(function ($subSubQuery) {
  233. $subSubQuery->where('article_ignore.c_show_time', '<=', date('Y-m-d H:i:s'))
  234. ->orWhereNull('article_ignore.c_show_time');
  235. });
  236. });
  237. })
  238. ->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();
  239. //拼接上拼音
  240. foreach ($imgArticles as $k => $v) {
  241. $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  242. }
  243. }
  244. //取出所有的子级的imgurl 为空的数据 ->whereNull('imgurl')
  245. if ($textSize > 0) {
  246. $textArticles = Db::table('article')->where('status', 1)
  247. ->leftJoin('article_ignore', function ($join) use ($website_id) {
  248. $join->on('article.id', '=', 'article_ignore.article_id')->where('article_ignore.website_id', '=', $website_id);
  249. })
  250. // ->whereNull('article_ignore.article_id')
  251. ->where(function ($query) {
  252. $query->whereNull('article_ignore.article_id')
  253. ->orWhere(function ($subQuery) {
  254. $subQuery->whereNotNull('article_ignore.article_id')
  255. ->where('article_ignore.is_ignore', 0)
  256. ->where(function ($subSubQuery) {
  257. $subSubQuery->where('article_ignore.c_show_time', '<=', date('Y-m-d H:i:s'))
  258. ->orWhereNull('article_ignore.c_show_time');
  259. });
  260. });
  261. })
  262. ->whereRaw('MATCH(level_text) AGAINST(? IN NATURAL LANGUAGE MODE)', [$level])->whereIn('catid', $categoryIds)->limit($textSize)->orderBy('updated_at', 'desc')->get()->toArray();
  263. //拼接上拼音
  264. foreach ($textArticles as $k => $v) {
  265. $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  266. }
  267. }
  268. }
  269. $componetList[$key]['componentData']['data'][$k1]['alias'] = $websiteInfoIndexed[$category_id]->alias ?? 'aliasnull' . $category_id;
  270. $componetList[$key]['componentData']['data'][$k1]['pinyin'] = $websiteInfoIndexed[$category_id]->pinyin ?? 'aliasnull' . $category_id;
  271. $componetList[$key]['componentData']['data'][$k1]['category_id'] = $category_id;
  272. $componetList[$key]['componentData']['data'][$k1]['textnum'] = $textArticles ?? [];
  273. $componetList[$key]['componentData']['data'][$k1]['imgnum'] = $imgArticles ?? [];
  274. }
  275. } else {
  276. $category_id = $componentData['category_id'] ?? 0;
  277. //此处应该有值
  278. $imgSize = $componentData['imgSize'];
  279. $textSize = $componentData['textSize'];
  280. $level = $componentData['level'];
  281. $child = $componentData['child'];
  282. //获取子级
  283. if ($child) {
  284. $child_id = $child['id'] ?? 0;
  285. $child_imgSize = $child['imgSize'] ?? 0;
  286. $child_textSize = $child['textSize'] ?? 0;
  287. }
  288. // 拼音就是路由----
  289. // unset($listType['pinyin']);
  290. //查询查询数据,返回到data字段里,根据这几个
  291. if ($category_id > 0) {
  292. // var_dump($category_id, '------------------category_id');
  293. // var_dump($cat_1st_arr, '-----------1-------2');
  294. // 第一次查询:imgurl 不为空的数据
  295. if ($imgSize > 0) {
  296. $imgArticles = Db::table('article')
  297. ->leftJoin('article_ignore', function ($join) use ($website_id) {
  298. $join->on('article.id', '=', 'article_ignore.article_id')
  299. ->where('article_ignore.website_id', '=', $website_id);
  300. })
  301. // ->whereNull('article_ignore.article_id')
  302. ->where(function ($query) {
  303. $query->whereNull('article_ignore.article_id')
  304. ->orWhere(function ($subQuery) {
  305. $subQuery->whereNotNull('article_ignore.article_id')
  306. ->where('article_ignore.is_ignore', 0)
  307. ->where(function ($subSubQuery) {
  308. $subSubQuery->where('article_ignore.c_show_time', '<=', date('Y-m-d H:i:s'))
  309. ->orWhereNull('article_ignore.c_show_time');
  310. });
  311. });
  312. })
  313. ->whereIn('catid', $temp_arr = array_merge([$category_id], $cat_1st_arr[$category_id]))->where('status', 1)
  314. ->whereNotNull('imgurl')->where('imgurl', '!=', '')->limit($imgSize)->orderBy('updated_at', 'desc')->get()->toArray();
  315. //拼接上拼音
  316. foreach ($imgArticles as $k => $v) {
  317. $imgArticles[$k]->pinyin = $catiall[$v->catid][0];
  318. }
  319. }
  320. // 第二次查询:imgurl 为空的数据
  321. if ($textSize > 0) {
  322. $textArticles = Db::table('article')
  323. ->leftJoin('article_ignore', function ($join) use ($website_id) {
  324. $join->on('article.id', '=', 'article_ignore.article_id')->where('article_ignore.website_id', '=', $website_id);
  325. })
  326. // ->whereNull('article_ignore.article_id')
  327. ->where(function ($query) {
  328. $query->whereNull('article_ignore.article_id')
  329. ->orWhere(function ($subQuery) {
  330. $subQuery->whereNotNull('article_ignore.article_id')
  331. ->where('article_ignore.is_ignore', 0)
  332. ->where(function ($subSubQuery) {
  333. $subSubQuery->where('article_ignore.c_show_time', '<=', date('Y-m-d H:i:s'))
  334. ->orWhereNull('article_ignore.c_show_time');
  335. });
  336. });
  337. })
  338. ->whereIn('catid', $temp_arr = array_merge([$category_id], $cat_1st_arr[$category_id]))
  339. ->where('status', 1)
  340. ->where('status', 1)->limit($textSize)->orderBy('updated_at', 'desc')->get()->toArray();
  341. //拼接上拼音
  342. foreach ($textArticles as $k => $v) {
  343. $textArticles[$k]->pinyin = $catiall[$v->catid][0];
  344. }
  345. }
  346. }
  347. // var_dump($level, 'level');
  348. if ($level > 0) {
  349. // 取出一级的所有子级
  350. //取出所有的子级的imgurl 不为空的数据
  351. if ($imgSize > 0) {
  352. $imgArticles = Db::table('article')
  353. ->leftJoin('article_ignore', function ($join) use ($website_id) {
  354. $join->on('article.id', '=', 'article_ignore.article_id')->where('article_ignore.website_id', '=', $website_id);
  355. })
  356. // ->whereNull('article_ignore.article_id')
  357. ->where(function ($query) {
  358. $query->whereNull('article_ignore.article_id')
  359. ->orWhere(function ($subQuery) {
  360. $subQuery->whereNotNull('article_ignore.article_id')
  361. ->where('article_ignore.is_ignore', 0)
  362. ->where(function ($subSubQuery) {
  363. $subSubQuery->where('article_ignore.c_show_time', '<=', date('Y-m-d H:i:s'))
  364. ->orWhereNull('article_ignore.c_show_time');
  365. });
  366. });
  367. })
  368. ->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();
  369. //拼接上拼音
  370. foreach ($imgArticles as $k => $v) {
  371. $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  372. }
  373. }
  374. //取出所有的子级的imgurl 为空的数据 ->whereNull('imgurl')
  375. if ($textSize > 0) {
  376. $textArticles = Db::table('article')->where('status', 1)
  377. ->leftJoin('article_ignore', function ($join) use ($website_id) {
  378. $join->on('article.id', '=', 'article_ignore.article_id')->where('article_ignore.website_id', '=', $website_id);
  379. })
  380. // ->whereNull('article_ignore.article_id')
  381. ->where(function ($query) {
  382. $query->whereNull('article_ignore.article_id')
  383. ->orWhere(function ($subQuery) {
  384. $subQuery->whereNotNull('article_ignore.article_id')
  385. ->where('article_ignore.is_ignore', 0)
  386. ->where(function ($subSubQuery) {
  387. $subSubQuery->where('article_ignore.c_show_time', '<=', date('Y-m-d H:i:s'))
  388. ->orWhereNull('article_ignore.c_show_time');
  389. });
  390. });
  391. })
  392. ->whereRaw('MATCH(level_text) AGAINST(? IN NATURAL LANGUAGE MODE)', [$level])->whereIn('catid', $categoryIds)->limit($textSize)->orderBy('updated_at', 'desc')->get()->toArray();
  393. //拼接上拼音
  394. foreach ($textArticles as $k => $v) {
  395. $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  396. }
  397. }
  398. }
  399. $componetList[$key]['componentData']['data']['alias'] = $websiteInfoIndexed[$category_id]->alias ?? 'aliasnull' . $category_id;
  400. $componetList[$key]['componentData']['data']['pinyin'] = $websiteInfoIndexed[$category_id]->pinyin ?? 'aliasnull' . $category_id;
  401. $componetList[$key]['componentData']['data']['category_id'] = $category_id;
  402. $componetList[$key]['componentData']['data']['textnum'] = $textArticles ?? [];
  403. $componetList[$key]['componentData']['data']['imgnum'] = $imgArticles ?? [];
  404. }
  405. // $page = $componentData['page'];
  406. // $pageSize = $componentData['pageSize'];
  407. // $listType = $componentData['listType'];
  408. //将查询到的数据放到componentData里
  409. //判断child
  410. var_dump($child_id, '--------child_id---');
  411. if ($child_id > 0) {
  412. //取出子级的imgurl 不为空的数据
  413. if ($child_imgSize > 0) {
  414. $child_imgArticles = Db::table('article')->where('catid', $child_id)
  415. ->where('status', 1)
  416. ->whereNotNull('imgurl')->limit($child_imgSize)->orderBy('updated_at', 'desc')->get()->toArray();
  417. } else {
  418. $child_imgArticles = [];
  419. }
  420. //取出子级的imgurl 为空的数据
  421. if ($child_textSize > 0) {
  422. $child_textArticles = Db::table('article')
  423. ->where('status', 1)
  424. ->where('catid', $child_id)->whereNull('imgurl')->limit($child_textSize)->orderBy('updated_at', 'desc')->get()->toArray();
  425. } else {
  426. $child_textArticles = [];
  427. }
  428. //将查询到的数据放到componentData里
  429. $componetList[$key]['componentData']['data']['child']['alias'] = $websiteInfoIndexed[$child_id]->alias;
  430. $componetList[$key]['componentData']['data']['child']['pinyin'] = $websiteInfoIndexed[$child_id]->pinyin;
  431. $componetList[$key]['componentData']['data']['child']['category_id'] = $child_id;
  432. $componetList[$key]['componentData']['data']['child']['textnum'] = $child_textArticles;
  433. $componetList[$key]['componentData']['data']['child']['imgnum'] = $child_imgArticles;
  434. $componetList[$key]['componentData']['data']['child']['is_url'] = $websiteInfoIndexed[$child_id]->is_url ?? 0;
  435. //取出自己
  436. $all_childcat = $cat_1st_arr[$category_id];
  437. $componetList[$key]['componentData']['data']['child']['children_count'] = count($all_childcat);
  438. $processedChildCat = array_map(function ($v) use ($websiteInfoIndexed, $cat_1st_arr) {
  439. // 从 $websiteInfoIndexed 中获取对应的数据
  440. $info = $websiteInfoIndexed[$v];
  441. var_dump($info, $v, '-------child循环--------');
  442. // 返回一个包含所需信息的数组
  443. return [
  444. 'pinyin' => $info->pinyin,
  445. 'alias' => $info->alias,
  446. 'category_id' => $info->category_id,
  447. 'aLIas_pinyin' => $info->aLIas_pinyin,
  448. 'pid' => $info->pid,
  449. 'cat_arr_id' => $info->category_arr_id ?? ['出错'],
  450. 'is_url' => $info->is_url ?? 0,
  451. 'children_count' => isset($cat_1st_arr[$v]) ? count($cat_1st_arr[$v]) : 0
  452. ];
  453. }, $all_childcat);
  454. $componetList[$key]['componentData']['data']['child']['all_childcat'] = $processedChildCat ?? [];
  455. }
  456. }
  457. $data['template']['index'][$k_ceng]['componentList'] = $componetList;
  458. }
  459. //将修改后的数据放到template里
  460. break;
  461. case 'class':
  462. if (!isset($data['template']['class']['data'][0]['componentList'])) {
  463. break;
  464. }
  465. $componetList = $data['template']['class']['data'][0]['componentList'];
  466. $class_count = count($componetList);
  467. $parent_id = $data['template']['class']['parent_id'] ?? 0;
  468. //获取子级;
  469. $child = $cat_1st_arr[$parent_id]; //获取子级
  470. // var_dump($child, 'child');
  471. //如果$child的数量小于$class_count,就填充一样多的数据给child,如果大于就截取一样的数量给child
  472. if (count($child) < $class_count) {
  473. $child = array_pad($child, $class_count, end($child) ?? 0);
  474. } elseif (count($child) > $class_count) {
  475. $child = array_slice($child, 0, $class_count);
  476. }
  477. foreach ($componetList as $key => $value) {
  478. $componentData = $value['componentData'];
  479. //取出imgSize和textSize,获取相应的文章,
  480. $category_id = $child[$key] ?? 0;
  481. $imgSize = $componentData['imgSize'] ?? 0;
  482. $textSize = $componentData['textSize'] ?? 0;
  483. $listType = $componentData['listType'] ?? [];
  484. var_dump($listType, 'before unset listType');
  485. // 定义要移除的元素数组
  486. $valuesToRemove = ['category_name', 'pinyin'];
  487. // 循环移除每个元素
  488. foreach ($valuesToRemove as $value) {
  489. while (($key = array_search($value, $listType)) !== false) {
  490. // 移除该元素
  491. unset($listType[$key]);
  492. }
  493. }
  494. var_dump($listType, 'listType');
  495. //child
  496. $child = $componentData['child'] ?? [];
  497. $data_class = [];
  498. //查询imgurl不为空的数据
  499. $imgArticles = Db::table('article')
  500. ->where('catid', $category_id)
  501. ->whereNotNull('imgurl')
  502. ->limit($imgSize)
  503. ->leftJoin('article_ignore', function ($join) use ($website_id) {
  504. $join->on('article.id', '=', 'article_ignore.article_id')->where('article_ignore.website_id', '=', $website_id);
  505. })
  506. ->where(function ($query) {
  507. $query->whereNull('article_ignore.article_id')
  508. ->orWhere(function ($subQuery) {
  509. $subQuery->whereNotNull('article_ignore.article_id')
  510. ->where('article_ignore.is_ignore', 0)
  511. ->where(function ($subSubQuery) {
  512. $subSubQuery->where('article_ignore.c_show_time', '<=', date('Y-m-d H:i:s'))
  513. ->orWhereNull('article_ignore.c_show_time');
  514. });
  515. });
  516. })
  517. // ->select($listType)
  518. ->orderBy('updated_at', 'desc')
  519. ->get()
  520. ->toArray();
  521. //拼接上拼音
  522. foreach ($imgArticles as $k => $v) {
  523. $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  524. }
  525. //查询imgurl为空的数据
  526. $textArticles = Db::table('article')
  527. ->where('catid', $category_id)
  528. ->whereNull('imgurl')
  529. ->limit($textSize)
  530. ->leftJoin('article_ignore', function ($join) use ($website_id) {
  531. $join->on('article.id', '=', 'article_ignore.article_id')->where('article_ignore.website_id', '=', $website_id);
  532. })
  533. ->where(function ($query) {
  534. $query->whereNull('article_ignore.article_id')
  535. ->orWhere(function ($subQuery) {
  536. $subQuery->whereNotNull('article_ignore.article_id')
  537. ->where('article_ignore.is_ignore', 0)
  538. ->where(function ($subSubQuery) {
  539. $subSubQuery->where('article_ignore.c_show_time', '<=', date('Y-m-d H:i:s'))
  540. ->orWhereNull('article_ignore.c_show_time');
  541. });
  542. });
  543. })
  544. ->orderBy('updated_at', 'desc')
  545. ->get()
  546. ->toArray();
  547. //拼接上拼音
  548. foreach ($textArticles as $k => $v) {
  549. $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  550. }
  551. $data_class['alias'] = $websiteInfoIndexed[$category_id]->alias ?? 'aliasnull' . $category_id;
  552. $data_class['pinyin'] = $websiteInfoIndexed[$category_id]->pinyin ?? 'aliasnull' . $category_id;
  553. $data_class['category_id'] = $category_id;
  554. $data_class['textnum'] = $textArticles ?? [];
  555. $data_class['imgnum'] = $imgArticles ?? [];
  556. $data['data'] = $data_class;
  557. }
  558. break;
  559. case 'list':
  560. if (!isset($data['template']['list']['data'][0]['componentList'])) {
  561. break;
  562. }
  563. $componetListdata = $data['template']['list']['data'];
  564. $category_id = $data['template']['list']['category_id'] ?? 0;
  565. foreach ($componetListdata as $k => $v) {
  566. if (!isset($v['componentList']) || !is_array($v['componentList'])) {
  567. continue; // 如果 componentList 不存在或不是数组,跳过当前循环
  568. }
  569. $componentList = &$data['template']['list']['data'][$k]['componentList'];
  570. var_dump(count($componentList), 'componentList111');
  571. foreach ($componentList as $key => $value) {
  572. $componentData = $value['componentData'];
  573. if (isset($value['componentData']['category_id'])) {
  574. $value['componentData']['category_id'] = $category_id;
  575. $page = $componentData['papageTypege']['page'] ?? 1;
  576. $pageSize = $componentData['papageTypege']['pageSize'] ?? 10;
  577. $listType = $componentData['listType'];
  578. $articals = Db::table('article')
  579. ->where('catid', $category_id)
  580. ->where('status', 1)->leftJoin('article_ignore', function ($join) use ($website_id) {
  581. $join->on('article.id', '=', 'article_ignore.article_id')->where('article_ignore.website_id', '=', $website_id);
  582. })
  583. ->where(function ($query) {
  584. $query->whereNull('article_ignore.article_id')
  585. ->orWhere(function ($subQuery) {
  586. $subQuery->whereNotNull('article_ignore.article_id')
  587. ->where('article_ignore.is_ignore', 0)
  588. ->where(function ($subSubQuery) {
  589. $subSubQuery->where('article_ignore.c_show_time', '<=', date('Y-m-d H:i:s'))
  590. ->orWhereNull('article_ignore.c_show_time');
  591. });
  592. });
  593. })
  594. ->orderBy('updated_at', 'desc')
  595. ->get();
  596. // ->paginate($pageSize, ['*'], 'page', $page);
  597. //拼接上拼音
  598. foreach ($articals as $k => $v) {
  599. $articals[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  600. }
  601. $value['componentData']['data'] = $articals;
  602. }
  603. var_dump(isset($value['componentData']['level']), $key, '---------------------------------------');
  604. if (isset($value['componentData']['level'])) {
  605. //取出imgSize和textSize,获取相应的文章,
  606. $imgSize = $componentData['imgnum'] ?? 0;
  607. $textSize = $componentData['textnum'] ?? 0;
  608. $listType = $componentData['listType'] ?? [];
  609. // 定义要移除的元素数组
  610. $valuesToRemove = ['category_name', 'pinyin'];
  611. // 循环移除每个元素
  612. foreach ($valuesToRemove as $value) {
  613. while (($key = array_search($value, $listType)) !== false) {
  614. // 移除该元素
  615. unset($listType[$key]);
  616. }
  617. }
  618. //查询imgurl不为空的数据
  619. if ($imgSize > 0) {
  620. $imgArticles = Db::table('article')
  621. ->where('catid', $category_id)
  622. ->whereNotNull('imgurl')
  623. ->where('status', 1)->leftJoin('article_ignore', function ($join) use ($website_id) {
  624. $join->on('article.id', '=', 'article_ignore.article_id')->where('article_ignore.website_id', '=', $website_id);
  625. })
  626. ->where(function ($query) {
  627. $query->whereNull('article_ignore.article_id')
  628. ->orWhere(function ($subQuery) {
  629. $subQuery->whereNotNull('article_ignore.article_id')
  630. ->where('article_ignore.is_ignore', 0)
  631. ->where(function ($subSubQuery) {
  632. $subSubQuery->where('article_ignore.c_show_time', '<=', date('Y-m-d H:i:s'))
  633. ->orWhereNull('article_ignore.c_show_time');
  634. });
  635. });
  636. })
  637. ->limit($imgSize)
  638. ->orderBy('updated_at', 'desc')
  639. ->get()
  640. ->toArray();
  641. //拼接上拼音
  642. foreach ($imgArticles as $k => $v) {
  643. $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  644. }
  645. }
  646. //查询imgurl为空的数据
  647. if ($textSize > 0) {
  648. $textArticles = Db::table('article')
  649. ->where('catid', $category_id)
  650. ->whereNull('imgurl')
  651. ->where('status', 1)->leftJoin('article_ignore', function ($join) use ($website_id) {
  652. $join->on('article.id', '=', 'article_ignore.article_id')->where('article_ignore.website_id', '=', $website_id);
  653. })
  654. ->where(function ($query) {
  655. $query->whereNull('article_ignore.article_id')
  656. ->orWhere(function ($subQuery) {
  657. $subQuery->whereNotNull('article_ignore.article_id')
  658. ->where('article_ignore.is_ignore', 0)
  659. ->where(function ($subSubQuery) {
  660. $subSubQuery->where('article_ignore.c_show_time', '<=', date('Y-m-d H:i:s'))
  661. ->orWhereNull('article_ignore.c_show_time');
  662. });
  663. });
  664. })
  665. ->limit($textSize)
  666. ->orderBy('updated_at', 'desc')
  667. ->get()
  668. ->toArray();
  669. //拼接上拼音
  670. foreach ($textArticles as $k => $v) {
  671. $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  672. }
  673. }
  674. //将查询到的数据放到componentData里
  675. $componetList[$key]['componentData']['data']['alias'] = $websiteInfoIndexed[$category_id]->alias ?? 'aliasnull' . $category_id;
  676. $componetList[$key]['componentData']['data']['pinyin'] = $websiteInfoIndexed[$category_id]->pinyin ?? 'aliasnull' . $category_id;
  677. $componetList[$key]['componentData']['data']['category_id'] = $category_id;
  678. $componetList[$key]['componentData']['data']['textnum'] = $textArticles ?? [];
  679. $componetList[$key]['componentData']['data']['imgnum'] = $imgArticles ?? [];
  680. //将修改后的数据放到template里
  681. // $data['template']['list']['data'][0]['componentList'] = $componetList;
  682. }
  683. }
  684. }
  685. break;
  686. case 'article':
  687. if (!isset($data['template']['article']['data'])) {
  688. break;
  689. }
  690. $article_id = 51434; //假设这是文章ID
  691. $componetListdata = &$data['template']['article']['data'];
  692. var_dump($categoryIds, '----------------');
  693. // $category_id = $data['template']['article']['category_id'] ?? 0;
  694. foreach ($componetListdata as $k => $v) {
  695. foreach ($v['componentList'] as $key => $value) {
  696. $componentData = $value['componentData'];
  697. if (isset($value['componentData']['article_id'])) {
  698. $value['componentData']['article_id'] = $article_id;
  699. // $page = $componentData['papageTypege']['page'] ?? 1;
  700. // $pageSize = $componentData['papageTypege']['pageSize'] ?? 10;
  701. $listType = $componentData['listType'];
  702. //查询数据
  703. $articals = Db::table('article')
  704. ->where('id', $article_id)
  705. ->where('status', 1)
  706. ->leftJoin('article_data', 'article.id', '=', 'article_data.article_id')
  707. ->select('article.*', 'article_data.content')
  708. ->orderBy('updated_at', 'desc')
  709. ->get();
  710. //拼接上拼音
  711. $value['componentData']['data'] = $articals;
  712. }
  713. if (isset($value['componentData']['level'])) {
  714. //:1:头条 2:轮播图 3:推荐图 4:最新新闻;5:推荐新闻;6:热点资讯 7:自动关联
  715. //4 update 5 30天之内 6 库中
  716. $level = $componentData['level'] ?? 0;
  717. $imgSize = $componentData['imgnum'] ?? 0;
  718. $textSize = $componentData['textnum'] ?? 0;
  719. if (in_array($level, [1, 2, 3, 6])) {
  720. //查询imgurl不为空的数据
  721. $imgArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNotNull('imgurl')
  722. ->where('status', 1)
  723. ->whereRaw('MATCH(level_text) AGAINST(? IN NATURAL LANGUAGE MODE)', [$level])
  724. ->limit($imgSize)->orderBy('updated_at', 'desc')->get()->toArray();
  725. //拼接上拼音
  726. foreach ($imgArticles as $k => $v) {
  727. $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  728. }
  729. //查询imgurl为空的数据
  730. $textArticles = Db::table('article')->where('catid', $categoryIds)->whereNull('imgurl')
  731. ->where('status', 1)
  732. ->whereRaw('MATCH(level_text) AGAINST(? IN NATURAL LANGUAGE MODE)', [$level])
  733. ->limit($textSize)->orderBy('updated_at', 'desc')->get()->toArray();
  734. //拼接上拼音
  735. foreach ($textArticles as $k => $v) {
  736. $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  737. }
  738. } else if ($level == 4) {
  739. //查询imgurl不为空的数据
  740. $imgArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNotNull('imgurl')
  741. ->where('status', 1)
  742. ->limit($imgSize)->orderBy('updated_at', 'desc')->get()->toArray();
  743. //拼接上拼音
  744. if (!empty($imgArticles)) {
  745. foreach ($imgArticles as $k => $v) {
  746. $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  747. }
  748. }
  749. //查询imgurl为空的数据
  750. $textArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNull('imgurl')
  751. ->where('status', 1)
  752. ->limit($textSize)->orderBy('updated_at', 'desc')->get()->toArray();
  753. //拼接上拼音
  754. var_dump('---4------');
  755. if (!empty($textArticles)) {
  756. foreach ($textArticles as $k => $v) {
  757. $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  758. }
  759. }
  760. } else if ($level == 5) {
  761. var_dump($value['componentData']['level'], 'level');
  762. //查询imgurl不为空的数据
  763. $imgArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNotNull('imgurl')
  764. ->where('status', 1)
  765. //最近三十天
  766. ->where('updated_at', '>=', date('Y-m-d H:i:s', strtotime('-30 days')))
  767. ->limit($imgSize)->orderBy('updated_at', 'desc')->get()->toArray();
  768. //拼接上拼音
  769. if (!empty($imgArticles)) {
  770. foreach ($imgArticles as $k => $v) {
  771. $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  772. }
  773. }
  774. //查询imgurl为空的数据
  775. $textArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNull('imgurl')
  776. ->where('status', 1)
  777. ->where('updated_at', '>=', date('Y-m-d H:i:s', strtotime('-30 days')))
  778. ->limit($textSize)->orderBy('updated_at', 'desc')->get()->toArray();
  779. //拼接上拼音
  780. var_dump('---5------');
  781. if (!empty($textArticles)) {
  782. foreach ($textArticles as $k => $v) {
  783. $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  784. }
  785. }
  786. } else if ($level == 7) {
  787. //获取artical的keyword
  788. $keword = DB::table('artical')->where('id', $article_id)->select('keyword')->get();
  789. $kewordl = $keword[0]->keyword;
  790. $kewordl = explode(',', $kewordl);
  791. //筛选出符合条件的article来
  792. $imgArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNotNull('imgurl')
  793. ->where('status', 1)
  794. // ->whereJsonContains('keyword', $kewordl)
  795. ->whereRaw('MATCH(keyword) AGAINST(? IN NATURAL LANGUAGE MODE)', [$kewordl])
  796. ->limit($imgSize)->orderBy('updated_at', 'desc')->get()->toArray();
  797. //拼接上拼音
  798. foreach ($imgArticles as $k => $v) {
  799. $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  800. }
  801. $textArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNull('imgurl')
  802. ->where('status', 1)
  803. // ->whereJsonContains('keyword', $kewordl)
  804. ->whereRaw('MATCH(keyword) AGAINST(? IN NATURAL LANGUAGE MODE)', [$kewordl])
  805. ->limit($textSize)->orderBy('updated_at', 'desc')->get()->toArray();
  806. //拼接上拼音
  807. foreach ($textArticles as $k => $v) {
  808. $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  809. }
  810. }
  811. }
  812. $value['componentData']['data']['imgnum'] = $imgArticles ?? [];
  813. $value['componentData']['data']['textnum'] = $textArticles ?? [];
  814. }
  815. }
  816. break;
  817. case 'search':
  818. break;
  819. case 'aboutList':
  820. $fact_id = 7;
  821. $componetList = &$data['template']['aboutList']['componentData'];
  822. $componetList['fact_id'] = $fact_id;
  823. $content = Db::table('footer_content')
  824. ->where('fcat_id', $fact_id)
  825. ->orderBy('updated_at', 'desc')
  826. ->get()
  827. ->toArray();
  828. $componetList['data'] = $content;
  829. break;
  830. case 'aboutArticle':
  831. $fact_id = 7;
  832. $componetList = &$data['template']['aboutList']['componentData'];
  833. $componetList['fact_id'] = $fact_id;
  834. $content = Db::table('footer_content')
  835. ->where('id', $fact_id)
  836. ->orderBy('updated_at', 'desc')
  837. ->get()
  838. ->toArray();
  839. $componetList['data'] = $content;
  840. break;
  841. default:
  842. $componetList = [];
  843. break;
  844. }
  845. return $data;
  846. }
  847. public function addWeb($data)
  848. {
  849. $result = WebsiteTemplate::insertGetId($data);
  850. if ($result) {
  851. return Result::success($result);
  852. } else {
  853. return Result::error('添加失败');
  854. }
  855. }
  856. public function deleteWeb($data)
  857. {
  858. $result = WebsiteTemplate::where('id', $data['id'])->delete();
  859. if ($result) {
  860. return Result::success($result);
  861. } else {
  862. return Result::error('删除失败');
  863. }
  864. }
  865. public function updateWeb($data)
  866. {
  867. $result = WebsiteTemplate::where('id', $data['id'])->update($data);
  868. if ($result) {
  869. return Result::success($result);
  870. } else {
  871. return Result::error('更新失败');
  872. }
  873. }
  874. public function getWebInfo($data)
  875. {
  876. $result = WebsiteTemplate::where('website_template.id', $data['id'])
  877. ->leftJoin('website', 'website.id', '=', 'website_template.website_id')
  878. ->select('website_template.*', 'website.website_name as website_name', 'website.website_url as website_url')
  879. ->first();
  880. return Result::success($result);
  881. }
  882. public function getWebList($data)
  883. {
  884. var_dump($data, 'data');
  885. $where = [];
  886. if (isset($data['website_name'])) {
  887. $where[] = ['website.website_name', 'like', '%' . $data['website_name'] . '%'];
  888. }
  889. if (isset($data['status'])) {
  890. $where[] = ['website_template.status', '=', $data['status']];
  891. }
  892. $result = WebsiteTemplate::where([])
  893. ->where($where)
  894. ->leftJoin('website', 'website.id', '=', 'website_template.website_id')
  895. ->select('website_template.*', 'website.website_name as website_name', 'website.website_url as website_url')
  896. ->paginate($data['pageSize'], ['*'], 'page', $data['page']);
  897. if ($result) {
  898. return Result::success($result);
  899. } else {
  900. return Result::error('获取列表失败');
  901. }
  902. }
  903. public function updateWebConfig($data)
  904. {
  905. var_dump($data, 'data1');
  906. // return Result::success($data);
  907. $where = [];
  908. if (isset($data['website_id'])) {
  909. $where['website_template.website_id'] = $data['website_id'];
  910. }
  911. $result = WebsiteTemplate::where([])
  912. ->where($where)
  913. ->leftJoin('website', 'website.id', '=', 'website_template.website_id')
  914. ->select('website_template.id', 'website_template.port', 'website.website_name as website_name', 'website.website_url as website_url', 'website_template.siteId')
  915. ->first()->toArray();
  916. var_dump($result, 'result');
  917. $domain = json_decode($result['website_url'], true);
  918. $domainlist = $domain;
  919. var_dump(empty($result['port']), 'port');
  920. $domain = $domain[3];
  921. if ($domain == null || empty($domain) || !isset($domain)) {
  922. return Result::error('域名不存在');
  923. }
  924. $port = $result['port'];
  925. if ($port == null || empty($port) || !isset($port)) {
  926. var_dump($port, 'youp0------port');
  927. return Result::error('端口不存在');
  928. }
  929. $postData = [
  930. 'domain' => $domain,
  931. 'port' => $port
  932. ];
  933. var_dump(empty($result['siteId']), 'siteId---');
  934. if (empty($result['siteId'])) {
  935. //实例化对象
  936. $api = new bt_api();
  937. //获取面板日志
  938. $r_data = $api->GetLogs();
  939. //输出JSON数据到浏览器
  940. echo json_encode($r_data);
  941. $webname = [
  942. 'domain' => $domain,
  943. 'domainlist' => $domainlist,
  944. 'count' => count($domainlist) - 1
  945. ];
  946. $siteData = [
  947. 'path' => '/www2/www/wwwroot/' . $domain,
  948. 'ftp' => 'false',
  949. 'type' => 'PHP',
  950. 'type_id' => 0,
  951. 'ps' => $domain,
  952. 'port' => 80,
  953. 'version' => '00',
  954. 'need_index' => 0,
  955. 'need_404' => 0,
  956. 'sql' => 'false',
  957. 'codeing' => 'utf8mb4',
  958. 'webname' => json_encode($webname),
  959. 'add_dns_record' => 'false'
  960. ];
  961. $r_data = $api->addSite($siteData);
  962. /*
  963. {
  964. "siteStatus": true,
  965. "siteId": 20,
  966. "ftpStatus": false,
  967. "databaseStatus": false,
  968. "gitStatus": false
  969. }
  970. */
  971. var_dump($r_data, '----------1--r_data--------');
  972. if (isset($r_data['status']) && $r_data['status'] == false) {
  973. return Result::error('宝塔添加失败' . $r_data['msg']);
  974. }
  975. if (isset($r_data['siteStatus']) && $r_data['siteStatus'] == true) {
  976. //记录siteId
  977. $siteId = $r_data['siteId'];
  978. //更新网站模板的siteId WebsiteTemplate
  979. WebsiteTemplate::where('website_id', $data['website_id'])
  980. ->update([
  981. 'siteId' => $siteId,
  982. 'dir' => '/www2/www/wwwroot/' . $domain,
  983. 'status' => 2
  984. ]);
  985. }
  986. // return Result::success($r_data);
  987. }
  988. // 使用Guzzle HTTP客户端发送POST请求
  989. try {
  990. $client = new \GuzzleHttp\Client();
  991. $response = $client->post(env('CREATE_WEB', 'http://adminpre.bjzxtw.org.cn/create.php'), [
  992. 'json' => $postData,
  993. 'timeout' => 30,
  994. 'headers' => [
  995. 'Content-Type' => 'application/json',
  996. ]
  997. ]);
  998. // 获取响应内容
  999. $responseBody = $response->getBody()->getContents();
  1000. $responseData = json_decode($responseBody, true);
  1001. var_dump($responseData, '脚本返回数据------------------------');
  1002. // 检查响应是否为有效的JSON
  1003. if (json_last_error() === JSON_ERROR_NONE) {
  1004. // 如果远程服务器返回成功结果
  1005. if (isset($responseData['code']) && $responseData['code'] === 1) {
  1006. return Result::success([
  1007. 'message' => '远程服务器处理成功' . $responseData['message'],
  1008. 'output' => $responseData['output'],
  1009. 'file' => $responseData['file'],
  1010. 'code' => $responseData['code'],
  1011. 'exec' => $responseData['exec'],
  1012. 'domain' => $domain,
  1013. 'port' => $port,
  1014. 'remote_response' => $responseData
  1015. ]);
  1016. } else {
  1017. // 远程服务器返回错误
  1018. return Result::error(500, '远程服务器处理失败', [
  1019. 'remote_response' => $responseData
  1020. ]);
  1021. }
  1022. } else {
  1023. // 响应不是有效的JSON
  1024. return Result::success([
  1025. 'message' => '收到远程服务器响应(非JSON格式)',
  1026. 'domain' => $domain,
  1027. 'port' => $port,
  1028. 'raw_response' => $responseBody
  1029. ]);
  1030. }
  1031. } catch (\GuzzleHttp\Exception\RequestException $e) {
  1032. // HTTP请求异常
  1033. $errorMessage = 'HTTP请求失败';
  1034. if ($e->hasResponse()) {
  1035. $response = $e->getResponse();
  1036. $errorMessage .= ': ' . $response->getStatusCode() . ' ' . $response->getReasonPhrase();
  1037. } else {
  1038. $errorMessage .= ': ' . $e->getMessage();
  1039. }
  1040. return Result::error(500, $errorMessage, [
  1041. 'exception' => $e->getMessage()
  1042. ]);
  1043. } catch (\Exception $e) {
  1044. // 其他异常
  1045. return Result::error(500, '请求处理异常: ' . $e->getMessage(), [
  1046. 'exception' => $e->getMessage()
  1047. ]);
  1048. }
  1049. if ($result) {
  1050. return Result::success($result);
  1051. } else {
  1052. return Result::error('获取列表失败');
  1053. }
  1054. }
  1055. private function bt($data) {}
  1056. public function updateWebOutput($data)
  1057. {
  1058. // return Result::error('更新输出失败');
  1059. //根据源程序, 复制出来,更改配置文件,打包,
  1060. // $result = 1;
  1061. $projectRoot = dirname(dirname(__DIR__)); // 从当前文件向上两级目录
  1062. $projectDir = $projectRoot . '/../zizhujianzhan_web';
  1063. $command = "cd " . escapeshellarg($projectDir) . " && npm run build";
  1064. // 执行命令并获取输出
  1065. exec($command, $output, $returnCode);
  1066. // 输出结果
  1067. echo "Return Code: " . $returnCode . "\n";
  1068. echo "Output:\n";
  1069. foreach ($output as $line) {
  1070. echo $line . "\n";
  1071. }
  1072. $result = $returnCode;
  1073. if ($result) {
  1074. return Result::success($result);
  1075. } else {
  1076. return Result::error('更新输出失败');
  1077. }
  1078. }
  1079. public function updateWebMove($data)
  1080. {
  1081. //更改到dir目录,
  1082. $result = WebsiteTemplate::where('id', $data['id'])->update($data);
  1083. if ($result) {
  1084. return Result::success($result);
  1085. } else {
  1086. return Result::error('更新移动失败');
  1087. }
  1088. }
  1089. public function runWeb($data)
  1090. {
  1091. //运行网站, 先检查是否有配置文件, pm2启动
  1092. $result = WebsiteTemplate::where('id', $data['id'])->update($data);
  1093. if ($result) {
  1094. return Result::success($result);
  1095. } else {
  1096. return Result::error('运行失败');
  1097. }
  1098. }
  1099. public function stopWeb($data)
  1100. {
  1101. $result = WebsiteTemplate::where('id', $data['id'])->update($data);
  1102. if ($result) {
  1103. return Result::success($result);
  1104. } else {
  1105. return Result::error('停止失败');
  1106. }
  1107. }
  1108. }
  1109. class bt_api
  1110. {
  1111. private $BT_KEY; // 只声明,不初始化
  1112. private $BT_PANEL; // 只声明,不初始化
  1113. //如果希望多台面板,可以在实例化对象时,将面板地址与密钥传入
  1114. public function __construct($bt_panel = null, $bt_key = null)
  1115. {
  1116. // 在构造函数中使用 env() 初始化
  1117. $this->BT_KEY = $bt_key ?? env('BT_KEY', 'RN0wTXLHpKKBXXxyDJoovHMpBhyLVFWZ');
  1118. $this->BT_PANEL = $bt_panel ?? env('BT_PANEL', 'http://192.168.1.234:18888');
  1119. }
  1120. //示例取面板日志
  1121. public function GetLogs()
  1122. {
  1123. //拼接URL地址
  1124. $url = $this->BT_PANEL . '/data?action=getData';
  1125. //准备POST数据
  1126. $p_data = $this->GetKeyData(); //取签名
  1127. $p_data['table'] = 'logs';
  1128. $p_data['limit'] = 10;
  1129. $p_data['tojs'] = 'test';
  1130. //请求面板接口
  1131. $result = $this->HttpPostCookie($url, $p_data);
  1132. //解析JSON数据
  1133. $data = json_decode($result, true);
  1134. return $data;
  1135. }
  1136. public function addSite(array $siteData): array
  1137. {
  1138. //拼接URL地址
  1139. $url = $this->BT_PANEL . '/site?action=AddSite';
  1140. //准备POST数据
  1141. $p_data = $this->GetKeyData(); //取签名
  1142. // Merge site data with key data
  1143. $p_data = array_merge($p_data, $siteData);
  1144. //请求面板接口
  1145. $result = $this->HttpPostCookie($url, $p_data);
  1146. //解析JSON数据
  1147. $data = json_decode($result, true);
  1148. return $data;
  1149. }
  1150. /**
  1151. * 检查删除数据
  1152. * ids [21]
  1153. */
  1154. public function checkDelSite(array $siteData): array
  1155. {
  1156. //拼接URL地址
  1157. $url = $this->BT_PANEL . '/site?action=check_del_data';
  1158. //准备POST数据
  1159. $p_data = $this->GetKeyData(); //取签名
  1160. // Merge site data with key data
  1161. $p_data = array_merge($p_data, $siteData);
  1162. //请求面板接口
  1163. $result = $this->HttpPostCookie($url, $p_data);
  1164. //解析JSON数据
  1165. $data = json_decode($result, true);
  1166. return $data;
  1167. }
  1168. /**
  1169. * 删除网站
  1170. * id 21
  1171. * webname t2.lj
  1172. * path 1
  1173. */
  1174. public function delSite(array $siteData): array
  1175. {
  1176. //拼接URL地址
  1177. $url = $this->BT_PANEL . '/site?action=DeleteSite';
  1178. //准备POST数据
  1179. $p_data = $this->GetKeyData(); //取签名
  1180. // Merge site data with key data
  1181. $p_data = array_merge($p_data, $siteData);
  1182. //请求面板接口
  1183. $result = $this->HttpPostCookie($url, $p_data);
  1184. //解析JSON数据
  1185. $data = json_decode($result, true);
  1186. return $data;
  1187. }
  1188. /**
  1189. * 构造带有签名的关联数组
  1190. */
  1191. private function GetKeyData()
  1192. {
  1193. $now_time = time();
  1194. $p_data = array(
  1195. 'request_token' => md5($now_time . '' . md5($this->BT_KEY)),
  1196. 'request_time' => $now_time
  1197. );
  1198. return $p_data;
  1199. }
  1200. /**
  1201. * 发起POST请求
  1202. * @param String $url 目标网填,带http://
  1203. * @param Array|String $data 欲提交的数据
  1204. * @return string
  1205. */
  1206. private function HttpPostCookie($url, $data, $timeout = 60)
  1207. {
  1208. //定义cookie保存位置
  1209. $cookie_file = './' . md5($this->BT_PANEL) . '.cookie';
  1210. if (!file_exists($cookie_file)) {
  1211. $fp = fopen($cookie_file, 'w+');
  1212. fclose($fp);
  1213. }
  1214. $ch = curl_init();
  1215. curl_setopt($ch, CURLOPT_URL, $url);
  1216. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  1217. curl_setopt($ch, CURLOPT_POST, 1);
  1218. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  1219. curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
  1220. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
  1221. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  1222. curl_setopt($ch, CURLOPT_HEADER, 0);
  1223. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  1224. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  1225. $output = curl_exec($ch);
  1226. curl_close($ch);
  1227. return $output;
  1228. }
  1229. }