PublicRpcService.php 52 KB

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