All Projects → chf007 → nestjs-wechat-work

chf007 / nestjs-wechat-work

Licence: MIT License
企业微信 NestJS 工具包,目前主要包括企业微信 API 的 Service 封装、登录校验守卫(JWT实现)、扫码登录支持等功能。

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to nestjs-wechat-work

acore-api
RESTful APIs for AzerothCore.
Stars: ✭ 24 (+33.33%)
Mutual labels:  nestjs
Nest-Js-Boiler-Plate
Nest Js Boilerplate with JWT authentication, CRUD functions and payment gateways.
Stars: ✭ 14 (-22.22%)
Mutual labels:  nestjs
nestjs-auth-starter-kit
NestJS Auth Starter Kit (typescript / typeorm / swagger / passport / bcrypt)
Stars: ✭ 37 (+105.56%)
Mutual labels:  nestjs
node-nestjs-structure
Node.js framework NestJS project structure
Stars: ✭ 258 (+1333.33%)
Mutual labels:  nestjs
nest-blog-api
Blog Web API with NestJs, Postgres, and Sequelize ORM
Stars: ✭ 69 (+283.33%)
Mutual labels:  nestjs
Books-Library-API
A starter template for building a restful API with nestjs, nodejs , expressjs , monogdb, mongoose
Stars: ✭ 21 (+16.67%)
Mutual labels:  nestjs
filimo-plus
FilimoPlus: browse, search, play or download filimo items
Stars: ✭ 23 (+27.78%)
Mutual labels:  nestjs
nest-js-boilerplate
Nest.js boilerplate
Stars: ✭ 79 (+338.89%)
Mutual labels:  nestjs
nestjs-auth0
An example NestJS application that uses Auth0 via Passport for authentication.
Stars: ✭ 148 (+722.22%)
Mutual labels:  nestjs
nest-angular
Full-stack with nest js & angular 8
Stars: ✭ 32 (+77.78%)
Mutual labels:  nestjs
tsrpc
A TypeScript RPC framework, with runtime type checking and serialization, support both HTTP and WebSocket. It is very suitable for website / APP / games, and absolutely comfortable to full-stack TypeScript developers.
Stars: ✭ 866 (+4711.11%)
Mutual labels:  nestjs
nestjs-cookie-session
Idiomatic Cookie Session Module for NestJS. Built on top of `cookie-session` 😻
Stars: ✭ 35 (+94.44%)
Mutual labels:  nestjs
nest-typeorm-auth-boilerplate
A NestJS boilerplate with TypeORM and authentication
Stars: ✭ 15 (-16.67%)
Mutual labels:  nestjs
nx-ng-nest-universal
Nx Workspace with a seperated Nest App for Angular Universal SSR.
Stars: ✭ 32 (+77.78%)
Mutual labels:  nestjs
api-old
The official API for the Spotistats app (Android + iOS) 💚
Stars: ✭ 33 (+83.33%)
Mutual labels:  nestjs
nestjs-unleash
Unleash feature toggle support for NestJS
Stars: ✭ 31 (+72.22%)
Mutual labels:  nestjs
daruma-backend
🎎 Shared Expense Manager (Backend) - NestJS+DDD+CQRS+Event Sourcing 🎎
Stars: ✭ 70 (+288.89%)
Mutual labels:  nestjs
nestjs-auth0
Nestjs + Auth0
Stars: ✭ 80 (+344.44%)
Mutual labels:  nestjs
nestjs-starter-kit
Nest.js+Postgres+Nginx+Docker starter kit
Stars: ✭ 41 (+127.78%)
Mutual labels:  nestjs
nest-typed-config
Intuitive, type-safe configuration module for Nest framework ✨
Stars: ✭ 47 (+161.11%)
Mutual labels:  nestjs

NestJS WechatWorkModule

企业微信 NestJS 工具包,目前主要包括企业微信 API 的 Service 封装、登录校验守卫(JWT 实现)、扫码登录支持等功能。 适用于使用企业微信的团队基于 NestJS 开发小型管理后台用。

access_token 缓存在内存中,每次调用企业微信 API 时先检查缓存,如果过期或失效则重新获取并缓存起来。

