Skip to content

Commit

Permalink
feat: 新版挂件-新增知乎网页授权
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Jul 29, 2023
1 parent 42e384d commit 0ff11a4
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 46 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@
"vue-i18n": "^9.2.2",
"vue-router": "^4.2.4",
"xmlbuilder2": "^3.1.1",
"zhi-blog-api": "^1.18.0",
"zhi-blog-api": "^1.18.1",
"zhi-common": "^1.11.5",
"zhi-device": "^2.3.0",
"zhi-fetch-middleware": "^0.1.19",
"zhi-github-middleware": "^0.1.2",
"zhi-lib-base": "^0.4.2",
"zhi-siyuan-api": "^1.27.7",
"zhi-wechatsync-middleware": "^0.1.7",
"zhi-siyuan-api": "^1.27.8",
"zhi-wechatsync-middleware": "^0.1.8",
"zhi-xmlrpc-middleware": "^0.2.19"
}
}
40 changes: 20 additions & 20 deletions pnpm-lock.yaml

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

4 changes: 3 additions & 1 deletion src/adaptors/web/zhihu/zhihuWebAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/

import { WebApi } from "zhi-blog-api"
import { JsonUtil } from "zhi-common"
import { JsonUtil, StrUtil } from "zhi-common"

/**
* 知乎网页授权适配器
Expand All @@ -41,7 +41,9 @@ class ZhihuWebAdaptor extends WebApi {
const resText = await fetchResponse.text()
// console.log(res);
const res = JsonUtil.safeParse<any>(resText, {} as any)
const flag = res.uid ? true : false
return {
flag: flag,
uid: res.uid,
title: res.name,
avatar: res.avatar_url,
Expand Down
27 changes: 20 additions & 7 deletions src/components/set/PublishSetting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ import { usePlatformDefine } from "~/src/composables/usePlatformDefine.ts"
import { ElMessage, ElMessageBox } from "element-plus"
import { svgIcons } from "~/src/utils/svgIcons.ts"
import { openBrowserWindow } from "~/src/utils/widgetUtils.ts"
import Adaptors from "~/src/adaptors"
import { Utils } from "~/src/utils/utils.ts"
import { AppInstance } from "~/src/appInstance.ts"
const logger = createAppLogger("publish-setting")
Expand Down Expand Up @@ -160,14 +163,24 @@ const handleOpenBrowserAuth = async (cfg: DynamicConfig) => {
}
const handleValidateWebAuth = (cfg: DynamicConfig) => {
const cookieCb = async (coo) => {
// 设置将要读取的域名
const domain = cfg.domain
const cookieCb = async (domain: string, coo: any) => {
ElMessage.info("验证中,请关注状态,没有授权表示不可用,已授权表示该平台可正常使用...")
console.log("coo=>", coo)
// ElMessage.success("验证成功,该平台可正常使用")
// ElMessage.error(("验证失败,该平台将不可用"))
console.log("get cookie result=>", coo)
const appInstance = new AppInstance()
const apiAdaptor = await Adaptors.getAdaptor(cfg.platformKey)
const api = Utils.webApi(appInstance, apiAdaptor)
const result = await api.getMetaData()
logger.info("get meta data=>", result)
if (result.flag) {
ElMessage.success("验证成功,该平台可正常使用")
} else {
ElMessage.error("验证失败,该平台将不可用")
}
}
openBrowserWindow(cfg.authUrl, cookieCb)
openBrowserWindow(cfg.authUrl, domain, cookieCb)
}
const handleImportPre = () => {
Expand Down Expand Up @@ -357,8 +370,8 @@ onMounted(async () => {
{{ platform.authMode === AuthMode.API ? "设置" : platform.isAuth ? "再次授权" : "授权" }}
</el-text>
<el-text
class="action-btn action-web-auth"
v-if="platform.isEnabled && platform.authMode === AuthMode.WEBSITE && !platform.isAuth"
class="action-btn action-web-auth"
@click="handleValidateWebAuth(platform)"
>
<el-icon>
Expand Down
5 changes: 5 additions & 0 deletions src/components/set/publish/PlatformAddForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ const initForm = async (ptype: PlatformType, subtype: SubPlatformType) => {
const newCfg = new DynamicConfig(ptype, newKey, newKey, subtype, svgIcon)
newCfg.authMode = preTmpl.authMode
newCfg.authUrl = preTmpl.authUrl ?? ""
newCfg.domain = preTmpl.domain ?? ""
formData.dynCfg = newCfg
logger.debug("pkey already exists, initialize the new one")
} else {
Expand Down Expand Up @@ -264,6 +265,10 @@ initPage()
<el-form-item v-if="formData.dynCfg.authMode === AuthMode.WEBSITE" label="登录地址" prop="authUrl">
<el-input v-model="formData.dynCfg.authUrl" placeholder="请输入该平台的网页登录地址" />
</el-form-item>
<!-- 主域名 -->
<el-form-item v-if="formData.dynCfg.authMode === AuthMode.WEBSITE" label="主域名" prop="domain">
<el-input v-model="formData.dynCfg.domain" placeholder="请输入该平台的主域名" />
</el-form-item>
<!-- 是否启用 -->
<el-form-item label="是否启用">
<el-switch v-model="formData.dynCfg.isEnabled" :disabled="formData.isPre" />
Expand Down
4 changes: 4 additions & 0 deletions src/components/set/publish/PlatformUpdateForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ initPage()
<el-form-item v-if="formData.dynCfg.authMode === AuthMode.WEBSITE" label="登录地址" prop="authUrl">
<el-input v-model="formData.dynCfg.authUrl" placeholder="请输入该平台的网页登录地址" />
</el-form-item>
<!-- 主域名 -->
<el-form-item v-if="formData.dynCfg.authMode === AuthMode.WEBSITE" label="主域名" prop="domain">
<el-input v-model="formData.dynCfg.domain" placeholder="请输入该平台的主域名" />
</el-form-item>
<!-- 是否启用 -->
<el-form-item label="是否启用">
<el-switch v-model="formData.dynCfg.isEnabled" />
Expand Down
10 changes: 10 additions & 0 deletions src/components/set/publish/platform/dynamicConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ export class DynamicConfig {
*/
authUrl?: string

