Skip to content

Commit

Permalink
feat: 微信公众号支持 cookie 认证
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Sep 4, 2023
1 parent 1c8934e commit 7816ca5
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 39 deletions.
21 changes: 11 additions & 10 deletions src/adaptors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { useHexoApi } from "~/src/adaptors/api/hexo/useHexoApi.ts"
import { CommonBlogConfig } from "~/src/adaptors/api/base/commonBlogConfig.ts"
import { useGitlabhexoApi } from "~/src/adaptors/api/gitlab-hexo/useGitlabhexoApi.ts"
import { useCsdnWeb } from "~/src/adaptors/web/csdn/useCsdnWeb.ts"
import { useWechatWeb } from "~/src/adaptors/web/wechat/useWechatWeb.ts"

/**
* 适配器统一入口
Expand Down Expand Up @@ -109,6 +110,11 @@ class Adaptors {
conf = cfg
break
}
case SubPlatformType.Custom_Wechat: {
const { cfg } = await useWechatWeb(key)
conf = cfg
break
}
// case SubPlatformType.Custom_Jianshu: {
// const { cfg } = await useJianshuWeb(key)
// conf = cfg
Expand All @@ -119,11 +125,6 @@ class Adaptors {
// conf = cfg
// break
// }
// case SubPlatformType.Custom_Wechat: {
// const { cfg } = await useWechatWeb(key)
// conf = cfg
// break
// }
case SubPlatformType.System_Siyuan: {
const { siyuanConfig } = useSiyuanApi()
conf = siyuanConfig
Expand Down Expand Up @@ -199,6 +200,11 @@ class Adaptors {
blogAdaptor = webApi
break
}
case SubPlatformType.Custom_Wechat: {
const { webApi } = await useWechatWeb(key, newCfg)
blogAdaptor = webApi
break
}
// case SubPlatformType.Custom_Jianshu: {
// const { webApi } = await useJianshuWeb(key, newCfg)
// blogAdaptor = webApi
Expand All @@ -209,11 +215,6 @@ class Adaptors {
// blogAdaptor = webApi
// break
// }
// case SubPlatformType.Custom_Wechat: {
// const { webApi } = await useWechatWeb(key, newCfg)
// blogAdaptor = webApi
// break
// }
case SubPlatformType.System_Siyuan: {
const { blogApi } = useSiyuanApi()
blogAdaptor = blogApi
Expand Down
1 change: 1 addition & 0 deletions src/adaptors/web/wechat/useWechatWeb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ const useWechatWeb = async (key?: string, newCfg?: WechatConfig) => {

const webApi = new WechatWebAdaptor(appInstance, cfg)
return {
cfg,
webApi,
}
}
Expand Down
39 changes: 33 additions & 6 deletions src/adaptors/web/wechat/wechatWebAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
*/

import { BaseWebApi } from "~/src/adaptors/web/base/baseWebApi.ts"
import * as cheerio from "cheerio"
import { StrUtil } from "zhi-common"

