From 155688790fa91b08349c447ca301e644b5c7f7c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BB=BB=E5=AE=9D=E7=A1=95?= Date: Mon, 9 Mar 2020 23:58:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E4=BA=86=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/index.php | 1 + api/set.php | 30 ++++++++++ asset/css/main.css | 103 ++++++++++++++++++++++++++++++++ asset/js/app.js | 60 +++++++++++++++++++ inc/class/database.db | Bin 0 -> 3072 bytes inc/class/db.class.php | 32 ++++++++++ inc/class/url.class.php | 60 +++++++++++++++++++ nginx.txt => rewrite.nginx.conf | 0 8 files changed, 286 insertions(+) create mode 100644 api/index.php create mode 100644 api/set.php create mode 100644 asset/css/main.css create mode 100644 asset/js/app.js create mode 100644 inc/class/database.db create mode 100644 inc/class/db.class.php create mode 100644 inc/class/url.class.php rename nginx.txt => rewrite.nginx.conf (100%) diff --git a/api/index.php b/api/index.php new file mode 100644 index 0000000..6c9ed5c --- /dev/null +++ b/api/index.php @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/api/set.php b/api/set.php new file mode 100644 index 0000000..9be7464 --- /dev/null +++ b/api/set.php @@ -0,0 +1,30 @@ +set_url($request_arr['url'], $config['length']); + } else if(strstr($request_arr['url'], $_SERVER['HTTP_HOST'])) { + $opt['content'] = '链接已经是短地址了。'; + } else if(!$is_link) { + $opt['content'] = '请输入正确格式的网址。'; + } + } else { + $opt['content'] = '调用参数不能为空。'; + } + // 输出 + echo json_encode($opt); +?> \ No newline at end of file diff --git a/asset/css/main.css b/asset/css/main.css new file mode 100644 index 0000000..7ef9ca5 --- /dev/null +++ b/asset/css/main.css @@ -0,0 +1,103 @@ +/* Reset */ +* { + margin: 0; + padding: 0; +} +html, body, input, text, textarea { + outline: none; + font-family: 'Arial', 'Microsoft YaHei', '黑体', '宋体', sans-serif; + font-size: 12px; +} +html, body { + background: #fff; +} +a { + text-decoration: none; +} +a:hover { + text-decoration: underline; +} + +/* Main */ +.wrap { + text-align: center; + overflow: hidden; +} +.wrap .meta { + margin: 160px 0 0 0; + opacity: 0; + transform: translateY(-150px); + transition: .5s all ease; +} +.on .wrap .meta { + opacity: 1; + transform: translateY(0); +} +.wrap .meta .title { + line-height: 1em; + color: #ff4665; + font-size: 42px; + text-transform: uppercase; +} +.wrap .meta .description { + margin: 10px 0 0 0; + line-height: 1em; + color: #7e7e7e; + font-size: 16px; + font-weight: normal; +} +.wrap .link-area { + margin: 50px 0 0 0; + opacity: 0; + transition: .5s opacity ease; +} +.on .wrap .link-area { + opacity: 1; +} +.wrap .link-area input { + display: inline-block; + vertical-align: middle; +} +.wrap .link-area #url, +.wrap .link-area #shorturl { + width: 320px; + height: 32px; + line-height: 32px; + padding: 0 10px; + border: 3px solid #bdc3c7; + border-radius: 5px; + color: #333; +} +.wrap .link-area #url.focus, +.wrap .link-area #url:focus { + border-color: #ff4665; + transition: .2s border ease; +} +.wrap .link-area #shorturlcopy, +.wrap .link-area #submit { + width: 90px; + height: 38px; + margin: 0 0 0 5px; + background: #ff4665; + border-radius: 5px; + color: #fff; + border: none; + cursor: pointer; + transition: .2s opacity ease; +} +.wrap .link-area #submit:hover { + opacity: .75; +} +.wrap .link-area #submit:active { + opacity: .9; +} +.wrap .footer { + width: 100%; + bottom: 80px; + left: 0; + position: absolute; + color: #7e7e7e; +} +.wrap .footer a { + color:#ff4665; +} \ No newline at end of file diff --git a/asset/js/app.js b/asset/js/app.js new file mode 100644 index 0000000..959a636 --- /dev/null +++ b/asset/js/app.js @@ -0,0 +1,60 @@ + +function copyText() { + var input = document.getElementById("shorturl"); + input.select(); // 选中文本 + document.execCommand("copy"); // 执行浏览器复制命令 +} + + +var APP = (function(){ + + var fn = { + + // 生成短地址 + setUrl: function(self) { + var urlEl = document.getElementById('url'), + tips = 'https://', + request = {"url": urlEl.value}; + fn.getJson('api/set.php', true, JSON.stringify(request), function(res) { + if(res.success == 'true') { + //urlEl.className = 'focus'; + //urlEl.value = res.content.url; + $res = document.getElementById('shorturl') + $res.className = 'focus'; + $res.value = res.content.url; + } else { + urlEl.className = ''; + urlEl.value = ''; + urlEl.setAttribute('placeholder', res.content); + setTimeout(function() { + urlEl.setAttribute('placeholder', tips); + }, 2000); + } + }); + }, + // 获取 JSON 数据 + getJson: function(url, post, data, callback) { + var xhr = new XMLHttpRequest(), + type = (post) ? 'POST' : 'GET'; + xhr.onreadystatechange = function() { + if(xhr.readyState == 4 && xhr.status == 200) { + var json = JSON.parse(xhr.responseText); + callback(json); + } else if(xhr.readyState == 4) { + callback(false); + } + } + xhr.open(type, url, true); + xhr.send(data); + } + }, + init = function() { + setTimeout(function() { + var el = document.getElementsByTagName('html')[0]; + el.className = 'on'; + }, 10); + }; + return {fn: fn, init: init} +})(); + +document.addEventListener('DOMContentLoaded', function() {APP.init();}) \ No newline at end of file diff --git a/inc/class/database.db b/inc/class/database.db new file mode 100644 index 0000000000000000000000000000000000000000..0e1275dacaa319ce51b1a238458ce20cc6092509 GIT binary patch literal 3072 zcmeH|&2HL25XaZvhhZov{=+xP=gE+r9aE1+r#Qj`-~$R=#E#$Ycs>W#il z-=J^Nx5%~1t;g=t(2rAkYZUn}jdy0pqaFR^S)aV`a>?K#Nnl#38n zwvnM6y||pgn|a)C zw+3w(w2r!M&^wj#9^8qa!+1i|O2dYC{WrZ<{|q|qvns&vS5yUvWO^ytQvQ|TGx$K$ zb-5SW)juleuS6{j?M=cyFiZPQ%Rqd3&X(C+sM#LUg-q7*PcX zqVFe0$@15{568OiOaYfUmy)HL^?$~?r{^EvAp8yAsNZydb = new PDO('sqlite:' . dirname(__FILE__) . '/database.db'); + $this->init_tab(); + } + // 初始化数据库结构 + function init_tab() { + // 网址表 + $this->db->exec("CREATE TABLE urls(id char(8) PRIMARY KEY, url longtext, ip varchar(16), ua varchar(255))"); + } + // 查询表内容 + function query($name, $rule = '') { + $query = $this->db->prepare("SELECT * FROM $name $rule"); + $query->execute(); + $result = $query->fetchAll(); + return $result; + } + // 插入表内容 + function insert($tab, $key, $val) { + $exec = $this->db->exec("INSERT INTO $tab ($key) VALUES($val)"); + if(!$exec) return false; + $this->db->beginTransaction(); + } + // 删除表内容 + function delete($tab, $rule = '') { + $exec = $this->db->exec("DELETE FROM $tab $rule"); + if(!$exec) return false; + $this->db->beginTransaction(); + } + } +?> \ No newline at end of file diff --git a/inc/class/url.class.php b/inc/class/url.class.php new file mode 100644 index 0000000..1af91ba --- /dev/null +++ b/inc/class/url.class.php @@ -0,0 +1,60 @@ +db = $db_c; + } + // 生成短地址 + public function set_url($url, $size = 4) { + $id = $this->get_id($url); + if(!$id) { + $id = $this->create_id($url, $size); + $ip = get_ip(); + $ua = get_ua(); + $this->db->insert('urls', 'id, url, ip, ua', "'$id', '$url', '$ip', '$ua'"); + } + $s_url = get_uri() . $id; + return $s_url; + } + // 生成地址 ID + public function create_id($url, $size = 4) { + $md5 = md5($url); + // 随机抽取 MD5 中的字符作为 ID + $id = ''; + for($i = 0; $i < $size; $i++) { + $rand_id = rand(0, strlen($md5) - 1); + $id .= $md5[$rand_id]; + } + // ID 检测 + if($this->has_id($id)) { + return $this->create_id($url, $size); + } else { + return $id; + } + } + // 查询 ID 号 + public function get_id($url) { + $result = $this->db->query('urls', "WHERE url = '$url'"); + (count($result) > 0) ? $opt = $result[0]['id'] : $opt = false; + return $opt; + } + // 查询目标地址 + public function get_url($id) { + $result = $this->db->query('urls', "WHERE id = '$id'"); + (count($result) > 0) ? $opt = $result[0]['url'] : $opt = false; + return $opt; + } + // 检测 ID 是否已经存在 + public function has_id($id) { + $result = $this->db->query('urls', "WHERE id = '$id'"); + (count($result) > 0) ? $opt = true : $opt = false; + return $opt; + } + // 清空短地址 + public function clean_urls() { + $del = $this->db->delete('urls'); + if($del) return true; + return false; + } + } +?> \ No newline at end of file diff --git a/nginx.txt b/rewrite.nginx.conf similarity index 100% rename from nginx.txt rename to rewrite.nginx.conf