PublicRpcService.php 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556
  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. #[RpcService(name: "PublicRpcService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  36. class PublicRpcService implements PublicRpcServiceInterface
  37. {
  38. #[Inject]
  39. protected Redis $redis;
  40. /**
  41. * @param array $data
  42. * @return array
  43. */
  44. public function getDistrictList(array $data): array
  45. {
  46. $where = [];
  47. if (isset($data['keyWord'])) {
  48. $where = [
  49. ['name', 'like', '%' . $data['keyWord'] . '%']
  50. ];
  51. }
  52. $result = [];
  53. if (isset($data['pageSize'])) {
  54. $rep = District::where($where)->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->orderBy("code", "asc")->get();
  55. $count = District::where($where)->count();
  56. $result = [
  57. 'rows' => $rep,
  58. 'count' => $count
  59. ];
  60. } else {
  61. $result = District::where($data)->orderBy("code", "asc")->get();
  62. }
  63. return $result ? Result::success($result) : Result::error("没有查到数据");
  64. }
  65. /**
  66. * @param array $data
  67. * @return array
  68. */
  69. public function getUserLevelList(array $data): array
  70. {
  71. $where = [];
  72. if (isset($data['keyWord'])) {
  73. $where = [
  74. ['name', 'like', '%' . $data['keyWord'] . '%'],
  75. ];
  76. }
  77. $result = [];
  78. if (isset($data['pageSize'])) {
  79. $rep = UserLevel::where($where)->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->orderBy("id", "asc")->get();
  80. $count = UserLevel::where($where)->count();
  81. $result = [
  82. 'rows' => $rep,
  83. 'count' => $count,
  84. ];
  85. } else {
  86. $result = UserLevel::orderBy("id", "asc")->get();
  87. }
  88. return $result ? Result::success($result) : Result::error("没有查到数据");
  89. }
  90. /**
  91. * 添加用户等级
  92. * @param array $data
  93. * @return array
  94. */
  95. public function addUserLevel(array $data): array
  96. {
  97. LevelUser::insertGetId($data);
  98. return Result::success([]);
  99. }
  100. /**
  101. * 更新等级
  102. * @param array $data
  103. * @return array
  104. */
  105. public function updateUserLevel(array $data): array
  106. {
  107. $result = LevelUser::where(['id' => $data['id']])->update($data);
  108. if ($result) {
  109. return Result::success($result);
  110. }
  111. return Result::error("更新失败");
  112. }
  113. /**
  114. * 删除等级
  115. * @param array $data
  116. * @return array
  117. */
  118. public function delUserLevel(array $data): array
  119. {
  120. $result = LevelUser::where(['id' => $data['id']])->delete();
  121. if ($result) {
  122. return Result::success($result);
  123. }
  124. return Result::error("删除失败");
  125. }
  126. /**
  127. * 查询投诉举报信息
  128. * @param array $data
  129. * @return array
  130. */
  131. public function getLetterOfComplaint(array $data = []): array
  132. {
  133. var_dump("====");
  134. $where = [];
  135. if (isset($data['user_id']) && !empty($data['user_id'])) {
  136. array_push($where, ['letter_of_complaint.user_id', '=', $data['user_id']]);
  137. }
  138. if (isset($data['nature']) && !empty($data['nature'])) {
  139. array_push($where, ['letter_of_complaint.nature', '=', $data['nature']]);
  140. }
  141. if (isset($data['nature_level0']) && !empty($data['nature_level0'])) {
  142. array_push($where, ['letter_of_complaint.nature_level0', '=', $data['nature_level0']]);
  143. }
  144. if (isset($data['status']) && !empty($data['status'])) {
  145. array_push($where, ['letter_of_complaint.status', '=', $data['status']]);
  146. }
  147. $result = [];
  148. if (isset($data['pageSize'])) {
  149. $rep = LetterOfComplaint::where($where)
  150. ->leftJoin("letter_type as type_a", "letter_of_complaint.nature", "type_a.id")
  151. ->leftJoin("letter_type as type_c", "letter_of_complaint.nature_level0", "type_c.id")
  152. ->leftJoin("letter_type as type_b", "letter_of_complaint.nature_level1", "type_b.id")
  153. ->leftJoin("letter_type as type_e", "letter_of_complaint.nature_level3", "type_e.id")
  154. ->leftJoin("letter_type as type_d", "letter_of_complaint.status", "type_d.id")
  155. ->select(
  156. "letter_of_complaint.*",
  157. "type_a.type_name as nature_name",
  158. "type_b.type_name as nature_name1",
  159. "type_c.type_name as nature_name0",
  160. "type_d.type_name as status_name",
  161. "type_e.type_name as nature_name3"
  162. )
  163. ->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->orderBy("letter_of_complaint.id", "desc")->get();
  164. $count = LetterOfComplaint::where($where)->count();
  165. if ($rep) {
  166. foreach ($rep as $val) {
  167. if ($val['judgment']) {
  168. $val['judgment'] = json_decode($val['judgment']);
  169. }
  170. if ($val['audio_and_video']) {
  171. $val['audio_and_video'] = json_decode($val['audio_and_video']);
  172. }
  173. if ($val['contract']) {
  174. $val['contract'] = json_decode($val['contract']);
  175. }
  176. if ($val['qualifications']) {
  177. $val['qualifications'] = json_decode($val['qualifications']);
  178. }
  179. }
  180. }
  181. $result = [
  182. 'rows' => $rep,
  183. 'count' => $count,
  184. ];
  185. } else {
  186. $result = LetterOfComplaint::where($where)
  187. ->leftJoin("letter_type as type_a", "letter_of_complaint.nature", "type_a.id")
  188. ->leftJoin("letter_type as type_c", "letter_of_complaint.nature_level0", "type_c.id")
  189. ->leftJoin("letter_type as type_b", "letter_of_complaint.nature_level1", "type_b.id")
  190. ->leftJoin("letter_type as type_e", "letter_of_complaint.nature_level3", "type_e.id")
  191. ->leftJoin("letter_type as type_d", "letter_of_complaint.status", "type_d.id")
  192. ->select(
  193. "letter_of_complaint.*",
  194. "type_a.type_name as nature_name",
  195. "type_b.type_name as nature_name1",
  196. "type_c.type_name as nature_name0",
  197. "type_d.type_name as status_name",
  198. "type_e.type_name as nature_name3"
  199. )
  200. ->orderBy("letter_of_complaint.id", "desc")->get();
  201. }
  202. return $result ? Result::success($result) : Result::error("没有查到数据");
  203. }
  204. /**
  205. * 添加投诉举报信息
  206. * @param array $data
  207. * @return array
  208. */
  209. public function addLetterOfComplaint(array $data): array
  210. {
  211. $data['judgment'] = $data['judgment'] ? json_encode($data['judgment']) : '';
  212. $data['audio_and_video'] = $data['audio_and_video'] ? json_encode($data['audio_and_video']) : '';
  213. $data['contract'] = $data['contract'] ? json_encode($data['contract']) : '';
  214. $data['qualifications'] = $data['qualifications'] ? json_encode($data['qualifications']) : '';
  215. unset($data['id']);
  216. $result = LetterOfComplaint::insertGetId($data);
  217. if (empty($result)) {
  218. return Result::error("创建失败", 0);
  219. } else {
  220. return Result::success(["id" => $result]);
  221. }
  222. }
  223. /**
  224. * 用户端更新投诉举报
  225. * @param array $data
  226. * @return array
  227. */
  228. public function userUpLetterOfComplaint(array $data): array
  229. {
  230. $data['judgment'] = $data['judgment'] ? json_encode($data['judgment']) : '';
  231. $data['audio_and_video'] = $data['audio_and_video'] ? json_encode($data['audio_and_video']) : '';
  232. $data['contract'] = $data['contract'] ? json_encode($data['contract']) : '';
  233. $data['qualifications'] = $data['qualifications'] ? json_encode($data['qualifications']) : '';
  234. $result = LetterOfComplaint::where(['id' => $data['id']])->update($data);
  235. if (empty($result)) {
  236. return Result::error("创建失败", 0);
  237. } else {
  238. return Result::success(["id" => $result]);
  239. }
  240. }
  241. /**
  242. * 管理后台更新投诉举报信息
  243. * @param array $data
  244. * @return array
  245. */
  246. public function upLetterOfComplaint(array $data): array
  247. {
  248. var_dump("admin:", $data);
  249. $where = [
  250. 'id' => $data['id'],
  251. ];
  252. $filtered_array = array_filter($data, function ($value) {
  253. return $value !== "" && $value !== null && $value !== false && !is_array($value) || !empty($value);
  254. });
  255. $filtered_array['judgment'] = isset($filtered_array['judgment']) ? json_encode($filtered_array['judgment']) : '';
  256. $filtered_array['audio_and_video'] = isset($filtered_array['audio_and_video']) ? json_encode($filtered_array['audio_and_video']) : '';
  257. $filtered_array['contract'] = isset($filtered_array['contract']) ? json_encode($filtered_array['contract']) : '';
  258. $filtered_array['qualifications'] = isset($filtered_array['qualifications']) ? json_encode($filtered_array['qualifications']) : '';
  259. unset($filtered_array['nature_name']);
  260. unset($filtered_array['type_name']);
  261. unset($filtered_array['nature_level_name']);
  262. unset($filtered_array['status_name']);
  263. unset($filtered_array['is_admin']);
  264. unset($filtered_array['type_level_name']);
  265. $result = LetterOfComplaint::where($where)->update($filtered_array);
  266. if ($result) {
  267. return Result::success($result);
  268. }
  269. return Result::error("更新失败", 0);
  270. }
  271. /**
  272. * 查询投诉举报记录
  273. * @param array $data
  274. * @return array
  275. */
  276. public function getLetterOfComplaintInfo(array $data): array
  277. {
  278. $where = [
  279. 'letter_of_complaint.id' => $data['id'],
  280. ];
  281. if (isset($data['user_id']) && !empty($data['user_id'])) {
  282. array_push($where, ['letter_of_complaint.user_id', '=', $data['user_id']]);
  283. }
  284. $result = LetterOfComplaint::where($where)
  285. ->leftJoin("letter_type as type_a", "letter_of_complaint.nature", "type_a.id")
  286. ->leftJoin("letter_type as type_c", "letter_of_complaint.nature_level0", "type_c.id")
  287. ->leftJoin("letter_type as type_b", "letter_of_complaint.nature_level1", "type_b.id")
  288. ->leftJoin("letter_type as type_e", "letter_of_complaint.nature_level3", "type_e.id")
  289. ->leftJoin("letter_type as type_d", "letter_of_complaint.status", "type_d.id")
  290. ->select(
  291. "letter_of_complaint.*",
  292. "type_a.type_name as nature_name",
  293. "type_b.type_name as nature_name1",
  294. "type_c.type_name as nature_name0",
  295. "type_d.type_name as status_name",
  296. "type_e.type_name as nature_name3"
  297. )
  298. ->first();
  299. return Result::success($result);
  300. }
  301. /**
  302. * 删除投诉举报信息
  303. * @param array $data
  304. * @return array
  305. */
  306. public function delLetterOfComplaint(array $data): array
  307. {
  308. $result = LetterOfComplaint::when($data, function ($query) use ($data) {
  309. if (isset($data['id']) && !empty($data['id'])) {
  310. $query->where(['id' => $data['id']]);
  311. }
  312. if (isset($data['user_id']) && !empty($data['user_id'])) {
  313. $query->where(['user_id' => $data['user_id']]);
  314. }
  315. })->delete();
  316. if (empty($result)) {
  317. return Result::error("删除失败", 0);
  318. } else {
  319. return Result::success();
  320. }
  321. }
  322. /**
  323. * 获取举报信息类型
  324. * @param array $data
  325. * @return array
  326. */
  327. public function getLetterType(array $data): array
  328. {
  329. $where = [];
  330. if (isset($data['type'])) {
  331. array_push($where, ['type', '=', $data['type']]);
  332. }
  333. if (isset($data['pid']) && $data['pid'] > 0) {
  334. array_push($where, ['pid', '=', $data['pid']]);
  335. }
  336. $result = LetterType::where($where)->orderBy('sort', 'asc')->get();
  337. return $result ? Result::success($result) : Result::error("没有查到数据");
  338. }
  339. /**
  340. * 更新举报类型
  341. * @param array $data
  342. * @return array
  343. */
  344. public function upLetterType(array $data): array
  345. {
  346. return [];
  347. }
  348. /**
  349. * 添加举报类型
  350. * @param array $data
  351. * @return array
  352. */
  353. public function addLetterType(array $data): array
  354. {
  355. $result = LetterType::insertGetId($data);
  356. if (empty($result)) {
  357. return Result::error("创建失败", 0);
  358. } else {
  359. return Result::success(["id" => $result]);
  360. }
  361. }
  362. /**
  363. * 删除举报类型
  364. * @param array $data
  365. * @return array
  366. */
  367. public function delLetterType(array $data): array
  368. {
  369. $result = LetterType::where('id', $data['id'])->delete();
  370. if (empty($result)) {
  371. return Result::error("删除失败", 0);
  372. } else {
  373. return Result::success();
  374. }
  375. }
  376. /**
  377. * 检测是否已经被接案
  378. * @param array $data
  379. * @return array
  380. */
  381. public function checkMeasure(array $data): array
  382. {
  383. $where = [
  384. 'id' => $data['id'],
  385. ];
  386. $letterOfComplaintInfo = LetterOfComplaint::where($where)->first();
  387. var_dump("查询数据:", $letterOfComplaintInfo['admin_id'], $data['user_id']);
  388. //操作人和当前登陆用户id 相等说明是当前人接收的案件
  389. if (($letterOfComplaintInfo['admin_id'] == $data['user_id']) || empty($letterOfComplaintInfo['admin_id'])) {
  390. return Result::success();
  391. } else {
  392. return Result::error("您不能处理其他人已经接过的案件", 0);
  393. }
  394. }
  395. /**
  396. * 后台获取职能部门
  397. * @param array $data
  398. * @return array
  399. */
  400. public function getZhinengbumenList(array $data): array
  401. {
  402. // 获取分页参数,默认每页 10 条记录
  403. $page = isset($data['page']) ? (int) $data['page'] : 1;
  404. $perPage = isset($data['pagesize']) ? (int) $data['pagesize'] : 10;
  405. // 查询数据并分页
  406. $query = Department::query();
  407. // 可以在这里添加更多的查询条件
  408. if (isset($data['search'])) {
  409. $query->where('name', 'like', '%' . $data['search'] . '%');
  410. }
  411. // 执行分页查询
  412. $result = $query->paginate($perPage, ['*'], 'page', $page);
  413. // 返回分页结果
  414. return Result::success([
  415. 'count' => $result->total(),
  416. 'current_page' => $result->currentPage(),
  417. 'last_page' => $result->lastPage(),
  418. 'pagesize' => $result->perPage(),
  419. 'rows' => $result->items(),
  420. ]);
  421. }
  422. /**
  423. * 添加获取职能部门
  424. * @param array $data
  425. * @return array
  426. */
  427. public function addZhinengbumen(array $data): array
  428. {
  429. $result = Department::insertGetId($data);
  430. if (empty($result)) {
  431. return Result::error("创建失败", 0);
  432. } else {
  433. return Result::success(["id" => $result]);
  434. }
  435. }
  436. public function delZhinengbumen(array $data): array
  437. {
  438. $result = Department::where('id', $data['id'])->delete();
  439. if (empty($result)) {
  440. return Result::error("删除失败", 0);
  441. } else {
  442. return Result::success();
  443. }
  444. }
  445. public function getZhinengbumen(array $data): array
  446. {
  447. $result = Department::where('id', $data['id'])
  448. ->orderBy('department', 'desc')
  449. ->first();
  450. return Result::success($result);
  451. }
  452. public function getPidZhinengbumen(array $data): array
  453. {
  454. if (empty($data['pid'])) {
  455. $data['pid'] = 0;
  456. }
  457. $result = Department::where('pid', $data['pid'])->get();
  458. return Result::success($result);
  459. }
  460. public function modZhinengbumen(array $data): array
  461. {
  462. $result = Department::where('id', $data['id'])->update($data);
  463. if (empty($result)) {
  464. return Result::error("修改失败", 0);
  465. } else {
  466. return Result::success();
  467. }
  468. }
  469. /**
  470. * 查询职能列表
  471. * @param array $data
  472. * @return array
  473. */
  474. public function getDepartment(array $data): array
  475. {
  476. $where = [];
  477. if (isset($data['pid'])) {
  478. $where = [
  479. 'pid' => $data['pid'] ?? 0
  480. ];
  481. }
  482. $result = Department::when(!empty($where), function ($query) use ($where) {
  483. $query->where($where);
  484. })->orderBy("sort", "desc")->get();
  485. if (empty($result)) {
  486. return Result::error("查询失败", 0);
  487. } else {
  488. return Result::success($result);
  489. }
  490. }
  491. /**
  492. * 获取所有的buckets
  493. * @param array $data
  494. * @return array
  495. */
  496. public function getBuckets(array $data): array
  497. {
  498. $result = new MinioService();
  499. // 调用服务层的方法获取存储桶列表
  500. $bucketsResponse = $result->listBuckets();
  501. // 直接返回服务层生成的响应
  502. return Result::success($bucketsResponse['data']);
  503. }
  504. /**
  505. * 上传文件
  506. * @param array $data
  507. * @return array
  508. */
  509. public function uploadFile(array $data): array
  510. {
  511. $result = new MinioService();
  512. $rep = $result->uploadFile($data);
  513. if ($rep['code'] == 200) {
  514. return Result::success($rep['data']);
  515. } else {
  516. return Result::error("上传失败!");
  517. }
  518. }
  519. /**
  520. * 黑名单管理
  521. * @param array $data
  522. * @return array
  523. */
  524. public function getBlackWordList(array $data): array
  525. {
  526. $result = BlackWord::when($data, function ($query) use ($data) {
  527. if (isset($data['name']) && $data['name']) {
  528. $query->where('black_word.name', 'like', '%' . $data['name'] . '%');
  529. }
  530. })->orderBy('black_word.id', 'desc')
  531. ->paginate(
  532. intval($data['pageSize']),
  533. [
  534. 'black_word.*',
  535. ],
  536. 'page',
  537. intval($data['page'])
  538. );
  539. $count = $result->total();
  540. $returnData = [
  541. 'rows' => $result->items(),
  542. 'count' => $count
  543. ];
  544. return Result::success($returnData);
  545. }
  546. /**
  547. * 添加黑名单
  548. * @param array $data
  549. * @return array
  550. */
  551. public function addBlackWord(array $data): array
  552. {
  553. Db::beginTransaction();
  554. try {
  555. $info = BlackWord::where(['name' => $data['name']])->first();
  556. if ($info) {
  557. Db::rollBack();
  558. return Result::error("该黑名单已存在", 0);
  559. }
  560. $data['type'] = 10;
  561. $result = BlackWord::insertGetId($data);
  562. $redisKey = 'black_word';
  563. $this->redis->sAdd($redisKey, $data['name']);
  564. Db::commit();
  565. return Result::success(["id" => $result]);
  566. } catch (\Exception $e) {
  567. Db::rollBack();
  568. return Result::error("创建失败" . $e->getMessage(), 0);
  569. }
  570. }
  571. /**
  572. * 删除黑名单
  573. * @param array $data
  574. * @return array
  575. */
  576. public function delBlackWord(array $data): array
  577. {
  578. Db::beginTransaction();
  579. try {
  580. BlackWord::where(['name' => $data['name']])->delete();
  581. $redisKey = 'black_word';
  582. $this->redis->sRem($redisKey, $data['name']);
  583. Db::commit();
  584. return Result::success([]);
  585. } catch (\Exception $e) {
  586. Db::rollBack();
  587. return Result::error("删除失败" . $e->getMessage(), 0);
  588. }
  589. }
  590. /**
  591. * 修改违禁词
  592. * @param array $data
  593. * @return array
  594. */
  595. public function upBlackWord(array $data): array
  596. {
  597. Db::beginTransaction();
  598. try {
  599. $checkInfo = BlackWord::where(['name' => $data['name']])->first();
  600. if ($checkInfo) {
  601. Db::rollBack();
  602. return Result::error("该违禁词已经存在", 0);
  603. }
  604. $blackWordInfo = BlackWord::where(['id' => $data['id']])->first();
  605. if ($blackWordInfo) {
  606. //先删除redis
  607. $blackWordInfo = $blackWordInfo->toArray();
  608. $redisKey = 'black_word';
  609. $this->redis->sRem($redisKey, $blackWordInfo['name']);
  610. $this->redis->sAdd($redisKey, $data['name']);
  611. BlackWord::where(['id' => $data['id']])->update(['name' => $data['name']]);
  612. Db::commit();
  613. return Result::success([]);
  614. } else {
  615. Db::rollBack();
  616. return Result::error("系统错误", 0);
  617. }
  618. } catch (\Exception $e) {
  619. Db::rollBack();
  620. return Result::error("修改失败" . $e->getMessage(), 0);
  621. }
  622. }
  623. /**
  624. * 获取风格
  625. * @return void
  626. */
  627. public function getTemplateClassList(array $data): array
  628. {
  629. $where = [];
  630. if (isset($data['name']) && $data['name']) {
  631. array_push($where, ['template_class.name', 'like', '%' . $data['name'] . '%']);
  632. }
  633. if (isset($data['keyword']) && $data['keyword']) {
  634. array_push($where, ['template_class.keyword', 'like', '%' . $data['keyword'] . '%']);
  635. }
  636. $template = TemplateClass::when($where, function ($query) use ($where) {
  637. $query->where($where);
  638. });
  639. $count = $template->count();
  640. // $countQuery = clone $template;
  641. $row = $template
  642. ->leftJoin('template', function ($join) {
  643. $join->on('template_class.class_id', '=', 'template.template_class_id');
  644. })
  645. ->select('template_class.*', DB::raw('COUNT(template.template_class_id) as template_count'))
  646. ->groupBy('template_class.id')
  647. ->orderBy('template_class.id', 'desc')
  648. ->offset(($data['page'] - 1) * $data['pageSize'])
  649. ->limit($data['pageSize'])
  650. ->get();
  651. $result = [
  652. 'rows' => $row,
  653. 'count' => $count,
  654. ];
  655. if ($row->isEmpty()) {
  656. return Result::error("暂无风格", 0);
  657. } else {
  658. return Result::success($result);
  659. }
  660. }
  661. /**
  662. * 添加风格
  663. * @param
  664. * @return void
  665. */
  666. public function addTemplateClass(array $data): array
  667. {
  668. $data['keyword'] = json_encode($data['keyword']);
  669. $template_class = TemplateClass::where('name', $data['name'])
  670. ->orWhere('class_id', $data['class_id'])
  671. ->first();
  672. if ($template_class) {
  673. return Result::error("风格名称或者风格编号已存在,不可添加!", 0);
  674. }
  675. $result = TemplateClass::insertGetId($data);
  676. if (empty($result)) {
  677. return Result::error("创建风格失败", 0);
  678. } else {
  679. return Result::success(["id" => $result]);
  680. }
  681. }
  682. /**
  683. * 更新风格
  684. * @param array $data
  685. * @return array
  686. */
  687. public function upTemplateClass(array $data): array
  688. {
  689. $where = [
  690. 'id' => $data['id'],
  691. ];
  692. $template_class = TemplateClass::where($where)->first();
  693. if (empty($template_class)) {
  694. return Result::error("未查询到风格", 0);
  695. }
  696. if ($template_class->type == 1) {
  697. return Result::error("默认风格不能修改", 0);
  698. }
  699. $template = TemplateClass::where('id', '!=', $data['id'])
  700. // ->where(['name' => $data['name']])
  701. // ->orWhere(['class_id' => $data['class_id']])
  702. ->where(function ($query) use ($data) {
  703. $query->where('name', $data['name'])
  704. ->orWhere('class_id', $data['class_id']);
  705. })
  706. ->first();
  707. if ($template) {
  708. return Result::error("风格名称或者风格编号已存在,不可编辑!", 0);
  709. }
  710. $updateData = [
  711. 'name' => $data['name'],
  712. 'keyword' => json_encode($data['keyword']),
  713. 'class_id' => $data['class_id'],
  714. ];
  715. $result = TemplateClass::where($where)->update($updateData);
  716. if (empty($result)) {
  717. return Result::error("更新失败", 0);
  718. } else {
  719. return Result::success($result);
  720. }
  721. }
  722. /**
  723. * 删除风格
  724. * @param array $data
  725. * @return array
  726. */
  727. public function delTemplateClass(array $data): array
  728. {
  729. $where = [
  730. 'id' => $data['id'],
  731. ];
  732. $template_class = TemplateClass::where($where)->first();
  733. if (empty($template_class)) {
  734. return Result::error("未查询到风格", 0);
  735. }
  736. if ($template_class->type == 1) {
  737. return Result::error("默认风格不能删除", 0);
  738. }
  739. $template = Template::where('template_class_id', $template_class['class_id'])->get();
  740. if (!empty($template->all())) {
  741. try {
  742. // 默认风格 1
  743. Db::beginTransaction();
  744. $template = Template::where('template_class_id', $template['class_id'])->update(['template_class_id' => 1]);
  745. if (empty($template)) {
  746. Db::rollBack();
  747. return Result::error("删除失败", 0);
  748. }
  749. $result = TemplateClass::where($where)->delete();
  750. } catch (\Exception $e) {
  751. return Result::error("删除失败" . $e->getMessage(), 0);
  752. }
  753. } else {
  754. $result = TemplateClass::where('id', $data['id'])->delete();
  755. }
  756. if (empty($result)) {
  757. return Result::error("删除失败", 0);
  758. }
  759. return Result::success($result);
  760. }
  761. /**
  762. * 根据风格名称-获取获取所有风格
  763. * @param array $data
  764. * @return array
  765. */
  766. public function getTemplateClass(array $data): array
  767. {
  768. $where = [];
  769. if (!empty($data['template_name'])) {
  770. $where['name'] = $data['template_name'];
  771. }
  772. $result = TemplateClass::where($where)->get();
  773. if (empty($result)) {
  774. return Result::error("暂无风格", 0);
  775. }
  776. return Result::success($result);
  777. }
  778. /**
  779. * 获取皮肤列表
  780. * @param array $data
  781. * @return array
  782. */
  783. public function getTemplateList(array $data): array
  784. {
  785. $where = [];
  786. if (!empty($data['template_class_id'])) {
  787. $where['template_class_id'] = $data['template_class_id'];
  788. }
  789. if (!empty($data['template_name'])) {
  790. array_push($where, ['template_name', 'like', '%' . $data['template_name'] . '%']);
  791. }
  792. if (!empty($data['template_keyword'])) {
  793. array_push($where, ['template_keyword', 'like', '%' . $data['template_keyword'] . '%']);
  794. }
  795. $result = Template::where($where)
  796. ->leftJoin('template_class', 'template.template_class_id', 'template_class.class_id')
  797. ->select('template.*', 'template_class.name as template_class_name')
  798. ->orderBy('template.id', 'desc')
  799. ->paginate($data['page_size'], ['*'], 'mypage_name', $data['page']);
  800. if (empty($result)) {
  801. return Result::error("暂无皮肤", 0);
  802. }
  803. return Result::success($result);
  804. }
  805. public function getTemplateInfo(array $data): array
  806. {
  807. $result = Template::where('template.id', $data['id'])
  808. ->leftJoin('template_class', 'template.template_class_id', 'template_class.class_id')
  809. ->select('template.*', 'template_class.name as template_class_name')
  810. ->first();
  811. if (empty($result)) {
  812. return Result::error("暂无皮肤", 0);
  813. }
  814. return Result::success($result);
  815. }
  816. /**
  817. * 添加皮肤
  818. * @param array $data
  819. * @return array
  820. */
  821. public function addTemplate(array $data): array
  822. {
  823. var_dump($data);
  824. unset($data['user_id']);
  825. $template = Template::where('template_name', $data['template_name'])
  826. ->orWhere('template_id', $data['template_id'])
  827. ->first();
  828. if ($template) {
  829. return Result::error("皮肤名称或者皮肤编号已存在,不可添加!", 0);
  830. }
  831. $data['template_keyword'] = json_encode($data['template_keyword']) ?? '';
  832. $page_type = json_decode($data['page_type'], true);
  833. $data['page_type'] = json_encode(array_values(array_unique($page_type)));
  834. $result = Template::insertGetId($data);
  835. if (empty($result)) {
  836. return Result::error("创建失败", 0);
  837. } else {
  838. return Result::success($result);
  839. }
  840. }
  841. /**
  842. * 删除皮肤
  843. * @param array $data
  844. * @return array
  845. */
  846. public function delTemplate(array $data): array
  847. {
  848. $rector = Sector::where('template_id', $data['id'])->first();
  849. if (!empty($rector)) {
  850. return Result::error("此皮肤已被绑定通栏,不可删除", 0);
  851. }
  852. $template = WebsiteTemplateInfo::where('template_id', $data['id'])->first();
  853. if (empty($template)) {
  854. return Result::error("此皮肤已被绑定网站,不可删除", 0);
  855. }
  856. if ($template->type == 1) {
  857. return Result::error("默认皮肤不能删除", 0);
  858. }
  859. $result = Template::where('id', $data['id'])->delete();
  860. var_dump($result, '-------------------delete');
  861. if ($result) {
  862. return Result::success($result);
  863. } else {
  864. return Result::error("删除失败", 0);
  865. }
  866. }
  867. /**
  868. * 更新皮肤
  869. * @param array $data
  870. * @return array
  871. */
  872. public function updateTemplate(array $data): array
  873. {
  874. unset($data['user_id']);
  875. $template = Template::where('id', $data['id'])
  876. ->first();
  877. if (empty($template)) {
  878. return Result::error("此皮肤不存在!", 0);
  879. }
  880. $template = Template::where('id', '!=', $data['id'])
  881. ->where(function ($query) use ($data) {
  882. $query->where('template_name', $data['template_name'])
  883. ->orWhere('template_id', $data['template_id']);
  884. })
  885. ->first();
  886. if ($template) {
  887. return Result::error("皮肤名称或者皮肤编号已存在,不可编辑!", 0);
  888. }
  889. $data['template_keyword'] = json_encode($data['template_keyword']) ?? '';
  890. $page_type = json_decode($data['page_type'], true);
  891. $data['page_type'] = json_encode(array_values(array_unique($page_type)));
  892. $result = Template::where('id', $data['id'])->update($data);
  893. var_dump($result, '-------------------update');
  894. if (!$result) {
  895. return Result::error("更新失败", 0);
  896. } else {
  897. return Result::success('更新成功');
  898. }
  899. }
  900. /**
  901. * 获取所有尺寸
  902. * @param array $data
  903. * @return array
  904. */
  905. public function getAllSize(array $data): array
  906. {
  907. if(isset($data['id']) && !empty($data['id'])){
  908. $result = Size::where('id', $data['id'])->get()->all();
  909. }else{
  910. $result = Size::get()->all();
  911. }
  912. if(empty($result)){
  913. return Result::error("暂无尺寸", 0);
  914. }
  915. return Result::success($result);
  916. }
  917. /**
  918. * 根据皮肤名称-获取所有皮肤
  919. * @param array $data
  920. * @return array
  921. */
  922. public function getAllTemplate(array $data): array
  923. {
  924. $where = [];
  925. if (isset($data['template_class_id']) && !empty($data['template_class_id'])) {
  926. $where[] = ['template_class_id', $data['template_class_id']];
  927. }
  928. if (isset($data['template_name']) && !empty($data['template_name'])) {
  929. $where[] = ['template_name', 'like', '%' . $data['template_name'] . '%'];
  930. }
  931. $result = Template::where($where)->get()->all();
  932. if (empty($result)) {
  933. return Result::error("暂无皮肤", 0);
  934. }
  935. return Result::success($result);
  936. }
  937. /**
  938. * 获取通栏列表
  939. * @param array $data
  940. * @return array
  941. */
  942. public function getSectorList(array $data): array
  943. {
  944. $where = [];
  945. if (isset($data['template_class_id']) && !empty($data['template_class_id'])) {
  946. $where['template.template_class_id'] = $data['template_class_id'];
  947. }
  948. if (isset($data['template_id']) && !empty($data['template_id'])) {
  949. array_push($where, ['template.template_id', $data['template_id']]);
  950. }
  951. if (isset($data['sector_name']) && !empty($data['sector_name'])) {
  952. array_push($where, ['sector.sector_name', 'like', '%' . $data['sector_name'] . '%']);
  953. }
  954. if (isset($data['width']) && !empty($data['width']) && isset($data['height']) && !empty($data['height'])) {
  955. $size_id = Size::where('width', 'like', '%' . $data['width'] . '%')->where('height', 'like', '%' . $data['height'] . '%')->pluck('id');
  956. } else if (isset($data['width']) && !empty($data['width'])) {
  957. $size_id = Size::where('width', 'like', '%' . $data['width'] . '%')->pluck('id');
  958. } else if (isset($data['height']) && !empty($data['height'])) {
  959. $size_id = Size::where('height', 'like', '%' . $data['height'] . '%')->pluck('id');
  960. } else {
  961. $size_id = [];
  962. }
  963. $result = Sector::when(!empty($size_id), function ($query) use ($size_id) {
  964. $query->whereIn('size_id', $size_id);
  965. })
  966. ->leftJoin('size', 'size.id', '=', 'sector.size_id')
  967. ->leftJoin('template', 'template.template_id', '=', 'sector.template_id')
  968. ->leftJoin('template_class', 'template_class.class_id', '=', 'template.template_class_id')
  969. ->where($where)
  970. ->select('sector.*', 'size.width','size.height','template_class.name as class_name','template.template_name')
  971. ->orderBy('sector.id', 'desc')
  972. ->paginate($data['page_size'], ['*'], 'page', $data['page']);
  973. if(empty($result)){
  974. return Result::error('暂无通栏!');
  975. }
  976. return Result::success($result);
  977. }
  978. /**
  979. * 获取通栏详情
  980. * @param array $data
  981. * @return array
  982. */
  983. public function getSectorInfo(array $data): array
  984. {
  985. $where = [];
  986. $where[] = ['sector.id', '=', $data['id']];
  987. $result = Sector::where($where)
  988. ->leftJoin('size', 'size.id', '=', 'sector.size_id')
  989. ->select('sector.*', 'size.width', 'size.height')
  990. ->orderBy('sector.id', 'desc')
  991. ->first();
  992. if (empty($result)) {
  993. return Result::error('此通栏不存在!');
  994. }
  995. return Result::success($result);
  996. }
  997. /**
  998. * 获取经纬度信息
  999. * @return void
  1000. */
  1001. public function getIpInfo(array $data): array
  1002. {
  1003. if (empty($data['ip'])) {
  1004. // 在RPC服务中,依赖$_SERVER是不可靠的,强制要求调用方传入IP
  1005. return Result::error('IP地址不能为空!');
  1006. }
  1007. $client_ip = $data['ip'];
  1008. try {
  1009. // 直接从容器获取 Guzzle 客户端实例
  1010. /** @var \GuzzleHttp\Client $client */
  1011. $client = \Hyperf\Context\ApplicationContext::getContainer()->get(Client::class);
  1012. // 使用新的 GeoJS API 地址
  1013. $api_url = "https://get.geojs.io/v1/ip/geo/{$client_ip}.json";
  1014. // 将 timeout 作为请求选项传递
  1015. $response = $client->get($api_url, ['timeout' => 5]);
  1016. if ($response->getStatusCode() !== 200) {
  1017. return Result::error('IP查询接口请求失败,状态码:' . $response->getStatusCode());
  1018. }
  1019. $ip_info = json_decode($response->getBody()->getContents(), true);
  1020. // GeoJS 的成功响应不包含 'status' 字段,直接检查关键数据是否存在
  1021. if ($ip_info && isset($ip_info['latitude']) && isset($ip_info['longitude'])) {
  1022. // GeoJS 返回的经纬度是字符串,转换为浮点数
  1023. $ip_info['latitude'] = floatval($ip_info['latitude']);
  1024. $ip_info['longitude'] = floatval($ip_info['longitude']);
  1025. return Result::success($ip_info);
  1026. }
  1027. // 如果返回的数据不符合预期,提供一个通用错误
  1028. $errorMessage = $ip_info['message'] ?? '获取IP地理位置信息失败';
  1029. return Result::error($errorMessage);
  1030. } catch (GuzzleException $e) {
  1031. // 这个异常会给出详细的网络错误信息,如超时、无法解析域名等
  1032. return Result::error('请求IP查询接口异常: ' . $e->getMessage());
  1033. } catch (\Throwable $e) {
  1034. return Result::error('获取IP信息时发生未知错误: ' . $e->getMessage());
  1035. }
  1036. }
  1037. /**
  1038. * 获取天气
  1039. * @param array $data
  1040. * @return array
  1041. */
  1042. public function getWeatherInfo(array $data): array
  1043. {
  1044. $month = $data['month'] ?? date('m');
  1045. $day = $data['day'] ?? date('d');
  1046. // 使用缓存键
  1047. $cacheKey = "tsbb_data_weather_{$month}_{$day}";
  1048. // 尝试从缓存获取数据
  1049. $container = \Hyperf\Context\ApplicationContext::getContainer();
  1050. $cache = $container->get(\Psr\SimpleCache\CacheInterface::class);
  1051. if ($cachedData = $cache->get($cacheKey)) {
  1052. return Result::success(unserialize($cachedData));
  1053. }
  1054. $location = $data['latitude'] . ":" . $data['longitude'];
  1055. $api_url = "https://api.seniverse.com/v3/weather/now.json?key=" . \Hyperf\Support\env('WEATHER_KEY') . "&location=" . $location . "&language=zh-Hans&unit=c";
  1056. $ch = curl_init($api_url);
  1057. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  1058. $response = curl_exec($ch);
  1059. curl_close($ch);
  1060. // 解析 JSON 响应
  1061. $WeatherInfo = json_decode($response, true);
  1062. // 缓存结果,设置1小时过期
  1063. $cache->set($cacheKey, serialize($WeatherInfo), 3600);
  1064. if ($WeatherInfo) {
  1065. return Result::success($WeatherInfo);
  1066. } else {
  1067. return Result::error("获取天气失败", 0);
  1068. }
  1069. }
  1070. /**
  1071. * 获取农历信息
  1072. * @return void
  1073. */
  1074. public function getCalendar(array $data): array
  1075. {
  1076. $calendar = new Calendar();
  1077. $result = $calendar->solar($data['year'], $data['month'], $data['day'], $data['hour']); // 阳历
  1078. return Result::success($result);
  1079. }
  1080. /**
  1081. * 添加通栏
  1082. * @param array $data
  1083. * @return array
  1084. */
  1085. public function addSector(array $data): array
  1086. {
  1087. unset($data['user_id']);
  1088. // 对传入的 page_type 数组进行去重、转换为整数并重新索引
  1089. $data['page_type'] = array_values(array_unique(array_map('intval', $data['page_type'])));
  1090. $data['page_type'] = json_encode($data['page_type']);
  1091. $template = Template::where('template_id', $data['template_id'])
  1092. ->first();
  1093. if (empty($template)) {
  1094. return Result::error('皮肤不存在!');
  1095. }
  1096. $sector_id = Sector::where('sector_id',$data['sector_id'])->first();
  1097. if(!empty($sector_id)){
  1098. return Result::error('通栏编号已存在!');
  1099. }
  1100. $result = Sector::insertGetId($data);
  1101. if (empty($result)) {
  1102. return Result::error('添加失败');
  1103. }
  1104. return Result::success($result);
  1105. }
  1106. /**
  1107. * 删除通栏
  1108. * @param array $data
  1109. * @return array
  1110. */
  1111. public function delSector(array $data): array
  1112. {
  1113. $component_id = Component::where('sector_id', $data['id'])->pluck('id')->toArray();
  1114. if (!empty($component_id)) {
  1115. return Result::error('请先删除相关组件!');
  1116. }
  1117. $result = Sector::where('id', $data['id'])->delete();
  1118. if ($result == 1) {
  1119. return Result::success('删除成功');
  1120. } else {
  1121. return Result::error('删除失败');
  1122. }
  1123. }
  1124. /**
  1125. * 修改通栏
  1126. * @param array $data
  1127. * @return array
  1128. */
  1129. public function updateSector(array $data): array
  1130. {
  1131. unset($data['user_id']);
  1132. $sector = Sector::where('id', $data['id'])->first();
  1133. if (empty($sector)) {
  1134. return Result::error('通栏不存在!');
  1135. }
  1136. if($sector['sector_id'] != $data['sector_id']){
  1137. $sector_id = Sector::where('sector_id',$data['sector_id'])->first();
  1138. if(!empty($sector_id)){
  1139. return Result::error('通栏编号已存在!');
  1140. }
  1141. }
  1142. // 对传入的 page_type 数组进行去重、转换为整数并重新索引
  1143. $data['page_type'] = array_values(array_unique(array_map('intval', $data['page_type'])));
  1144. $data['page_type'] = json_encode($data['page_type']);
  1145. $template = Template::where('template_id', $data['template_id'])
  1146. ->whereRaw("JSON_CONTAINS(template.page_type, ?)", [$data['page_type']])
  1147. ->first();
  1148. if (empty($template)) {
  1149. return Result::error('皮肤不存在!');
  1150. }
  1151. $result = Sector::where('id', $data['id'])->update($data);
  1152. if ($result == 1) {
  1153. return Result::success('修改成功');
  1154. } else {
  1155. return Result::error('修改失败');
  1156. }
  1157. }
  1158. public function getComponentList(array $data): array
  1159. {
  1160. var_dump($data, '---------');
  1161. $where = [];
  1162. // $where[] = ['sector.id', '=', $data['id']];
  1163. if (!empty($data['template_class_id'])) {
  1164. $where['template_class.id'] = $data['template_class_id'];
  1165. }
  1166. if (!empty($data['component_name'])) {
  1167. $where['component.component_name'] = $data['component_name'];
  1168. }
  1169. if (!empty($data['sector_id'])) {
  1170. $where['sector.id'] = $data['sector_id'];
  1171. };
  1172. $result = Component::where($where)
  1173. ->leftJoin('template', 'template.id', '=', 'component.template_id')
  1174. ->leftJoin('template_class', 'template_class.id', '=', 'template.template_class_id') // 添加这一行
  1175. ->leftJoin('sector', 'sector.id', '=', 'component.sector_id')
  1176. ->select('template_class.name as template_class_name', 'template.template_name as template_name', 'template_class.id as template_class_id', 'sector.sector_name as sector_name', 'component.*', 'sector.id as sector_id') // 修改这一行)
  1177. ->orderBy('sector.updated_at', 'desc')
  1178. ->orderBy('sector.created_at', 'desc')
  1179. ->paginate($data['page_size'], ['*'], 'mypage_name', $data['page']);
  1180. return Result::success($result);
  1181. }
  1182. public function getComponentInfo(array $data): array
  1183. {
  1184. $where = [];
  1185. $result = Component::where($where)
  1186. ->leftJoin('template', 'template.id', '=', 'component.template_id')
  1187. ->leftJoin('template_class', 'template_class.id', '=', 'template.template_class_id') // 添加这一行
  1188. ->leftJoin('sector', 'sector.id', '=', 'component.sector_id')
  1189. ->select('template_class.name as template_class_name', 'template.template_name as template_name', 'sector.sector_name as sector_name', 'component.*')
  1190. ->get();
  1191. return Result::success($result);
  1192. }
  1193. public function addComponent(array $data): array
  1194. {
  1195. unset($data['user_id']);
  1196. $result = Component::insertGetId($data);
  1197. if ($result) {
  1198. return Result::success($result);
  1199. } else {
  1200. return Result::error('添加失败');
  1201. }
  1202. }
  1203. public function delComponent(array $data): array
  1204. {
  1205. $result = Component::where('id', $data['id'])->delete();
  1206. return Result::success($result);
  1207. }
  1208. public function updateComponent(array $data): array
  1209. {
  1210. $result = Component::where('id', $data['id'])->update($data);
  1211. return Result::success($result);
  1212. }
  1213. public function getWebsiteTemplateInfo(array $data)
  1214. {
  1215. $where = [];
  1216. if (isset($data['id'])) {
  1217. $where[] = ['id', '=', $data['id']];
  1218. }
  1219. $result = WebsiteTemplateInfo::where($where)
  1220. ->leftJoin('website', 'website_template_info.website_id', '=', 'website.id')
  1221. ->leftJoin('website_template', 'website_template_info.template_id', '=', 'website_template.id')
  1222. ->select('website_template_info.*', 'website.website_name')
  1223. ->get();
  1224. if ($result) {
  1225. return Result::success($result);
  1226. } else {
  1227. return Result::error('暂无数据');
  1228. }
  1229. }
  1230. /**
  1231. * 自助建站----1.获取页面类型回显
  1232. * @param array $data
  1233. * @return array
  1234. */
  1235. public function getWebPageType(array $data): array
  1236. {
  1237. // 1:图片 2:文字 3:底部
  1238. $friend_link = Link::where('website_id', $data['website_id'])->where('type', 3)->get();
  1239. if (empty($friend_link->toArray())) {
  1240. return Result::error('暂无友情链接数据!');
  1241. }
  1242. $footer_category = FooterCategory::where('website_id', $data['website_id'])->get();
  1243. if (empty($footer_category->toArray())) {
  1244. return Result::error('暂无底部导航数据!');
  1245. }
  1246. $page_type = WebsiteTemplateInfo::where('website_id', $data['website_id'])->value('page_type');
  1247. if (empty($page_type)) {
  1248. $page_type = [];
  1249. } else {
  1250. $page_type = json_decode($page_type, true);
  1251. }
  1252. // '网站页面类型(存数组)1:首页 2:分类页 3:列表页 4:详情页 5:搜索页 6:特殊列表 7:特殊详情页
  1253. if (isset($page_type) && in_array(5, $page_type)) {
  1254. $page_type = 1;
  1255. } else {
  1256. $page_type = 0;
  1257. }
  1258. $result = [
  1259. 'friend_link' => $friend_link,
  1260. 'footer_category' => $footer_category,
  1261. 'page_type' => $page_type,
  1262. ];
  1263. return Result::success($result);
  1264. }
  1265. /**
  1266. * 自助建站----2.添加页面类型
  1267. * @param array $data
  1268. * @return array
  1269. */
  1270. public function addWebPageType(array $data): array
  1271. {
  1272. // return Result::success($data);
  1273. // 1:首页 2:频道页 3:列表页 4:详情页 5:搜索页 6:特殊列表 7:特殊详情页'
  1274. if ($data['is_search'] == 1) {
  1275. $page_type = json_encode([1, 2, 3, 4, 5, 6, 7]);
  1276. } else {
  1277. $page_type = json_encode([1, 2, 3, 4, 6, 7]);
  1278. }
  1279. // 0:未构建;1:已填写基础信息;2:已选择模板; action_id
  1280. $website_template_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1281. // return Result::success($website_template_info);
  1282. // return Result::success(empty($website_template_info));
  1283. if (empty($website_template_info)) {
  1284. $result = WebsiteTemplateInfo::insertGetId(['website_id' => $data['website_id'], 'page_type' => $page_type, 'user_id' => $data['user_id']]);
  1285. } else {
  1286. if ($website_template_info['status'] == 2) {
  1287. return Result::error('网站已应用,不可再次修改!');
  1288. }
  1289. $result = WebsiteTemplateInfo::where('website_id', $data['website_id'])->update(['page_type' => $page_type], ['action_id' => 1, 'user_id' => $data['user_id']]);
  1290. }
  1291. if ($result) {
  1292. return Result::success($result);
  1293. } else {
  1294. return Result::error('添加失败');
  1295. }
  1296. }
  1297. /**
  1298. * 自助建站---流程---3.选择皮肤列表
  1299. * @param array $data
  1300. * @return array
  1301. */
  1302. public function getWebsiteTemplateList(array $data): array
  1303. {
  1304. $where = [];
  1305. if (isset($data['template_class_id']) && $data['template_class_id']) {
  1306. $where[] = ['template_class_id', '=', $data['template_class_id']];
  1307. }
  1308. // 0:未构建 1:未应用 2:已应用 status
  1309. $templste_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1310. if (empty($templste_info)) {
  1311. return Result::error('请先填写网站基础信息!');
  1312. }
  1313. if ($templste_info['status'] == 2) {
  1314. return Result::error('网站已应用,不可再次修改!');
  1315. }
  1316. if (isset($data['keyword']) && !empty($data['keyword'])) {
  1317. if (is_array($data['keyword'])) {
  1318. // 拼接多个模糊查询条件
  1319. $query = Template::query();
  1320. foreach ($data['keyword'] as $kw) {
  1321. $escapedKw = addcslashes($kw, '%_'); // 转义通配符
  1322. $query->WhereRaw("JSON_EXTRACT(template_keyword, '$[*]') LIKE ?", ["%$escapedKw%"]);
  1323. }
  1324. } else {
  1325. $escapedKeyword = addcslashes($data['keyword'], '%_'); // 转义通配符
  1326. $where[] = [DB::raw("JSON_EXTRACT(template_keyword, '$[*]') LIKE ?"), "%$escapedKeyword%"];
  1327. }
  1328. } else {
  1329. $query = Template::where($where);
  1330. }
  1331. $template_id = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first(['template_id', 'page_type']);
  1332. // return Result::success($template_id);
  1333. if (!empty($template_id['page_type']) && $templste_info['page_type'] != '[]' && is_string($templste_info['page_type'])) {
  1334. $template_page = is_string($templste_info['page_type']) ? json_decode($templste_info['page_type'], true) : [];
  1335. } else {
  1336. $template_page = [];
  1337. }
  1338. if (!empty($template_id['template_id'])) {
  1339. $result['template_id'] = $templste_info['template_id'];
  1340. }
  1341. // 确保 $template_page 为有效数据,避免 SQL 注入风险,这里使用参数绑定
  1342. $template_page_str = json_encode($template_page);
  1343. $result['template'] = $query->where($where)
  1344. ->whereRaw("JSON_CONTAINS(template.page_type, ?)", [$template_page_str])
  1345. ->orderBy('updated_at', 'desc')
  1346. ->paginate($data['page_size'], ['*'], 'page', $data['page']);
  1347. if (empty($result['template'])) {
  1348. $result['template'] = Template::orderBy('updated_at', 'desc')
  1349. ->paginate($data['page_size'], ['*'], 'page', $data['page']);
  1350. }
  1351. $template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->value('template_id');
  1352. if ($template) {
  1353. $result['template_id'] = $template;
  1354. } else {
  1355. $result['template_id'] = 0;
  1356. }
  1357. return Result::success($result);
  1358. }
  1359. /**
  1360. * 自助建站---流程---4.选择皮肤
  1361. * @param array $data
  1362. * @return array
  1363. */
  1364. public function chooseWebsiteTemplate(array $data): array
  1365. {
  1366. $website_template_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1367. if (empty($website_template_info)) {
  1368. return Result::error('请先填写网站基础信息!');
  1369. }
  1370. // 0:未构建 1:未应用 2:已应用 status
  1371. if ($website_template_info['status'] == 2) {
  1372. return Result::error('网站已应用,不可再次修改!');
  1373. }
  1374. $template = Template::where('template_id', $data['template_id'])->first();
  1375. if (empty($template)) {
  1376. return Result::error('未查询到皮肤!');
  1377. }
  1378. // 0:未构建;1:已填写基础信息;2:已选择模板; action_id
  1379. $web_template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->update(['template_id' => $data['template_id'], 'action_id' => 2]);
  1380. if ($web_template) {
  1381. return Result::success($web_template);
  1382. } else {
  1383. return Result::error('选择皮肤失败!');
  1384. }
  1385. }
  1386. /**
  1387. * 自助建站---流程---5.保存模版
  1388. * @param array $data
  1389. * @return array
  1390. */
  1391. public function saveWebsiteTemplate(array $data): array
  1392. {
  1393. // return Result::success($data);
  1394. $website_template_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1395. if (empty($website_template_info) || empty($website_template_info->template_id) || empty($website_template_info->page_type)) {
  1396. return Result::error('请先填写网站基础信息!');
  1397. }
  1398. // 0:未构建 1:未应用 2:已应用 status
  1399. if ($website_template_info['status'] == 2) {
  1400. return Result::error('网站已应用,不可再次保存!');
  1401. }
  1402. if ($website_template_info['action_id'] != 2) {
  1403. return Result::error('请完成之前的步骤!');
  1404. }
  1405. $template_info = WebsiteTemplate::where('website_id', $data['website_id'])->first();
  1406. // var_dump($data['template_data']);
  1407. try {
  1408. Db::beginTransaction();
  1409. if (empty($template_info)) {
  1410. $template = WebsiteTemplate::insertGetId([
  1411. 'website_id' => $data['website_id'],
  1412. 'template_data' => $data['template_data'],
  1413. 'user_id' => $data['user_id'],
  1414. 'canvas_data' => $data['canvas_data'],
  1415. 'page_type' => $website_template_info['page_type'],
  1416. 'template_id' => $website_template_info['template_id'],
  1417. ]);
  1418. } else {
  1419. $template = WebsiteTemplate::where('website_id', $data['website_id'])->update(
  1420. [
  1421. 'template_data' => $data['template_data'],
  1422. 'user_id' => $data['user_id'],
  1423. 'canvas_data' => $data['canvas_data'],
  1424. 'page_type' => $website_template_info['page_type'],
  1425. 'template_id' => $website_template_info['template_id'],
  1426. ]
  1427. );
  1428. }
  1429. if (empty($template)) {
  1430. Db::rollback();
  1431. return Result::error('保存失败!');
  1432. } else {
  1433. $tempalte_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->update(['status' => 1]);
  1434. if (empty($tempalte_info)) {
  1435. Db::rollback();
  1436. return Result::error('保存失败!');
  1437. }
  1438. Db::commit();
  1439. }
  1440. } catch (\Exception $e) {
  1441. return Result::error('模版数据格式错误!');
  1442. }
  1443. return Result::success($template);
  1444. }
  1445. /**
  1446. * 自助建站---流程---6.返显保存的模版
  1447. * @param array $data
  1448. * @return array
  1449. */
  1450. public function getWebsiteTemplate(array $data): array
  1451. {
  1452. $web = Website::where('id', $data['website_id'])->first();
  1453. if (empty($web)) {
  1454. return Result::error('未查询到网站信息!');
  1455. }
  1456. $template = WebsiteTemplate::where('website_id', $data['website_id'])
  1457. ->first(['website_id', 'template_id', 'page_type', 'template_data', 'canvas_data']);
  1458. if (empty($template)) {
  1459. return Result::error('未查询到模版!');
  1460. }
  1461. return Result::success($template);
  1462. }
  1463. /**
  1464. * 自助建站-通栏管理-获取通栏位置
  1465. */
  1466. public function getSectorPlace(array $data): array
  1467. {
  1468. if(isset($data['sector_num']) && !empty($data['sector_num'])){
  1469. $where['t.sector_num'] = $data['sector_num'];
  1470. $template = DB::table(DB::raw('(
  1471. SELECT
  1472. sp.*,
  1473. s.width,
  1474. s.height,
  1475. ROW_NUMBER() OVER (PARTITION BY sp.type ORDER BY sp.id DESC) as rn
  1476. FROM sector_place sp
  1477. LEFT JOIN size s ON sp.size_id = s.id
  1478. ) as t'))
  1479. ->where('rn', 1)
  1480. ->where($where)
  1481. ->select('t.*')
  1482. ->get();
  1483. }
  1484. if(isset($data['type']) && !empty($data['type'])){
  1485. $template = SectorPlace::where('type', $data['type'])->get()->all();
  1486. }
  1487. if(empty($template)){
  1488. return Result::error('未查询到通栏位置相关模版!');
  1489. }
  1490. return Result::success($template);
  1491. }
  1492. }