Index.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace app\controller\apps\weather;
  3. use app\model\CardModel;
  4. use app\PluginsBase;
  5. use IP2Location\Database;
  6. $directory = (__DIR__ . '/vendor/IP2Location/src');
  7. $files = glob($directory . '/*.php');
  8. foreach ($files as $file) {
  9. require_once $file;
  10. }
  11. class Index extends PluginsBase
  12. {
  13. public $gateway = '';
  14. function _initialize()
  15. {
  16. parent::_initialize();
  17. $this->gateway = CardModel::config("weather", "gateway", "https://devapi.qweather.com");
  18. }
  19. function ip(): \think\response\Json
  20. {
  21. $file = __DIR__ . '/ipLocation/IP2LOCATION-LITE-DB5.BIN';
  22. if (file_exists($file)) {
  23. $ip = getRealIp();
  24. $db = new Database($file, Database::FILE_IO);
  25. try {
  26. $records = $db->lookup($ip, Database::ALL);
  27. $ipInfo = [
  28. 'ipAddress' => $records['ipAddress'],
  29. 'latitude' => $records['latitude'],
  30. 'longitude' => $records['longitude'],
  31. 'cityName' => $records['cityName'],
  32. 'regionName' => $records['regionName'],
  33. 'countryName' => $records['countryName']
  34. ];
  35. if ($ipInfo['latitude'] == 0 && $ipInfo['longitude'] == 0) {
  36. $ipInfo['latitude'] = 39.91;
  37. $ipInfo['longitude'] = 116.41;
  38. }
  39. return json(['code' => 1, 'msg' => 'success', 'data' => $ipInfo]);
  40. } catch (\Exception $e) {
  41. return json(['code' => 0, 'msg' => '不支持ipv6']);
  42. }
  43. }
  44. return json(['code' => 0, 'msg' => '地理位置数据包不存在']);
  45. }
  46. function setting()
  47. {
  48. $this->getAdmin();
  49. if ($this->request->isPost()) {
  50. $form = $this->request->post();
  51. CardModel::saveConfigs("weather", $form);
  52. return $this->success("保存成功");
  53. }
  54. if ($this->request->isPut()) {
  55. $form = CardModel::configs('weather');
  56. return $this->success('ok', $form);
  57. }
  58. return $this->fetch("setting.html");
  59. }
  60. function everyDay(): \think\response\Json
  61. {
  62. $location = $this->request->get("location", "101010100");
  63. try {
  64. $result = \Axios::http()->get($this->gateway . '/v7/weather/7d', [
  65. 'query' => [
  66. 'location' => $location,
  67. 'key' => CardModel::config('weather', 'key'),
  68. ]
  69. ]);
  70. if ($result->getStatusCode() === 200) {
  71. $json = \Axios::toJson($result->getBody()->getContents());
  72. if ($json && $json['code'] == "200") {
  73. return $this->success($json['daily']);
  74. }
  75. }
  76. } catch (\Exception $e) {
  77. }
  78. return $this->error("数据获取错误");
  79. }
  80. function now(): \think\response\Json
  81. {
  82. $location = $this->request->get('location', '101010100');
  83. try {
  84. $result = \Axios::http()->get($this->gateway . '/v7/weather/now', [
  85. 'query' => [
  86. 'location' => $location,
  87. 'key' => CardModel::config('weather', 'key'),
  88. ]
  89. ]);
  90. if ($result->getStatusCode() === 200) {
  91. $json = \Axios::toJson($result->getBody()->getContents());
  92. if ($json && $json['code'] == '200') {
  93. return $this->success($json['now']);
  94. }
  95. }
  96. } catch (\Exception $e) {
  97. }
  98. return $this->error('数据获取错误');
  99. }
  100. function locationToCity(): \think\response\Json
  101. {
  102. $location = $this->request->all('location', '101010100');
  103. try {
  104. $result = \Axios::http()->get('https://geoapi.qweather.com/v2/city/lookup', [
  105. 'query' => [
  106. 'location' => $location,
  107. 'key' => CardModel::config('weather', 'key'),
  108. ]
  109. ]);
  110. if ($result->getStatusCode() === 200) {
  111. $json = \Axios::toJson($result->getBody()->getContents());
  112. if ($json && $json['code'] == '200') {
  113. if (count($json['location']) > 0) {
  114. return $this->success($json['location'][0]);
  115. }
  116. }
  117. }
  118. } catch (\Exception $e) {
  119. }
  120. return $this->error('数据获取错误');
  121. }
  122. function citySearch(): \think\response\Json
  123. {
  124. $city = $this->request->post("city", "");
  125. if (trim($city)) {
  126. try {
  127. $result = \Axios::http()->get('https://geoapi.qweather.com/v2/city/lookup', [
  128. 'query' => [
  129. 'location' => $city,
  130. 'key' => CardModel::config('weather', 'key'),
  131. ]
  132. ]);
  133. if ($result->getStatusCode() === 200) {
  134. $json = \Axios::toJson($result->getBody()->getContents());
  135. if ($json && $json['code'] == '200') {
  136. if (count($json['location']) > 0) {
  137. return $this->success($json['location']);
  138. }
  139. }
  140. }
  141. } catch (\Exception $e) {
  142. return $this->error($e->getMessage());
  143. }
  144. }
  145. return $this->error('数据获取错误');
  146. }
  147. }