多实例应用各自维护缓存,机器少的情况下应该不会超出企业微信 API 频率限制。如果机器数很多,说明业务搞大了,恭喜你,到时候请换用别的解决方案或者 fork 代码自行扩展功能,例如使用中心化服务或使用 redis 缓存。

当前支持特性

  • WechatWorkBaseService 企业微信 API 基本服务,包括获取 accessToken,按扫码返回的 code 查询用户 ID
  • WechatWorkContactsService 企业微信 API 通讯录服务,目前只支持成员信息查询、部门信息查询,其它暂未添加
  • WechatWorkAuthGuard 企业微信身份验证守卫,在需要的 Controller 或方法上添加
  • WechatWorkAuthMiddleware 企业微信扫码登录中间件,按以下约定进行扫码登录流程:
    • COOKIE 模式:
      • 分为页面路由和 API 路由,页面路由如果没有登录直接跳企业微信扫码页,API 路由如果没有登录只返回错误码,是否跳转由前端来决定
      • noRedirectPaths 用来配置 API 路由规则,符合此规则的不直接跳企业微信扫码页,只返回错误码,是否跳转由前端来决定
      • WechatWorkAuthMiddleware 中间件会监控 loginPath 路由,如传入 code 则校验企业微信用户信息,正确则生成 jwt token,写入 cookie,然后跳转至 loginSuccessPath,如失败则跳至 loginFailPath;如直接访问,则直接跳转至企业微信扫码页
      • loginSuccessPath 和 loginFailPath 对应的页面要自已实现
      • WechatWorkAuthMiddleware 中间件会监控 logoutPath 路由,如访问,则清空 cookie,跳转至 loginSuccessPath。前端不访问这个页面也可以自行清除 cookie 达到登出的效果
      • 会查询用户基本信息存储在 req 上下文中
      • 身份验证使用 JsonWebToken 实现,一经签发,则在有效期内一直有效
    • CALLBACK_TOKEN 模式:
      • 分为页面路由和 API 路由,页面路由如果没有登录直接跳企业微信扫码页,API 路由如果没有登录只返回错误码,是否跳转由前端来决定
      • noRedirectPaths 用来配置 API 路由规则,符合此规则的不直接跳企业微信扫码页,只返回错误码,是否跳转由前端来决定
      • WechatWorkAuthMiddleware 中间件会监控 loginPath 路由,如传入 code 则校验企业微信用户信息,正确则生成 jwt token,然后跳转至 ${loginSuccessPath}[?|&]${tokenName}=token,如失败则跳至 loginFailPath;如直接访问,则直接跳转至企业微信扫码页
      • loginSuccessPath 和 loginFailPath 对应的页面要自已实现,loginSuccessPath 的页面要处理 url 中返回的 token
      • WechatWorkAuthMiddleware 中间件不处理 logoutPath 路由。前端自行清除 token 达到登出的效果
      • 会查询用户基本信息存储在 req 上下文中
      • 身份验证使用 JsonWebToken 实现,一经签发,则在有效期内一直有效

安装

npm install nestjs-wechat-work

使用

导入 Module

同步配置方式

// 导入 Module
import { Module } from '@nestjs/common';
import { WechatWorkModule } from 'nestjs-wechat-work';

