PublicRpcService.php 55 KB

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