Ver Fonte

短地址修改

523013183@qq.com há 2 anos atrás
pai
commit
b9f0df676a

+ 20 - 0
app/Api/Controllers/ApiController.php

@@ -37,4 +37,24 @@ class ApiController extends ApiBaseController
         ]);
         return $this->apiService->getShortUrl($request->input("url"));
     }
+
+    /**
+     * @param string url 生成短地址的url
+     * @return array
+     * @throws \App\Base\Exceptions\ApiException
+     * @throws
+     * @api get /api/short-hits 获取短地址访问量
+     * @group 短地址
+     * @successExample
+     * {"ret":0,"msg":"success.","data":[]}
+     */
+    public function getShortUrlHits(Request $request)
+    {
+        $this->validate($request, [
+            'url' => 'required'
+        ], [
+            'url.required' => "url不能为空"
+        ]);
+        return $this->apiService->getShortUrlHits($request->input("url"));
+    }
 }

+ 9 - 0
app/Api/Services/ApiService.php

@@ -5,6 +5,7 @@ namespace App\Api\Services;
 
 use App\Base\Library\Shorty;
 use App\Base\Services\ApiBaseService;
+use App\Basic\Facades\UrlsFacade;
 use Illuminate\Support\Facades\Cache;
 
 class ApiService extends ApiBaseService
@@ -48,4 +49,12 @@ class ApiService extends ApiBaseService
             return $list;
         }
     }
+
+    /**
+     * 获取短地址访问量
+     */
+    public function getShortUrlHits($urls)
+    {
+        return UrlsFacade::getShortUrlHits($urls);
+    }
 }

+ 1 - 0
app/Api/routes.php

@@ -11,6 +11,7 @@ $app->group([
     // 生成短地址
     $app->get('/short', ['uses' => 'ApiController@getShortUrl']);
     $app->post('/short', ['uses' => 'ApiController@getShortUrl']);
+    $app->get('/short-hits', ['uses' => 'ApiController@getShortUrlHits']);
 });
 
 $app->group([

+ 38 - 0
app/Basic/Services/UrlsService.php

@@ -43,4 +43,42 @@ class UrlsService extends BaseService
         }
         return $this->urlsLogModel->findOneBy($map, $fields);
     }
+
+    /**
+     * 获取短地址详情
+     */
+    public function getShortUrlHits($urls)
+    {
+        $map = [];
+        $keys = $this->getKeyByUrls($urls);
+        if (is_array($urls)) {
+            $map['key'] = ['in', $keys];
+        } else {
+            $map['key'] = $keys;
+        }
+        $list = $this->findBy($map, '`key`,hits,uv');
+        if (is_array($urls)) {
+            return $list;
+        } else {
+            return $list[0] ?? [];
+        }
+    }
+
+    /**
+     * 获取url对应的key
+     */
+    public function getKeyByUrls($urls)
+    {
+        $keys = [];
+        if (is_array($urls)) {
+            foreach ($urls as $url) {
+                $pathInfo = parse_url($url);
+                $keys[] = trim($pathInfo['path'], '/');
+            }
+            return $keys;
+        } else {
+            $pathInfo = parse_url($urls);
+            return trim($pathInfo['path'], '/');
+        }
+    }
 }