/**
* 域名
*/
domain?: string

/**
* cookie
*/
cookie?: string

/**
* YAML转换器
*/
Expand Down
1 change: 1 addition & 0 deletions src/utils/import/pre.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export const pre = {
platformIcon: svgIcons.iconIFZhihu,
authMode: AuthMode.WEBSITE,
authUrl: "https://www.zhihu.com/signin",
domain: "zhihu.com",
isEnabled: false,
},
],
Expand Down
26 changes: 11 additions & 15 deletions src/utils/widgetUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ const logger = createAppLogger("widget-utils")
/**
* 打开网页弹窗
*/
export const openBrowserWindow = (url: string, cookieCb?: any) => {
export const openBrowserWindow = (url: string, domain?: string, cookieCb?: any) => {
const { isInSiyuanWidget } = useSiyuanDevice()

if (isInSiyuanWidget()) {
const isDev = false
const isModel = false
const isShow = !cookieCb
doOpenBrowserWindow(url, undefined, undefined, isDev, isModel, isShow, cookieCb)
doOpenBrowserWindow(url, undefined, undefined, isDev, isModel, isShow, domain, cookieCb)
} else {
window.open(url)
}
Expand All @@ -65,6 +65,7 @@ export const openBrowserWindow = (url: string, cookieCb?: any) => {
* @param isDev - 是否打开开发者工具
* @param modal - 是否模态
* @param isShow - 是否显示
* @param domain - 域名
* @param cookieCallback - 窗口关闭回调
*/
const doOpenBrowserWindow = (
Expand All @@ -74,7 +75,8 @@ const doOpenBrowserWindow = (
isDev = false,
modal = false,
isShow = true,
cookieCallback
domain = "",
cookieCallback = undefined
) => {
try {
if (StrUtil.isEmptyString(url)) {
Expand Down Expand Up @@ -134,7 +136,7 @@ const doOpenBrowserWindow = (
}

// 监听 close 事件
newWindow.on("close", (evt) => {
newWindow.on("close", (evt: any) => {
logger.info("窗口关闭事件触发")
})
newWindow.loadURL(url)
Expand All @@ -144,25 +146,19 @@ const doOpenBrowserWindow = (
const readCookies = () => {
// https://www.electronjs.org/zh/docs/latest/api/session
const ses = newWindow.webContents.session

// 设置将要读取的域名
const domain = "zhihu.com"

ses.cookies
.get({ domain })
.then((cookies) => {
.then((cookies: any) => {
logger.info(`读取cookie事件触发,准备读取 ${domain} 下的所有 Cookie`)
cookieCallback(cookies)
cookieCallback(domain, cookies)
})
.catch((error) => {
.catch((error: any) => {
console.error(`读取 Cookie 失败:${error}`)
})
}
readCookies()
// 将窗口隐藏起来
newWindow.once("ready-to-show", () => {
newWindow.hide()
})
newWindow.hide()
newWindow.close()
}
} catch (e) {
logger.error("Open browser window failed", e)
Expand Down

0 comments on commit 0ff11a4

Please sign in to comment.