PublicRpcService.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  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. #[RpcService(name: "PublicRpcService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  25. class PublicRpcService implements PublicRpcServiceInterface
  26. {
  27. #[Inject]
  28. protected Redis $redis;
  29. /**
  30. * @param array $data
  31. * @return array
  32. */
  33. public function getDistrictList(array $data): array
  34. {
  35. $where = [];
  36. if (isset($data['keyWord'])) {
  37. $where = [
  38. ['name', 'like', '%' . $data['keyWord'] . '%']
  39. ];
  40. }
  41. $result = [];
  42. if (isset($data['pageSize'])) {
  43. $rep = District::where($where)->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->orderBy("code", "asc")->get();
  44. $count = District::where($where)->count();
  45. $result = [
  46. 'rows' => $rep,
  47. 'count' => $count
  48. ];
  49. } else {
  50. $result = District::where($data)->orderBy("code", "asc")->get();
  51. }
  52. return $result ? Result::success($result) : Result::error("没有查到数据");
  53. }
  54. /**
  55. * @param array $data
  56. * @return array
  57. */
  58. public function getUserLevelList(array $data): array
  59. {
  60. $where = [];
  61. if (isset($data['keyWord'])) {
  62. $where = [
  63. ['name', 'like', '%' . $data['keyWord'] . '%'],
  64. ];
  65. }
  66. $result = [];
  67. if (isset($data['pageSize'])) {
  68. $rep = UserLevel::where($where)->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->orderBy("id", "asc")->get();
  69. $count = UserLevel::where($where)->count();
  70. $result = [
  71. 'rows' => $rep,
  72. 'count' => $count,
  73. ];
  74. } else {
  75. $result = UserLevel::orderBy("id", "asc")->get();
  76. }
  77. return $result ? Result::success($result) : Result::error("没有查到数据");
  78. }
  79. /**
  80. * 添加用户等级
  81. * @param array $data
  82. * @return array
  83. */
  84. public function addUserLevel(array $data): array
  85. {
  86. LevelUser::insertGetId($data);
  87. return Result::success([]);
  88. }
  89. /**
  90. * 更新等级
  91. * @param array $data
  92. * @return array
  93. */
  94. public function updateUserLevel(array $data): array
  95. {
  96. $result = LevelUser::where(['id' => $data['id']])->update($data);
  97. if ($result) {
  98. return Result::success($result);
  99. }
  100. return Result::error("更新失败");
  101. }
  102. /**
  103. * 删除等级
  104. * @param array $data
  105. * @return array
  106. */
  107. public function delUserLevel(array $data): array
  108. {
  109. $result = LevelUser::where(['id' => $data['id']])->delete();
  110. if ($result) {
  111. return Result::success($result);
  112. }
  113. return Result::error("删除失败");
  114. }
  115. /**
  116. * 查询投诉举报信息
  117. * @param array $data
  118. * @return array
  119. */
  120. public function getLetterOfComplaint(array $data = []): array
  121. {
  122. var_dump("====");
  123. $where = [];
  124. if (isset($data['user_id']) && !empty($data['user_id'])) {
  125. array_push($where, ['letter_of_complaint.user_id', '=', $data['user_id']]);
  126. }
  127. if (isset($data['nature']) && !empty($data['nature'])) {
  128. array_push($where, ['letter_of_complaint.nature', '=', $data['nature']]);
  129. }
  130. if (isset($data['nature_level0']) && !empty($data['nature_level0'])) {
  131. array_push($where, ['letter_of_complaint.nature_level0', '=', $data['nature_level0']]);
  132. }
  133. if (isset($data['status']) && !empty($data['status'])) {
  134. array_push($where, ['letter_of_complaint.status', '=', $data['status']]);
  135. }
  136. $result = [];
  137. if (isset($data['pageSize'])) {
  138. $rep = LetterOfComplaint::where($where)
  139. ->leftJoin("letter_type as type_a", "letter_of_complaint.nature", "type_a.id")
  140. ->leftJoin("letter_type as type_c", "letter_of_complaint.nature_level0", "type_c.id")
  141. ->leftJoin("letter_type as type_b", "letter_of_complaint.nature_level1", "type_b.id")
  142. ->leftJoin("letter_type as type_e", "letter_of_complaint.nature_level3", "type_e.id")
  143. ->leftJoin("letter_type as type_d", "letter_of_complaint.status", "type_d.id")
  144. ->select(
  145. "letter_of_complaint.*",
  146. "type_a.type_name as nature_name",
  147. "type_b.type_name as nature_name1",
  148. "type_c.type_name as nature_name0",
  149. "type_d.type_name as status_name",
  150. "type_e.type_name as nature_name3"
  151. )
  152. ->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->orderBy("letter_of_complaint.id", "desc")->get();
  153. $count = LetterOfComplaint::where($where)->count();
  154. if ($rep) {
  155. foreach ($rep as $val) {
  156. if ($val['judgment']) {
  157. $val['judgment'] = json_decode($val['judgment']);
  158. }
  159. if ($val['audio_and_video']) {
  160. $val['audio_and_video'] = json_decode($val['audio_and_video']);
  161. }
  162. if ($val['contract']) {
  163. $val['contract'] = json_decode($val['contract']);
  164. }
  165. if ($val['qualifications']) {
  166. $val['qualifications'] = json_decode($val['qualifications']);
  167. }
  168. }
  169. }
  170. $result = [
  171. 'rows' => $rep,
  172. 'count' => $count,
  173. ];
  174. } else {
  175. $result = LetterOfComplaint::where($where)
  176. ->leftJoin("letter_type as type_a", "letter_of_complaint.nature", "type_a.id")
  177. ->leftJoin("letter_type as type_c", "letter_of_complaint.nature_level0", "type_c.id")
  178. ->leftJoin("letter_type as type_b", "letter_of_complaint.nature_level1", "type_b.id")
  179. ->leftJoin("letter_type as type_e", "letter_of_complaint.nature_level3", "type_e.id")
  180. ->leftJoin("letter_type as type_d", "letter_of_complaint.status", "type_d.id")
  181. ->select(
  182. "letter_of_complaint.*",
  183. "type_a.type_name as nature_name",
  184. "type_b.type_name as nature_name1",
  185. "type_c.type_name as nature_name0",
  186. "type_d.type_name as status_name",
  187. "type_e.type_name as nature_name3"
  188. )
  189. ->orderBy("letter_of_complaint.id", "desc")->get();
  190. }
  191. return $result ? Result::success($result) : Result::error("没有查到数据");
  192. }
  193. /**
  194. * 添加投诉举报信息
  195. * @param array $data
  196. * @return array
  197. */
  198. public function addLetterOfComplaint(array $data): array
  199. {
  200. $data['judgment'] = $data['judgment'] ? json_encode($data['judgment']) : '';
  201. $data['audio_and_video'] = $data['audio_and_video'] ? json_encode($data['audio_and_video']) : '';
  202. $data['contract'] = $data['contract'] ? json_encode($data['contract']) : '';
  203. $data['qualifications'] = $data['qualifications'] ? json_encode($data['qualifications']) : '';
  204. unset($data['id']);
  205. $result = LetterOfComplaint::insertGetId($data);
  206. if (empty($result)) {
  207. return Result::error("创建失败", 0);
  208. } else {
  209. return Result::success(["id" => $result]);
  210. }
  211. }
  212. /**
  213. * 用户端更新投诉举报
  214. * @param array $data
  215. * @return array
  216. */
  217. public function userUpLetterOfComplaint(array $data): array
  218. {
  219. $data['judgment'] = $data['judgment'] ? json_encode($data['judgment']) : '';
  220. $data['audio_and_video'] = $data['audio_and_video'] ? json_encode($data['audio_and_video']) : '';
  221. $data['contract'] = $data['contract'] ? json_encode($data['contract']) : '';
  222. $data['qualifications'] = $data['qualifications'] ? json_encode($data['qualifications']) : '';
  223. $result = LetterOfComplaint::where(['id' => $data['id']])->update($data);
  224. if (empty($result)) {
  225. return Result::error("创建失败", 0);
  226. } else {
  227. return Result::success(["id" => $result]);
  228. }
  229. }
  230. /**
  231. * 管理后台更新投诉举报信息
  232. * @param array $data
  233. * @return array
  234. */
  235. public function upLetterOfComplaint(array $data): array
  236. {
  237. var_dump("admin:", $data);
  238. $where = [
  239. 'id' => $data['id'],
  240. ];
  241. $filtered_array = array_filter($data, function ($value) {
  242. return $value !== "" && $value !== null && $value !== false && !is_array($value) || !empty($value);
  243. });
  244. $filtered_array['judgment'] = isset($filtered_array['judgment']) ? json_encode($filtered_array['judgment']) : '';
  245. $filtered_array['audio_and_video'] = isset($filtered_array['audio_and_video']) ? json_encode($filtered_array['audio_and_video']) : '';
  246. $filtered_array['contract'] = isset($filtered_array['contract']) ? json_encode($filtered_array['contract']) : '';
  247. $filtered_array['qualifications'] = isset($filtered_array['qualifications']) ? json_encode($filtered_array['qualifications']) : '';
  248. unset($filtered_array['nature_name']);
  249. unset($filtered_array['type_name']);
  250. unset($filtered_array['nature_level_name']);
  251. unset($filtered_array['status_name']);
  252. unset($filtered_array['is_admin']);
  253. unset($filtered_array['type_level_name']);
  254. $result = LetterOfComplaint::where($where)->update($filtered_array);
  255. if ($result) {
  256. return Result::success($result);
  257. }
  258. return Result::error("更新失败", 0);
  259. }
  260. /**
  261. * 查询投诉举报记录
  262. * @param array $data
  263. * @return array
  264. */
  265. public function getLetterOfComplaintInfo(array $data): array
  266. {
  267. $where = [
  268. 'letter_of_complaint.id' => $data['id'],
  269. ];
  270. if (isset($data['user_id']) && !empty($data['user_id'])) {
  271. array_push($where, ['letter_of_complaint.user_id', '=', $data['user_id']]);
  272. }
  273. $result = LetterOfComplaint::where($where)
  274. ->leftJoin("letter_type as type_a", "letter_of_complaint.nature", "type_a.id")
  275. ->leftJoin("letter_type as type_c", "letter_of_complaint.nature_level0", "type_c.id")
  276. ->leftJoin("letter_type as type_b", "letter_of_complaint.nature_level1", "type_b.id")
  277. ->leftJoin("letter_type as type_e", "letter_of_complaint.nature_level3", "type_e.id")
  278. ->leftJoin("letter_type as type_d", "letter_of_complaint.status", "type_d.id")
  279. ->select(
  280. "letter_of_complaint.*",
  281. "type_a.type_name as nature_name",
  282. "type_b.type_name as nature_name1",
  283. "type_c.type_name as nature_name0",
  284. "type_d.type_name as status_name",
  285. "type_e.type_name as nature_name3"
  286. )
  287. ->first();
  288. return Result::success($result);
  289. }
  290. /**
  291. * 删除投诉举报信息
  292. * @param array $data
  293. * @return array
  294. */
  295. public function delLetterOfComplaint(array $data): array
  296. {
  297. $result = LetterOfComplaint::when($data, function ($query) use ($data) {
  298. if (isset($data['id']) && !empty($data['id'])) {
  299. $query->where(['id' => $data['id']]);
  300. }
  301. if (isset($data['user_id']) && !empty($data['user_id'])) {
  302. $query->where(['user_id' => $data['user_id']]);
  303. }
  304. })->delete();
  305. if (empty($result)) {
  306. return Result::error("删除失败", 0);
  307. } else {
  308. return Result::success();
  309. }
  310. }
  311. /**
  312. * 获取举报信息类型
  313. * @param array $data
  314. * @return array
  315. */
  316. public function getLetterType(array $data): array
  317. {
  318. $where = [];
  319. if (isset($data['type'])) {
  320. array_push($where, ['type', '=', $data['type']]);
  321. }
  322. if (isset($data['pid']) && $data['pid'] > 0) {
  323. array_push($where, ['pid', '=', $data['pid']]);
  324. }
  325. $result = LetterType::where($where)->orderBy('sort', 'asc')->get();
  326. return $result ? Result::success($result) : Result::error("没有查到数据");
  327. }
  328. /**
  329. * 更新举报类型
  330. * @param array $data
  331. * @return array
  332. */
  333. public function upLetterType(array $data): array
  334. {
  335. return [];
  336. }
  337. /**
  338. * 添加举报类型
  339. * @param array $data
  340. * @return array
  341. */
  342. public function addLetterType(array $data): array
  343. {
  344. $result = LetterType::insertGetId($data);
  345. if (empty($result)) {
  346. return Result::error("创建失败", 0);
  347. } else {
  348. return Result::success(["id" => $result]);
  349. }
  350. }
  351. /**
  352. * 删除举报类型
  353. * @param array $data
  354. * @return array
  355. */
  356. public function delLetterType(array $data): array
  357. {
  358. $result = LetterType::where('id', $data['id'])->delete();
  359. if (empty($result)) {
  360. return Result::error("删除失败", 0);
  361. } else {
  362. return Result::success();
  363. }
  364. }
  365. /**
  366. * 检测是否已经被接案
  367. * @param array $data
  368. * @return array
  369. */
  370. public function checkMeasure(array $data): array
  371. {
  372. $where = [
  373. 'id' => $data['id'],
  374. ];
  375. $letterOfComplaintInfo = LetterOfComplaint::where($where)->first();
  376. var_dump("查询数据:", $letterOfComplaintInfo['admin_id'], $data['user_id']);
  377. //操作人和当前登陆用户id 相等说明是当前人接收的案件
  378. if (($letterOfComplaintInfo['admin_id'] == $data['user_id']) || empty($letterOfComplaintInfo['admin_id'])) {
  379. return Result::success();
  380. } else {
  381. return Result::error("您不能处理其他人已经接过的案件", 0);
  382. }
  383. }
  384. /**
  385. * 后台获取职能部门
  386. * @param array $data
  387. * @return array
  388. */
  389. public function getZhinengbumenList(array $data): array
  390. {
  391. // 获取分页参数,默认每页 10 条记录
  392. $page = isset($data['page']) ? (int) $data['page'] : 1;
  393. $perPage = isset($data['pagesize']) ? (int) $data['pagesize'] : 10;
  394. // 查询数据并分页
  395. $query = Department::query();
  396. // 可以在这里添加更多的查询条件
  397. if (isset($data['search'])) {
  398. $query->where('name', 'like', '%' . $data['search'] . '%');
  399. }
  400. // 执行分页查询
  401. $result = $query->paginate($perPage, ['*'], 'page', $page);
  402. // 返回分页结果
  403. return Result::success([
  404. 'count' => $result->total(),
  405. 'current_page' => $result->currentPage(),
  406. 'last_page' => $result->lastPage(),
  407. 'pagesize' => $result->perPage(),
  408. 'rows' => $result->items(),
  409. ]);
  410. }
  411. /**
  412. * 添加获取职能部门
  413. * @param array $data
  414. * @return array
  415. */
  416. public function addZhinengbumen(array $data): array
  417. {
  418. $result = Department::insertGetId($data);
  419. if (empty($result)) {
  420. return Result::error("创建失败", 0);
  421. } else {
  422. return Result::success(["id" => $result]);
  423. }
  424. }
  425. public function delZhinengbumen(array $data): array
  426. {
  427. $result = Department::where('id', $data['id'])->delete();
  428. if (empty($result)) {
  429. return Result::error("删除失败", 0);
  430. } else {
  431. return Result::success();
  432. }
  433. }
  434. public function getZhinengbumen(array $data): array
  435. {
  436. $result = Department::where('id', $data['id'])->first();
  437. return Result::success($result);
  438. }
  439. public function getPidZhinengbumen(array $data): array
  440. {
  441. if (empty($data['pid'])) {
  442. $data['pid'] = 0;
  443. }
  444. $result = Department::where('pid', $data['pid'])->get();
  445. return Result::success($result);
  446. }
  447. public function modZhinengbumen(array $data): array
  448. {
  449. $result = Department::where('id', $data['id'])->update($data);
  450. if (empty($result)) {
  451. return Result::error("修改失败", 0);
  452. } else {
  453. return Result::success();
  454. }
  455. }
  456. /**
  457. * 查询职能列表
  458. * @param array $data
  459. * @return array
  460. */
  461. public function getDepartment(array $data): array
  462. {
  463. $where = [];
  464. if (isset($data['pid'])) {
  465. $where = [
  466. 'pid' => $data['pid'] ?? 0
  467. ];
  468. }
  469. $result = Department::when(!empty($where), function ($query) use ($where) {
  470. $query->where($where);
  471. })->orderBy("sort", "desc")->get();
  472. if (empty($result)) {
  473. return Result::error("查询失败", 0);
  474. } else {
  475. return Result::success($result);
  476. }
  477. }
  478. /**
  479. * 获取所有的buckets
  480. * @param array $data
  481. * @return array
  482. */
  483. public function getBuckets(array $data): array
  484. {
  485. $result = new MinioService();
  486. // 调用服务层的方法获取存储桶列表
  487. $bucketsResponse = $result->listBuckets();
  488. // 直接返回服务层生成的响应
  489. return Result::success($bucketsResponse['data']);
  490. }
  491. /**
  492. * 上传文件
  493. * @param array $data
  494. * @return array
  495. */
  496. public function uploadFile(array $data): array
  497. {
  498. $result = new MinioService();
  499. $rep = $result->uploadFile($data);
  500. if ($rep['code'] == 200) {
  501. return Result::success($rep['data']);
  502. } else {
  503. return Result::error("上传失败!");
  504. }
  505. }
  506. /**
  507. * 黑名单管理
  508. * @param array $data
  509. * @return array
  510. */
  511. public function getBlackWordList(array $data): array
  512. {
  513. $result = BlackWord::when($data, function ($query) use ($data) {
  514. if (isset($data['name']) && $data['name']) {
  515. $query->where('black_word.name', 'like', '%' . $data['name'] . '%');
  516. }
  517. })->orderBy('black_word.id', 'desc')
  518. ->paginate(
  519. intval($data['pageSize']),
  520. [
  521. 'black_word.*',
  522. ],
  523. 'page',
  524. intval($data['page'])
  525. );
  526. $count = $result->total();
  527. $returnData = [
  528. 'rows' => $result->items(),
  529. 'count' => $count
  530. ];
  531. return Result::success($returnData);
  532. }
  533. /**
  534. * 添加黑名单
  535. * @param array $data
  536. * @return array
  537. */
  538. public function addBlackWord(array $data): array
  539. {
  540. Db::beginTransaction();
  541. try {
  542. $info = BlackWord::where(['name' => $data['name']])->first();
  543. if ($info) {
  544. Db::rollBack();
  545. return Result::error("该黑名单已存在", 0);
  546. }
  547. $data['type'] = 10;
  548. $result = BlackWord::insertGetId($data);
  549. $redisKey = 'black_word';
  550. $this->redis->sAdd($redisKey, $data['name']);
  551. Db::commit();
  552. return Result::success(["id" => $result]);
  553. } catch (\Exception $e) {
  554. Db::rollBack();
  555. return Result::error("创建失败" . $e->getMessage(), 0);
  556. }
  557. }
  558. /**
  559. * 删除黑名单
  560. * @param array $data
  561. * @return array
  562. */
  563. public function delBlackWord(array $data): array
  564. {
  565. Db::beginTransaction();
  566. try {
  567. BlackWord::where(['name' => $data['name']])->delete();
  568. $redisKey = 'black_word';
  569. $this->redis->sRem($redisKey, $data['name']);
  570. Db::commit();
  571. return Result::success([]);
  572. } catch (\Exception $e) {
  573. Db::rollBack();
  574. return Result::error("删除失败" . $e->getMessage(), 0);
  575. }
  576. }
  577. /**
  578. * 修改违禁词
  579. * @param array $data
  580. * @return array
  581. */
  582. public function upBlackWord(array $data): array
  583. {
  584. Db::beginTransaction();
  585. try {
  586. $checkInfo = BlackWord::where(['name' => $data['name']])->first();
  587. if ($checkInfo) {
  588. Db::rollBack();
  589. return Result::error("该违禁词已经存在", 0);
  590. }
  591. $blackWordInfo = BlackWord::where(['id' => $data['id']])->first();
  592. if ($blackWordInfo) {
  593. //先删除redis
  594. $blackWordInfo = $blackWordInfo->toArray();
  595. $redisKey = 'black_word';
  596. $this->redis->sRem($redisKey, $blackWordInfo['name']);
  597. $this->redis->sAdd($redisKey, $data['name']);
  598. BlackWord::where(['id' => $data['id']])->update(['name' => $data['name']]);
  599. Db::commit();
  600. return Result::success([]);
  601. } else {
  602. Db::rollBack();
  603. return Result::error("系统错误", 0);
  604. }
  605. } catch (\Exception $e) {
  606. Db::rollBack();
  607. return Result::error("修改失败" . $e->getMessage(), 0);
  608. }
  609. }
  610. /**
  611. * 获取风格
  612. * @return void
  613. */
  614. public function getTemplateClassList(array $data): array
  615. {
  616. $where = [];
  617. if (isset($data['name']) && $data['name']) {
  618. array_push($where, ['template_class.name', 'like', '%' . $data['name'] . '%']);
  619. }
  620. $template = TemplateClass::when($where, function ($query) use ($where) {
  621. $query->where($where);
  622. });
  623. $count = $template->count();
  624. // $countQuery = clone $template;
  625. $row = $template
  626. ->leftJoin('template', 'template_class.id', '=', 'template.template_class_id')
  627. ->select('template_class.*', DB::raw('COUNT(template.id) as template_count'))
  628. ->groupBy('template_class.id')
  629. ->orderBy('template_class.updated_at', 'desc')
  630. ->offset(($data['page'] - 1) * $data['pageSize'])
  631. ->limit($data['pageSize'])
  632. ->get();
  633. $result = [
  634. 'rows' => $row,
  635. 'count' => $count,
  636. ];
  637. if ($row->isEmpty()) {
  638. return Result::error("暂无风格", 0);
  639. } else {
  640. return Result::success($result);
  641. }
  642. }
  643. /**
  644. * 添加风格
  645. * @param
  646. * @return void
  647. */
  648. public function addTemplateClass(array $data): array
  649. {
  650. $data['keyword'] = json_encode($data['keyword']);
  651. $template = TemplateClass::where(['name' => $data['name']])->first();
  652. if ($template) {
  653. return Result::error("风格名称已存在", 0);
  654. }
  655. $result = TemplateClass::insertGetId($data);
  656. if (empty($result)) {
  657. return Result::error("创建风格失败", 0);
  658. } else {
  659. return Result::success(["id" => $result]);
  660. }
  661. }
  662. /**
  663. * 更新风格
  664. * @param array $data
  665. * @return array
  666. */
  667. public function upTemplateClass(array $data): array
  668. {
  669. $where = [
  670. 'id' => $data['id'],
  671. ];
  672. $template = TemplateClass::where($where)->first();
  673. if (empty($template)) {
  674. return Result::error("未查询到风格", 0);
  675. }
  676. if($template->type == 1){
  677. return Result::error("默认风格不能修改", 0);
  678. }
  679. $template = TemplateClass::where('id','!=',$data['id'])->where(['name' => $data['name']])->first();
  680. if ($template) {
  681. return Result::error("风格名称已存在", 0);
  682. }
  683. $updateData = [
  684. 'name' => $data['name'],
  685. 'keyword' => json_encode($data['keyword']),
  686. ];
  687. $result = TemplateClass::where($where)->update($updateData);
  688. if (empty($result)) {
  689. return Result::error("更新失败", 0);
  690. } else {
  691. return Result::success($result);
  692. }
  693. }
  694. /**
  695. * 删除风格
  696. * @param array $data
  697. * @return array
  698. */
  699. public function delTemplateClass(array $data): array
  700. {
  701. $where = [
  702. 'id' => $data['id'],
  703. ];
  704. $template = TemplateClass::where($where)->first();
  705. if (empty($template)) {
  706. return Result::error("未查询到风格", 0);
  707. }
  708. if($template->type == 1){
  709. return Result::error("默认风格不能删除", 0);
  710. }
  711. $result = TemplateClass::where($where)->delete();
  712. if (empty($result)) {
  713. return Result::error("删除失败", 0);
  714. } else {
  715. return Result::success($result);
  716. }
  717. }
  718. /**
  719. * 获取getTemplateClass
  720. * @param array $data
  721. * @return array
  722. */
  723. public function getTemplateClass(array $data): array
  724. {
  725. $result = TemplateClass::get();
  726. return Result::success($result);
  727. }
  728. public function getTemplateList(array $data): array
  729. {
  730. $where = [];
  731. if (!empty($data['template_class_id'])) {
  732. $where['template_class_id'] = $data['template_class_id'];
  733. }
  734. if (!empty($data['template_name'])) {
  735. $where['template_name'] = $data['template_name'];
  736. }
  737. $result = Template::where($where)
  738. ->leftJoin('template_class', 'template.template_class_id', 'template_class.id')
  739. ->select('template.*', 'template_class.name as template_class_name')
  740. ->orderBy('template.id', 'desc')
  741. ->paginate($data['page_size'], ['*'], 'mypage_name', $data['page']);
  742. // 使用 items 方法获取分页数据
  743. // $items = collect($result->items());
  744. // $items->each(function ($item) {
  745. // $item['template_content'] = json_decode($item['template_content'], true);
  746. // });
  747. return Result::success($result);
  748. }
  749. public function getTemplateInfo(array $data): array
  750. {
  751. $result = Template::where('template.id', $data['id'])
  752. ->leftJoin('template_class', 'template.template_class_id', 'template_class.id')
  753. ->select('template.*', 'template_class.name as template_class_name')
  754. ->first();
  755. return Result::success($result);
  756. }
  757. public function addTemplate(array $data): array
  758. {
  759. var_dump($data);
  760. unset($data['user_id']);
  761. $result = Template::insertGetId($data);
  762. if ($result) {
  763. $returData = Template::where('id', $result)->first();
  764. return Result::success($returData);
  765. } else {
  766. return Result::error("添加失败", 0);
  767. }
  768. }
  769. public function delTemplate(array $data): array
  770. {
  771. $result = Template::where('id', $data['id'])->delete();
  772. var_dump($result, '-------------------delete');
  773. if ($result) {
  774. return Result::success($result);
  775. } else {
  776. return Result::error("删除失败", 0);
  777. }
  778. }
  779. public function updateTemplate(array $data): array
  780. {
  781. unset($data['user_id']);
  782. $result = Template::where('id', $data['id'])->update($data);
  783. var_dump($result, '-------------------update');
  784. if (!$result) {
  785. return Result::error("更新失败", 0);
  786. } else {
  787. return Result::success('更新成功');
  788. }
  789. }
  790. public function getSectorList(array $data): array
  791. {
  792. $where = [];
  793. if (!empty($data['template_class_id'])) {
  794. $where['template_class.id'] = $data['template_class_id'];
  795. }
  796. if (!empty($data['template_class_name'])) {
  797. $where[] = ['template_class.name', 'like', '%' . $data['template_class_name'] . '%'];
  798. }
  799. if (!empty($data['sector_name'])) {
  800. if (!empty($data['sector_name'])) {
  801. // $where['sector_name'] = $data['sector_name'];
  802. $where[] = ['sector.sector_name', 'like', '%' . $data['sector_name'] . '%'];
  803. }
  804. }
  805. $result = Sector::where($where)
  806. ->leftJoin('template', 'template.id', '=', 'sector.template_id')
  807. ->leftJoin('template_class', 'template_class.id', '=', 'sector.template_id') // 添加这一行
  808. ->select('sector.*', 'sector.sector_name', 'template.template_name', 'template_class.name as template_class_name', 'template_class.id as template_class_id') // 修改这一行
  809. ->orderBy('sector.id', 'desc')
  810. ->paginate($data['page_size'], ['*'], 'mypage_name', $data['page']);
  811. return Result::success($result);
  812. }
  813. public function getSectorInfo(array $data): array
  814. {
  815. $where = [];
  816. $where[] = ['sector.id', '=', $data['id']];
  817. $result = Sector::where($where)
  818. ->leftJoin('template', 'template.id', '=', 'sector.template_id')
  819. ->leftJoin('template_class', 'template_class.id', '=', 'template.template_class_id') // 添加这一行
  820. ->select('sector.*', 'template.template_name', 'template_class.name as template_class_name', 'template_class.id as template_class_id') // 修改这一行
  821. ->orderBy('sector.id', 'desc')
  822. ->get();
  823. return Result::success($result);
  824. }
  825. /**
  826. * 获取经纬度信息
  827. * @return void
  828. */
  829. public function getIpInfo(array $data) :array
  830. {
  831. $client_ip = isset($data['ip']) && $data['ip']??$_SERVER['REMOTE_ADDR'];
  832. // 使用 IPinfo 服务获取 IP 信息
  833. $api_url = "https://ipinfo.io/{$client_ip}/json";
  834. $ch = curl_init($api_url);
  835. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  836. $response = curl_exec($ch);
  837. curl_close($ch);
  838. // 解析 JSON 响应
  839. $ip_info = json_decode($response, true);
  840. // 提取地址和经纬度
  841. if ($ip_info && !isset($ip_info['bogon'])) {
  842. $latitude = explode(',', $ip_info['loc'])[0];
  843. $longitude = explode(',', $ip_info['loc'])[1];
  844. $ip_info['latitude'] = $latitude;
  845. $ip_info['longitude'] = $longitude;
  846. return Result::success($ip_info);
  847. } else {
  848. $data['ip'] = '101.254.114.212';
  849. $this->getIpinfo(["ip"=>$data['ip']]);
  850. }
  851. }
  852. /**
  853. * 获取天气
  854. * @param array $data
  855. * @return array
  856. */
  857. public function getWeatherInfo(array $data) :array
  858. {
  859. $month = $data['month'] ?? date('m');
  860. $day = $data['day'] ?? date('d');
  861. // 使用缓存键
  862. $cacheKey = "tsbb_data_weather_{$month}_{$day}";
  863. // 尝试从缓存获取数据
  864. $container = \Hyperf\Context\ApplicationContext::getContainer();
  865. $cache = $container->get(\Psr\SimpleCache\CacheInterface::class);
  866. if ($cachedData = $cache->get($cacheKey)) {
  867. return Result::success(unserialize($cachedData));
  868. }
  869. $location = $data['latitude'].":".$data['longitude'];
  870. $api_url = "https://api.seniverse.com/v3/weather/now.json?key=".\Hyperf\Support\env('WEATHER_KEY')."&location=".$location."&language=zh-Hans&unit=c";
  871. $ch = curl_init($api_url);
  872. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  873. $response = curl_exec($ch);
  874. curl_close($ch);
  875. // 解析 JSON 响应
  876. $WeatherInfo = json_decode($response, true);
  877. // 缓存结果,设置1小时过期
  878. $cache->set($cacheKey, serialize($WeatherInfo), 3600);
  879. if($WeatherInfo){
  880. return Result::success($WeatherInfo);
  881. }else{
  882. return Result::error("获取天气失败", 0);
  883. }
  884. }
  885. /**
  886. * 获取农历信息
  887. * @return void
  888. */
  889. public function getCalendar(array $data) :array
  890. {
  891. $calendar = new Calendar();
  892. $result = $calendar->solar($data['year'], $data['month'], $data['day'], $data['hour']); // 阳历
  893. return Result::success($result);
  894. }
  895. public function addSector(array $data): array
  896. {
  897. unset($data['user_id']);
  898. // $data['page_type'] = json_encode($data['page_type']);
  899. $result = Sector::insertGetId($data);
  900. return Result::success();
  901. }
  902. public function delSector(array $data): array
  903. {
  904. $result = Sector::where('id', $data['id'])->delete();
  905. if ($result == 1) {
  906. return Result::success('删除成功');
  907. } else {
  908. return Result::error('删除失败');
  909. }
  910. }
  911. public function updateSector(array $data): array
  912. {
  913. unset($data['user_id']);
  914. $result = Sector::where('id', $data['id'])->update($data);
  915. if ($result == 1) {
  916. return Result::success('修改成功');
  917. } else {
  918. return Result::error('修改失败');
  919. }
  920. }
  921. public function getComponentList(array $data): array
  922. {
  923. var_dump($data, '---------');
  924. $where = [];
  925. // $where[] = ['sector.id', '=', $data['id']];
  926. if (!empty($data['template_class_id'])) {
  927. $where['template_class.id'] = $data['template_class_id'];
  928. }
  929. if (!empty($data['component_name'])) {
  930. $where['component.component_name'] = $data['component_name'];
  931. }
  932. if (!empty($data['sector_id'])) {
  933. $where['sector.id'] = $data['sector_id'];
  934. };
  935. $result = Component::where($where)
  936. ->leftJoin('template', 'template.id', '=', 'component.template_id')
  937. ->leftJoin('template_class', 'template_class.id', '=', 'template.template_class_id') // 添加这一行
  938. ->leftJoin('sector', 'sector.id', '=', 'component.sector_id')
  939. ->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') // 修改这一行)
  940. ->orderBy('sector.updated_at', 'desc')
  941. ->orderBy('sector.created_at', 'desc')
  942. ->paginate($data['page_size'], ['*'], 'mypage_name', $data['page']);
  943. return Result::success($result);
  944. }
  945. public function getComponentInfo(array $data): array
  946. {
  947. $where = [];
  948. $result = Component::where($where)
  949. ->leftJoin('template', 'template.id', '=', 'component.template_id')
  950. ->leftJoin('template_class', 'template_class.id', '=', 'template.template_class_id') // 添加这一行
  951. ->leftJoin('sector', 'sector.id', '=', 'component.sector_id')
  952. ->select('template_class.name as template_class_name', 'template.template_name as template_name', 'sector.sector_name as sector_name', 'component.*')
  953. ->get();
  954. return Result::success($result);
  955. }
  956. public function addComponent(array $data): array
  957. {
  958. unset($data['user_id']);
  959. $result = Component::insertGetId($data);
  960. if ($result) {
  961. return Result::success($result);
  962. } else {
  963. return Result::error('添加失败');
  964. }
  965. }
  966. public function delComponent(array $data): array
  967. {
  968. $result = Component::where('id', $data['id'])->delete();
  969. return Result::success($result);
  970. }
  971. public function updateComponent(array $data): array
  972. {
  973. $result = Component::where('id', $data['id'])->update($data);
  974. return Result::success($result);
  975. }
  976. public function getWebsiteTemplateList(array $data): array
  977. {
  978. $where = [];
  979. $result = WebsiteTemplateInfo::where($where)
  980. ->leftJoin('website', 'website_template_info.website_id', '=', 'website.id')
  981. ->select('website_template_info.*', 'website.website_name')
  982. ->orderBy('website_template_info.id', 'desc')
  983. ->paginate($data['page_size'], ['*'], 'page', $data['page']);
  984. if ($result) {
  985. return Result::success($result);
  986. } else {
  987. return Result::error('暂无数据');
  988. }
  989. }
  990. public function getWebsiteTemplateInfo(array $data)
  991. {
  992. $where = [];
  993. if (isset($data['id'])) {
  994. $where[] = ['id', '=', $data['id']];
  995. }
  996. $result = WebsiteTemplateInfo::where($where)
  997. ->leftJoin('website', 'website_template_info.website_id', '=', 'website.id')
  998. ->leftJoin('website_template', 'website_template_info.template_id', '=', 'website_template.id')
  999. ->select('website_template_info.*', 'website.website_name')
  1000. ->get();
  1001. if ($result) {
  1002. return Result::success($result);
  1003. } else {
  1004. return Result::error('暂无数据');
  1005. }
  1006. }
  1007. }