Skip to content

Commit

Permalink
## 0.10.0 -> 批量更新图标
Browse files Browse the repository at this point in the history
## feat
- 批量获取导入的书签的图标和站点介绍信息
- 分类显示该分类下书签数
- 搜索结果书签与分类下统一使用一个组件

## fix
- 搜索结果未时间逆序
- 打开搜索弹窗会保留上次结果内容的问题

fixed #2
fixed #8
  • Loading branch information
glennliao committed Jul 12, 2023
1 parent 0cb1466 commit 2c59940
Show file tree
Hide file tree
Showing 12 changed files with 202 additions and 21 deletions.
86 changes: 86 additions & 0 deletions app/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package app
import (
"context"
"github.com/gogf/gf/v2/container/gset"
"github.com/panjf2000/ants/v2"
url2 "net/url"
"strconv"
"strings"
Expand All @@ -29,6 +30,8 @@ func initFunc(a *apijson.ApiJson) {
a.Config().Functions.Bind("cateIdByBmId", cateIdByBmId())
a.Config().Functions.Bind("latestVersion", latestVersion())
a.Config().Functions.Bind("noteTags", noteTags())
a.Config().Functions.Bind("cateBmNum", cateBmNum())
a.Config().Functions.Bind("fetchMetaBatch", fetchMetaBatchFunc())
}

var cateIdByBmId = func() config.Func {
Expand Down Expand Up @@ -276,3 +279,86 @@ func noteTags() config.Func {
},
}
}

func cateBmNum() config.Func {
return config.Func{
ParamList: nil,
Batch: false,
Handler: func(ctx context.Context, param model.Map) (res any, err error) {
groupId := gconv.String(param["groupId"])
if groupId == "" {
groupId = ctx.Value(UserIdKey).(*CurrentUser).UserId
}
values, err := g.DB().Model("group_bookmark").Where("group_id", groupId).WhereNull("drop_at").Group("cate_id").Fields("cate_id cateId, count(1) cnt").All()

return values.List(), err
},
}
}

