PublicRpcService.php 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508
  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'])->first();
  447. return Result::success($result);
  448. }
  449. public function getPidZhinengbumen(array $data): array
  450. {
  451. if (empty($data['pid'])) {
  452. $data['pid'] = 0;
  453. }
  454. $result = Department::where('pid', $data['pid'])->get();
  455. return Result::success($result);
  456. }
  457. public function modZhinengbumen(array $data): array
  458. {
  459. $result = Department::where('id', $data['id'])->update($data);
  460. if (empty($result)) {
  461. return Result::error("修改失败", 0);
  462. } else {
  463. return Result::success();
  464. }
  465. }
  466. /**
  467. * 查询职能列表
  468. * @param array $data
  469. * @return array
  470. */
  471. public function getDepartment(array $data): array
  472. {
  473. $where = [];
  474. if (isset($data['pid'])) {
  475. $where = [
  476. 'pid' => $data['pid'] ?? 0
  477. ];
  478. }
  479. $result = Department::when(!empty($where), function ($query) use ($where) {
  480. $query->where($where);
  481. })->orderBy("sort", "desc")->get();
  482. if (empty($result)) {
  483. return Result::error("查询失败", 0);
  484. } else {
  485. return Result::success($result);
  486. }
  487. }
  488. /**
  489. * 获取所有的buckets
  490. * @param array $data
  491. * @return array
  492. */
  493. public function getBuckets(array $data): array
  494. {
  495. $result = new MinioService();
  496. // 调用服务层的方法获取存储桶列表
  497. $bucketsResponse = $result->listBuckets();
  498. // 直接返回服务层生成的响应
  499. return Result::success($bucketsResponse['data']);
  500. }
  501. /**
  502. * 上传文件
  503. * @param array $data
  504. * @return array
  505. */
  506. public function uploadFile(array $data): array
  507. {
  508. $result = new MinioService();
  509. $rep = $result->uploadFile($data);
  510. if ($rep['code'] == 200) {
  511. return Result::success($rep['data']);
  512. } else {
  513. return Result::error("上传失败!");
  514. }
  515. }
  516. /**
  517. * 黑名单管理
  518. * @param array $data
  519. * @return array
  520. */
  521. public function getBlackWordList(array $data): array
  522. {
  523. $result = BlackWord::when($data, function ($query) use ($data) {
  524. if (isset($data['name']) && $data['name']) {
  525. $query->where('black_word.name', 'like', '%' . $data['name'] . '%');
  526. }
  527. })->orderBy('black_word.id', 'desc')
  528. ->paginate(
  529. intval($data['pageSize']),
  530. [
  531. 'black_word.*',
  532. ],
  533. 'page',
  534. intval($data['page'])
  535. );
  536. $count = $result->total();
  537. $returnData = [
  538. 'rows' => $result->items(),
  539. 'count' => $count
  540. ];
  541. return Result::success($returnData);
  542. }
  543. /**
  544. * 添加黑名单
  545. * @param array $data
  546. * @return array
  547. */
  548. public function addBlackWord(array $data): array
  549. {
  550. Db::beginTransaction();
  551. try {
  552. $info = BlackWord::where(['name' => $data['name']])->first();
  553. if ($info) {
  554. Db::rollBack();
  555. return Result::error("该黑名单已存在", 0);
  556. }
  557. $data['type'] = 10;
  558. $result = BlackWord::insertGetId($data);
  559. $redisKey = 'black_word';
  560. $this->redis->sAdd($redisKey, $data['name']);
  561. Db::commit();
  562. return Result::success(["id" => $result]);
  563. } catch (\Exception $e) {
  564. Db::rollBack();
  565. return Result::error("创建失败" . $e->getMessage(), 0);
  566. }
  567. }
  568. /**
  569. * 删除黑名单
  570. * @param array $data
  571. * @return array
  572. */
  573. public function delBlackWord(array $data): array
  574. {
  575. Db::beginTransaction();
  576. try {
  577. BlackWord::where(['name' => $data['name']])->delete();
  578. $redisKey = 'black_word';
  579. $this->redis->sRem($redisKey, $data['name']);
  580. Db::commit();
  581. return Result::success([]);
  582. } catch (\Exception $e) {
  583. Db::rollBack();
  584. return Result::error("删除失败" . $e->getMessage(), 0);
  585. }
  586. }
  587. /**
  588. * 修改违禁词
  589. * @param array $data
  590. * @return array
  591. */
  592. public function upBlackWord(array $data): array
  593. {
  594. Db::beginTransaction();
  595. try {
  596. $checkInfo = BlackWord::where(['name' => $data['name']])->first();
  597. if ($checkInfo) {
  598. Db::rollBack();
  599. return Result::error("该违禁词已经存在", 0);
  600. }
  601. $blackWordInfo = BlackWord::where(['id' => $data['id']])->first();
  602. if ($blackWordInfo) {
  603. //先删除redis
  604. $blackWordInfo = $blackWordInfo->toArray();
  605. $redisKey = 'black_word';
  606. $this->redis->sRem($redisKey, $blackWordInfo['name']);
  607. $this->redis->sAdd($redisKey, $data['name']);
  608. BlackWord::where(['id' => $data['id']])->update(['name' => $data['name']]);
  609. Db::commit();
  610. return Result::success([]);
  611. } else {
  612. Db::rollBack();
  613. return Result::error("系统错误", 0);
  614. }
  615. } catch (\Exception $e) {
  616. Db::rollBack();
  617. return Result::error("修改失败" . $e->getMessage(), 0);
  618. }
  619. }
  620. /**
  621. * 获取风格
  622. * @return void
  623. */
  624. public function getTemplateClassList(array $data): array
  625. {
  626. $where = [];
  627. if (isset($data['name']) && $data['name']) {
  628. array_push($where, ['template_class.name', 'like', '%' . $data['name'] . '%']);
  629. }
  630. if(isset($data['keyword']) && $data['keyword']){
  631. array_push($where, ['template_class.keyword', 'like', '%'. $data['keyword']. '%']);
  632. }
  633. $template = TemplateClass::when($where, function ($query) use ($where) {
  634. $query->where($where);
  635. });
  636. $count = $template->count();
  637. // $countQuery = clone $template;
  638. $row = $template
  639. ->leftJoin('template', function ($join) {
  640. $join->on('template_class.class_id', '=', 'template.template_class_id');
  641. })
  642. ->select('template_class.*', DB::raw('COUNT(template.template_class_id) as template_count'))
  643. ->groupBy('template_class.id')
  644. ->orderBy('template_class.id', 'desc')
  645. ->offset(($data['page'] - 1) * $data['pageSize'])
  646. ->limit($data['pageSize'])
  647. ->get();
  648. $result = [
  649. 'rows' => $row,
  650. 'count' => $count,
  651. ];
  652. if ($row->isEmpty()) {
  653. return Result::error("暂无风格", 0);
  654. } else {
  655. return Result::success($result);
  656. }
  657. }
  658. /**
  659. * 添加风格
  660. * @param
  661. * @return void
  662. */
  663. public function addTemplateClass(array $data): array
  664. {
  665. $data['keyword'] = json_encode($data['keyword']);
  666. $template_class = TemplateClass::where('name', $data['name'])
  667. ->orWhere('class_id', $data['class_id'])
  668. ->first();
  669. if ($template_class) {
  670. return Result::error("风格名称或者风格编号已存在,不可添加!", 0);
  671. }
  672. $result = TemplateClass::insertGetId($data);
  673. if (empty($result)) {
  674. return Result::error("创建风格失败", 0);
  675. } else {
  676. return Result::success(["id" => $result]);
  677. }
  678. }
  679. /**
  680. * 更新风格
  681. * @param array $data
  682. * @return array
  683. */
  684. public function upTemplateClass(array $data): array
  685. {
  686. $where = [
  687. 'id' => $data['id'],
  688. ];
  689. $template_class = TemplateClass::where($where)->first();
  690. if (empty($template_class)) {
  691. return Result::error("未查询到风格", 0);
  692. }
  693. if($template_class->type == 1){
  694. return Result::error("默认风格不能修改", 0);
  695. }
  696. $template = TemplateClass::where('id','!=',$data['id'])
  697. // ->where(['name' => $data['name']])
  698. // ->orWhere(['class_id' => $data['class_id']])
  699. ->where(function ($query) use ($data) {
  700. $query->where('name', $data['name'])
  701. ->orWhere('class_id', $data['class_id']);
  702. })
  703. ->first();
  704. if ($template) {
  705. return Result::error("风格名称或者风格编号已存在,不可编辑!", 0);
  706. }
  707. $updateData = [
  708. 'name' => $data['name'],
  709. 'keyword' => json_encode($data['keyword']),
  710. 'class_id' => $data['class_id'],
  711. ];
  712. $result = TemplateClass::where($where)->update($updateData);
  713. if (empty($result)) {
  714. return Result::error("更新失败", 0);
  715. } else {
  716. return Result::success($result);
  717. }
  718. }
  719. /**
  720. * 删除风格
  721. * @param array $data
  722. * @return array
  723. */
  724. public function delTemplateClass(array $data): array
  725. {
  726. $where = [
  727. 'id' => $data['id'],
  728. ];
  729. $template_class = TemplateClass::where($where)->first();
  730. if (empty($template_class)) {
  731. return Result::error("未查询到风格", 0);
  732. }
  733. if($template_class->type == 1){
  734. return Result::error("默认风格不能删除", 0);
  735. }
  736. $template = Template::where('template_class_id',$template_class['class_id'])->get();
  737. if(!empty($template->all())){
  738. try{
  739. // 默认风格 1
  740. Db::beginTransaction();
  741. $template = Template::where('template_class_id',$template['class_id'])->update(['template_class_id'=>1]);
  742. if(empty($template)){
  743. Db::rollBack();
  744. return Result::error("删除失败", 0);
  745. }
  746. $result = TemplateClass::where($where)->delete();
  747. }catch (\Exception $e){
  748. return Result::error("删除失败".$e->getMessage(), 0);
  749. }
  750. }else{
  751. $result = TemplateClass::where('id',$data['id'])->delete();
  752. }
  753. if(empty($result)){
  754. return Result::error("删除失败", 0);
  755. }
  756. return Result::success($result);
  757. }
  758. /**
  759. * 根据风格名称-获取获取所有风格
  760. * @param array $data
  761. * @return array
  762. */
  763. public function getTemplateClass(array $data): array
  764. {
  765. $where = [];
  766. if (!empty($data['template_name'])) {
  767. $where['name'] = $data['template_name'];
  768. }
  769. $result = TemplateClass::where($where)->get();
  770. if(empty($result)){
  771. return Result::error("暂无风格", 0);
  772. }
  773. return Result::success($result);
  774. }
  775. /**
  776. * 获取皮肤列表
  777. * @param array $data
  778. * @return array
  779. */
  780. public function getTemplateList(array $data): array
  781. {
  782. $where = [];
  783. if (!empty($data['template_class_id'])) {
  784. $where['template_class_id'] = $data['template_class_id'];
  785. }
  786. if (!empty($data['template_name'])) {
  787. array_push($where, ['template_name', 'like', '%'. $data['template_name']. '%']);
  788. }
  789. if (!empty($data['template_keyword'])) {
  790. array_push($where, ['template_keyword', 'like', '%'. $data['template_keyword']. '%']);
  791. }
  792. $result = Template::where($where)
  793. ->leftJoin('template_class', 'template.template_class_id', 'template_class.class_id')
  794. ->select('template.*', 'template_class.name as template_class_name')
  795. ->orderBy('template.id', 'desc')
  796. ->paginate($data['page_size'], ['*'], 'mypage_name', $data['page']);
  797. if(empty($result)){
  798. return Result::error("暂无皮肤", 0);
  799. }
  800. return Result::success($result);
  801. }
  802. public function getTemplateInfo(array $data): array
  803. {
  804. $result = Template::where('template.id', $data['id'])
  805. ->leftJoin('template_class', 'template.template_class_id', 'template_class.class_id')
  806. ->select('template.*', 'template_class.name as template_class_name')
  807. ->first();
  808. if(empty($result)){
  809. return Result::error("暂无皮肤", 0);
  810. }
  811. return Result::success($result);
  812. }
  813. /**
  814. * 添加皮肤
  815. * @param array $data
  816. * @return array
  817. */
  818. public function addTemplate(array $data): array
  819. {
  820. var_dump($data);
  821. unset($data['user_id']);
  822. $template = Template::where('template_name', $data['template_name'])
  823. ->orWhere('template_id', $data['template_id'])
  824. ->first();
  825. if ($template) {
  826. return Result::error("皮肤名称或者皮肤编号已存在,不可添加!", 0);
  827. }
  828. $data['template_keyword'] = json_encode($data['template_keyword']) ?? '';
  829. $page_type = json_decode($data['page_type'], true);
  830. $data['page_type'] = json_encode(array_values(array_unique($page_type)));
  831. $result = Template::insertGetId($data);
  832. if(empty($result)){
  833. return Result::error("创建失败", 0);
  834. }else{
  835. return Result::success($result);
  836. }
  837. }
  838. /**
  839. * 删除皮肤
  840. * @param array $data
  841. * @return array
  842. */
  843. public function delTemplate(array $data): array
  844. {
  845. $rector = Sector::where('template_id', $data['id'])->first();
  846. if (!empty($rector)) {
  847. return Result::error("此皮肤已被绑定通栏,不可删除", 0);
  848. }
  849. $template = WebsiteTemplateInfo::where('template_id', $data['id'])->first();
  850. if(empty($template)){
  851. return Result::error("此皮肤已被绑定网站,不可删除", 0);
  852. }
  853. if($template->type == 1){
  854. return Result::error("默认皮肤不能删除", 0);
  855. }
  856. $result = Template::where('id', $data['id'])->delete();
  857. var_dump($result, '-------------------delete');
  858. if ($result) {
  859. return Result::success($result);
  860. } else {
  861. return Result::error("删除失败", 0);
  862. }
  863. }
  864. /**
  865. * 更新皮肤
  866. * @param array $data
  867. * @return array
  868. */
  869. public function updateTemplate(array $data): array
  870. {
  871. unset($data['user_id']);
  872. $template = Template::where('id', $data['id'])
  873. ->first();
  874. if (empty($template)) {
  875. return Result::error("此皮肤不存在!", 0);
  876. }
  877. $template = Template::where('id', '!=', $data['id'])
  878. ->where(function ($query) use ($data) {
  879. $query->where('template_name', $data['template_name'])
  880. ->orWhere('template_id', $data['template_id']);
  881. })
  882. ->first();
  883. if ($template) {
  884. return Result::error("皮肤名称或者皮肤编号已存在,不可编辑!", 0);
  885. }
  886. $data['template_keyword'] = json_encode($data['template_keyword']) ?? '';
  887. $page_type = json_decode($data['page_type'], true);
  888. $data['page_type'] = json_encode(array_values(array_unique($page_type)));
  889. $result = Template::where('id', $data['id'])->update($data);
  890. var_dump($result, '-------------------update');
  891. if (!$result) {
  892. return Result::error("更新失败", 0);
  893. } else {
  894. return Result::success('更新成功');
  895. }
  896. }
  897. /**
  898. * 根据皮肤名称-获取所有皮肤
  899. * @param array $data
  900. * @return array
  901. */
  902. public function getAllTemplate(array $data): array
  903. {
  904. $result = Template::where('template_name','like','%'.$data['template_name'].'%')->get();
  905. if(empty($result)){
  906. return Result::error("暂无皮肤", 0);
  907. }
  908. return Result::success($result);
  909. }
  910. /**
  911. * 获取通栏列表
  912. * @param array $data
  913. * @return array
  914. */
  915. public function getSectorList(array $data): array
  916. {
  917. $where = [];
  918. if (isset($data['template_class_id']) && !empty($data['template_class_id'])) {
  919. $where['template_class_id'] = $data['template_class_id'];
  920. }
  921. if (isset($data['template_id']) && !empty($data['template_id'])) {
  922. array_push($where, ['template_id', $data['template_id']]);
  923. }
  924. if (isset($data['sector_name']) && !empty($data['sector_name'])) {
  925. array_push($where, ['sector.sector_name', 'like', '%' . $data['sector_name'] . '%']);
  926. }
  927. if (isset($data['width']) && !empty($data['width']) && isset($data['height']) && !empty($data['height'])){
  928. $size_id = Size::where('width','like','%' .$data['width'].'%')->where('height','like','%' .$data['height'].'%')->pluck('id');
  929. } else if( isset($data['width']) && !empty($data['width'])){
  930. $size_id = Size::where('width','like','%' .$data['width'].'%')->pluck('id');
  931. }else if( isset($data['height']) && !empty($data['height'])){
  932. $size_id = Size::where('height','like','%' .$data['height'].'%')->pluck('id');
  933. }else{
  934. $size_id = [];
  935. }
  936. // $size_id = $size_id->toArray();
  937. $result = Sector::where($where)
  938. ->when(!empty($size_id), function ($query) use ($size_id) {
  939. $query->whereIn('size_id', $size_id);
  940. })
  941. ->leftJoin('size', 'size.id', '=', 'sector.size_id')
  942. ->select('sector.*', 'size.width','size.height')
  943. ->orderBy('sector.id', 'desc')
  944. ->paginate($data['page_size'], ['*'], 'page', $data['page']);
  945. return Result::success($result);
  946. }
  947. /**
  948. * 获取通栏详情
  949. * @param array $data
  950. * @return array
  951. */
  952. public function getSectorInfo(array $data): array
  953. {
  954. $where = [];
  955. $where[] = ['sector.id', '=', $data['id']];
  956. $result = Sector::where($where)
  957. ->leftJoin('size', 'size.id', '=', 'sector.size_id')
  958. ->select('sector.*', 'size.width','size.height')
  959. ->orderBy('sector.id', 'desc')
  960. ->first();
  961. if(empty($result)){
  962. return Result::error('此通栏不存在!');
  963. }
  964. return Result::success($result);
  965. }
  966. /**
  967. * 获取经纬度信息
  968. * @return void
  969. */
  970. public function getIpInfo(array $data) :array
  971. {
  972. if (empty($data['ip'])) {
  973. // 在RPC服务中,依赖$_SERVER是不可靠的,强制要求调用方传入IP
  974. return Result::error('IP地址不能为空!');
  975. }
  976. $client_ip = $data['ip'];
  977. try {
  978. // 直接从容器获取 Guzzle 客户端实例
  979. /** @var \GuzzleHttp\Client $client */
  980. $client = \Hyperf\Context\ApplicationContext::getContainer()->get(Client::class);
  981. // 使用新的 GeoJS API 地址
  982. $api_url = "https://get.geojs.io/v1/ip/geo/{$client_ip}.json";
  983. // 将 timeout 作为请求选项传递
  984. $response = $client->get($api_url, ['timeout' => 5]);
  985. if ($response->getStatusCode() !== 200) {
  986. return Result::error('IP查询接口请求失败,状态码:' . $response->getStatusCode());
  987. }
  988. $ip_info = json_decode($response->getBody()->getContents(), true);
  989. // GeoJS 的成功响应不包含 'status' 字段,直接检查关键数据是否存在
  990. if ($ip_info && isset($ip_info['latitude']) && isset($ip_info['longitude'])) {
  991. // GeoJS 返回的经纬度是字符串,转换为浮点数
  992. $ip_info['latitude'] = floatval($ip_info['latitude']);
  993. $ip_info['longitude'] = floatval($ip_info['longitude']);
  994. return Result::success($ip_info);
  995. }
  996. // 如果返回的数据不符合预期,提供一个通用错误
  997. $errorMessage = $ip_info['message'] ?? '获取IP地理位置信息失败';
  998. return Result::error($errorMessage);
  999. } catch (GuzzleException $e) {
  1000. // 这个异常会给出详细的网络错误信息,如超时、无法解析域名等
  1001. return Result::error('请求IP查询接口异常: ' . $e->getMessage());
  1002. } catch (\Throwable $e) {
  1003. return Result::error('获取IP信息时发生未知错误: ' . $e->getMessage());
  1004. }
  1005. }
  1006. /**
  1007. * 获取天气
  1008. * @param array $data
  1009. * @return array
  1010. */
  1011. public function getWeatherInfo(array $data) :array
  1012. {
  1013. $month = $data['month'] ?? date('m');
  1014. $day = $data['day'] ?? date('d');
  1015. // 使用缓存键
  1016. $cacheKey = "tsbb_data_weather_{$month}_{$day}";
  1017. // 尝试从缓存获取数据
  1018. $container = \Hyperf\Context\ApplicationContext::getContainer();
  1019. $cache = $container->get(\Psr\SimpleCache\CacheInterface::class);
  1020. if ($cachedData = $cache->get($cacheKey)) {
  1021. return Result::success(unserialize($cachedData));
  1022. }
  1023. $location = $data['latitude'].":".$data['longitude'];
  1024. $api_url = "https://api.seniverse.com/v3/weather/now.json?key=".\Hyperf\Support\env('WEATHER_KEY')."&location=".$location."&language=zh-Hans&unit=c";
  1025. $ch = curl_init($api_url);
  1026. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  1027. $response = curl_exec($ch);
  1028. curl_close($ch);
  1029. // 解析 JSON 响应
  1030. $WeatherInfo = json_decode($response, true);
  1031. // 缓存结果,设置1小时过期
  1032. $cache->set($cacheKey, serialize($WeatherInfo), 3600);
  1033. if($WeatherInfo){
  1034. return Result::success($WeatherInfo);
  1035. }else{
  1036. return Result::error("获取天气失败", 0);
  1037. }
  1038. }
  1039. /**
  1040. * 获取农历信息
  1041. * @return void
  1042. */
  1043. public function getCalendar(array $data) :array
  1044. {
  1045. $calendar = new Calendar();
  1046. $result = $calendar->solar($data['year'], $data['month'], $data['day'], $data['hour']); // 阳历
  1047. return Result::success($result);
  1048. }
  1049. /**
  1050. * 添加通栏
  1051. * @param array $data
  1052. * @return array
  1053. */
  1054. public function addSector(array $data): array
  1055. {
  1056. unset($data['user_id']);
  1057. // 对传入的 page_type 数组进行去重、转换为整数并重新索引
  1058. $data['page_type'] = array_values(array_unique(array_map('intval', $data['page_type'])));
  1059. $data['page_type'] = json_encode($data['page_type']);
  1060. $template = Template::where('template_id', $data['template_id'])
  1061. ->whereRaw("JSON_CONTAINS(template.page_type, ?)", [$data['page_type']])
  1062. ->first();
  1063. if(empty($template)){
  1064. return Result::error('皮肤不存在!');
  1065. }
  1066. // 皮肤相关信息
  1067. $data['template_name'] = $template['template_name'];
  1068. $data['template_id'] = $template['template_id'];
  1069. // 风格相关信息
  1070. $template_class = TemplateClass::where('class_id',$template['template_class_id'])->first();
  1071. if(empty($template_class)){
  1072. return Result::error('所属风格不存在!');
  1073. }
  1074. // $data['sector_keyword'] = json_encode($data['sector_keyword']);
  1075. $data['template_class_id'] = $template_class['class_id'];
  1076. $data['template_class_name'] = $template_class['name'];
  1077. $result = Sector::insertGetId($data);
  1078. if(empty($result)){
  1079. return Result::error('添加失败');
  1080. }
  1081. return Result::success($result);
  1082. }
  1083. /**
  1084. * 删除通栏
  1085. * @param array $data
  1086. * @return array
  1087. */
  1088. public function delSector(array $data): array
  1089. {
  1090. $component_id = Component::where('sector_id', $data['id'])->pluck('id')->toArray();
  1091. if(!empty($component_id)){
  1092. return Result::error('请先删除相关组件!');
  1093. }
  1094. $result = Sector::where('id', $data['id'])->delete();
  1095. if ($result == 1) {
  1096. return Result::success('删除成功');
  1097. } else {
  1098. return Result::error('删除失败');
  1099. }
  1100. }
  1101. /**
  1102. * 修改通栏
  1103. * @param array $data
  1104. * @return array
  1105. */
  1106. public function updateSector(array $data): array
  1107. {
  1108. unset($data['user_id']);
  1109. $sector = Sector::where('id', $data['id'])->first();
  1110. if(empty($sector)){
  1111. return Result::error('通栏不存在!');
  1112. }
  1113. // 对传入的 page_type 数组进行去重、转换为整数并重新索引
  1114. $data['page_type'] = array_values(array_unique(array_map('intval', $data['page_type'])));
  1115. $data['page_type'] = json_encode($data['page_type']);
  1116. $template = Template::where('template_id', $data['template_id'])
  1117. ->whereRaw("JSON_CONTAINS(template.page_type, ?)", [$data['page_type']])
  1118. ->first();
  1119. if(empty($template)){
  1120. return Result::error('皮肤不存在!');
  1121. }
  1122. // 皮肤相关信息
  1123. $data['template_name'] = $template['template_name'];
  1124. $data['template_id'] = $template['template_id'];
  1125. // 风格相关信息
  1126. $template_class = TemplateClass::where('class_id',$template['template_class_id'])->first();
  1127. if(empty($template_class)){
  1128. return Result::error('所属风格不存在!');
  1129. }
  1130. // $data['sector_keyword'] = json_encode($data['sector_keyword']);
  1131. $data['template_class_id'] = $template_class['class_id'];
  1132. $data['template_class_name'] = $template_class['name'];
  1133. $result = Sector::where('id', $data['id'])->update($data);
  1134. if ($result == 1) {
  1135. return Result::success('修改成功');
  1136. } else {
  1137. return Result::error('修改失败');
  1138. }
  1139. }
  1140. public function getComponentList(array $data): array
  1141. {
  1142. var_dump($data, '---------');
  1143. $where = [];
  1144. // $where[] = ['sector.id', '=', $data['id']];
  1145. if (!empty($data['template_class_id'])) {
  1146. $where['template_class.id'] = $data['template_class_id'];
  1147. }
  1148. if (!empty($data['component_name'])) {
  1149. $where['component.component_name'] = $data['component_name'];
  1150. }
  1151. if (!empty($data['sector_id'])) {
  1152. $where['sector.id'] = $data['sector_id'];
  1153. };
  1154. $result = Component::where($where)
  1155. ->leftJoin('template', 'template.id', '=', 'component.template_id')
  1156. ->leftJoin('template_class', 'template_class.id', '=', 'template.template_class_id') // 添加这一行
  1157. ->leftJoin('sector', 'sector.id', '=', 'component.sector_id')
  1158. ->select('template_class.name as template_class_name', 'template.template_name as template_name', 'template_class.id as template_class_id', 'sector.sector_name as sector_name', 'component.*', 'sector.id as sector_id') // 修改这一行)
  1159. ->orderBy('sector.updated_at', 'desc')
  1160. ->orderBy('sector.created_at', 'desc')
  1161. ->paginate($data['page_size'], ['*'], 'mypage_name', $data['page']);
  1162. return Result::success($result);
  1163. }
  1164. public function getComponentInfo(array $data): array
  1165. {
  1166. $where = [];
  1167. $result = Component::where($where)
  1168. ->leftJoin('template', 'template.id', '=', 'component.template_id')
  1169. ->leftJoin('template_class', 'template_class.id', '=', 'template.template_class_id') // 添加这一行
  1170. ->leftJoin('sector', 'sector.id', '=', 'component.sector_id')
  1171. ->select('template_class.name as template_class_name', 'template.template_name as template_name', 'sector.sector_name as sector_name', 'component.*')
  1172. ->get();
  1173. return Result::success($result);
  1174. }
  1175. public function addComponent(array $data): array
  1176. {
  1177. unset($data['user_id']);
  1178. $result = Component::insertGetId($data);
  1179. if ($result) {
  1180. return Result::success($result);
  1181. } else {
  1182. return Result::error('添加失败');
  1183. }
  1184. }
  1185. public function delComponent(array $data): array
  1186. {
  1187. $result = Component::where('id', $data['id'])->delete();
  1188. return Result::success($result);
  1189. }
  1190. public function updateComponent(array $data): array
  1191. {
  1192. $result = Component::where('id', $data['id'])->update($data);
  1193. return Result::success($result);
  1194. }
  1195. public function getWebsiteTemplateInfo(array $data)
  1196. {
  1197. $where = [];
  1198. if (isset($data['id'])) {
  1199. $where[] = ['id', '=', $data['id']];
  1200. }
  1201. $result = WebsiteTemplateInfo::where($where)
  1202. ->leftJoin('website', 'website_template_info.website_id', '=', 'website.id')
  1203. ->leftJoin('website_template', 'website_template_info.template_id', '=', 'website_template.id')
  1204. ->select('website_template_info.*', 'website.website_name')
  1205. ->get();
  1206. if ($result) {
  1207. return Result::success($result);
  1208. } else {
  1209. return Result::error('暂无数据');
  1210. }
  1211. }
  1212. /**
  1213. * 自助建站----1.获取页面类型回显
  1214. * @param array $data
  1215. * @return array
  1216. */
  1217. public function getWebPageType(array $data): array
  1218. {
  1219. // 1:图片 2:文字 3:底部
  1220. $friend_link = Link::where('website_id', $data['website_id'])->where('type',3)->get();
  1221. if(empty($friend_link->toArray())){
  1222. return Result::error('暂无友情链接数据!');
  1223. }
  1224. $footer_category = FooterCategory::where('website_id', $data['website_id'])->get();
  1225. if(empty($footer_category->toArray())){
  1226. return Result::error('暂无底部导航数据!');
  1227. }
  1228. $page_type = WebsiteTemplateInfo::where('website_id', $data['website_id'])->value('page_type');
  1229. if(empty($page_type)){
  1230. $page_type = [];
  1231. }else{
  1232. $page_type = json_decode($page_type,true);
  1233. }
  1234. // '网站页面类型(存数组)1:首页 2:分类页 3:列表页 4:详情页 5:搜索页 6:特殊列表 7:特殊详情页
  1235. if(isset($page_type) && in_array(5,$page_type)){
  1236. $page_type = 1;
  1237. }else{
  1238. $page_type = 0;
  1239. }
  1240. $result = [
  1241. 'friend_link' => $friend_link,
  1242. 'footer_category' => $footer_category,
  1243. 'page_type' => $page_type,
  1244. ];
  1245. return Result::success($result);
  1246. }
  1247. /**
  1248. * 自助建站----2.添加页面类型
  1249. * @param array $data
  1250. * @return array
  1251. */
  1252. public function addWebPageType(array $data): array
  1253. {
  1254. // return Result::success($data);
  1255. // 1:首页 2:频道页 3:列表页 4:详情页 5:搜索页 6:特殊列表 7:特殊详情页'
  1256. if($data['is_search'] == 1){
  1257. $page_type = json_encode([1,2,3,4,5,6,7]);
  1258. }else{
  1259. $page_type = json_encode([1,2,3,4,6,7]);
  1260. }
  1261. // 0:未构建;1:已填写基础信息;2:已选择模板; action_id
  1262. $website_template_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1263. // return Result::success($website_template_info);
  1264. // return Result::success(empty($website_template_info));
  1265. if(empty($website_template_info)){
  1266. $result = WebsiteTemplateInfo::insertGetId(['website_id' => $data['website_id'], 'page_type' => $page_type, 'user_id' => $data['user_id']]);
  1267. }else{
  1268. if($website_template_info['status'] == 2){
  1269. return Result::error('网站已应用,不可再次修改!');
  1270. }
  1271. $result = WebsiteTemplateInfo::where('website_id', $data['website_id'])->update(['page_type' => $page_type],['action_id' => 1,'user_id' => $data['user_id']]);
  1272. }
  1273. if ($result) {
  1274. return Result::success($result);
  1275. } else {
  1276. return Result::error('添加失败');
  1277. }
  1278. }
  1279. /**
  1280. * 自助建站---流程---3.选择皮肤列表
  1281. * @param array $data
  1282. * @return array
  1283. */
  1284. public function getWebsiteTemplateList(array $data): array
  1285. {
  1286. $where = [];
  1287. if(isset($data['template_class_id']) && $data['template_class_id']){
  1288. $where[] = ['template_class_id', '=', $data['template_class_id']];
  1289. }
  1290. // 0:未构建 1:未应用 2:已应用 status
  1291. $templste_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1292. if(empty($templste_info)){
  1293. return Result::error('请先填写网站基础信息!');
  1294. }
  1295. if($templste_info['status'] == 2){
  1296. return Result::error('网站已应用,不可再次修改!');
  1297. }
  1298. if(isset($data['keyword']) && !empty($data['keyword'])){
  1299. if (is_array($data['keyword'])) {
  1300. // 拼接多个模糊查询条件
  1301. $query = Template::query();
  1302. foreach ($data['keyword'] as $kw) {
  1303. $escapedKw = addcslashes($kw, '%_'); // 转义通配符
  1304. $query->WhereRaw("JSON_EXTRACT(template_keyword, '$[*]') LIKE ?", ["%$escapedKw%"]);
  1305. }
  1306. } else {
  1307. $escapedKeyword = addcslashes($data['keyword'], '%_'); // 转义通配符
  1308. $where[] = [DB::raw("JSON_EXTRACT(template_keyword, '$[*]') LIKE ?"), "%$escapedKeyword%"];
  1309. }
  1310. }else{
  1311. $query = Template::where($where);
  1312. }
  1313. $template_id = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first(['template_id', 'page_type']);
  1314. // return Result::success($template_id);
  1315. if(!empty($template_id['page_type']) && $templste_info['page_type'] != '[]' && is_string($templste_info['page_type'])){
  1316. $template_page = is_string($templste_info['page_type']) ? json_decode($templste_info['page_type'], true) : [];
  1317. }else{
  1318. $template_page = [];
  1319. }
  1320. if(!empty($template_id['template_id'])){
  1321. $result['template_id'] = $templste_info['template_id'];
  1322. }
  1323. // 确保 $template_page 为有效数据,避免 SQL 注入风险,这里使用参数绑定
  1324. $template_page_str = json_encode($template_page);
  1325. $result['template'] = $query->where($where)
  1326. ->whereRaw("JSON_CONTAINS(template.page_type, ?)", [$template_page_str])
  1327. ->orderBy('updated_at','desc')
  1328. ->paginate($data['page_size'], ['*'], 'page', $data['page']);
  1329. if (empty($result['template'])) {
  1330. $result['template'] = Template::orderBy('updated_at','desc')
  1331. ->paginate($data['page_size'], ['*'], 'page', $data['page']);
  1332. }
  1333. $template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->value('template_id');
  1334. if($template){
  1335. $result['template_id'] = $template;
  1336. }else{
  1337. $result['template_id'] = 0;
  1338. }
  1339. return Result::success($result);
  1340. }
  1341. /**
  1342. * 自助建站---流程---4.选择皮肤
  1343. * @param array $data
  1344. * @return array
  1345. */
  1346. public function chooseWebsiteTemplate(array $data): array
  1347. {
  1348. $website_template_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1349. if(empty($website_template_info)){
  1350. return Result::error('请先填写网站基础信息!');
  1351. }
  1352. // 0:未构建 1:未应用 2:已应用 status
  1353. if($website_template_info['status'] == 2){
  1354. return Result::error('网站已应用,不可再次修改!');
  1355. }
  1356. $template = Template::where('template_id', $data['template_id'])->first();
  1357. if(empty($template)){
  1358. return Result::error('未查询到皮肤!');
  1359. }
  1360. // 0:未构建;1:已填写基础信息;2:已选择模板; action_id
  1361. $web_template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->update(['template_id' => $data['template_id'],'action_id' => 2]);
  1362. if ($web_template) {
  1363. return Result::success($web_template);
  1364. } else {
  1365. return Result::error('选择皮肤失败!');
  1366. }
  1367. }
  1368. /**
  1369. * 自助建站---流程---5.保存模版
  1370. * @param array $data
  1371. * @return array
  1372. */
  1373. public function saveWebsiteTemplate(array $data): array
  1374. {
  1375. // return Result::success($data);
  1376. $website_template_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1377. if(empty($website_template_info) || empty($website_template_info->template_id) || empty($website_template_info->page_type)){
  1378. return Result::error('请先填写网站基础信息!');
  1379. }
  1380. // 0:未构建 1:未应用 2:已应用 status
  1381. if($website_template_info['status'] == 2){
  1382. return Result::error('网站已应用,不可再次保存!');
  1383. }
  1384. if($website_template_info['action_id'] != 2){
  1385. return Result::error('请完成之前的步骤!');
  1386. }
  1387. $template_info = WebsiteTemplate::where('website_id', $data['website_id'])->first();
  1388. // var_dump($data['template_data']);
  1389. try{
  1390. Db::beginTransaction();
  1391. if(empty($template_info)){
  1392. $template = WebsiteTemplate::insertGetId([
  1393. 'website_id' => $data['website_id'],
  1394. 'template_data' => $data['template_data'],
  1395. 'user_id' => $data['user_id'],
  1396. 'canvas_data' => $data['canvas_data'],
  1397. 'page_type' => $website_template_info['page_type'],
  1398. 'template_id' => $website_template_info['template_id'],
  1399. ]);
  1400. }else{
  1401. $template = WebsiteTemplate::where('website_id', $data['website_id'])->update(
  1402. ['template_data' => $data['template_data'],'user_id' => $data['user_id'],'canvas_data' => $data['canvas_data'],
  1403. 'page_type' => $website_template_info['page_type'],
  1404. 'template_id' => $website_template_info['template_id'],
  1405. ]);
  1406. }
  1407. if(empty($template)){
  1408. Db::rollback();
  1409. return Result::error('保存失败!');
  1410. }else{
  1411. $tempalte_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->update(['status' => 1]);
  1412. if(empty($tempalte_info)){
  1413. Db::rollback();
  1414. return Result::error('保存失败!');
  1415. }
  1416. Db::commit();
  1417. }
  1418. }catch (\Exception $e){
  1419. return Result::error('模版数据格式错误!');
  1420. }
  1421. return Result::success($template);
  1422. }
  1423. /**
  1424. * 自助建站---流程---6.返显保存的模版
  1425. * @param array $data
  1426. * @return array
  1427. */
  1428. public function getWebsiteTemplate(array $data): array
  1429. {
  1430. $web = Website::where('id', $data['website_id'])->first();
  1431. if(empty($web)){
  1432. return Result::error('未查询到网站信息!');
  1433. }
  1434. $template = WebsiteTemplate::where('website_id', $data['website_id'])
  1435. ->first(['website_id','template_id','page_type','template_data','canvas_data']);
  1436. if(empty($template)){
  1437. return Result::error('未查询到模版!');
  1438. }
  1439. return Result::success($template);
  1440. }
  1441. }