PublicRpcService.php 59 KB

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