Skip to content

Commit

Permalink
feat: 新版挂件-知乎网页授权同时兼容Electron、Chrome浏览器插件、Chrome浏览器
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Jul 30, 2023
1 parent 044091e commit 3591d1c
Show file tree
Hide file tree
Showing 9 changed files with 456 additions and 98 deletions.
2 changes: 2 additions & 0 deletions components.d.ts
Expand Up @@ -14,12 +14,14 @@ declare module '@vue/runtime-core' {
CnblogsSetting: typeof import('./src/components/set/publish/singleplatform/metaweblog/CnblogsSetting.vue')['default']
CnblogsTest: typeof import('./src/components/test/CnblogsTest.vue')['default']
CommonBlogSetting: typeof import('./src/components/set/publish/singleplatform/CommonBlogSetting.vue')['default']
CookieSetting: typeof import('./src/components/set/publish/singleplatform/CookieSetting.vue')['default']
ElAlert: typeof import('element-plus/es')['ElAlert']
ElBadge: typeof import('element-plus/es')['ElBadge']
ElButton: typeof import('element-plus/es')['ElButton']
ElCard: typeof import('element-plus/es')['ElCard']
ElCol: typeof import('element-plus/es')['ElCol']
ElContainer: typeof import('element-plus/es')['ElContainer']
ElDialog: typeof import('element-plus/es')['ElDialog']
ElDivider: typeof import('element-plus/es')['ElDivider']
ElForm: typeof import('element-plus/es')['ElForm']
ElFormItem: typeof import('element-plus/es')['ElFormItem']
Expand Down
8 changes: 4 additions & 4 deletions package.json
Expand Up @@ -55,14 +55,14 @@
"vue-i18n": "^9.2.2",
"vue-router": "^4.2.4",
"xmlbuilder2": "^3.1.1",
"zhi-blog-api": "^1.20.0",
"zhi-blog-api": "^1.20.1",
"zhi-common": "^1.11.5",
"zhi-device": "^2.3.0",
"zhi-fetch-middleware": "^0.1.19",
"zhi-fetch-middleware": "^0.1.20",
"zhi-github-middleware": "^0.1.2",
"zhi-lib-base": "^0.4.2",
"zhi-siyuan-api": "^1.28.1",
"zhi-wechatsync-middleware": "^0.1.12",
"zhi-siyuan-api": "^1.28.2",
"zhi-wechatsync-middleware": "^0.1.13",
"zhi-xmlrpc-middleware": "^0.2.19"
}
}
36 changes: 18 additions & 18 deletions pnpm-lock.yaml

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

75 changes: 19 additions & 56 deletions src/adaptors/web/zhihu/adaptor/zhihuWebAdaptor.ts
Expand Up @@ -23,13 +23,8 @@
* questions.
*/

import { ElectronCookie, WebApi } from "zhi-blog-api"
import { JsonUtil } from "zhi-common"
import { AppInstance } from "~/src/appInstance.ts"
import { createAppLogger } from "~/src/utils/appLogger.ts"
import { ZhihuConfig } from "~/src/adaptors/web/zhihu/config/zhihuConfig.ts"
import { useSiyuanApi } from "~/src/composables/useSiyuanApi.ts"
import { SiyuanKernelApi } from "zhi-siyuan-api"
import { ElectronCookie } from "zhi-blog-api"
import { WebAuthApi } from "~/src/adaptors/web/zhihu/base/WebAuthApi.ts"

