PublicRpcService.php 50 KB

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