PublicRpcService.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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. #[RpcService(name: "PublicRpcService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  19. class PublicRpcService implements PublicRpcServiceInterface
  20. {
  21. #[Inject]
  22. protected Redis $redis;
  23. /**
  24. * @param array $data
  25. * @return array
  26. */
  27. public function getDistrictList(array $data): array
  28. {
  29. $where = [];
  30. if(isset($data['keyWord'])){
  31. $where = [
  32. ['name','like','%'.$data['keyWord'].'%']
  33. ];
  34. }
  35. $result = [];
  36. if(isset($data['pageSize'])){
  37. $rep = District::where($where)->limit($data['pageSize'])->offset(($data['page']-1)*$data['pageSize'])->orderBy("code","asc")->get();
  38. $count = District::where($where)->count();
  39. $result = [
  40. 'rows'=>$rep,
  41. 'count'=>$count
  42. ];
  43. }else{
  44. $result = District::where($data)->orderBy("code","asc")->get();
  45. }
  46. return $result?Result::success($result):Result::error("没有查到数据");
  47. }
  48. /**
  49. * @param array $data
  50. * @return array
  51. */
  52. public function getUserLevelList(array $data): array
  53. {
  54. $where = [];
  55. if (isset($data['keyWord'])) {
  56. $where = [
  57. ['name', 'like', '%' . $data['keyWord'] . '%'],
  58. ];
  59. }
  60. $result = [];
  61. if (isset($data['pageSize'])) {
  62. $rep = UserLevel::where($where)->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->orderBy("id", "asc")->get();
  63. $count = UserLevel::where($where)->count();
  64. $result = [
  65. 'rows' => $rep,
  66. 'count' => $count,
  67. ];
  68. } else {
  69. $result = UserLevel::orderBy("id", "asc")->get();
  70. }
  71. return $result ? Result::success($result) : Result::error("没有查到数据");
  72. }
  73. /**
  74. * 添加用户等级
  75. * @param array $data
  76. * @return array
  77. */
  78. public function addUserLevel(array $data): array
  79. {
  80. LevelUser::insertGetId($data);
  81. return Result::success([]);
  82. }
  83. /**
  84. * 更新等级
  85. * @param array $data
  86. * @return array
  87. */
  88. public function updateUserLevel(array $data): array
  89. {
  90. $result = LevelUser::where(['id' => $data['id']])->update($data);
  91. if ($result) {
  92. return Result::success($result);
  93. }
  94. return Result::error("更新失败");
  95. }
  96. /**
  97. * 删除等级
  98. * @param array $data
  99. * @return array
  100. */
  101. public function delUserLevel(array $data): array
  102. {
  103. $result = LevelUser::where(['id' => $data['id']])->delete();
  104. if ($result) {
  105. return Result::success($result);
  106. }
  107. return Result::error("删除失败");
  108. }
  109. /**
  110. * 查询投诉举报信息
  111. * @param array $data
  112. * @return array
  113. */
  114. public function getLetterOfComplaint(array $data = []): array
  115. {
  116. var_dump("====");
  117. $where = [];
  118. if (isset($data['user_id']) && !empty($data['user_id'])) {
  119. array_push($where, ['letter_of_complaint.user_id', '=', $data['user_id']]);
  120. }
  121. if (isset($data['nature']) && !empty($data['nature'])) {
  122. array_push($where, ['letter_of_complaint.nature', '=', $data['nature']]);
  123. }
  124. if (isset($data['nature_level0']) && !empty($data['nature_level0'])) {
  125. array_push($where, ['letter_of_complaint.nature_level0', '=', $data['nature_level0']]);
  126. }
  127. if (isset($data['status']) && !empty($data['status'])) {
  128. array_push($where, ['letter_of_complaint.status', '=', $data['status']]);
  129. }
  130. $result = [];
  131. if (isset($data['pageSize'])) {
  132. $rep = LetterOfComplaint::where($where)
  133. ->leftJoin("letter_type as type_a", "letter_of_complaint.nature", "type_a.id")
  134. ->leftJoin("letter_type as type_c", "letter_of_complaint.nature_level0", "type_c.id")
  135. ->leftJoin("letter_type as type_b", "letter_of_complaint.nature_level1", "type_b.id")
  136. ->leftJoin("letter_type as type_e", "letter_of_complaint.nature_level3", "type_e.id")
  137. ->leftJoin("letter_type as type_d", "letter_of_complaint.status", "type_d.id")
  138. ->select("letter_of_complaint.*",
  139. "type_a.type_name as nature_name",
  140. "type_b.type_name as nature_name1",
  141. "type_c.type_name as nature_name0",
  142. "type_d.type_name as status_name",
  143. "type_e.type_name as nature_name3")
  144. ->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->orderBy("letter_of_complaint.id", "desc")->get();
  145. $count = LetterOfComplaint::where($where)->count();
  146. if ($rep) {
  147. foreach ($rep as $val) {
  148. if ($val['judgment']) {
  149. $val['judgment'] = json_decode($val['judgment']);
  150. }
  151. if ($val['audio_and_video']) {
  152. $val['audio_and_video'] = json_decode($val['audio_and_video']);
  153. }
  154. if ($val['contract']) {
  155. $val['contract'] = json_decode($val['contract']);
  156. }
  157. if ($val['qualifications']) {
  158. $val['qualifications'] = json_decode($val['qualifications']);
  159. }
  160. }
  161. }
  162. $result = [
  163. 'rows' => $rep,
  164. 'count' => $count,
  165. ];
  166. } else {
  167. $result = LetterOfComplaint::where($where)
  168. ->leftJoin("letter_type as type_a", "letter_of_complaint.nature", "type_a.id")
  169. ->leftJoin("letter_type as type_c", "letter_of_complaint.nature_level0", "type_c.id")
  170. ->leftJoin("letter_type as type_b", "letter_of_complaint.nature_level1", "type_b.id")
  171. ->leftJoin("letter_type as type_e", "letter_of_complaint.nature_level3", "type_e.id")
  172. ->leftJoin("letter_type as type_d", "letter_of_complaint.status", "type_d.id")
  173. ->select("letter_of_complaint.*",
  174. "type_a.type_name as nature_name",
  175. "type_b.type_name as nature_name1",
  176. "type_c.type_name as nature_name0",
  177. "type_d.type_name as status_name",
  178. "type_e.type_name as nature_name3")
  179. ->orderBy("letter_of_complaint.id", "desc")->get();
  180. }
  181. return $result ? Result::success($result) : Result::error("没有查到数据");
  182. }
  183. /**
  184. * 添加投诉举报信息
  185. * @param array $data
  186. * @return array
  187. */
  188. public function addLetterOfComplaint(array $data): array
  189. {
  190. $data['judgment'] = $data['judgment'] ? json_encode($data['judgment']) : '';
  191. $data['audio_and_video'] = $data['audio_and_video'] ? json_encode($data['audio_and_video']) : '';
  192. $data['contract'] = $data['contract'] ? json_encode($data['contract']) : '';
  193. $data['qualifications'] = $data['qualifications'] ? json_encode($data['qualifications']) : '';
  194. unset($data['id']);
  195. $result = LetterOfComplaint::insertGetId($data);
  196. if (empty($result)) {
  197. return Result::error("创建失败", 0);
  198. } else {
  199. return Result::success(["id" => $result]);
  200. }
  201. }
  202. /**
  203. * 用户端更新投诉举报
  204. * @param array $data
  205. * @return array
  206. */
  207. public function userUpLetterOfComplaint(array $data): array
  208. {
  209. $data['judgment'] = $data['judgment'] ? json_encode($data['judgment']) : '';
  210. $data['audio_and_video'] = $data['audio_and_video'] ? json_encode($data['audio_and_video']) : '';
  211. $data['contract'] = $data['contract'] ? json_encode($data['contract']) : '';
  212. $data['qualifications'] = $data['qualifications'] ? json_encode($data['qualifications']) : '';
  213. $result = LetterOfComplaint::where(['id' => $data['id']])->update($data);
  214. if (empty($result)) {
  215. return Result::error("创建失败", 0);
  216. } else {
  217. return Result::success(["id" => $result]);
  218. }
  219. }
  220. /**
  221. * 管理后台更新投诉举报信息
  222. * @param array $data
  223. * @return array
  224. */
  225. public function upLetterOfComplaint(array $data): array
  226. {
  227. var_dump("admin:", $data);
  228. $where = [
  229. 'id' => $data['id'],
  230. ];
  231. $filtered_array = array_filter($data, function ($value) {
  232. return $value !== "" && $value !== null && $value !== false && !is_array($value) || !empty($value);
  233. });
  234. $filtered_array['judgment'] = isset($filtered_array['judgment']) ? json_encode($filtered_array['judgment']) : '';
  235. $filtered_array['audio_and_video'] = isset($filtered_array['audio_and_video']) ? json_encode($filtered_array['audio_and_video']) : '';
  236. $filtered_array['contract'] = isset($filtered_array['contract']) ? json_encode($filtered_array['contract']) : '';
  237. $filtered_array['qualifications'] = isset($filtered_array['qualifications']) ? json_encode($filtered_array['qualifications']) : '';
  238. unset($filtered_array['nature_name']);
  239. unset($filtered_array['type_name']);
  240. unset($filtered_array['nature_level_name']);
  241. unset($filtered_array['status_name']);
  242. unset($filtered_array['is_admin']);
  243. unset($filtered_array['type_level_name']);
  244. $result = LetterOfComplaint::where($where)->update($filtered_array);
  245. if ($result) {
  246. return Result::success($result);
  247. }
  248. return Result::error("更新失败", 0);
  249. }
  250. /**
  251. * 查询投诉举报记录
  252. * @param array $data
  253. * @return array
  254. */
  255. public function getLetterOfComplaintInfo(array $data): array
  256. {
  257. $where = [
  258. 'letter_of_complaint.id' => $data['id'],
  259. ];
  260. if (isset($data['user_id']) && !empty($data['user_id'])) {
  261. array_push($where, ['letter_of_complaint.user_id', '=', $data['user_id']]);
  262. }
  263. $result = LetterOfComplaint::where($where)
  264. ->leftJoin("letter_type as type_a", "letter_of_complaint.nature", "type_a.id")
  265. ->leftJoin("letter_type as type_c", "letter_of_complaint.nature_level0", "type_c.id")
  266. ->leftJoin("letter_type as type_b", "letter_of_complaint.nature_level1", "type_b.id")
  267. ->leftJoin("letter_type as type_e", "letter_of_complaint.nature_level3", "type_e.id")
  268. ->leftJoin("letter_type as type_d", "letter_of_complaint.status", "type_d.id")
  269. ->select("letter_of_complaint.*",
  270. "type_a.type_name as nature_name",
  271. "type_b.type_name as nature_name1",
  272. "type_c.type_name as nature_name0",
  273. "type_d.type_name as status_name",
  274. "type_e.type_name as nature_name3")
  275. ->first();
  276. return Result::success($result);
  277. }
  278. /**
  279. * 删除投诉举报信息
  280. * @param array $data
  281. * @return array
  282. */
  283. public function delLetterOfComplaint(array $data): array
  284. {
  285. $result = LetterOfComplaint::when($data,function ($query) use ($data){
  286. if(isset($data['id']) && !empty($data['id'])){
  287. $query->where(['id'=>$data['id']]);
  288. }
  289. if(isset($data['user_id']) && !empty($data['user_id'])){
  290. $query->where(['user_id'=>$data['user_id']]);
  291. }
  292. })->delete();
  293. if (empty($result)) {
  294. return Result::error("删除失败", 0);
  295. } else {
  296. return Result::success();
  297. }
  298. }
  299. /**
  300. * 获取举报信息类型
  301. * @param array $data
  302. * @return array
  303. */
  304. public function getLetterType(array $data): array
  305. {
  306. $where = [];
  307. if (isset($data['type'])) {
  308. array_push($where, ['type', '=', $data['type']]);
  309. }
  310. if (isset($data['pid']) && $data['pid']>0) {
  311. array_push($where, ['pid', '=', $data['pid']]);
  312. }
  313. $result = LetterType::where($where)->orderBy('sort','asc')->get();
  314. return $result ? Result::success($result) : Result::error("没有查到数据");
  315. }
  316. /**
  317. * 更新举报类型
  318. * @param array $data
  319. * @return array
  320. */
  321. public function upLetterType(array $data): array
  322. {
  323. return [];
  324. }
  325. /**
  326. * 添加举报类型
  327. * @param array $data
  328. * @return array
  329. */
  330. public function addLetterType(array $data): array
  331. {
  332. $result = LetterType::insertGetId($data);
  333. if (empty($result)) {
  334. return Result::error("创建失败", 0);
  335. } else {
  336. return Result::success(["id" => $result]);
  337. }
  338. }
  339. /**
  340. * 删除举报类型
  341. * @param array $data
  342. * @return array
  343. */
  344. public function delLetterType(array $data): array
  345. {
  346. $result = LetterType::where('id', $data['id'])->delete();
  347. if (empty($result)) {
  348. return Result::error("删除失败", 0);
  349. } else {
  350. return Result::success();
  351. }
  352. }
  353. /**
  354. * 检测是否已经被接案
  355. * @param array $data
  356. * @return array
  357. */
  358. public function checkMeasure(array $data): array
  359. {
  360. $where = [
  361. 'id' => $data['id'],
  362. ];
  363. $letterOfComplaintInfo = LetterOfComplaint::where($where)->first();
  364. var_dump("查询数据:", $letterOfComplaintInfo['admin_id'], $data['user_id']);
  365. //操作人和当前登陆用户id 相等说明是当前人接收的案件
  366. if (($letterOfComplaintInfo['admin_id'] == $data['user_id']) || empty($letterOfComplaintInfo['admin_id'])) {
  367. return Result::success();
  368. } else {
  369. return Result::error("您不能处理其他人已经接过的案件", 0);
  370. }
  371. }
  372. /**
  373. * 后台获取职能部门
  374. * @param array $data
  375. * @return array
  376. */
  377. public function getZhinengbumenList(array $data): array
  378. {
  379. // 获取分页参数,默认每页 10 条记录
  380. $page = isset($data['page']) ? (int) $data['page'] : 1;
  381. $perPage = isset($data['pagesize']) ? (int) $data['pagesize'] : 10;
  382. // 查询数据并分页
  383. $query = Department::query();
  384. // 可以在这里添加更多的查询条件
  385. if (isset($data['search'])) {
  386. $query->where('name', 'like', '%' . $data['search'] . '%');
  387. }
  388. // 执行分页查询
  389. $result = $query->paginate($perPage, ['*'], 'page', $page);
  390. // 返回分页结果
  391. return Result::success([
  392. 'count' => $result->total(),
  393. 'current_page' => $result->currentPage(),
  394. 'last_page' => $result->lastPage(),
  395. 'pagesize' => $result->perPage(),
  396. 'rows' => $result->items(),
  397. ]);
  398. }
  399. /**
  400. * 添加获取职能部门
  401. * @param array $data
  402. * @return array
  403. */
  404. public function addZhinengbumen(array $data): array
  405. {
  406. $result = Department::insertGetId($data);
  407. if (empty($result)) {
  408. return Result::error("创建失败", 0);
  409. } else {
  410. return Result::success(["id" => $result]);
  411. }
  412. }
  413. public function delZhinengbumen(array $data): array
  414. {
  415. $result = Department::where('id', $data['id'])->delete();
  416. if (empty($result)) {
  417. return Result::error("删除失败", 0);
  418. } else {
  419. return Result::success();
  420. }
  421. }
  422. public function getZhinengbumen(array $data): array
  423. {
  424. $result = Department::where('id', $data['id'])->first();
  425. return Result::success($result);
  426. }
  427. public function getPidZhinengbumen(array $data): array
  428. {
  429. if (empty($data['pid'])) {
  430. $data['pid'] = 0;
  431. }
  432. $result = Department::where('pid', $data['pid'])->get();
  433. return Result::success($result);
  434. }
  435. public function modZhinengbumen(array $data): array
  436. {
  437. $result = Department::where('id', $data['id'])->update($data);
  438. if (empty($result)) {
  439. return Result::error("修改失败", 0);
  440. } else {
  441. return Result::success();
  442. }
  443. }
  444. /**
  445. * 查询职能列表
  446. * @param array $data
  447. * @return array
  448. */
  449. public function getDepartment(array $data) :array
  450. {
  451. $where = [];
  452. if(isset($data['pid'])){
  453. $where = [
  454. 'pid'=>$data['pid']??0
  455. ];
  456. }
  457. $result = Department::when(!empty($where),function ($query) use ($where){
  458. $query->where($where);
  459. })->orderBy("sort","desc")->get();
  460. if (empty($result)) {
  461. return Result::error("查询失败", 0);
  462. }else{
  463. return Result::success($result);
  464. }
  465. }
  466. /**
  467. * 获取所有的buckets
  468. * @param array $data
  469. * @return array
  470. */
  471. public function getBuckets(array $data) :array
  472. {
  473. $result = new MinioService();
  474. // 调用服务层的方法获取存储桶列表
  475. $bucketsResponse =$result->listBuckets();
  476. // 直接返回服务层生成的响应
  477. return Result::success($bucketsResponse['data']);
  478. }
  479. /**
  480. * 上传文件
  481. * @param array $data
  482. * @return array
  483. */
  484. public function uploadFile(array $data) :array
  485. {
  486. $result = new MinioService();
  487. $rep = $result->uploadFile($data);
  488. if($rep['code']==200){
  489. return Result::success($rep['data']);
  490. }else{
  491. return Result::error("上传失败!");
  492. }
  493. }
  494. /**
  495. * 黑名单管理
  496. * @param array $data
  497. * @return array
  498. */
  499. public function getBlackWordList(array $data) :array
  500. {
  501. $result = BlackWord::when($data, function ($query) use ($data) {
  502. if (isset($data['name']) && $data['name']){
  503. $query->where('black_word.name', 'like', '%'.$data['name'].'%');
  504. }
  505. })->orderBy('black_word.id','desc')
  506. ->paginate(intval($data['pageSize']),
  507. [
  508. 'black_word.*',
  509. ],
  510. 'page', intval($data['page']));
  511. $count = $result->total();
  512. $returnData = [
  513. 'rows'=>$result->items(),
  514. 'count'=>$count
  515. ];
  516. return Result::success($returnData);
  517. }
  518. /**
  519. * 添加黑名单
  520. * @param array $data
  521. * @return array
  522. */
  523. public function addBlackWord(array $data) :array
  524. {
  525. Db::beginTransaction();
  526. try {
  527. $info = BlackWord::where(['name'=>$data['name']])->first();
  528. if($info){
  529. Db::rollBack();
  530. return Result::error("该黑名单已存在", 0);
  531. }
  532. $data['type'] = 10;
  533. $result = BlackWord::insertGetId($data);
  534. $redisKey = 'black_word';
  535. $this->redis->sAdd($redisKey, $data['name']);
  536. Db::commit();
  537. return Result::success(["id" => $result]);
  538. }catch (\Exception $e){
  539. Db::rollBack();
  540. return Result::error("创建失败".$e->getMessage(), 0);
  541. }
  542. }
  543. /**
  544. * 删除黑名单
  545. * @param array $data
  546. * @return array
  547. */
  548. public function delBlackWord(array $data) :array
  549. {
  550. Db::beginTransaction();
  551. try {
  552. BlackWord::where(['name'=>$data['name']])->delete();
  553. $redisKey = 'black_word';
  554. $this->redis->sRem($redisKey, $data['name']);
  555. Db::commit();
  556. return Result::success([]);
  557. }catch (\Exception $e){
  558. Db::rollBack();
  559. return Result::error("删除失败".$e->getMessage(), 0);
  560. }
  561. }
  562. /**
  563. * 修改违禁词
  564. * @param array $data
  565. * @return array
  566. */
  567. public function upBlackWord(array $data) :array
  568. {
  569. Db::beginTransaction();
  570. try {
  571. $checkInfo = BlackWord::where(['name'=>$data['name']])->first();
  572. if($checkInfo){
  573. Db::rollBack();
  574. return Result::error("该违禁词已经存在", 0);
  575. }
  576. $blackWordInfo = BlackWord::where(['id'=>$data['id']])->first();
  577. if($blackWordInfo){
  578. //先删除redis
  579. $blackWordInfo = $blackWordInfo->toArray();
  580. $redisKey = 'black_word';
  581. $this->redis->sRem($redisKey, $blackWordInfo['name']);
  582. $this->redis->sAdd($redisKey, $data['name']);
  583. BlackWord::where(['id'=>$data['id']])->update(['name'=>$data['name']]);
  584. Db::commit();
  585. return Result::success([]);
  586. }else{
  587. Db::rollBack();
  588. return Result::error("系统错误", 0);
  589. }
  590. }catch (\Exception $e){
  591. Db::rollBack();
  592. return Result::error("修改失败".$e->getMessage(), 0);
  593. }
  594. }
  595. /**
  596. * 获取经纬度信息
  597. * @return void
  598. */
  599. public function getIpInfo(array $data) :array
  600. {
  601. $client_ip = isset($data['ip']) && $data['ip']??$_SERVER['REMOTE_ADDR'];
  602. // 使用 IPinfo 服务获取 IP 信息
  603. $api_url = "https://ipinfo.io/{$client_ip}/json";
  604. $ch = curl_init($api_url);
  605. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  606. $response = curl_exec($ch);
  607. curl_close($ch);
  608. // 解析 JSON 响应
  609. $ip_info = json_decode($response, true);
  610. // 提取地址和经纬度
  611. if ($ip_info && !isset($ip_info['bogon'])) {
  612. $latitude = explode(',', $ip_info['loc'])[0];
  613. $longitude = explode(',', $ip_info['loc'])[1];
  614. $ip_info['latitude'] = $latitude;
  615. $ip_info['longitude'] = $longitude;
  616. return Result::success($ip_info);
  617. } else {
  618. $data['ip'] = '101.254.114.212';
  619. $this->getIpinfo(["ip"=>$data['ip']]);
  620. }
  621. }
  622. /**
  623. * 获取天气
  624. * @param array $data
  625. * @return array
  626. */
  627. public function getWeatherInfo(array $data) :array
  628. {
  629. $month = $data['month'] ?? date('m');
  630. $day = $data['day'] ?? date('d');
  631. // 使用缓存键
  632. $cacheKey = "tsbb_data_weather_{$month}_{$day}";
  633. // 尝试从缓存获取数据
  634. $container = \Hyperf\Context\ApplicationContext::getContainer();
  635. $cache = $container->get(\Psr\SimpleCache\CacheInterface::class);
  636. if ($cachedData = $cache->get($cacheKey)) {
  637. return Result::success(unserialize($cachedData));
  638. }
  639. $location = $data['latitude'].":".$data['longitude'];
  640. $api_url = "https://api.seniverse.com/v3/weather/now.json?key=".\Hyperf\Support\env('WEATHER_KEY')."&location=".$location."&language=zh-Hans&unit=c";
  641. $ch = curl_init($api_url);
  642. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  643. $response = curl_exec($ch);
  644. curl_close($ch);
  645. // 解析 JSON 响应
  646. $WeatherInfo = json_decode($response, true);
  647. // 缓存结果,设置1小时过期
  648. $cache->set($cacheKey, serialize($WeatherInfo), 3600);
  649. if($WeatherInfo){
  650. return Result::success($WeatherInfo);
  651. }else{
  652. return Result::error("获取天气失败", 0);
  653. }
  654. }
  655. /**
  656. * 获取农历信息
  657. * @return void
  658. */
  659. public function getCalendar(array $data) :array
  660. {
  661. $calendar = new Calendar();
  662. $result = $calendar->solar($data['year'], $data['month'], $data['day'], $data['hour']); // 阳历
  663. return Result::success($result);
  664. }
  665. }