PublicRpcService.php 54 KB

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