Skip to content

Commit

Permalink
refactor: 新增 ApiTest 页面
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed May 30, 2023
1 parent 8c174ae commit a4408ff
Show file tree
Hide file tree
Showing 8 changed files with 503 additions and 373 deletions.
2 changes: 2 additions & 0 deletions components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ declare module '@vue/runtime-core' {
RouterLink: typeof import('vue-router')['RouterLink']
RouterView: typeof import('vue-router')['RouterView']
SetIndex: typeof import('./src/components/set/SetIndex.vue')['default']
SiyuanTest: typeof import('./src/components/test/SiyuanTest.vue')['default']
WordpressTest: typeof import('./src/components/test/WordpressTest.vue')['default']
}
}
363 changes: 2 additions & 361 deletions src/components/publish/PublishIndex.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,371 +25,12 @@

<script setup lang="ts">
import { createLogger } from "~/src/utils/simpleLogger.ts"
import { reactive, ref } from "vue"
import { AppInstance } from "~/src/appInstance.ts"
import { Utils } from "~/src/utils/utils.ts"
import { SiYuanApiAdaptor, SiyuanConfig, SiyuanKernelApi } from "zhi-siyuan-api"
import { MediaObject, Post } from "zhi-blog-api"
import { Buffer } from "node:buffer"
const logger = createLogger("publisher-index")
const params = ref("{}")
const showParamFile = ref(false)
const paramFile = ref(null)
const logMessage = ref("")
const methodOption = ref("getUsersBlogs")
const METHOD_GET_USERS_BLOGS = "getUsersBlogs"
const METHOD_GET_RECENT_POSTS_COUNT = "getRecentPostsCount"
const METHOD_GET_RECENT_POSTS = "getRecentPosts"
const METHOD_NEW_POST = "newPost"
const METHOD_GET_POST = "getPost"
const METHOD_EDIT_POST = "editPost"
const METHOD_DELETE_POST = "deletePost"
const METHOD_GET_CATEGORIES = "getCategories"
const METHOD_GET_PREVIEW_URL = "getPreviewUrl"
const METHOD_NEW_MEDIA_OBJECT = "newMediaObject"
const methodOptions = reactive({
options: [
{
value: METHOD_GET_USERS_BLOGS,
label: "获取博客信息",
},
{
value: METHOD_GET_RECENT_POSTS_COUNT,
label: "获取最新文章数目",
},
{
value: METHOD_GET_RECENT_POSTS,
label: "获取最新文章列表",
},
{
value: METHOD_NEW_POST,
label: "发布文章",
},
{
value: METHOD_GET_POST,
label: "获取文章详情",
},
{
value: METHOD_EDIT_POST,
label: "编辑文章",
},
{
value: METHOD_DELETE_POST,
label: "删除文章",
},
{
value: METHOD_GET_CATEGORIES,
label: "获取分类列表",
},
{
value: METHOD_GET_PREVIEW_URL,
label: "获取文章预览地址",
},
{
value: METHOD_NEW_MEDIA_OBJECT,
label: "上传资源文件",
},
],
})
const onMethodChange = (val: string) => {
showParamFile.value = false
switch (val) {
case METHOD_GET_USERS_BLOGS: {
params.value = "{}"
break
}
case METHOD_GET_RECENT_POSTS_COUNT: {
params.value = "{}"
break
}
case METHOD_GET_RECENT_POSTS: {
params.value = JSON.stringify({
numOfPosts: 10,
})
break
}
case METHOD_NEW_POST: {
params.value = JSON.stringify({
title: "自动发布的测试标题",
description: "自动发布的测试内容",
mt_keywords: "标签1,标签2",
categories: ["分类1", "分类2"],
// dateCreated: new Date(),
})
break
}
case METHOD_GET_POST: {
params.value = JSON.stringify({
postid: "20230526221603-3mgotyw",
})
break
}
case METHOD_EDIT_POST: {
params.value = JSON.stringify({
postid: "20230527202519-k09a4gx",
post: {
title: "自动发布的测试标题2",
description: "自动发布的测试内容2",
mt_keywords: "标签1,标签2",
categories: ["分类1", "分类2"],
// dateCreated: new Date(),
},
})
break
}
case METHOD_DELETE_POST: {
params.value = JSON.stringify({
postid: "20230528192554-mlcxbe8",
})
break
}
case METHOD_GET_CATEGORIES: {
params.value = "{}"
break
}
case METHOD_GET_PREVIEW_URL: {
params.value = JSON.stringify({
postid: "20230528192554-mlcxbe8",
})
break
}
case METHOD_NEW_MEDIA_OBJECT: {
params.value = "{}"
showParamFile.value = true
break
}
default: {
params.value = "{}"
break
}
}
}
const onImageSelect = async (event: Event) => {
const input = event.target as HTMLInputElement
if (input.files && input.files[0]) {
paramFile.value = input.files[0]
}
}
const fileToBuffer = async (file: any): Promise<any> => {
return new Promise((resolve, reject) => {
const reader = new FileReader()
reader.onload = (e: any) => {
// 将 ArrayBuffer 转换成 Buffer 对象
const buffer = Buffer.from(e.target.result)
resolve(buffer)
}
reader.onerror = reject
reader.readAsArrayBuffer(file)
})
}
const siyuanGetRecentPosts = async () => {
logMessage.value = ""
logMessage.value = "siyuan requesting..."
try {
// appInstance
const appInstance = new AppInstance()
await appInstance.init()
logger.info("appInstance=>", appInstance)
switch (methodOption.value) {
case METHOD_GET_USERS_BLOGS: {
const siyuanCfg = new SiyuanConfig("http://127.0.0.1:6806", "")
// 显示指定修复标题
siyuanCfg.fixTitle = true
const siyuanApiAdaptor = new SiYuanApiAdaptor(appInstance, siyuanCfg)
const siyuanApi = Utils.blogApi(appInstance, siyuanApiAdaptor)
const siyuanUsersBlogs = await siyuanApi.getUsersBlogs()
logMessage.value = JSON.stringify(siyuanUsersBlogs)
logger.info("siyuan users blogs=>", siyuanUsersBlogs)
break
}
case METHOD_GET_RECENT_POSTS_COUNT: {
const siyuanCfg = new SiyuanConfig("http://127.0.0.1:6806", "")
const siyuanApiAdaptor = new SiYuanApiAdaptor(appInstance, siyuanCfg)
const siyuanApi = Utils.blogApi(appInstance, siyuanApiAdaptor)
const recentPostsCount = await siyuanApi.getRecentPostsCount()
logMessage.value = JSON.stringify(recentPostsCount)
logger.info("siyuan recent post count=>", recentPostsCount)
break
}
case METHOD_GET_RECENT_POSTS: {
const siyuanCfg = new SiyuanConfig("http://127.0.0.1:6806", "")
// 显示指定修复标题
siyuanCfg.fixTitle = true
const siyuanApiAdaptor = new SiYuanApiAdaptor(appInstance, siyuanCfg)
const siyuanApi = Utils.blogApi(appInstance, siyuanApiAdaptor)
const paramsValue = JSON.parse(params.value)
const siyuanPosts = await siyuanApi.getRecentPosts(paramsValue.numOfPosts)
logMessage.value = JSON.stringify(siyuanPosts)
logger.info("siyuan recent post=>", siyuanPosts)
break
}
case METHOD_NEW_POST: {
const siyuanCfg = new SiyuanConfig("http://127.0.0.1:6806", "")
siyuanCfg.notebook = "20230506132031-qbtyjdk"
const siyuanApiAdaptor = new SiYuanApiAdaptor(appInstance, siyuanCfg)
const siyuanApi = Utils.blogApi(appInstance, siyuanApiAdaptor)
const paramsValue = JSON.parse(params.value)
let post = new Post()
post = {
...post,
...paramsValue,
}
const result = await siyuanApi.newPost(post)
logMessage.value = JSON.stringify(result)
break
}
case METHOD_GET_POST: {
const siyuanCfg = new SiyuanConfig("http://127.0.0.1:6806", "")
const siyuanApiAdaptor = new SiYuanApiAdaptor(appInstance, siyuanCfg)
const siyuanApi = Utils.blogApi(appInstance, siyuanApiAdaptor)
const paramsValue = JSON.parse(params.value)
const postid = paramsValue.postid
const siyuanPost = await siyuanApi.getPost(postid)
logMessage.value = JSON.stringify(siyuanPost)
logger.info("siyuan post=>", siyuanPost)
break
}
case METHOD_EDIT_POST: {
const siyuanCfg = new SiyuanConfig("http://127.0.0.1:6806", "")
siyuanCfg.notebook = "20230506132031-qbtyjdk"
const siyuanApiAdaptor = new SiYuanApiAdaptor(appInstance, siyuanCfg)
const siyuanApi = Utils.blogApi(appInstance, siyuanApiAdaptor)
const paramsValue = JSON.parse(params.value)
const postid = paramsValue.postid
let post = new Post()
post = {
...post,
...paramsValue.post,
}
const result = await siyuanApi.editPost(postid, post)
logMessage.value = JSON.stringify(result)
break
}
case METHOD_DELETE_POST: {
const siyuanCfg = new SiyuanConfig("http://127.0.0.1:6806", "")
siyuanCfg.notebook = "20230506132031-qbtyjdk"
const siyuanApiAdaptor = new SiYuanApiAdaptor(appInstance, siyuanCfg)
const siyuanApi = Utils.blogApi(appInstance, siyuanApiAdaptor)
const paramsValue = JSON.parse(params.value)
const postid = paramsValue.postid
const result = await siyuanApi.deletePost(postid)
logMessage.value = JSON.stringify(result)
break
}
case METHOD_GET_CATEGORIES: {
const siyuanCfg = new SiyuanConfig("http://127.0.0.1:6806", "")
const siyuanApiAdaptor = new SiYuanApiAdaptor(appInstance, siyuanCfg)
const siyuanApi = Utils.blogApi(appInstance, siyuanApiAdaptor)
const siyuanCategories = await siyuanApi.getCategories()
logMessage.value = JSON.stringify(siyuanCategories)
logger.info("siyuan categories=>", siyuanCategories)
break
}
case METHOD_GET_PREVIEW_URL: {
const siyuanCfg = new SiyuanConfig("http://127.0.0.1:6806", "")
const siyuanApiAdaptor = new SiYuanApiAdaptor(appInstance, siyuanCfg)
const siyuanApi = Utils.blogApi(appInstance, siyuanApiAdaptor)
const paramsValue = JSON.parse(params.value)
const postid = paramsValue.postid
const siyuanPreviewUrl = await siyuanApi.getPreviewUrl(postid)
logMessage.value = JSON.stringify(siyuanPreviewUrl)
logger.info("siyuan preview url=>", siyuanPreviewUrl)
break
}
case METHOD_NEW_MEDIA_OBJECT: {
const siyuanCfg = new SiyuanConfig("http://127.0.0.1:6806", "")
const siyuanApiAdaptor = new SiYuanApiAdaptor(appInstance, siyuanCfg)
const siyuanApi = Utils.blogApi(appInstance, siyuanApiAdaptor)
const bits = await fileToBuffer(paramFile.value)
const mediaObject = new MediaObject(paramFile.value.name, paramFile.value.type, bits)
logger.info("mediaObject=>", mediaObject)
const result = await siyuanApi.newMediaObject(mediaObject, async () => {
const formData = new FormData()
const blob = new Blob([mediaObject.bits])
formData.append("file[]", blob, mediaObject.name)
formData.append("assetsDirPath", "/assets/")
const siyuanKernelApi = new SiyuanKernelApi(appInstance, siyuanCfg)
return await siyuanKernelApi.uploadAsset(formData)
})
logMessage.value = JSON.stringify(result)
logger.info("siyuan new mediaObject result=>", result)
break
}
default:
break
}
} catch (e) {
logMessage.value = e
console.error(e)
}
}
const wordpressGetRecentPosts = async () => {
logMessage.value = ""
logMessage.value = "wordpress requesting..."
try {
// appInstance
const appInstance = new AppInstance()
await appInstance.init()
logger.info("appInstance=>", appInstance)
const wordpressCfg = {}
const wordpressApiAdaptor = {}
const wordpressApi = Utils.blogApi(appInstance, wordpressApiAdaptor)
const wordpressPosts = await wordpressApi.getRecentPosts(10)
logMessage.value = JSON.stringify(wordpressPosts)
logger.info("wordpress recent post=>", wordpressPosts)
} catch (e) {
logMessage.value = e
}
}
</script>

