Skip to content

Commit

Permalink
[add] better refresh thumb api
Browse files Browse the repository at this point in the history
  • Loading branch information
VecHK committed Jan 7, 2024
1 parent b82d2d2 commit 74dcb76
Show file tree
Hide file tree
Showing 14 changed files with 349 additions and 135 deletions.
2 changes: 1 addition & 1 deletion dashboard/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ module.exports = {
'no-extend-native': 2,
'no-extra-bind': 2,
'no-extra-boolean-cast': 2,
'no-extra-parens': [2, 'functions'],
'no-extra-parens': [0],
'no-fallthrough': 2,
'no-floating-decimal': 2,
'no-func-assign': 2,
Expand Down
23 changes: 19 additions & 4 deletions dashboard/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"nprogress": "0.2.0",
"pad-left": "^2.1.0",
"path-to-regexp": "2.4.0",
"uuid": "^9.0.1",
"vue": "2.6.10",
"vue-router": "3.0.6",
"vuex": "3.1.0"
Expand Down
14 changes: 12 additions & 2 deletions dashboard/src/api/image.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import request from '@/utils/request'

export const refreshThumbs = () =>
export const __AVATAR_THUMB_SIZE__ = 128

export const refreshThumb = (src, thumb_size) =>
request({
url: `admin/image/refresh-thumb`,
method: 'POST',
timeout: 0,
data: { src, thumb_size },
})

export const getAllAvailablePhoto = () =>
request({
url: 'admin/image/refresh-thumb',
url: `admin/image/available-photo`,
method: 'GET',
timeout: 0,
})
120 changes: 120 additions & 0 deletions dashboard/src/views/dashboard/components/refresh-thumbs.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<template>
<div>
<ElButton
size="small"
type="primary"
icon="el-icon-refresh"
@click="processing || refreshThumbsConfirm()"
>刷新缩略图</ElButton>
<ElProgress
v-if="show_progress"
:percentage="percentage"
:color="color"
/>
</div>
</template>

<script>
import { refreshThumb, getAllAvailablePhoto, __AVATAR_THUMB_SIZE__ } from '@/api/image'
import { getList as getMemberList } from '@/api/member'
const initProgress = () => ({ total: 0, success: 0, failure: 0 })
export default {
data: () => ({
...initProgress(),
processing: false,
show_progress: false,
}),
computed: {
color() { return (this.failure > 0) ? '#e6a23c' : '#409eff' },
current() { return this.success + this.failure },
percentage() {
return (this.total === 0) ? 0 : Math.ceil(
100 * (this.current / this.total)
)
},
},
watch: {
processing(val) {
if (val === true) {
this.show_progress = true
}
}
},
methods: {
async refreshThumbs() {
Object.assign(this, initProgress())
const [photos, members] = await Promise.all([
getAllAvailablePhoto(), getMemberList()
])
const task_list = [
...photos.map(p => (
[`相片(id=${p.id})`, p.src]
)),
...members.map(m => (
[`成员头像(id=${m.id})`, m.avatar_src, __AVATAR_THUMB_SIZE__]
))
]
this.total = task_list.length
for (const [name, ...args] of task_list) {
try {
await refreshThumb(...args)
this.success = this.success + 1
} catch (err) {
this.failure = this.failure + 1
this.$emit('warning', `${name}的缩略图生成失败`, err)
}
}
},
async refreshThumbsConfirm() {
const confirm = await this.confirm('', `你确定要刷新吗?`, {
confirmButtonClass: 'el-button--warning',
})
if (!confirm) { return }
try {
this.processing = true
await this.refreshThumbs()
if (this.failure === 0) {
this.$emit('success', `所有缩略图已刷新`)
}
} catch (err) {
this.$emit('error', '刷新缓存失败', err)
} finally {
this.processing = false
}
},
async confirm(title, content, appendOpt = {}) {
const opt = Object.assign({
confirmButtonText: '确定',
cancelButtonText: '取消',
confirmButtonClass: 'el-button--warning',
showClose: false,
}, appendOpt)
return this.$confirm(content, title, opt)
.then(() => true)
.catch(() => false)
},
}
}
</script>

<style lang="scss" scoped>
.dashboard {
&-container {
margin: 30px;
}
&-text {
font-size: 30px;
line-height: 46px;
}
}
</style>
80 changes: 37 additions & 43 deletions dashboard/src/views/dashboard/index.vue
Original file line number Diff line number Diff line change
@@ -1,64 +1,58 @@
<template>
<ElContainer
v-loading="loading"
style="padding-top: 20px; width: 1180px"
style="padding-top: 20px; width: 640px"
direction="vertical"
>
<ElHeader height="2em">
torzo gallery dashboard beta
</ElHeader>

<ElMain>
<ElButton size="small" type="primary" icon="el-icon-refresh" @click="refreshThumbs">刷新缩略图</ElButton>
<ElAlert v-for="alert in alerts" :key="alert.id" :type="alert.type" :title="alert.title" style="margin-bottom: 10px;" />
<RefreshThumbs @error="handleError" @warning="handleWarning" @success="handleSuccess" @info="handleInfo" />
</ElMain>
</ElContainer>
</template>

<script>
import { mapGetters } from 'vuex'
import { refreshThumbs as refreshThumbsApi } from '@/api/image'
import RefreshThumbs from './components/refresh-thumbs.vue'
import { v4 as uuidv4 } from 'uuid'
function ConsolePrint(alert_type) {
switch (alert_type) {
default: return console.log
case 'error': return console.error
case 'warning': return console.warn
}
}
function HandleAlertEvent(alert_type) {
const consolePrint = ConsolePrint(alert_type).bind(console)
return function(title, err) {
if (err) {
consolePrint(`${title}`, err)
this.addAlert(alert_type, `${title}: ${err.message}`)
} else {
this.addAlert(alert_type, title)
}
}
}
export default {
name: 'Dashboard',
name: 'DashboardHome',
components: { RefreshThumbs },
data: () => ({
loading: false,
alerts: [],
}),
computed: {
...mapGetters([
'name'
])
},
methods: {
confirm(title, content, appendOpt = {}) {
const opt = Object.assign({
confirmButtonText: '确定',
cancelButtonText: '取消',
confirmButtonClass: 'el-button--warning',
showClose: false,
}, appendOpt)
return this.$confirm(content, title, opt)
.then(() => true)
.catch(() => false)
},
async refreshThumbs() {
const confirm = await this.confirm('', `你确定要刷新吗?`, {
confirmButtonClass: 'el-button--warning',
})
if (!confirm) {
return
}
try {
this.loading = true
await refreshThumbsApi()
this.$message.success(`缩略图已刷新`)
} catch (err) {
console.error('刷新失败', err)
this.$message.error(`刷新失败: ${err.message}`)
} finally {
this.loading = false
}
methods: {
addAlert(type, title) {
this.alerts = [...this.alerts, { id: uuidv4(), type, title }]
},
handleError: HandleAlertEvent('error'),
handleWarning: HandleAlertEvent('warning'),
handleSuccess: HandleAlertEvent('success'),
handleInfo: HandleAlertEvent('info'),
}
}
</script>
Expand Down
4 changes: 3 additions & 1 deletion dashboard/src/views/member/detail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
>
<UploadImageBox
ref="UploadImageBox"
:thumb-size="128"
:thumb-size="avatar_thumb_size"
:preview-url="preview_url"
@upload-success="uploadSuccess"
/>
Expand All @@ -44,6 +44,7 @@
</template>

<script>
import { __AVATAR_THUMB_SIZE__ } from '@/api/image'
import { getDetail, create, update } from '@/api/member'
import UploadImageBox from '@/components/UploadImageBox'
Expand All @@ -54,6 +55,7 @@
},
data: () => ({
avatar_thumb_size: __AVATAR_THUMB_SIZE__,
loading: false,
preview_url: '',
form: {
Expand Down
Loading

0 comments on commit 74dcb76

Please sign in to comment.