ClientService.php 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670
  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. $componetList[$key]['componentData']['data']['alias'] = $websiteInfoIndexed[$category_id]->alias ?? 'aliasnull' . $category_id;
  347. $componetList[$key]['componentData']['data']['pinyin'] = $websiteInfoIndexed[$category_id]->pinyin ?? 'aliasnull' . $category_id;
  348. $componetList[$key]['componentData']['data']['category_id'] = $category_id;
  349. $componetList[$key]['componentData']['data']['textnum'] = $textArticles ?? [];
  350. $componetList[$key]['componentData']['data']['imgnum'] = $imgArticles ?? [];
  351. }
  352. // var_dump($level, 'level');
  353. if ($level > 0) {
  354. // 取出一级的所有子级
  355. //取出所有的子级的imgurl 不为空的数据
  356. if ($imgSize > 0) {
  357. $imgArticles = Db::table('article')
  358. ->leftJoin('article_ignore', function ($join) use ($website_id) {
  359. $join->on('article.id', '=', 'article_ignore.article_id')->where('article_ignore.website_id', '=', $website_id);
  360. })
  361. // ->whereNull('article_ignore.article_id')
  362. ->where(function ($query) {
  363. $query->whereNull('article_ignore.article_id')
  364. ->orWhere(function ($subQuery) {
  365. $subQuery->whereNotNull('article_ignore.article_id')
  366. ->where('article_ignore.is_ignore', 0)
  367. ->where(function ($subSubQuery) {
  368. $subSubQuery->where('article_ignore.c_show_time', '<=', date('Y-m-d H:i:s'))
  369. ->orWhereNull('article_ignore.c_show_time');
  370. });
  371. });
  372. })
  373. ->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();
  374. //拼接上拼音
  375. foreach ($imgArticles as $k => $v) {
  376. $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  377. }
  378. }
  379. //取出所有的子级的imgurl 为空的数据 ->whereNull('imgurl')
  380. if ($textSize > 0) {
  381. $textArticles = Db::table('article')->where('status', 1)
  382. ->leftJoin('article_ignore', function ($join) use ($website_id) {
  383. $join->on('article.id', '=', 'article_ignore.article_id')->where('article_ignore.website_id', '=', $website_id);
  384. })
  385. // ->whereNull('article_ignore.article_id')
  386. ->where(function ($query) {
  387. $query->whereNull('article_ignore.article_id')
  388. ->orWhere(function ($subQuery) {
  389. $subQuery->whereNotNull('article_ignore.article_id')
  390. ->where('article_ignore.is_ignore', 0)
  391. ->where(function ($subSubQuery) {
  392. $subSubQuery->where('article_ignore.c_show_time', '<=', date('Y-m-d H:i:s'))
  393. ->orWhereNull('article_ignore.c_show_time');
  394. });
  395. });
  396. })
  397. ->whereRaw('MATCH(level_text) AGAINST(? IN NATURAL LANGUAGE MODE)', [$level])->whereIn('catid', $categoryIds)->limit($textSize)->orderBy('updated_at', 'desc')->get()->toArray();
  398. //拼接上拼音
  399. foreach ($textArticles as $k => $v) {
  400. $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '=====';
  401. }
  402. }
  403. }
  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. }