Skip to content

Commit

Permalink
feat: captcha use newest cacheManager component (#3570)
Browse files Browse the repository at this point in the history
* feat: captcha use newest cacheManager component

* fix: baseDir is directory
  • Loading branch information
czy88840616 authored Jan 16, 2024
1 parent c3dd094 commit 2ffce0a
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 28 deletions.
2 changes: 1 addition & 1 deletion packages/captcha/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"license": "MIT",
"dependencies": {
"@midwayjs/cache": "^3.14.0",
"@midwayjs/cache-manager": "^3.14.0",
"mini-svg-data-uri": "1.4.4",
"nanoid": "3.3.7",
"svg-captcha": "1.4.0"
Expand Down
35 changes: 22 additions & 13 deletions packages/captcha/src/config/config.default.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import { CaptchaOptions } from '../interface';

export const captcha: CaptchaOptions = {
default: {
size: 4,
noise: 1,
width: 120,
height: 40,
export default {
captcha: {
default: {
size: 4,
noise: 1,
width: 120,
height: 40,
},
image: {
type: 'mixed',
},
formula: {},
text: {},
expirationTime: 3600,
idPrefix: 'midway:vc',
} as CaptchaOptions,
cacheManager: {
clients: {
captcha: {
store: 'memory',
},
},
},
image: {
type: 'mixed',
},
formula: {},
text: {},
expirationTime: 3600,
idPrefix: 'midway:vc',
};
2 changes: 1 addition & 1 deletion packages/captcha/src/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Configuration } from '@midwayjs/core';
import * as cacheComponent from '@midwayjs/cache';
import * as cacheComponent from '@midwayjs/cache-manager';
import * as DefaultConfig from './config/config.default';
@Configuration({
namespace: 'captcha',
Expand Down
24 changes: 15 additions & 9 deletions packages/captcha/src/service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { Provide, Inject, Config, Scope, ScopeEnum } from '@midwayjs/core';
import { CacheManager } from '@midwayjs/cache';
import {
Provide,
Config,
Scope,
ScopeEnum,
InjectClient,
} from '@midwayjs/core';
import { CachingFactory, MidwayCache } from '@midwayjs/cache-manager';
import * as svgCaptcha from 'svg-captcha';
import * as svgBase64 from 'mini-svg-data-uri';
import { nanoid } from 'nanoid';
Expand All @@ -13,11 +19,11 @@ import { letters, numbers } from './constants';
@Provide()
@Scope(ScopeEnum.Singleton)
export class CaptchaService {
@Inject()
cacheManager: CacheManager;
@InjectClient(CachingFactory, 'captcha')
protected captchaCaching: MidwayCache;

@Config('captcha')
captcha: CaptchaOptions;
protected captcha: CaptchaOptions;

async image(options?: ImageCaptchaOptions): Promise<{
id: string;
Expand Down Expand Up @@ -102,10 +108,10 @@ export class CaptchaService {

async set(text: string): Promise<string> {
const id = nanoid();
await this.cacheManager.set(
await this.captchaCaching.set(
this.getStoreId(id),
(text || '').toLowerCase(),
{ ttl: this.captcha.expirationTime }
this.captcha.expirationTime
);
return id;
}
Expand All @@ -115,11 +121,11 @@ export class CaptchaService {
return false;
}
const storeId = this.getStoreId(id);
const storedValue = await this.cacheManager.get(storeId);
const storedValue = await this.captchaCaching.get(storeId);
if (value.toLowerCase() !== storedValue) {
return false;
}
this.cacheManager.del(storeId);
await this.captchaCaching.del(storeId);
return true;
}

Expand Down
5 changes: 4 additions & 1 deletion packages/mock/src/creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ export async function create<
}

if (options.baseDir) {
if (!isAbsolute(options.baseDir)) {
options.baseDir = join(appDir, options.baseDir);
}
await loadModule(
join(`${options.baseDir}`, getFileNameWithSuffix('interface')),
{
Expand All @@ -139,7 +142,7 @@ export async function create<
}
);
} else if (appDir) {
options.baseDir = `${appDir}/src`;
options.baseDir = join(appDir, 'src');
await loadModule(
join(`${options.baseDir}`, getFileNameWithSuffix('interface')),
{
Expand Down
20 changes: 18 additions & 2 deletions site/docs/extensions/captcha.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ export class MainConfiguration {}
## 调用服务

```typescript
import { Controller, Inject } from '@midwayjs/core';
import { CaptchaService } from '@midwayjs/captcha';

@Controller('/')
export class HomeController {

Expand Down Expand Up @@ -255,9 +257,23 @@ export const captcha: CaptchaOptions = {

## 组件依赖

验证码的内容存储基于 `@midwayjs/cache` 组件,默认是在 `memory` 中存储,如果要替换为 `redis` 或其他服务,请参照 `@midwayjs/cache`[文档](/docs/extensions/cache),对 cache 进行配置。
验证码的内容存储基于 `@midwayjs/cache-manager` 组件,默认创建了一个名为 `captcha` 的缓存实例,将数据存储在 `memory` 中。

```typescript
export default {
cacheManager: {
clients: {
captcha: {
store: 'memory',
},
},
},
};
```

如果要替换为 `redis` 或其他服务,请参照 `@midwayjs/cache-manager`[文档](/docs/extensions/caching),对 cache 进行配置。


`@midwayjs/cache` 组件已在 `@midwayjs/captcha` 组件 `package.json``dependencies` 中,无需额外再次安装。


## 效果
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ export class MainConfiguration {}
## Call service

```typescript
import { Controller, Inject } from '@midwayjs/core';
import { CaptchaService } from '@midwayjs/captcha';

@Controller('/')
export class HomeController {

@Inject()
ctx;

Expand Down Expand Up @@ -254,6 +255,28 @@ export const captcha: CaptchaOptions = {
}
```



## Component Dependency

The content storage of the verification code is based on the '@ midwayjs/cache-manager' component. By default, a cache instance named 'captcha' is created and the data is stored in 'memory.

```typescript
export default {
cacheManager: {
clients: {
captcha: {
store: 'memory',
},
},
},
};
```

If you want to replace it with 'redis' or other services, please refer to the [documentation](/docs/extensions/caching) of `@midwayjs/cache-manager` to configure the cache.



## Effect

**Picture verification code**
Expand Down

0 comments on commit 2ffce0a

Please sign in to comment.