12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- <?php
- namespace App\Api\Services;
- use App\Base\Library\Shorty;
- use App\Base\Services\ApiBaseService;
- 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)
- {
- $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) {
- continue;
- }
- }
- if (count($urls) == 1) {
- return $list[$urls[0]];
- } else {
- return $list;
- }
- }
- }
|