PublicRpcService.php 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219
  1. <?php
  2. namespace App\JsonRpc;
  3. use _PHPStan_62c6a0a8b\OndraM\CiDetector\Env;
  4. use App\Model\BlackWord;
  5. use App\Model\Department;
  6. use App\Model\District;
  7. use App\Model\LetterOfComplaint;
  8. use App\Model\LetterType;
  9. use App\Model\LevelUser;
  10. use App\Model\UserLevel;
  11. use App\Tools\Result;
  12. use Hyperf\DbConnection\Db;
  13. use Hyperf\Di\Annotation\Inject;
  14. use Hyperf\RpcServer\Annotation\RpcService;
  15. use App\Service\MinioService;
  16. use Hyperf\Redis\Redis;
  17. use Overtrue\ChineseCalendar\Calendar;
  18. use App\Model\TemplateClass;
  19. use App\Model\Template;
  20. use App\Model\WebsiteTemplate;
  21. use App\Model\WebsiteTemplateInfo;
  22. use App\Model\Sector;
  23. use App\Model\Component;
  24. use App\Model\Link;
  25. use App\Model\FooterCategory;
  26. use App\Model\Size;
  27. use function Hyperf\Support\retry;
  28. use Hyperf\Paginator\Paginator;
  29. use App\Model\User;
  30. use App\Model\Website;
  31. use App\Constants\ErrorCode;
  32. use GuzzleHttp\Client;
  33. use GuzzleHttp\Exception\GuzzleException;
  34. use App\Model\SectorPlace;
  35. use App\Model\ComponentType;
  36. use App\Model\Article;
  37. use App\Model\WebsiteCategory;
  38. #[RpcService(name: "PublicRpcService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  39. class PublicRpcService implements PublicRpcServiceInterface
  40. {
  41. #[Inject]
  42. protected Redis $redis;
  43. /**
  44. * @param array $data
  45. * @return array
  46. */
  47. public function getDistrictList(array $data): array
  48. {
  49. $where = [];
  50. if (isset($data['keyWord'])) {
  51. $where = [
  52. ['name', 'like', '%' . $data['keyWord'] . '%']
  53. ];
  54. }
  55. $result = [];
  56. if (isset($data['pageSize'])) {
  57. $rep = District::where($where)->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->orderBy("code", "asc")->get();
  58. $count = District::where($where)->count();
  59. $result = [
  60. 'rows' => $rep,
  61. 'count' => $count
  62. ];
  63. } else {
  64. $result = District::where($data)->orderBy("code", "asc")->get();
  65. }
  66. return $result ? Result::success($result) : Result::error("没有查到数据");
  67. }
  68. /**
  69. * @param array $data
  70. * @return array
  71. */
  72. public function getUserLevelList(array $data): array
  73. {
  74. $where = [];
  75. if (isset($data['keyWord'])) {
  76. $where = [
  77. ['name', 'like', '%' . $data['keyWord'] . '%'],
  78. ];
  79. }
  80. $result = [];
  81. if (isset($data['pageSize'])) {
  82. $rep = UserLevel::where($where)->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->orderBy("id", "asc")->get();
  83. $count = UserLevel::where($where)->count();
  84. $result = [
  85. 'rows' => $rep,
  86. 'count' => $count,
  87. ];
  88. } else {
  89. $result = UserLevel::orderBy("id", "asc")->get();
  90. }
  91. return $result ? Result::success($result) : Result::error("没有查到数据");
  92. }
  93. /**
  94. * 添加用户等级
  95. * @param array $data
  96. * @return array
  97. */
  98. public function addUserLevel(array $data): array
  99. {
  100. LevelUser::insertGetId($data);
  101. return Result::success([]);
  102. }
  103. /**
  104. * 更新等级
  105. * @param array $data
  106. * @return array
  107. */
  108. public function updateUserLevel(array $data): array
  109. {
  110. $result = LevelUser::where(['id' => $data['id']])->update($data);
  111. if ($result) {
  112. return Result::success($result);
  113. }
  114. return Result::error("更新失败");
  115. }
  116. /**
  117. * 删除等级
  118. * @param array $data
  119. * @return array
  120. */
  121. public function delUserLevel(array $data): array
  122. {
  123. $result = LevelUser::where(['id' => $data['id']])->delete();
  124. if ($result) {
  125. return Result::success($result);
  126. }
  127. return Result::error("删除失败");
  128. }
  129. /**
  130. * 查询投诉举报信息
  131. * @param array $data
  132. * @return array
  133. */
  134. public function getLetterOfComplaint(array $data = []): array
  135. {
  136. var_dump("====");
  137. $where = [];
  138. if (isset($data['user_id']) && !empty($data['user_id'])) {
  139. array_push($where, ['letter_of_complaint.user_id', '=', $data['user_id']]);
  140. }
  141. if (isset($data['nature']) && !empty($data['nature'])) {
  142. array_push($where, ['letter_of_complaint.nature', '=', $data['nature']]);
  143. }
  144. if (isset($data['nature_level0']) && !empty($data['nature_level0'])) {
  145. array_push($where, ['letter_of_complaint.nature_level0', '=', $data['nature_level0']]);
  146. }
  147. if (isset($data['status']) && !empty($data['status'])) {
  148. array_push($where, ['letter_of_complaint.status', '=', $data['status']]);
  149. }
  150. $result = [];
  151. if (isset($data['pageSize'])) {
  152. $rep = LetterOfComplaint::where($where)
  153. ->leftJoin("letter_type as type_a", "letter_of_complaint.nature", "type_a.id")
  154. ->leftJoin("letter_type as type_c", "letter_of_complaint.nature_level0", "type_c.id")
  155. ->leftJoin("letter_type as type_b", "letter_of_complaint.nature_level1", "type_b.id")
  156. ->leftJoin("letter_type as type_e", "letter_of_complaint.nature_level3", "type_e.id")
  157. ->leftJoin("letter_type as type_d", "letter_of_complaint.status", "type_d.id")
  158. ->select(
  159. "letter_of_complaint.*",
  160. "type_a.type_name as nature_name",
  161. "type_b.type_name as nature_name1",
  162. "type_c.type_name as nature_name0",
  163. "type_d.type_name as status_name",
  164. "type_e.type_name as nature_name3"
  165. )
  166. ->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->orderBy("letter_of_complaint.id", "desc")->get();
  167. $count = LetterOfComplaint::where($where)->count();
  168. if ($rep) {
  169. foreach ($rep as $val) {
  170. if ($val['judgment']) {
  171. $val['judgment'] = json_decode($val['judgment']);
  172. }
  173. if ($val['audio_and_video']) {
  174. $val['audio_and_video'] = json_decode($val['audio_and_video']);
  175. }
  176. if ($val['contract']) {
  177. $val['contract'] = json_decode($val['contract']);
  178. }
  179. if ($val['qualifications']) {
  180. $val['qualifications'] = json_decode($val['qualifications']);
  181. }
  182. }
  183. }
  184. $result = [
  185. 'rows' => $rep,
  186. 'count' => $count,
  187. ];
  188. } else {
  189. $result = LetterOfComplaint::where($where)
  190. ->leftJoin("letter_type as type_a", "letter_of_complaint.nature", "type_a.id")
  191. ->leftJoin("letter_type as type_c", "letter_of_complaint.nature_level0", "type_c.id")
  192. ->leftJoin("letter_type as type_b", "letter_of_complaint.nature_level1", "type_b.id")
  193. ->leftJoin("letter_type as type_e", "letter_of_complaint.nature_level3", "type_e.id")
  194. ->leftJoin("letter_type as type_d", "letter_of_complaint.status", "type_d.id")
  195. ->select(
  196. "letter_of_complaint.*",
  197. "type_a.type_name as nature_name",
  198. "type_b.type_name as nature_name1",
  199. "type_c.type_name as nature_name0",
  200. "type_d.type_name as status_name",
  201. "type_e.type_name as nature_name3"
  202. )
  203. ->orderBy("letter_of_complaint.id", "desc")->get();
  204. }
  205. return $result ? Result::success($result) : Result::error("没有查到数据");
  206. }
  207. /**
  208. * 添加投诉举报信息
  209. * @param array $data
  210. * @return array
  211. */
  212. public function addLetterOfComplaint(array $data): array
  213. {
  214. $data['judgment'] = $data['judgment'] ? json_encode($data['judgment']) : '';
  215. $data['audio_and_video'] = $data['audio_and_video'] ? json_encode($data['audio_and_video']) : '';
  216. $data['contract'] = $data['contract'] ? json_encode($data['contract']) : '';
  217. $data['qualifications'] = $data['qualifications'] ? json_encode($data['qualifications']) : '';
  218. unset($data['id']);
  219. $result = LetterOfComplaint::insertGetId($data);
  220. if (empty($result)) {
  221. return Result::error("创建失败", 0);
  222. } else {
  223. return Result::success(["id" => $result]);
  224. }
  225. }
  226. /**
  227. * 用户端更新投诉举报
  228. * @param array $data
  229. * @return array
  230. */
  231. public function userUpLetterOfComplaint(array $data): array
  232. {
  233. $data['judgment'] = $data['judgment'] ? json_encode($data['judgment']) : '';
  234. $data['audio_and_video'] = $data['audio_and_video'] ? json_encode($data['audio_and_video']) : '';
  235. $data['contract'] = $data['contract'] ? json_encode($data['contract']) : '';
  236. $data['qualifications'] = $data['qualifications'] ? json_encode($data['qualifications']) : '';
  237. $result = LetterOfComplaint::where(['id' => $data['id']])->update($data);
  238. if (empty($result)) {
  239. return Result::error("创建失败", 0);
  240. } else {
  241. return Result::success(["id" => $result]);
  242. }
  243. }
  244. /**
  245. * 管理后台更新投诉举报信息
  246. * @param array $data
  247. * @return array
  248. */
  249. public function upLetterOfComplaint(array $data): array
  250. {
  251. var_dump("admin:", $data);
  252. $where = [
  253. 'id' => $data['id'],
  254. ];
  255. $filtered_array = array_filter($data, function ($value) {
  256. return $value !== "" && $value !== null && $value !== false && !is_array($value) || !empty($value);
  257. });
  258. $filtered_array['judgment'] = isset($filtered_array['judgment']) ? json_encode($filtered_array['judgment']) : '';
  259. $filtered_array['audio_and_video'] = isset($filtered_array['audio_and_video']) ? json_encode($filtered_array['audio_and_video']) : '';
  260. $filtered_array['contract'] = isset($filtered_array['contract']) ? json_encode($filtered_array['contract']) : '';
  261. $filtered_array['qualifications'] = isset($filtered_array['qualifications']) ? json_encode($filtered_array['qualifications']) : '';
  262. unset($filtered_array['nature_name']);
  263. unset($filtered_array['type_name']);
  264. unset($filtered_array['nature_level_name']);
  265. unset($filtered_array['status_name']);
  266. unset($filtered_array['is_admin']);
  267. unset($filtered_array['type_level_name']);
  268. $result = LetterOfComplaint::where($where)->update($filtered_array);
  269. if ($result) {
  270. return Result::success($result);
  271. }
  272. return Result::error("更新失败", 0);
  273. }
  274. /**
  275. * 查询投诉举报记录
  276. * @param array $data
  277. * @return array
  278. */
  279. public function getLetterOfComplaintInfo(array $data): array
  280. {
  281. $where = [
  282. 'letter_of_complaint.id' => $data['id'],
  283. ];
  284. if (isset($data['user_id']) && !empty($data['user_id'])) {
  285. array_push($where, ['letter_of_complaint.user_id', '=', $data['user_id']]);
  286. }
  287. $result = LetterOfComplaint::where($where)
  288. ->leftJoin("letter_type as type_a", "letter_of_complaint.nature", "type_a.id")
  289. ->leftJoin("letter_type as type_c", "letter_of_complaint.nature_level0", "type_c.id")
  290. ->leftJoin("letter_type as type_b", "letter_of_complaint.nature_level1", "type_b.id")
  291. ->leftJoin("letter_type as type_e", "letter_of_complaint.nature_level3", "type_e.id")
  292. ->leftJoin("letter_type as type_d", "letter_of_complaint.status", "type_d.id")
  293. ->select(
  294. "letter_of_complaint.*",
  295. "type_a.type_name as nature_name",
  296. "type_b.type_name as nature_name1",
  297. "type_c.type_name as nature_name0",
  298. "type_d.type_name as status_name",
  299. "type_e.type_name as nature_name3"
  300. )
  301. ->first();
  302. return Result::success($result);
  303. }
  304. /**
  305. * 删除投诉举报信息
  306. * @param array $data
  307. * @return array
  308. */
  309. public function delLetterOfComplaint(array $data): array
  310. {
  311. $result = LetterOfComplaint::when($data, function ($query) use ($data) {
  312. if (isset($data['id']) && !empty($data['id'])) {
  313. $query->where(['id' => $data['id']]);
  314. }
  315. if (isset($data['user_id']) && !empty($data['user_id'])) {
  316. $query->where(['user_id' => $data['user_id']]);
  317. }
  318. })->delete();
  319. if (empty($result)) {
  320. return Result::error("删除失败", 0);
  321. } else {
  322. return Result::success();
  323. }
  324. }
  325. /**
  326. * 获取举报信息类型
  327. * @param array $data
  328. * @return array
  329. */
  330. public function getLetterType(array $data): array
  331. {
  332. $where = [];
  333. if (isset($data['type'])) {
  334. array_push($where, ['type', '=', $data['type']]);
  335. }
  336. if (isset($data['pid']) && $data['pid'] > 0) {
  337. array_push($where, ['pid', '=', $data['pid']]);
  338. }
  339. $result = LetterType::where($where)->orderBy('sort', 'asc')->get();
  340. return $result ? Result::success($result) : Result::error("没有查到数据");
  341. }
  342. /**
  343. * 更新举报类型
  344. * @param array $data
  345. * @return array
  346. */
  347. public function upLetterType(array $data): array
  348. {
  349. return [];
  350. }
  351. /**
  352. * 添加举报类型
  353. * @param array $data
  354. * @return array
  355. */
  356. public function addLetterType(array $data): array
  357. {
  358. $result = LetterType::insertGetId($data);
  359. if (empty($result)) {
  360. return Result::error("创建失败", 0);
  361. } else {
  362. return Result::success(["id" => $result]);
  363. }
  364. }
  365. /**
  366. * 删除举报类型
  367. * @param array $data
  368. * @return array
  369. */
  370. public function delLetterType(array $data): array
  371. {
  372. $result = LetterType::where('id', $data['id'])->delete();
  373. if (empty($result)) {
  374. return Result::error("删除失败", 0);
  375. } else {
  376. return Result::success();
  377. }
  378. }
  379. /**
  380. * 检测是否已经被接案
  381. * @param array $data
  382. * @return array
  383. */
  384. public function checkMeasure(array $data): array
  385. {
  386. $where = [
  387. 'id' => $data['id'],
  388. ];
  389. $letterOfComplaintInfo = LetterOfComplaint::where($where)->first();
  390. var_dump("查询数据:", $letterOfComplaintInfo['admin_id'], $data['user_id']);
  391. //操作人和当前登陆用户id 相等说明是当前人接收的案件
  392. if (($letterOfComplaintInfo['admin_id'] == $data['user_id']) || empty($letterOfComplaintInfo['admin_id'])) {
  393. return Result::success();
  394. } else {
  395. return Result::error("您不能处理其他人已经接过的案件", 0);
  396. }
  397. }
  398. /**
  399. * 后台获取职能部门
  400. * @param array $data
  401. * @return array
  402. */
  403. public function getZhinengbumenList(array $data): array
  404. {
  405. // 获取分页参数,默认每页 10 条记录
  406. $page = isset($data['page']) ? (int) $data['page'] : 1;
  407. $perPage = isset($data['pagesize']) ? (int) $data['pagesize'] : 10;
  408. // 查询数据并分页
  409. $query = Department::query();
  410. // 可以在这里添加更多的查询条件
  411. if (isset($data['search'])) {
  412. $query->where('name', 'like', '%' . $data['search'] . '%');
  413. }
  414. // 执行分页查询
  415. $result = $query->paginate($perPage, ['*'], 'page', $page);
  416. // 返回分页结果
  417. return Result::success([
  418. 'count' => $result->total(),
  419. 'current_page' => $result->currentPage(),
  420. 'last_page' => $result->lastPage(),
  421. 'pagesize' => $result->perPage(),
  422. 'rows' => $result->items(),
  423. ]);
  424. }
  425. /**
  426. * 添加获取职能部门
  427. * @param array $data
  428. * @return array
  429. */
  430. public function addZhinengbumen(array $data): array
  431. {
  432. $result = Department::insertGetId($data);
  433. if (empty($result)) {
  434. return Result::error("创建失败", 0);
  435. } else {
  436. return Result::success(["id" => $result]);
  437. }
  438. }
  439. public function delZhinengbumen(array $data): array
  440. {
  441. $result = Department::where('id', $data['id'])->delete();
  442. if (empty($result)) {
  443. return Result::error("删除失败", 0);
  444. } else {
  445. return Result::success();
  446. }
  447. }
  448. public function getZhinengbumen(array $data): array
  449. {
  450. $result = Department::where('id', $data['id'])
  451. ->orderBy('department', 'desc')
  452. ->first();
  453. return Result::success($result);
  454. }
  455. public function getPidZhinengbumen(array $data): array
  456. {
  457. if (empty($data['pid'])) {
  458. $data['pid'] = 0;
  459. }
  460. $result = Department::where('pid', $data['pid'])->get();
  461. return Result::success($result);
  462. }
  463. public function modZhinengbumen(array $data): array
  464. {
  465. $result = Department::where('id', $data['id'])->update($data);
  466. if (empty($result)) {
  467. return Result::error("修改失败", 0);
  468. } else {
  469. return Result::success();
  470. }
  471. }
  472. /**
  473. * 查询职能列表
  474. * @param array $data
  475. * @return array
  476. */
  477. public function getDepartment(array $data): array
  478. {
  479. $where = [];
  480. if (isset($data['pid'])) {
  481. $where = [
  482. 'pid' => $data['pid'] ?? 0
  483. ];
  484. }
  485. $result = Department::when(!empty($where), function ($query) use ($where) {
  486. $query->where($where);
  487. })->orderBy("sort", "desc")->get();
  488. if (empty($result)) {
  489. return Result::error("查询失败", 0);
  490. } else {
  491. return Result::success($result);
  492. }
  493. }
  494. /**
  495. * 获取所有的buckets
  496. * @param array $data
  497. * @return array
  498. */
  499. public function getBuckets(array $data): array
  500. {
  501. $result = new MinioService();
  502. // 调用服务层的方法获取存储桶列表
  503. $bucketsResponse = $result->listBuckets();
  504. // 直接返回服务层生成的响应
  505. return Result::success($bucketsResponse['data']);
  506. }
  507. /**
  508. * 上传文件
  509. * @param array $data
  510. * @return array
  511. */
  512. public function uploadFile(array $data): array
  513. {
  514. $result = new MinioService();
  515. $rep = $result->uploadFile($data);
  516. if ($rep['code'] == 200) {
  517. return Result::success($rep['data']);
  518. } else {
  519. return Result::error("上传失败!");
  520. }
  521. }
  522. /**
  523. * 黑名单管理
  524. * @param array $data
  525. * @return array
  526. */
  527. public function getBlackWordList(array $data): array
  528. {
  529. $result = BlackWord::when($data, function ($query) use ($data) {
  530. if (isset($data['name']) && $data['name']) {
  531. $query->where('black_word.name', 'like', '%' . $data['name'] . '%');
  532. }
  533. })->orderBy('black_word.id', 'desc')
  534. ->paginate(
  535. intval($data['pageSize']),
  536. [
  537. 'black_word.*',
  538. ],
  539. 'page',
  540. intval($data['page'])
  541. );
  542. $count = $result->total();
  543. $returnData = [
  544. 'rows' => $result->items(),
  545. 'count' => $count
  546. ];
  547. return Result::success($returnData);
  548. }
  549. /**
  550. * 添加黑名单
  551. * @param array $data
  552. * @return array
  553. */
  554. public function addBlackWord(array $data): array
  555. {
  556. Db::beginTransaction();
  557. try {
  558. $info = BlackWord::where(['name' => $data['name']])->first();
  559. if ($info) {
  560. Db::rollBack();
  561. return Result::error("该黑名单已存在", 0);
  562. }
  563. $data['type'] = 10;
  564. $result = BlackWord::insertGetId($data);
  565. $redisKey = 'black_word';
  566. $this->redis->sAdd($redisKey, $data['name']);
  567. Db::commit();
  568. return Result::success(["id" => $result]);
  569. } catch (\Exception $e) {
  570. Db::rollBack();
  571. return Result::error("创建失败" . $e->getMessage(), 0);
  572. }
  573. }
  574. /**
  575. * 删除黑名单
  576. * @param array $data
  577. * @return array
  578. */
  579. public function delBlackWord(array $data): array
  580. {
  581. Db::beginTransaction();
  582. try {
  583. BlackWord::where(['name' => $data['name']])->delete();
  584. $redisKey = 'black_word';
  585. $this->redis->sRem($redisKey, $data['name']);
  586. Db::commit();
  587. return Result::success([]);
  588. } catch (\Exception $e) {
  589. Db::rollBack();
  590. return Result::error("删除失败" . $e->getMessage(), 0);
  591. }
  592. }
  593. /**
  594. * 修改违禁词
  595. * @param array $data
  596. * @return array
  597. */
  598. public function upBlackWord(array $data): array
  599. {
  600. Db::beginTransaction();
  601. try {
  602. $checkInfo = BlackWord::where(['name' => $data['name']])->first();
  603. if ($checkInfo) {
  604. Db::rollBack();
  605. return Result::error("该违禁词已经存在", 0);
  606. }
  607. $blackWordInfo = BlackWord::where(['id' => $data['id']])->first();
  608. if ($blackWordInfo) {
  609. //先删除redis
  610. $blackWordInfo = $blackWordInfo->toArray();
  611. $redisKey = 'black_word';
  612. $this->redis->sRem($redisKey, $blackWordInfo['name']);
  613. $this->redis->sAdd($redisKey, $data['name']);
  614. BlackWord::where(['id' => $data['id']])->update(['name' => $data['name']]);
  615. Db::commit();
  616. return Result::success([]);
  617. } else {
  618. Db::rollBack();
  619. return Result::error("系统错误", 0);
  620. }
  621. } catch (\Exception $e) {
  622. Db::rollBack();
  623. return Result::error("修改失败" . $e->getMessage(), 0);
  624. }
  625. }
  626. /**
  627. * 获取风格
  628. * @return void
  629. */
  630. public function getTemplateClassList(array $data): array
  631. {
  632. $where = [];
  633. if (isset($data['name']) && $data['name']) {
  634. array_push($where, ['template_class.name', 'like', '%' . $data['name'] . '%']);
  635. }
  636. if (isset($data['keyword']) && $data['keyword']) {
  637. array_push($where, ['template_class.keyword', 'like', '%' . $data['keyword'] . '%']);
  638. }
  639. $template = TemplateClass::when($where, function ($query) use ($where) {
  640. $query->where($where);
  641. });
  642. $count = $template->count();
  643. // $countQuery = clone $template;
  644. $row = $template
  645. ->leftJoin('template', function ($join) {
  646. $join->on('template_class.class_id', '=', 'template.template_class_id');
  647. })
  648. ->select('template_class.*', DB::raw('COUNT(template.template_class_id) as template_count'))
  649. ->groupBy('template_class.id')
  650. ->orderBy('template_class.id', 'desc')
  651. ->offset(($data['page'] - 1) * $data['pageSize'])
  652. ->limit($data['pageSize'])
  653. ->get();
  654. $result = [
  655. 'rows' => $row,
  656. 'count' => $count,
  657. ];
  658. if ($row->isEmpty()) {
  659. return Result::error("暂无风格", 0);
  660. } else {
  661. return Result::success($result);
  662. }
  663. }
  664. /**
  665. * 添加风格
  666. * @param
  667. * @return void
  668. */
  669. public function addTemplateClass(array $data): array
  670. {
  671. $data['keyword'] = json_encode($data['keyword']);
  672. $template_class = TemplateClass::where('name', $data['name'])
  673. ->orWhere('class_id', $data['class_id'])
  674. ->first();
  675. if ($template_class) {
  676. return Result::error("风格名称或者风格编号已存在,不可添加!", 0);
  677. }
  678. $result = TemplateClass::insertGetId($data);
  679. if (empty($result)) {
  680. return Result::error("创建风格失败", 0);
  681. } else {
  682. return Result::success(["id" => $result]);
  683. }
  684. }
  685. /**
  686. * 更新风格
  687. * @param array $data
  688. * @return array
  689. */
  690. public function upTemplateClass(array $data): array
  691. {
  692. $where = [
  693. 'id' => $data['id'],
  694. ];
  695. $template_class = TemplateClass::where($where)->first();
  696. if (empty($template_class)) {
  697. return Result::error("未查询到风格", 0);
  698. }
  699. if ($template_class->type == 1) {
  700. return Result::error("默认风格不能修改", 0);
  701. }
  702. $template = TemplateClass::where('id', '!=', $data['id'])
  703. // ->where(['name' => $data['name']])
  704. // ->orWhere(['class_id' => $data['class_id']])
  705. ->where(function ($query) use ($data) {
  706. $query->where('name', $data['name'])
  707. ->orWhere('class_id', $data['class_id']);
  708. })
  709. ->first();
  710. if ($template) {
  711. return Result::error("风格名称或者风格编号已存在,不可编辑!", 0);
  712. }
  713. $updateData = [
  714. 'name' => $data['name'],
  715. 'keyword' => json_encode($data['keyword']),
  716. 'class_id' => $data['class_id'],
  717. ];
  718. $result = TemplateClass::where($where)->update($updateData);
  719. if (empty($result)) {
  720. return Result::error("更新失败", 0);
  721. } else {
  722. return Result::success($result);
  723. }
  724. }
  725. /**
  726. * 删除风格
  727. * @param array $data
  728. * @return array
  729. */
  730. public function delTemplateClass(array $data): array
  731. {
  732. $where = [
  733. 'id' => $data['id'],
  734. ];
  735. $template_class = TemplateClass::where($where)->first();
  736. if (empty($template_class)) {
  737. return Result::error("未查询到风格", 0);
  738. }
  739. if ($template_class->type == 1) {
  740. return Result::error("默认风格不能删除", 0);
  741. }
  742. $template = Template::where('template_class_id', $template_class['class_id'])->get();
  743. if (!empty($template->all())) {
  744. try {
  745. // 默认风格 1
  746. Db::beginTransaction();
  747. $template = Template::where('template_class_id', $template['class_id'])->update(['template_class_id' => 1]);
  748. if (empty($template)) {
  749. Db::rollBack();
  750. return Result::error("删除失败", 0);
  751. }
  752. $result = TemplateClass::where($where)->delete();
  753. } catch (\Exception $e) {
  754. return Result::error("删除失败" . $e->getMessage(), 0);
  755. }
  756. } else {
  757. $result = TemplateClass::where('id', $data['id'])->delete();
  758. }
  759. if (empty($result)) {
  760. return Result::error("删除失败", 0);
  761. }
  762. return Result::success($result);
  763. }
  764. /**
  765. * 根据风格名称-获取获取所有风格
  766. * @param array $data
  767. * @return array
  768. */
  769. public function getTemplateClass(array $data): array
  770. {
  771. $where = [];
  772. if (!empty($data['template_name'])) {
  773. $where['name'] = $data['template_name'];
  774. }
  775. $result = TemplateClass::where($where)->get();
  776. if (empty($result)) {
  777. return Result::error("暂无风格", 0);
  778. }
  779. return Result::success($result);
  780. }
  781. /**
  782. * 获取皮肤列表
  783. * @param array $data
  784. * @return array
  785. */
  786. public function getTemplateList(array $data): array
  787. {
  788. $where = [];
  789. if (!empty($data['template_class_id'])) {
  790. $where['template_class_id'] = $data['template_class_id'];
  791. }
  792. if (!empty($data['template_name'])) {
  793. array_push($where, ['template_name', 'like', '%' . $data['template_name'] . '%']);
  794. }
  795. if (!empty($data['template_keyword'])) {
  796. array_push($where, ['template_keyword', 'like', '%' . $data['template_keyword'] . '%']);
  797. }
  798. $result = Template::where($where)
  799. ->leftJoin('template_class', 'template.template_class_id', 'template_class.class_id')
  800. ->select('template.*', 'template_class.name as template_class_name')
  801. ->orderBy('template.id', 'desc')
  802. ->paginate($data['page_size'], ['*'], 'mypage_name', $data['page']);
  803. if (empty($result)) {
  804. return Result::error("暂无皮肤", 0);
  805. }
  806. return Result::success($result);
  807. }
  808. public function getTemplateInfo(array $data): array
  809. {
  810. $result = Template::where('template.id', $data['id'])
  811. ->leftJoin('template_class', 'template.template_class_id', 'template_class.class_id')
  812. ->select('template.*', 'template_class.name as template_class_name')
  813. ->first();
  814. if (empty($result)) {
  815. return Result::error("暂无皮肤", 0);
  816. }
  817. return Result::success($result);
  818. }
  819. /**
  820. * 添加皮肤
  821. * @param array $data
  822. * @return array
  823. */
  824. public function addTemplate(array $data): array
  825. {
  826. var_dump($data);
  827. unset($data['user_id']);
  828. $template = Template::where('template_name', $data['template_name'])
  829. ->orWhere('template_id', $data['template_id'])
  830. ->first();
  831. if ($template) {
  832. return Result::error("皮肤名称或者皮肤编号已存在,不可添加!", 0);
  833. }
  834. $data['template_keyword'] = json_encode($data['template_keyword']) ?? '';
  835. $page_type = json_decode($data['page_type'], true);
  836. $data['page_type'] = json_encode(array_values(array_unique($page_type)));
  837. $result = Template::insertGetId($data);
  838. if (empty($result)) {
  839. return Result::error("创建失败", 0);
  840. } else {
  841. return Result::success($result);
  842. }
  843. }
  844. /**
  845. * 删除皮肤
  846. * @param array $data
  847. * @return array
  848. */
  849. public function delTemplate(array $data): array
  850. {
  851. $rector = Sector::where('template_id', $data['id'])->first();
  852. if (!empty($rector)) {
  853. return Result::error("此皮肤已被绑定通栏,不可删除", 0);
  854. }
  855. $template = WebsiteTemplateInfo::where('template_id', $data['id'])->first();
  856. if (empty($template)) {
  857. return Result::error("此皮肤已被绑定网站,不可删除", 0);
  858. }
  859. if ($template->type == 1) {
  860. return Result::error("默认皮肤不能删除", 0);
  861. }
  862. $result = Template::where('id', $data['id'])->delete();
  863. var_dump($result, '-------------------delete');
  864. if ($result) {
  865. return Result::success($result);
  866. } else {
  867. return Result::error("删除失败", 0);
  868. }
  869. }
  870. /**
  871. * 更新皮肤
  872. * @param array $data
  873. * @return array
  874. */
  875. public function updateTemplate(array $data): array
  876. {
  877. unset($data['user_id']);
  878. $template = Template::where('id', $data['id'])
  879. ->first();
  880. if (empty($template)) {
  881. return Result::error("此皮肤不存在!", 0);
  882. }
  883. $template = Template::where('id', '!=', $data['id'])
  884. ->where(function ($query) use ($data) {
  885. $query->where('template_name', $data['template_name'])
  886. ->orWhere('template_id', $data['template_id']);
  887. })
  888. ->first();
  889. if ($template) {
  890. return Result::error("皮肤名称或者皮肤编号已存在,不可编辑!", 0);
  891. }
  892. $data['template_keyword'] = json_encode($data['template_keyword']) ?? '';
  893. $page_type = json_decode($data['page_type'], true);
  894. $data['page_type'] = json_encode(array_values(array_unique($page_type)));
  895. $result = Template::where('id', $data['id'])->update($data);
  896. var_dump($result, '-------------------update');
  897. if (!$result) {
  898. return Result::error("更新失败", 0);
  899. } else {
  900. return Result::success('更新成功');
  901. }
  902. }
  903. /**
  904. * 获取所有尺寸
  905. * @param array $data
  906. * @return array
  907. */
  908. public function getAllSize(array $data): array
  909. {
  910. if(isset($data['id']) && !empty($data['id'])){
  911. $result = Size::where('id', $data['id'])->get()->all();
  912. }else{
  913. $result = Size::get()->all();
  914. }
  915. if(empty($result)){
  916. return Result::error("暂无尺寸", 0);
  917. }
  918. return Result::success($result);
  919. }
  920. /**
  921. * 根据皮肤名称-获取所有皮肤
  922. * @param array $data
  923. * @return array
  924. */
  925. public function getAllTemplate(array $data): array
  926. {
  927. $where = [];
  928. if (isset($data['template_class_id']) && !empty($data['template_class_id'])) {
  929. $where[] = ['template_class_id', $data['template_class_id']];
  930. }
  931. if (isset($data['template_name']) && !empty($data['template_name'])) {
  932. $where[] = ['template_name', 'like', '%' . $data['template_name'] . '%'];
  933. }
  934. $result = Template::where($where)->get()->all();
  935. if (empty($result)) {
  936. return Result::error("暂无皮肤", 0);
  937. }
  938. return Result::success($result);
  939. }
  940. /**
  941. * 获取通栏列表
  942. * @param array $data
  943. * @return array
  944. */
  945. public function getSectorList(array $data): array
  946. {
  947. $where = [];
  948. if (isset($data['template_class_id']) && !empty($data['template_class_id'])) {
  949. $where['template.template_class_id'] = $data['template_class_id'];
  950. }
  951. if (isset($data['template_id']) && !empty($data['template_id'])) {
  952. array_push($where, ['template.template_id', $data['template_id']]);
  953. }
  954. if (isset($data['sector_name']) && !empty($data['sector_name'])) {
  955. array_push($where, ['sector.sector_name', 'like', '%' . $data['sector_name'] . '%']);
  956. }
  957. if (isset($data['width']) && !empty($data['width']) && isset($data['height']) && !empty($data['height'])) {
  958. $size_id = Size::where('width', 'like', '%' . $data['width'] . '%')->where('height', 'like', '%' . $data['height'] . '%')->pluck('id');
  959. } else if (isset($data['width']) && !empty($data['width'])) {
  960. $size_id = Size::where('width', 'like', '%' . $data['width'] . '%')->pluck('id');
  961. } else if (isset($data['height']) && !empty($data['height'])) {
  962. $size_id = Size::where('height', 'like', '%' . $data['height'] . '%')->pluck('id');
  963. } else {
  964. $size_id = [];
  965. }
  966. if(isset($data['page_type']) && !empty($data['page_type'])){
  967. array_push($where, ['sector.page_type', 'like', '%' . $data['page_type'] . '%']);
  968. }
  969. $result = Sector::when(!empty($size_id), function ($query) use ($size_id) {
  970. $query->whereIn('sector.size_id', $size_id);
  971. })
  972. ->leftJoin('size', 'size.id', '=', 'sector.size_id')
  973. ->leftJoin('template', 'template.template_id', '=', 'sector.template_id')
  974. ->leftJoin('template_class', 'template_class.class_id', '=', 'template.template_class_id')
  975. ->leftJoin('sector_place', 'sector_place.sector_type', '=', 'sector.place_type')
  976. ->where($where)
  977. ->where('sector_place.type_id', 1)
  978. ->select('sector.*', 'size.width', 'size.height', 'template_class.name as class_name', 'template_class.class_id', 'template.template_name',
  979. 'sector_place.name as place_name', 'sector_place.sector_img as place_img')
  980. ->orderBy('sector.id', 'desc')
  981. ->paginate($data['page_size'], ['*'], 'page', $data['page']);
  982. if(empty($result)){
  983. return Result::error('暂无通栏!');
  984. }
  985. return Result::success($result);
  986. }
  987. /**
  988. * 获取通栏详情
  989. * @param array $data
  990. * @return array
  991. */
  992. public function getSectorInfo(array $data): array
  993. {
  994. $where = [];
  995. $where[] = ['sector.id', '=', $data['id']];
  996. $result = Sector::where($where)
  997. ->leftJoin('size', 'size.id', '=', 'sector.size_id')
  998. ->select('sector.*', 'size.width', 'size.height')
  999. ->orderBy('sector.id', 'desc')
  1000. ->first();
  1001. if (empty($result)) {
  1002. return Result::error('此通栏不存在!');
  1003. }
  1004. return Result::success($result);
  1005. }
  1006. /**
  1007. * 获取经纬度信息
  1008. * @return void
  1009. */
  1010. public function getIpInfo(array $data): array
  1011. {
  1012. if (empty($data['ip'])) {
  1013. // 在RPC服务中,依赖$_SERVER是不可靠的,强制要求调用方传入IP
  1014. return Result::error('IP地址不能为空!');
  1015. }
  1016. $client_ip = $data['ip'];
  1017. try {
  1018. // 直接从容器获取 Guzzle 客户端实例
  1019. /** @var \GuzzleHttp\Client $client */
  1020. $client = \Hyperf\Context\ApplicationContext::getContainer()->get(Client::class);
  1021. // 使用新的 GeoJS API 地址
  1022. $api_url = "https://get.geojs.io/v1/ip/geo/{$client_ip}.json";
  1023. // 将 timeout 作为请求选项传递
  1024. $response = $client->get($api_url, ['timeout' => 5]);
  1025. if ($response->getStatusCode() !== 200) {
  1026. return Result::error('IP查询接口请求失败,状态码:' . $response->getStatusCode());
  1027. }
  1028. $ip_info = json_decode($response->getBody()->getContents(), true);
  1029. // GeoJS 的成功响应不包含 'status' 字段,直接检查关键数据是否存在
  1030. if ($ip_info && isset($ip_info['latitude']) && isset($ip_info['longitude'])) {
  1031. // GeoJS 返回的经纬度是字符串,转换为浮点数
  1032. $ip_info['latitude'] = floatval($ip_info['latitude']);
  1033. $ip_info['longitude'] = floatval($ip_info['longitude']);
  1034. return Result::success($ip_info);
  1035. }
  1036. // 如果返回的数据不符合预期,提供一个通用错误
  1037. $errorMessage = $ip_info['message'] ?? '获取IP地理位置信息失败';
  1038. return Result::error($errorMessage);
  1039. } catch (GuzzleException $e) {
  1040. // 这个异常会给出详细的网络错误信息,如超时、无法解析域名等
  1041. return Result::error('请求IP查询接口异常: ' . $e->getMessage());
  1042. } catch (\Throwable $e) {
  1043. return Result::error('获取IP信息时发生未知错误: ' . $e->getMessage());
  1044. }
  1045. }
  1046. /**
  1047. * 获取天气
  1048. * @param array $data
  1049. * @return array
  1050. */
  1051. public function getWeatherInfo(array $data): array
  1052. {
  1053. $month = $data['month'] ?? date('m');
  1054. $day = $data['day'] ?? date('d');
  1055. // 使用缓存键
  1056. $cacheKey = "tsbb_data_weather_{$month}_{$day}";
  1057. // 尝试从缓存获取数据
  1058. $container = \Hyperf\Context\ApplicationContext::getContainer();
  1059. $cache = $container->get(\Psr\SimpleCache\CacheInterface::class);
  1060. if ($cachedData = $cache->get($cacheKey)) {
  1061. return Result::success(unserialize($cachedData));
  1062. }
  1063. $location = $data['latitude'] . ":" . $data['longitude'];
  1064. $api_url = "https://api.seniverse.com/v3/weather/now.json?key=" . \Hyperf\Support\env('WEATHER_KEY') . "&location=" . $location . "&language=zh-Hans&unit=c";
  1065. $ch = curl_init($api_url);
  1066. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  1067. $response = curl_exec($ch);
  1068. curl_close($ch);
  1069. // 解析 JSON 响应
  1070. $WeatherInfo = json_decode($response, true);
  1071. // 缓存结果,设置1小时过期
  1072. $cache->set($cacheKey, serialize($WeatherInfo), 3600);
  1073. if ($WeatherInfo) {
  1074. return Result::success($WeatherInfo);
  1075. } else {
  1076. return Result::error("获取天气失败", 0);
  1077. }
  1078. }
  1079. /**
  1080. * 获取农历信息
  1081. * @return void
  1082. */
  1083. public function getCalendar(array $data): array
  1084. {
  1085. $calendar = new Calendar();
  1086. $result = $calendar->solar($data['year'], $data['month'], $data['day'], $data['hour']); // 阳历
  1087. return Result::success($result);
  1088. }
  1089. /**
  1090. * 添加通栏
  1091. * @param array $data
  1092. * @return array
  1093. */
  1094. public function addSector(array $data): array
  1095. {
  1096. unset($data['user_id']);
  1097. // 对传入的 page_type 数组进行去重、转换为整数并重新索引
  1098. $data['page_type'] = array_values(array_unique(array_map('intval', $data['page_type'])));
  1099. $data['page_type'] = json_encode($data['page_type']);
  1100. $template = Template::where('template_id', $data['template_id'])
  1101. ->first();
  1102. if (empty($template)) {
  1103. return Result::error('皮肤不存在!');
  1104. }
  1105. $sector_id = Sector::where('sector_id',$data['sector_id'])->first();
  1106. if(!empty($sector_id)){
  1107. return Result::error('通栏编号已存在!');
  1108. }
  1109. $data['status'] = 0;
  1110. // 通栏分类:1:资讯类:2:通栏广告类;3:混合类;4:头条类;5:轮播图类;
  1111. $sector_code = [
  1112. 'sectorName' => $data['sector_name'],
  1113. 'sectorId' => $data['sector_id'],
  1114. 'sectorType' => $data['sector_type'],
  1115. 'sectorPlace' => $data['place_type'],
  1116. ];
  1117. // return Result::success($sector_code);
  1118. $data['sector_code'] = json_encode($sector_code);
  1119. $result = Sector::insertGetId($data);
  1120. if (empty($result)) {
  1121. return Result::error('添加失败');
  1122. }
  1123. return Result::success($result);
  1124. }
  1125. /**
  1126. * 删除通栏
  1127. * @param array $data
  1128. * @return array
  1129. */
  1130. public function delSector(array $data): array
  1131. {
  1132. $component_id = Component::where('sector_id', $data['id'])->pluck('id')->toArray();
  1133. if (!empty($component_id)) {
  1134. return Result::error('请先删除相关组件!');
  1135. }
  1136. $result = Sector::where('id', $data['id'])->delete();
  1137. if ($result == 1) {
  1138. return Result::success('删除成功');
  1139. } else {
  1140. return Result::error('删除失败');
  1141. }
  1142. }
  1143. /**
  1144. * 修改通栏
  1145. * @param array $data
  1146. * @return array
  1147. */
  1148. public function updateSector(array $data): array
  1149. {
  1150. unset($data['user_id']);
  1151. $sector = Sector::where('id', $data['id'])->first();
  1152. if (empty($sector)) {
  1153. return Result::error('通栏不存在!');
  1154. }
  1155. if($sector['sector_id'] != $data['sector_id']){
  1156. $sector_id = Sector::where('sector_id',$data['sector_id'])->first();
  1157. if(!empty($sector_id)){
  1158. return Result::error('通栏编号已存在!');
  1159. }
  1160. }
  1161. $sector_code = [
  1162. 'sectorName' => strval($data['sector_name']),
  1163. 'sectorId' => intval($data['sector_id']),
  1164. 'sectorType' => intval($data['sector_type']),
  1165. 'sectorPlace' => intval($data['place_type']),
  1166. ];
  1167. // return Result::success($sector_code);
  1168. $data['sector_code'] = json_encode($sector_code);
  1169. // 对传入的 page_type 数组进行去重、转换为整数并重新索引
  1170. $data['page_type'] = array_values(array_unique(array_map('intval', $data['page_type'])));
  1171. $data['page_type'] = json_encode($data['page_type']);
  1172. $template = Template::where('template_id', $data['template_id'])
  1173. ->whereRaw("JSON_CONTAINS(template.page_type, ?)", [$data['page_type']])
  1174. ->first();
  1175. if (empty($template)) {
  1176. return Result::error('皮肤不存在!');
  1177. }
  1178. $result = Sector::where('id', $data['id'])->update($data);
  1179. if ($result == 1) {
  1180. return Result::success('修改成功');
  1181. } else {
  1182. return Result::error('修改失败');
  1183. }
  1184. }
  1185. public function getComponentList(array $data): array
  1186. {
  1187. $where = [];
  1188. if (isset($data['template_class_id']) && !empty($data['template_class_id'])) {
  1189. $where['template_class.class_id'] = $data['template_class_id'];
  1190. }
  1191. if (isset($data['component_name']) && !empty($data['component_name'])) {
  1192. array_push($where, ['component.component_name', 'like', '%' . $data['component_name'] . '%']);
  1193. }
  1194. if (isset($data['sector_id']) && !empty($data['sector_id'])) {
  1195. $where['component.sector_id'] = $data['sector_id'];
  1196. };
  1197. if (isset($data['width']) && !empty($data['width']) && isset($data['height']) && !empty($data['height'])) {
  1198. $size_id = Size::where('width', 'like', '%' . $data['width'] . '%')->where('height', 'like', '%' . $data['height'] . '%')->pluck('id');
  1199. } else if (isset($data['width']) && !empty($data['width'])) {
  1200. $size_id = Size::where('width', 'like', '%' . $data['width'] . '%')->pluck('id');
  1201. } else if (isset($data['height']) && !empty($data['height'])) {
  1202. $size_id = Size::where('height', 'like', '%' . $data['height'] . '%')->pluck('id');
  1203. } else {
  1204. $size_id = [];
  1205. }
  1206. if(isset($data['page_type']) && !empty($data['page_type'])){
  1207. array_push($where, ['component.page_type', 'like', '%' . $data['page_type'] . '%']);
  1208. }
  1209. if(isset($data['type_id']) && !empty($data['type_id'])){
  1210. $where['component.type_id'] = $data['type_id'];
  1211. }
  1212. $result = Component::when(!empty($size_id), function ($query) use ($size_id) {
  1213. $query->whereIn('component.size_id', $size_id);
  1214. })
  1215. ->where($where)
  1216. ->leftJoin('template', 'template.template_id', '=', 'component.template_id')
  1217. ->leftJoin('template_class', 'template.template_class_id', '=', 'template_class.class_id') // 添加与 template_class 的关联
  1218. ->leftJoin('sector', 'sector.sector_id', '=', 'component.sector_id')
  1219. ->leftJoin('size','size.id','=','component.size_id')
  1220. ->leftJoin('component_type','component_type.id','=','component.type_id')
  1221. ->select( 'component.*', 'sector.sector_id','sector.sector_name','template.template_id',
  1222. 'template.template_name','template_class.class_id', 'template_class.name as class_name',
  1223. 'size.width','size.height','component_type.com_typename')
  1224. ->orderBy('sector.updated_at', 'desc')
  1225. ->paginate($data['page_size'], ['*'], 'mypage_name', $data['page']);
  1226. if(empty($result)){
  1227. return Result::error('暂无数据');
  1228. }
  1229. return Result::success($result);
  1230. }
  1231. public function getComponentInfo(array $data): array
  1232. {
  1233. $where = ['component.id'=>$data['id']];
  1234. $result = Component::where($where)
  1235. // ->leftJoin('template_class', 'template_class.id', '=', 'template.template_class_id') // 去掉这一行
  1236. ->leftJoin('sector', 'sector.id', '=', 'component.sector_id')
  1237. ->leftJoin('template', 'template.template_id', '=', 'sector.template_id')
  1238. ->leftJoin('size','size.id','=','component.size_id')
  1239. ->leftJoin('component_type','component_type.id','=','component.type_id')
  1240. ->select( 'template.template_name as template_name', 'sector.sector_name as sector_name', 'component.*',
  1241. 'size.width','size.height','component_type.com_typename')
  1242. ->get();
  1243. if(empty($result)){
  1244. return Result::error('暂无数据');
  1245. }
  1246. return Result::success($result);
  1247. }
  1248. /*
  1249. 新增组件
  1250. */
  1251. public function addComponent(array $data): array
  1252. {
  1253. unset($data['user_id']);
  1254. $sector = Sector::where('sector_id',$data['sector_id'])->first();
  1255. if(empty($sector)){
  1256. return Result::error('通栏不存在!');
  1257. }
  1258. $component_id = Component::where('component_type',$data['component_type'])->first();
  1259. if(!empty($component_id)){
  1260. return Result::error('组件编号已存在!');
  1261. }
  1262. $component_place = SectorPlace::where('sector_type',$sector['place_type'])
  1263. ->where('type_id',2)->where('sort_id',$data['sort_id'])->first();
  1264. // 组件分类:1:资讯-头条组件;2:资讯-轮播组件;3:资讯-推荐类组件;4:资讯-热点类组件;5:栏目资讯类;
  1265. // 6:广告类组件;7:列表类组件;8:详情类组件;9:底部导航类组件;10:最新资讯类组件;11:导航栏类;
  1266. if(is_array($data['page_type']) && !empty($data['page_type'])){
  1267. $page_type = array_values(array_map('intval',$data['page_type']));
  1268. $data['page_type'] = json_encode($page_type,true);
  1269. }
  1270. $data['type_id'] = intval($data['type_id']);
  1271. $add_arr = [
  1272. 'template_id' => intval($data['template_id']),
  1273. 'sector_id' => intval($data['sector_id']),
  1274. 'page_type' => $data['page_type'],
  1275. 'component_type' => intval($data['component_type']),
  1276. 'component_name' => $data['component_name'],
  1277. 'component_img' => $data['component_img'],
  1278. 'place_type' => $component_place['component_type'],
  1279. 'size_id' => $component_place['size_id'],
  1280. 'type_id' => intval($data['type_id']),
  1281. 'sort_id' => intval($data['sort_id']),
  1282. ];
  1283. switch($data['type_id']){
  1284. case 1: //1:资讯-头条组件;
  1285. case 2: //2:资讯-轮播组件;
  1286. case 3: //3:资讯-推荐类组件;
  1287. case 4: //4:资讯-热点类组件;
  1288. case 10: //10:最新资讯类;
  1289. $add_arr['level'] = intval($data['level']);
  1290. $add_arr['img_num'] = intval($data['img_num']);
  1291. $add_arr['text_num'] = intval($data['text_num']);
  1292. $add_arr['child_imgnum'] = intval($data['img_num'] ?? null);
  1293. $add_arr['child_textnum'] = intval($data['text_num'] ?? null);
  1294. $component_data['componentData'] = [
  1295. 'category_id' => '',
  1296. 'level' => $add_arr['level'] ?? '',
  1297. 'imgSize' => $add_arr['img_num'] ?? '',
  1298. 'textSize' => $add_arr['text_num'] ?? '',
  1299. 'child' => [
  1300. 'id' => '',
  1301. 'imgSize' => $add_arr['child_imgnum'] ?? '',
  1302. 'textSize' => $add_arr['child_textnum'] ?? '',
  1303. ]
  1304. ];
  1305. break;
  1306. case 5: //5:栏目资讯类组件;
  1307. $add_arr['img_num'] = intval($data['img_num']);
  1308. $add_arr['text_num'] = intval($data['text_num']);
  1309. $add_arr['child_imgnum'] = intval($data['child_imgnum'] ?? null);
  1310. $add_arr['child_textnum'] = intval($data['child_textnum'] ?? null);
  1311. $component_data['componentData'] = [
  1312. 'category_id' => '',
  1313. 'level' => intval($data['level'] ?? ''),
  1314. 'imgSize' => $add_arr['img_num'] ?? '',
  1315. 'textSize' => $add_arr['text_num'] ?? '',
  1316. 'child' => [
  1317. 'id' => '',
  1318. 'imgSize' => $add_arr['child_imgnum'] ?? '',
  1319. 'textSize' => $add_arr['child_textnum'] ?? '',
  1320. ]
  1321. ];
  1322. break;
  1323. case 7: //7:列表类组件;
  1324. $add_arr['pageSize'] = intval($data['pageSize']);
  1325. $component_data['componentData'] = [
  1326. 'category_id' => '',
  1327. 'pageType' => [
  1328. 'page' => 1,
  1329. 'pageSize' => $add_arr['pageSize'] ?? '',
  1330. ]
  1331. ];
  1332. break;
  1333. case 8: //8:详情类组件;
  1334. $component_data['componentData'] = [
  1335. 'article_id' => '',
  1336. ];
  1337. break;
  1338. case 9: //9:底部导航类组件;
  1339. $component_data['componentData'] = [
  1340. 'fcat_id' => '',
  1341. ];
  1342. break;
  1343. case 11: //11:导航栏类;
  1344. $add_arr['cate_place'] = 0;
  1345. $add_arr['pageSize'] = intval($data['pageSize']);
  1346. $component_data['componentData'] = [
  1347. 'pid' => '',
  1348. 'placeid' => 1,
  1349. 'num' => $add_arr['pageSize'],
  1350. ];
  1351. break;
  1352. default:
  1353. break;
  1354. }
  1355. $component_head_code = [
  1356. 'component_type' => $add_arr['component_type'],
  1357. 'type_id' => $add_arr['type_id'],
  1358. 'component_name' => $add_arr['component_name'],
  1359. 'sort_id' => $add_arr['sort_id'],
  1360. ];
  1361. if(isset($data['listType']) && !empty($data['listType']) && is_array($data['listType'])){
  1362. $list_type['listType'] = $data['listType'];
  1363. $add_arr['component_column'] = json_encode($list_type,true);
  1364. }
  1365. if(isset($component_head_code) && !empty($component_head_code) && is_array($component_head_code)){
  1366. $add_arr['component_code'] = json_encode($component_head_code,true);
  1367. }
  1368. if(isset($component_data) && !empty($component_data) && is_array($component_data)){
  1369. $add_arr['component_data'] = json_encode($component_data,true);
  1370. }
  1371. // return Result::success($add_arr);
  1372. Db::beginTransaction();
  1373. try{
  1374. $result = Component::insertGetId($add_arr);
  1375. if(empty($result)){
  1376. Db::rollBack();
  1377. return Result::error('添加失败!');
  1378. }
  1379. $component = Component::where('sector_id',$sector['sector_id'])->distinct('sort_id')->count('sort_id');
  1380. // return Result::success($component == $sector['component_num']);
  1381. if($component == $sector['component_num'] && $sector['status'] == 0){
  1382. $sector_status = Sector::where('sector_id',$sector['sector_id'])->update(['status'=>1]);
  1383. if(empty($sector_status)){
  1384. Db::rollBack();
  1385. return Result::error('通栏状态不存在!');
  1386. }
  1387. }
  1388. Db::commit();
  1389. return Result::success($result);
  1390. }catch(\Exception $e){
  1391. Db::rollBack();
  1392. return Result::error($e->getMessage());
  1393. }
  1394. }
  1395. public function delComponent(array $data): array
  1396. {
  1397. $component = Component::where('id', $data['id'])->first();
  1398. if(empty($component)){
  1399. return Result::error('组件不存在!');
  1400. }
  1401. // $sector_num = Sector::where('sector_id',$component['sector_id'])->first();
  1402. Db::beginTransaction();
  1403. try{
  1404. $result = Component::where('id',$data['id'])->delete();
  1405. if(empty($result)){
  1406. Db::rollBack();
  1407. return Result::error('删除失败!');
  1408. }
  1409. $component_num = Component::where('id','!=',$data['id'])->where('sector_id',$component['sector_id'])
  1410. ->where('sort_id',$component['sort_id'])->count();
  1411. $sector_status = Sector::where('sector_id',$component['sector_id'])->first();
  1412. if($component_num == 0 && $sector_status['status'] == 1){
  1413. $sector = Sector::where('sector_id',$component['sector_id'])->update(['status',0]);
  1414. if(empty($sector)){
  1415. Db::rollBack();
  1416. return Result::error('通栏不存在!');
  1417. }
  1418. }
  1419. Db::commit();
  1420. }catch(\Exception $e){
  1421. Db::rollBack();
  1422. return Result::error($e->getMessage());
  1423. }
  1424. return Result::success($result);
  1425. }
  1426. public function updateComponent(array $data): array
  1427. {
  1428. unset($data['user_id']);
  1429. $id = $data['id'];
  1430. unset($data['id']);
  1431. $sector = Sector::where('sector_id',$data['sector_id'])->first();
  1432. if(empty($sector)){
  1433. return Result::error('通栏不存在!');
  1434. }
  1435. $component_id = Component::where('id','!=',$id)->where('component_type',$data['component_type'])->first();
  1436. if(!empty($component_id) || $component_id != null){
  1437. return Result::error('组件编号已存在!');
  1438. }
  1439. // return Result::success($component_id);
  1440. $component_place = SectorPlace::where('sector_type',$sector['place_type'])
  1441. ->where('type_id',2)->where('sort_id',$data['sort_id'])->first();
  1442. // return Result::success($component_place);
  1443. // 组件分类:1:资讯-头条组件;2:资讯-轮播组件;3:资讯-推荐类组件;4:资讯-热点类组件;5:栏目资讯类;
  1444. // 6:广告类组件;7:列表类组件;8:详情类组件;9:底部导航类组件;10:最新资讯类组件;11:导航栏类;
  1445. if(is_array($data['page_type']) && !empty($data['page_type'])){
  1446. $page_type = array_values(array_map('intval',$data['page_type']));
  1447. $data['page_type'] = json_encode($page_type,true);
  1448. }
  1449. $data['type_id'] = intval($data['type_id']);
  1450. $add_arr = [
  1451. 'template_id' => intval($data['template_id']),
  1452. 'sector_id' => intval($data['sector_id']),
  1453. 'page_type' => $data['page_type'],
  1454. 'component_type' => intval($data['component_type']),
  1455. 'component_name' => $data['component_name'],
  1456. 'component_img' => $data['component_img'],
  1457. 'place_type' => $component_place['component_type'],
  1458. 'size_id' => $component_place['size_id'],
  1459. 'type_id' => intval($data['type_id']),
  1460. 'sort_id' => intval($data['sort_id']),
  1461. ];
  1462. switch($data['type_id']){
  1463. case 1: //1:资讯-头条组件;
  1464. case 2: //2:资讯-轮播组件;
  1465. case 3: //3:资讯-推荐类组件;
  1466. case 4: //4:资讯-热点类组件;
  1467. case 10: //10:最新资讯类;
  1468. $add_arr['level'] = intval($data['level']);
  1469. $add_arr['img_num'] = intval($data['img_num']);
  1470. $add_arr['text_num'] = intval($data['text_num']);
  1471. $add_arr['child_imgnum'] = intval($data['img_num'] ?? null);
  1472. $add_arr['child_textnum'] = intval($data['text_num'] ?? null);
  1473. $component_data['componentData'] = [
  1474. 'category_id' => '',
  1475. 'level' => $add_arr['level'] ?? '',
  1476. 'imgSize' => $add_arr['img_num'] ?? '',
  1477. 'textSize' => $add_arr['text_num'] ?? '',
  1478. 'child' => [
  1479. 'id' => '',
  1480. 'imgSize' => $add_arr['child_imgnum'] ?? '',
  1481. 'textSize' => $add_arr['child_textnum'] ?? '',
  1482. ]
  1483. ];
  1484. break;
  1485. case 5: //5:栏目资讯类组件;
  1486. $add_arr['img_num'] = intval($data['img_num']);
  1487. $add_arr['text_num'] = intval($data['text_num']);
  1488. $add_arr['child_imgnum'] = intval($data['child_imgnum'] ?? null);
  1489. $add_arr['child_textnum'] = intval($data['child_textnum'] ?? null);
  1490. $component_data['componentData'] = [
  1491. 'category_id' => '',
  1492. 'level' => intval($data['level'] ?? ''),
  1493. 'imgSize' => $add_arr['img_num'] ?? '',
  1494. 'textSize' => $add_arr['text_num'] ?? '',
  1495. 'child' => [
  1496. 'id' => '',
  1497. 'imgSize' => $add_arr['child_imgnum'] ?? '',
  1498. 'textSize' => $add_arr['child_textnum'] ?? '',
  1499. ]
  1500. ];
  1501. break;
  1502. case 7: //7:列表类组件;
  1503. $add_arr['pageSize'] = intval($data['pageSize']);
  1504. $component_data['componentData'] = [
  1505. 'category_id' => '',
  1506. 'pageType' => [
  1507. 'page' => 1,
  1508. 'pageSize' => $add_arr['pageSize'] ?? '',
  1509. ]
  1510. ];
  1511. break;
  1512. case 8: //8:详情类组件;
  1513. $component_data['componentData'] = [
  1514. 'article_id' => '',
  1515. ];
  1516. break;
  1517. case 9: //9:底部导航类组件;
  1518. $component_data['componentData'] = [
  1519. 'fcat_id' => '',
  1520. ];
  1521. break;
  1522. case 11: //11:导航栏类;
  1523. $add_arr['cate_place'] = 0;
  1524. $add_arr['pageSize'] = intval($data['pageSize']);
  1525. $component_data['componentData'] = [
  1526. 'pid' => '',
  1527. 'placeid' => 1,
  1528. 'num' => $add_arr['pageSize'],
  1529. ];
  1530. break;
  1531. default:
  1532. break;
  1533. }
  1534. $component_head_code = [
  1535. 'component_type' => $add_arr['component_type'],
  1536. 'type_id' => $add_arr['type_id'],
  1537. 'component_name' => $add_arr['component_name'],
  1538. 'sort_id' => $add_arr['sort_id'],
  1539. ];
  1540. if(isset($data['listType']) && !empty($data['listType']) && is_array($data['listType'])){
  1541. $list_type['listType'] = $data['listType'];
  1542. $add_arr['component_column'] = json_encode($list_type,true);
  1543. }
  1544. if(isset($component_head_code) && !empty($component_head_code) && is_array($component_head_code)){
  1545. $add_arr['component_code'] = json_encode($component_head_code,true);
  1546. }
  1547. if(isset($component_data) && !empty($component_data) && is_array($component_data)){
  1548. $add_arr['component_data'] = json_encode($component_data,true);
  1549. }
  1550. // return Result::success($add_arr);
  1551. $result = Component::where('id',$id)->update($add_arr);
  1552. if ($result) {
  1553. return Result::success($result);
  1554. } else {
  1555. return Result::error('添加失败');
  1556. }
  1557. }
  1558. public function getWebsiteTemplateInfo(array $data)
  1559. {
  1560. $where = [];
  1561. if (isset($data['id'])) {
  1562. $where[] = ['id', '=', $data['id']];
  1563. }
  1564. $result = WebsiteTemplateInfo::where($where)
  1565. ->leftJoin('website', 'website_template_info.website_id', '=', 'website.id')
  1566. ->leftJoin('website_template', 'website_template_info.template_id', '=', 'website_template.id')
  1567. ->select('website_template_info.*', 'website.website_name')
  1568. ->get();
  1569. if ($result) {
  1570. return Result::success($result);
  1571. } else {
  1572. return Result::error('暂无数据');
  1573. }
  1574. }
  1575. /**
  1576. * 自助建站----1.获取页面类型回显
  1577. * @param array $data
  1578. * @return array
  1579. */
  1580. public function getWebPageType(array $data): array
  1581. {
  1582. // 1:图片 2:文字 3:底部
  1583. $friend_link = Link::where('website_id', $data['website_id'])->where('type', 3)->get();
  1584. if (empty($friend_link->toArray())) {
  1585. return Result::error('暂无友情链接数据!');
  1586. }
  1587. $footer_category = FooterCategory::where('website_id', $data['website_id'])->get();
  1588. if (empty($footer_category->toArray())) {
  1589. return Result::error('暂无底部导航数据!');
  1590. }
  1591. $page_type = WebsiteTemplateInfo::where('website_id', $data['website_id'])->value('page_type');
  1592. if (empty($page_type)) {
  1593. $page_type = [];
  1594. } else {
  1595. $page_type = json_decode($page_type, true);
  1596. }
  1597. // '网站页面类型(存数组)1:首页 2:分类页 3:列表页 4:详情页 5:搜索页 6:特殊列表 7:特殊详情页
  1598. if (isset($page_type) && in_array(5, $page_type)) {
  1599. $page_type = 1;
  1600. } else {
  1601. $page_type = 0;
  1602. }
  1603. $result = [
  1604. 'friend_link' => $friend_link,
  1605. 'footer_category' => $footer_category,
  1606. 'page_type' => $page_type,
  1607. ];
  1608. return Result::success($result);
  1609. }
  1610. /**
  1611. * 自助建站----2.添加页面类型
  1612. * @param array $data
  1613. * @return array
  1614. */
  1615. public function addWebPageType(array $data): array
  1616. {
  1617. // return Result::success($data);
  1618. // 1:首页 2:频道页 3:列表页 4:详情页 5:搜索页 6:特殊列表 7:特殊详情页'
  1619. if ($data['is_search'] == 1) {
  1620. $page_type = json_encode([1, 2, 3, 4, 5, 6, 7]);
  1621. } else {
  1622. $page_type = json_encode([1, 2, 3, 4, 6, 7]);
  1623. }
  1624. // 0:未构建;1:已填写基础信息;2:已选择模板; action_id
  1625. $website_template_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1626. // return Result::success($website_template_info);
  1627. // return Result::success(empty($website_template_info));
  1628. if (empty($website_template_info)) {
  1629. $result = WebsiteTemplateInfo::insertGetId(['website_id' => $data['website_id'], 'page_type' => $page_type, 'user_id' => $data['user_id']]);
  1630. } else {
  1631. if ($website_template_info['status'] == 2) {
  1632. return Result::error('网站已应用,不可再次修改!');
  1633. }
  1634. $result = WebsiteTemplateInfo::where('website_id', $data['website_id'])->update(['page_type' => $page_type], ['action_id' => 1, 'user_id' => $data['user_id']]);
  1635. }
  1636. if ($result) {
  1637. return Result::success($result);
  1638. } else {
  1639. return Result::error('添加失败');
  1640. }
  1641. }
  1642. /**
  1643. * 自助建站---流程---3.选择皮肤列表
  1644. * @param array $data
  1645. * @return array
  1646. */
  1647. public function getWebsiteTemplateList(array $data): array
  1648. {
  1649. $where = [];
  1650. if (isset($data['template_class_id']) && $data['template_class_id']) {
  1651. $where[] = ['template_class_id', '=', $data['template_class_id']];
  1652. }
  1653. // 0:未构建 1:未应用 2:已应用 status
  1654. $templste_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1655. if (empty($templste_info)) {
  1656. return Result::error('请先填写网站基础信息!');
  1657. }
  1658. if ($templste_info['status'] == 2) {
  1659. return Result::error('网站已应用,不可再次修改!');
  1660. }
  1661. if (isset($data['keyword']) && !empty($data['keyword'])) {
  1662. if (is_array($data['keyword'])) {
  1663. // 拼接多个模糊查询条件
  1664. $query = Template::query();
  1665. foreach ($data['keyword'] as $kw) {
  1666. $escapedKw = addcslashes($kw, '%_'); // 转义通配符
  1667. $query->WhereRaw("JSON_EXTRACT(template_keyword, '$[*]') LIKE ?", ["%$escapedKw%"]);
  1668. }
  1669. } else {
  1670. $escapedKeyword = addcslashes($data['keyword'], '%_'); // 转义通配符
  1671. $where[] = [DB::raw("JSON_EXTRACT(template_keyword, '$[*]') LIKE ?"), "%$escapedKeyword%"];
  1672. }
  1673. } else {
  1674. $query = Template::where($where);
  1675. }
  1676. $template_id = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first(['template_id', 'page_type']);
  1677. // return Result::success($template_id);
  1678. if (!empty($template_id['page_type']) && $templste_info['page_type'] != '[]' && is_string($templste_info['page_type'])) {
  1679. $template_page = is_string($templste_info['page_type']) ? json_decode($templste_info['page_type'], true) : [];
  1680. } else {
  1681. $template_page = [];
  1682. }
  1683. if (!empty($template_id['template_id'])) {
  1684. $result['template_id'] = $templste_info['template_id'];
  1685. }
  1686. // 确保 $template_page 为有效数据,避免 SQL 注入风险,这里使用参数绑定
  1687. $template_page_str = json_encode($template_page);
  1688. $result['template'] = $query->where($where)
  1689. ->whereRaw("JSON_CONTAINS(template.page_type, ?)", [$template_page_str])
  1690. ->orderBy('updated_at', 'desc')
  1691. ->paginate($data['page_size'], ['*'], 'page', $data['page']);
  1692. if (empty($result['template'])) {
  1693. $result['template'] = Template::orderBy('updated_at', 'desc')
  1694. ->paginate($data['page_size'], ['*'], 'page', $data['page']);
  1695. }
  1696. $template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->value('template_id');
  1697. if ($template) {
  1698. $result['template_id'] = $template;
  1699. } else {
  1700. $result['template_id'] = 0;
  1701. }
  1702. return Result::success($result);
  1703. }
  1704. /**
  1705. * 自助建站---流程---4.选择皮肤
  1706. * @param array $data
  1707. * @return array
  1708. */
  1709. public function chooseWebsiteTemplate(array $data): array
  1710. {
  1711. $website_template_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1712. if (empty($website_template_info)) {
  1713. return Result::error('请先填写网站基础信息!');
  1714. }
  1715. // 0:未构建 1:未应用 2:已应用 status
  1716. if ($website_template_info['status'] == 2) {
  1717. return Result::error('网站已应用,不可再次修改!');
  1718. }
  1719. $template = Template::where('template_id', $data['template_id'])->first();
  1720. if (empty($template)) {
  1721. return Result::error('未查询到皮肤!');
  1722. }
  1723. // 0:未构建;1:已填写基础信息;2:已选择模板; action_id
  1724. $web_template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->update(['template_id' => $data['template_id'], 'action_id' => 2]);
  1725. if ($web_template) {
  1726. return Result::success($web_template);
  1727. } else {
  1728. return Result::error('选择皮肤失败!');
  1729. }
  1730. }
  1731. /**
  1732. * 自助建站---流程---5.保存模版
  1733. * @param array $data
  1734. * @return array
  1735. */
  1736. public function saveWebsiteTemplate(array $data): array
  1737. {
  1738. // return Result::success($data);
  1739. $website_template_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1740. if (empty($website_template_info) || empty($website_template_info->template_id) || empty($website_template_info->page_type)) {
  1741. return Result::error('请先填写网站基础信息!');
  1742. }
  1743. // 0:未构建 1:未应用 2:已应用 status
  1744. if ($website_template_info['status'] == 2) {
  1745. return Result::error('网站已应用,不可再次保存!');
  1746. }
  1747. if ($website_template_info['action_id'] != 2) {
  1748. return Result::error('请完成之前的步骤!');
  1749. }
  1750. $template_info = WebsiteTemplate::where('website_id', $data['website_id'])->first();
  1751. // var_dump($data['template_data']);
  1752. try {
  1753. Db::beginTransaction();
  1754. if (empty($template_info)) {
  1755. $template = WebsiteTemplate::insertGetId([
  1756. 'website_id' => $data['website_id'],
  1757. 'template_data' => $data['template_data'],
  1758. 'user_id' => $data['user_id'],
  1759. 'canvas_data' => $data['canvas_data'],
  1760. 'page_type' => $website_template_info['page_type'],
  1761. 'template_id' => $website_template_info['template_id'],
  1762. ]);
  1763. } else {
  1764. $template = WebsiteTemplate::where('website_id', $data['website_id'])->update(
  1765. [
  1766. 'template_data' => $data['template_data'],
  1767. 'user_id' => $data['user_id'],
  1768. 'canvas_data' => $data['canvas_data'],
  1769. 'page_type' => $website_template_info['page_type'],
  1770. 'template_id' => $website_template_info['template_id'],
  1771. ]
  1772. );
  1773. }
  1774. if (empty($template)) {
  1775. Db::rollback();
  1776. return Result::error('保存失败!');
  1777. } else {
  1778. $tempalte_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->update(['status' => 1]);
  1779. if (empty($tempalte_info)) {
  1780. Db::rollback();
  1781. return Result::error('保存失败!');
  1782. }
  1783. Db::commit();
  1784. }
  1785. } catch (\Exception $e) {
  1786. return Result::error('模版数据格式错误!');
  1787. }
  1788. return Result::success($template);
  1789. }
  1790. /**
  1791. * 自助建站---流程---6.返显保存的模版
  1792. * @param array $data
  1793. * @return array
  1794. */
  1795. public function getWebsiteTemplate(array $data): array
  1796. {
  1797. $web = Website::where('id', $data['website_id'])->first();
  1798. if (empty($web)) {
  1799. return Result::error('未查询到网站信息!');
  1800. }
  1801. $template = WebsiteTemplate::where('website_id', $data['website_id'])
  1802. ->first(['website_id', 'template_id', 'page_type', 'template_data', 'canvas_data']);
  1803. if (empty($template)) {
  1804. return Result::error('未查询到模版!');
  1805. }
  1806. return Result::success($template);
  1807. }
  1808. /**
  1809. * 通栏版式管理-获取通栏版式列表
  1810. */
  1811. public function getSectorPlaceList(array $data): array
  1812. {
  1813. $where['sector_place.type_id'] = $data['type_id'];
  1814. if(isset($data['component_num']) && !empty($data['component_num'])){
  1815. $where['sector_place.component_num'] = $data['component_num'];
  1816. }
  1817. if(isset($data['sector_type']) && !empty($data['sector_type'])){
  1818. $where['sector_place.sector_type'] = $data['sector_type'];
  1819. }
  1820. // if(isset($data['component_type']) && !empty($data['component_type'])){
  1821. // $where['sector_place.component_type'] = $data['component_type'];
  1822. // }
  1823. if(isset($data['name']) && !empty($data['name'])){
  1824. array_push($where,['sector_place.name', 'like', '%'.$data['name'].'%']);
  1825. }
  1826. if (isset($data['width']) && !empty($data['width']) && isset($data['height']) && !empty($data['height'])){
  1827. $size_id = Size::where('width','like','%' .$data['width'].'%')->where('height','like','%' .$data['height'].'%')->pluck('id');
  1828. } else if( isset($data['width']) && !empty($data['width'])){
  1829. $size_id = Size::where('width','like','%' .$data['width'].'%')->pluck('id');
  1830. }else if( isset($data['height']) && !empty($data['height'])){
  1831. $size_id = Size::where('height','like','%' .$data['height'].'%')->pluck('id');
  1832. }else{
  1833. $size_id = [];
  1834. }
  1835. $sector_place = SectorPlace::where($where)
  1836. ->when(!empty($size_id), function ($query) use ($size_id) {
  1837. $query->whereIn('sector_place.size_id', $size_id);
  1838. })
  1839. ->leftJoin('size','sector_place.size_id','=','size.id')
  1840. ->select('sector_place.*','size.width','size.height');
  1841. $result['count'] = $sector_place->count();
  1842. $result['row'] =$sector_place->paginate($data['page_size'], ['*'], 'page', $data['page']);
  1843. if(empty($result['count'])){
  1844. return Result::error("暂无相关版式数据!");
  1845. }
  1846. return Result::success($result);
  1847. }
  1848. /**
  1849. * 通栏版式管理-添加通栏版式
  1850. */
  1851. public function addSectorPlace(array $data): array
  1852. {
  1853. // 1:通栏;2:组件;
  1854. if($data['type_id'] == 1){
  1855. $sector_place = SectorPlace::where('sector_type', $data['type'])->first();
  1856. if(!empty($sector_place)){
  1857. return Result::error('通栏版式类别编号已存在!');
  1858. }
  1859. $data = [
  1860. 'name' => $data['name'],
  1861. 'component_num' => $data['component_num'],
  1862. 'size_id' => $data['size_id'],
  1863. 'sort_id' => 0,
  1864. 'sector_img' => $data['sector_img'],
  1865. 'sector_type' => $data['type'],
  1866. 'type_id' => $data['type_id'],
  1867. 'status' => 0,
  1868. ];
  1869. }else{
  1870. $sector_type = SectorPlace::where('sector_type', $data['sector_type'])->where('type_id',1)->first();
  1871. if(empty($sector_type)){
  1872. return Result::error('通栏版式类别不存在!');
  1873. }
  1874. // $com_query = SectorPlace::where('type_id',2);
  1875. // ->query(function($query) use ($data){
  1876. // $query->where('component_type', $data['type'])
  1877. // ->Subquery(function($subQuery) use ($data) {
  1878. // $subQuery->where('sector_type', $data['sector_type'])
  1879. // ->where('sort_id', $data['sort']);
  1880. // });
  1881. // }) //若是组件版式编号或者组件版式位置重复用此语句筛选
  1882. // ->where('sector_type',$data['sector_type']) //若是组件版式编号在同一个通栏版式下不重复就行,则启用此语句
  1883. $component = SectorPlace::where('type_id',2)->where('component_type',$data['type'])->first();
  1884. if(!empty($component)){
  1885. return Result::error('组件版式类别编号已存在!');
  1886. }
  1887. // $com_query = clone $com_query;
  1888. $com_num = SectorPlace::where('type_id',2)->where('sector_type',$data['sector_type'])->count('sort_id');
  1889. // return Result::success($com_num);
  1890. if($sector_type['component_num'] = $com_num){
  1891. $sector = SectorPlace::where('sector_type',$data['sector_type'])->where('type_id',1)->update(['status'=>1]);
  1892. if(empty($sector)){
  1893. return Result::error('通栏版式状态修改失败!');
  1894. }
  1895. }
  1896. $data = [
  1897. 'name' => $data['name'],
  1898. 'component_num' => 0,
  1899. 'size_id' => $data['size_id'],
  1900. 'sort_id' => $data['sort_id'],
  1901. 'sector_img' => $data['sector_img'],
  1902. 'component_type' => $data['type'],
  1903. 'type_id' => $data['type_id'],
  1904. 'sector_type' => $data['sector_type'],
  1905. ];
  1906. }
  1907. $result = SectorPlace::insertGetId($data);
  1908. if(empty($result)){
  1909. return Result::error('添加失败!');
  1910. }
  1911. return Result::success('添加成功!',$result);
  1912. }
  1913. /**
  1914. * 通栏版式管理-修改通栏版式
  1915. */
  1916. public function upSectorPlace(array $data): array
  1917. {
  1918. $id = $data['id'];
  1919. // 1:通栏;2:组件;
  1920. if($data['type_id'] == 1){
  1921. $sector_place = SectorPlace::where('id','!=',$id)->where('sector_type', $data['type'])->where('type_id',1)->first();
  1922. if(!empty($sector_place)){
  1923. return Result::error('通栏版式类别编号已存在!');
  1924. }
  1925. $data = [
  1926. 'name' => $data['name'],
  1927. 'component_num' => $data['component_num'],
  1928. 'size_id' => $data['size_id'],
  1929. 'sort_id' => 0,
  1930. 'sector_img' => $data['sector_img'],
  1931. 'sector_type' => $data['type'],
  1932. 'type_id' => $data['type_id'],
  1933. ];
  1934. }else{
  1935. $sector_type = SectorPlace::where('sector_type', $data['sector_type'])->where('type_id',1)->first();
  1936. if(empty($sector_type)){
  1937. return Result::error('通栏版式类别不存在!');
  1938. }
  1939. $component = SectorPlace::where('id','!=',$id)
  1940. ->where('component_type', $data['type'])
  1941. ->where('type_id',2)
  1942. ->first();
  1943. if(!empty($component)){
  1944. return Result::error('组件版式类别编号已存在!');
  1945. }
  1946. $data = [
  1947. 'name' => $data['name'],
  1948. 'component_num' => 0,
  1949. 'size_id' => $data['size_id'],
  1950. 'sort_id' => $data['sort_id'],
  1951. 'sector_img' => $data['sector_img'],
  1952. 'component_type' => $data['type'],
  1953. 'type_id' => $data['type_id'],
  1954. 'sector_type' => $data['sector_type'],
  1955. ];
  1956. }
  1957. $result = SectorPlace::where('id',$id)->update($data);
  1958. if(empty($result)){
  1959. return Result::error('修改失败!');
  1960. }
  1961. return Result::success('修改成功!',$result);
  1962. }
  1963. /**
  1964. * 通栏版式管理-删除通栏版式
  1965. */
  1966. public function delSectorPlace(array $data): array
  1967. {
  1968. $sector_place = SectorPlace::where('id',$data['id'])->first();
  1969. if(empty($sector_place)){
  1970. return Result::error('通栏版式不存在!');
  1971. }
  1972. if($sector_place['type_id'] == 1){
  1973. $sector = Sector::where('place_type', $sector_place['sector_type'])->first();
  1974. $component = SectorPlace::where('sector_type', $sector_place['sector_type'])
  1975. ->where('type_id',2)
  1976. ->first();
  1977. if(!empty($sector) || !empty($component)){
  1978. return Result::error('通栏版式分类已应用,暂不可删除!');
  1979. }
  1980. }else{
  1981. $sector = Component::where('place_type', $sector_place['component_type'])->first();
  1982. if(!empty($sector)){
  1983. return Result::error('组件版式分类已应用,暂不可删除!');
  1984. }
  1985. $component_num = SectorPlace::where('sector_type', $sector_place['sector_type'])
  1986. ->where('type_id',1)->update(['status'=> 0]);
  1987. if(empty($component_num)){
  1988. return Result::error('通栏版式状态修改失败!');
  1989. }
  1990. }
  1991. $result = SectorPlace::where('id',$data['id'])->delete();
  1992. if(empty($result)){
  1993. return Result::error('删除失败!');
  1994. }
  1995. return Result::success('删除成功!');
  1996. }
  1997. /**
  1998. * 自助建站-通栏版式管理-获取通栏版式详情
  1999. */
  2000. public function getSectorPlaceInfo(array $data): array
  2001. {
  2002. $result = SectorPlace::where('sector_place.id',$data['id'])
  2003. ->leftJoin('size','sector_place.size_id','=','size.id')
  2004. ->select('sector_place.*','size.width','size.height')
  2005. ->first();
  2006. if(empty($result)){
  2007. return Result::error('通栏版式不存在!');
  2008. }
  2009. return Result::success($result);
  2010. }
  2011. /**
  2012. * 自助建站-通栏版式管理-获取通栏版式列表
  2013. */
  2014. public function getSectorPlaceSort(array $data): array
  2015. {
  2016. $sector = SectorPlace::where('sector_type',$data['sector_type'])->first();
  2017. if(empty($sector)){
  2018. return Result::error('通栏版式不存在!');
  2019. }
  2020. $component = SectorPlace::where('type_id',2)->where('sector_type',$sector['sector_type'])->pluck('sort_id')->toArray();
  2021. if(count($component) < $sector['component_num']){
  2022. // 根据 $sector['component_num'] 的值生成对应元素数量的数组
  2023. if (isset($sector['component_num']) && is_numeric($sector['component_num'])) {
  2024. $num = intval($sector['component_num']);
  2025. if ($num > 0 && $num <= 10) {
  2026. // 使用 range 函数生成一个从 1 到 $num 的连续整数数组,存储到 $sector_arr 中
  2027. $sector_arr = range(1, $num);
  2028. } else {
  2029. $sector_arr = [];
  2030. }
  2031. } else {
  2032. $sector_arr = [];
  2033. }
  2034. // 获取 $sector_arr 中不在 $component 里的元素
  2035. $sort = array_diff($sector_arr, $component);
  2036. // return Result::success($sort);
  2037. }
  2038. if(!empty($sort)){
  2039. $sort = array_values(array_map(function($value) {
  2040. return ['id' => $value];
  2041. }, $sort));
  2042. }else{
  2043. return Result::error("组件版式数量已达上限!");
  2044. }
  2045. return Result::success($sort);
  2046. }
  2047. /**
  2048. * 获取所有通栏版式
  2049. */
  2050. public function getAllSectorPlace(array $data): array
  2051. {
  2052. $where['type_id'] = $data['type_id'];
  2053. if($data['type_id'] == 1){
  2054. $where['status'] = 1;
  2055. }
  2056. if(isset($data['sector_type']) && !empty($data['sector_type'])){
  2057. $where['sector_type'] = $data['sector_type'];
  2058. }
  2059. if(isset($data['component_num']) && !empty($data['component_num'])){
  2060. $where['component_num'] = $data['component_num'];
  2061. }
  2062. $sector_place = SectorPlace::where($where)
  2063. ->leftJoin('size','sector_place.size_id','=','size.id')
  2064. ->select('sector_place.*','size.width','size.height')
  2065. ->get()->all();
  2066. if(empty($sector_place)){
  2067. return Result::error('通栏版式不存在!');
  2068. }
  2069. return Result::success($sector_place);
  2070. }
  2071. /**
  2072. * 组件管理-获取组件类型
  2073. */
  2074. public function getComponentType(array $data): array
  2075. {
  2076. $component_type = ComponentType::get()->all();
  2077. $article = Article::first()->toArray();
  2078. $category = WebsiteCategory::first()->toArray();
  2079. $footer_category = FooterCategory::first()->toArray();
  2080. $type_arr = [1,2,3,4,5,7,8,10];
  2081. // return Result::success(in_array(intval($value['id']),$type_arr));
  2082. foreach($component_type as $key => $value){
  2083. // 组件分类:1:资讯-头条组件;2:资讯-轮播组件;3:资讯-推荐类组件;4:资讯-热点类组件;5:栏目资讯类;
  2084. // 6:广告类组件;7:列表类组件;8:详情类组件;9:底部导航类组件;10:最新资讯类组件;11:导航栏类;
  2085. if(in_array(intval($value['id']),$type_arr)){
  2086. $component_type[$key]['all_code'] = array_keys($article);
  2087. }
  2088. if($value['id'] == 9){
  2089. $component_type[$key]['all_code'] = array_keys($footer_category);
  2090. }
  2091. if($value['id'] == 11){
  2092. $component_type[$key]['all_code'] = array_keys($category);
  2093. }
  2094. if($value['com_code']){
  2095. $component_type[$key]['com_code'] = json_decode($value['com_code'],true);
  2096. }
  2097. // $component_type[$key]['com_code'] = json_decode($value['com_code'],true);
  2098. }
  2099. if(empty($category)){
  2100. return Result::error('组件类型不存在!');
  2101. }
  2102. return Result::success($component_type);
  2103. }
  2104. /**
  2105. * 组件管理-获取所有组件
  2106. */
  2107. public function getAllComponent(array $data): array
  2108. {
  2109. $where = [];
  2110. if(isset($data['sector_id']) && !empty($data['sector_id'])){
  2111. $where['component.sector_id'] = $data['sector_id'];
  2112. }
  2113. if(isset($data['sort_id']) && !empty($data['sort_id'])){
  2114. $where['component.sort_id'] = $data['sort_id'];
  2115. }
  2116. $component = Component::where($where)->leftJoin('size','component.size_id','=','size.id')
  2117. ->select('component.*','size.width','size.height')
  2118. ->get()->all();
  2119. if(empty($component)){
  2120. return Result::error('组件不存在!');
  2121. }
  2122. return Result::success($component);
  2123. }
  2124. /**
  2125. * 自助建站-流程管理-获取所有通栏
  2126. */
  2127. public function getAllSector(array $data): array
  2128. {
  2129. $where = [];
  2130. if(isset($data['template_id']) && !empty($data['template_id'])){
  2131. $where['template_id'] = $data['template_id'];
  2132. }
  2133. if(isset($data['page_type']) && !empty($data['page_type'])){
  2134. array_push($where,['page_type', 'like', '%'.$data['page_type'].'%']);
  2135. }
  2136. $sector = Sector::where($where)->where('status','!=',0)
  2137. ->leftJoin('size','sector.size_id','=','size.id')
  2138. ->select('sector.*','size.width','size.height')
  2139. ->get()->all();
  2140. if(empty($sector)){
  2141. return Result::error('通栏不存在!');
  2142. }
  2143. return Result::success($sector);
  2144. }
  2145. }