PublicRpcService.php 57 KB

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