jinbi-utils
Version:
这是一个实用工具库,包含了多个常用的功能模块。以下是各个模块的详细说明:
190 lines (135 loc) • 4.58 kB
Markdown
# jbwy-utils 使用说明
这是一个实用工具库,包含了多个常用的功能模块。以下是各个模块的详细说明:
## 1. Common 模块 (common/index.ts)
通用函数集合,提供了一些基础工具方法:
### 1.1 判空相关
```typescript
import { isEmpty } from 'jbwy-utils/common'
isEmpty('') // 返回 true
isEmpty(null) // 返回 true
isEmpty(undefined) // 返回 true
isEmpty(12323) // 返回 false
```
### 1.2 环境判断
```typescript
import { getDeviceType, getEnvironment, getIsComWx } from 'jbwy-utils/common'
// 获取设备类型
const deviceInfo = getDeviceType()
// 返回: { isWxWork, isWeixin, isMobileScreen, isMobileAny }
// 获取运行环境
const env = getEnvironment()
// 返回: 'com-wx-mobile' | 'com-wx-pc' | 'wx-mobile' | 'wx-pc' | 'other'
// 判断是否企业微信环境
const isComWx = getIsComWx()
```
### 1.3 URL参数处理
```typescript
import { getQueryString, getQueryVariable } from 'jbwy-utils/common'
// 获取URL参数
getQueryString(url, 'key')
getQueryVariable('key')
```
## 2. Number 模块 (number/index.ts)
数字处理相关函数:
```typescript
import { toThousands, formatFloat, ceil, floor } from 'jbwy-utils/number'
// 千分位格式化
toThousands(12345) // 返回 '12,345'
toThousands('12323.12') // 返回 '12,323.12'
// 格式化小数位
formatFloat('1.2345') // 返回 '1.23'
formatFloat('1.2345', 3) // 返回 '1.235'
// 向上取整
ceil('1.234', 2) // 返回 1.24
// 向下取整
floor('1.234', 2) // 返回 1.23
```
## 3. String 模块 (string/index.ts)
字符串处理相关函数:
```typescript
import { formatPhone, formatPhoneHide, formatBank } from 'jbwy-utils/string'
// 格式化手机号
formatPhone('18211572781') // 返回 '182 1157 2781'
formatPhone('18211572781', '-') // 返回 '182-1157-2781'
// 隐藏手机号中间四位
formatPhoneHide('18211572781') // 返回 '182****2781'
// 格式化银行卡号
formatBank('6282356862823568123') // 返回 '6282 3568 6282 3568 123'
```
## 4. Validate 模块 (validate/index.ts)
提供各种验证函数:
```typescript
import { isMobile, isEmail, isTelephone, isQQ } from 'jbwy-utils/validate'
// 验证手机号
isMobile('13800138000') // 返回 true/false
// 验证邮箱
isEmail('example@email.com') // 返回 true/false
// 验证固定电话
isTelephone('0755-88888888') // 返回 true/false
// 验证QQ号
isQQ('10000') // 返回 true/false
```
## 5. File 模块 (file/index.ts)
文件处理相关函数:
```typescript
import { calcFileSize, fileSizeFormat, compressImg } from 'jbwy-utils/file'
// 计算文件大小
calcFileSize(1024) // 返回 { size: 1, unit: 'KB' }
// 格式化文件大小
fileSizeFormat('1024', 'KB') // 返回 '1MB'
// 压缩图片
compressImg(file, scaleCallback, qualityCallback)
```
## 6. Object 模块 (object/index.ts)
对象处理相关函数:
```typescript
import { deepEqual, findNodePath } from 'jbwy-utils/object'
// 深度比较两个对象是否相等
deepEqual({a: 10}, {a: 10}) // 返回 true
deepEqual({a: 10}, {a: 10, b: 20}) // 返回 false
// 查找树结构节点路径
findNodePath(nodeValue, nodeKey, treeArray, childrenKey)
```
## 7. WeChat Work 模块 (wecom/wecom.ts)
企业微信相关功能:
```typescript
import { initWWConfig } from 'jbwy-utils/wecom'
// 初始化企业微信配置
initWWConfig()
```
## 8. middleware 模块 (middleware)
中间件相关功能:
### 8.1 requestLogger.middware.ts
日志服务 此插件支持使用了koa的nodejs项目
注意:(recordLogMiddleWare('project-plan-service'))需要传入项目名,日志文件会根据传入的项目名来 命名前缀
```typescript
// midway 项目接入方案 configuration.ts
import { recordLogMiddleWare } from 'jinbi-utils'
export class MainConfiguration {
@App('koa')
app: koa.Application;
async onReady() {
//
this.app.useMiddleware([recordLogMiddleWare('project-plan-service')]);
}
}
```
```typescript
// 只用了koa的nodejs项目接入方案 app.js
import { recordLogMiddleWare } from 'jinbi-utils'
const app = new Koa();
app.use(bodyParser());
app.use(router.routes()).use(router.allowedMethods()).use(recordLogMiddleWare('project-plan-service'));
```
## 安装和使用
1. 安装依赖:
```bash
npm install jbwy-utils
```
2. 引入使用:
```typescript
import { isEmpty } from 'jbwy-utils/common'
import { formatPhone } from 'jbwy-utils/string'
// ... 按需引入其他功能
```
注意:所有模块都支持按需引入,可以只引入需要使用的功能,减少打包体积。