func fetchMetaBatchFunc() config.Func {
return config.Func{
Handler: func(ctx context.Context, param model.Map) (res any, err error) {
all, err := g.DB().Ctx(ctx).Model("bookmark").Where("description", "@import ").All()
if err != nil {
return nil, err
}

task := func(data interface{}) {
item := data.(g.Map)
url := gconv.String(item["url"])
bmId := gconv.String(item["bm_id"])
var meta *fetchurl.UrlMeta
if !strings.HasPrefix(strings.ToLower(url), "http") {
meta, err = fetchurl.FetchURLMeta(ctx, "https://"+url)
if err != nil {
meta, err = fetchurl.FetchURLMeta(ctx, "http://"+url)
if err != nil {
g.Log().Error(ctx, err, url, bmId)
return
}
}
} else {
meta, err = fetchurl.FetchURLMeta(ctx, url)
if err != nil {
g.Log().Error(ctx, err, url, bmId)
return
}
}

if meta != nil {
if strings.HasPrefix(meta.Icon, "/") {
_url, err := url2.Parse(meta.Url)
if err != nil {
g.Log().Error(ctx, err, url, bmId)
return
}
meta.Icon = _url.Scheme + "://" + _url.Host + meta.Icon
}

meta.Url = url
}

_, err := g.DB().Model("bookmark").Ctx(ctx).Where("bm_id", bmId).Update(g.Map{
"icon": meta.Icon,
"description": meta.Description,
})
if err != nil {
g.Log().Error(ctx, err)
}

}

p, _ := ants.NewPoolWithFunc(8, task)
defer p.Release()

for _, item := range all {
err := p.Invoke(item.Map())
if err != nil {
return nil, err
}
}
return nil, err
},
}
}
5 changes: 5 additions & 0 deletions app/util/fetchurl/fetchurl.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ func FetchURLMeta(ctx context.Context, uri string) (meta *UrlMeta, err error) {
resp, err := client.Get(uri)

if err != nil {

if strings.Contains(err.Error(), "context deadline") {
return &UrlMeta{}, err
}

if e, ok := err.(*url.Error); ok {
if !e.Timeout() {
return &UrlMeta{}, err
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ require (
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/panjf2000/ants/v2 v2.8.1 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect
go.opentelemetry.io/otel v1.7.0 // indirect
go.opentelemetry.io/otel/sdk v1.7.0 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,24 @@ github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vv
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/onsi/gomega v1.16.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
github.com/panjf2000/ants/v2 v2.8.1 h1:C+n/f++aiW8kHCExKlpX6X+okmxKXP7DWLutxuAPuwQ=
github.com/panjf2000/ants/v2 v2.8.1/go.mod h1:KIBmYG9QQX5U2qzFP/yQJaq/nSb6rahS9iEHkrCMgM8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 h1:OdAsTTz6OkFY5QxjkYwrChwuRruF69c169dPK26NUlk=
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/samber/lo v1.33.0 h1:2aKucr+rQV6gHpY3bpeZu69uYoQOzVhGT3J22Op6Cjk=
github.com/samber/lo v1.33.0/go.mod h1:HLeWcJRRyLKp3+/XBJvOrerCQn9mhdKMHyd7IRlgeQ8=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/thoas/go-funk v0.9.1 h1:O549iLZqPpTUQ10ykd26sZhzD+rmR5pWhuElrhbC20M=
github.com/yitter/idgenerator-go v1.3.2 h1:MHk1NEplm4Ca1sJI8WrFMuwG110UO6KftmClBMMV0ig=
github.com/yitter/idgenerator-go v1.3.2/go.mod h1:VVjbqFjGUsIkaXVkXEdmx1LiXUL3K1NvyxWPJBPbBpE=
Expand Down Expand Up @@ -151,6 +158,7 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
2 changes: 1 addition & 1 deletion ui/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
Upload,
Breadcrumb,
Badge,
Tag,Avatar
Tag , Avatar
} from 'ant-design-vue'
import 'ant-design-vue/dist/reset.css'

Expand Down
16 changes: 14 additions & 2 deletions ui/src/utils/tree.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@

export function toCateTree (list: any[]): any[] {
// 首先按级别进行排序(不需要请删除)
list.sort(function (a, b) {
return a.sorts - b.sorts
})

const map:Record<string, any[]> = {}
const map: Record<string, any[]> = {}

list.forEach(item => {
map[item.parentId] = map[item.parentId] || []
Expand All @@ -20,3 +19,16 @@ export function toCateTree (list: any[]): any[] {

return map.root
}

export function treeEach (tree: any[], call: (any) => any): void {
tree = tree || []
tree.forEach(item => {

if (item.children) {
treeEach(item.children, call)
}

console.log("item",item)
call(item)
})
}
1 change: 1 addition & 0 deletions ui/src/views/bookmark/components/Bookmark.vue
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ function drop () {
emit("drop")
bookmark.loadBookmarkList()
bookmark.loadSubCateBookmark()
bookmark.loadCateBmNum()
})
},
onCancel () {
Expand Down
18 changes: 18 additions & 0 deletions ui/src/views/bookmark/components/Import.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@
band files
</p>
</a-upload-dragger>

<a-alert class="mt-2">
<template #message>
上传完毕后将自动在后台同步书签的信息,可刷新页面后查看
</template>
</a-alert>

<div class="mt-2">
<a-button @click="fetchMeta">手动触发书签图标更新</a-button>
<div style="margin-top: 4px;font-size: 13px;color:#818181">
尝试更新 @import 标记的书签如果网络获取失败则暂保留该标记
</div>
</div>
</template>
<script lang="ts" setup>
import { InboxOutlined } from '@ant-design/icons-vue'
Expand All @@ -39,12 +52,17 @@ const handleChange = (info: UploadChangeParam) => {
console.log('import data', data)
bookmark.loadCate()
bookmark.loadBookmarkList()
bookmark.fetchMetaBatch()
message.success('导入成功')
})
})

reader.readAsText(info.file)
}

function fetchMeta(){
bookmark.fetchMetaBatch()
}
const fileList = ref([])

</script>
11 changes: 8 additions & 3 deletions ui/src/views/bookmark/components/SubMemu.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
<template>
<a-sub-menu :key="menuInfo.cateId" @titleClick="click([])">
<template #title ><span >{{ menuInfo.title }}</span></template>
<template #title >
<span>{{ menuInfo.title }} <span v-if="menuInfo.count > 0" style="font-size: 12px;margin-left: 3px;color:#8b8b8b">({{menuInfo.count}})</span></span>

<!-- <span >{{ menuInfo.title }}</span>-->
</template>
<template v-for="item in menuInfo.children" :key="item.cateId">
<template v-if="!item.children">
<a-menu-item :key="item.cateId">
<div @click="click([item.cateId])">
{{ item.title }}
<div class="flex items-end" @click="click([item.cateId])">
<span>{{ item.title }}</span>
<span v-if="item.count > 0" style="font-size: 12px;margin-left: 3px;color:#8b8b8b">({{item.count}})</span>
</div>
</a-menu-item>
</template>
Expand Down
61 changes: 50 additions & 11 deletions ui/src/views/bookmark/hook/bookmark.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,50 @@
import { ref } from 'vue'
import { computed, ref } from 'vue'
import { apiJson } from '@/api'
import { toCateTree } from '@/utils/tree'
import { toCateTree, treeEach } from '@/utils/tree'

const cateTree = ref({ children: [] } as { children: any[] })
const cateList = ref([])
const curCate = ref([] as string[])
const curGroupId = ref('')
const cateBookmarkNumMap = ref({})
const cateTree = computed(() => {
let treeList = toCateTree(cateList.value)


treeEach(treeList, (item) => {
item.count = cateBookmarkNumMap.value[item.cateId] || 0
if(item.children){
item.children.forEach(subItem=>{
item.count += subItem.count
})
}
})
return {
children: treeList
}
})

function loadCate () {
apiJson.get({
'BookmarkCate[]': {
count: 0
}
},
'cateBmNum()': 'cateBmNum()'
}).then((data) => {
cateList.value = data['BookmarkCate[]']
cateTree.value = {
children: toCateTree(data['BookmarkCate[]'])
}
loadCateBmNum()
})
}

function loadCateBmNum () {
apiJson.get({
'cateBmNum()': 'cateBmNum()'
}).then((data) => {
let list = data.cateBmNum
let m = {}
list.forEach(item => {
m[item.cateId] = item.cnt
})
cateBookmarkNumMap.value = m
})
}

Expand All @@ -25,7 +53,7 @@ function clickCate (cateIds: string[]) {
loadBookmarkList()
}

const groups = ref([] as {groupId:string}[])
const groups = ref([] as { groupId: string }[])

function loadGroup () {
return apiJson.get({
Expand All @@ -41,6 +69,7 @@ function loadGroup () {
}

const bookmarkList = ref([] as any[])

function loadBookmarkList () {
apiJson.get({
'Bookmark[]': {
Expand All @@ -56,11 +85,11 @@ function loadBookmarkList () {

const curSubCateBookmark = ref([])
const curSubCateId = ref('') // 下方分类
function loadSubCateBookmark(){
function loadSubCateBookmark () {
apiJson.get({
'Bookmark[]': {
count: 0,
cateId:curSubCateId.value,
cateId: curSubCateId.value,
'@order': 'createdAt desc',
'cateId()': 'cateIdByBmId(bmId,groupId)'
}
Expand All @@ -69,8 +98,17 @@ function loadSubCateBookmark(){
})
}

function fetchMetaBatch () {
apiJson.get({
'fetchMetaBatch()': 'fetchMetaBatch()'
}).then((data) => {

})
}

export function useBookmark () {
return {
fetchMetaBatch,
loadCate,
clickCate,
curCate,
Expand All @@ -83,6 +121,7 @@ export function useBookmark () {
groups,
curSubCateId,
curSubCateBookmark,
loadSubCateBookmark
loadSubCateBookmark,
loadCateBmNum
}
}
Loading

0 comments on commit 2c59940

Please sign in to comment.