/**
* 知乎网页授权适配器
Expand All @@ -38,35 +33,25 @@ import { SiyuanKernelApi } from "zhi-siyuan-api"
* @version 0.9.0
* @since 0.9.0
*/
class ZhihuWebAdaptor extends WebApi {
private readonly logger
private cfg: ZhihuConfig
private readonly kernelApi: SiyuanKernelApi

/**
* 初始化博客园 API 适配器
*
* @param appInstance 应用实例
* @param cfg 配置项
*/
constructor(appInstance: AppInstance, cfg: ZhihuConfig) {
super()

this.cfg = cfg
this.logger = createAppLogger("cnblogs-api-adaptor")
const { kernelApi } = useSiyuanApi()
this.kernelApi = kernelApi
}

public updateCfg(cfg: ZhihuConfig) {
this.cfg = cfg
}
class ZhihuWebAdaptor extends WebAuthApi {
// /**
// * 初始化知乎 API 适配器
// *
// * @param appInstance 应用实例
// * @param cfg 配置项
// */
// constructor(appInstance: AppInstance, cfg: ZhihuConfig) {
// super(appInstance, cfg)
//
// this.cfg = cfg
// this.logger = createAppLogger("zhihu-web-adaptor")
// }

public async buildCookie(cookies: ElectronCookie[]): Promise<string> {
return cookies.map((cookie) => `${cookie.name}=${cookie.value}`).join(";")
}

public async getMetaData(): Promise<object> {
public async getMetaData(): Promise<any> {
const res = await this.proxyFetch(
"https://www.zhihu.com/api/v4/me?include=account_status%2Cis_bind_phone%2Cis_force_renamed%2Cemail%2Crenamed_fullname"
)
Expand All @@ -85,31 +70,9 @@ class ZhihuWebAdaptor extends WebApi {
}
}

public async getPreviewUrl(postid: string): Promise<string> {
return Promise.resolve(`https://zhuanlan.zhihu.com/p/${postid}`)
}

// ================
// private methods
// ================
private async proxyFetch(url: string, params: any = {}): Promise<any> {
const fetchResult = await this.kernelApi.forwardProxy(
url,
[
{
Cookie: this.cfg.password,
},
],
params,
"GET",
"application/json",
7000
)
this.logger.debug("proxyFetch result=>", fetchResult)
const resText = fetchResult?.body
const res = JsonUtil.safeParse<any>(resText, {} as any)
return res
}
// public async getPreviewUrl(postid: string): Promise<string> {
// return Promise.resolve(`https://zhuanlan.zhihu.com/p/${postid}`)
// }
}

export { ZhihuWebAdaptor }
125 changes: 125 additions & 0 deletions src/adaptors/web/zhihu/base/WebAuthApi.ts
@@ -0,0 +1,125 @@
/*
* 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 { WebApi, WebConfig } from "zhi-blog-api"
import { SiyuanKernelApi } from "zhi-siyuan-api"
import { CommonFetchClient } from "zhi-fetch-middleware"
import { AppInstance } from "~/src/appInstance.ts"
import { createAppLogger } from "~/src/utils/appLogger.ts"
import { useSiyuanApi } from "~/src/composables/useSiyuanApi.ts"
import { useSiyuanDevice } from "~/src/composables/useSiyuanDevice.ts"
import { ZhihuConfig } from "~/src/adaptors/web/zhihu/config/zhihuConfig.ts"
import { JsonUtil } from "zhi-common"

/**
* 网页授权统一封装基类
*
* @author terwer
* @version 0.9.0
* @since 0.9.0
*/
export class WebAuthApi extends WebApi {
protected logger
protected cfg: WebConfig
private readonly kernelApi: SiyuanKernelApi
private readonly commonFetchClient: CommonFetchClient
private isInSiyuanWidget
private isInChromeExtension

/**
* 初始化网页授权 API 适配器
*
* @param appInstance 应用实例
* @param cfg 配置项
*/
constructor(appInstance: AppInstance, cfg: WebConfig) {
super()

this.cfg = cfg
this.logger = createAppLogger("web-auth-api")
const { kernelApi } = useSiyuanApi()
const { isInSiyuanWidget, isInChromeExtension } = useSiyuanDevice()
this.kernelApi = kernelApi
this.commonFetchClient = new CommonFetchClient(appInstance)
this.isInSiyuanWidget = isInSiyuanWidget()
this.isInChromeExtension = isInChromeExtension()
}

public updateCfg(cfg: ZhihuConfig) {
this.cfg = cfg
}

// ================
// private methods
// ================
protected async proxyFetch(url: string, params: any = {}): Promise<any> {
if (this.isInSiyuanWidget) {
this.logger.info("using siyuan forwardProxy")
const fetchResult = await this.kernelApi.forwardProxy(
url,
[
{
Cookie: this.cfg.password,
},
],
params,
"GET",
"application/json",
7000
)
this.logger.debug("proxyFetch result=>", fetchResult)
const resText = fetchResult?.body
const res = JsonUtil.safeParse<any>(resText, {} as any)
return res
} else if (this.isInChromeExtension) {
this.logger.info("using chrome background")
const fetchOptions = {
method: "GET",
headers: {
"Content-Type": "application/json",
Cookie: this.cfg.password,
},
}
this.logger.info("commonFetchClient from proxyFetch url =>", url)
this.logger.info("commonFetchClient from proxyFetch fetchOptions =>", fetchOptions)
const res = await this.commonFetchClient.fetchCall(url, fetchOptions)
this.logger.debug("commonFetchClient res from proxyFetch =>", res)
return res
} else {
this.logger.info("using middleware proxy")
const fetchOptions = {
method: "GET",
headers: {
"Content-Type": "application/json",
Cookie: this.cfg.password,
},
}
this.logger.info("commonFetchClient from proxyFetch url =>", url)
this.logger.info("commonFetchClient from proxyFetch fetchOptions =>", fetchOptions)
const res = await this.commonFetchClient.fetchCall(url, fetchOptions, this.cfg.middlewareUrl)
this.logger.debug("commonFetchClient res from proxyFetch =>", res)
return res
}
}
}
7 changes: 7 additions & 0 deletions src/adaptors/web/zhihu/useZhihuWeb.ts
Expand Up @@ -62,6 +62,13 @@ const useZhihuWeb = async (key?: string, newCfg?: ZhihuConfig) => {
} 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)) {
// 默认值
Expand Down

0 comments on commit 3591d1c

Please sign in to comment.