123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- <?php
- 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
- {
- private $shorty;// 短地址服务
- public function __construct()
- {
- $this->shorty = new Shorty(config("app.domain"));
- }
- /**
- * 获取短地址
- */
- public function getShortUrl($urls)
- {
- $isArray = 0;
- if (is_array($urls)) {
- $isArray = 1;
- }
- $urls = is_array($urls) ? $urls : [$urls];
- $list = [];
- foreach ($urls as $url) {
- $key = "shortUrl_" . md5($url);
- $value = Cache::get($key, '');
- if ($value) {
- $list[$url] = $value;
- continue;
- }
- try {
- $list[$url] = $this->shorty->run($url);
- Cache::put($key, $list[$url], 86400 * 7);
- } catch (\Exception $e) {
- \Log::info($e->getMessage());
- //continue;
- }
- }
- if ($isArray == 0) {
- return $list[$urls[0]] ?? '';
- } else {
- return $list;
- }
- }
- /**
- * 获取短地址访问量
- */
- public function getShortUrlHits($urls)
- {
- return UrlsFacade::getShortUrlHits($urls);
- }
- }
|