ClientService.php 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672
  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 indexData1(array $data): array
  848. {
  849. $time1 = microtime(true);
  850. // 解析模板数据
  851. $templateData = json_decode($data['template'], true);
  852. $website_id = $templateData['base']['website_id'] ?? 2;
  853. $componentList = $templateData['template']['index'][0]['componentList'];
  854. // 1️⃣ 获取网站分类信息
  855. $websiteInfo = Db::table('website_category')
  856. ->where('website_id', $website_id)
  857. ->get()
  858. ->toArray();
  859. // 构建索引和拼音映射
  860. $websiteInfoIndexed = array_column($websiteInfo, null, 'category_id');
  861. $categoryIds = array_column($websiteInfo, 'category_id');
  862. $aliasPinyins = array_column($websiteInfo, 'aLIas_pinyin');
  863. $cat_arr = array_combine($categoryIds, $aliasPinyins);
  864. // 构建 catiall 和 cat_1st_arr
  865. $catiall = [];
  866. $cat_1st_arr = [];
  867. foreach ($websiteInfo as $item) {
  868. $arrId = json_decode($item->category_arr_id);
  869. $pinyin_str = implode('/', array_map(fn($id) => $cat_arr[$id], $arrId));
  870. $pinyin_str = rtrim($pinyin_str, '/');
  871. $catiall[$item->category_id][] = $pinyin_str;
  872. $websiteInfoIndexed[$item->category_id]->pinyin = $pinyin_str;
  873. if ($item->pid == 0) {
  874. $cat_1st_arr[$item->category_id] = [];
  875. } else {
  876. $cat_1st_arr[$item->pid][] = $item->category_id;
  877. }
  878. }
  879. // 2️⃣ 提前聚合所有需要查询的 category_id 和限制参数
  880. $allCatIds = [];
  881. $imgLimits = [];
  882. $txtLimits = [];
  883. $childImgLimits = [];
  884. $childTxtLimits = [];
  885. foreach ($componentList as $item) {
  886. $componentData = $item['componentData'];
  887. $category_id = $componentData['category_id'] ?? 0;
  888. if ($category_id > 0) {
  889. $allCatIds[] = $category_id;
  890. $imgLimits[$category_id] = $componentData['imgSize'] ?? 0;
  891. $txtLimits[$category_id] = $componentData['textSize'] ?? 0;
  892. if (!empty($cat_1st_arr[$category_id])) {
  893. $allCatIds = array_merge($allCatIds, $cat_1st_arr[$category_id]);
  894. }
  895. }
  896. // 子级
  897. $child = $componentData['child'] ?? null;
  898. if ($child && isset($child['id']) && $child['id'] > 0) {
  899. $child_id = $child['id'];
  900. $childImgLimits[$child_id] = $child['imgSize'] ?? 0;
  901. $childTxtLimits[$child_id] = $child['textSize'] ?? 0;
  902. }
  903. }
  904. $allCatIds = array_unique(array_filter($allCatIds));
  905. // 3️⃣ 一次性查询所有 article 数据
  906. $allArticles = Db::table('article')
  907. ->whereIn('catid', $allCatIds)
  908. ->where('status', 1)
  909. ->get()
  910. ->toArray();
  911. // 构建按 imgurl 分类的文章集合
  912. $articlesByCat = ['img' => [], 'txt' => []];
  913. foreach ($allArticles as $article) {
  914. if (!empty($article->imgurl)) {
  915. $articlesByCat['img'][$article->catid][] = $article;
  916. } else {
  917. $articlesByCat['txt'][$article->catid][] = $article;
  918. }
  919. }
  920. // 4️⃣ 使用协程并发处理每个组件
  921. $results = [];
  922. foreach ($componentList as $key => $value) {
  923. $componentKey = $key;
  924. $componentData = $value['componentData'];
  925. Coroutine::create(function () use (&$results, $componentKey, $componentData, $websiteInfoIndexed, $cat_1st_arr, $catiall, $articlesByCat, $imgLimits, $txtLimits, $childImgLimits, $childTxtLimits) {
  926. $result = [];
  927. $category_id = $componentData['category_id'] ?? 0;
  928. $imgLimit = $imgLimits[$category_id] ?? 0;
  929. $txtLimit = $txtLimits[$category_id] ?? 0;
  930. if ($category_id > 0) {
  931. // 图文数据
  932. $imgArticles = array_slice($articlesByCat['img'][$category_id] ?? [], 0, $imgLimit);
  933. foreach ($imgArticles as &$v) {
  934. $v->pinyin = $catiall[$v->catid][0] ?? '';
  935. }
  936. // 纯文字数据
  937. $textArticles = array_slice($articlesByCat['txt'][$category_id] ?? [], 0, $txtLimit);
  938. foreach ($textArticles as &$v) {
  939. $v->pinyin = $catiall[$v->catid][0] ?? '';
  940. }
  941. $result['imgnum'] = $imgArticles;
  942. $result['textnum'] = $textArticles;
  943. $result['alias'] = $websiteInfoIndexed[$category_id]['alias'] ?? '';
  944. $result['pinyin'] = $websiteInfoIndexed[$category_id]['pinyin'] ?? '';
  945. $result['category_id'] = $category_id;
  946. }
  947. // 子级处理
  948. $child = $componentData['child'] ?? null;
  949. if ($child && isset($child['id']) && $child['id'] > 0) {
  950. $child_id = $child['id'];
  951. $c_imgLimit = $childImgLimits[$child_id] ?? 0;
  952. $c_txtLimit = $childTxtLimits[$child_id] ?? 0;
  953. $childImg = array_slice($articlesByCat['img'][$child_id] ?? [], 0, $c_imgLimit);
  954. foreach ($childImg as &$v) {
  955. $v->pinyin = $catiall[$v->catid][0] ?? '';
  956. }
  957. $childText = array_slice($articlesByCat['txt'][$child_id] ?? [], 0, $c_txtLimit);
  958. foreach ($childText as &$v) {
  959. $v->pinyin = $catiall[$v->catid][0] ?? '';
  960. }
  961. $result['child'] = [
  962. 'alias' => $websiteInfoIndexed[$child_id]['alias'] ?? '',
  963. 'pinyin' => $websiteInfoIndexed[$child_id]['pinyin'] ?? '',
  964. 'category_id' => $child_id,
  965. 'textnum' => $childText,
  966. 'imgnum' => $childImg,
  967. 'all_childcat' => array_map(fn($v) => $websiteInfoIndexed[$v], $cat_1st_arr[$category_id] ?? []),
  968. ];
  969. }
  970. $results[$componentKey] = $result;
  971. });
  972. }
  973. // 等待所有协程完成(生产环境应使用 WaitGroup)
  974. usleep(200000); // 示例中简单等待
  975. // 5️⃣ 回填结果到 componentList
  976. foreach ($componentList as $key => &$component) {
  977. $componentData = $component['componentData'];
  978. $category_id = $componentData['category_id'] ?? 0;
  979. if (isset($results[$key])) {
  980. $component['componentData']['data'] = $results[$key];
  981. }
  982. }
  983. // 6️⃣ 返回最终数据
  984. $templateData['template']['index'][0]['componentList'] = $componentList;
  985. return $templateData;
  986. }
  987. public function addWeb($data)
  988. {
  989. $result = WebsiteTemplate::insertGetId($data);
  990. if ($result) {
  991. return Result::success($result);
  992. } else {
  993. return Result::error('添加失败');
  994. }
  995. }
  996. public function deleteWeb($data)
  997. {
  998. $result = WebsiteTemplate::where('id', $data['id'])->delete();
  999. if ($result) {
  1000. return Result::success($result);
  1001. } else {
  1002. return Result::error('删除失败');
  1003. }
  1004. }
  1005. public function updateWeb($data)
  1006. {
  1007. $result = WebsiteTemplate::where('id', $data['id'])->update($data);
  1008. if ($result) {
  1009. return Result::success($result);
  1010. } else {
  1011. return Result::error('更新失败');
  1012. }
  1013. }
  1014. public function getWebInfo($data)
  1015. {
  1016. $result = WebsiteTemplate::where('website_template.id', $data['id'])
  1017. ->leftJoin('website', 'website.id', '=', 'website_template.website_id')
  1018. ->select('website_template.*', 'website.website_name as website_name', 'website.website_url as website_url')
  1019. ->first();
  1020. return Result::success($result);
  1021. }
  1022. public function getWebList($data)
  1023. {
  1024. var_dump($data, 'data');
  1025. $where = [];
  1026. if (isset($data['website_name'])) {
  1027. $where[] = ['website.website_name', 'like', '%' . $data['website_name'] . '%'];
  1028. }
  1029. if (isset($data['status'])) {
  1030. $where[] = ['website_template.status', '=', $data['status']];
  1031. }
  1032. $result = WebsiteTemplate::where([])
  1033. ->where($where)
  1034. ->leftJoin('website', 'website.id', '=', 'website_template.website_id')
  1035. ->select('website_template.*', 'website.website_name as website_name', 'website.website_url as website_url')
  1036. ->paginate($data['pageSize'], ['*'], 'page', $data['page']);
  1037. if ($result) {
  1038. return Result::success($result);
  1039. } else {
  1040. return Result::error('获取列表失败');
  1041. }
  1042. }
  1043. public function updateWebConfig($data)
  1044. {
  1045. var_dump($data, 'data1');
  1046. // return Result::success($data);
  1047. $where = [];
  1048. if (isset($data['website_id'])) {
  1049. $where['website_template.website_id'] = $data['website_id'];
  1050. }
  1051. $result = WebsiteTemplate::where([])
  1052. ->where($where)
  1053. ->leftJoin('website', 'website.id', '=', 'website_template.website_id')
  1054. ->select('website_template.id', 'website_template.port', 'website.website_name as website_name', 'website.website_url as website_url', 'website_template.siteId')
  1055. ->first()->toArray();
  1056. var_dump($result, 'result');
  1057. $domain = json_decode($result['website_url'], true);
  1058. $domainlist = $domain;
  1059. var_dump(empty($result['port']), 'port');
  1060. $domain = $domain[3];
  1061. if ($domain == null || empty($domain) || !isset($domain)) {
  1062. return Result::error('域名不存在');
  1063. }
  1064. $port = $result['port'];
  1065. if ($port == null || empty($port) || !isset($port)) {
  1066. var_dump($port, 'youp0------port');
  1067. return Result::error('端口不存在');
  1068. }
  1069. $postData = [
  1070. 'domain' => $domain,
  1071. 'port' => $port
  1072. ];
  1073. var_dump(empty($result['siteId']), 'siteId---');
  1074. if (empty($result['siteId'])) {
  1075. //实例化对象
  1076. $api = new bt_api();
  1077. //获取面板日志
  1078. $r_data = $api->GetLogs();
  1079. //输出JSON数据到浏览器
  1080. echo json_encode($r_data);
  1081. $webname = [
  1082. 'domain' => $domain,
  1083. 'domainlist' => $domainlist,
  1084. 'count' => count($domainlist) - 1
  1085. ];
  1086. $siteData = [
  1087. 'path' => '/www2/www/wwwroot/' . $domain,
  1088. 'ftp' => 'false',
  1089. 'type' => 'PHP',
  1090. 'type_id' => 0,
  1091. 'ps' => $domain,
  1092. 'port' => 80,
  1093. 'version' => '00',
  1094. 'need_index' => 0,
  1095. 'need_404' => 0,
  1096. 'sql' => 'false',
  1097. 'codeing' => 'utf8mb4',
  1098. 'webname' => json_encode($webname),
  1099. 'add_dns_record' => 'false'
  1100. ];
  1101. $r_data = $api->addSite($siteData);
  1102. /*
  1103. {
  1104. "siteStatus": true,
  1105. "siteId": 20,
  1106. "ftpStatus": false,
  1107. "databaseStatus": false,
  1108. "gitStatus": false
  1109. }
  1110. */
  1111. var_dump($r_data, '----------1--r_data--------');
  1112. if (isset($r_data['status']) && $r_data['status'] == false) {
  1113. return Result::error('宝塔添加失败' . $r_data['msg']);
  1114. }
  1115. if (isset($r_data['siteStatus']) && $r_data['siteStatus'] == true) {
  1116. //记录siteId
  1117. $siteId = $r_data['siteId'];
  1118. //更新网站模板的siteId WebsiteTemplate
  1119. WebsiteTemplate::where('website_id', $data['website_id'])
  1120. ->update([
  1121. 'siteId' => $siteId,
  1122. 'dir' => '/www2/www/wwwroot/' . $domain,
  1123. 'status' => 2
  1124. ]);
  1125. }
  1126. // return Result::success($r_data);
  1127. }
  1128. // 使用Guzzle HTTP客户端发送POST请求
  1129. try {
  1130. $client = new \GuzzleHttp\Client();
  1131. $response = $client->post('http://adminpre.bjzxtw.org.cn/create.php', [
  1132. 'json' => $postData,
  1133. 'timeout' => 30,
  1134. 'headers' => [
  1135. 'Content-Type' => 'application/json',
  1136. ]
  1137. ]);
  1138. // 获取响应内容
  1139. $responseBody = $response->getBody()->getContents();
  1140. $responseData = json_decode($responseBody, true);
  1141. var_dump($responseData, '脚本返回数据------------------------');
  1142. // 检查响应是否为有效的JSON
  1143. if (json_last_error() === JSON_ERROR_NONE) {
  1144. // 如果远程服务器返回成功结果
  1145. if (isset($responseData['code']) && $responseData['code'] === 1) {
  1146. return Result::success([
  1147. 'message' => '远程服务器处理成功' . $responseData['message'],
  1148. 'output' => $responseData['output'],
  1149. 'file' => $responseData['file'],
  1150. 'code' => $responseData['code'],
  1151. 'exec' => $responseData['exec'],
  1152. 'domain' => $domain,
  1153. 'port' => $port,
  1154. 'remote_response' => $responseData
  1155. ]);
  1156. } else {
  1157. // 远程服务器返回错误
  1158. return Result::error(500, '远程服务器处理失败', [
  1159. 'remote_response' => $responseData
  1160. ]);
  1161. }
  1162. } else {
  1163. // 响应不是有效的JSON
  1164. return Result::success([
  1165. 'message' => '收到远程服务器响应(非JSON格式)',
  1166. 'domain' => $domain,
  1167. 'port' => $port,
  1168. 'raw_response' => $responseBody
  1169. ]);
  1170. }
  1171. } catch (\GuzzleHttp\Exception\RequestException $e) {
  1172. // HTTP请求异常
  1173. $errorMessage = 'HTTP请求失败';
  1174. if ($e->hasResponse()) {
  1175. $response = $e->getResponse();
  1176. $errorMessage .= ': ' . $response->getStatusCode() . ' ' . $response->getReasonPhrase();
  1177. } else {
  1178. $errorMessage .= ': ' . $e->getMessage();
  1179. }
  1180. return Result::error(500, $errorMessage, [
  1181. 'exception' => $e->getMessage()
  1182. ]);
  1183. } catch (\Exception $e) {
  1184. // 其他异常
  1185. return Result::error(500, '请求处理异常: ' . $e->getMessage(), [
  1186. 'exception' => $e->getMessage()
  1187. ]);
  1188. }
  1189. if ($result) {
  1190. return Result::success($result);
  1191. } else {
  1192. return Result::error('获取列表失败');
  1193. }
  1194. }
  1195. private function bt($data) {}
  1196. public function updateWebConfig1($data)
  1197. {
  1198. // 先根据
  1199. // 根据是否测试环境配置不同 的vue项目的 配置文件
  1200. //获取根目录下 yuan 源代码,开始处理
  1201. //获取目前目录,获取根目录
  1202. $root = dirname(dirname(__DIR__));
  1203. $yuan = $root . '/yuan';
  1204. //取出envAPP_ENV=dev
  1205. $APP_ENV = env('APP_ENV');
  1206. switch ($APP_ENV) {
  1207. case 'dev':
  1208. $file = $yuan . '/plugins/category.ts';
  1209. break;
  1210. case 'prod':
  1211. $file = $yuan . '/plugins/category.ts';
  1212. break;
  1213. default:
  1214. $file = $yuan . '/plugins/category.ts';
  1215. }
  1216. //处理文件plugins\category.ts
  1217. //找到 export default defineNuxtPlugin((nuxtApp) => {,处理到文件结尾
  1218. /*
  1219. export default defineNuxtPlugin((nuxtApp) => {
  1220. //master 环境
  1221. nuxtApp.provide('pageNav', navMaster)
  1222. //pre 环境
  1223. // nuxtApp.provide('pageNav', navPre)
  1224. })
  1225. */
  1226. if (0 == 1) {
  1227. $content = file_get_contents($file);
  1228. $content = str_replace("nuxtApp.provide('pageNav', navPre)", "//nuxtApp.provide('pageNav', navPre)", $content);
  1229. $content = str_replace("//nuxtApp.provide('pageNav', navMaster)", "nuxtApp.provide('pageNav', navMaster)", $content);
  1230. //写入文件
  1231. $res = file_put_contents($file, $content);
  1232. var_dump('已经处理/plugins/category.ts');
  1233. //处理文件\plugins\globals.js
  1234. $file = $yuan . '/plugins/globals.js';
  1235. $content = file_get_contents($file);
  1236. // 1️⃣ 将没有注释的 nuxtApp.provide(...) 行加上注释
  1237. $content = preg_replace('/^(?!\s*\/\/)(?=.*nuxtApp\.provide)/m', '// ', $content);
  1238. // 2️⃣ 找到 "//乡村网正式环境" 下面的注释行并取消注释
  1239. $lines = explode("\n", $content);
  1240. $output = [];
  1241. $foundSection = false;
  1242. foreach ($lines as $line) {
  1243. // 判断是否进入目标区域
  1244. if (preg_match('/\/\/乡村网正式环境/', $line)) {
  1245. $foundSection = true;
  1246. $output[] = $line;
  1247. continue;
  1248. }
  1249. // 在目标区域内,去掉注释
  1250. if ($foundSection && preg_match('/^\s*\/\/\s*(nuxtApp\.provide)/', $line, $matches)) {
  1251. $output[] = preg_replace('/^\s*\/\/\s*/', '', $line, 1);
  1252. } else {
  1253. // 如果遇到下一个注释块或空白行,结束当前处理
  1254. if ($foundSection && preg_match('/^\s*$/', $line)) {
  1255. $foundSection = false;
  1256. }
  1257. $output[] = $line;
  1258. }
  1259. }
  1260. // 合并回字符串
  1261. $newContent = implode("\n", $output);
  1262. // 写入文件
  1263. $res1 = file_put_contents($file, $newContent);
  1264. var_dump($res1, '已经处理/plugins/globals.js');
  1265. //处理文件\plugins\request.js
  1266. $file = $yuan . '/plugins/request.js';
  1267. $content = file_get_contents($file);
  1268. $lines = explode("\n", $content);
  1269. $in_prod = false;
  1270. $in_pre = false;
  1271. for ($i = 0; $i < count($lines); $i++) {
  1272. $line = $lines[$i];
  1273. // 匹配 "//正式环境"
  1274. if (preg_match('/\/\/正式环境/', $line)) {
  1275. $in_prod = true;
  1276. $in_pre = false;
  1277. continue;
  1278. }
  1279. // 匹配 "//pre环境"
  1280. if (preg_match('/\/\/pre环境/', $line)) {
  1281. $in_pre = true;
  1282. $in_prod = false;
  1283. continue;
  1284. }
  1285. // 处理正式环境下的两行:去注释
  1286. if ($in_prod && $i + 0 <= count($lines) && $i + 1 <= count($lines)) {
  1287. for ($j = $i + 0; $j <= $i + 1; $j++) {
  1288. if (isset($lines[$j])) {
  1289. $lines[$j] = preg_replace('/^\s*\/\/\s*/', '', $lines[$j]);
  1290. }
  1291. }
  1292. $in_prod = false; // 只处理一次
  1293. }
  1294. // 处理 pre 环境下的两行:添加注释
  1295. if ($in_pre && $i + 0 <= count($lines) && $i + 1 <= count($lines)) {
  1296. for ($j = $i + 1; $j <= $i + 2; $j++) {
  1297. if (isset($lines[$j]) && !preg_match('/^\s*\/\//', $lines[$j])) {
  1298. // $lines[$j] = '// ' . $lines[$j];
  1299. }
  1300. }
  1301. $in_pre = false; // 只处理一次
  1302. }
  1303. }
  1304. // 写回文件
  1305. $res2 = file_put_contents($file, implode("\n", $lines));
  1306. var_dump($res2, '已经处理/plugins/request.js');
  1307. echo "✅ 环境配置切换完成!";
  1308. //第三步 定位到yuan目录,执行 npm run build 生成output目录
  1309. chdir($yuan);
  1310. $APP_ENV = env('APP_ENV');
  1311. $res3 = exec("npm run build");
  1312. var_dump($res, '执行 npm run build');
  1313. echo "✅ 构建完成!";
  1314. //第四步
  1315. //修改 .output\server\chunks\routes\index.mjs ,
  1316. // 根据网站的id的port更改端口
  1317. //第五步
  1318. //x
  1319. $file4 = $yuan . '/.output/server/chunks/routes/index.mjs';
  1320. $content = file_get_contents($file4);
  1321. // 使用正则将 "destr(...) || 3e3" 替换为 "destr(...) || 9000"
  1322. $content = preg_replace(
  1323. '/(destr$[^)]+$$\s*\|\|\s*)3e3/i',
  1324. '$19000',
  1325. $content
  1326. );
  1327. // 写回文件
  1328. file_put_contents($file, $content);
  1329. }
  1330. //第六步 yuan压缩成压缩包,复制到/hyperf-skeleton/web1/,解压
  1331. $dir_yuan = $yuan . '/.output/';
  1332. // 移动到web 没有就创建
  1333. $dir_web = '/hyperf-skeleton/' . '/web1/';
  1334. if (!is_dir($dir_web)) {
  1335. mkdir($dir_web, 0755, true);
  1336. }
  1337. $fs = new Filesystem();
  1338. $fs->rename($dir_yuan, $dir_web . 'output', true);
  1339. var_dump('已经移动到/hyperf-skeleton/web1/output');
  1340. $result = [
  1341. 'res' => $res,
  1342. 'res1' => $res1,
  1343. 'res2' => $res2,
  1344. 'res3' => $res3,
  1345. 'root' => $root,
  1346. 'yuan' => $yuan,
  1347. 'APP_ENV' => $APP_ENV,
  1348. 'website_id' => $data['website_id'],
  1349. ];
  1350. if ($result) {
  1351. return Result::success($result);
  1352. } else {
  1353. return Result::error('更新配置失败');
  1354. }
  1355. }
  1356. public function updateWebOutput($data)
  1357. {
  1358. // return Result::error('更新输出失败');
  1359. //根据源程序, 复制出来,更改配置文件,打包,
  1360. // $result = 1;
  1361. $projectRoot = dirname(dirname(__DIR__)); // 从当前文件向上两级目录
  1362. $projectDir = $projectRoot . '/../zizhujianzhan_web';
  1363. $command = "cd " . escapeshellarg($projectDir) . " && npm run build";
  1364. // 执行命令并获取输出
  1365. exec($command, $output, $returnCode);
  1366. // 输出结果
  1367. echo "Return Code: " . $returnCode . "\n";
  1368. echo "Output:\n";
  1369. foreach ($output as $line) {
  1370. echo $line . "\n";
  1371. }
  1372. $result = $returnCode;
  1373. if ($result) {
  1374. return Result::success($result);
  1375. } else {
  1376. return Result::error('更新输出失败');
  1377. }
  1378. }
  1379. public function updateWebMove($data)
  1380. {
  1381. //更改到dir目录,
  1382. $result = WebsiteTemplate::where('id', $data['id'])->update($data);
  1383. if ($result) {
  1384. return Result::success($result);
  1385. } else {
  1386. return Result::error('更新移动失败');
  1387. }
  1388. }
  1389. public function runWeb($data)
  1390. {
  1391. //运行网站, 先检查是否有配置文件, pm2启动
  1392. $result = WebsiteTemplate::where('id', $data['id'])->update($data);
  1393. if ($result) {
  1394. return Result::success($result);
  1395. } else {
  1396. return Result::error('运行失败');
  1397. }
  1398. }
  1399. public function stopWeb($data)
  1400. {
  1401. $result = WebsiteTemplate::where('id', $data['id'])->update($data);
  1402. if ($result) {
  1403. return Result::success($result);
  1404. } else {
  1405. return Result::error('停止失败');
  1406. }
  1407. }
  1408. }
  1409. class bt_api
  1410. {
  1411. private $BT_KEY = "RN0wTXLHpKKBXXxyDJoovHMpBhyLVFWZ"; //接口密钥
  1412. private $BT_PANEL = "http://192.168.1.234:18888"; //面板地址
  1413. //如果希望多台面板,可以在实例化对象时,将面板地址与密钥传入
  1414. public function __construct($bt_panel = null, $bt_key = null)
  1415. {
  1416. if ($bt_panel) $this->BT_PANEL = $bt_panel;
  1417. if ($bt_key) $this->BT_KEY = $bt_key;
  1418. }
  1419. //示例取面板日志
  1420. public function GetLogs()
  1421. {
  1422. //拼接URL地址
  1423. $url = $this->BT_PANEL . '/data?action=getData';
  1424. //准备POST数据
  1425. $p_data = $this->GetKeyData(); //取签名
  1426. $p_data['table'] = 'logs';
  1427. $p_data['limit'] = 10;
  1428. $p_data['tojs'] = 'test';
  1429. //请求面板接口
  1430. $result = $this->HttpPostCookie($url, $p_data);
  1431. //解析JSON数据
  1432. $data = json_decode($result, true);
  1433. return $data;
  1434. }
  1435. public function addSite(array $siteData): array
  1436. {
  1437. //拼接URL地址
  1438. $url = $this->BT_PANEL . '/site?action=AddSite';
  1439. //准备POST数据
  1440. $p_data = $this->GetKeyData(); //取签名
  1441. // Merge site data with key data
  1442. $p_data = array_merge($p_data, $siteData);
  1443. //请求面板接口
  1444. $result = $this->HttpPostCookie($url, $p_data);
  1445. //解析JSON数据
  1446. $data = json_decode($result, true);
  1447. return $data;
  1448. }
  1449. /**
  1450. * 检查删除数据
  1451. * ids [21]
  1452. */
  1453. public function checkDelSite(array $siteData): array
  1454. {
  1455. //拼接URL地址
  1456. $url = $this->BT_PANEL . '/site?action=check_del_data';
  1457. //准备POST数据
  1458. $p_data = $this->GetKeyData(); //取签名
  1459. // Merge site data with key data
  1460. $p_data = array_merge($p_data, $siteData);
  1461. //请求面板接口
  1462. $result = $this->HttpPostCookie($url, $p_data);
  1463. //解析JSON数据
  1464. $data = json_decode($result, true);
  1465. return $data;
  1466. }
  1467. /**
  1468. * 删除网站
  1469. * id 21
  1470. * webname t2.lj
  1471. * path 1
  1472. */
  1473. public function delSite(array $siteData): array
  1474. {
  1475. //拼接URL地址
  1476. $url = $this->BT_PANEL . '/site?action=DeleteSite';
  1477. //准备POST数据
  1478. $p_data = $this->GetKeyData(); //取签名
  1479. // Merge site data with key data
  1480. $p_data = array_merge($p_data, $siteData);
  1481. //请求面板接口
  1482. $result = $this->HttpPostCookie($url, $p_data);
  1483. //解析JSON数据
  1484. $data = json_decode($result, true);
  1485. return $data;
  1486. }
  1487. /**
  1488. * 构造带有签名的关联数组
  1489. */
  1490. private function GetKeyData()
  1491. {
  1492. $now_time = time();
  1493. $p_data = array(
  1494. 'request_token' => md5($now_time . '' . md5($this->BT_KEY)),
  1495. 'request_time' => $now_time
  1496. );
  1497. return $p_data;
  1498. }
  1499. /**
  1500. * 发起POST请求
  1501. * @param String $url 目标网填,带http://
  1502. * @param Array|String $data 欲提交的数据
  1503. * @return string
  1504. */
  1505. private function HttpPostCookie($url, $data, $timeout = 60)
  1506. {
  1507. //定义cookie保存位置
  1508. $cookie_file = './' . md5($this->BT_PANEL) . '.cookie';
  1509. if (!file_exists($cookie_file)) {
  1510. $fp = fopen($cookie_file, 'w+');
  1511. fclose($fp);
  1512. }
  1513. $ch = curl_init();
  1514. curl_setopt($ch, CURLOPT_URL, $url);
  1515. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  1516. curl_setopt($ch, CURLOPT_POST, 1);
  1517. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  1518. curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
  1519. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
  1520. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  1521. curl_setopt($ch, CURLOPT_HEADER, 0);
  1522. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  1523. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  1524. $output = curl_exec($ch);
  1525. curl_close($ch);
  1526. return $output;
  1527. }
  1528. }