Browse Source

发布动态需要ip获取地址

lizhen_gitee 10 months ago
parent
commit
7e96959029
2 changed files with 46 additions and 5 deletions
  1. 7 5
      application/api/controller/Topicdongtai.php
  2. 39 0
      application/common/controller/Api.php

+ 7 - 5
application/api/controller/Topicdongtai.php

@@ -310,7 +310,7 @@ class Topicdongtai extends Api
         $content = input('content','', 'trim');
         $images = input('images','', 'trim');
         $show_real = input('show_real', 0, 'intval'); //是否标记真人:0=否,1=是
-        $address = input('address', '', 'trim'); //位置
+        //$address = input('address', '', 'trim'); //位置
         $topic_id = input('topic_id', 0, 'intval'); //热门话题id
         $type = input('type', 0, 'intval'); //类型:0=文字,1=图片,2=视频
         if(!$content && !$images){
@@ -322,9 +322,9 @@ class Topicdongtai extends Api
         if ($show_real == 1 && $this->auth->real_status != 1) { //验证是否已经通过真人认证
             $this->error('您尚未通过真人认证,暂不能标记真人');
         }
-        if (iconv_strlen($address, 'utf-8') > 255) {
+        /*if (iconv_strlen($address, 'utf-8') > 255) {
             $this->error('请选择正确位置');
-        }
+        }*/
         if ($topic_id) {
             $topic_info = Db::name('topic_hub')->where(['id' => $topic_id])->find();
             if (!$topic_info) {
@@ -341,13 +341,15 @@ class Topicdongtai extends Api
         //关键字替换
         $content = Keyworld::sensitive($content);
 
+        $address = $this->ip_to_address();
+
         $data = [
             'topic_id' => $topic_id,
             'user_id' => $this->auth->id,
             'content' => $content,
             'images' => $images,
-            'longitude' => input('longitude',''),
-            'latitude'  => input('latitude',''),
+//            'longitude' => input('longitude',''),
+//            'latitude'  => input('latitude',''),
             'createtime' => time(),
             'updatetime' => time(),
             'is_show_real' => $show_real,

+ 39 - 0
application/common/controller/Api.php

@@ -387,6 +387,45 @@ class Api
         }
     }
 
+    //ip获取地址
+    protected  function ip_to_address(){
+
+        try{
+            $ip = request()->ip();
+            //$ip = '182.37.138.94';
+
+            // http协议:http://api.ip138.com/ip/
+            // https协议:https://api.ip138.com/ip/
+
+            $data = '';
+
+            $url = 'http://api.ip138.com/ip/?ip='.$ip.'&datatype=jsonp&token=19fc471ea42124e6d1f7e4e473ef2647'; //15811816496   huang19871217
+            $result = json_decode(curl_get($url),true);
+            //dump($result);exit;
+
+            if(is_array($result) && !empty($result)){
+                if(isset($result['ret']) && $result['ret'] == 'ok'){
+                    if(isset($result['data']) && is_array($result['data']) && !empty($result['data'])){
+
+                        if(isset($result['data'][1]) && !empty($result['data'][1])){
+                            $data .= $result['data'][1];
+                        }
+                        if(isset($result['data'][2]) && !empty($result['data'][2])){
+                            $data .= $result['data'][2];
+                        }
+
+                    }
+                }
+            }
+            //dump($data);
+            return $data;
+        }catch (Exception $e) {
+            return [];
+        }
+
+        return [];
+    }
+
     protected function request_log_update($log_result){
 
         if(defined('API_REQUEST_ID')) { //记录app正常返回结果