@Module({
  imports: [
    WechatWorkModule.register({
      baseConfig: {
        corpId: 'string', // 企业ID 必填
        agentId: 'string', // 自建应用ID
        agentSecret: 'string', // 自建应用secret
        contactsSecret: 'string', // 通讯录secret
        telephoneSecret: 'string', // 企业电话secret
        scheduleSecret: 'string', // 日程secret
        customerSecret: 'string', // 外部联系人secret
        attendanceAgentId: 'string', // 打卡agent
        attendanceSecret: 'string', // 打卡secret
        approvalAgentId: 'string', // 审批agentId
        approvalSecret: 'string', // 审批secret
        hongbaoAgentId: 'string', // 企业红包agentId
        hongbaoSecret: 'string', // 企业红包secret
      },
      authConfig: {
        type: 'COOKIE', // auth 模式:COOKIE | CALLBACK_TOKEN,默认为 'COOKIE'
        returnDomainName: 'https://admin.xxx.com', // 扫码回跳域名 必填
        jwtSecret: 'adsadsad', // jwt secret 必填
        loginPath: '/login', // 登陆处理 可以在 loginPath 中加一个 _loginFrom 参数,在 loginSuccessPath 中附上该参数,loginSuccessPath 可以再跳转到 _loginFrom 的地址(_loginFrom 可以是任意地址,loginSuccessPath 要做好白名单控制)
        logoutPath: '/logout', // 登出处理
        loginSuccessPath: '/', // 登陆成功后跳转地址
        loginFailPath: '/login-fail.html', // 登陆失败后跳转地址,可以应用中自定义
        noRedirectPaths: [ // 哪些开头的地址不直接跳转而是将控制权交给前端,
          '/api/',
          '/apiv2/',
        ],
        tokenName: '_token', // token cookie name
        tokenExpires: 3600 * 24 * 7, // token 过期秒数
        cookieHttpOnly: true, // COOKIE 模式下是否对 cookie 启用 httpOnly 模式,默认是 true,为了安全建议设为 true
      },
    }),
  ],
  providers: [],
})
export class AppModule {}

异步配置方式

适用于配置项从别的异步服务取得的场景

// 导入 Module
import { Module } from '@nestjs/common';
import { WechatWorkModule } from 'nestjs-wechat-work';

@Module({
  imports: [
    WechatWorkModule.registerAsync({
      imports: [ConfigModule],
      useFactory: async (configService: ConfigService) => ({
        baseConfig: {
          corpId: configService.get<string>('WECHAT_WORK_CORP_ID'), // 企业ID 必填
          agentId: configService.get<string>('WECHAT_WORK_AGENT_ID'), // 自建应用ID
          agentSecret: configService.get<string>('WECHAT_WORK_AGENT_SECRET'), // 自建应用secret
          contactsSecret: configService.get<string>('WECHAT_WORK_CONTACTS_SECRET'), // 通讯录secret
        },
        authConfig: {
          type: 'COOKIE', // auth 模式:COOKIE | CALLBACK_TOKEN,默认为 'COOKIE'
          returnDomainName: configService.get<string>('WECHAT_WORK_RETURN_DOMAIN_NAME'), // 扫码回跳域名 必填
          jwtSecret: configService.get<string>('WECHAT_WORK_JWT_SECRET'), // jwt secret 必填
          loginPath: '/usm/api/login', // 登陆处理
          logoutPath: '/usm/api/logout', // 登出处理
          loginSuccessPath: '/usm/', // 登陆成功后跳转地址
          loginFailPath: '/usm/login-fail', // 登陆失败后跳转地址,可以应用中自定义
          noRedirectPaths: ['/usm/api/'], // 哪些开头的地址不直接跳转而是将控制权交给前端
          tokenName: '_token', // token cookie name
          tokenExpires: 3600 * 24 * 7, // token 过期秒数
        },
      }),
      inject: [ConfigService],
    }),
  ],
  providers: [],
})
export class AppModule {}

使用 Service

// 使用 Service
import { Controller, Get, UseGuards, } from '@nestjs/common';
import { WechatWorkBaseService, WechatWorkContactsService, } from 'nestjs-wechat-work';

@Controller('api')
export class SomeController {
  constructor(private readonly wechatWorkBaseService: WechatWorkBaseService, private readonly wechatWorkContactsService: WechatWorkContactsService) {}

  @Get('test')
  async test() {
    return await this.wechatWorkContactsService.getUserInfo('wechatwork userId');
  }
}

使用 Guard

// 使用 WechatWorkAuthGuard
import { Controller, Get, UseGuards, Req, Request, } from '@nestjs/common';
import { WechatWorkBaseService, WechatWorkContactsService, WechatWorkAuthGuard } from 'nestjs-wechat-work';

@Controller('api')
export class SomeController {
  constructor(private readonly wechatWorkBaseService: WechatWorkBaseService, private readonly wechatWorkContactsService: WechatWorkContactsService) {}

  @UseGuards(WechatWorkAuthGuard)
  @Get('test')
  async test(@Req() request: any) {
    return await this.wechatWorkContactsService.getUserInfo(request.user.userId);
  }
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].