<template>
<div id="publish-index">
<div class="method-list">
<el-select v-model="methodOption" class="m-2" placeholder="请选择方法名称" @change="onMethodChange">
<el-option v-for="item in methodOptions.options" :key="item.value" :label="item.label" :value="item.value" />
</el-select>
</div>

<el-tabs type="border-card">
<el-tab-pane label="思源">
<p><el-button @click="siyuanGetRecentPosts">开始测试思源</el-button></p>
</el-tab-pane>
<el-tab-pane label="WordPress">
<p><el-button @click="wordpressGetRecentPosts">开始测试WordPress</el-button></p>
</el-tab-pane>
</el-tabs>

<p><el-button>入参</el-button></p>
<p><el-input v-model="params" type="textarea" :rows="5"></el-input></p>
<p v-if="showParamFile"><input type="file" @change="onImageSelect" /></p>

<p><el-button>结果</el-button></p>
<p>
<el-input v-model="logMessage" type="textarea" :rows="10" placeholder="日志信息"></el-input>
</p>
</div>
<div id="publish-index">PublishIndex</div>
</template>

<style lang="stylus" scoped>
#publish-index
margin 16px 20px
.method-list
margin-bottom 16px
</style>
<style lang="stylus" scoped></style>
Loading

0 comments on commit a4408ff

Please sign in to comment.