IndexController.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace App\Api\Controllers;
  3. use App\Api\Services\ApiService;
  4. use App\Base\Controllers\ApiBaseController;
  5. use App\Base\Library\Shorty;
  6. use App\Basic\Facades\UrlsFacade;
  7. use Illuminate\Http\Request;
  8. class IndexController extends ApiBaseController
  9. {
  10. private $apiService;
  11. private $shorty;
  12. public function __construct(ApiService $apiService)
  13. {
  14. $this->apiService = $apiService;
  15. $this->shorty = new Shorty(config("app.domain"));
  16. }
  17. /**
  18. * 短地址自动跳转
  19. */
  20. public function index(Request $request)
  21. {
  22. $plink = $request->route("plink", ""); //详情页面分配的地址
  23. $id = $this->shorty->decode($plink);
  24. $info = UrlsFacade::findOneById($id, "url");
  25. if (empty($info['url'])) {
  26. abort(404);
  27. }
  28. UrlsFacade::incrementBy(["id" => $id], "hits");
  29. $ip = getClientIp();
  30. $uvInfo = UrlsFacade::findOneLogInfo([
  31. 'urls_id' => $id,
  32. 'ip' => $ip
  33. ], 'id');
  34. if (empty($uvInfo['id'])) {
  35. UrlsFacade::incrementBy(["id" => $id], "uv");
  36. }
  37. UrlsFacade::addLog($id, $ip);
  38. $this->shorty->redirect($info['url']);
  39. }
  40. }