PublicRpcService.php 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527
  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'])
  447. ->orderBy('department', 'desc')
  448. ->first();
  449. return Result::success($result);
  450. }
  451. public function getPidZhinengbumen(array $data): array
  452. {
  453. if (empty($data['pid'])) {
  454. $data['pid'] = 0;
  455. }
  456. $result = Department::where('pid', $data['pid'])->get();
  457. return Result::success($result);
  458. }
  459. public function modZhinengbumen(array $data): array
  460. {
  461. $result = Department::where('id', $data['id'])->update($data);
  462. if (empty($result)) {
  463. return Result::error("修改失败", 0);
  464. } else {
  465. return Result::success();
  466. }
  467. }
  468. /**
  469. * 查询职能列表
  470. * @param array $data
  471. * @return array
  472. */
  473. public function getDepartment(array $data): array
  474. {
  475. $where = [];
  476. if (isset($data['pid'])) {
  477. $where = [
  478. 'pid' => $data['pid'] ?? 0
  479. ];
  480. }
  481. $result = Department::when(!empty($where), function ($query) use ($where) {
  482. $query->where($where);
  483. })->orderBy("sort", "desc")->get();
  484. if (empty($result)) {
  485. return Result::error("查询失败", 0);
  486. } else {
  487. return Result::success($result);
  488. }
  489. }
  490. /**
  491. * 获取所有的buckets
  492. * @param array $data
  493. * @return array
  494. */
  495. public function getBuckets(array $data): array
  496. {
  497. $result = new MinioService();
  498. // 调用服务层的方法获取存储桶列表
  499. $bucketsResponse = $result->listBuckets();
  500. // 直接返回服务层生成的响应
  501. return Result::success($bucketsResponse['data']);
  502. }
  503. /**
  504. * 上传文件
  505. * @param array $data
  506. * @return array
  507. */
  508. public function uploadFile(array $data): array
  509. {
  510. $result = new MinioService();
  511. $rep = $result->uploadFile($data);
  512. if ($rep['code'] == 200) {
  513. return Result::success($rep['data']);
  514. } else {
  515. return Result::error("上传失败!");
  516. }
  517. }
  518. /**
  519. * 黑名单管理
  520. * @param array $data
  521. * @return array
  522. */
  523. public function getBlackWordList(array $data): array
  524. {
  525. $result = BlackWord::when($data, function ($query) use ($data) {
  526. if (isset($data['name']) && $data['name']) {
  527. $query->where('black_word.name', 'like', '%' . $data['name'] . '%');
  528. }
  529. })->orderBy('black_word.id', 'desc')
  530. ->paginate(
  531. intval($data['pageSize']),
  532. [
  533. 'black_word.*',
  534. ],
  535. 'page',
  536. intval($data['page'])
  537. );
  538. $count = $result->total();
  539. $returnData = [
  540. 'rows' => $result->items(),
  541. 'count' => $count
  542. ];
  543. return Result::success($returnData);
  544. }
  545. /**
  546. * 添加黑名单
  547. * @param array $data
  548. * @return array
  549. */
  550. public function addBlackWord(array $data): array
  551. {
  552. Db::beginTransaction();
  553. try {
  554. $info = BlackWord::where(['name' => $data['name']])->first();
  555. if ($info) {
  556. Db::rollBack();
  557. return Result::error("该黑名单已存在", 0);
  558. }
  559. $data['type'] = 10;
  560. $result = BlackWord::insertGetId($data);
  561. $redisKey = 'black_word';
  562. $this->redis->sAdd($redisKey, $data['name']);
  563. Db::commit();
  564. return Result::success(["id" => $result]);
  565. } catch (\Exception $e) {
  566. Db::rollBack();
  567. return Result::error("创建失败" . $e->getMessage(), 0);
  568. }
  569. }
  570. /**
  571. * 删除黑名单
  572. * @param array $data
  573. * @return array
  574. */
  575. public function delBlackWord(array $data): array
  576. {
  577. Db::beginTransaction();
  578. try {
  579. BlackWord::where(['name' => $data['name']])->delete();
  580. $redisKey = 'black_word';
  581. $this->redis->sRem($redisKey, $data['name']);
  582. Db::commit();
  583. return Result::success([]);
  584. } catch (\Exception $e) {
  585. Db::rollBack();
  586. return Result::error("删除失败" . $e->getMessage(), 0);
  587. }
  588. }
  589. /**
  590. * 修改违禁词
  591. * @param array $data
  592. * @return array
  593. */
  594. public function upBlackWord(array $data): array
  595. {
  596. Db::beginTransaction();
  597. try {
  598. $checkInfo = BlackWord::where(['name' => $data['name']])->first();
  599. if ($checkInfo) {
  600. Db::rollBack();
  601. return Result::error("该违禁词已经存在", 0);
  602. }
  603. $blackWordInfo = BlackWord::where(['id' => $data['id']])->first();
  604. if ($blackWordInfo) {
  605. //先删除redis
  606. $blackWordInfo = $blackWordInfo->toArray();
  607. $redisKey = 'black_word';
  608. $this->redis->sRem($redisKey, $blackWordInfo['name']);
  609. $this->redis->sAdd($redisKey, $data['name']);
  610. BlackWord::where(['id' => $data['id']])->update(['name' => $data['name']]);
  611. Db::commit();
  612. return Result::success([]);
  613. } else {
  614. Db::rollBack();
  615. return Result::error("系统错误", 0);
  616. }
  617. } catch (\Exception $e) {
  618. Db::rollBack();
  619. return Result::error("修改失败" . $e->getMessage(), 0);
  620. }
  621. }
  622. /**
  623. * 获取风格
  624. * @return void
  625. */
  626. public function getTemplateClassList(array $data): array
  627. {
  628. $where = [];
  629. if (isset($data['name']) && $data['name']) {
  630. array_push($where, ['template_class.name', 'like', '%' . $data['name'] . '%']);
  631. }
  632. if (isset($data['keyword']) && $data['keyword']) {
  633. array_push($where, ['template_class.keyword', 'like', '%' . $data['keyword'] . '%']);
  634. }
  635. $template = TemplateClass::when($where, function ($query) use ($where) {
  636. $query->where($where);
  637. });
  638. $count = $template->count();
  639. // $countQuery = clone $template;
  640. $row = $template
  641. ->leftJoin('template', function ($join) {
  642. $join->on('template_class.class_id', '=', 'template.template_class_id');
  643. })
  644. ->select('template_class.*', DB::raw('COUNT(template.template_class_id) as template_count'))
  645. ->groupBy('template_class.id')
  646. ->orderBy('template_class.id', 'desc')
  647. ->offset(($data['page'] - 1) * $data['pageSize'])
  648. ->limit($data['pageSize'])
  649. ->get();
  650. $result = [
  651. 'rows' => $row,
  652. 'count' => $count,
  653. ];
  654. if ($row->isEmpty()) {
  655. return Result::error("暂无风格", 0);
  656. } else {
  657. return Result::success($result);
  658. }
  659. }
  660. /**
  661. * 添加风格
  662. * @param
  663. * @return void
  664. */
  665. public function addTemplateClass(array $data): array
  666. {
  667. $data['keyword'] = json_encode($data['keyword']);
  668. $template_class = TemplateClass::where('name', $data['name'])
  669. ->orWhere('class_id', $data['class_id'])
  670. ->first();
  671. if ($template_class) {
  672. return Result::error("风格名称或者风格编号已存在,不可添加!", 0);
  673. }
  674. $result = TemplateClass::insertGetId($data);
  675. if (empty($result)) {
  676. return Result::error("创建风格失败", 0);
  677. } else {
  678. return Result::success(["id" => $result]);
  679. }
  680. }
  681. /**
  682. * 更新风格
  683. * @param array $data
  684. * @return array
  685. */
  686. public function upTemplateClass(array $data): array
  687. {
  688. $where = [
  689. 'id' => $data['id'],
  690. ];
  691. $template_class = TemplateClass::where($where)->first();
  692. if (empty($template_class)) {
  693. return Result::error("未查询到风格", 0);
  694. }
  695. if ($template_class->type == 1) {
  696. return Result::error("默认风格不能修改", 0);
  697. }
  698. $template = TemplateClass::where('id', '!=', $data['id'])
  699. // ->where(['name' => $data['name']])
  700. // ->orWhere(['class_id' => $data['class_id']])
  701. ->where(function ($query) use ($data) {
  702. $query->where('name', $data['name'])
  703. ->orWhere('class_id', $data['class_id']);
  704. })
  705. ->first();
  706. if ($template) {
  707. return Result::error("风格名称或者风格编号已存在,不可编辑!", 0);
  708. }
  709. $updateData = [
  710. 'name' => $data['name'],
  711. 'keyword' => json_encode($data['keyword']),
  712. 'class_id' => $data['class_id'],
  713. ];
  714. $result = TemplateClass::where($where)->update($updateData);
  715. if (empty($result)) {
  716. return Result::error("更新失败", 0);
  717. } else {
  718. return Result::success($result);
  719. }
  720. }
  721. /**
  722. * 删除风格
  723. * @param array $data
  724. * @return array
  725. */
  726. public function delTemplateClass(array $data): array
  727. {
  728. $where = [
  729. 'id' => $data['id'],
  730. ];
  731. $template_class = TemplateClass::where($where)->first();
  732. if (empty($template_class)) {
  733. return Result::error("未查询到风格", 0);
  734. }
  735. if ($template_class->type == 1) {
  736. return Result::error("默认风格不能删除", 0);
  737. }
  738. $template = Template::where('template_class_id', $template_class['class_id'])->get();
  739. if (!empty($template->all())) {
  740. try {
  741. // 默认风格 1
  742. Db::beginTransaction();
  743. $template = Template::where('template_class_id', $template['class_id'])->update(['template_class_id' => 1]);
  744. if (empty($template)) {
  745. Db::rollBack();
  746. return Result::error("删除失败", 0);
  747. }
  748. $result = TemplateClass::where($where)->delete();
  749. } catch (\Exception $e) {
  750. return Result::error("删除失败" . $e->getMessage(), 0);
  751. }
  752. } else {
  753. $result = TemplateClass::where('id', $data['id'])->delete();
  754. }
  755. if (empty($result)) {
  756. return Result::error("删除失败", 0);
  757. }
  758. return Result::success($result);
  759. }
  760. /**
  761. * 根据风格名称-获取获取所有风格
  762. * @param array $data
  763. * @return array
  764. */
  765. public function getTemplateClass(array $data): array
  766. {
  767. $where = [];
  768. if (!empty($data['template_name'])) {
  769. $where['name'] = $data['template_name'];
  770. }
  771. $result = TemplateClass::where($where)->get();
  772. if (empty($result)) {
  773. return Result::error("暂无风格", 0);
  774. }
  775. return Result::success($result);
  776. }
  777. /**
  778. * 获取皮肤列表
  779. * @param array $data
  780. * @return array
  781. */
  782. public function getTemplateList(array $data): array
  783. {
  784. $where = [];
  785. if (!empty($data['template_class_id'])) {
  786. $where['template_class_id'] = $data['template_class_id'];
  787. }
  788. if (!empty($data['template_name'])) {
  789. array_push($where, ['template_name', 'like', '%' . $data['template_name'] . '%']);
  790. }
  791. if (!empty($data['template_keyword'])) {
  792. array_push($where, ['template_keyword', 'like', '%' . $data['template_keyword'] . '%']);
  793. }
  794. $result = Template::where($where)
  795. ->leftJoin('template_class', 'template.template_class_id', 'template_class.class_id')
  796. ->select('template.*', 'template_class.name as template_class_name')
  797. ->orderBy('template.id', 'desc')
  798. ->paginate($data['page_size'], ['*'], 'mypage_name', $data['page']);
  799. if (empty($result)) {
  800. return Result::error("暂无皮肤", 0);
  801. }
  802. return Result::success($result);
  803. }
  804. public function getTemplateInfo(array $data): array
  805. {
  806. $result = Template::where('template.id', $data['id'])
  807. ->leftJoin('template_class', 'template.template_class_id', 'template_class.class_id')
  808. ->select('template.*', 'template_class.name as template_class_name')
  809. ->first();
  810. if (empty($result)) {
  811. return Result::error("暂无皮肤", 0);
  812. }
  813. return Result::success($result);
  814. }
  815. /**
  816. * 添加皮肤
  817. * @param array $data
  818. * @return array
  819. */
  820. public function addTemplate(array $data): array
  821. {
  822. var_dump($data);
  823. unset($data['user_id']);
  824. $template = Template::where('template_name', $data['template_name'])
  825. ->orWhere('template_id', $data['template_id'])
  826. ->first();
  827. if ($template) {
  828. return Result::error("皮肤名称或者皮肤编号已存在,不可添加!", 0);
  829. }
  830. $data['template_keyword'] = json_encode($data['template_keyword']) ?? '';
  831. $page_type = json_decode($data['page_type'], true);
  832. $data['page_type'] = json_encode(array_values(array_unique($page_type)));
  833. $result = Template::insertGetId($data);
  834. if (empty($result)) {
  835. return Result::error("创建失败", 0);
  836. } else {
  837. return Result::success($result);
  838. }
  839. }
  840. /**
  841. * 删除皮肤
  842. * @param array $data
  843. * @return array
  844. */
  845. public function delTemplate(array $data): array
  846. {
  847. $rector = Sector::where('template_id', $data['id'])->first();
  848. if (!empty($rector)) {
  849. return Result::error("此皮肤已被绑定通栏,不可删除", 0);
  850. }
  851. $template = WebsiteTemplateInfo::where('template_id', $data['id'])->first();
  852. if (empty($template)) {
  853. return Result::error("此皮肤已被绑定网站,不可删除", 0);
  854. }
  855. if ($template->type == 1) {
  856. return Result::error("默认皮肤不能删除", 0);
  857. }
  858. $result = Template::where('id', $data['id'])->delete();
  859. var_dump($result, '-------------------delete');
  860. if ($result) {
  861. return Result::success($result);
  862. } else {
  863. return Result::error("删除失败", 0);
  864. }
  865. }
  866. /**
  867. * 更新皮肤
  868. * @param array $data
  869. * @return array
  870. */
  871. public function updateTemplate(array $data): array
  872. {
  873. unset($data['user_id']);
  874. $template = Template::where('id', $data['id'])
  875. ->first();
  876. if (empty($template)) {
  877. return Result::error("此皮肤不存在!", 0);
  878. }
  879. $template = Template::where('id', '!=', $data['id'])
  880. ->where(function ($query) use ($data) {
  881. $query->where('template_name', $data['template_name'])
  882. ->orWhere('template_id', $data['template_id']);
  883. })
  884. ->first();
  885. if ($template) {
  886. return Result::error("皮肤名称或者皮肤编号已存在,不可编辑!", 0);
  887. }
  888. $data['template_keyword'] = json_encode($data['template_keyword']) ?? '';
  889. $page_type = json_decode($data['page_type'], true);
  890. $data['page_type'] = json_encode(array_values(array_unique($page_type)));
  891. $result = Template::where('id', $data['id'])->update($data);
  892. var_dump($result, '-------------------update');
  893. if (!$result) {
  894. return Result::error("更新失败", 0);
  895. } else {
  896. return Result::success('更新成功');
  897. }
  898. }
  899. /**
  900. * 获取所有尺寸
  901. * @param array $data
  902. * @return array
  903. */
  904. public function getAllSize(array $data): array
  905. {
  906. if(isset($data['id']) && !empty($data['id'])){
  907. $result = Size::where('id', $data['id'])->get()->all();
  908. }else{
  909. $result = Size::get()->all();
  910. }
  911. if(empty($result)){
  912. return Result::error("暂无尺寸", 0);
  913. }
  914. return Result::success($result);
  915. }
  916. /**
  917. * 根据皮肤名称-获取所有皮肤
  918. * @param array $data
  919. * @return array
  920. */
  921. public function getAllTemplate(array $data): array
  922. {
  923. $where = [];
  924. if (isset($data['template_class_id']) && !empty($data['template_class_id'])) {
  925. $where[] = ['template_class_id', $data['template_class_id']];
  926. }
  927. if (isset($data['template_name']) && !empty($data['template_name'])) {
  928. $where[] = ['template_name', 'like', '%' . $data['template_name'] . '%'];
  929. }
  930. $result = Template::where($where)->get()->all();
  931. if (empty($result)) {
  932. return Result::error("暂无皮肤", 0);
  933. }
  934. return Result::success($result);
  935. }
  936. /**
  937. * 获取通栏列表
  938. * @param array $data
  939. * @return array
  940. */
  941. public function getSectorList(array $data): array
  942. {
  943. $where = [];
  944. if (isset($data['template_class_id']) && !empty($data['template_class_id'])) {
  945. $where['template.template_class_id'] = $data['template_class_id'];
  946. }
  947. if (isset($data['template_id']) && !empty($data['template_id'])) {
  948. array_push($where, ['template.template_id', $data['template_id']]);
  949. }
  950. if (isset($data['sector_name']) && !empty($data['sector_name'])) {
  951. array_push($where, ['sector.sector_name', 'like', '%' . $data['sector_name'] . '%']);
  952. }
  953. if (isset($data['width']) && !empty($data['width']) && isset($data['height']) && !empty($data['height'])) {
  954. $size_id = Size::where('width', 'like', '%' . $data['width'] . '%')->where('height', 'like', '%' . $data['height'] . '%')->pluck('id');
  955. } else if (isset($data['width']) && !empty($data['width'])) {
  956. $size_id = Size::where('width', 'like', '%' . $data['width'] . '%')->pluck('id');
  957. } else if (isset($data['height']) && !empty($data['height'])) {
  958. $size_id = Size::where('height', 'like', '%' . $data['height'] . '%')->pluck('id');
  959. } else {
  960. $size_id = [];
  961. }
  962. $result = Sector::when(!empty($size_id), function ($query) use ($size_id) {
  963. $query->whereIn('size_id', $size_id);
  964. })
  965. ->leftJoin('size', 'size.id', '=', 'sector.size_id')
  966. ->leftJoin('template', 'template.template_id', '=', 'sector.template_id')
  967. ->leftJoin('template_class', 'template_class.class_id', '=', 'template.template_class_id')
  968. ->where($where)
  969. ->select('sector.*', 'size.width','size.height','template_class.name as class_name','template.template_name')
  970. ->orderBy('sector.id', 'desc')
  971. ->paginate($data['page_size'], ['*'], 'page', $data['page']);
  972. if(empty($result)){
  973. return Result::error('暂无通栏!');
  974. }
  975. return Result::success($result);
  976. }
  977. /**
  978. * 获取通栏详情
  979. * @param array $data
  980. * @return array
  981. */
  982. public function getSectorInfo(array $data): array
  983. {
  984. $where = [];
  985. $where[] = ['sector.id', '=', $data['id']];
  986. $result = Sector::where($where)
  987. ->leftJoin('size', 'size.id', '=', 'sector.size_id')
  988. ->select('sector.*', 'size.width', 'size.height')
  989. ->orderBy('sector.id', 'desc')
  990. ->first();
  991. if (empty($result)) {
  992. return Result::error('此通栏不存在!');
  993. }
  994. return Result::success($result);
  995. }
  996. /**
  997. * 获取经纬度信息
  998. * @return void
  999. */
  1000. public function getIpInfo(array $data): array
  1001. {
  1002. if (empty($data['ip'])) {
  1003. // 在RPC服务中,依赖$_SERVER是不可靠的,强制要求调用方传入IP
  1004. return Result::error('IP地址不能为空!');
  1005. }
  1006. $client_ip = $data['ip'];
  1007. try {
  1008. // 直接从容器获取 Guzzle 客户端实例
  1009. /** @var \GuzzleHttp\Client $client */
  1010. $client = \Hyperf\Context\ApplicationContext::getContainer()->get(Client::class);
  1011. // 使用新的 GeoJS API 地址
  1012. $api_url = "https://get.geojs.io/v1/ip/geo/{$client_ip}.json";
  1013. // 将 timeout 作为请求选项传递
  1014. $response = $client->get($api_url, ['timeout' => 5]);
  1015. if ($response->getStatusCode() !== 200) {
  1016. return Result::error('IP查询接口请求失败,状态码:' . $response->getStatusCode());
  1017. }
  1018. $ip_info = json_decode($response->getBody()->getContents(), true);
  1019. // GeoJS 的成功响应不包含 'status' 字段,直接检查关键数据是否存在
  1020. if ($ip_info && isset($ip_info['latitude']) && isset($ip_info['longitude'])) {
  1021. // GeoJS 返回的经纬度是字符串,转换为浮点数
  1022. $ip_info['latitude'] = floatval($ip_info['latitude']);
  1023. $ip_info['longitude'] = floatval($ip_info['longitude']);
  1024. return Result::success($ip_info);
  1025. }
  1026. // 如果返回的数据不符合预期,提供一个通用错误
  1027. $errorMessage = $ip_info['message'] ?? '获取IP地理位置信息失败';
  1028. return Result::error($errorMessage);
  1029. } catch (GuzzleException $e) {
  1030. // 这个异常会给出详细的网络错误信息,如超时、无法解析域名等
  1031. return Result::error('请求IP查询接口异常: ' . $e->getMessage());
  1032. } catch (\Throwable $e) {
  1033. return Result::error('获取IP信息时发生未知错误: ' . $e->getMessage());
  1034. }
  1035. }
  1036. /**
  1037. * 获取天气
  1038. * @param array $data
  1039. * @return array
  1040. */
  1041. public function getWeatherInfo(array $data): array
  1042. {
  1043. $month = $data['month'] ?? date('m');
  1044. $day = $data['day'] ?? date('d');
  1045. // 使用缓存键
  1046. $cacheKey = "tsbb_data_weather_{$month}_{$day}";
  1047. // 尝试从缓存获取数据
  1048. $container = \Hyperf\Context\ApplicationContext::getContainer();
  1049. $cache = $container->get(\Psr\SimpleCache\CacheInterface::class);
  1050. if ($cachedData = $cache->get($cacheKey)) {
  1051. return Result::success(unserialize($cachedData));
  1052. }
  1053. $location = $data['latitude'] . ":" . $data['longitude'];
  1054. $api_url = "https://api.seniverse.com/v3/weather/now.json?key=" . \Hyperf\Support\env('WEATHER_KEY') . "&location=" . $location . "&language=zh-Hans&unit=c";
  1055. $ch = curl_init($api_url);
  1056. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  1057. $response = curl_exec($ch);
  1058. curl_close($ch);
  1059. // 解析 JSON 响应
  1060. $WeatherInfo = json_decode($response, true);
  1061. // 缓存结果,设置1小时过期
  1062. $cache->set($cacheKey, serialize($WeatherInfo), 3600);
  1063. if ($WeatherInfo) {
  1064. return Result::success($WeatherInfo);
  1065. } else {
  1066. return Result::error("获取天气失败", 0);
  1067. }
  1068. }
  1069. /**
  1070. * 获取农历信息
  1071. * @return void
  1072. */
  1073. public function getCalendar(array $data): array
  1074. {
  1075. $calendar = new Calendar();
  1076. $result = $calendar->solar($data['year'], $data['month'], $data['day'], $data['hour']); // 阳历
  1077. return Result::success($result);
  1078. }
  1079. /**
  1080. * 添加通栏
  1081. * @param array $data
  1082. * @return array
  1083. */
  1084. public function addSector(array $data): array
  1085. {
  1086. unset($data['user_id']);
  1087. // 对传入的 page_type 数组进行去重、转换为整数并重新索引
  1088. $data['page_type'] = array_values(array_unique(array_map('intval', $data['page_type'])));
  1089. $data['page_type'] = json_encode($data['page_type']);
  1090. $template = Template::where('template_id', $data['template_id'])
  1091. ->first();
  1092. if (empty($template)) {
  1093. return Result::error('皮肤不存在!');
  1094. }
  1095. $sector_id = Sector::where('sector_id',$data['sector_id'])->first();
  1096. if(!empty($sector_id)){
  1097. return Result::error('通栏编号已存在!');
  1098. }
  1099. $result = Sector::insertGetId($data);
  1100. if (empty($result)) {
  1101. return Result::error('添加失败');
  1102. }
  1103. return Result::success($result);
  1104. }
  1105. /**
  1106. * 删除通栏
  1107. * @param array $data
  1108. * @return array
  1109. */
  1110. public function delSector(array $data): array
  1111. {
  1112. $component_id = Component::where('sector_id', $data['id'])->pluck('id')->toArray();
  1113. if (!empty($component_id)) {
  1114. return Result::error('请先删除相关组件!');
  1115. }
  1116. $result = Sector::where('id', $data['id'])->delete();
  1117. if ($result == 1) {
  1118. return Result::success('删除成功');
  1119. } else {
  1120. return Result::error('删除失败');
  1121. }
  1122. }
  1123. /**
  1124. * 修改通栏
  1125. * @param array $data
  1126. * @return array
  1127. */
  1128. public function updateSector(array $data): array
  1129. {
  1130. unset($data['user_id']);
  1131. $sector = Sector::where('id', $data['id'])->first();
  1132. if (empty($sector)) {
  1133. return Result::error('通栏不存在!');
  1134. }
  1135. if($sector['sector_id'] != $data['sector_id']){
  1136. $sector_id = Sector::where('sector_id',$data['sector_id'])->first();
  1137. if(!empty($sector_id)){
  1138. return Result::error('通栏编号已存在!');
  1139. }
  1140. }
  1141. // 对传入的 page_type 数组进行去重、转换为整数并重新索引
  1142. $data['page_type'] = array_values(array_unique(array_map('intval', $data['page_type'])));
  1143. $data['page_type'] = json_encode($data['page_type']);
  1144. $template = Template::where('template_id', $data['template_id'])
  1145. ->whereRaw("JSON_CONTAINS(template.page_type, ?)", [$data['page_type']])
  1146. ->first();
  1147. if (empty($template)) {
  1148. return Result::error('皮肤不存在!');
  1149. }
  1150. $result = Sector::where('id', $data['id'])->update($data);
  1151. if ($result == 1) {
  1152. return Result::success('修改成功');
  1153. } else {
  1154. return Result::error('修改失败');
  1155. }
  1156. }
  1157. public function getComponentList(array $data): array
  1158. {
  1159. var_dump($data, '---------');
  1160. $where = [];
  1161. // $where[] = ['sector.id', '=', $data['id']];
  1162. if (!empty($data['template_class_id'])) {
  1163. $where['template_class.id'] = $data['template_class_id'];
  1164. }
  1165. if (!empty($data['component_name'])) {
  1166. $where['component.component_name'] = $data['component_name'];
  1167. }
  1168. if (!empty($data['sector_id'])) {
  1169. $where['sector.id'] = $data['sector_id'];
  1170. };
  1171. $result = Component::where($where)
  1172. ->leftJoin('template', 'template.id', '=', 'component.template_id')
  1173. ->leftJoin('template_class', 'template_class.id', '=', 'template.template_class_id') // 添加这一行
  1174. ->leftJoin('sector', 'sector.id', '=', 'component.sector_id')
  1175. ->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') // 修改这一行)
  1176. ->orderBy('sector.updated_at', 'desc')
  1177. ->orderBy('sector.created_at', 'desc')
  1178. ->paginate($data['page_size'], ['*'], 'mypage_name', $data['page']);
  1179. return Result::success($result);
  1180. }
  1181. public function getComponentInfo(array $data): array
  1182. {
  1183. $where = [];
  1184. $result = Component::where($where)
  1185. ->leftJoin('template', 'template.id', '=', 'component.template_id')
  1186. ->leftJoin('template_class', 'template_class.id', '=', 'template.template_class_id') // 添加这一行
  1187. ->leftJoin('sector', 'sector.id', '=', 'component.sector_id')
  1188. ->select('template_class.name as template_class_name', 'template.template_name as template_name', 'sector.sector_name as sector_name', 'component.*')
  1189. ->get();
  1190. return Result::success($result);
  1191. }
  1192. public function addComponent(array $data): array
  1193. {
  1194. unset($data['user_id']);
  1195. $result = Component::insertGetId($data);
  1196. if ($result) {
  1197. return Result::success($result);
  1198. } else {
  1199. return Result::error('添加失败');
  1200. }
  1201. }
  1202. public function delComponent(array $data): array
  1203. {
  1204. $result = Component::where('id', $data['id'])->delete();
  1205. return Result::success($result);
  1206. }
  1207. public function updateComponent(array $data): array
  1208. {
  1209. $result = Component::where('id', $data['id'])->update($data);
  1210. return Result::success($result);
  1211. }
  1212. public function getWebsiteTemplateInfo(array $data)
  1213. {
  1214. $where = [];
  1215. if (isset($data['id'])) {
  1216. $where[] = ['id', '=', $data['id']];
  1217. }
  1218. $result = WebsiteTemplateInfo::where($where)
  1219. ->leftJoin('website', 'website_template_info.website_id', '=', 'website.id')
  1220. ->leftJoin('website_template', 'website_template_info.template_id', '=', 'website_template.id')
  1221. ->select('website_template_info.*', 'website.website_name')
  1222. ->get();
  1223. if ($result) {
  1224. return Result::success($result);
  1225. } else {
  1226. return Result::error('暂无数据');
  1227. }
  1228. }
  1229. /**
  1230. * 自助建站----1.获取页面类型回显
  1231. * @param array $data
  1232. * @return array
  1233. */
  1234. public function getWebPageType(array $data): array
  1235. {
  1236. // 1:图片 2:文字 3:底部
  1237. $friend_link = Link::where('website_id', $data['website_id'])->where('type', 3)->get();
  1238. if (empty($friend_link->toArray())) {
  1239. return Result::error('暂无友情链接数据!');
  1240. }
  1241. $footer_category = FooterCategory::where('website_id', $data['website_id'])->get();
  1242. if (empty($footer_category->toArray())) {
  1243. return Result::error('暂无底部导航数据!');
  1244. }
  1245. $page_type = WebsiteTemplateInfo::where('website_id', $data['website_id'])->value('page_type');
  1246. if (empty($page_type)) {
  1247. $page_type = [];
  1248. } else {
  1249. $page_type = json_decode($page_type, true);
  1250. }
  1251. // '网站页面类型(存数组)1:首页 2:分类页 3:列表页 4:详情页 5:搜索页 6:特殊列表 7:特殊详情页
  1252. if (isset($page_type) && in_array(5, $page_type)) {
  1253. $page_type = 1;
  1254. } else {
  1255. $page_type = 0;
  1256. }
  1257. $result = [
  1258. 'friend_link' => $friend_link,
  1259. 'footer_category' => $footer_category,
  1260. 'page_type' => $page_type,
  1261. ];
  1262. return Result::success($result);
  1263. }
  1264. /**
  1265. * 自助建站----2.添加页面类型
  1266. * @param array $data
  1267. * @return array
  1268. */
  1269. public function addWebPageType(array $data): array
  1270. {
  1271. // return Result::success($data);
  1272. // 1:首页 2:频道页 3:列表页 4:详情页 5:搜索页 6:特殊列表 7:特殊详情页'
  1273. if ($data['is_search'] == 1) {
  1274. $page_type = json_encode([1, 2, 3, 4, 5, 6, 7]);
  1275. } else {
  1276. $page_type = json_encode([1, 2, 3, 4, 6, 7]);
  1277. }
  1278. // 0:未构建;1:已填写基础信息;2:已选择模板; action_id
  1279. $website_template_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1280. // return Result::success($website_template_info);
  1281. // return Result::success(empty($website_template_info));
  1282. if (empty($website_template_info)) {
  1283. $result = WebsiteTemplateInfo::insertGetId(['website_id' => $data['website_id'], 'page_type' => $page_type, 'user_id' => $data['user_id']]);
  1284. } else {
  1285. if ($website_template_info['status'] == 2) {
  1286. return Result::error('网站已应用,不可再次修改!');
  1287. }
  1288. $result = WebsiteTemplateInfo::where('website_id', $data['website_id'])->update(['page_type' => $page_type], ['action_id' => 1, 'user_id' => $data['user_id']]);
  1289. }
  1290. if ($result) {
  1291. return Result::success($result);
  1292. } else {
  1293. return Result::error('添加失败');
  1294. }
  1295. }
  1296. /**
  1297. * 自助建站---流程---3.选择皮肤列表
  1298. * @param array $data
  1299. * @return array
  1300. */
  1301. public function getWebsiteTemplateList(array $data): array
  1302. {
  1303. $where = [];
  1304. if (isset($data['template_class_id']) && $data['template_class_id']) {
  1305. $where[] = ['template_class_id', '=', $data['template_class_id']];
  1306. }
  1307. // 0:未构建 1:未应用 2:已应用 status
  1308. $templste_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1309. if (empty($templste_info)) {
  1310. return Result::error('请先填写网站基础信息!');
  1311. }
  1312. if ($templste_info['status'] == 2) {
  1313. return Result::error('网站已应用,不可再次修改!');
  1314. }
  1315. if (isset($data['keyword']) && !empty($data['keyword'])) {
  1316. if (is_array($data['keyword'])) {
  1317. // 拼接多个模糊查询条件
  1318. $query = Template::query();
  1319. foreach ($data['keyword'] as $kw) {
  1320. $escapedKw = addcslashes($kw, '%_'); // 转义通配符
  1321. $query->WhereRaw("JSON_EXTRACT(template_keyword, '$[*]') LIKE ?", ["%$escapedKw%"]);
  1322. }
  1323. } else {
  1324. $escapedKeyword = addcslashes($data['keyword'], '%_'); // 转义通配符
  1325. $where[] = [DB::raw("JSON_EXTRACT(template_keyword, '$[*]') LIKE ?"), "%$escapedKeyword%"];
  1326. }
  1327. } else {
  1328. $query = Template::where($where);
  1329. }
  1330. $template_id = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first(['template_id', 'page_type']);
  1331. // return Result::success($template_id);
  1332. if (!empty($template_id['page_type']) && $templste_info['page_type'] != '[]' && is_string($templste_info['page_type'])) {
  1333. $template_page = is_string($templste_info['page_type']) ? json_decode($templste_info['page_type'], true) : [];
  1334. } else {
  1335. $template_page = [];
  1336. }
  1337. if (!empty($template_id['template_id'])) {
  1338. $result['template_id'] = $templste_info['template_id'];
  1339. }
  1340. // 确保 $template_page 为有效数据,避免 SQL 注入风险,这里使用参数绑定
  1341. $template_page_str = json_encode($template_page);
  1342. $result['template'] = $query->where($where)
  1343. ->whereRaw("JSON_CONTAINS(template.page_type, ?)", [$template_page_str])
  1344. ->orderBy('updated_at', 'desc')
  1345. ->paginate($data['page_size'], ['*'], 'page', $data['page']);
  1346. if (empty($result['template'])) {
  1347. $result['template'] = Template::orderBy('updated_at', 'desc')
  1348. ->paginate($data['page_size'], ['*'], 'page', $data['page']);
  1349. }
  1350. $template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->value('template_id');
  1351. if ($template) {
  1352. $result['template_id'] = $template;
  1353. } else {
  1354. $result['template_id'] = 0;
  1355. }
  1356. return Result::success($result);
  1357. }
  1358. /**
  1359. * 自助建站---流程---4.选择皮肤
  1360. * @param array $data
  1361. * @return array
  1362. */
  1363. public function chooseWebsiteTemplate(array $data): array
  1364. {
  1365. $website_template_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1366. if (empty($website_template_info)) {
  1367. return Result::error('请先填写网站基础信息!');
  1368. }
  1369. // 0:未构建 1:未应用 2:已应用 status
  1370. if ($website_template_info['status'] == 2) {
  1371. return Result::error('网站已应用,不可再次修改!');
  1372. }
  1373. $template = Template::where('template_id', $data['template_id'])->first();
  1374. if (empty($template)) {
  1375. return Result::error('未查询到皮肤!');
  1376. }
  1377. // 0:未构建;1:已填写基础信息;2:已选择模板; action_id
  1378. $web_template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->update(['template_id' => $data['template_id'], 'action_id' => 2]);
  1379. if ($web_template) {
  1380. return Result::success($web_template);
  1381. } else {
  1382. return Result::error('选择皮肤失败!');
  1383. }
  1384. }
  1385. /**
  1386. * 自助建站---流程---5.保存模版
  1387. * @param array $data
  1388. * @return array
  1389. */
  1390. public function saveWebsiteTemplate(array $data): array
  1391. {
  1392. // return Result::success($data);
  1393. $website_template_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1394. if (empty($website_template_info) || empty($website_template_info->template_id) || empty($website_template_info->page_type)) {
  1395. return Result::error('请先填写网站基础信息!');
  1396. }
  1397. // 0:未构建 1:未应用 2:已应用 status
  1398. if ($website_template_info['status'] == 2) {
  1399. return Result::error('网站已应用,不可再次保存!');
  1400. }
  1401. if ($website_template_info['action_id'] != 2) {
  1402. return Result::error('请完成之前的步骤!');
  1403. }
  1404. $template_info = WebsiteTemplate::where('website_id', $data['website_id'])->first();
  1405. // var_dump($data['template_data']);
  1406. try {
  1407. Db::beginTransaction();
  1408. if (empty($template_info)) {
  1409. $template = WebsiteTemplate::insertGetId([
  1410. 'website_id' => $data['website_id'],
  1411. 'template_data' => $data['template_data'],
  1412. 'user_id' => $data['user_id'],
  1413. 'canvas_data' => $data['canvas_data'],
  1414. 'page_type' => $website_template_info['page_type'],
  1415. 'template_id' => $website_template_info['template_id'],
  1416. ]);
  1417. } else {
  1418. $template = WebsiteTemplate::where('website_id', $data['website_id'])->update(
  1419. [
  1420. 'template_data' => $data['template_data'],
  1421. 'user_id' => $data['user_id'],
  1422. 'canvas_data' => $data['canvas_data'],
  1423. 'page_type' => $website_template_info['page_type'],
  1424. 'template_id' => $website_template_info['template_id'],
  1425. ]
  1426. );
  1427. }
  1428. if (empty($template)) {
  1429. Db::rollback();
  1430. return Result::error('保存失败!');
  1431. } else {
  1432. $tempalte_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->update(['status' => 1]);
  1433. if (empty($tempalte_info)) {
  1434. Db::rollback();
  1435. return Result::error('保存失败!');
  1436. }
  1437. Db::commit();
  1438. }
  1439. } catch (\Exception $e) {
  1440. return Result::error('模版数据格式错误!');
  1441. }
  1442. return Result::success($template);
  1443. }
  1444. /**
  1445. * 自助建站---流程---6.返显保存的模版
  1446. * @param array $data
  1447. * @return array
  1448. */
  1449. public function getWebsiteTemplate(array $data): array
  1450. {
  1451. $web = Website::where('id', $data['website_id'])->first();
  1452. if (empty($web)) {
  1453. return Result::error('未查询到网站信息!');
  1454. }
  1455. $template = WebsiteTemplate::where('website_id', $data['website_id'])
  1456. ->first(['website_id', 'template_id', 'page_type', 'template_data', 'canvas_data']);
  1457. if (empty($template)) {
  1458. return Result::error('未查询到模版!');
  1459. }
  1460. return Result::success($template);
  1461. }
  1462. }