Skip to content

Commit

Permalink
Merge pull request #1479 from terwer/dev
Browse files Browse the repository at this point in the history
feat: wechat support scan login
  • Loading branch information
terwer authored Nov 26, 2024
2 parents 8f7ab83 + b3b8575 commit b71155f
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/adaptors/web/zhihu/zhihuWebAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ class ZhihuWebAdaptor extends BaseWebApi {

const mergedHeaders = {
...Object.fromEntries(reqHeaderMap),
...MockBrowser.HEADERS.MACOS,
...MockBrowser.HEADERS.MACOS_CHROME,
...headers,
}

Expand Down Expand Up @@ -406,7 +406,7 @@ class ZhihuWebAdaptor extends BaseWebApi {

const mergedHeaders = {
...Object.fromEntries(reqHeaderMap),
...MockBrowser.HEADERS.MACOS,
...MockBrowser.HEADERS.MACOS_CHROME,
...headers,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import {
import { svgIcons } from "~/src/utils/svgIcons.ts"
import { useVueI18n } from "~/src/composables/useVueI18n.ts"
import { JsonUtil, StrUtil } from "zhi-common"
import { DYNAMIC_CONFIG_KEY } from "~/src/utils/constants.ts"
import { DYNAMIC_CONFIG_KEY, isDev } from "~/src/utils/constants.ts"
import { usePublishSettingStore } from "~/src/stores/usePublishSettingStore.ts"
import { createAppLogger } from "~/src/utils/appLogger.ts"
import { useRouter } from "vue-router"
Expand Down Expand Up @@ -438,7 +438,11 @@ const handleHideCookieDlg = () => {
// hande extra data ====================================================================================================
const handleExtraData = async (dynamicConfigArray: DynamicConfig[]) => {
for (const cfg of dynamicConfigArray) {
// cookie限制
// cookie白名单
if (extraPreCfg.cookieWhiteList.includes(cfg.subPlatformType.toString())) {
cfg.cookieLimit = false
}
// cookie黑名单
if (extraPreCfg.cookieLimit.includes(cfg.subPlatformType.toString())) {
cfg.cookieLimit = true
}
Expand Down
16 changes: 8 additions & 8 deletions src/platforms/pre.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@ import { PRE_COMTANTS } from "~/src/platforms/PreConstants.ts"
* 一些因为政策原因必须要特殊处理的平台
*/
export const extraPreCfg = {
// cookieLimit
// 白名单
cookieWhiteList: [SubPlatformType.Custom_Wechat.toString(), SubPlatformType.Custom_Zhihu.toString()],
// UA白名单
uaWhiteList: ["https://*.qq.com/*", "https://*.zhihu.com/*"],
// 黑名单
cookieLimit: [
SubPlatformType.Custom_Wechat.toString(),
// 知乎可以扫码登录了
// SubPlatformType.Custom_Wechat.toString()
// SubPlatformType.Custom_Zhihu.toString(),
],
cookieLimitTipsAuth: {
[SubPlatformType.Custom_Wechat.toString()]: "https://mp.weixin.qq.com",
// 知乎可以扫码登录了
// [SubPlatformType.Custom_Wechat.toString()]: "https://mp.weixin.qq.com",
// [SubPlatformType.Custom_Zhihu.toString()]: "https://www.zhihu.com/people/terwer",
},
cookieLimitTipsImg: {
[SubPlatformType.Custom_Wechat.toString()]: "https://img1.terwer.space/api/public/202309051734289.png",
// 知乎可以扫码登录了
// [SubPlatformType.Custom_Wechat.toString()]: "https://img1.terwer.space/api/public/202309051734289.png",
// [SubPlatformType.Custom_Zhihu.toString()]: "https://img1.siyuan.wiki/api/vip/open/media/aHR0cHM6Ly9jZG4uc2EubmV0LzIwMjQvMTEvMjYvdFpmSWp6VjE2U3U4djRXLnBuZw==",
},
}
Expand Down Expand Up @@ -303,7 +304,6 @@ export const pre = {
authMode: AuthMode.WEBSITE,
authUrl: "https://mp.weixin.qq.com/",
domain: "qq.com",
cookieLimit: true,
isEnabled: false,
},
{
Expand Down
19 changes: 17 additions & 2 deletions src/utils/MockBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,24 @@
class MockBrowser {
// macOS Edge 浏览器的请求头
public static HEADERS = {
MACOS: {
MACOS_CHROME: {
Accept:
"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"Accept-Encoding": "gzip, deflate, br, zstd",
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
"Cache-Control": "no-cache",
Pragma: "no-cache",
Priority: "u=0, i",
"Sec-Ch-Ua": '"Google Chrome";v="131", "Chromium";v="131", "Not_A Brand";v="24"',
"Sec-Ch-Ua-Mobile": "?0",
"Sec-Ch-Ua-Platform": '"macOS"',
"Sec-Fetch-Dest": "document",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-Site": "none",
"Sec-Fetch-User": "?1",
"Upgrade-Insecure-Requests": "1",
"User-Agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
},
}
}
Expand Down
19 changes: 13 additions & 6 deletions src/utils/widgetUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,18 @@ import { BrowserUtil, SiyuanDevice } from "zhi-device"
import { createAppLogger } from "~/src/utils/appLogger.ts"
import { DynamicConfig } from "~/src/platforms/dynamicConfig.ts"
import { MockBrowser } from "~/src/utils/MockBrowser.ts"
import { extraPreCfg } from "~/src/platforms/pre.ts"

const logger = createAppLogger("widget-utils")

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

if (isInSiyuanWidget()) {
const isDev = false
const isDev = isDevMode ?? false
const isModel = false
const isShow = !cookieCb
doOpenBrowserWindow(url, undefined, undefined, isDev, isModel, isShow, dynCfg, cookieCb)
Expand Down Expand Up @@ -131,10 +132,16 @@ const doOpenBrowserWindow = (
})

// newWindow.webContents.userAgent = `SiYuan/${app.getVersion()} https://b3log.org/siyuan Electron`
newWindow.webContents.userAgent = MockBrowser.HEADERS.MACOS["User-Agent"]
// Object.keys(MockBrowser.MACOS_EDGE_HEADERS).forEach((key) => {
// newWindow.webContents[key] = MockBrowser.MACOS_EDGE_HEADERS[key]
// })
// newWindow.webContents.userAgent = MockBrowser.HEADERS.MACOS_CHROME["User-Agent"]

// 设置 session
const session = newWindow.webContents.session
session.webRequest.onBeforeSendHeaders({ urls: extraPreCfg.uaWhiteList }, (details: any, callback: any) => {
const reqUrl = new URL(url)
console.warn("UA 已修改适配,当前请求为", reqUrl)
details.requestHeaders["User-Agent"] = MockBrowser.HEADERS.MACOS_CHROME["User-Agent"]
callback({ cancel: false, requestHeaders: details.requestHeaders })
})

// 允许
remote.enable(newWindow.webContents)
Expand Down

0 comments on commit b71155f

Please sign in to comment.