nestjs-wechat
v0.1.0
Published
nestjs module for wechat server-side api.
Downloads
3
Readme
nest-wechat
微信公众号、微信程序开、微信小游戏、微信支付以及企业微信等服务端API nestjs 模块封装。也可以直接当工具类使用。
快速开始
安装
npm i --save nest-wechat
nestjs 模块引入
- register方法注册
import { Module } from '@nestjs/common';
import { WeChatModule } from 'nest-wechat';
@Module({
imports: [WeChatModule.register({appId: 'your app id', secret: 'your secret'})],
})
export class AppModule {
}
- forRoot配置注册
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { WeChatModule } from 'nest-wechat';
@Module({
imports: [ConfigModule.forRoot({
envFilePath: '.env.test.local',
})],
inject: [ConfigService],
useFactory: (configService: ConfigService) => ({
appId: configService.get('TEST_APPID') || '',
secret: configService.get('TEST_SECRET') || '',
}),
})],
})
export class AppModule {
}
工具类引入
import { WeChatService } from 'nest-wechat';
const service = new WeChatService({ appId: 'your app id', secret: 'your secret'});
微信公众号API
网页授权
getAccessTokenByCode
- 参数:
- {string} code 微信客户端打开授权页跳转后获取到的code
- 返回值
- Promise<AccessTokenResult> 调用后返回Promise
正确时返回
{
"access_token":"ACCESS_TOKEN",
"expires_in":7200,
"refresh_token":"REFRESH_TOKEN",
"openid":"OPENID",
"scope":"SCOPE"
}
错误时返回
{
"errcode": 40029,
"errmsg": "invalid code"
}
获取Access token
getAccountAccessToken
- 返回值
- Promise<AccountAccessTokenResult> 调用后返回Promise
正确时返回
{
"access_token": "52_s0Mcl3E3DBKs12rthjxG8_DOvsIC4puV9A34WQR6Bhb_30TW9W9BjhUxDRkyph-hY9Ab2QS03Q8wZBe5UkA1k0q0hc17eUDZ7vAWItl4iahnhq_57dCoKc1dQ3AfiHUKGCCMJ2NcQ0BmbBRIKBEgAAAPGJ",
"expires_in": 7200
}
错误时返回
{
"errcode": 40013,
"errmsg": "invalid appid"
}
获取jsapi_ticket
getJSApiTicket
- 返回值
- Promise<TicketResult> 调用后返回Promise
返回数据
{
"errcode": 0,
"errmsg": "ok",
"ticket": "bxLdikRXVbTPdHSM05e5u5sUoXNKd8-41ZO3MhKoyN5OfkWITDGgnr2fwJ0m9E8NYzWKVZvdVtaUgWvsdshFKA",
"expires_in": 7200
}
JS-SDK使用权限签名
jssdkSignature
- 参数:
- {string} url 参与签名计算的url
- 返回值
- Promise<SignatureResult> 调用后返回Promise
发送模板消息
sendTemplateMessage
- 参数:
- {TemplateMessage} message 模板消息参数类型
- 返回值
- Promise<DefaultRequestResult & { msgid: string }> 调用后返回Promise
Run Test
Create .env.test.local file, and save your test appid and secret in the file.
TEST_APPID=your/test/appid
TEST_SECRET=your/test/secret
TEST_JSSDK_URL=your/test/url
Run test.
npm run test