PublicRpcService.php 50 KB

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