PublicRpcService.php 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426
  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. $where = [];
  751. if (!empty($data['template_name'])) {
  752. $where['name'] = $data['template_name'];
  753. }
  754. $result = TemplateClass::where($where)->get();
  755. if(empty($result)){
  756. return Result::error("暂无风格", 0);
  757. }
  758. return Result::success($result);
  759. }
  760. /**
  761. * 获取皮肤列表
  762. * @param array $data
  763. * @return array
  764. */
  765. public function getTemplateList(array $data): array
  766. {
  767. $where = [];
  768. if (!empty($data['template_class_id'])) {
  769. $where['template_class_id'] = $data['template_class_id'];
  770. }
  771. if (!empty($data['template_name'])) {
  772. array_push($where, ['template_name', 'like', '%'. $data['template_name']. '%']);
  773. }
  774. if (!empty($data['template_keyword'])) {
  775. array_push($where, ['template_keyword', 'like', '%'. $data['template_keyword']. '%']);
  776. }
  777. $result = Template::where($where)
  778. ->leftJoin('template_class', 'template.template_class_id', 'template_class.class_id')
  779. ->select('template.*', 'template_class.name as template_class_name')
  780. ->orderBy('template.id', 'desc')
  781. ->paginate($data['page_size'], ['*'], 'mypage_name', $data['page']);
  782. if(empty($result)){
  783. return Result::error("暂无皮肤", 0);
  784. }
  785. return Result::success($result);
  786. }
  787. public function getTemplateInfo(array $data): array
  788. {
  789. $result = Template::where('template.id', $data['id'])
  790. ->leftJoin('template_class', 'template.template_class_id', 'template_class.class_id')
  791. ->select('template.*', 'template_class.name as template_class_name')
  792. ->first();
  793. if(empty($result)){
  794. return Result::error("暂无皮肤", 0);
  795. }
  796. return Result::success($result);
  797. }
  798. /**
  799. * 添加皮肤
  800. * @param array $data
  801. * @return array
  802. */
  803. public function addTemplate(array $data): array
  804. {
  805. var_dump($data);
  806. unset($data['user_id']);
  807. $template = Template::where('template_name', $data['template_name'])
  808. ->orWhere('template_id', $data['template_id'])
  809. ->first();
  810. if ($template) {
  811. return Result::error("皮肤名称或者皮肤编号已存在,不可添加!", 0);
  812. }
  813. $data['template_keyword'] = json_encode($data['template_keyword']) ?? '';
  814. $page_type = json_decode($data['page_type'], true);
  815. $data['page_type'] = json_encode(array_values(array_unique($page_type)));
  816. $result = Template::insertGetId($data);
  817. if(empty($result)){
  818. return Result::error("创建失败", 0);
  819. }else{
  820. return Result::success($result);
  821. }
  822. }
  823. /**
  824. * 删除皮肤
  825. * @param array $data
  826. * @return array
  827. */
  828. public function delTemplate(array $data): array
  829. {
  830. $rector = Sector::where('template_id', $data['id'])->first();
  831. if (!empty($rector)) {
  832. return Result::error("此皮肤已被绑定,不可删除", 0);
  833. }
  834. $result = Template::where('id', $data['id'])->delete();
  835. var_dump($result, '-------------------delete');
  836. if ($result) {
  837. return Result::success($result);
  838. } else {
  839. return Result::error("删除失败", 0);
  840. }
  841. }
  842. /**
  843. * 更新皮肤
  844. * @param array $data
  845. * @return array
  846. */
  847. public function updateTemplate(array $data): array
  848. {
  849. unset($data['user_id']);
  850. $template = Template::where('id', $data['id'])
  851. ->first();
  852. if (empty($template)) {
  853. return Result::error("此皮肤不存在!", 0);
  854. }
  855. $template = Template::where('id', '!=', $data['id'])
  856. ->where(function ($query) use ($data) {
  857. $query->where('template_name', $data['template_name'])
  858. ->orWhere('template_id', $data['template_id']);
  859. })
  860. ->first();
  861. if ($template) {
  862. return Result::error("皮肤名称或者皮肤编号已存在,不可编辑!", 0);
  863. }
  864. $data['template_keyword'] = json_encode($data['template_keyword']) ?? '';
  865. $page_type = json_decode($data['page_type'], true);
  866. $data['page_type'] = json_encode(array_values(array_unique($page_type)));
  867. $result = Template::where('id', $data['id'])->update($data);
  868. var_dump($result, '-------------------update');
  869. if (!$result) {
  870. return Result::error("更新失败", 0);
  871. } else {
  872. return Result::success('更新成功');
  873. }
  874. }
  875. /**
  876. * 根据皮肤名称-获取所有皮肤
  877. * @param array $data
  878. * @return array
  879. */
  880. public function getAllTemplate(array $data): array
  881. {
  882. $result = Template::where('template_name','like','%'.$data['template_name'].'%')->get();
  883. if(empty($result)){
  884. return Result::error("暂无皮肤", 0);
  885. }
  886. return Result::success($result);
  887. }
  888. /**
  889. * 获取通栏列表
  890. * @param array $data
  891. * @return array
  892. */
  893. public function getSectorList(array $data): array
  894. {
  895. $where = [];
  896. if (isset($data['template_class_id']) && !empty($data['template_class_id'])) {
  897. $where['template_class_id'] = $data['template_class_id'];
  898. }
  899. if (isset($data['template_id']) && !empty($data['template_id'])) {
  900. array_push($where, ['template_id', $data['template_id']]);
  901. }
  902. if (isset($data['sector_name']) && !empty($data['sector_name'])) {
  903. array_push($where, ['sector.sector_name', 'like', '%' . $data['sector_name'] . '%']);
  904. }
  905. if (isset($data['width']) && !empty($data['width']) && isset($data['height']) && !empty($data['height'])){
  906. $size_id = Size::where('width','like','%' .$data['width'].'%')->where('height','like','%' .$data['height'].'%')->pluck('id');
  907. } else if( isset($data['width']) && !empty($data['width'])){
  908. $size_id = Size::where('width','like','%' .$data['width'].'%')->pluck('id');
  909. }else if( isset($data['height']) && !empty($data['height'])){
  910. $size_id = Size::where('height','like','%' .$data['height'].'%')->pluck('id');
  911. }else{
  912. $size_id = [];
  913. }
  914. // $size_id = $size_id->toArray();
  915. $result = Sector::where($where)
  916. ->when(!empty($size_id), function ($query) use ($size_id) {
  917. $query->whereIn('size_id', $size_id);
  918. })
  919. ->leftJoin('size', 'size.id', '=', 'sector.size_id')
  920. ->select('sector.*', 'size.width','size.height')
  921. ->orderBy('sector.id', 'desc')
  922. ->paginate($data['page_size'], ['*'], 'page', $data['page']);
  923. return Result::success($result);
  924. }
  925. /**
  926. * 获取通栏详情
  927. * @param array $data
  928. * @return array
  929. */
  930. public function getSectorInfo(array $data): array
  931. {
  932. $where = [];
  933. $where[] = ['sector.id', '=', $data['id']];
  934. $result = Sector::where($where)
  935. ->leftJoin('size', 'size.id', '=', 'sector.size_id')
  936. ->select('sector.*', 'size.width','size.height')
  937. ->orderBy('sector.id', 'desc')
  938. ->first();
  939. if(empty($result)){
  940. return Result::error('此通栏不存在!');
  941. }
  942. return Result::success($result);
  943. }
  944. /**
  945. * 获取经纬度信息
  946. * @return void
  947. */
  948. public function getIpInfo(array $data) :array
  949. {
  950. $client_ip = isset($data['ip']) && $data['ip']??$_SERVER['REMOTE_ADDR'];
  951. // 使用 IPinfo 服务获取 IP 信息
  952. // $api_url = "https://ipinfo.io/{$client_ip}/json";
  953. //http://ip-api.com/json/117.136.12.79?lang=zh-cn
  954. $api_url = "http://ip-api.com/json/{$client_ip}/?lang=zh-cn";
  955. $ch = curl_init($api_url);
  956. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  957. $response = curl_exec($ch);
  958. curl_close($ch);
  959. // 解析 JSON 响应
  960. $ip_info = json_decode($response, true);
  961. // 提取地址和经纬度
  962. if ($ip_info) {
  963. // $latitude = explode(',', $ip_info['loc'])[0];
  964. // $longitude = explode(',', $ip_info['loc'])[1];
  965. $ip_info['latitude'] = $ip_info['lat'];
  966. $ip_info['longitude'] = $ip_info['lon'];
  967. return Result::success($ip_info);
  968. } else {
  969. $data['ip'] = '101.254.114.212';
  970. $this->getIpinfo(["ip"=>$data['ip']]);
  971. }
  972. }
  973. /**
  974. * 获取天气
  975. * @param array $data
  976. * @return array
  977. */
  978. public function getWeatherInfo(array $data) :array
  979. {
  980. $month = $data['month'] ?? date('m');
  981. $day = $data['day'] ?? date('d');
  982. // 使用缓存键
  983. $cacheKey = "tsbb_data_weather_{$month}_{$day}";
  984. // 尝试从缓存获取数据
  985. $container = \Hyperf\Context\ApplicationContext::getContainer();
  986. $cache = $container->get(\Psr\SimpleCache\CacheInterface::class);
  987. if ($cachedData = $cache->get($cacheKey)) {
  988. return Result::success(unserialize($cachedData));
  989. }
  990. $location = $data['latitude'].":".$data['longitude'];
  991. $api_url = "https://api.seniverse.com/v3/weather/now.json?key=".\Hyperf\Support\env('WEATHER_KEY')."&location=".$location."&language=zh-Hans&unit=c";
  992. $ch = curl_init($api_url);
  993. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  994. $response = curl_exec($ch);
  995. curl_close($ch);
  996. // 解析 JSON 响应
  997. $WeatherInfo = json_decode($response, true);
  998. // 缓存结果,设置1小时过期
  999. $cache->set($cacheKey, serialize($WeatherInfo), 3600);
  1000. if($WeatherInfo){
  1001. return Result::success($WeatherInfo);
  1002. }else{
  1003. return Result::error("获取天气失败", 0);
  1004. }
  1005. }
  1006. /**
  1007. * 获取农历信息
  1008. * @return void
  1009. */
  1010. public function getCalendar(array $data) :array
  1011. {
  1012. $calendar = new Calendar();
  1013. $result = $calendar->solar($data['year'], $data['month'], $data['day'], $data['hour']); // 阳历
  1014. return Result::success($result);
  1015. }
  1016. /**
  1017. * 添加通栏
  1018. * @param array $data
  1019. * @return array
  1020. */
  1021. public function addSector(array $data): array
  1022. {
  1023. unset($data['user_id']);
  1024. // 对传入的 page_type 数组进行去重、转换为整数并重新索引
  1025. $data['page_type'] = array_values(array_unique(array_map('intval', $data['page_type'])));
  1026. $data['page_type'] = json_encode($data['page_type']);
  1027. $template = Template::where('template_id', $data['template_id'])
  1028. ->whereRaw("JSON_CONTAINS(template.page_type, ?)", [$data['page_type']])
  1029. ->first();
  1030. if(empty($template)){
  1031. return Result::error('皮肤不存在!');
  1032. }
  1033. // 皮肤相关信息
  1034. $data['template_name'] = $template['template_name'];
  1035. $data['template_id'] = $template['template_id'];
  1036. // 风格相关信息
  1037. $template_class = TemplateClass::where('class_id',$template['template_class_id'])->first();
  1038. if(empty($template_class)){
  1039. return Result::error('所属风格不存在!');
  1040. }
  1041. // $data['sector_keyword'] = json_encode($data['sector_keyword']);
  1042. $data['template_class_id'] = $template_class['class_id'];
  1043. $data['template_class_name'] = $template_class['name'];
  1044. $result = Sector::insertGetId($data);
  1045. if(empty($result)){
  1046. return Result::error('添加失败');
  1047. }
  1048. return Result::success($result);
  1049. }
  1050. /**
  1051. * 删除通栏
  1052. * @param array $data
  1053. * @return array
  1054. */
  1055. public function delSector(array $data): array
  1056. {
  1057. $component_id = Component::where('sector_id', $data['id'])->pluck('id')->toArray();
  1058. if(!empty($component_id)){
  1059. return Result::error('请先删除相关组件!');
  1060. }
  1061. $result = Sector::where('id', $data['id'])->delete();
  1062. if ($result == 1) {
  1063. return Result::success('删除成功');
  1064. } else {
  1065. return Result::error('删除失败');
  1066. }
  1067. }
  1068. /**
  1069. * 修改通栏
  1070. * @param array $data
  1071. * @return array
  1072. */
  1073. public function updateSector(array $data): array
  1074. {
  1075. unset($data['user_id']);
  1076. $sector = Sector::where('id', $data['id'])->first();
  1077. if(empty($sector)){
  1078. return Result::error('通栏不存在!');
  1079. }
  1080. // 对传入的 page_type 数组进行去重、转换为整数并重新索引
  1081. $data['page_type'] = array_values(array_unique(array_map('intval', $data['page_type'])));
  1082. $data['page_type'] = json_encode($data['page_type']);
  1083. $template = Template::where('template_id', $data['template_id'])
  1084. ->whereRaw("JSON_CONTAINS(template.page_type, ?)", [$data['page_type']])
  1085. ->first();
  1086. if(empty($template)){
  1087. return Result::error('皮肤不存在!');
  1088. }
  1089. // 皮肤相关信息
  1090. $data['template_name'] = $template['template_name'];
  1091. $data['template_id'] = $template['template_id'];
  1092. // 风格相关信息
  1093. $template_class = TemplateClass::where('class_id',$template['template_class_id'])->first();
  1094. if(empty($template_class)){
  1095. return Result::error('所属风格不存在!');
  1096. }
  1097. // $data['sector_keyword'] = json_encode($data['sector_keyword']);
  1098. $data['template_class_id'] = $template_class['class_id'];
  1099. $data['template_class_name'] = $template_class['name'];
  1100. $result = Sector::where('id', $data['id'])->update($data);
  1101. if ($result == 1) {
  1102. return Result::success('修改成功');
  1103. } else {
  1104. return Result::error('修改失败');
  1105. }
  1106. }
  1107. public function getComponentList(array $data): array
  1108. {
  1109. var_dump($data, '---------');
  1110. $where = [];
  1111. // $where[] = ['sector.id', '=', $data['id']];
  1112. if (!empty($data['template_class_id'])) {
  1113. $where['template_class.id'] = $data['template_class_id'];
  1114. }
  1115. if (!empty($data['component_name'])) {
  1116. $where['component.component_name'] = $data['component_name'];
  1117. }
  1118. if (!empty($data['sector_id'])) {
  1119. $where['sector.id'] = $data['sector_id'];
  1120. };
  1121. $result = Component::where($where)
  1122. ->leftJoin('template', 'template.id', '=', 'component.template_id')
  1123. ->leftJoin('template_class', 'template_class.id', '=', 'template.template_class_id') // 添加这一行
  1124. ->leftJoin('sector', 'sector.id', '=', 'component.sector_id')
  1125. ->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') // 修改这一行)
  1126. ->orderBy('sector.updated_at', 'desc')
  1127. ->orderBy('sector.created_at', 'desc')
  1128. ->paginate($data['page_size'], ['*'], 'mypage_name', $data['page']);
  1129. return Result::success($result);
  1130. }
  1131. public function getComponentInfo(array $data): array
  1132. {
  1133. $where = [];
  1134. $result = Component::where($where)
  1135. ->leftJoin('template', 'template.id', '=', 'component.template_id')
  1136. ->leftJoin('template_class', 'template_class.id', '=', 'template.template_class_id') // 添加这一行
  1137. ->leftJoin('sector', 'sector.id', '=', 'component.sector_id')
  1138. ->select('template_class.name as template_class_name', 'template.template_name as template_name', 'sector.sector_name as sector_name', 'component.*')
  1139. ->get();
  1140. return Result::success($result);
  1141. }
  1142. public function addComponent(array $data): array
  1143. {
  1144. unset($data['user_id']);
  1145. $result = Component::insertGetId($data);
  1146. if ($result) {
  1147. return Result::success($result);
  1148. } else {
  1149. return Result::error('添加失败');
  1150. }
  1151. }
  1152. public function delComponent(array $data): array
  1153. {
  1154. $result = Component::where('id', $data['id'])->delete();
  1155. return Result::success($result);
  1156. }
  1157. public function updateComponent(array $data): array
  1158. {
  1159. $result = Component::where('id', $data['id'])->update($data);
  1160. return Result::success($result);
  1161. }
  1162. public function getWebsiteTemplateInfo(array $data)
  1163. {
  1164. $where = [];
  1165. if (isset($data['id'])) {
  1166. $where[] = ['id', '=', $data['id']];
  1167. }
  1168. $result = WebsiteTemplateInfo::where($where)
  1169. ->leftJoin('website', 'website_template_info.website_id', '=', 'website.id')
  1170. ->leftJoin('website_template', 'website_template_info.template_id', '=', 'website_template.id')
  1171. ->select('website_template_info.*', 'website.website_name')
  1172. ->get();
  1173. if ($result) {
  1174. return Result::success($result);
  1175. } else {
  1176. return Result::error('暂无数据');
  1177. }
  1178. }
  1179. /**
  1180. * 自助建站----1.获取页面类型回显
  1181. * @param array $data
  1182. * @return array
  1183. */
  1184. public function getWebPageType(array $data): array
  1185. {
  1186. // 1:图片 2:文字 3:底部
  1187. $friend_link = Link::where('website_id', $data['website_id'])->where('type',3)->get();
  1188. if(empty($friend_link->toArray())){
  1189. return Result::error('暂无友情链接数据!');
  1190. }
  1191. $footer_category = FooterCategory::where('website_id', $data['website_id'])->get();
  1192. if(empty($footer_category->toArray())){
  1193. return Result::error('暂无底部导航数据!');
  1194. }
  1195. $page_type = WebsiteTemplateInfo::where('website_id', $data['website_id'])->value('page_type');
  1196. $page_type = json_decode($page_type,true);
  1197. // '网站页面类型(存数组)1:首页 2:分类页 3:列表页 4:详情页 5:搜索页 6:特殊列表 7:特殊详情页
  1198. if(isset($page_type) && in_array(5,$page_type)){
  1199. $page_type = 1;
  1200. }else{
  1201. $page_type = 0;
  1202. }
  1203. $result = [
  1204. 'friend_link' => $friend_link,
  1205. 'footer_category' => $footer_category,
  1206. 'page_type' => $page_type,
  1207. ];
  1208. return Result::success($result);
  1209. }
  1210. /**
  1211. * 自助建站----2.添加页面类型
  1212. * @param array $data
  1213. * @return array
  1214. */
  1215. public function addWebPageType(array $data): array
  1216. {
  1217. // return Result::success($data);
  1218. // 1:首页 2:频道页 3:列表页 4:详情页 5:搜索页 6:特殊列表 7:特殊详情页'
  1219. if($data['is_search'] == 1){
  1220. $page_type = json_encode([1,2,3,4,5,6,7]);
  1221. }else{
  1222. $page_type = json_encode([1,2,3,4,6,7]);
  1223. }
  1224. // 0:未构建;1:已填写基础信息;2:已选择模板; action_id
  1225. $website_template_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1226. // return Result::success($website_template_info);
  1227. // return Result::success(empty($website_template_info));
  1228. if(empty($website_template_info)){
  1229. $result = WebsiteTemplateInfo::insertGetId(['website_id' => $data['website_id'], 'page_type' => $page_type, 'user_id' => $data['user_id']]);
  1230. }else{
  1231. if($website_template_info['action_id'] == 2){
  1232. return Result::error('网站已应用,不可再次修改!');
  1233. }
  1234. $result = WebsiteTemplateInfo::where('website_id', $data['website_id'])->update(['page_type' => $page_type],['action_id' => 1,'user_id' => $data['user_id']]);
  1235. }
  1236. if ($result) {
  1237. return Result::success($result);
  1238. } else {
  1239. return Result::error('添加失败');
  1240. }
  1241. }
  1242. /**
  1243. * 自助建站---流程---3.选择皮肤列表
  1244. * @param array $data
  1245. * @return array
  1246. */
  1247. public function getWebsiteTemplateList(array $data): array
  1248. {
  1249. $where = [];
  1250. if(isset($data['template_class_id']) && $data['template_class_id']){
  1251. $where[] = ['template_class_id', '=', $data['template_class_id']];
  1252. }
  1253. // 0:未构建 1:未应用 2:已应用 status
  1254. $templste_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1255. if(empty($templste_info)){
  1256. return Result::error('请先填写网站基础信息!');
  1257. }
  1258. if($templste_info['status'] == 2){
  1259. return Result::error('网站已应用,不可再次修改!');
  1260. }
  1261. if(isset($data['keyword']) && !empty($data['keyword'])){
  1262. if (is_array($data['keyword'])) {
  1263. // 拼接多个模糊查询条件
  1264. $query = Template::query();
  1265. foreach ($data['keyword'] as $kw) {
  1266. $escapedKw = addcslashes($kw, '%_'); // 转义通配符
  1267. $query->WhereRaw("JSON_EXTRACT(template_keyword, '$[*]') LIKE ?", ["%$escapedKw%"]);
  1268. }
  1269. } else {
  1270. $escapedKeyword = addcslashes($data['keyword'], '%_'); // 转义通配符
  1271. $where[] = [DB::raw("JSON_EXTRACT(template_keyword, '$[*]') LIKE ?"), "%$escapedKeyword%"];
  1272. }
  1273. }
  1274. $template_id = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first(['template_id', 'page_type']);
  1275. // return Result::success($template_id);
  1276. if(!empty($template_id['page_type']) && $templste_info['page_type'] != '[]' && is_string($templste_info['page_type'])){
  1277. $template_page = is_string($templste_info['page_type']) ? json_decode($templste_info['page_type'], true) : [];
  1278. }else{
  1279. $template_page = [];
  1280. }
  1281. if(!empty($template_id['template_id'])){
  1282. $result['template_id'] = $templste_info['template_id'];
  1283. }
  1284. // 确保 $template_page 为有效数据,避免 SQL 注入风险,这里使用参数绑定
  1285. $template_page_str = json_encode($template_page);
  1286. if (isset($query)) {
  1287. $result['template'] = $query->where($where)
  1288. ->whereRaw("JSON_CONTAINS(template.page_type, ?)", [$template_page_str])
  1289. ->paginate($data['page_size'], ['*'], 'page', $data['page']);
  1290. } else {
  1291. $result['template'] = Template::where($where)
  1292. ->whereRaw("JSON_CONTAINS(template.page_type, ?)", [$template_page_str])
  1293. ->paginate($data['page_size'], ['*'], 'page', $data['page']);
  1294. }
  1295. if (empty($result['template']['data'])) {
  1296. $result['template'] = Template::orderBy('updated_at','desc')
  1297. ->paginate($data['page_size'], ['*'], 'page', $data['page']);
  1298. }
  1299. $template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->value('template_id');
  1300. if($template){
  1301. $result['template_id'] = $template;
  1302. }else{
  1303. $result['template_id'] = 0;
  1304. }
  1305. return Result::success($result);
  1306. }
  1307. /**
  1308. * 自助建站---流程---4.选择皮肤
  1309. * @param array $data
  1310. * @return array
  1311. */
  1312. public function chooseWebsiteTemplate(array $data): array
  1313. {
  1314. $website_template_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1315. if(empty($website_template_info)){
  1316. return Result::error('请先填写网站基础信息!');
  1317. }
  1318. // 0:未构建 1:未应用 2:已应用 status
  1319. if($website_template_info['status'] == 2){
  1320. return Result::error('网站已应用,不可再次修改!');
  1321. }
  1322. $template = Template::where('id', $data['template_id'])->first();
  1323. if(empty($template)){
  1324. return Result::error('未查询到皮肤!');
  1325. }
  1326. // 0:未构建;1:已填写基础信息;2:已选择模板; action_id
  1327. $web_template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->update(['template_id' => $data['template_id'],'action_id' => 2]);
  1328. if ($web_template) {
  1329. return Result::success($web_template);
  1330. } else {
  1331. return Result::error('选择皮肤失败!');
  1332. }
  1333. }
  1334. /**
  1335. * 自助建站---流程---5.保存模版
  1336. * @param array $data
  1337. * @return array
  1338. */
  1339. public function saveWebsiteTemplate(array $data): array
  1340. {
  1341. $website_template_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1342. if(empty($website_template_info)){
  1343. return Result::error('请先填写网站基础信息!');
  1344. }
  1345. // 0:未构建 1:未应用 2:已应用 status
  1346. if($website_template_info['status'] == 2){
  1347. return Result::error('网站已应用,不可再次保存!');
  1348. }
  1349. if($website_template_info['action_id'] != 2){
  1350. return Result::error('请完成之前的步骤!');
  1351. }
  1352. $template_info = WebsiteTemplate::where('website_id', $data['website_id'])->first();
  1353. var_dump($data['template_data']);
  1354. if(empty($template_info)){
  1355. $template = WebsiteTemplate::insertGetId([
  1356. 'website_id' => $data['website_id'],
  1357. 'template_data' => $data['template_data']
  1358. ]);
  1359. }else{
  1360. $template = WebsiteTemplate::where('website_id', $data['website_id'])->update(['template_data' => $data['template_data']]);
  1361. }
  1362. if(empty($template)){
  1363. return Result::error('保存失败!');
  1364. }else{
  1365. return Result::success($template);
  1366. }
  1367. }
  1368. }