From 6dfd1505d498220fba34da30ffec33b8668cfbb4 Mon Sep 17 00:00:00 2001 From: xiaoz Date: Mon, 9 May 2022 17:42:23 +0800 Subject: [PATCH] 20220509 --- class/Api.php | 39 +++++++++++++++++++++++++++++++++ controller/api.php | 28 +++++++++++++++++++++++ templates/admin/header.php | 2 +- templates/admin/link_list.php | 5 ++++- templates/admin/static/embed.js | 25 +++++++++++++++++++++ 5 files changed, 97 insertions(+), 2 deletions(-) diff --git a/class/Api.php b/class/Api.php index 0c620e9..8d58b60 100755 --- a/class/Api.php +++ b/class/Api.php @@ -380,6 +380,8 @@ class Api { } } $categoryt = array_unique($categoryt); + //追加一个默认分类,用来存储部分链接找不到分类的情况 + array_push($categoryt,"默认分类"); //批量创建分类 $this->batch_create_category($categoryt); @@ -398,6 +400,8 @@ class Api { //批量导入链接 foreach ($data as $key => $value) { $category_name = trim($value['category']); + //如果链接的分类是空的,则设置为默认分类 + $value['category'] = empty( $value['category'] ) ? "默认分类" : $value['category']; foreach ($categorys as $category) { if( trim( $category['name'] ) == $category_name ) { @@ -405,6 +409,7 @@ class Api { break; } } + //合并数据 $link_data = [ 'fid' => $fid, @@ -494,6 +499,40 @@ class Api { } } } + /** + * 导出HTML链接进行备份 + */ + public function export_link(){ + //鉴权 + $this->auth($token); + //查询所有分类 + $categorys = $this->db->select("on_categorys","*"); + + //定义一个空数组用来存储查询后的数据 + $data = []; + + //遍历分类 + foreach ($categorys as $key => $category) { + //查询该分类下的所有链接 + $links = $this->db->select("on_links","*",[ + "fid" => $category['id'] + ]); + // echo $category['name']; + // var_dump($links); + // exit; + //组合为一个一维数组 + + $arr[$category['name']] = $links; + // var_dump(); + // exit; + $data[$category['name']] = $arr[$category['name']]; + + //清除临时数据 + unset($arr); + } + //返回数据 + return $data; + } /** * name:修改链接 */ diff --git a/controller/api.php b/controller/api.php index ebbaacb..c9a3ef4 100755 --- a/controller/api.php +++ b/controller/api.php @@ -375,4 +375,32 @@ function set_link_attribute($api) { "property" => $property ]; $api->set_link_attribute($data); +} + +//导出链接数据 +function export_link($api) { + header('Content-Type: text/html;charset=utf8'); + $data = $api->export_link(); + //当前时间 + $current = time(); + echo <<< EOF + +从OneNav导出的书签 +

Bookmarks

+EOF; + //遍历结果 + foreach ($data as $key => $value) { + + echo "
\n"; + echo "

$key

\n"; + echo "

\n"; + foreach ($value as $link) { + $title = $link['title']; + $add_time = $link['add_time']; + echo "
$title
\n"; + } + echo "

\n"; + echo "
\n"; + + } } \ No newline at end of file diff --git a/templates/admin/header.php b/templates/admin/header.php index b70e2e7..75d70b6 100755 --- a/templates/admin/header.php +++ b/templates/admin/header.php @@ -10,7 +10,7 @@
- +
  • 前台首页
  • diff --git a/templates/admin/link_list.php b/templates/admin/link_list.php index 9feb99e..a351e57 100755 --- a/templates/admin/link_list.php +++ b/templates/admin/link_list.php @@ -21,11 +21,11 @@
    -
+
@@ -46,6 +46,9 @@ 编辑 删除 + + +
diff --git a/templates/admin/static/embed.js b/templates/admin/static/embed.js index c90440e..ad077fd 100755 --- a/templates/admin/static/embed.js +++ b/templates/admin/static/embed.js @@ -783,4 +783,29 @@ function set_link_attribute(ids,property) { } }); } +} + +//导出所有链接 +function export_link(url, fileName) { + layer.confirm('导出的链接可以导入到浏览器也可以再次导入到OneNav!', {icon: 3, title:'确定导出所有链接?'}, function(index){ + var date = new Date(); + var current_time = date.toLocaleDateString(); + current_time = current_time.replaceAll("/","-"); + var url = "index.php?c=api&method=export_link"; + var fileName = "OneNav_Export_" + current_time + ".html"; + var x = new XMLHttpRequest(); + x.open("GET", url, true); + x.responseType = 'blob'; + x.onload=function(e) { + var url = window.URL.createObjectURL(x.response) + var a = document.createElement('a'); + a.href = url + a.download = fileName; + a.click() + } + x.send(); + + layer.close(index); + }); + } \ No newline at end of file