PublicRpcService.php 54 KB

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