Skip to content

Commit

Permalink
feat: 新版挂件-修复Electron校验执行顺序问题
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Jul 30, 2023
1 parent ce4a51b commit a063926
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
32 changes: 16 additions & 16 deletions src/components/set/PublishSetting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ const handleSinglePlatformSetting = async (cfg: DynamicConfig) => {
} else if (isInChromeExtension()) {
_handleChromeExtensionAuth(cfg)
} else {
_handleSetCookieAuth(cfg)
await _handleSetCookieAuth(cfg)
}
}
}
Expand Down Expand Up @@ -243,33 +243,32 @@ const _handleSetCookieAuth = async (cfg: DynamicConfig) => {
const handleValidateWebAuth = async (cfg: DynamicConfig) => {
if (isInSiyuanWidget()) {
await _handleValidateOpenBrowserAuth(cfg)
_handleValidateOpenBrowserAuth(cfg)
} else if (isInChromeExtension()) {
await _handleValidateChromeExtensionAuth(cfg)
} else {
await _handleValidateCookieAuth(cfg)
}
}
const _handleValidateOpenBrowserAuth = async (cfg: DynamicConfig) => {
const _handleValidateOpenBrowserAuth = (cfg: DynamicConfig) => {
// 设置将要读取的域名
const domain = cfg.domain
const cookieCb = async (domain: string, cookies: ElectronCookie[]) => {
const cookieCb = async (dynCfg: DynamicConfig, cookies: ElectronCookie[]) => {
// ElMessage.info("验证中,请关注状态,没有授权表示不可用,已授权表示该平台可正常使用...")
logger.debug("get cookie result=>", cookies)
formData.isWebAuthLoading = true
try {
const appInstance = new AppInstance()
const apiAdaptor = await Adaptors.getAdaptor(cfg.platformKey)
const apiAdaptor = await Adaptors.getAdaptor(dynCfg.platformKey)
const api = Utils.webApi(appInstance, apiAdaptor)
// 构造对应平台的cookie
const cookieStr = await api.buildCookie(cookies)
// 更新cookie
const newSettingCfg = JsonUtil.safeParse<WebConfig>(formData.setting[cfg.platformKey], {} as WebConfig)
const newSettingCfg = JsonUtil.safeParse<WebConfig>(formData.setting[dynCfg.platformKey], {} as WebConfig)
newSettingCfg.password = cookieStr
formData.setting[cfg.platformKey] = newSettingCfg
formData.setting[dynCfg.platformKey] = newSettingCfg
// 更新cookie
await updateSetting(formData.setting)
Expand All @@ -278,34 +277,35 @@ const _handleValidateOpenBrowserAuth = async (cfg: DynamicConfig) => {
const metadata = await api.getMetaData()
logger.debug("get meta data=>", metadata)
if (metadata.flag) {
cfg.isAuth = true
const newSettingCfg2 = JsonUtil.safeParse<WebConfig>(formData.setting[cfg.platformKey], {} as WebConfig)
dynCfg.isAuth = true
const newSettingCfg2 = JsonUtil.safeParse<WebConfig>(formData.setting[dynCfg.platformKey], {} as WebConfig)
newSettingCfg2.metadata = metadata
// newSettingCfg2
formData.setting[cfg.platformKey] = newSettingCfg2
formData.setting[dynCfg.platformKey] = newSettingCfg2
// 更新metadata
await updateSetting(formData.setting)
logger.info("已更新最新的metadata")
ElMessage.success("验证成功,该平台可正常使用")
} else {
cfg.isAuth = false
dynCfg.isAuth = false
ElMessage.error("验证失败,该平台将不可用")
}
} catch (e) {
cfg.isAuth = false
dynCfg.isAuth = false
ElMessage.error(t("main.opt.failure") + "=>" + e)
logger.error(e)
}
formData.dynamicConfigArray = replacePlatformByKey(formData.dynamicConfigArray, cfg.platformKey, cfg)
formData.dynamicConfigArray = replacePlatformByKey(formData.dynamicConfigArray, dynCfg.platformKey, dynCfg)
// 替换删除后的平台配置
const dynJsonCfg = setDynamicJsonCfg(formData.dynamicConfigArray)
formData.setting[DYNAMIC_CONFIG_KEY] = dynJsonCfg
// 更新状态
await updateSetting(formData.setting)
formData.isWebAuthLoading = false
}
openBrowserWindow(cfg.authUrl, domain, cookieCb)
openBrowserWindow(cfg.authUrl, cfg, cookieCb)
}
const _handleValidateChromeExtensionAuth = async (cfg: DynamicConfig) => {
Expand Down Expand Up @@ -789,4 +789,4 @@ html[class="dark"]
.add-btn
margin-top 12px
</style>
</style>
14 changes: 8 additions & 6 deletions src/utils/widgetUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,21 @@ import { useSiyuanDevice } from "~/src/composables/useSiyuanDevice.ts"
import { StrUtil } from "zhi-common"
import { BrowserUtil, SiyuanDevice } from "zhi-device"
import { createAppLogger } from "~/src/utils/appLogger.ts"
import { DynamicConfig } from "~/src/components/set/publish/platform/dynamicConfig.ts"

const logger = createAppLogger("widget-utils")

/**
* 打开网页弹窗
*/
export const openBrowserWindow = (url: string, domain?: string, cookieCb?: any) => {
export const openBrowserWindow = (url: string, dynCfg?: DynamicConfig, cookieCb?: any) => {
const { isInSiyuanWidget } = useSiyuanDevice()

if (isInSiyuanWidget()) {
const isDev = false
const isModel = false
const isShow = !cookieCb
doOpenBrowserWindow(url, undefined, undefined, isDev, isModel, isShow, domain, cookieCb)
doOpenBrowserWindow(url, undefined, undefined, isDev, isModel, isShow, dynCfg, cookieCb)
} else {
window.open(url)
}
Expand All @@ -65,7 +66,7 @@ export const openBrowserWindow = (url: string, domain?: string, cookieCb?: any)
* @param isDev - 是否打开开发者工具
* @param modal - 是否模态
* @param isShow - 是否显示
* @param domain - 域名
* @param dynCfg - 动态配置
* @param cookieCallback - 窗口关闭回调
*/
const doOpenBrowserWindow = (
Expand All @@ -75,7 +76,7 @@ const doOpenBrowserWindow = (
isDev = false,
modal = false,
isShow = true,
domain = "",
dynCfg?: DynamicConfig,
cookieCallback = undefined
) => {
try {
Expand Down Expand Up @@ -146,11 +147,12 @@ const doOpenBrowserWindow = (
const readCookies = () => {
// https://www.electronjs.org/zh/docs/latest/api/session
const ses = newWindow.webContents.session
const domain = dynCfg.domain
ses.cookies
.get({ domain })
.then((cookies: any) => {
.then(async (cookies: any) => {
logger.info(`读取cookie事件触发,准备读取 ${domain} 下的所有 Cookie`)
cookieCallback(domain, cookies)
await cookieCallback(dynCfg, cookies)
})
.catch((error: any) => {
console.error(`读取 Cookie 失败:${error}`)
Expand Down

0 comments on commit a063926

Please sign in to comment.