Skip to content

Commit

Permalink
WIP: chore(wechatmp):基本处理完成微信公众号登录
Browse files Browse the repository at this point in the history
  • Loading branch information
liuhuapiaoyuan committed Nov 10, 2024
1 parent 3f57e81 commit 52d2d12
Show file tree
Hide file tree
Showing 13 changed files with 385 additions and 53 deletions.
36 changes: 36 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Next.js: debug server-side",
"type": "node-terminal",
"request": "launch",
"command": "bun run example:dev"
},
{
"name": "Next.js: debug client-side",
"type": "chrome",
"request": "launch",
"url": "http://localhost:3000"
},
{
"name": "Next.js: debug full stack",
"type": "node",
"request": "launch",
"program": "${workspaceFolder}/node_modules/.bin/next",
"runtimeArgs": [
"--inspect"
],
"skipFiles": [
"<node_internals>/**"
],
"serverReadyAction": {
"action": "debugWithEdge",
"killOnServerStop": true,
"pattern": "- Local:.+(https?://.+)",
"uriFormat": "%s",
"webRoot": "${workspaceFolder}"
}
}
]
}
2 changes: 2 additions & 0 deletions apps/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
"next": "15.0.1",
"next-auth": "beta",
"next-auth-oauth": "^1",
"@next-auth-oauth/wechatmp": "*",
"wechatmp-kit": "*",
"react": "19.0.0-rc-cae764ce-20241025",
"react-dom": "19.0.0-rc-cae764ce-20241025",
"tailwind-merge": "^2.5.2"
Expand Down
2 changes: 2 additions & 0 deletions apps/example/src/app/api/auth/wechatmp/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { wechatMpProvder } from '@/auth'
export const { GET, POST } = wechatMpProvder
16 changes: 14 additions & 2 deletions apps/example/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,22 @@ import { prisma } from '@/lib/db'
import { AuthService } from '@/service/auth.service'
import { Gitee } from '@next-auth-oauth/gitee'
import Github from 'next-auth/providers/github'

import Wehcatmp from '@next-auth-oauth/wechatmp'
import { WechatMpApi } from 'wechatmp-kit'
import { AuthConfig } from './auth.config'

export const authAdapter = PrismaAdapter(prisma)
export const wechatMpProvder = Wehcatmp({
wechatMpApi: new WechatMpApi({
appId: process.env.AUTH_WECHATMP_APPID!,
appSecret: process.env.AUTH_WECHATMP_APPSECRET!,
}),
endpoint: 'http://localhost:3000/api/auth/wechatmp',
/**
* 通过消息回复
*/
checkType: 'QRCODE',
})
export const authService = new AuthService()
export const {
handlers,
Expand All @@ -23,7 +35,7 @@ export const {
listAccount,
} = AdavanceNextAuth({
...AuthConfig,
providers: [Gitee, Github],
providers: [Gitee, Github, wechatMpProvder],
adapter: authAdapter,
userService: authService,
autoBind: true,
Expand Down
Binary file modified bun.lockb
Binary file not shown.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"build": "bun --filter 'next-auth-oauth' build",
"release": "bun --filter 'next-auth-oauth' release",
"dev": "bun --filter '{next-auth-oauth,example}' dev",
"example:build": "bun run --filter '{next-auth-oauth,@next-auth-oauth/gitee,example}' build",
"example:dev": "bun run --filter '{example}' dev",
"example:build": "bun run --filter '{next-auth-oauth,@next-auth-oauth/wechatmp,@next-auth-oauth/gitee,example}' build",
"example:dev": "bun run --filter 'example' dev",
"release:wechatmp-kit": "bun run --filter 'wechatmp-kit' release ",
"release:weibo": "bun run --filter '@next-auth-oauth/weibo' release ",
"release:gitee": "bun run --filter '@next-auth-oauth/gitee' release ",
Expand Down
4 changes: 2 additions & 2 deletions packages/wechatmp-kit/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wechatmp-kit",
"version": "0.1.0",
"version": "0.1.1",
"description": "微信公众号工具包",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -29,4 +29,4 @@
"license": "MIC",
"peerDependencies": {},
"devDependencies": {}
}
}
2 changes: 2 additions & 0 deletions packages/wechatmp-kit/src/service/MessageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ export class MessageService {
}
/**
* 解析输入消息
* 1: 如果有AES加密,会先解包获得encrypt,然后解包xml
* 2: 校验签名
*/
parserInput(input: string, params: Omit<QueryParams, 'encrypt'>) {
if (!this.aesMode) {
Expand Down
21 changes: 0 additions & 21 deletions packages/wechatmp-kit/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,3 @@ export function renderXML(data: Record<string, string | number | object>) {
xmls.push('</xml>')
return xmls.join('')
}
const t1 = parseWehcatMessageXML<
Record<string, string | number | object>
>(`<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>
<A>1348831860</A>
<B>1348831860</B>
</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[this is a test]]></Content>
<MsgId>1234567890123456</MsgId>
<MsgDataId>xxxx</MsgDataId>
<Idx>xxxx</Idx>
</xml>
`)
console.log({ t1 })
const t2 = renderXML(t1)
console.log({ t2 })
const t3 = parseWehcatMessageXML(t2)
console.log({ t3 })
4 changes: 3 additions & 1 deletion packages/wechatmp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"docs"
],
"scripts": {
"dev": "tsc --declaration --emitDeclarationOnly && bun build ./src/index.ts --target=node --outdir=dist --watch",
"build": "bun build ./src/index.ts --target=node --outdir=dist && tsc --declaration --emitDeclarationOnly",
"patch:version": "npm version patch",
"release": "bun run build && npm publish && npm version patch"
Expand All @@ -31,7 +32,8 @@
},
"license": "MIC",
"peerDependencies": {
"next-auth": "beta"
"next-auth": "beta",
"next": "^15"
},
"dependencies": {
"wechatmp-kit": "*"
Expand Down
Loading

0 comments on commit 52d2d12

Please sign in to comment.