ADD сокращалка ссылок
This commit is contained in:
parent
d2e7e4d20e
commit
61cabf756e
7
app.php
7
app.php
|
|
@ -57,6 +57,13 @@ class App
|
|||
} else if (isset (self::$url[2])) {
|
||||
$controller = self::$controller;
|
||||
$action = self::$action;
|
||||
|
||||
// Костылёк для сокрощалки ссылок
|
||||
if ($controller == 'SController') {
|
||||
self::$urlParams['key'] = self::$url[2];
|
||||
self::$action = $action = 'actionIndex';
|
||||
}
|
||||
|
||||
if (!method_exists($controller, $action)) {
|
||||
self::$urlParams['id'] = (int)self::$url[2];
|
||||
self::$action = 'actionIndex';
|
||||
|
|
|
|||
|
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Класс для сокращения ссылок - переводит INT идентификатор в 62-ричную систему исчисления.
|
||||
*/
|
||||
class x62
|
||||
{
|
||||
|
||||
private static $ABC = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
|
||||
public static function encode($in)
|
||||
{
|
||||
$len = strlen(self::$ABC);
|
||||
|
||||
$in = intval($in);
|
||||
$out = '';
|
||||
|
||||
while ($in >= 0) {
|
||||
if ($in >= $len) {
|
||||
$tmp = floor($in / $len);
|
||||
$out .= self::$ABC[(int) ($in - $tmp * $len)];
|
||||
$in = $tmp;
|
||||
} else {
|
||||
$out .= self::$ABC[(int) $in];
|
||||
$in = -1;
|
||||
}
|
||||
}
|
||||
|
||||
return strrev($out);
|
||||
}
|
||||
|
||||
public static function decode($in)
|
||||
{
|
||||
$out = 0;
|
||||
$exp = 0;
|
||||
for ($i = strlen($in) - 1; $i >= 0; $i--) {
|
||||
$n = strpos(self::$ABC, $in[$i]);
|
||||
if ($n === false) {
|
||||
return false; //Хрень какую-то прислали
|
||||
}
|
||||
$out += $n * pow(62, $exp);
|
||||
$exp++;
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -192,4 +192,32 @@ class IndexController extends Controller
|
|||
));
|
||||
}
|
||||
|
||||
function actionShorturlroute()
|
||||
{
|
||||
if (!isset($_GET['elements']) || empty($_GET['elements'])) {
|
||||
App::error404();
|
||||
}
|
||||
|
||||
$elements = (array)$_GET['elements'];
|
||||
|
||||
$url = '/?route=[';
|
||||
foreach ($elements AS $key => $element) {
|
||||
if ($key != 0) {
|
||||
$url .= ',';
|
||||
}
|
||||
|
||||
if (isset($element['id'])) {
|
||||
$url .= '"'.$element['id'].'"';
|
||||
}
|
||||
|
||||
if (isset($element['latLng'])) {
|
||||
$url .= '"['.$element['latLng']['lat'].','.$element['latLng']['lng'].']"';
|
||||
}
|
||||
}
|
||||
$url .= ']';
|
||||
|
||||
$shortUrl = ShortUrl::addNew('https://wikipoints.ru'.$url);
|
||||
|
||||
print 'https://wikipoints.ru/s/'.x62::encode($shortUrl->id);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
class SController extends Controller
|
||||
{
|
||||
static function actionIndex()
|
||||
{
|
||||
$key = intval(x62::decode(App::getParam('key')));
|
||||
$shortUrl = ShortUrl::model()->getByPK($key);
|
||||
|
||||
if($shortUrl->id) {
|
||||
App::redirect($shortUrl->url);
|
||||
} else {
|
||||
App::error404();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
|
||||
class ShortUrl extends Model
|
||||
{
|
||||
|
||||
function __construct($fromArray = array())
|
||||
{
|
||||
$this->_tableName_ = 'shortUrl';
|
||||
parent::__construct($fromArray);
|
||||
}
|
||||
|
||||
static function model()
|
||||
{
|
||||
return new self();
|
||||
}
|
||||
|
||||
static function addNew($url)
|
||||
{
|
||||
$tmp = new ShortUrl(array('url' => $url));
|
||||
|
||||
|
||||
return $tmp->save();
|
||||
}
|
||||
}
|
||||
|
|
@ -523,3 +523,30 @@ window.addEventListener('load', function(){
|
|||
}, false);
|
||||
|
||||
}, false);
|
||||
|
||||
function getShortLink()
|
||||
{
|
||||
elements = Array();
|
||||
$('#wayPoints div').each(function(i, el)
|
||||
{
|
||||
if ($(el).attr('pointId').length <= 10) {
|
||||
elements.push({id: $(el).attr('pointId')});
|
||||
} else {
|
||||
coords = JSON.parse($(el).attr('pointId'));
|
||||
lat = coords[0];
|
||||
lng = coords[1];
|
||||
elements.push({latLng: {lat: lat, lng: lng}});
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
$.ajax({
|
||||
url: "/index/shorturlroute/",
|
||||
data: {
|
||||
elements: elements
|
||||
},
|
||||
success: function (data) {
|
||||
$('#shortLink').val(data).select();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -69,6 +69,10 @@ if (App::$user) {
|
|||
<button class="btn btn-default" onclick="saveRoute()"><span class="glyphicon glyphicon-cloud-upload" title="Сохранить"></span></button>
|
||||
<?php endif; ?>
|
||||
<br/>
|
||||
<hr/>
|
||||
Получить короткую ссылку:<br/>
|
||||
<button class="btn btn-default" onclick="getShortLink()"><span class="glyphicon glyphicon-link"></span></button>
|
||||
<input type="text" id="shortLink"/>
|
||||
</div>
|
||||
<?php if ($user): ?>
|
||||
<div id="boxFavorites">
|
||||
|
|
|
|||
Loading…
Reference in New Issue