/**
* 掘金网页授权适配器
Expand All @@ -35,13 +37,38 @@ import { BaseWebApi } from "~/src/adaptors/web/base/baseWebApi.ts"
*/
class WechatWebAdaptor extends BaseWebApi {
public async getMetaData(): Promise<any> {
const res = await this.proxyFetch("https://mp.weixin.qq.com/")
console.log("WechatWebAdaptor res=>", res)
const flag = false
this.logger.info(`get wechat metadata finished, flag => ${flag}`)
return {
flag: flag,
const res = await this.webProxyFetch("https://mp.weixin.qq.com/", [], {}, "GET", "text/html")
this.logger.debug("WechatWebAdaptor res=>", { res: res })
const $ = cheerio.load(res)
const scriptElement = $("script")[0]
// 获取元素的文本内容
const scriptText = $(scriptElement).text()
this.logger.debug("scriptElement =>", { scriptText: scriptText })

const code = scriptText.substring(scriptText.indexOf("window.wx.commonData"))
const wx = new Function("window.wx = {}; window.handlerNickname = function(){};" + code + "; return window.wx;")()
this.logger.debug(code, wx)

const commonData = Object.assign({}, wx.commonData)
delete (window as any).wx
if (!commonData.data.t) {
throw new Error("微信公众号未登录或者等于过期,请更新cookie")
}

const metadata = {
flag: !StrUtil.isEmptyString(commonData.data.t),
uid: commonData.data.user_name,
title: commonData.data.nick_name,
token: commonData.data.t,
commonData: commonData,
avatar: $(".weui-desktop-account__thumb").eq(0).attr("src"),
type: "weixin",
supportTypes: ["html"],
home: "https://mp.weixin.qq.com",
icon: "https://mp.weixin.qq.com/favicon.ico",
}
this.logger.info(`get wechat metadata finished, flag => ${metadata}`)
return metadata
}
}

Expand Down
24 changes: 16 additions & 8 deletions src/components/set/PublishSetting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,14 @@ const handleSinglePlatformSetting = async (cfg: DynamicConfig) => {
}
const handleSinglePlatformWebAuth = async (cfg: DynamicConfig) => {
if (isInSiyuanWidget()) {
await _handleOpenBrowserAuth(cfg)
} else if (isInChromeExtension()) {
_handleChromeExtensionAuth(cfg)
if (!cfg.cookieLimit) {
if (isInSiyuanWidget()) {
await _handleOpenBrowserAuth(cfg)
} else if (isInChromeExtension()) {
_handleChromeExtensionAuth(cfg)
} else {
await _handleSetCookieAuth(cfg)
}
} else {
await _handleSetCookieAuth(cfg)
}
Expand Down Expand Up @@ -246,10 +250,14 @@ const _handleSetCookieAuth = async (cfg: DynamicConfig) => {
}
const handleValidateWebAuth = async (cfg: DynamicConfig) => {
if (isInSiyuanWidget()) {
_handleValidateOpenBrowserAuth(cfg)
} else if (isInChromeExtension()) {
await _handleValidateChromeExtensionAuth(cfg)
if (!cfg.cookieLimit) {
if (isInSiyuanWidget()) {
_handleValidateOpenBrowserAuth(cfg)
} else if (isInChromeExtension()) {
await _handleValidateChromeExtensionAuth(cfg)
} else {
await _handleValidateCookieAuth(cfg)
}
} else {
await _handleValidateCookieAuth(cfg)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ const submitForm = async (formEl) => {
>
<el-alert
class="top-tip"
title="检测到当前为浏览器页面,受技术限制无法获取Cookie,请手动粘贴平台Cookie。本插件承诺,所有数据仅在您的本地存储,我们不会上传或者分享你的任何数据,请放心使用。如有疑问请查看帮助文档。"
title="检测到当前为浏览器页面或者受平台技术限制,无法获取Cookie,请手动粘贴平台Cookie。本插件承诺,所有数据仅在您的本地存储,我们不会上传或者分享你的任何数据,请放心使用。如有疑问请查看帮助文档。"
type="error"
:closable="false"
/>
Expand Down
10 changes: 8 additions & 2 deletions src/platforms/dynamicConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ export class DynamicConfig {
*/
domain?: string

/**
* cookie现在
*/
cookieLimit?: boolean

/**
* 是否内置
*/
Expand All @@ -99,6 +104,7 @@ export class DynamicConfig {
this.isAuth = false
this.isEnabled = false
this.authMode = AuthMode.API
this.cookieLimit = false
this.isSys = false

this.subPlatformType = subPlatformType
Expand Down Expand Up @@ -188,9 +194,9 @@ export enum SubPlatformType {
// Custom
Custom_Zhihu = "Zhihu",
Custom_CSDN = "Csdn",
Custom_Wechat = "Wechat",
// Custom_Jianshu = "Jianshu",
// Custom_Juejin = "Juejin",
// Custom_Wechat = "Wechat",

// System
System_Siyuan = "Siyuan",
Expand Down Expand Up @@ -246,9 +252,9 @@ export function getSubtypeList(ptype: PlatformType): SubPlatformType[] {
case PlatformType.Custom:
subtypeList.push(SubPlatformType.Custom_Zhihu)
subtypeList.push(SubPlatformType.Custom_CSDN)
subtypeList.push(SubPlatformType.Custom_Wechat)
// subtypeList.push(SubPlatformType.Custom_Jianshu)
// subtypeList.push(SubPlatformType.Custom_Juejin)
// subtypeList.push(SubPlatformType.Custom_Wechat)
break
case PlatformType.System:
subtypeList.push(SubPlatformType.System_Siyuan)
Expand Down
25 changes: 13 additions & 12 deletions src/utils/import/pre.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export const pre = {
domain: "zhihu.com",
isEnabled: false,
},
// CSDN 目前有CA验证
// CSDN 目前有CA验证 已解决
{
platformType: PlatformType.Custom,
subPlatformType: SubPlatformType.Custom_CSDN,
Expand All @@ -173,6 +173,18 @@ export const pre = {
domain: "csdn.net",
isEnabled: false,
},
{
platformType: PlatformType.Custom,
subPlatformType: SubPlatformType.Custom_Wechat,
platformKey: "custom_Wechat",
platformName: "微信公众号",
platformIcon: svgIcons.iconIFWechat,
authMode: AuthMode.WEBSITE,
authUrl: "https://mp.weixin.qq.com/",
domain: "qq.com",
cookieLimit: true,
isEnabled: false,
},
// {
// platformType: PlatformType.Custom,
// subPlatformType: SubPlatformType.Custom_Jianshu,
Expand All @@ -195,17 +207,6 @@ export const pre = {
// domain: "juejin.cn",
// isEnabled: false,
// },
// {
// platformType: PlatformType.Custom,
// subPlatformType: SubPlatformType.Custom_Wechat,
// platformKey: "custom_Wechat",
// platformName: "微信公众号",
// platformIcon: svgIcons.iconIFWechat,
// authMode: AuthMode.WEBSITE,
// authUrl: "https://mp.weixin.qq.com/",
// domain: "qq.com",
// isEnabled: false,
// },
],
systemCfg: <DynamicConfig[]>[
{
Expand Down

0 comments on commit 7816ca5

Please sign in to comment.