PublicRpcService.php 54 KB

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