Skip to content

Commit

Permalink
feat: 新版挂件-新增简书适配器
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Jul 30, 2023
1 parent 4bbc435 commit b596687
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/adaptors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { useTypechoApi } from "~/src/adaptors/api/typecho/useTypechoApi.ts"
import { useYuqueApi } from "~/src/adaptors/api/yuque/useYuqueApi.ts"
import { useZhihuWeb } from "~/src/adaptors/web/zhihu/useZhihuWeb.ts"
import { useCsdnWeb } from "~/src/adaptors/web/csdn/useCsdnWeb.ts"
import { useJianshuWeb } from "~/src/adaptors/web/jianshu/useJianshuWeb.ts"

/**
* 适配器统一入口
Expand Down Expand Up @@ -83,6 +84,11 @@ class Adaptors {
blogAdaptor = webApi
break
}
case SubPlatformType.Custom_Jianshu: {
const { webApi } = await useJianshuWeb(key, newCfg)
blogAdaptor = webApi
break
}
default: {
break
}
Expand Down
59 changes: 59 additions & 0 deletions src/adaptors/web/jianshu/adaptor/jianshuWebAdaptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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 { WebAuthApi } from "~/src/adaptors/web/base/web/WebAuthApi.ts"

/**
* 知乎网页授权适配器
*
* @see [wechatsync jianshu adaptor](https://github.com/wechatsync/Wechatsync/blob/master/packages/@wechatsync/drivers/src/jianshu.js)
* @author terwer
* @version 0.9.0
* @since 0.9.0
*/
class JianshuWebAdaptor extends WebAuthApi {
public async getMetaData(): Promise<any> {
const res = await this.proxyFetch("https://www.jianshu.com/settings/basic.json")
const notebooks = await this.proxyFetch("https://www.jianshu.com/author/notebooks")
const avatar = res.data.avatar
const uid = avatar.substring(avatar.lastIndexOf("/") + 1, avatar.lastIndexOf("."))
const flag = !!uid
this.logger.info(`get jianshu metadata finished, flag => ${flag}`)
return {
flag: flag,
uid: uid,
title: res.data.nickname,
avatar: avatar,
type: "jianshu",
displayName: "简书",
supportTypes: ["html"],
home: "https://www.jianshu.com/settings/basic",
icon: "https://www.jianshu.com/favicon.ico",
notebooks: notebooks,
}
}
}

export { JianshuWebAdaptor }
31 changes: 31 additions & 0 deletions src/adaptors/web/jianshu/config/JianshuConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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 { CommonWebConfig } from "~/src/adaptors/web/base/web/config/CommonWebConfig.ts"

/**
* 简书配置
*/
export class JianshuConfig extends CommonWebConfig {}
1 change: 1 addition & 0 deletions src/adaptors/web/jianshu/docs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# JianshuWebAdaptor
85 changes: 85 additions & 0 deletions src/adaptors/web/jianshu/useJianshuWeb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* 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 { JianshuConfig } from "~/src/adaptors/web/jianshu/config/JianshuConfig.ts"
import { createAppLogger } from "~/src/utils/appLogger.ts"
import { AppInstance } from "~/src/appInstance.ts"
import { useSettingStore } from "~/src/stores/useSettingStore.ts"
import { JsonUtil, ObjectUtil, StrUtil } from "zhi-common"
import { Utils } from "~/src/utils/utils.ts"
import { getDynPostidKey } from "~/src/components/set/publish/platform/dynamicConfig.ts"
import { JianshuWebAdaptor } from "~/src/adaptors/web/jianshu/adaptor/jianshuWebAdaptor.ts"

/**
* 用于获取JianshuWeb的API的自定义Hook
*/
const useJianshuWeb = async (key?: string, newCfg?: JianshuConfig) => {
// 创建应用日志记录器
const logger = createAppLogger("use-jianshu-web")

// 记录开始使用Jianshu WebAuth
logger.info("Start using Jianshu WebAuth...")

// 创建应用实例
const appInstance = new AppInstance()
let cfg: JianshuConfig
if (newCfg) {
logger.info("Initialize with the latest newCfg passed in...")
cfg = newCfg
} else {
// 从配置中获取数据
const { getSetting } = useSettingStore()
const setting = await getSetting()
cfg = JsonUtil.safeParse<JianshuConfig>(setting[key], {} as JianshuConfig)
// 如果配置为空,则使用默认的环境变量值,并记录日志
if (ObjectUtil.isEmptyObject(cfg)) {
// 从环境变量获取Jianshu的cookie
const jianshuCookie = Utils.emptyOrDefault(process.env.VITE_JIANSHU_AUTH_TOKEN, "")
cfg = new JianshuConfig(jianshuCookie)
logger.debug("Configuration is empty, using default environment variables.")
} else {
logger.info("Using configuration from settings...")
}
const middlewareUrl = Utils.emptyOrDefault(
process.env.VITE_MIDDLEWARE_URL,
"https://api.terwer.space/api/middleware"
)
if (StrUtil.isEmptyString(cfg.middlewareUrl)) {
cfg.middlewareUrl = middlewareUrl
}
// 初始化posidKey
if (StrUtil.isEmptyString(cfg.posidKey)) {
// 默认值
cfg.posidKey = getDynPostidKey(key)
}
}

const webApi = new JianshuWebAdaptor(appInstance, cfg)
return {
webApi,
}
}

export { useJianshuWeb }
2 changes: 2 additions & 0 deletions src/components/set/publish/platform/dynamicConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ export enum SubPlatformType {
// Custom
Custom_Zhihu = "Zhihu",
Custom_CSDN = "Csdn",
Custom_Jianshu = "Jianshu",

NONE = "none",
}
Expand Down Expand Up @@ -220,6 +221,7 @@ export function getSubtypeList(ptype: PlatformType): SubPlatformType[] {
case PlatformType.Custom:
subtypeList.push(SubPlatformType.Custom_Zhihu)
subtypeList.push(SubPlatformType.Custom_CSDN)
subtypeList.push(SubPlatformType.Custom_Jianshu)
break
default:
break
Expand Down
11 changes: 11 additions & 0 deletions src/utils/import/pre.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,16 @@ export const pre = {
// domain: "csdn.net",
// isEnabled: false,
// },
{
platformType: PlatformType.Custom,
subPlatformType: SubPlatformType.Custom_Jianshu,
platformKey: "custom_Jianshu",
platformName: "简书",
platformIcon: svgIcons.iconIFJianshu,
authMode: AuthMode.WEBSITE,
authUrl: "https://www.jianshu.com/sign_in",
domain: "jianshu.com",
isEnabled: false,
},
],
}
Loading

0 comments on commit b596687

Please sign in to comment.