Skip to content

Commit

Permalink
feat: 在线分享第一版-新增权限控制
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Jun 20, 2023
1 parent f47847b commit 23b61a1
Show file tree
Hide file tree
Showing 13 changed files with 417 additions and 144 deletions.
13 changes: 11 additions & 2 deletions components/default/Detail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@

<script setup lang="ts">
import { usePost } from "~/composables/usePost"
import { getFirstImageSrc, getSummery } from "~/utils/utils"
import { checkExpires, getFirstImageSrc, getSummery } from "~/utils/utils"
import { createAppLogger } from "~/common/appLogger"
import { JsonUtil, StrUtil } from "zhi-common"
import { PostStatusEnum } from "zhi-blog-api"
const logger = createAppLogger("share-page")
Expand All @@ -45,6 +47,10 @@ const { currentPost, setCurrentPost } = usePost()
await setCurrentPost(props.pageId)
// datas
const attrs = JsonUtil.safeParse<any>(currentPost.post?.attrs ?? "{}", {})
const shareEnabled = attrs["custom-publish-status"] === PostStatusEnum.PostStatusEnum_Publish
const isExpires = checkExpires(attrs)
logger.info("current document status isExpires=>", isExpires)
if (!props.overrideSeo) {
const titleSign = " - " + t("blog.share")
const title = `${currentPost.post.title}${props.showTitleSign ? titleSign : ""}`
Expand Down Expand Up @@ -72,7 +78,10 @@ const VNode = () =>
</script>

<template>
<div class="fn__flex-1 protyle" data-loading="finished">
<div v-if="!shareEnabled || isExpires">
<el-empty :description="isExpires ? t('blog.index.no.expires') : t('blog.index.no.permission')"> </el-empty>
</div>
<div v-else class="fn__flex-1 protyle" data-loading="finished">
<div class="protyle-content protyle-content--transition" data-fullwidth="true">
<div class="protyle-title protyle-wysiwyg--attr">
<div
Expand Down
4 changes: 2 additions & 2 deletions composables/api/usePostApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const usePostApi = () => {
const logger = createAppLogger("use-post")
const env = useRuntimeConfig()

const getPost = async (id: string) => {
const getPost = async (id: string, useSlug?: boolean, skipBody?: boolean) => {
logger.info("Loading post from remote api...")

// logger.info("env=>", env)
Expand All @@ -44,7 +44,7 @@ export const usePostApi = () => {
const siyuanConfig = new SiyuanConfig(env.public.siyuanApiUrl, env.siyuanAuthToken)
const blogApi = new SiYuanApiAdaptor(siyuanConfig)
const postid = id.replace(/\.html$/, "")
return await blogApi.getPost(postid)
return await blogApi.getPost(postid, useSlug, skipBody)
}

return { getPost }
Expand Down
42 changes: 42 additions & 0 deletions composables/api/useSiyuanApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2023, Terwer . All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Terwer designates this
* particular file as subject to the "Classpath" exception as provided
* by Terwer in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com
* or visit www.terwer.space if you need additional information or have any
* questions.
*/

import { SiYuanApiAdaptor, SiyuanConfig, SiyuanKernelApi } from "zhi-siyuan-api"

/**
* 通用 Siyuan API 封装
*/
export const useSiyuanApi = () => {
const env = useRuntimeConfig()

const siyuanConfig = new SiyuanConfig(env.public.siyuanApiUrl, env.siyuanAuthToken)
const blogApi = new SiYuanApiAdaptor(siyuanConfig)
const kernelApi = new SiyuanKernelApi(siyuanConfig)

return {
blogApi,
kernelApi,
}
}
50 changes: 50 additions & 0 deletions composables/useMethod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2023, Terwer . All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Terwer designates this
* particular file as subject to the "Classpath" exception as provided
* by Terwer in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com
* or visit www.terwer.space if you need additional information or have any
* questions.
*/

import { createAppLogger } from "~/common/appLogger"

/**
* 通用的可处理异常的方法-同步
*/
export const useMethod = () => {
const logger = createAppLogger("use-method")
const { t } = useI18n()

const handleMethod = (methodCall: () => any) => {
try {
methodCall()
ElMessage.success(t("main.opt.success"))
} catch (e) {
logger.error(t("main.opt.failure"), e)
ElMessage({
type: "error",
message: t("main.opt.failure") + e,
})
throw e
}
}

return { handleMethod }
}
56 changes: 56 additions & 0 deletions composables/useMethodAsync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2023, Terwer . All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Terwer designates this
* particular file as subject to the "Classpath" exception as provided
* by Terwer in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com
* or visit www.terwer.space if you need additional information or have any
* questions.
*/

import { createAppLogger } from "~/common/appLogger"

/**
* 通用的可处理异常的方法-异步
*/
export const useMethodAsync = () => {
const logger = createAppLogger("use-method")
const { t } = useI18n()

const handleMethodAsync = async (methodCall: () => Promise<any>, errorHandler?: any) => {
if (errorHandler) {
if (!errorHandler()) {
return
}
}

try {
await methodCall()
ElMessage.success(t("main.opt.success"))
} catch (e) {
logger.error(t("main.opt.failure"), e)
ElMessage({
type: "error",
message: t("main.opt.failure") + e,
})
throw e
}
}

return { handleMethodAsync }
}
9 changes: 7 additions & 2 deletions locales/en_US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,15 @@ export default {
"share.show.link.option": "Show link options",
"share.other.option": "Set other options here",
"share.other.option.link.expires": "Allow link expires",
"share.link.expires.time.placeholder":
"If this feature is enabled, you can set the link expiration time, unit/second, and support up to 7 days",
"share.link.expires.time.placeholder": "Units/sec, up to 7 days. 0 means permanent",
"share.help": "Learn about sharing",
"share.copy.link": "Copy link",
"share.set.home": "Set home",
"share.share": "Share",
"blog.index.no.permission":
"Sorry, you don't have permission to view this page or the page has expired, please contact the author to reopen sharing!",
"blog.index.no.expires": "Sorry, the page has expired, please contact the author to reopen sharing!",
"blog.index.home.exists": "Sorry, the page is already set as the Page, please remove the Page before unsharing",
"share.link.expires.error":
"Please enter a number that must be legal and no older than 7 days! For perpetual effect, enter 0",
}
8 changes: 6 additions & 2 deletions locales/zh_CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,14 @@ export default {
"share.copy.web.link": "复制网页链接",
"share.show.link.option": "链接选项",
"share.other.option": "在此处设置其他选项",
"share.other.option.link.expires": "允许链接过期",
"share.link.expires.time.placeholder": "单位/秒,最多支持7天",
"share.other.option.link.expires": "链接过期时间",
"share.link.expires.time.placeholder": "单位/秒,最多支持7天。0表示永久生效",
"share.help": "了解共享",
"share.copy.link": "复制链接",
"share.set.home": "设为主页",
"share.share": "分享",
"blog.index.no.permission": "对不起,您暂时无权限查看此页面,请联系作者重新开放分享!",
"blog.index.no.expires": "对不起,该页面已过有效期,请联系作者重新开放分享!",
"blog.index.home.exists": "对不起,该页面已设置为主页,请先移除主页再取消分享",
"share.link.expires.error": "请输入必须为一个合法且不大于 7 天的数字!如需永久生效,请输入0",
}
19 changes: 2 additions & 17 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const generateDynamicV = () => {
}

const isDev = process.env.NODE_ENV === "development"
const appBase = "/plugins/siyuan-blog/"
const appBase = "/"
const staticV = generateDynamicV()

// https://nuxt.com/docs/api/configuration/nuxt-config
Expand All @@ -24,14 +24,7 @@ export default defineNuxtConfig({
},

// build modules
modules: [
"@vueuse/nuxt",
"@nuxtjs/i18n-edge",
"@element-plus/nuxt",
"@nuxtjs/color-mode",
"@pinia/nuxt",
"@nuxt/image",
],
modules: ["@vueuse/nuxt", "@nuxtjs/i18n", "@element-plus/nuxt", "@nuxtjs/color-mode", "@pinia/nuxt", "@nuxt/image"],

// vueuse
vueuse: {
Expand Down Expand Up @@ -64,14 +57,6 @@ export default defineNuxtConfig({
themes: ["dark"],
},

// https://nuxt.com/docs/guide/going-further/custom-routing#hash-mode-spa
ssr: false,
router: {
options: {
hashMode: true,
},
},

css: ["~/assets/siyuan/style.styl", "~/assets/siyuan/index.styl"],

app: {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@
"highlight.js": "^11.8.0",
"pinia": "^2.1.4",
"zhi-device": "^2.3.0",
"zhi-siyuan-api": "^1.16.0"
"zhi-siyuan-api": "^1.20.0"
}
}
Loading

0 comments on commit 23b61a1

Please sign in to comment.