slider-captcha-sdk 1.0.20 → 1.0.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +254 -37
- package/dist/README.md +254 -37
- package/dist/index.esm.js +1 -1303
- package/dist/index.min.js +1 -1
- package/dist/password-validator.esm.js +1 -342
- package/dist/password-validator.min.js +1 -1
- package/dist/slider-captcha.esm.js +1 -937
- package/dist/slider-captcha.min.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -3,17 +3,18 @@
|
|
|
3
3
|
[](https://badge.fury.io/js/slider-captcha-sdk)
|
|
4
4
|
[](https://opensource.org/licenses/MIT)
|
|
5
5
|
|
|
6
|
-
纯JavaScript滑块验证码SDK
|
|
6
|
+
纯JavaScript滑块验证码SDK和密码校验工具,无外部依赖,支持多种模块格式。
|
|
7
7
|
|
|
8
8
|
## ✨ 特性
|
|
9
9
|
|
|
10
|
-
- 🚀 **零依赖** - 纯JavaScript
|
|
11
|
-
- 📱 **响应式** - 完美支持移动端和桌面端
|
|
10
|
+
- 🚀 **零依赖** - 纯JavaScript实现,无外部依赖(除密码校验需要JSEncrypt)
|
|
12
11
|
- 🎨 **可定制** - 丰富的配置选项和样式定制
|
|
13
12
|
- 📦 **多格式** - 支持UMD、ES Module、CommonJS
|
|
14
13
|
- 💪 **TypeScript** - 完整的TypeScript类型定义
|
|
15
14
|
- 🔧 **易集成** - 简单的API,快速集成
|
|
16
15
|
- 🛡️ **安全可靠** - 防暴力破解,多重验证
|
|
16
|
+
- 🔐 **密码校验** - 内置RSA加密密码校验功能
|
|
17
|
+
- ⚡ **高性能** - 优化的代码结构和缓存机制
|
|
17
18
|
|
|
18
19
|
## 📦 安装
|
|
19
20
|
|
|
@@ -29,9 +30,11 @@ yarn add slider-captcha-sdk
|
|
|
29
30
|
|
|
30
31
|
## 🚀 快速开始
|
|
31
32
|
|
|
32
|
-
###
|
|
33
|
+
### 滑块验证码
|
|
34
|
+
|
|
35
|
+
#### ES Module
|
|
33
36
|
```javascript
|
|
34
|
-
import { PopupSliderCaptcha}
|
|
37
|
+
import { PopupSliderCaptcha } from 'slider-captcha-sdk'
|
|
35
38
|
|
|
36
39
|
const captcha = new PopupSliderCaptcha({
|
|
37
40
|
apiUrl: '/api/captcha',
|
|
@@ -48,11 +51,11 @@ const captcha = new PopupSliderCaptcha({
|
|
|
48
51
|
captcha.show()
|
|
49
52
|
```
|
|
50
53
|
|
|
51
|
-
|
|
54
|
+
#### UMD (浏览器直接引入)
|
|
52
55
|
```html
|
|
53
|
-
<script src="https://unpkg.com/slider-captcha-sdk/dist/
|
|
56
|
+
<script src="https://unpkg.com/slider-captcha-sdk/dist/index.min.js"></script>
|
|
54
57
|
<script>
|
|
55
|
-
const captcha = new
|
|
58
|
+
const captcha = new PopupSliderCaptcha({
|
|
56
59
|
apiUrl: '/api/captcha',
|
|
57
60
|
verifyUrl: '/api/captcha/verify'
|
|
58
61
|
})
|
|
@@ -60,78 +63,203 @@ captcha.show()
|
|
|
60
63
|
</script>
|
|
61
64
|
```
|
|
62
65
|
|
|
63
|
-
|
|
66
|
+
#### 使用baseUrl配置域名
|
|
67
|
+
```javascript
|
|
68
|
+
// 方式1:使用baseUrl自动拼接域名
|
|
69
|
+
const captcha = new PopupSliderCaptcha({
|
|
70
|
+
baseUrl: 'https://api.example.com',
|
|
71
|
+
apiUrl: '/api/captcha', // 自动拼接为 https://api.example.com/api/captcha
|
|
72
|
+
verifyUrl: '/api/captcha/verify' // 自动拼接为 https://api.example.com/api/captcha/verify
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
// 方式2:直接使用完整URL(优先级更高)
|
|
76
|
+
const captcha2 = new PopupSliderCaptcha({
|
|
77
|
+
baseUrl: 'https://api.example.com',
|
|
78
|
+
apiUrl: 'https://other-api.com/api/captcha', // 使用完整URL,不会拼接baseUrl
|
|
79
|
+
verifyUrl: '/api/captcha/verify' // 仍会拼接为 https://api.example.com/api/captcha/verify
|
|
80
|
+
})
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
#### CommonJS
|
|
64
84
|
```javascript
|
|
65
|
-
const
|
|
85
|
+
const { PopupSliderCaptcha } = require('slider-captcha-sdk')
|
|
66
86
|
|
|
67
|
-
const captcha = new
|
|
87
|
+
const captcha = new PopupSliderCaptcha()
|
|
68
88
|
captcha.show()
|
|
69
89
|
```
|
|
70
90
|
|
|
91
|
+
### 密码校验
|
|
92
|
+
|
|
93
|
+
#### ES Module
|
|
94
|
+
```javascript
|
|
95
|
+
import { PasswordValidator, validatePassword } from 'slider-captcha-sdk'
|
|
96
|
+
|
|
97
|
+
// 方式1:使用类
|
|
98
|
+
const validator = new PasswordValidator({
|
|
99
|
+
publicKeyUrl: '/api/getPublicKey',
|
|
100
|
+
validateUrl: '/api/validatePassword'
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
const result = await validator.validatePassword('password123', 'username')
|
|
104
|
+
|
|
105
|
+
// 方式2:使用便捷函数
|
|
106
|
+
const result = await validatePassword('password123', 'username', {
|
|
107
|
+
publicKeyUrl: '/api/getPublicKey',
|
|
108
|
+
validateUrl: '/api/validatePassword'
|
|
109
|
+
}, {
|
|
110
|
+
// additionalData 可选
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
// 方式3:使用baseUrl配置域名
|
|
114
|
+
const validator = new PasswordValidator({
|
|
115
|
+
baseUrl: 'https://api.example.com',
|
|
116
|
+
publicKeyUrl: '/microservice/strongPassword/getPublicKey', // 自动拼接为 https://api.example.com/microservice/strongPassword/getPublicKey
|
|
117
|
+
validateUrl: '/microservice/strongPassword/checkPassword' // 自动拼接为 https://api.example.com/microservice/strongPassword/checkPassword
|
|
118
|
+
})
|
|
119
|
+
```
|
|
120
|
+
|
|
71
121
|
## 📖 API文档
|
|
72
122
|
|
|
73
|
-
###
|
|
123
|
+
### 滑块验证码 (PopupSliderCaptcha)
|
|
124
|
+
|
|
125
|
+
#### 配置选项
|
|
74
126
|
|
|
75
127
|
| 参数 | 类型 | 默认值 | 描述 |
|
|
76
128
|
|------|------|--------|---------|
|
|
77
|
-
| `width` | `number` | `
|
|
78
|
-
| `height` | `number` | `
|
|
79
|
-
| `sliderSize` | `number` | `
|
|
129
|
+
| `width` | `number` | `320` | 验证码宽度 |
|
|
130
|
+
| `height` | `number` | `155` | 验证码高度 |
|
|
131
|
+
| `sliderSize` | `number` | `38` | 滑块大小 |
|
|
80
132
|
| `maxRetries` | `number` | `3` | 最大重试次数 |
|
|
81
133
|
| `timeout` | `number` | `30000` | 超时时间(ms) |
|
|
82
134
|
| `apiUrl` | `string` | `'/api/captcha'` | 获取验证码接口 |
|
|
83
135
|
| `verifyUrl` | `string` | `'/api/captcha/verify'` | 验证接口 |
|
|
136
|
+
| `baseUrl` | `string` | `''` | 基础域名,如果apiUrl和verifyUrl不包含域名则拼接此域名 |
|
|
137
|
+
| `throttleDelay` | `number` | `16` | 节流延迟(ms) |
|
|
138
|
+
| `clickMaskClose` | `boolean` | `false` | 点击遮罩关闭 |
|
|
84
139
|
| `onSuccess` | `function` | `undefined` | 验证成功回调 |
|
|
85
140
|
| `onFail` | `function` | `undefined` | 验证失败回调 |
|
|
86
141
|
| `onClose` | `function` | `undefined` | 关闭回调 |
|
|
87
142
|
|
|
88
|
-
|
|
143
|
+
#### 实例方法
|
|
89
144
|
|
|
90
|
-
|
|
145
|
+
##### `show(): Promise<any>`
|
|
91
146
|
显示验证码弹窗
|
|
92
147
|
|
|
93
|
-
|
|
148
|
+
##### `hide(): void`
|
|
94
149
|
隐藏验证码弹窗
|
|
95
150
|
|
|
96
|
-
|
|
151
|
+
##### `refresh(): Promise<void>`
|
|
97
152
|
刷新验证码
|
|
98
153
|
|
|
99
|
-
|
|
154
|
+
##### `destroy(): void`
|
|
100
155
|
销毁实例,清理所有事件监听器
|
|
101
156
|
|
|
102
|
-
####
|
|
103
|
-
|
|
157
|
+
#### 静态属性
|
|
158
|
+
|
|
159
|
+
##### `PopupSliderCaptcha.DEFAULTS`
|
|
160
|
+
默认配置对象
|
|
104
161
|
|
|
105
|
-
|
|
162
|
+
##### `PopupSliderCaptcha.CSS_CLASSES`
|
|
163
|
+
CSS类名映射
|
|
106
164
|
|
|
107
|
-
|
|
108
|
-
|
|
165
|
+
##### `PopupSliderCaptcha.ERROR_TYPES`
|
|
166
|
+
错误类型枚举
|
|
109
167
|
|
|
110
|
-
|
|
111
|
-
|
|
168
|
+
### 密码校验 (PasswordValidator)
|
|
169
|
+
|
|
170
|
+
#### 配置选项
|
|
171
|
+
|
|
172
|
+
| 参数 | 类型 | 默认值 | 描述 |
|
|
173
|
+
|------|------|--------|---------|
|
|
174
|
+
| `publicKeyUrl` | `string` | `'/microservice/strongPassword/getPublicKey'` | 获取公钥接口 |
|
|
175
|
+
| `validateUrl` | `string` | `'/microservice/strongPassword/checkPassword'` | 密码校验接口 |
|
|
176
|
+
| `baseUrl` | `string` | `''` | 基础域名,如果URL不包含域名则拼接此域名 |
|
|
177
|
+
| `timeout` | `number` | `10000` | 超时时间(ms) |
|
|
178
|
+
| `headers` | `object` | `{}` | 请求头 |
|
|
179
|
+
| `cacheDuration` | `number` | `300000` | 缓存时长(ms) |
|
|
180
|
+
|
|
181
|
+
#### 实例方法
|
|
182
|
+
|
|
183
|
+
##### `validatePassword(password, userName, additionalData): Promise<Object>`
|
|
184
|
+
校验密码
|
|
185
|
+
|
|
186
|
+
**参数:**
|
|
187
|
+
- `password` (string): 密码
|
|
188
|
+
- `userName` (string): 用户名
|
|
189
|
+
- `additionalData` (object): 额外数据(可选)
|
|
190
|
+
|
|
191
|
+
**返回值:**
|
|
192
|
+
```javascript
|
|
193
|
+
{
|
|
194
|
+
success: boolean,
|
|
195
|
+
data: any,
|
|
196
|
+
message: string,
|
|
197
|
+
code: string,
|
|
198
|
+
originalResponse: object
|
|
199
|
+
}
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
##### `encryptPassword(password, publicKey): string`
|
|
203
|
+
加密密码
|
|
204
|
+
|
|
205
|
+
##### `getPublicKey(): Promise<string>`
|
|
206
|
+
获取公钥
|
|
207
|
+
|
|
208
|
+
##### `destroy(): void`
|
|
209
|
+
销毁实例
|
|
210
|
+
|
|
211
|
+
#### 静态方法
|
|
212
|
+
|
|
213
|
+
##### `validatePassword(password, userName, options, additionalData): Promise<Object>`
|
|
214
|
+
便捷的密码校验函数
|
|
215
|
+
|
|
216
|
+
**参数:**
|
|
217
|
+
- `password` (string): 密码
|
|
218
|
+
- `userName` (string): 用户名
|
|
219
|
+
- `options` (object): 配置选项
|
|
220
|
+
- `additionalData` (object): 额外数据(可选)
|
|
112
221
|
|
|
113
222
|
## 🔧 服务端接口
|
|
114
223
|
|
|
115
|
-
###
|
|
224
|
+
### 滑块验证码接口
|
|
225
|
+
|
|
226
|
+
#### 获取验证码 (POST /api/captcha)
|
|
227
|
+
|
|
228
|
+
**请求格式:**
|
|
229
|
+
```json
|
|
230
|
+
{
|
|
231
|
+
"timestamp": 1640995200000
|
|
232
|
+
}
|
|
233
|
+
```
|
|
116
234
|
|
|
117
235
|
**响应格式:**
|
|
118
236
|
```json
|
|
119
237
|
{
|
|
120
|
-
"
|
|
121
|
-
"
|
|
122
|
-
|
|
123
|
-
|
|
238
|
+
"success": true,
|
|
239
|
+
"data": {
|
|
240
|
+
"id": "captcha_id",
|
|
241
|
+
"backgroundImage": "data:image/png;base64,...",
|
|
242
|
+
"sliderImage": "data:image/png;base64,...",
|
|
243
|
+
"y": 120,
|
|
244
|
+
"nonceStr": "random_string"
|
|
245
|
+
}
|
|
124
246
|
}
|
|
125
247
|
```
|
|
126
248
|
|
|
127
|
-
|
|
249
|
+
#### 验证接口 (POST /api/captcha/verify)
|
|
128
250
|
|
|
129
251
|
**请求格式:**
|
|
130
252
|
```json
|
|
131
253
|
{
|
|
132
|
-
"
|
|
133
|
-
|
|
134
|
-
|
|
254
|
+
"loginVo": {
|
|
255
|
+
"nonceStr": "random_string",
|
|
256
|
+
"value": 200
|
|
257
|
+
},
|
|
258
|
+
"dragEventList": [
|
|
259
|
+
{"time": 100, "position": 50},
|
|
260
|
+
{"time": 200, "position": 100},
|
|
261
|
+
{"time": 300, "position": 200}
|
|
262
|
+
]
|
|
135
263
|
}
|
|
136
264
|
```
|
|
137
265
|
|
|
@@ -140,7 +268,43 @@ captcha.show()
|
|
|
140
268
|
{
|
|
141
269
|
"success": true,
|
|
142
270
|
"message": "验证成功",
|
|
143
|
-
"
|
|
271
|
+
"data": {
|
|
272
|
+
"token": "verification_token"
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
### 密码校验接口
|
|
278
|
+
|
|
279
|
+
#### 获取公钥 (GET /microservice/strongPassword/getPublicKey)
|
|
280
|
+
|
|
281
|
+
**响应格式:**
|
|
282
|
+
```json
|
|
283
|
+
{
|
|
284
|
+
"success": true,
|
|
285
|
+
"data": "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA...\n-----END PUBLIC KEY-----"
|
|
286
|
+
}
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
#### 密码校验 (POST /microservice/strongPassword/checkPassword)
|
|
290
|
+
|
|
291
|
+
**请求格式:**
|
|
292
|
+
```json
|
|
293
|
+
{
|
|
294
|
+
"userName": "username",
|
|
295
|
+
"password": "encrypted_password",
|
|
296
|
+
"timestamp": 1640995200000
|
|
297
|
+
}
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
**响应格式:**
|
|
301
|
+
```json
|
|
302
|
+
{
|
|
303
|
+
"success": true,
|
|
304
|
+
"message": "密码校验成功",
|
|
305
|
+
"data": {
|
|
306
|
+
"valid": true
|
|
307
|
+
}
|
|
144
308
|
}
|
|
145
309
|
```
|
|
146
310
|
|
|
@@ -149,6 +313,7 @@ captcha.show()
|
|
|
149
313
|
你可以通过CSS覆盖默认样式:
|
|
150
314
|
|
|
151
315
|
```css
|
|
316
|
+
/* 使用CSS类名 */
|
|
152
317
|
.slider-captcha-modal {
|
|
153
318
|
border-radius: 12px;
|
|
154
319
|
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
|
|
@@ -157,6 +322,48 @@ captcha.show()
|
|
|
157
322
|
.slider-captcha-btn {
|
|
158
323
|
background: linear-gradient(45deg, #667eea 0%, #764ba2 100%);
|
|
159
324
|
}
|
|
325
|
+
|
|
326
|
+
/* 或者使用CSS变量 */
|
|
327
|
+
:root {
|
|
328
|
+
--sc-primary: #409eff;
|
|
329
|
+
--sc-success: #67c23a;
|
|
330
|
+
--sc-danger: #f56c6c;
|
|
331
|
+
--sc-border: #e4e7eb;
|
|
332
|
+
--sc-bg: linear-gradient(90deg, #f7f9fa 0%, #e8f4fd 100%);
|
|
333
|
+
--sc-text: #333;
|
|
334
|
+
--sc-text-light: #999;
|
|
335
|
+
--sc-shadow: 0 4px 20px rgba(0,0,0,.3);
|
|
336
|
+
--sc-radius: 8px;
|
|
337
|
+
--sc-transition: .3s ease;
|
|
338
|
+
}
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
## 🔧 开发
|
|
342
|
+
|
|
343
|
+
### 安装依赖
|
|
344
|
+
```bash
|
|
345
|
+
npm install
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
### 开发模式
|
|
349
|
+
```bash
|
|
350
|
+
npm run dev
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
### 构建
|
|
354
|
+
```bash
|
|
355
|
+
npm run build:prod
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
### 代码检查
|
|
359
|
+
```bash
|
|
360
|
+
npm run lint
|
|
361
|
+
npm run lint:fix
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
### 完整检查
|
|
365
|
+
```bash
|
|
366
|
+
npm run check
|
|
160
367
|
```
|
|
161
368
|
|
|
162
369
|
## 📄 许可证
|
|
@@ -170,3 +377,13 @@ MIT License - 详见 [LICENSE](LICENSE) 文件
|
|
|
170
377
|
## 📞 支持
|
|
171
378
|
|
|
172
379
|
如果你觉得这个项目有用,请给它一个 ⭐️
|
|
380
|
+
|
|
381
|
+
## 📝 更新日志
|
|
382
|
+
|
|
383
|
+
### v1.0.20
|
|
384
|
+
- ✅ 添加密码校验功能
|
|
385
|
+
- ✅ 优化代码结构和性能
|
|
386
|
+
- ✅ 添加ESLint配置
|
|
387
|
+
- ✅ 完善TypeScript类型定义
|
|
388
|
+
- ✅ 修复并发请求问题
|
|
389
|
+
- ✅ 优化打包配置
|